-
Notifications
You must be signed in to change notification settings - Fork 0
/
solver.py
149 lines (134 loc) · 5.64 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
import rubik
import util
import sys
from sortedcontainers import SortedList
import utilEstado
import os
import time
def shortest_path(start, end):
"""
Using 2-way BFS, finds the shortest path from start_position to
end_position. Returns a list of moves.
Assumes the rubik.quarter_twists move set.
"""
os.system('cls')
tic = time.perf_counter()
path = []
inicio = util.Node(start, None, None)
print(f"Nodo inicio: {inicio.state}, {inicio.parent}, {inicio.action}")
frontierFIFO = util.QueueFrontier()
frontierFIFO.add(inicio)
explorados = SortedList()
pathRegreso = []
fin = util.Node(end, None, None)
print(f"Nodo fin: {fin.state}, {fin.parent}, {fin.action}")
frontierRegreso = util.QueueFrontier()
frontierRegreso.add(fin)
exploradosRegreso = SortedList()
print()
counter = 0
SuperCounter = 0
while frontierFIFO:
SuperCounter += 1
# print(f"SuperCounter {SuperCounter}")
nodo = frontierFIFO.remove()
# print(f"nodo.state: {nodo.state} \n")
if frontierRegreso.contains_state(nodo.state):
print(f"\nEncontro la solucion a la ida!!")
print(f"nodo.state: {nodo.state}")
print(f"nodo.action: {nodo.action} \n")
actions = []
cells = []
estadoComun = nodo.state
while nodo.parent is not None:
actions.append(rubik.movimientos[nodo.action])
cells.append(nodo.state)
dupla = (nodo.action, nodo.state)
path.append(dupla)
nodo = nodo.parent
actions.reverse()
cells.reverse()
path.reverse()
print(f"actions {actions}")
nodoRegreso = frontierRegreso.retorna_nodo(estadoComun)
while nodoRegreso.parent is not None:
actions.append(rubik.movimientos[rubik.movimiento_inverso(nodoRegreso.action)])
cells.append(nodoRegreso.parent.state)
dupla = (rubik.movimiento_inverso(nodoRegreso.action), nodoRegreso.parent.state)
path.append(dupla)
nodoRegreso = nodoRegreso.parent
toc = time.perf_counter()
print(f"Resuelto en {toc - tic:0.4f} segundos\n")
return actions
try:
explorados.add(utilEstado.Estado(nodo.state))
# print(f"explorados.add: {nodo.state}")
for action, state in next_positions(nodo.state):
counter += 1
# if counter == 20001:
# return None
if not frontierFIFO.contains_state(state) and utilEstado.Estado(state) not in explorados:
child = util.Node(state=state, parent=nodo, action=action)
frontierFIFO.add(child)
# print(f"frontierFIFO.add: {state}")
# print()
except Exception as e:
print(e.args)
nodoRegreso = frontierRegreso.remove()
counter += 1
# print(f"counter de regreso: {counter}")
# print(f"nodoRegreso.state: {nodoRegreso.state}")
if frontierFIFO.contains_state(nodoRegreso.state):
print()
print(f"Encontro la solucion al regreso!!")
actions = []
cells = []
estadoComun = nodoRegreso.state
nodoComun = nodoRegreso
nodo = frontierFIFO.retorna_nodo(estadoComun)
while nodo.parent is not None:
actions.append(rubik.movimientos[nodo.action])
cells.append(nodo.state)
dupla = (nodo.action, nodo.state)
path.append(dupla)
nodo = nodo.parent
actions.reverse()
cells.reverse()
path.reverse()
# nodoRegreso = frontierRegreso.retorna_nodo(estadoComun)
nodoRegreso = nodoComun
if nodoRegreso is not None:
while nodoRegreso.parent is not None:
actions.append(rubik.movimientos[rubik.movimiento_inverso(nodoRegreso.action)])
cells.append(nodoRegreso.parent.state)
dupla = (rubik.movimiento_inverso(nodoRegreso.action), nodoRegreso.parent.state)
path.append(dupla)
nodoRegreso = nodoRegreso.parent
toc = time.perf_counter()
print(f"Resuelto en {toc - tic:0.4f} segundos\n")
return actions
try:
exploradosRegreso.add(utilEstado.Estado(nodoRegreso.state))
# print(f"exploradosRegreso.add: {nodoRegreso.state}")
for action, state in next_positions(nodoRegreso.state):
if not frontierRegreso.contains_state(state) and utilEstado.Estado(state) not in exploradosRegreso:
child = util.Node(state=state, parent=nodoRegreso, action=action)
frontierRegreso.add(child)
# print(f"frontierRegreso.add: {state}")
# print()
except Exception as e:
print(e.args)
return None
def next_positions(position):
siguientes = set()
for movimiento in rubik.quarter_twists:
next = rubik.perm_apply(movimiento, position)
sNext = rubik.perm_to_string(next)
tupleNext = tuple(next)
tupla = (rubik.quarter_twists_names[movimiento], tupleNext)
try:
siguientes.add(tupla)
except Exception as e:
print(e.args)
pass
return siguientes