def resolver(metodo_busqueda, posiciones_personas): """ Devuelve un nodo resultado, a partir de una búqueda especificada. Argumentos: metodo_busqueda -- nombre de método a usar (string); posiciones_personas -- lista de tuplas. Cada tupla representa la posicion de una persona en la grilla (aún por rescatar). """ """ Estado inicial: Lista compuesta de 4 posiciones: 1era posicion: Tupla. Posicion del robot en la grilla. 2da posicion: Lista de tuplas. Posiciones de la grilla que el robot va hundiendo (inutilizando). 3era posicion: Lista de tuplas. Posiciones de la grilla donde se ubican las personas aún no rescatadas. 4ta posicion: Booleano. Es True si el robot esta 'ocupado' (esta cargando personas), caso contrario False. """ ESTADO_INICIAL = ((0, 0), (), posiciones_personas, False) problema = RescatePersonas(ESTADO_INICIAL) if metodo_busqueda == 'astar': return astar(problema, graph_search=True) elif metodo_busqueda == 'depth_first': return depth_first(problema, graph_search=True) elif metodo_busqueda == 'breadth_first': return breadth_first(problema, graph_search=True) elif metodo_busqueda == 'greedy': return greedy(problema, graph_search=True)
def GBFS(SearchingProblem): print("---Greedy Best First Search---") before = datetime.now() search = greedy(SearchingProblem,lambda node: node.state) #search = greedy(SearchingProblem,lambda node: node.state,viewer=my_viewer) after = datetime.now() print("Path -->",search.path()) print("Time -->", (after - before).total_seconds()) print("Path cost-->",search.cost) print("-" * 40)
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)
def resolver(metodo_busqueda, posiciones_aparatos): Inicial = [(3,3)] for ap in posiciones_aparatos: Inicial.append((ap[0], ap[1], 300)) if metodo_busqueda == "breadth_first": return breadth_first(Problem(tuple(Inicial)), graph_search=True) if metodo_busqueda == "astar": return astar(Problem(tuple(Inicial)), graph_search=True) if metodo_busqueda == "greedy": return greedy(Problem(tuple(Inicial)), graph_search=True) if metodo_busqueda == "depth_first": return depth_first(Problem(tuple(Inicial)), graph_search=True)
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
def resolver(metodo_busqueda, posicion_rey, controlar_estados_repetidos): INITIAL = posicion_rey problema = HnefataflProblema(posicion_rey) visor = BaseViewer() #Busqueda en amplitud if (metodo_busqueda == 'breadth_first'): resultado = breadth_first(problema, graph_search=controlar_estados_repetidos, viewer=visor) return resultado #Busqueda en profundidad if (metodo_busqueda == 'depth_first'): resultado = depth_first(problema, graph_search=controlar_estados_repetidos, viewer=visor) return resultado #Busqueda avara if (metodo_busqueda == 'greedy'): resultado = greedy(problema, graph_search=controlar_estados_repetidos, viewer=visor) return resultado #Busqueda A Estrella if (metodo_busqueda == 'astar'): resultado = astar(problema, graph_search=controlar_estados_repetidos, viewer=visor) return resultado # for action, state in resultado.path(): # print action # print resultado.path() # print 'produndidad: ' + str(resultado.depth) # print state # print 'El costo es: ' + str(resultado.cost) # print visor.stats #if __name__ == '__main__': # resolver('breadth_first', (5,3),True)
def resolver(metodo_busqueda, posiciones_personas): inicial = GenerarEstado(posiciones_personas) problema = RescateHieloProblem(inicial) if metodo_busqueda == "breadth_first": return breadth_first(problema, graph_search=True, viewer=ConsoleViewer()) if metodo_busqueda == "greedy": return greedy(problema, graph_search=True, viewer=ConsoleViewer()) if metodo_busqueda == "depth_first": return depth_first(problema, graph_search=True, viewer=ConsoleViewer()) if metodo_busqueda == "astar": return astar(problema, graph_search=True, viewer=ConsoleViewer())
def resolver(metodo_busqueda, posiciones_personas): state = ((0, 0), (posiciones_personas), ()) my_viewer = BaseViewer() if 'astar': result = astar(TpProblem(state), graph_search=True, viewer=my_viewer) elif 'breadth_first': result = breadth_first(TpProblem(state), graph_search=True, viewer=my_viewer) elif 'depth_first': result = depth_first(TpProblem(state), graph_search=True, viewer=my_viewer) elif 'greedy': result = greedy(TpProblem(state), graph_search=True, viewer=my_viewer) return result
def resolver(metodo_busqueda, franceses, piratas): viewer = None letra = 0 franceses = list(franceses) piratas = list(piratas) for barcos in piratas: if letra == 0: piratas[0] = list(piratas[0]) piratas[0].insert(0, 'A') piratas[0].append(0) piratas[0].append(0) piratas[0] = tuple(piratas[0]) if letra == 1: piratas[1] = list(piratas[1]) piratas[1].insert(0, 'B') piratas[1].append(0) piratas[1].append(0) piratas[1] = tuple(piratas[1]) if letra == 2: piratas[2] = list(piratas[2]) piratas[2].insert(0, 'C') piratas[2].append(0) piratas[2].append(0) piratas[2] = tuple(piratas[2]) letra = letra + 1 estado = ((tuple(piratas), tuple(franceses))) problem = Barcos_Piratas(estado) if metodo_busqueda == "breadth_first": resultado = breadth_first(problem, graph_search=True, viewer = viewer) elif metodo_busqueda == "greedy": resultado = greedy(problem, graph_search=True, viewer = viewer) elif metodo_busqueda == "depth_first": resultado = depth_first(problem, graph_search=True, viewer = viewer) elif metodo_busqueda == "astar": resultado = astar(problem, graph_search=True, viewer = viewer) elif metodo_busqueda == "uniform_cost": resultado = uniform_cost(problem, graph_search=True, viewer = viewer) return resultado
def resolver(metodo_busqueda, posicion_rey, controlar_estados_repetidos): problema = HnefataflProblema(posicion_rey) visor = BaseViewer() #Busquedas, Grafo -> graph_search=True if (metodo_busqueda == 'breadth_first'): # En amplitud resultado = breadth_first(problema, graph_search=controlar_estados_repetidos, viewer=visor) elif (metodo_busqueda == 'depth_first'): # Profundidad resultado = depth_first(problema, graph_search=controlar_estados_repetidos, viewer=visor) elif (metodo_busqueda == 'greedy'): # Avara resultado = greedy(problema, graph_search=controlar_estados_repetidos, viewer=visor) elif (metodo_busqueda == 'astar'): # Estrella resultado = astar(problema, graph_search=controlar_estados_repetidos, viewer=visor) print(visor.stats) return resultado
def resolver(metodo_busqueda, posiciones_aparatos): estado = [ (3, 3, 0), ] nuevo_estado = ((), ) lista_aparatos = [] posiciones_aparatos = list(posiciones_aparatos) for artefacto in posiciones_aparatos: lista_artefacto = list(artefacto) lista_aparatos.append(lista_artefacto) posiciones_aparatos = tuple(posiciones_aparatos) for aparato in lista_aparatos: aparato.append(300) for aparato in lista_aparatos: tupla_aparato = tuple(aparato) estado.append(tupla_aparato) nuevo_estado = tuple(estado) problema = BomerobotProblem(nuevo_estado) if metodo_busqueda == 'breadth_first': resultado = breadth_first(problema, graph_search=True) return resultado if metodo_busqueda == 'depth_first': resultado = depth_first(problema, graph_search=True) return resultado if metodo_busqueda == 'greedy': resultado = greedy(problema, graph_search=True) return resultado if metodo_busqueda == 'astar': resultado = astar(problema, graph_search=True) return resultado
return list(' ABCDEFGHIJKLMNOPQRSTUVWXYZ') return [] def result(self, state, action): return state + action def is_goal(self, state): return state == GOAL def heuristic(self, state): wrong = sum( [1 if state[i] != GOAL[i] else 0 for i in range(len(state))]) missing = len(GOAL) - len(state) return wrong + missing # Using Breadth-First Search # t1 = time.time() # problem = HelloProblem(initial_state='') # result = breadth_first(problem) # print('Time: {:.5f}s'.format(time.time() - t1)) # print(result.state) # print(result.path()) # Using Greedy Search t1 = time.time() problem = HelloProblem(initial_state='') result = greedy(problem) print('Time: {:.5f}s'.format(time.time() - t1)) print(result.state) print(result.path())
"down left": cost_diagonal, "down right": cost_diagonal, } problem = SRTS(TRACK) s_root = problem.initial s_goal = problem.goal while s_root != s_goal: Comfortables = [] c = best_first(problem, graph_search=True) t = s_root if c is not None or problem.is_safe(c): Comfortables.append(t) if len(Comfortables) is not 0: result = greedy(problem, graph_search=True) break ancestors = [a[1] for a in c.path()] s_safe = Comfortables[0] s_target = Comfortables[0] for a in ancestors: Comfortables.append(a) if problem.is_safe(Comfortables[0]): s_target = s_safe elif problem.is_safe(problem.result(s_target,'up')) or \ problem.is_safe(problem.result(s_target, 'up')) or \ problem.is_safe(problem.result(s_target, 'up')) or \ problem.is_safe(problem.result(s_target, 'up')) or \
def heuristic(self, cur_state): # Compare current string with target string dist = sum([ 1 if cur_state[i] != self.target_string[i] else 0 for i in range(len(cur_state)) ]) # Difference between the lengths diff = len(self.target_string) - len(cur_state) return dist + diff if __name__ == '__main__': args = build_arg_parser().parse_args() # Initialize the object problem = CustomProblem() # Set target string and initial state problem.set_target(args.input_string) problem.initial_state = args.initial_state # Solve the problem output = ss.greedy(problem) print('\nTarget string:', args.input_string) print('\nPath to the solution:') for item in output.path(): print(item)
def actions(self, state): if len(state) < len(GOAL): return list(' ABCDEFGHIJKLMNOPQRSTUVWXYZ') return [] def result(self, state, action): return state + action def is_goal(self, state): return state == GOAL def heuristic(self, state): wrong = sum([1 if state[i] != GOAL[i] else 0 for i in range(len(state))]) missing = len(GOAL) - len(state) return wrong + missing # Using Breadth-First Search t1 = time.time() problem = HelloProblem(initial_state='') result = breadth_first(problem) print('Time: {:.5f}s'.format(time.time() - t1)) print(result.state) print(result.path()) # Using Greedy Search t1 = time.time() problem = HelloProblem(initial_state='') result = greedy(problem) print('Time: {:.5f}s'.format(time.time() - t1)) print(result.state) print(result.path())
def test_greedy(self): result = greedy(self.problem) self.assertEquals(result.state, GOAL)