def solve_and_print(problem): depth_first = search.depth_first_graph_search(problem) breadth_first = search.breadth_first_graph_search(problem) branch_and_bound = search.bab(problem) subestimation = search.subBAB(problem) print("==================== %s ====================" % (problem.problem_title())) print('Busqueda en profundidad: \n', depth_first["solution"].path()) print('- Nodos visitados: %d\n- Nodos generados %d' % (depth_first["visited_nodes"], depth_first["generated_nodes"])) print('Busqueda en anchura: \n', breadth_first["solution"].path()) print('- Nodos visitados: %d\n- Nodos generados %d' % (breadth_first["visited_nodes"], breadth_first["generated_nodes"])) print('Branch & Bound: \n', branch_and_bound["solution"].path()) print('- Nodos visitados: %d\n- Nodos generados %d' % (branch_and_bound["visited_nodes"], branch_and_bound["generated_nodes"])) print('Branch & Bound (subestimation): \n', subestimation["solution"].path()) print('- Nodos visitados: %d\n- Nodos generados %d' % (subestimation["visited_nodes"], subestimation["generated_nodes"]))
# Search methods import search ab = search.GPSProblem('A', 'B', search.romania) print("A - B") print("Breadth first") print(search.breadth_first_graph_search(ab)) print("Depth first") print(search.depth_first_graph_search(ab)) print("Branch and Bound") print(search.bab(ab)) print("Branch and Bound (subestimation)") print(search.ras(ab)) print() oe = search.GPSProblem('O', 'E', search.romania) print("O - E") print("Breadth first") print(search.breadth_first_graph_search(oe)) print("Depth first") print(search.depth_first_graph_search(oe)) print("Branch and Bound") print(search.bab(oe)) print("Branch and Bound (subestimation)") print(search.ras(oe)) print() pz = search.GPSProblem('P', 'Z', search.romania) print("P - Z") print("Breadth first")
# Search methods import search ab = search.GPSProblem('S', 'P', search.romania) print('Búsqueda anchura') print(search.breadth_first_graph_search(ab).path()) print('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>') print('Búsqueda profundidad') print(search.depth_first_graph_search(ab).path()) print('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>') print('Rafimifación y salto') print(search.bab(ab).path()) print('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>') print('Rafimifación y salto con Subestimacion') print(search.babSub(ab).path()) # Result: # [<Node B>, <Node P>, <Node R>, <Node S>, <Node A>] : 101 + 97 + 80 + 140 = 418 # [<Node B>, <Node F>, <Node S>, <Node A>] : 211 + 99 + 140 = 450
# Search methods import search ab = search.GPSProblem('A', 'B', search.romania) az = search.GPSProblem('A', 'Z', search.romania) ad = search.GPSProblem('A', 'D', search.romania) asa = search.GPSProblem('A', 'S', search.romania) ag = search.GPSProblem('A', 'G', search.romania) #print(search.breadth_first_graph_search(ab).path()) #print(search.depth_first_graph_search(ab).path()) print("Ramificacion y acotacion sin subestimacion", search.bab(ab).path()) print("Ramificacion y acotacion con subestimacion", search.heur(ab).path()) print("Ramificacion y acotacion sin subestimacion", search.bab(az).path()) print("Ramificacion y acotacion con subestimacion", search.heur(az).path()) print("Ramificacion y acotacion sin subestimacion", search.bab(ad).path()) print("Ramificacion y acotacion con subestimacion", search.heur(ad).path()) print("Ramificacion y acotacion sin subestimacion", search.bab(asa).path()) print("Ramificacion y acotacion con subestimacion", search.heur(asa).path()) print("Ramificacion y acotacion sin subestimacion", search.bab(ag).path()) print("Ramificacion y acotacion con subestimacion", search.heur(ag).path()) # Result: # [<Node B>, <Node P>, <Node R>, <Node S>, <Node A>] : 101 + 97 + 80 + 140 = 418 # [<Node B>, <Node F>, <Node S>, <Node A>] : 211 + 99 + 140 = 450
# Search methods import search ab = search.GPSProblem('A', 'H', search.romania) print("Búsqueda en anchura:") print("----------------------------------------------------------------") print(search.breadth_first_graph_search(ab).path(), '\n') print("Búsqueda en profundidad:") print("----------------------------------------------------------------") print(search.depth_first_graph_search(ab).path(), '\n') print("Búsqueda en Ramificación y acotación:") print("----------------------------------------------------------------") print(search.bab(ab).path(), '\n') print("Búsqueda en Ramificación y acotación con subestimación:") print("----------------------------------------------------------------") print(search.babsub(ab).path()) # Result: # [<Node B>, <Node P>, <Node R>, <Node S>, <Node A>] : 101 + 97 + 80 + 140 = 418 # [<Node B>, <Node F>, <Node S>, <Node A>] : 211 + 99 + 140 = 450
print("\nT -> U") print(search.breadth_first_graph_search(tu).path()) print("\nH -> L") print(search.breadth_first_graph_search(hl).path()) print( "\n\n__________________________ Depth first ___________________________\n") print("A -> B") print(search.depth_first_graph_search(ab).path()) print("\nT -> U") print(search.depth_first_graph_search(tu).path()) print("\nH -> L") print(search.depth_first_graph_search(hl).path()) print( "\n\n________________________ Branch and bound ________________________\n") print("A -> B") print(search.bab(ab).path()) print("\nT -> U") print(search.bab(tu).path()) print("\nH -> L") print(search.bab(hl).path()) print( "\n\n_______________ Branch and bound - subestimation _________________\n") print("A -> B") print(search.babh(ab).path()) print("\nT -> U") print(search.babh(tu).path()) print("\nH -> L") print(search.babh(hl).path()) # Result: # [<Node B>, <Node P>, <Node R>, <Node S>, <Node A>] : 101 + 97 + 80 + 140 = 418
# Search methods import search ab = search.GPSProblem('A', 'B', search.romania) sb = search.GPSProblem('S', 'B', search.romania) db = search.GPSProblem('D', 'B', search.romania) cb = search.GPSProblem('C', 'B', search.romania) eb = search.GPSProblem('E', 'B', search.romania) print("Ruta de AB sin subestimacion: ", search.bab(ab).path()) print("Ruta de AB con subestimacion: ", search.babs(ab).path()) print("###############################################") print("Ruta de SB sin subestimacion: ", search.bab(sb).path()) print("Ruta de SB con subestimacion: ", search.babs(sb).path()) print("###############################################") print("Ruta de DB sin subestimacion: ", search.bab(db).path()) print("Ruta de DB con subestimacion: ", search.babs(db).path()) print("###############################################") print("Ruta de CB sin subestimacion: ", search.bab(cb).path()) print("Ruta de CB con subestimacion: ", search.babs(cb).path()) print("###############################################") print("Ruta de EB sin subestimacion: ", search.bab(eb).path())
# Search methods import search ab = search.GPSProblem('A', 'B', search.romania) print("Solución para ir desde B hasta A") print(" ") print("Con el algoritmo de búsqueda en anchura se obtiene:") print(search.breadth_first_graph_search(ab).path()) print("Con el algoritmo de búsqueda en profundidad se obtiene:") print(search.depth_first_graph_search(ab).path()) print("Con el algoritmo de búsqueda de ramificación y salto se obtiene:") print(search.bab(ab).path()) print( "Con el algoritmo de búsqueda de ramificación y salto con subestimación se obtiene:" ) print(search.babh(ab).path()) print(" ") print(" ") print(" ") ac = search.GPSProblem('O', 'A', search.romania) print("Solución para ir desde A hasta O") print(" ") print("Con el algoritmo de búsqueda en anchura se obtiene:") print(search.breadth_first_graph_search(ac).path())
# Search methods import search print("DESDE B hasta A\n") ab = search.GPSProblem('A', 'B' , search.romania) print("\nBusqueda por ramificacion y acotacion") print(search.bab(ab).path()) print("\nBusqueda por ramificacion y acotacion con subestimacion") print(search.consubestimacion(ab).path()) # Result: # [<Node B>, <Node P>, <Node R>, <Node S>, <Node A>] : 101 + 97 + 80 + 140 = 418 # [<Node B>, <Node P>, <Node R>, <Node S>, <Node A>] : 101 + 97 + 80 + 140 = 418 print("------------------------------------------------------------------------------------") print("DESDE E hasta S\n") se = search.GPSProblem('S', 'E' , search.romania) print("\nBusqueda por ramificacion y acotacion") print(search.bab(se).path()) print("\nBusqueda por ramificacion y acotacion con subestimacion") print(search.consubestimacion(se).path()) # Result: # [<Node E>, <Node H>, <Node U>, <Node B>, <Node P>, <Node R>, <Node S>] : 547 # [<Node E>, <Node H>, <Node U>, <Node B>, <Node P>, <Node R>, <Node S>] : 547