def test__ConvertWorldImageToWeightedMatrix_EmptyGrid(self): Maze = MazeFacade(DIYMazeParser(8)) Provider = UnifiedWorldImageProvider(maze=Maze, config=DictionaryConfigProvider()) weights = {NodeStateEnum.Clear: 2, NodeStateEnum.Obs: np.inf, NodeStateEnum.UnExplored: 1, NodeStateEnum.Ant: np.inf} Planner = AntPathPlanner(safetyRadius=2, cellTypeWeights=weights, stabilityFactor=1) StartPosition = Position(0, 0) mazeMatrix = Maze.GetMatrix() manuallyAdjustedMaze = np.where(mazeMatrix == 1, NodeStateEnum.UnExplored, NodeStateEnum.Clear) manuallyAdjustedMaze[0, 0] = NodeStateEnum.Clear singleAntWorldImage = SimpleSingleAntWorldImage(manuallyAdjustedMaze, {}) result = Planner._AntPathPlanner__ConvertWorldImageToWeightedMatrix(StartPosition, singleAntWorldImage) Width, Height = result.shape for i in range(0, Width): for j in range(0, Height): if i == 0 and j == 0: self.assertEqual(result[i][j], weights[NodeStateEnum.Clear]) else: self.assertEqual(result[i][j], weights[NodeStateEnum.UnExplored])
def test__CalculatePathToDestination_UniformGrid(self): Maze = MazeFacade(DIYMazeParser(8)) Provider = UnifiedWorldImageProvider(maze=Maze, config=DictionaryConfigProvider()) weights = {NodeStateEnum.Clear: 2, NodeStateEnum.Obs: np.inf, NodeStateEnum.UnExplored: 1, NodeStateEnum.Ant: np.inf} Planner = AntPathPlanner(safetyRadius=2, cellTypeWeights=weights, stabilityFactor=1) StartPosition = Position(0, 0) mazeMatrix = Maze.GetMatrix() manuallyAdjustedMaze = np.where(mazeMatrix == 1, NodeStateEnum.UnExplored, NodeStateEnum.Clear) manuallyAdjustedMaze[0, 0] = NodeStateEnum.Clear singleAntWorldImage = SimpleSingleAntWorldImage(manuallyAdjustedMaze, {}) weightedMatrix = Planner._AntPathPlanner__ConvertWorldImageToWeightedMatrix(StartPosition, singleAntWorldImage) pathMatrix = Dijkstra(weightedMatrix, StartPosition) resultRoute = Planner._AntPathPlanner__CalculatePathToDestination(StartPosition, Position(4, 4), pathMatrix) weightSum = 0 for position in resultRoute: weightSum += weightedMatrix[position.X][position.Y] self.assertEqual(pathMatrix[position.X][position.Y], weightSum) def printRoute(route): for position in route: print(f'[{position.X}, {position.Y}] ')
def GetMaze(configprovider: BaseConfigProvider) -> MazeFacade: filename = configprovider.GetConfigValueForSectionAndKey( 'MAZE', 'filename') filename = os.path.join(os.path.dirname(__file__), filename) parser = FileMazeParser(filename) # parser=DIYMazeParser(5) return MazeFacade(parser)
def __init__(self, config, maze_facade: MazeFacade): self.__conncetivity_radius = (int)( config.GetConfigValueForSectionAndKey("SimpleAnt", "connectivity_range")) self.__maze_facade = maze_facade self.__enterence_position = maze_facade.GetEnterence() self.__enterence_key = 1000
def test_GetAntWorldImage_NoObs_Center_2(self): mazeparser = DIYMazeParser(5) mazeparser.SetExits([Position(4, 4)]) mazefacade = MazeFacade(mazeparser) WorldImageProvider = SimpleWorldImageProvider(self.__Config, mazefacade) ant = RemoteControlledAnt(1, self.__Config) ant.UpdatePosition(position=Position(2, 1)) antworldimage = WorldImageProvider.GetAntWorldImage(ant) self.assertTrue(len(antworldimage.VisibleNodes) == 20)
def __init__(self, id: int, maze: MazeFacade, connectivty_calculator: connectivty_calculator, config): BasicAnt.__init__(self, id, config) self.__movement_range = (int)(config.GetConfigValueForSectionAndKey( "SimpleAnt", "AllowedMovement")) self.__connectivty_calculator = connectivty_calculator self.__maze = maze self.__old_choice = [] self.__path_builder = path_builder(config, maze) self.__explored_map = np.ones(maze.GetDims()) self.__last_steps = [] self.__last_steps_depth = 2
def __build_connectivity_matrix(self,maze_facade:MazeFacade): self.__path_graph=nx.Graph() maze_width,maze_height=maze_facade.GetDims() for x in range(maze_width): for y in range(maze_height): node=self.__coord_to_node(x,y) self.__path_graph.add_node(node) for x in range(maze_width): for y in range(maze_height): node = self.__coord_to_node(x, y) left_node_x=x-1 right_node_x = x + 1 top_node_y=y-1 bottom_node_y=y+1 connected_nodes=[] connected_nodes.append(Position(x=x,y=top_node_y)) connected_nodes.append(Position(x=x, y=bottom_node_y)) connected_nodes.append(Position(x=left_node_x, y=y)) connected_nodes.append(Position(x=right_node_x, y=y)) for connected_node in connected_nodes: if(maze_facade.is_in_bounds(connected_node)): self.__path_graph.add_edge(node,self.__position_to_node(connected_node))
def test_UpdateStep_SeeObs(self): mazeparser = DIYMazeParser(5) mazeparser.SetExits([Position(4, 4)]) mazeparser.SetObs([Position(3, 3)]) mazefacade = MazeFacade(mazeparser) WorldImageProvider = SimpleWorldImageProvider(self.__Config, mazefacade) ant = RemoteControlledAnt(1, self.__Config) ant.UpdatePosition(position=Position(4, 4)) WorldImageProvider.ProcessStep(ant, AntStep(1, Position(4, 4))) WorldImageProvider.UpdatePositionsAccordingToMoves() antworldimage = WorldImageProvider.GetAntWorldImage(ant) obsList = list( filter(lambda x: x.NodeState == NodeStateEnum.Obs, antworldimage.VisibleNodes)) self.assertTrue(len(obsList) == 1)
def test_SingleScout(self): Maze = MazeFacade(DIYMazeParser(8)) Provider = UnifiedWorldImageProvider(maze=Maze, config=DictionaryConfigProvider()) weights = {NodeStateEnum.Clear: 2, NodeStateEnum.Obs: np.inf, NodeStateEnum.UnExplored: 1, NodeStateEnum.Ant: np.inf} ant = AlgoAnt(id=1, config=DictionaryConfigProvider(), position=Position(3, 3)) Provider.ProcessStep(ant, AntStep(ant.ID, ant.CurrentPosition)) Provider.UpdatePositionsAccordingToMoves() Planner = AntPathPlanner(safetyRadius=0, cellTypeWeights=weights) result = Planner._AntPathPlanner__ConvertWorldImageToWeightedMatrix(Position(0, 0), Provider.GetAntWorldImage(ant)) print(result)
def test_MazeFacade_Dims(self): mazeFacade = MazeFacade(self.__mazeparser) self.assertTrue(mazeFacade.GetDims() == (31, 31))
def test_MazeFacade_Entrence(self): mazeFacade = MazeFacade(self.__mazeparser) self.assertTrue(mazeFacade.GetEnterence() == Position(0, 0))
def test_MazeFacade_NotEmpty(self): mazeFacade = MazeFacade(self.__mazeparser) self.assertTrue(mazeFacade.GetMatrix().any())
def test_MazeFacade_mayMove_SamePoint(self): mazeFacade = MazeFacade(self.__mazeparser) self.assertTrue(mazeFacade.MayMove(src=Position(0,0),dst=Position(0,0)))
def test_MazeFacade_mayMove_False_Path_Too_Long(self): mazeFacade = MazeFacade(self.__mazeparser) self.assertFalse(mazeFacade.MayMove(src=Position(2, 1), dst=Position(2, 6)))
def test_MazeFacade_mayMove_False_Node_Not_InGrpah(self): mazeFacade = MazeFacade(self.__mazeparser) self.assertFalse(mazeFacade.MayMove(src=Position(2, 1), dst=Position(2, 2)))
def test_MazeFacade_mayMove_True_2(self): mazeFacade = MazeFacade(self.__mazeparser) self.assertTrue(mazeFacade.MayMove(src=Position(14,8),dst=Position(15,8)))