-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
38 lines (30 loc) · 1.69 KB
/
main.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
import time
from csp.problem import Problem
from csp.solvers import MinConflictsSolver, RecursiveBacktrackingSolver
regions = ['BAV', 'BAD', 'SAAR', 'RHINE', 'WEST', 'HES', 'THUR', 'SAX', 'SAXAN', 'LOWSAX', 'HOLS', 'BRAND', 'MECK']
borders = [('BAV', 'BAD'), ('BAV', 'HES'), ('BAV', 'THUR'), ('BAV', 'SAX'), ('BAD', 'HES'), ('BAD', 'RHINE'),
('SAAR', 'RHINE'), ('RHINE', 'HES'), ('RHINE', 'WEST'), ('WEST', 'HES'), ('WEST', 'LOWSAX'), ('HES', 'THUR'),
('HES', 'LOWSAX'), ('THUR', 'SAX'), ('THUR', 'LOWSAX'), ('THUR', 'SAXAN'), ('SAX', 'SAXAN'), ('SAX', 'BRAND'),
('SAXAN', 'BRAND'), ('SAXAN', 'LOWSAX'), ('LOWSAX', 'BRAND'), ('LOWSAX', 'HOLS'), ('LOWSAX', 'MECK'),
('HOLS', 'MECK'), ('BRAND', 'MECK')]
colors = ["red", "blue", "green", "yellow"]
def check_border(variables, *args):
zipped = list(zip(variables, args))
return zipped[0][1] != zipped[1][1]
def solve_csp(solver):
problem = Problem(solver)
problem.add_variables(regions, colors)
for node in regions:
borders_per_node = [borders[index] for (index, a_tuple) in enumerate(borders) if a_tuple[0] == node]
if borders_per_node:
for border in borders_per_node:
problem.add_constraint(check_border, list(border))
start_time = time.time()
problem.get_solution()
end_time = (time.time() - start_time)
print(f"Solution with {solver.get_description()} took {end_time} sec and {solver.counter} checks")
problem.plot_map(borders)
if __name__ == "__main__":
solvers = [RecursiveBacktrackingSolver(forwardcheck=False), RecursiveBacktrackingSolver(forwardcheck=True), MinConflictsSolver()]
for solver in solvers:
solve_csp(solver)