-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConstraintNetwork.py
195 lines (156 loc) · 5.65 KB
/
ConstraintNetwork.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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import Variable
import Constraint
import SudokuBoard
from math import floor
"""
CSP representation of the problem. Contains the variables, constraints, and
many helpful accessors.
"""
class ConstraintNetwork:
# ==================================================================
# Constructors
# ==================================================================
def __init__ ( self, sboard = None ):
self.constraints = []
self.variables = []
if sboard != None:
board = sboard.board
temp = []
value = 0
for i in range(sboard.N):
for j in range(sboard.N):
value = board[i][j]
domain = []
if value == 0:
d = 1
while d <= sboard.N:
domain.append(d)
d += 1
else:
domain.append(value)
block = int(((floor(i/sboard.p) * sboard.p) + floor(j/sboard.q)))
temp.append(Variable.Variable(domain,i,j,block))
rows = dict()
cols = dict()
blocks = dict()
for v in temp:
row = v.row
col = v.col
block = v.block
if not (row in rows.keys()):
rows[row] = []
if not (col in cols.keys()):
cols[col] = []
if not (block in blocks.keys()):
blocks[block] = []
rows[row].append(v)
cols[col].append(v)
blocks[block].append(v)
for v in temp:
self.addVariable(v)
for e in rows:
c = Constraint.Constraint()
for v in rows[e]:
c.addVariable(v)
self.addConstraint(c)
for e in cols:
c = Constraint.Constraint()
for v in cols[e]:
c.addVariable(v)
self.addConstraint(c)
for e in blocks:
c = Constraint.Constraint()
for v in blocks[e]:
c.addVariable(v)
self.addConstraint(c)
# ==================================================================
# Modifiers
# ==================================================================
def addConstraint ( self, c ):
if c not in self.constraints:
self.constraints.append( c )
def addVariable ( self, v ):
if v not in self.variables:
self.variables.append( v )
# ==================================================================
# Accessors
# ==================================================================
def getConstraints ( self ):
return self.constraints
def getVariables ( self ):
return self.variables
# Returns all variables that share a constraint with v
def getNeighborsOfVariable ( self, v ):
neighbors = set()
for c in self.constraints:
if c.contains( v ):
for x in c.vars:
neighbors.add( x )
neighbors.remove( v )
return list( neighbors )
# Returns true is every constraint is consistent
def isConsistent ( self ):
for c in self.constraints:
if not c.isConsistent():
return False
return True
# Returns a list of constraints that contains v
def getConstraintsContainingVariable ( self, v ):
"""
@param v variable to check
@return list of constraints that contains v
"""
outList = []
for c in self.constraints:
if c.contains( v ):
outList.append( c )
return outList
"""
Returns the constraints that contain variables whose domains were
modified since the last call to this method.
After getting the constraints, it will reset each variable to
unmodified
Note* The first call to this method returns the constraints containing
the initialized variables.
"""
def getModifiedConstraints ( self ):
mConstraints = []
for c in self.constraints:
if c.isModified():
mConstraints.append( c )
for v in self.variables:
v.setModified( False )
return mConstraints
# ==================================================================
# String Representation
# ==================================================================
def __str__ ( self ):
output = str(len(self.variables)) + " Variables: {"
delim = ""
for v in self.variables:
output += delim + v.name
delim = ","
output += "}"
output += "\n" + str(len(self.constraints)) + " Constraints:"
delim = "\n"
for c in self.constraints:
output += delim + str(c)
output += "\n"
for v in self.variables:
output += str(v) + "\n"
return output
# ==================================================================
# Sudoku Board Representation
# ==================================================================
def toSudokuBoard ( self, p, q ):
n = p*q
board = [[ 0 for j in range( n )] for i in range( n )]
row = 0
col = 0
for v in self.variables:
board[row][col] = v.getAssignment()
col += 1
if col == n:
col = 0
row += 1
return SudokuBoard.SudokuBoard( p, q, board = board )