Ejemplo n.º 1
0
 def search(origen, destino):
     expandidos = set()
     if origen == destino:
         return ruta(origen)
     f_adelante = {origen: origen}
     f_atras = {destino: destino}
     while f_adelante and f_atras:
         #exp en frontera adelante
         temp_adelante = {}
         for n in f_adelante:
             expandidos.add(n)
             if n in f_atras:
                 r = ruta(f_atras[n])[:-1]
                 r.reverse()
                 return ruta(n) + r
             for hijo in n.expand():
                 if hijo not in expandidos:
                     temp_adelante[hijo] = hijo
         f_adelante = temp_adelante
         # exp en frontera atras
         temp_atras = {}
         for n in f_atras:
             expandidos.add(n)
             if n in f_adelante:
                 r = ruta(n)[:-1]
                 r.reverse()
                 return ruta(f_adelante[n]) + r
             for hijo in n.expand():
                 if hijo not in expandidos:
                     temp_atras[hijo] = hijo
         f_atras = temp_atras
     return None
Ejemplo n.º 2
0
 def search(origen, stop, heuristica):
     agenda = []
     expandidos = set()
     if (stop()):
         return ruta(origen)
     heapq.heappush(agenda, (0, origen))
     while agenda:
         nodo = heapq.heappop(agenda)[1]
         if stop(nodo):
             return ruta(nodo)
         for hijo in nodo.expand():
             if hijo not in explored:
                 heapq.heappush(agenda, heuristica(hijo), hijo)
Ejemplo n.º 3
0
 def search(origen, stop):
     if stop(origen):
         return ruta(origen)
     agenda = deque()
     expandidos = set()
     agenda.append(origen)
     while agenda:
         nodo = agenda.pop()
         expandidos.add(nodo)
         for hijo in nodo.expand():
             if stop(hijo):
                 return ruta(hijo)
             if hijo not in expandidos:
                 agenda.append(hijo)