This repository has been archived by the owner on Aug 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Constraint solver.py
77 lines (62 loc) · 2.94 KB
/
Constraint solver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#Note: Requires 'pip install python-constraint'
from constraint import AllDifferentConstraint, InSetConstraint, Problem
# variables
jobs = "FA EA PA RO AC".split()
deskcolor = "Aqua Maroon Pink Cream Purple".split()
travel = "Fiji France Canada Japan Thailand".split()
drinks = "Peppermint GreenTea Chamomile EarlGrey EnglishBreakfast".split()
suburb = "Brunswick, Werribee, Frankston, Oakleigh, StKilda".split(", ")
# There are five houses.
minn, maxn = 1, 5
problem = Problem()
# value of a variable is the number of a house with corresponding property
variables = jobs + deskcolor + travel + drinks + suburb
problem.addVariables(variables, range(minn, maxn+1))
# All jobs, colors, travel, drinks, and suburb are unique to each person
for vars_ in (jobs, deskcolor, travel, drinks, suburb):
problem.addConstraint(AllDifferentConstraint(), vars_)
# RULE 4: The cream desk is to the left of the purple desk.
#NOTE: interpret it as 'cream desk number' < 'purple desk number'
problem.addConstraint(lambda a,b: a < b, ["Cream", "Purple"])
# RULE 8: In the middle desk the drink is Chamomile
#NOTE: interpret "middle" in a numerical sense (not geometrical)
problem.addConstraint(InSetConstraint([(minn + maxn) // 2]), ["Chamomile"])
# RULE 9: The leftmost desk's job is Financial analyst.
#NOTE: interpret "the first" as the desk number.
problem.addConstraint(InSetConstraint([minn]), ["FA"])
def add_constraints(constraint, statements, variables=variables, problem=problem):
for stmt in (line for line in statements if line.strip()):
problem.addConstraint(constraint, [v for v in variables if v in stmt])
#Rules 1, 2, 3, 5, 6, 7, 12, 13
and_statements = """
The jobs PA deskcolor is Pink
The jobs RO travel is Japan
The jobs EA drinks is GreenTea
The deskcolor Cream drinks is EarlGrey
The suburb Werribee travel is France
The deskcolor Maroon suburb is Brunswick
The suburb StKilda drinks is EnglishBreakfast
The jobs AC suburb is Frankston
""".split("\n")
add_constraints(lambda a,b: a == b, and_statements)
#Rules 10, 11, 14, 15
nextto_statements = """
The suburb Oakleigh is next to the travel Fiji
The travel Canada is next to the suburb Brunswick
The jobs FA is next to deskcolor Aqua
The suburb Oakleigh is next to the drinks Peppermint
""".split("\n")
#XXX: what is "next to"? (linear, circular, two sides, 2D house arrangment)
add_constraints(lambda a,b: abs(a - b) == 1, nextto_statements)
def solve(variables=variables, problem=problem):
from itertools import groupby
from operator import itemgetter
# find & print solutions
for solution in problem.getSolutionIter():
for key, group in groupby(sorted(solution.items(), key=itemgetter(1)), key=itemgetter(1)):
print(key),
for v in sorted(dict(group).keys(), key=variables.index):
print(v.ljust(9)),
print()
if __name__ == '__main__':
solve()