def runStage3(self): import algorithms if self.openmessagebox: algorithm, showSteps = graphics.chooseAlgoWindow() self.openmessagebox = False status = None if algorithm == "Bfs": status = algorithms.Bfs(self.grid, self.start, self.stop, showSteps) elif algorithm == "Dfs": status = algorithms.Dfs(self.grid, self.start, self.stop, showSteps) elif algorithm == "A*": status = algorithms.Astar(self.grid, self.start, self.stop, showSteps) elif algorithm == "Greedy Best-First Search": status = algorithms.GreedyBestfs(self.grid, self.start, self.stop, showSteps) if status == 0: # no path graphics.okMessageBox("There is no path") else: graphics.okMessageBox("Path found!") self.state = 4 self.openmessagebox = True
def rungame(self): self.creategrid() running = True # variable is use to keep loop running # following variables will used in program for different purposes currentx = 0 currenty = 0 while running: for event in pygame.event.get(): # checks for the event if event.type == pygame.QUIT: # if event type is quit, then proceed to quit the window pygame.quit() # end the pygame instance quit() # closes the window # checks if thread exists and is it dead or not if self.runningthreadname is not None and self.runningthreadname.is_alive( ) is False: self.isalgorunning = False self.runningthreadname = None if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1 and ( self.isalgorunning is False): # checks if mouse button is pressed if self.dijkstrabutton.chechkifclicked( ): # checks if the dijkstra button is pressed self.cleargrid() g = algorithms.Dijkstra( self.grid ) # creates an instance of class graph from dijkstra module g.dijkstra() # gets the list of all visited nodes self.isalgorunning = True t1 = threading.Thread(target=self.print, args=[g]) self.runningthreadname = t1 t1.start() elif self.bfsbutton.chechkifclicked( ): # checks if the bfs button is pressed self.cleargrid() b = algorithms.Bfs(self.grid) b.bfs() self.isalgorunning = True t1 = threading.Thread(target=self.print, args=[b]) self.runningthreadname = t1 t1.start() elif self.dfsbutton.chechkifclicked( ): # checks if the bfs button is pressed self.cleargrid() d = algorithms.Dfs(self.grid) d.dfs() self.isalgorunning = True t1 = threading.Thread(target=self.print, args=[d]) self.runningthreadname = t1 t1.start() elif self.astarbutton.chechkifclicked( ): # checks if the bfs button is pressed self.cleargrid() a = algorithms.Astar(self.grid) a.astar() self.isalgorunning = True t1 = threading.Thread(target=self.print, args=[a]) self.runningthreadname = t1 t1.start() elif self.resetbutton.chechkifclicked(): pygame.display.flip() pygame.time.wait(100) # add time delay self.startnoderow = node.start_node_row self.startnodecolumn = node.start_node_column self.finishnoderow = node.finish_node_row self.finishnodecolumn = node.finish_node_column self.flag = 0 self.grid = [] self.creategrid() else: mousex, mousey = pygame.mouse.get_pos( ) # gets the current coordinates of mouse # by using mouse coordinates, it can be determined if any of the rectangle on screen is clicked # the following function also returns the starting coordinates of particular rectangle # if no rectangle is clicked it returns -1 currentx, currenty = self.get_rectanglepos( mousex, mousey) # by using rectangle coordinates, # the type of rectangle/node can be determined start-node, finish-node, normal nodes etc # according to type of rectangle/node the flag value is set self.flag = self.checknode(currentx, currenty) if self.flag == 3: cnode = self.getnode( currentx, currenty) # gets the node related to rectangle cnode.iswall = True # sets the iswall attribute to true self.drawrect( 8, currentx, currenty) # changes the color of rectangle if event.type == pygame.MOUSEBUTTONUP and event.button == 1 and ( self.flag == 1 or self.flag == 2) and (self.isalgorunning is False): # checks if mouse button is released mousex, mousey = pygame.mouse.get_pos( ) # gets the coordinates where mouse was released newx, newy = self.get_rectanglepos( mousex, mousey) # gets the coordinates of rectangle if self.checknode(newx, newy) == 3: # checks the type of node self.setpos(newx, newy, currentx, currenty) if event.type == pygame.MOUSEBUTTONDOWN and event.button == 3 and ( self.isalgorunning is False): mousex, mousey = pygame.mouse.get_pos() currentx, currenty = self.get_rectanglepos(mousex, mousey) self.flag = self.checknode(currentx, currenty) if self.flag == 3: cnode = self.getnode(currentx, currenty) cnode.isweight = True self.drawrect(9, currentx, currenty)
def astar_cost(self): path, cost = alg.Astar(alg.WeightedGraph(self), self.custom_start, self.custom_goal) return cost
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 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)
def get_path(self, start, goal, filter_function): return alg.Astar(self.weighted_graph, goal, start, filter_function)