Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions busquedas.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,6 @@ def busqueda_costo_uniforme(problema):
#
# ---------------------------------------------------------------------


def busqueda_A_estrella(problema, heuristica):
"""
Búsqueda A*
Expand All @@ -283,5 +282,19 @@ def busqueda_A_estrella(problema, heuristica):
@return Un objeto tipo Nodo con la estructura completa

"""
raise NotImplementedError('Hay que hacerlo de tarea \
(problema 2 en el archivo busquedas.py)')
frontera = []
heapq.heappush(frontera, (0, Nodo(problema.x0)))
visitados = {problema.x0: 0}

while frontera:
(_, nodo) = heapq.heappop(frontera)
if problema.es_meta(nodo.estado):
nodo.nodos_visitados = problema.num_nodos
return nodo
for hijo in nodo.expande(problema.modelo):
if (hijo.estado not in visitados or
visitados[hijo.estado] > hijo.costo):
# UCS pero cambié nomas esta línea
heapq.heappush(frontera, (hijo.costo + heuristica(hijo), hijo))
visitados[hijo.estado] = hijo.costo
return None
20 changes: 10 additions & 10 deletions ocho_puzzle.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,18 +156,18 @@ def probando(pos_ini):
print("Explorando {} nodos\n\n".format(solucion.nodos_visitados))

# # ------- A* con h1 -----------
# print("---------- Utilizando A* con h1 -------------")
# problema = Ocho_puzzle(pos_ini)
# solucion = busquedas.busqueda_A_estrella(problema, h_1)
# print(solucion)
# print("Explorando {} nodos".format(solucion.nodos_visitados))
print("---------- Utilizando A* con h1 -------------")
problema = Ocho_puzzle(pos_ini)
solucion = busquedas.busqueda_A_estrella(problema, h_1)
print(solucion)
print("Explorando {} nodos".format(solucion.nodos_visitados))

# # ------- A* con h2 -----------
# print("---------- Utilizando A* con h2 -------------")
# problema = Ocho_puzzle(pos_ini)
# solucion = busquedas.busqueda_A_estrella(problema, h_2)
# print(solucion)
# print("Explorando {} nodos".format(solucion.nodos_visitados))
print("---------- Utilizando A* con h2 -------------")
problema = Ocho_puzzle(pos_ini)
solucion = busquedas.busqueda_A_estrella(problema, h_2)
print(solucion)
print("Explorando {} nodos".format(solucion.nodos_visitados))


if __name__ == "__main__":
Expand Down
Loading