-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.py
180 lines (147 loc) · 5.63 KB
/
state.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
import queue
from random import randint
class AssignQueue:
assign_queue = queue.Queue(0)
@staticmethod
def push(value):
AssignQueue.assign_queue.put(value)
return True
@staticmethod
def pop():
value = AssignQueue.assign_queue.get()
return value
@staticmethod
def empty():
return AssignQueue.assign_queue.empty()
class Value:
def __init__(self, value, x, y):
self.val = value
self.x = x
self.y = y
class State:
def __init__(self, board):
ncells = len(board)
self.blockSize = ncells # blocks have dimension ncells x ncells
self.counter = 0
self.initBoard(board)
if not self.boardHasSolution():
raise Exception("No possible solutions can be found for this configuration")
def initBoard(self, board):
ncells = self.blockSize
self.cells = []
for i in range(0, ncells*ncells):
self.cells.append(['0',[str(i) for i in range(1,ncells+1)]])
for i in range(0,self.blockSize):
for j in range(0, self.blockSize):
if board[i][j] != '0':
value = Value(str(board[i][j]),i,j)
if not self.assignValue(value):
return False
return True
def boardHasSolution(self):
for i in range(0, self.blockSize):
for j in range(0, self.blockSize):
domainSet = self.cells[i * self.blockSize + j][1]
val = self.cells[i * self.blockSize + j][0]
if len(domainSet) == 0 and val == '0':
return False
return True
def assignValue(self, value):
if self.satisfyConstraints(value):
self.cells[value.x * self.blockSize + value.y] = [str(value.val),[]]
self.counter = self.counter + 1
return True
# Not valid! Value doesn't satisfy domain constraints!
return False
def satisfyConstraints(self, value):
return self.isEmpty(value.x,value.y) and self.squareConstraint(value) and self.propagateColumn(value) and self.propagateRow(value)
def propagateColumn(self, value):
columns = list(range(0, self.blockSize))
for j in columns:
if not self.removeConstraintValue(value.x, j, value):
return False
return True
def propagateRow(self, value):
rows = list(range(0, self.blockSize))
for i in rows:
if not self.removeConstraintValue(i, value.y, value):
return False
return True
def squareConstraint(self, value):
start_x = value.x - value.x%3
start_y = value.y - value.y%3
for i in range(start_x, start_x +3):
for j in range(start_y, start_y + 3):
if not self.removeConstraintValue(i,j,value):
return False
return True
def indirectConstraint(self):
indirect = False
for i in [0,3,6]:
for j in [0,3,6]:
occ = [Value(0, 0, 0)] * self.blockSize
for x in range(i,i+3):
for y in range(j,j+3):
domainSet = self.cells[x * self.blockSize + y][1]
for k in domainSet:
k = int(k)
occ[k-1] = Value(occ[k-1].val+1,x,y)
u = 0
valid = False
while u < self.blockSize and not valid:
if occ[u].val == 1:
self.assignValue(Value(str(u+1), occ[u].x, occ[u].y))
valid = indirect = True
u = u + 1
return indirect
def removeConstraintValue(self, i, j, value):
domainSet = self.cells[i * self.blockSize + j][1]
val = self.cells[i * self.blockSize + j][0]
if val == value.val:
return False
if value.val in domainSet:
domainSet.remove(value.val)
if len(domainSet) == 1:
assign = Value(domainSet[0],i,j)
AssignQueue.push(assign)
return True
def print(self):
for i in range(0,self.blockSize):
print("")
for j in range(0, self.blockSize):
print(str(self.cells[i*self.blockSize + j][0]), end = " ")
def printDomains(self):
for i in range(0,self.blockSize):
print("")
for j in range(0, self.blockSize):
print(str(self.cells[i*self.blockSize + j][1]), end = "\t\t")
def indexMinDomain(self):
min = 10
x, y = -1, -1
#print("\n===========(==========================\n")
for i in range(0,self.blockSize):
for j in range(0, self.blockSize):
domainSet = self.cells[i * self.blockSize + j][1]
l = len(domainSet)
if l < min and l > 0:
min = l
x = i
y = j
return x,y
def guess(self, i, j):
domainSet = self.cells[i * self.blockSize + j][1]
#val = domainSet[0] # Deterministic approach
r = randint(0, len(domainSet)-1) # random approach
val = domainSet[r]
domainSet.remove(val)
return Value(val, i, j)
def isUnique(self, i,j):
return len(self.cells[i * self.blockSize + j][1])== 1
def getDomainSet(self, i,j):
return self.cells[i * self.blockSize + j][1]
def getValue(self,i,j):
return self.cells[i * self.blockSize + j][0]
def isEmpty(self, i,j):
return (self.cells[i * self.blockSize + j][0] == '0')
def isFinish(self):
return self.counter == 81