def test_undefined_goal(): """Test all 4 map boundaries <0, >width, <0, >height. if unchecked, negatives would index backwards into grid.env[][] """ bad_goal = GridState(-1, env.get_random().y) with pytest.raises(StateDoesNotExistError): GenericAstar(env, heuristic=OctileGridHeuristic(), goal=bad_goal) bad_goal = GridState(env.get_random().x, -1) with pytest.raises(StateDoesNotExistError): GenericAstar(env, heuristic=OctileGridHeuristic(), goal=bad_goal) bad_goal = GridState(env.width + 1, env.get_random().y) with pytest.raises(StateDoesNotExistError): GenericAstar(env, heuristic=OctileGridHeuristic(), goal=bad_goal) bad_goal = GridState(env.get_random().x, env._height + 1) with pytest.raises(StateDoesNotExistError): GenericAstar(env, heuristic=OctileGridHeuristic(), goal=bad_goal)
def test_impassable_start(): """Try to create search with start node that is not passable""" bad_start = env.get_random(valid=False) with pytest.raises(StateNotValidError): GridOptimizedAstar(env, heuristic=OctileGridHeuristic(), start=bad_start)
def speed_test(env: Environment, search_1: Search.__class__, search_2: Search.__class__, start: State = None, goal: State = None, heuristic=None) -> Tuple[float, float]: if start is None: start = env.get_random(valid=True) if goal is None: goal = env.get_random(valid=True) generic_astar = GenericAstar(env, heuristic=OctileGridHeuristic(), start=SearchNode(start), goal=SearchNode(goal)) gridopt_astar = GridOptimizedAstar(env, heuristic=OctileGridHeuristic(), start=SearchNode(start), goal=SearchNode(goal)) if verbose: print('\n-----Beginning Speeedtest-----\n') print(f'environment: {str(env)}') print(f'start: {str(start)}') print(f'goal: {str(goal)}') print(f'heuristic: {str(OctileGridHeuristic())}') print('\nrunning GenericAstar...') generic_t1 = time.time() generic_astar.get_path() generic_t2 = time.time() generic_time = generic_t2 - generic_t1 if verbose: print(f'completed in: ' + str(round(generic_time, 2)) + 's') print('\nrunning GridOptimizedAstar...') gridopt_t1 = time.time() gridopt_astar.get_path() gridopt_t2 = time.time() gridopt_time = gridopt_t2 - gridopt_t1 if verbose: print(f'completed in: ' + str(round(gridopt_time, 2)) + 's') print( f"\n{'GenericAstar' if generic_time < gridopt_time else 'GridOptimizedAstar'} was faster!" ) if not verbose: print(generic_time + ' ' + gridopt_time)
def get_random_search( environment: Environment = env, heuristic: Heuristic = OctileGridHeuristic() ) -> GenericAstar: start = environment.get_random(valid=True) goal = environment.get_random(valid=True) gastar = GenericAstar(environment, heuristic, start=start, goal=goal) return gastar
def test_simple_randomized_search(): """Test a simple reandomized search. Start and goal should be chosen to be a random, valid GridState because that is default behavior when they are not provided. As long as the state space is not disconnected, a path should be returned and it must include at least the start ang goal """ astar = GenericAstar(env, heuristic=OctileGridHeuristic()) path = astar.get_path() # simplest possible path must include start and goal # each should have been chosen for expansion from the open list assert astar.nodes_expanded >= 2 assert len(path) >= 2
def speed_diff(): filepath = os.path.join( os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + '/data/maps/large/brc202d.map') environment = OctileGrid(filepath) start = environment.get_random(valid=True) goal = GridState(474, 426) generic_astar = GenericAstar(environment, heuristic=OctileGridHeuristic(), start=SearchNode(start), goal=SearchNode(goal), verbose=True) gridopt_astar = GridOptimizedAstar(environment, heuristic=OctileGridHeuristic(), start=SearchNode(start), goal=SearchNode(goal)) print('\n-----Beginning Speeedtest-----\n') print(f'environment: {str(environment)}') print(f'start: {str(start)}') print(f'goal: {str(goal)}') print(f'heuristic: {str(OctileGridHeuristic())}') print('\nrunning GenericAstar...') generic_t1 = time.time() generic_astar.get_path() generic_t2 = time.time() generic_time = generic_t2 - generic_t1 print(f'completed in: ' + str(round(generic_time, 2)) + 's') print('\nrunning GridOptimizedAstar...') gridopt_t1 = time.time() gridopt_astar.get_path() gridopt_t2 = time.time() gridopt_time = gridopt_t2 - gridopt_t1 print(f'completed in: ' + str(round(gridopt_time, 2)) + 's') print( f"\n{'GenericAstar' if generic_time < gridopt_time else 'GridOptimizedAstar'} was faster!" )
def test_simple_shortest_search(): """Test a search where the start and goal are neighbors. Ensure correct path and open list sizes. Assumes start is in an 'open area' where all neighbors are valid. """ start = GridState(18, 24, valid=True) goal = GridState(19, 24, valid=True) astar = GenericAstar(env, heuristic=OctileGridHeuristic(), start=start, goal=goal) astar.get_path() print(astar.open) assert len(astar.path) == 2 # start, goal assert astar.nodes_expanded == 2 # start, goal assert len(astar.open) == 7 # start + 8 neighbors - start - goal
def test_invalid_goal(): """Try to create search with goal node that is not passable""" bad_goal = env.get_random(valid=False) with pytest.raises(StateNotValidError): GenericAstar(env, heuristic=OctileGridHeuristic(), goal=bad_goal)
def pressed_start(self) -> None: if self.start is None or self.goal is None: raise Exception('Search started without start / goal') search = 'gas' if self.app_frame.combo1.get() == 'Generic Dijkstra': search = 'gd' elif self.app_frame.combo1.get() == 'Grid Optimized Dijkstra': search = 'god' elif self.app_frame.combo1.get() == 'Grid Optimized A*': search = 'goas' elif self.app_frame.combo1.get() == 'BFS': search = 'bfs' elif self.app_frame.combo1.get() == 'DFS': search = 'dfs' heuristic = OctileGridHeuristic() if self.app_frame.combo2.get() == 'Manhattan Distance': heuristic = ManhattanGridHeuristic() elif self.app_frame.combo2.get() == 'Euclidean Distance': heuristic = EuclideanGridHeuristic() if not self.env.is_valid(self.start.state): raise StateNotValidError(self.start.state) else: self.start.state._valid = True if not self.env.is_valid(self.goal.state): raise StateNotValidError(self.goal.state) else: self.goal.state._valid = True if search == 'gas': self.search = GenericAstar(self.env, start=self.start, goal=self.goal, heuristic=heuristic, verbose=self.verbose) elif search == 'gd': self.search = GenericDijkstra(self.env, start=self.start, goal=self.goal, verbose=self.verbose) elif search == 'god': self.search = GridOptimizedDijkstra(self.env, start=self.start, goal=self.goal, verbose=self.verbose) elif search == 'goas': self.search = GridOptimizedAstar(self.env, start=self.start, goal=self.goal, heuristic=heuristic, verbose=self.verbose) elif search == 'bfs': self.search = GenericBFS(self.env, start=self.start, goal=self.goal, verbose=self.verbose) elif search == 'dfs': self.search = GenericDFS(self.env, start=self.start, goal=self.goal, verbose=self.verbose) self.draw_initial() self.after(1000, self.draw_step)