def execute_current_algorithm(): Adj, source, target = Tile.neighborsDict, Config.source, Config.target W = {} # weight dictionary that maps each tile to its cost for tile in Tile.tilesDict.values(): W[tile.id] = tile.W algDict = { "BFS" : algorithms.BFS(Adj, source, target), "DFS" : algorithms.DFS(Adj, source, target), "Dijkstra" : algorithms.Dijkstra(Adj, W, source, target), "B_FS" : algorithms.B_FS(Adj, W, source, target), "A*" : algorithms.A_star(Adj, W, source, target), } alg = algDict[Config.currentAlgorithm] if Config.currentAlgorithm in ["BFS", "DFS"]: Tile.pathToTargetList, Tile.idToLevelDict, Tile.levelToIdList = alg.search() Tile.explored_tiles = Tile.idToLevelDict.keys() # to draw explored tiles Tile.idToLevelAux = Tile.idToLevelDict elif Config.currentAlgorithm in ["Dijkstra", "B_FS", "A*"]: Tile.pathToTargetList, Tile.idToCostDict, Tile.levelToIdList, Tile.explored_tiles, Tile.levelToCostList = alg.search() Tile.idToCostAux = Tile.idToCostDict GameController.currentAlg = alg
def main( ) : if validInput( sys.argv ) : fileName = sys.argv[1] board = helper.loadState( fileName ) print "Puzzle: " + fileName time0 = time.clock() if (sys.argv[2] == "bfs" ) : metrics = algorithms.BFS( board ) elif (sys.argv[2] == "dfs" ) : metrics = algorithms.DFS( board ) elif (sys.argv[2] == "id" ) : metrics = algorithms.IDS( board ) elif (sys.argv[2] == "astar") : metrics = algorithms.aStar( board ) time1 = time.clock() delTime = time1-time0 print "Nodes Explored: " + str(metrics[0]) print "Solution Length: " + str(metrics[1]) print "Time Elapsed: " + str(delTime) + " sec" print "" else : print "invalid input" print ""
def dfs_python(): data = request.json startNode = data["startNode"] nodes = data["nodes"] edges = data["edges"] dfs = algo.DFS(nodes, startNode, None) trace = dfs.run() return jsonify(trace)
def plotSearchAlgorithms(dim, p): start = (0, 0) goal = (dim - 1, dim - 1) grid = gd.generateGrid(dim, p) solved = [False for _ in range(5)] solved[0], DFSPath = algos.DFS(grid, start, goal) solved[1], BFSPath, _ = algos.BFS(grid, start, goal) solved[2], BDBFSPath = algos.BDBFS(grid, start, goal) solved[3], AStarPathEuclidean, _ = algos.aStar(grid, start, goal, h.Euc) solved[4], AStarPathManhattan, _ = algos.aStar(grid, start, goal, h.Manhattan) paths = { "DFS": DFSPath, "BFS": BFSPath, "BDBFS": BDBFSPath, "A* Euclidean": AStarPathEuclidean, "A* Manhattan": AStarPathManhattan } # if solvable, then all search algorithms should work, and so plot all of them one at a time if all(solved): print("Close window after each search algorithm to see next one") for i, path in enumerate(paths): print(path) for y, x in paths.get(path): grid[y][x] = 3 pyplot.matshow(grid) pyplot.title(path) pyplot.show() for y, x in paths.get(path): grid[y][x] = 0 # otherwise just show the unsolvable grid else: pyplot.matshow(grid) pyplot.title("Unsolvable") pyplot.show()
def update(self): self.sprite_group_all.update() # catch inputs keystate = pg.key.get_pressed() if keystate[pg.K_ESCAPE]: pg.event.post(pg.event.Event(pg.QUIT)) if keystate[pg.K_TAB]: self.sprite_group_all.empty() self.tilemap.randomize_start_goal(self) if keystate[pg.K_KP0]: self.neural.train() if keystate[pg.K_KP1]: self.neural.save(assets.neural_network_model_folder) if keystate[pg.K_KP2]: self.neural.load(assets.neural_network_model_folder) if keystate[pg.K_q]: # BFS self.pathqueue = None self.path = alg.BFS(alg.SquareGraph(self.tilemap), self.tilemap.custom_start, self.tilemap.custom_goal) if keystate[pg.K_a]: # Visual BFS self.path = alg.BFS(alg.SquareGraph(self.tilemap), self.tilemap.custom_start, self.tilemap.custom_goal) self.visual_helper() if keystate[pg.K_w]: # DFS self.pathqueue = None self.path = alg.DFS(alg.SquareGraph(self.tilemap), self.tilemap.custom_start, self.tilemap.custom_goal) if keystate[pg.K_s]: # Visual DFS self.path = alg.DFS(alg.SquareGraph(self.tilemap), self.tilemap.custom_start, self.tilemap.custom_goal) self.visual_helper() if keystate[pg.K_e]: # Dijkstra self.pathqueue = None self.path = alg.Dijkstra(alg.WeightedGraph(self.tilemap), self.tilemap.custom_start, self.tilemap.custom_goal) if keystate[pg.K_d]: # Visual Dijkstra self.path = alg.Dijkstra(alg.WeightedGraph(self.tilemap), self.tilemap.custom_start, self.tilemap.custom_goal) self.visual_helper() if keystate[pg.K_z]: # Dijkstra - only path self.pathqueue = None d_path = alg.Dijkstra(alg.WeightedGraph(self.tilemap), self.tilemap.custom_start, self.tilemap.custom_goal) self.path = alg.ReconstructPath(d_path, self.tilemap.custom_start, self.tilemap.custom_goal) if keystate[pg.K_x]: # Visual Dijkstra - only path d_path = alg.Dijkstra(alg.WeightedGraph(self.tilemap), self.tilemap.custom_start, self.tilemap.custom_goal) self.path = alg.ReconstructPath(d_path, self.tilemap.custom_start, self.tilemap.custom_goal) self.visual_helper(True) if keystate[pg.K_r]: # Astar self.pathqueue = None self.path, cost = alg.Astar(alg.WeightedGraph(self.tilemap), self.tilemap.custom_start, self.tilemap.custom_goal) if keystate[pg.K_f]: # Visual Astar self.path, cost = alg.Astar(alg.WeightedGraph(self.tilemap), self.tilemap.custom_start, self.tilemap.custom_goal) self.visual_helper() if keystate[pg.K_c]: # Astar - only path self.pathqueue = None d_path, cost = alg.Astar(alg.WeightedGraph(self.tilemap), self.tilemap.custom_start, self.tilemap.custom_goal) self.path = alg.ReconstructPath(d_path, self.tilemap.custom_start, self.tilemap.custom_goal) if keystate[pg.K_v]: # Visual Astar - only path d_path, cost = alg.Astar(alg.WeightedGraph(self.tilemap), self.tilemap.custom_start, self.tilemap.custom_goal) self.path = alg.ReconstructPath(d_path, self.tilemap.custom_start, self.tilemap.custom_goal) self.visual_helper(True) if keystate[pg.K_t]: # Astar NN self.pathqueue = None self.path, cost = alg.Astar(alg.WeightedGraph(self.tilemap), self.tilemap.custom_start, self.tilemap.custom_goal, self.neural) if keystate[pg.K_g]: # Visual Astar NN self.path, cost = alg.Astar(alg.WeightedGraph(self.tilemap), self.tilemap.custom_start, self.tilemap.custom_goal, self.neural) self.visual_helper() if keystate[pg.K_p]: # test num_tests=50 def bfs_test(): self.sprite_group_all.empty() self.tilemap.randomize_start_goal(self) self.pathqueue = None self.path = alg.BFS(alg.SquareGraph(self.tilemap), self.tilemap.custom_start, self.tilemap.custom_goal) def dfs_test(): self.sprite_group_all.empty() self.tilemap.randomize_start_goal(self) self.pathqueue = None self.path = alg.DFS(alg.SquareGraph(self.tilemap), self.tilemap.custom_start, self.tilemap.custom_goal) def dijkstra_test(): self.sprite_group_all.empty() self.tilemap.randomize_start_goal(self) self.pathqueue = None self.path = alg.Dijkstra(alg.WeightedGraph(self.tilemap), self.tilemap.custom_start, self.tilemap.custom_goal) def astar_test(): self.sprite_group_all.empty() self.tilemap.randomize_start_goal(self) self.pathqueue = None self.path, cost = alg.Astar(alg.WeightedGraph(self.tilemap), self.tilemap.custom_start, self.tilemap.custom_goal) def neural_test(): self.sprite_group_all.empty() self.tilemap.randomize_start_goal(self) self.pathqueue = None self.path, cost = alg.Astar(alg.WeightedGraph(self.tilemap), self.tilemap.custom_start, self.tilemap.custom_goal, self.neural) bfs_time = timeit.timeit(bfs_test, number=num_tests) / num_tests print("BFS: " + str(bfs_time)) dfs_time = timeit.timeit(dfs_test, number=num_tests) / num_tests print("DFS: " + str(dfs_time)) # dijkstra_time = timeit.timeit(dijkstra_test, number=num_tests) / num_tests # print("Dijkstra: " + str(dijkstra_time)) astar_time = timeit.timeit(astar_test, number=num_tests) / num_tests print("Astar: " + str(astar_time)) neural_time = timeit.timeit(neural_test, number=num_tests) / num_tests print("Neural: " + str(neural_time) + "\n\n")
def dfs_test(): self.sprite_group_all.empty() self.tilemap.randomize_start_goal(self) self.pathqueue = None self.path = alg.DFS(alg.SquareGraph(self.tilemap), self.tilemap.custom_start, self.tilemap.custom_goal)