-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathtest_constraint.py
More file actions
145 lines (119 loc) · 3.89 KB
/
Copy pathtest_constraint.py
File metadata and controls
145 lines (119 loc) · 3.89 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import constraint
from examples.abc import abc
from examples.coins import coins
# from examples.crosswords import crosswords
from examples.einstein import einstein
from examples.queens import queens
from examples.rooks import rooks
from examples.studentdesks import studentdesks
# from examples.sudoku import sudoku
# from examples.wordmath import (seisseisdoze, sendmoremoney, twotwofour)
# from examples.xsum import xsum
def test_abc():
solutions = abc.solve()
minvalue, minsolution = solutions
assert minvalue == 10.473684210526315
assert minsolution == {'a': 1, 'b': 9, 'c': 9}
def test_coins():
solutions = coins.solve()
assert len(solutions) == 2
def test_einstein():
solutions = einstein.solve()
expected_solutions = [
{
"nationality2": "dane",
"nationality3": "brit",
"nationality1": "norwegian",
"nationality4": "german",
"nationality5": "swede",
"color1": "yellow",
"color3": "red",
"color2": "blue",
"color5": "white",
"color4": "green",
"drink4": "coffee",
"drink5": "beer",
"drink1": "water",
"drink2": "tea",
"drink3": "milk",
"smoke5": "bluemaster",
"smoke4": "prince",
"smoke3": "pallmall",
"smoke2": "blends",
"smoke1": "dunhill",
"pet5": "dogs",
"pet4": "fish",
"pet1": "cats",
"pet3": "birds",
"pet2": "horses",
}
]
assert solutions == expected_solutions
def test_queens():
solutions, size = queens.solve()
assert size == 8
for solution in solutions:
queens.showSolution(solution, size)
def test_rooks():
size = 8
solutions = rooks.solve(size)
assert len(solutions) == rooks.factorial(size)
def test_studentdesks():
solutions = studentdesks.solve()
expected_solutions = {
1: "A",
2: "E",
3: "D",
4: "E",
5: "D",
6: "A",
7: "C",
8: "B",
9: "C",
10: "B",
11: "E",
12: "D",
13: "E",
14: "D",
15: "A",
16: "C",
17: "B",
18: "C",
19: "B",
20: "A",
}
assert solutions == expected_solutions
def test_constraint_without_variables():
problem = constraint.Problem()
problem.addVariable("a", [1, 2, 3])
problem.addConstraint(lambda a: a * 2 == 6)
solutions = problem.getSolutions()
assert solutions == [{"a": 3}]
def test_multipliers():
"""Test the multiplier functionality in the constraints."""
from constraint import MaxSumConstraint, ExactSumConstraint, MinSumConstraint
problem = constraint.Problem()
problem.addVariable("x", [-1, 0, 1, 2])
problem.addVariable("y", [1, 2])
problem.addConstraint(MaxSumConstraint(4, [2, 1]), ["x", "y"])
problem.addConstraint(ExactSumConstraint(4, [1, 2]), ["x", "y"])
problem.addConstraint(MinSumConstraint(0, [0.5, 1]), ["x"])
possible_solutions = [{"y": 2, "x": 0}, {"y": 1, "x": 2}]
# get the solutions
solutions = problem.getSolutions()
for solution in solutions:
assert solution in possible_solutions
def test_pruning_constraint():
"""Test that the VariableExactSumConstraint prunes the domain correctly."""
from constraint import VariableExactSumConstraint
problem = constraint.Problem()
problem.addVariable("a", [1, 2, -1, 3])
problem.addVariable("b", [1, 2, -1, 3])
problem.addVariable("c", [3])
problem.addConstraint(VariableExactSumConstraint("c", ["a", "b"]))
possible_solutions = [{"a": 2, "b": 1, "c": 3}, {"a": 1, "b": 2, "c": 3}]
# get the solutions
solutions = problem.getSolutions()
assert len(solutions) == len(possible_solutions)
for solution in solutions:
assert solution in possible_solutions