def example2(): city1 = 'Arad' city2 = 'Craiova' heuristic = calculate_heuristics(city1, coordinates) path1 = astar_search(romania_map, heuristic, city1, city2) path2 = astar_search(romania_map, heuristic, city2, city1) P1_task(path1, path2)
def main(): print("Choose city from list of possible cities:") print( "Arad, Bucharest, Craiova, Drobeta, Eforie, Fagaras, Giuegiu, Hirsova, Iasi, Lugoj, Mehadia, Neamt, Oradea," ) print("Pitesti, Rimnicu, Sibiu, Timisoara, Urziceni, Vaslui, Zerind", end='\n') city1 = input("Input the first city ") city2 = input("Input the second city ") heuristic = calculate_heuristics(city1, coordinates) path1 = astar_search(romania_map, heuristic, city1, city2) path2 = astar_search(romania_map, heuristic, city2, city1) P1_task(path1, path2)
maze_graph = dict() for node in maze: c = dict() for connected_node in node.connected_nodes: c[str(connected_node.num)] = 1 maze_graph[str(node.num)] = c undirectedGraph_maze = undirectedGraph(maze_graph) undirectedGraph_maze.locations = dict() for node in maze: undirectedGraph_maze.locations[str(node.num)] = (node.col, node.row) Maze_problem = GraphProblem('0', str(maze[-1].num), undirectedGraph_maze) start = datetime.now() search_result = astar_search(Maze_problem) end = datetime.now() print('search time cost: ' + str(end - start)) solution = search_result.solution() h = w = 30 tl = turtle.Turtle() screen = turtle.Screen() screen.screensize(col * w, row * h) screen.tracer(50000) tl.hideturtle() x0, y0 = draw(maze, row, col, tl, h, w) ''' tl.setposition(x0 + w / 2, y0 - h / 2) tl.pencolor('red') tl.pensize(2) tl.pendown()
avgnumExpandedNodesM = 0 avgeffectiveBranchingFactorM = 0 avgpenetranceM = 0 avgnumExpandedNodesPDB = 0 avgeffectiveBranchingFactorPDB = 0 avgpenetrancePDB = 0 for i in range(numTests): # creo un problema puzzleProblemM = Puzzle(n, nScrambles) # copio il problema puzzleProblemPDB = deepcopy(puzzleProblemM) ''' Manhattan heuristic ''' nodeResultM, numExpandedNodesM = astar_search(puzzleProblemM, h_manhattan) avgnumExpandedNodesM += numExpandedNodesM effectiveBranchingFactorM = calculateEffectiveBranchingFactor(numExpandedNodesM, nodeResultM.path_cost) avgeffectiveBranchingFactorM += effectiveBranchingFactorM penetranceM = float(nodeResultM.path_cost) / numExpandedNodesM avgpenetranceM += penetranceM ''' Pattern databases heuristic ''' nodeResultPDB, numExpandedNodesPDB = astar_search(puzzleProblemPDB, h_dbPattern) avgnumExpandedNodesPDB += numExpandedNodesPDB penetrancePDB = float(nodeResultPDB.path_cost) / numExpandedNodesPDB avgpenetrancePDB += penetrancePDB
path_backward = results[1].solution() print('forward') print(path_forward) print('backward') print(path_backward) bi_path.extend(path_forward) bi_path.extend(move_reverse(path_backward)) print(bi_path) print(len(bi_path)) ''' start = datetime.now() results = astar_search(puzzle1) end = datetime.now() print('time cost: ' + str(end - start)) as_path = results.solution() print(as_path) print(len(as_path)) results_move_bi = bi_path results_move_as = as_path state1 = initial print(initial) print('bi')