module special.einstein_prolog#

Inheritance diagram of ensae_teaching_cs.special.einstein_prolog

Short summary#

module ensae_teaching_cs.special.einstein_prolog

This programs solves Einstein’s riddle ou en Français Intégramme. The algorithm is based on logic and its clause.

source on GitHub

Classes#

class

truncated documentation

Enigma

this class solves the enigma

Rule

This class defines a constraint of the problem or a clause (see http://en.wikipedia.org/wiki/Clause_(logic)) …

RuleAvant

p1 before p2

RuleEnsemble

permutation of the elements of a category

RuleEquivalence

p1 equivalent to p2

RulePosition

p1 at position

RuleVoisin

p1 and p2 are neighbors

Functions#

function

truncated documentation

permutation

Compute all permutations of set [[ 1, 2, …, nb ]]. Example for 3:

Methods#

method

truncated documentation

__init__

we describe the enigma using the classes we defined above

__init__

__init__

__init__

__init__

__init__

__init__

__str__

usual

__str__

display

__str__

display

__str__

display

__str__

display

__str__

display

__str__

display

combine

combine two clauses, two cases : 1. nothing in common or everything in common –> concatenation of clauses …

combine

combine two clauses, two cases : 1. nothing in common or everything in common –> concatenation of clauses …

combine

combine two clauses, two cases : 1. nothing in common or everything in common –> concatenation of clauses …

combine

combine two clauses, two cases : 1. nothing in common or everything in common –> concatenation of clauses …

combine

combine two clauses, two cases : 1. nothing in common or everything in common –> concatenation of clauses …

combine

combine two clauses, two cases : 1. nothing in common or everything in common –> concatenation of clauses …

combine_cross_sets

combines two sets of clauses

combine_cross_sets

combines two sets of clauses

combine_cross_sets

combines two sets of clauses

combine_cross_sets

combines two sets of clauses

combine_cross_sets

combines two sets of clauses

combine_cross_sets

combines two sets of clauses

find

finds a clause in the different sets of clause (houses, colors, …)

genere

Generates all possible clauses (list of lists) (l [0][0] et l [0][1]) ou (l [1][0] et l [1][1]), a clause …

genere

overrides method genere

genere

overrides method genere

genere

overrides method genere

genere

overrides method genere

genere

overrides method genere

solve

Solves the enigma by eploring in deepness, the method is recursive

Documentation#

This programs solves Einstein’s riddle ou en Français Intégramme. The algorithm is based on logic and its clause.

source on GitHub

class ensae_teaching_cs.special.einstein_prolog.Enigma(display=True)#

Bases : object

this class solves the enigma

source on GitHub

we describe the enigma using the classes we defined above

Paramètres:

display – if True, use print to print some information

source on GitHub

__init__(display=True)#

we describe the enigma using the classes we defined above

Paramètres:

display – if True, use print to print some information

source on GitHub

__str__()#

usual

source on GitHub

find(p)#

finds a clause in the different sets of clause (houses, colors, …)

Paramètres:

p – clause

Renvoie:

tuple (clause, position)

source on GitHub

solve(solution=None, logf=<built-in function print>)#

Solves the enigma by eploring in deepness, the method is recursive

Paramètres:

solution – [] empty at the beginning, recursively used then

Renvoie:

solution

source on GitHub

class ensae_teaching_cs.special.einstein_prolog.Rule#

Bases : object

This class defines a constraint of the problem or a clause (see http://en.wikipedia.org/wiki/Clause_(logic))

There are 5 different types of clauses described by Einstein’s enigma each of them is described by a different class. There are defined by classes: RulePosition, RuleEquivalence, RuleVoisin, RuleAvant, RuleEnsemble.

source on GitHub

__init__()#
__str__()#

display

source on GitHub

combine(cl1, cl2)#
combine two clauses, two cases :
  1. nothing in common or everything in common –> concatenation of clauses

  2. a position or a property in common –> null clause

Paramètres:
  • cl1 – clause 1

  • cl2 – clause 2

Renvoie:

the new clause

A clause is a Rule.

source on GitHub

combine_cross_sets(set1, set2)#

combines two sets of clauses

Paramètres:
  • set1 – set of clauses 1

  • set2 – set of clauses 2

Renvoie:

combination

source on GitHub

genere()#

Generates all possible clauses (list of lists) (l [0][0] et l [0][1]) ou (l [1][0] et l [1][1]), a clause is a triplet of (person, (property, category) )

source on GitHub

name#

name of the rule

set#

set of clauses

class ensae_teaching_cs.special.einstein_prolog.RuleAvant(p1, p2)#

Bases : Rule

p1 before p2

source on GitHub

__init__(p1, p2)#
genere()#

overrides method genere

source on GitHub

class ensae_teaching_cs.special.einstein_prolog.RuleEnsemble(sets, categorie)#

Bases : Rule

permutation of the elements of a category

source on GitHub

__init__(sets, categorie)#
genere()#

overrides method genere

source on GitHub

class ensae_teaching_cs.special.einstein_prolog.RuleEquivalence(p1, p2)#

Bases : Rule

p1 equivalent to p2

source on GitHub

__init__(p1, p2)#
genere()#

overrides method genere

source on GitHub

class ensae_teaching_cs.special.einstein_prolog.RulePosition(p1, pos)#

Bases : Rule

p1 at position

source on GitHub

__init__(p1, pos)#
genere()#

overrides method genere

source on GitHub

class ensae_teaching_cs.special.einstein_prolog.RuleVoisin(p1, p2)#

Bases : Rule

p1 and p2 are neighbors

source on GitHub

__init__(p1, p2)#
genere()#

overrides method genere

source on GitHub

ensae_teaching_cs.special.einstein_prolog.ensemble = [['jaune', 'bleu', 'rouge', 'blanc', 'vert'], ['danois', 'norvegien', 'anglais', 'allemand', 'suedois'], ['eau', 'the', 'lait', 'cafe', 'biere'], ['Dunhill', 'Blend', 'Pall Mall', 'Prince', 'Bluemaster'], ['chats', 'cheval', 'oiseaux', 'poisson', 'chiens']]#

all possibles values

ensae_teaching_cs.special.einstein_prolog.permutation(nb)#

Compute all permutations of set [[ 1, 2, …, nb ]]. Example for 3:

[[0, 1, 2], [0, 2, 1], [1, 0, 2],
[1, 2, 0], [2, 0, 1], [2, 1, 0]]
Paramètres:

nb – permutation over the set [[1..n]]

Renvoie:

list of all possible permutations

Avertissement

This method can be very long if nb is high (>10).

This function does something very similar to itertools.permutations.

source on GitHub

ensae_teaching_cs.special.einstein_prolog.ttanimal = ['chats', 'cheval', 'oiseaux', 'poisson', 'chiens']#

animal

ensae_teaching_cs.special.einstein_prolog.ttboisson = ['eau', 'the', 'lait', 'cafe', 'biere']#

drinks

ensae_teaching_cs.special.einstein_prolog.ttcigare = ['Dunhill', 'Blend', 'Pall Mall', 'Prince', 'Bluemaster']#

smoke brand

ensae_teaching_cs.special.einstein_prolog.ttcouleur = ['jaune', 'bleu', 'rouge', 'blanc', 'vert']#

definition of all possible values (French terms) colors

ensae_teaching_cs.special.einstein_prolog.ttnationalite = ['danois', 'norvegien', 'anglais', 'allemand', 'suedois']#

nationalities