/
entrega1tradicional.py
executable file
·104 lines (80 loc) · 2.97 KB
/
entrega1tradicional.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
from simpleai.search import SearchProblem, breadth_first, depth_first, astar, greedy, iterative_limited_depth_first
from simpleai.search.viewers import BaseViewer
INITIAL = (
(0, 0, 0, 0, 1),
(0, 0, 0, 0, 0),
(0, 0, 1, 0, 0),
(0, 0, 0, 0, 0),
(2, 0, 0, 0, 0),
)
nMatriz = 4 #5 - 1
def find_number(board, number):
for row_index, row in reversed(list(enumerate(board))):
for col_index, current_number in enumerate(row):
if current_number == number:
return row_index, col_index
def count_number(board, number):
i = 0
for row_index, row in reversed(list(enumerate(board))):
for col_index, current_number in enumerate(row):
if current_number == number:
i += 1
return i
# def find_element(board, number):
# row = board[number][0]
# col = board[number][1]
# return row, col
# def isEnem(board, row, col):
# tup = (row, col)
# for ntup in board:
# if ntup == tup:
# return ntup
# return None
# def find_action(action):
# row = action[0]
# col = action[1]
# enem = action[2]
# return row, col, enem
class entrega1tradicional(SearchProblem):
def cost(self, state1, action, state2):
return 1
def is_goal(self, state):
count = count_number(state, 1)
return count == 0
def actions(self, state):
row_0, col_0 = find_number(state, 2)
actions = []
if row_0 > 0:
actions.append([row_0 - 1, col_0])
if row_0 < nMatriz:
actions.append([row_0 + 1, col_0])
if col_0 > 0:
actions.append([row_0, col_0 - 1])
if col_0 < nMatriz:
actions.append([row_0, col_0 + 1])
return actions
def result(self, state, action):
state_modificable = [list(row) for row in state]
row_a, col_a = action[0], action[1]
if (state_modificable[row_a][col_a]) == 1:
state_modificable[row_a][col_a] = 0
else:
state_modificable[row_a][col_a] = 2
row_0, col_0 = find_number(state, 2)
state_modificable[row_0][col_0] = 0
return tuple(tuple(row) for row in state_modificable)
def heuristic(self, state):
return count_number(state, 1)
def resolver(metodo_busqueda):
visor = BaseViewer()
if metodo_busqueda == 'breadth_first':
result = breadth_first(entrega1tradicional(INITIAL), graph_search=True, viewer=visor)
if metodo_busqueda == 'depth_first':
result = depth_first(entrega1tradicional(INITIAL), graph_search=True, viewer=visor)
if metodo_busqueda == 'limited_depth_first':
result = iterative_limited_depth_first(entrega1tradicional(INITIAL),10, viewer=visor)
if metodo_busqueda == 'greedy':
result = greedy(entrega1tradicional(INITIAL), graph_search=True, viewer=visor)
if metodo_busqueda == 'astar':
result = astar(entrega1tradicional(INITIAL), graph_search=True, viewer=visor)
return result