ex2: init

This commit is contained in:
2025-09-23 12:47:42 +02:00
parent 8fbcaf4875
commit 4e65d200d3
8 changed files with 255 additions and 0 deletions

View File

@@ -0,0 +1,27 @@
# The map coloring problem from the text book.
# The CSP.backtrack() method needs to be implemented
from csp import CSP, alldiff
variables = ['WA', 'NT', 'Q', 'NSW', 'V', 'SA', 'T']
csp = CSP(
variables=variables,
domains={variable: {'red', 'green', 'blue'}
for variable in variables},
edges=[
('SA', 'WA'),
('SA', 'NT'),
('SA', 'Q'),
('SA', 'NSW'),
('SA', 'V'),
('WA', 'NT'),
('NT', 'Q'),
('Q', 'NSW'),
('NSW', 'V'),
],
)
print(csp.backtracking_search())
# Example output after implementing csp.backtracking_search():
# {'WA': 'red', 'NT': 'green', 'Q': 'red', 'NSW': 'green', 'V': 'red', 'SA': 'blue', 'T': 'red'}