module 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.
Classes#
class |
truncated documentation |
---|---|
this class solves the enigma |
|
This class defines a constraint of the problem or a clause (see http://en.wikipedia.org/wiki/Clause_(logic)) … |
|
p1 before p2 |
|
permutation of the elements of a category |
|
p1 equivalent to p2 |
|
p1 at position |
|
p1 and p2 are neighbors |
Functions#
function |
truncated documentation |
---|---|
Compute all permutations of set [[ 1, 2, …, nb ]]. Example for 3: |
Methods#
method |
truncated documentation |
---|---|
we describe the enigma using the classes we defined above |
|
usual |
|
display |
|
|
display |
|
display |
|
display |
|
display |
|
display |
combine two clauses, two cases : 1. nothing in common or everything in common –> concatenation of clauses … |
|
|
combine two clauses, two cases : 1. nothing in common or everything in common –> concatenation of clauses … |
|
combine two clauses, two cases : 1. nothing in common or everything in common –> concatenation of clauses … |
|
combine two clauses, two cases : 1. nothing in common or everything in common –> concatenation of clauses … |
|
combine two clauses, two cases : 1. nothing in common or everything in common –> concatenation of clauses … |
|
combine two clauses, two cases : 1. nothing in common or everything in common –> concatenation of clauses … |
combines two sets of clauses |
|
|
combines two sets of clauses |
|
combines two sets of clauses |
|
combines two sets of clauses |
|
combines two sets of clauses |
|
combines two sets of clauses |
finds a clause in the different sets of clause (houses, colors, …) |
|
Generates all possible clauses (list of lists) (l [0][0] et l [0][1]) ou (l [1][0] et l [1][1]), a clause … |
|
overrides method |
|
overrides method |
|
overrides method |
|
overrides method |
|
overrides method |
|
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.
- class ensae_teaching_cs.special.einstein_prolog.Enigma(display=True)#
Bases :
object
this class solves the enigma
we describe the enigma using the classes we defined above
- Paramètres:
display – if True, use print to print some information
- __init__(display=True)#
we describe the enigma using the classes we defined above
- Paramètres:
display – if True, use print to print some information
- __str__()#
usual
- find(p)#
finds a clause in the different sets of clause (houses, colors, …)
- Paramètres:
p – clause
- Renvoie:
tuple (clause, position)
- 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
- 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
.- __init__()#
- __str__()#
display
- combine(cl1, cl2)#
- combine two clauses, two cases :
nothing in common or everything in common –> concatenation of clauses
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
.
- combine_cross_sets(set1, set2)#
combines two sets of clauses
- Paramètres:
set1 – set of clauses 1
set2 – set of clauses 2
- Renvoie:
combination
- 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) )
- name#
name of the rule
- set#
set of clauses
- class ensae_teaching_cs.special.einstein_prolog.RuleAvant(p1, p2)#
Bases :
Rule
p1 before p2
- __init__(p1, p2)#
- genere()#
overrides method
genere
- class ensae_teaching_cs.special.einstein_prolog.RuleEnsemble(sets, categorie)#
Bases :
Rule
permutation of the elements of a category
- __init__(sets, categorie)#
- genere()#
overrides method
genere
- class ensae_teaching_cs.special.einstein_prolog.RuleEquivalence(p1, p2)#
Bases :
Rule
p1 equivalent to p2
- __init__(p1, p2)#
- genere()#
overrides method
genere
- class ensae_teaching_cs.special.einstein_prolog.RulePosition(p1, pos)#
Bases :
Rule
p1 at position
- __init__(p1, pos)#
- genere()#
overrides method
genere
- class ensae_teaching_cs.special.einstein_prolog.RuleVoisin(p1, p2)#
Bases :
Rule
p1 and p2 are neighbors
- __init__(p1, p2)#
- genere()#
overrides method
genere
- 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.
- 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