-
Notifications
You must be signed in to change notification settings - Fork 0
/
csp_graphcolor.py
48 lines (44 loc) · 1.97 KB
/
csp_graphcolor.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
class GraphColorCSP:
"""
Variables is list of region names.
Colors is list of colors (this will be domain for all variables).
Adjacency is dictionary of key, val where key is a variable and value is list of its neighbor variables.
"""
def __init__(self, variables, colors, adjacency):
self.variables = variables
self.domains = {}
for var in self.variables:
self.domains[var] = [c for c in colors]
self.adjacency = adjacency
# checks constraint between two variables. for graph-color this is inequality constraint.
def constraint_consistent(self, var1, val1, var2, val2):
if var2 in self.adjacency[var1] and var1 in self.adjacency[var2]: # neighbors
return val1 != val2
else: # not neighbors
return True
# to check if a partial assignment is consistent,
# we check if assigned variables and their assigned neighbors are consistently assigned.
def check_partial_assignment(self, assignment):
if assignment is None:
return False
for var in assignment:
assigned_neighbors = [n for n in self.adjacency[var] if n in assignment]
for n in assigned_neighbors:
if not self.constraint_consistent(var, assignment[var], n, assignment[n]):
return False
return True
# a solution is (1) completely assigned variables and (2) consistently assigned.
def is_goal(self, assignment):
if assignment is None:
return False
# check complete assignment
for var in self.variables:
if var not in assignment:
return False
# check consistency
for var in self.variables:
neighbors = self.adjacency[var]
for n in neighbors:
if not self.constraint_consistent(var, assignment[var], n, assignment[n]):
return False
return True