-
Notifications
You must be signed in to change notification settings - Fork 0
/
takuzu_astar.py
461 lines (371 loc) · 16.2 KB
/
takuzu_astar.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
# takuzu.py: Template para implementação do projeto de Inteligência Artificial 2021/2022.
# Devem alterar as classes e funções neste ficheiro de acordo com as instruções do enunciado.
# Além das funções e classes já definidas, podem acrescentar outras que considerem pertinentes.
# Grupo 04:
# 99314 Raquel Cardoso
# 99287 Miguel Eleutério
from calendar import c
import sys
from numpy import size
from search import (
Problem,
Node,
astar_search,
breadth_first_tree_search,
depth_first_tree_search,
greedy_search,
compare_searchers
)
guaranteed_actions = list()
class Board:
"""Representação interna de um tabuleiro de Takuzu."""
def __init__(self, size, given_board, empty_positions):
self.size = size
self.board = given_board
self.empty_positions = empty_positions
def __str__(self) -> str:
to_print = ""
size = self.get_size()
for i in range(size):
line = self.get_row(i)
for j in range(size):
if j != size-1:
to_print += str(line[j]) + "\t"
elif i == size-1:
to_print += str(line[j])
else:
to_print += str(line[j]) + "\n"
return to_print
def get_board(self):
return self.board
def get_size(self):
return self.size
def get_empty_positions(self):
return self.empty_positions
def get_row(self, row: int):
return self.board[row]
def get_col(self, col: int):
return [line[col] for line in self.board]
def get_number(self, row: int, col: int):
return self.board[row][col]
def change_number(self, row: int, col: int, number: int):
self.board[row][col] = number
def adjacent_vertical_numbers(self, row: int, col: int):
"""Devolve os valores imediatamente abaixo e acima,
respectivamente."""
return (self.board[row - 1][col] if self.get_size() > row > 0 else None,
self.board[row + 1][col] if 0 <= row < (self.get_size() - 1) else None)
def adjacent_horizontal_numbers(self, row: int, col: int):
"""Devolve os valores imediatamente à esquerda e à direita,
respectivamente."""
return (self.board[row][col - 1] if self.get_size() > col > 0 else None,
self.board[row][col + 1] if 0 <= col < (self.get_size() - 1) else None)
def filter_guaranteed(self):
global guaranteed_actions
def same_numbers(n1, n2):
numbers = [n1, n2]
if n1 == n2 and 2 not in numbers and None not in numbers:
return True
return False
for i in range(self.get_size()):
one = False
zero = False
row = self.get_row(i)
row_one_count = row.count(1)
row_zero_count = row.count(0)
if int(self.get_size()) % 2 != 0:
if row_one_count - self.get_size()//2 >= 1:
zero = True
if row_zero_count - self.get_size()//2 >= 1:
one = True
else:
if row_one_count == self.get_size()//2:
zero = True
if row_zero_count == self.get_size()//2:
one = True
for j in range(self.get_size()):
if (self.get_number(i, j) == 2):
if one:
guaranteed_actions.append((i, j, 1), )
elif zero:
guaranteed_actions.append((i, j, 0), )
else:
next_horizontals = self.get_horizontal_values(i, j, 1)
prev_horizontals = self.get_horizontal_values(i, j, 0)
next_verticals = self.get_vertical_values(i, j, 1)
prev_verticals = self.get_vertical_values(i, j, 0)
adjacent_verticals = self.adjacent_vertical_numbers(i, j)
adjacent_horizontals = self.adjacent_horizontal_numbers(i, j)
if same_numbers(adjacent_verticals[0], adjacent_verticals[1]):
guaranteed_actions.append((i, j, 1 - adjacent_verticals[0]), )
elif same_numbers(adjacent_horizontals[0], adjacent_horizontals[1]):
guaranteed_actions.append((i, j, 1 - adjacent_horizontals[0]), )
elif same_numbers(next_horizontals[0], next_horizontals[1]):
guaranteed_actions.append((i, j, 1 - next_horizontals[0]), )
elif same_numbers(prev_horizontals[0], prev_horizontals[1]):
guaranteed_actions.append((i, j, 1 - prev_horizontals[0]), )
elif same_numbers(next_verticals[0], next_verticals[1]):
guaranteed_actions.append((i, j, 1 - next_verticals[0]), )
elif same_numbers(prev_verticals[0], prev_verticals[1]):
guaranteed_actions.append((i, j, 1 - prev_verticals[0]), )
def get_first_empty_position(self):
for i in range(self.get_size()):
for j in range(self.get_size()):
if self.get_number(i, j) == 2:
return (i, j, 2)
return None
def get_horizontal_values(self, row: int, col: int, identifier: int):
"""Identifier:
1 = Next 2 values; 0 = Previous 2 values; Returns None if non-existant"""
if identifier == 1:
return (self.board[row][col + 1] if 0 <= col < (self.get_size() - 1) else None,
self.board[row][col + 2] if 0 <= col < (self.get_size() - 2) else None)
else:
return (self.board[row][col - 1] if self.get_size() > col > 0 else None,
self.board[row][col - 2] if self.get_size() > col > 1 else None)
def get_vertical_values(self, row: int, col: int, identifier: int):
"""Identifier:
1 = Next 2 values; 0 = Previous 2 values; Returns None if non-existant"""
if identifier == 1:
return (self.board[row + 1][col] if 0 <= row < (self.get_size() - 1) else None,
self.board[row + 2][col] if 0 <= row < (self.get_size() - 2) else None)
else:
return (self.board[row - 1][col] if self.get_size() > row > 0 else None,
self.board[row - 2][col] if self.get_size() > row > 1 else None)
def copy_board(self):
new_board = []
for i in range(self.get_size()):
line = []
for j in range(self.get_size()):
line += [self.get_number(i, j)]
new_board += [line]
return new_board
def exists(self, vector1, identifier: int):
"""Identifier:
0 = Row; 1 = Columnn
"""
if identifier == 0:
for i in range(self.get_size()):
row = self.get_row(i)
if row == vector1:
return True
if identifier == 1:
for i in range(self.get_size()):
col = self.get_col(i)
if col == vector1:
return True
return False
@staticmethod
def parse_instance_from_stdin():
"""Lê o test do standard input (stdin) que é passado como argumento
e retorna uma instância da classe Board.
Por exemplo:
$ python3 takuzu.py < input_T01
> from sys import stdin
> stdin.readline()
"""
board = []
count = 0
n = sys.stdin.readline()
n = n.rstrip('\n')
n = int(n)
for i in range(n):
line = sys.stdin.readline()
line = line.rstrip('\n')
values = line.split('\t')
for j in range(n):
values[j] = int(values[j])
if values[j] == 2:
count += 1
board += [values]
new_board = Board(n, board, count)
global guaranteed_actions
new_board.filter_guaranteed()
return new_board
# TODO: outros metodos da classe
class TakuzuState:
state_id = 0
def __init__(self, board):
self.state = board
self.id = TakuzuState.state_id
TakuzuState.state_id += 1
def __lt__(self, other):
return self.id < other.id
def get_state(self):
return self.state
def get_size(self):
return self.state.get_size()
def get_empty_positions(self):
return self.state.get_empty_positions()
def change_number(self, row: int, col: int, number: int):
self.state.change_number(row, col, number)
def get_number(self, row: int, col: int):
"""Devolve o valor na respetiva posição do tabuleiro."""
return self.state.get_number(row, col)
def get_row(self, row: int):
return self.state.get_row(row)
def get_col(self, col: int):
return self.state.get_col(col)
def get_first_empty_position(self):
return self.get_state().get_first_empty_position()
def get_horizontal_values(self, row: int, col: int, identifier: int):
return self.get_state().get_horizontal_values(row, col, identifier)
def get_vertical_values(self, row: int, col: int, identifier: int):
return self.get_state().get_vertical_values(row, col, identifier)
def adjacent_horizontal_numbers(self, row: int, col: int):
return self.get_state().adjacent_horizontal_numbers(row, col)
def adjacent_vertical_numbers(self, row: int, col: int):
return self.get_state().adjacent_vertical_numbers(row, col)
def copy_board(self):
return self.get_state().copy_board()
def exists(self, vector1, identifier: int):
"""Identifier:
0 = Row; 1 = Columnn
"""
return self.get_state().exists(vector1, identifier)
# TODO: outros metodos da classe
class Takuzu(Problem):
def __init__(self, board: Board):
"""O construtor especifica o estado inicial."""
self.initial = TakuzuState(board)
def actions(self, given_state: TakuzuState):
"""Retorna uma lista de ações que podem ser executadas a
partir do estado passado como argumento."""
result = tuple()
global guaranteed_actions
if len(guaranteed_actions) > 0:
action = guaranteed_actions[0]
guaranteed_actions = guaranteed_actions[1:]
result += (action, )
return result
position = given_state.get_state().get_first_empty_position()
put = {0, 1}
next_horizontals = given_state.get_horizontal_values(position[0], position[1], 1)
prev_horizontals = given_state.get_horizontal_values(position[0], position[1], 0)
next_verticals = given_state.get_vertical_values(position[0], position[1], 1)
prev_verticals = given_state.get_vertical_values(position[0], position[1], 0)
adjacent_verticals = given_state.adjacent_vertical_numbers(position[0], position[1])
adjacent_horizontals = given_state.adjacent_horizontal_numbers(position[0], position[1])
row = given_state.get_row(position[0])
col = []
row_one_count = row.count(1)
row_zero_count = row.count(0)
col_one_count = 0
col_zero_count = 0
for i in range(given_state.get_size()):
if given_state.state.board[i][position[1]] == 0:
col_zero_count += 1
elif given_state.state.board[i][position[1]] == 1:
col_one_count += 1
col += [given_state.state.board[i][position[1]]]
def same_numbers(n1, n2):
numbers = [n1, n2]
if n1 == n2 and 2 not in numbers and None not in numbers:
return True
return False
if int(given_state.get_size()) % 2 != 0:
if row_one_count - given_state.get_size()//2 >= 1:
if [2, 2, 2] in row:
return ()
put.discard(1)
if row_zero_count - given_state.get_size()//2 >= 1:
if [2, 2, 2] in row:
return ()
put.discard(0)
if col_one_count - given_state.get_size()//2 >= 1:
if [2, 2, 2] in col:
return ()
put.discard(1)
if col_zero_count - given_state.get_size()//2 >= 1:
if [2, 2, 2] in col:
return ()
put.discard(0)
else:
if row_one_count == given_state.get_size()//2:
if [2, 2, 2] in row:
return ()
put.discard(1)
if row_zero_count == given_state.get_size()//2:
if [2, 2, 2] in row:
return ()
put.discard(0)
if col_one_count == given_state.get_size()//2:
if [2, 2, 2] in col:
return ()
put.discard(1)
if col_zero_count == given_state.get_size()//2:
if [2, 2, 2] in col:
return ()
put.discard(0)
if put == {}:
return ()
if same_numbers(adjacent_verticals[0], adjacent_verticals[1]):
put.discard(adjacent_verticals[0])
if same_numbers(adjacent_horizontals[0], adjacent_horizontals[1]):
put.discard(adjacent_horizontals[0])
if same_numbers(next_horizontals[0], next_horizontals[1]):
put.discard(next_horizontals[0])
if same_numbers(prev_horizontals[0], prev_horizontals[1]):
put.discard(prev_horizontals[0])
if same_numbers(next_verticals[0], next_verticals[1]):
put.discard(next_verticals[0])
if same_numbers(prev_verticals[0], prev_verticals[1]):
put.discard(prev_verticals[0])
iterate = tuple(put)
for number in iterate:
if row.count(2) == 1:
new_row = row.copy()
new_row[position[1]] = number
if given_state.exists(new_row, 0):
put.discard(number)
if col.count(2) == 1:
col[position[0]] = number
if given_state.exists(col, 1):
put.discard(number)
if put == {}:
return ()
if 1 in put:
result += ((position[0], position[1], 1),)
if 0 in put:
result += ((position[0], position[1], 0),)
return result
def result(self, state: TakuzuState, action):
"""Retorna o estado resultante de executar a 'action' sobre
'state' passado como argumento. A ação a executar deve ser uma
das presentes na lista obtida pela execução de
self.actions(state)."""
copied_board = state.copy_board()
copied_board[action[0]][action[1]] = action[2]
return TakuzuState(Board(state.get_size(), copied_board, state.get_empty_positions() - 1))
def goal_test(self, given_state: TakuzuState):
"""Retorna True se e só se o estado passado como argumento é
um estado objetivo. Deve verificar se todas as posições do tabuleiro
estão preenchidas com uma sequência de números adjacentes."""
return given_state.get_empty_positions() == 0
def h(self, node: Node):
"""Função heuristica utilizada para a procura A*."""
state = node.state
size = state.get_state().get_size()
total_size = size * size
one_count = 0
zero_count = 0
result = total_size//2 if total_size % 2 == 0 else total_size//2 + 1
for i in range(size):
for j in range(size):
number = state.get_state().get_number(i, j)
if number == 0:
zero_count += 1
elif number == 1:
one_count += 1
result = result - one_count if one_count > zero_count else result - zero_count
return result
if __name__ == "__main__":
# Ler tabuleiro do ficheiro 'i1.txt' (Figura 1):
# $ python3 takuzu < i1.txt
board = Board.parse_instance_from_stdin()
# Criar uma instância de Takuzu:
problem = Takuzu(board)
compare_searchers([problem], ["Searcher", "Resultado"], [astar_search])
# Obter o nó solução usando a procura em profundidade:
# goal_node = depth_first_tree_search(problem)
# print(goal_node.state.get_state())