-
Notifications
You must be signed in to change notification settings - Fork 0
/
entrega_1_tradicional.py
149 lines (126 loc) · 4.99 KB
/
entrega_1_tradicional.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
from simpleai.search import breadth_first, SearchProblem, astar, greedy, depth_first
from simpleai.search.viewers import BaseViewer
Enemigos = ((0,2,4,6),
(4, ),
(0, ),
(1,6,7,9),
(0,7,8),
(4,9),
(0,5,9),
(0,7),
(2,4,9),
(1,4,6,7))
Rey = (5,3)
META = (0,9)
class HnefataflProblem(SearchProblem):
def cost(self, state1, action, state2):
return 1
def is_goal(self, state):
filaRey, columnaRey = state
return (filaRey in META) or (columnaRey in META)
def actions(self, state):
acciones = []
filaRey, columnaRey = state
#MoverseArriba
if columnaRey not in Enemigos[filaRey - 1]:
enemigosAdyacentes = 0
if (filaRey > 1) and (columnaRey in Enemigos[filaRey - 2]):
enemigosAdyacentes += 1
if (columnaRey - 1) in Enemigos[filaRey - 1]:
enemigosAdyacentes += 1
if (columnaRey + 1) in Enemigos[filaRey - 1]:
enemigosAdyacentes += 1
if enemigosAdyacentes < 2:
acciones.append((filaRey-1, columnaRey))
#MoverseAbajo
if columnaRey not in Enemigos[filaRey + 1]:
enemigosAdyacentes = 0
if (filaRey < 8) and (columnaRey in Enemigos[filaRey + 2]):
enemigosAdyacentes += 1
if (columnaRey - 1) in Enemigos[filaRey + 1]:
enemigosAdyacentes += 1
if (columnaRey + 1) in Enemigos[filaRey + 1]:
enemigosAdyacentes += 1
if enemigosAdyacentes < 2:
acciones.append((filaRey+1, columnaRey))
#MoverseIzquierda
if (columnaRey - 1) not in Enemigos[filaRey]:
enemigosAdyacentes = 0
if (columnaRey > 1) and (columnaRey - 2 in Enemigos[filaRey]):
enemigosAdyacentes += 1
if columnaRey - 1 in Enemigos[filaRey - 1]:
enemigosAdyacentes += 1
if columnaRey - 1 in Enemigos[filaRey + 1]:
enemigosAdyacentes += 1
if enemigosAdyacentes < 2:
acciones.append((filaRey, columnaRey - 1))
#MoverseDerecha
if (columnaRey + 1) not in Enemigos[filaRey]:
enemigosAdyacentes = 0
if (columnaRey < 8) and (columnaRey + 2 in Enemigos[filaRey]):
enemigosAdyacentes += 1
if columnaRey + 1 in Enemigos[filaRey - 1]:
enemigosAdyacentes += 1
if columnaRey + 1 in Enemigos[filaRey + 1]:
enemigosAdyacentes += 1
if enemigosAdyacentes < 2:
acciones.append((filaRey, columnaRey + 1))
return acciones
def result(self, state, action):
return action
def heuristic(self, state):
filaRey, columnaRey = state
return min(filaRey, 9 - filaRey, columnaRey, 9 - columnaRey) #Devuelve la cantidad de pasos que me faltan al borde mas cercano
def resolver(metodo_busqueda, posicion_rey, controlar_estados_repetidos):
problema = HnefataflProblem(posicion_rey)
visor = BaseViewer()
if metodo_busqueda == "breadth_first":
resultado = breadth_first(problema, graph_search=controlar_estados_repetidos)
return resultado
if metodo_busqueda == "depth_first":
return depth_first(problema, graph_search=controlar_estados_repetidos)
if metodo_busqueda == "greedy":
return greedy(problema, graph_search=controlar_estados_repetidos)
if metodo_busqueda == "astar":
return astar(problema, graph_search=controlar_estados_repetidos)
if __name__ == '__main__':
# Amplitud en arbol
# problema = HnefataflProblem(Rey)
# Visor = BaseViewer()
# resultado = breadth_first(problema,viewer=Visor)
# Amplitud en grafo
# problema = HnefataflProblem(Rey)
# Visor = BaseViewer()
# resultado = breadth_first(problema,graph_search=True,viewer=Visor)
# Profundidad en arbol
# problema = HnefataflProblem(Rey)
# Visor = BaseViewer()
# resultado = depth_first(problema,viewer=Visor)
# Profundidad en grafo
# problema = HnefataflProblem(Rey)
# Visor = BaseViewer()
# resultado = depth_first(problema,graph_search=True,viewer=Visor)
# Avara en arbol
# problema = HnefataflProblem(Rey)
# Visor = BaseViewer()
# resultado = greedy(problema,viewer=Visor)
#Avara en grafo
#problema = HnefataflProblem(Rey)
#Visor = BaseViewer()
#resultado = greedy(problema,graph_search=True,viewer=Visor)
# A* en arbol
problema = HnefataflProblem(Rey)
Visor = BaseViewer()
resultado = astar(problema,viewer=Visor)
#A* en grafo
#problema = HnefataflProblem(Rey)
#Visor = BaseViewer()
#resultado = astar(problema,graph_search=True,viewer=Visor)
print 'Estado meta:'
print resultado.state
print 'Camino:'
for accion, estado in resultado.path():
print 'Movi', accion
print 'Llegue a', estado
print
print 'Estadisticas',Visor.stats