-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsolver.py
247 lines (209 loc) · 7.61 KB
/
solver.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
class Variable:
def __init__(self, direction, row, col, length, domain):
self.word = ""
self.direction = direction
self.row = row
self.col = col
self.length = length
self.domain = set(domain)
self.removed_domain = {}
def print_board(boardstr, assignment):
board = boardstr.split("\n")
board = [list(row) for row in board]
for v in assignment:
val = assignment[v]
if v.direction == "horizontal":
for i in range(v.length):
board[v.row][v.col + i] = val[i]
else:
for i in range(v.length):
board[v.row + i][v.col] = val[i]
for row in board:
print(row)
def satisfy_constraint(V, assignment, Vx, val):
for v in V:
Cxv = create_constraint(Vx, v)
if v != Vx and v in assignment and Cxv:
if val[Cxv[0]] != assignment[v][Cxv[1]]:
return False
return True
def select_unassigned_variable(V, assignment):
unassigned = []
for v in V:
if v not in assignment:
unassigned.append(v)
unassigned.sort(key=lambda x: len(x.domain))
return unassigned[0]
def reduce_domain(V, assignment, Vx, val):
for v in V:
Cxv = create_constraint(Vx, v)
if v != Vx and v not in assignment and Cxv:
elements_to_remove = set()
for word in v.domain:
if val[Cxv[0]] != word[Cxv[1]]:
elements_to_remove.add(word)
v.domain.difference_update(elements_to_remove)
if v not in Vx.removed_domain:
Vx.removed_domain[v] = []
Vx.removed_domain[v].extend(elements_to_remove)
def restore_domain(V, assignment, Vx, val):
for v in V:
Cxv = create_constraint(Vx, v)
if v != Vx and v not in assignment and Cxv:
if v in Vx.removed_domain:
for word in Vx.removed_domain[v]:
if val[Cxv[0]] != word[Cxv[1]]:
v.domain.add(word)
Vx.removed_domain[v] = []
def backtrack(V, assignment):
if len(assignment) == len(V):
return True
Vx = select_unassigned_variable(V, assignment)
for val in Vx.domain:
if val in assignment.values():
continue
if satisfy_constraint(V, assignment, Vx, val):
assignment[Vx] = val
reduce_domain(V, assignment, Vx, val)
result = backtrack(V, assignment)
if result:
return True
assignment.pop(Vx, None)
restore_domain(V, assignment, Vx, val)
return False
def revise(Vx, Vy, Cxy):
if not Cxy:
return False
revised = False
elements_to_remove = set()
for x in Vx.domain:
satisfied = False
for y in Vy.domain:
if x[Cxy[0]] == y[Cxy[1]]:
satisfied = True
break
if not satisfied:
elements_to_remove.add(x)
revised = True
Vx.domain.difference_update(elements_to_remove)
return revised
def arc_consistency_3(S):
for s in S:
X, Y, Cxy = s
revise(X, Y, Cxy)
def create_constraint(Vx, Vy):
constraint = ()
if Vx.direction != Vy.direction:
if Vx.direction == "horizontal":
if Vy.col >= Vx.col and Vy.col <= Vx.col + Vx.length - 1:
if Vx.row >= Vy.row and Vx.row <= Vy.row + Vy.length - 1:
constraint = (Vy.col - Vx.col, Vx.row - Vy.row)
else:
if Vy.row >= Vx.row and Vy.row <= Vx.row + Vx.length - 1:
if Vx.col >= Vy.col and Vx.col <= Vy.col + Vy.length - 1:
constraint = (Vy.row - Vx.row, Vx.col - Vy.col)
return constraint
def create_arc(V):
arcs = []
for i in range(len(V)):
for j in range(i + 1, len(V)):
if i != j:
Cij = create_constraint(V[i], V[j])
if len(Cij) > 0:
arcs.append((V[i], V[j], Cij))
return arcs
def create_variables(boardstr, words):
variables = []
board = boardstr.split("\n")
for row in range(len(board)):
for col in range(len(board[row])):
if board[row][col] == "-":
if col == 0 or board[row][col - 1] == "#":
length = 0
for i in range(col, len(board[row])):
if board[row][i] == "-":
length += 1
else:
break
if length == 1:
cond = True
try:
if board[row][col + 1] == "-":
cond = False
except IndexError:
pass
try:
if board[row][col - 1] == "-" and col - 1 >= 0:
cond = False
except IndexError:
pass
try:
if board[row - 1][col] == "-" and row - 1 >= 0:
cond = False
except IndexError:
pass
try:
if board[row + 1][col] == "-":
cond = False
except IndexError:
pass
if cond:
domain = []
for word in words:
if len(word) == length:
domain.append(word)
variables.append(Variable(
"horizontal",
row,
col,
length,
domain
))
if length > 1:
domain = []
for word in words:
if len(word) == length:
domain.append(word)
variables.append(Variable(
"horizontal",
row,
col,
length,
domain
))
if row == 0 or board[row - 1][col] == "#":
length = 0
for i in range(row, len(board)):
if board[i][col] == "-":
length += 1
else:
break
if length > 1:
domain = []
for word in words:
if len(word) == length:
domain.append(word)
variables.append(Variable(
"vertical",
row,
col,
length,
domain
))
return variables
def read_file(file_path):
with open(file_path) as file:
return file.read()
def main():
assignment = {}
boardstr = read_file("crossword.txt")
words = read_file("words.txt").splitlines()
words = [word.upper() for word in words]
V = create_variables(boardstr, words)
S = create_arc(V)
arc_consistency_3(S)
V.sort(key=lambda x: len(x.domain))
backtrack(V, assignment)
print_board(boardstr, assignment)
if __name__ == "__main__":
main()