-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathtest_problem.py
More file actions
65 lines (49 loc) · 1.72 KB
/
test_problem.py
File metadata and controls
65 lines (49 loc) · 1.72 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
from constraint import Constraint, Domain, Problem
def test_addVariable_support_domain_subclasses():
class MyCustomDomain(Domain):
pass
class MyConstraint(Constraint):
def __call__(self, variables, domains, assignments, forwardcheck=False):
assert isinstance(domains["x"], Domain)
assert isinstance(domains["y"], MyCustomDomain)
return True
problem = Problem()
problem.addVariable("x", [0, 1])
problem.addVariable("y", MyCustomDomain([0, 1]))
problem.addConstraint(MyConstraint())
solution = problem.getSolution()
possible_solutions = [
{"x": 0, "y": 0},
{"x": 0, "y": 1},
{"x": 1, "y": 0},
{"x": 1, "y": 1},
]
assert solution in possible_solutions
def test_addVariable_string_constraint():
problem = Problem()
problem.addVariable("x", [0, 1])
problem.addVariable("y", [0, 1])
problem.addConstraint("x != y")
solutions = problem.getSolutions()
possible_solutions = [
{"x": 0, "y": 1},
{"x": 1, "y": 0},
]
assert len(solutions) == len(possible_solutions)
for solution in solutions:
assert solution in possible_solutions
assert solution in possible_solutions
def test_addVariable_string_constraints():
problem = Problem()
problem.addVariable("x", [0, 1, 2])
problem.addVariable("y", [0, 1, 2])
problem.addConstraint(["x >= 1", "y > 0", "x != y"])
solutions = problem.getSolutions()
possible_solutions = [
{"x": 1, "y": 2},
{"x": 2, "y": 1},
]
assert len(solutions) == len(possible_solutions)
for solution in solutions:
assert solution in possible_solutions
assert solution in possible_solutions