def test_add_multiple_prey(self): pred1 = Predator('1', Coord(0, 0), self.env, 8) prey1 = Prey('1', Coord(2, 3), self.env, perception_radius=8) prey1.perceive() #print(pred1._beliefs) self.assertEqual(prey1.find_nearest_predator(), pred1) prey2 = Prey('2', Coord(9, 5), self.env, perception_radius=8, speed=10) pred2 = Predator('3', Coord(0, 7), self.env, perception_radius=2) prey2.perceive() self.assertEqual(prey2.find_nearest_predator(), None) prey2.move(Coord(9, 9))
def test_actuate(self): pred1 = Predator('1', Coord(0, 0), self.env, 5, speed=1) pred2 = Predator('2', Coord(0, 1), self.env, 5, speed=2) prey1 = Prey('1', Coord(0, 4), self.env) pred1.perceive() pred1.actuate('simple_hunt_strategy') self.assertEqual(pred1.current_coord, self.env._get_coord(Coord(1, 0))) pred2.perceive() pred2.actuate('simple_hunt_strategy') self.assertEqual(pred2.current_coord, self.env._get_coord(Coord(0, 3)))
def test_best_move(self): pred1 = Predator('1', Coord(0, 0), self.env, 5, 1) pred2 = Predator('2', Coord(0, 1), self.env, 2) prey1 = Prey('1', Coord(0, 3), self.env) pred1.perceive() nearest_prey = pred1.find_nearest_prey() pred1.perceive() self.assertEqual(nearest_prey, prey1) prey_location_details = pred1.get_prey_path(nearest_prey) best_move = pred1.get_best_move(prey_location_details) self.assertEqual(best_move, self.env._get_coord(Coord(1, 0))) pred1.actuate() self.assertEqual(pred1.current_coord, self.env._get_coord(Coord(1, 0)))
def test_get_dist(self): coord1 = Coord(1, 1) coord2 = Coord(2, 2) coord3 = Coord(10, 10) self.assertEqual(coord1.get_dist(coord2), 2) self.assertEqual(coord1.get_dist(coord3), 18) with self.assertRaises(NotImplementedError): coord3.get_dist(None)
def test_move_pawns(self): pawn1 = GridPawn('1', Coord(0, 0), self.env) pawn2 = GridPawn('2', Coord(0, 1), self.env) pawn3 = GridPawn('3', Coord(9, 9), self.env) pawn1.move(Coord(5, 5)) self.assertEqual(pawn1.current_coord, Coord(5, 5)) self.assertTrue(pawn1.current_coord in self.env.get_occupied_coords()) pawn2.move(Coord(0, 0)) self.assertEqual(pawn2.current_coord, Coord(0, 0)) with self.assertRaises(Exception): pawn1.move(Coord(0, 0)) self.assertTrue(pawn1.move(Coord(1, 1)))
def place_pawn(self, pawn: GridPawn, coord: Coord): '''Places a pawn on the grid at position coord if the coordinate is free''' coord = self._get_coord(coord) if not self.coord_occupied(coord): if pawn in self.grid_pawns: raise Exception( 'Pawn is already in the grid; move it with move_pawn') else: self.grid_pawns.append(pawn) self.occupied_coords.append(coord) coord.set_occupied(True) coord.occupied_val = pawn return coord else: raise CoordOutOfBoundsException( 'Coordinate {} is already occupied.'.format(coord.__str__()))
def get_neighbor_coords(self, coord: Coord): '''returns all non-diagonal adjacent nodes that do not contain predators. Used for BFS to find prey but avoid other predators''' return list( filter( lambda x: coord.get_dist(x) == 1, list( filter(lambda x: not isinstance(x.get_value(), Predator), self.coords))))
def test_add_multiple_predator(self): pred1 = Predator('1', Coord(0,0), self.env, 8) prey1 = Prey('2', Coord(2,3), self.env, speed = 20) pred1.perceive() #print(pred1._beliefs) self.assertEqual(pred1.find_nearest_prey(), prey1) pred2 = Predator('3', Coord(0,1), self.env, 2) self.assertEqual(pred2.find_nearest_prey(), None) prey1.move(Coord(9,9)) print(self.env.print_board()) #predator has not perveived so still believes prey is near it self.assertEqual(pred1.find_nearest_prey(), prey1) pred1.perceive() #once predator has perceived, finds prey has moved self.assertEqual(pred1.find_nearest_prey(), None)
def __init__(self, rows=10, columns=10): '''constructs a rowsXColumns grid environment. Coordinates are in the range (0->(rows-1),(0->(columns-1)))''' self.rows = rows self.columns = columns self.coords = [ Coord(x, y) for x in range(rows) for y in range(columns) ] self.occupied_coords = [] self.grid_pawns = []
def __init__(self, name, coord: Coord, env): '''initialises the pawn's environment and places the pawn in the environment''' self.env = env self.name = 'Pn' + name if (self.env.coord_occupied(coord)): raise CoordOccupiedException('Coordinate {} is already occupied', coord.__str__()) else: env_coord = self.env.place_pawn(self, coord) self.current_coord = env_coord
def test_bfs(self): print('\n\n\n\n\n') print( 'bfs result: ', self.env.bfs(Coord(0, 0), Coord(3, 3), self.env.get_neighbor_coords)) self.assertEqual( self.env.bfs(Coord(0, 0), Coord(3, 3), self.env.get_neighbor_coords)[0], 6) self.assertEqual( self.env.bfs(Coord(0, 0), Coord(0, 3), self.env.get_neighbor_coords), (3, list( map(lambda x: self.env._get_coord(x), [Coord(0, 3), Coord(0, 2), Coord(0, 1), Coord(0, 0)]))))
def print_board(self) -> str: padding = ' |' self.__log.info_string = ' |' for i in range(self.rows): self.__log.info_string += ' {} |'.format(i) self.__log.info_string += '\n ' + '-' * len( padding) * self.rows + '\n' for x_coord in range(self.columns): self.__log.info_string += ' {}|'.format(x_coord) for y_coord in range(self.rows): if self.coord_occupied(self._get_coord(Coord(x_coord, y_coord))): pawn = self._get_coord(Coord(x_coord, y_coord)).get_value() self.__log.info_string += ' {} |'.format(pawn.name) else: self.__log.info_string += padding self.__log.info_string += '\n ' + '-' * len( padding) * self.rows + '\n' return self.__log.info_string
def move_pawn(self, pawn: GridPawn, coord: Coord): '''Moves a pawn that has already been placed on the grid to a new position''' if pawn in self.grid_pawns: coord = self._get_coord(coord) if not self.coord_occupied(coord): self.__log.info('moving pawn {} to coordinate {}'.format( pawn.__str__(), coord.__str__())) pawn.current_coord.set_occupied(False) pawn.current_coord.occupied_val = None self.occupied_coords.remove(pawn.current_coord) self.occupied_coords.append(coord) coord.set_occupied(True) coord.occupied_val = pawn return coord else: raise CoordOccupiedException( 'Coord {} is already occupied, cannot move pawn'.format( coord.__str__())) else: raise Exception( 'Please place pawn on the grid first using place_pawn')
def find_nearest_Agent(self, AgentType: 'Predator, Prey or other derived class'): '''returns the nearest predator or prey to the current agent''' perceived_nearest_agent_dist = Coord(self.env.columns, self.env.rows).get_dist( Coord(0, 0)) perceived_nearest_agent = None #self.__log.info('beliefs: ', self._beliefs) #self.__log.info(sorted(self._beliefs.items(), key = lambda x: x[1].get_dist(self))) for agent_key, agent_value in self._beliefs.items(): #if the distance from the nearest prey to the predator is less than the current #nearest prey, update current_prey #self.__log.info('prey_value: ', prey_value) if isinstance(agent_key, AgentType) and isinstance( agent_value, Coord): if agent_value.get_dist( self.current_coord) <= perceived_nearest_agent_dist: perceived_nearest_agent = agent_key perceived_nearest_agent_dist = agent_value.get_dist( self.current_coord) self.__log.info('{} detected nearest Agent: {}'.format( self, perceived_nearest_agent)) return perceived_nearest_agent
def test_move_pawnAgent(self): pawn1 = GridPawnAgent('1', Coord(0, 0), self.env) pawn2 = GridPawnAgent('2', Coord(5, 5), self.env) pawn3 = GridPawnAgent('3', Coord(5, 6), self.env) self.assertEqual(self.env.grid_pawns, [pawn1, pawn2, pawn3]) self.assertEqual(self.env.get_occupied_coords(), [ self.env._get_coord(Coord(0, 0)), self.env._get_coord(Coord(5, 5)), self.env._get_coord(Coord(5, 6)) ]) pawn1.move(Coord(1, 0)) self.assertFalse( self.env._get_coord(Coord(0, 0)) in self.env.occupied_coords) print(self.env.print_board())
def test_bfs(self): pred1 = Predator('1', Coord(0, 0), self.env) pred2 = Predator('2', Coord(0, 1), self.env) prey1 = Prey('1', Coord(0, 2), self.env) #should be 1,0 1,1 1,2 print('\n\n\n Testing bfs') bfs_res = self.env.bfs(pred1.current_coord, prey1.current_coord)[1] self.assertEqual( bfs_res, list( map(lambda x: self.env._get_coord(x), [ Coord(0, 2), Coord(1, 2), Coord(1, 1), Coord(1, 0), Coord(0, 0) ])))
def test_set_up_env(self): self.assertEqual(self.env.rows, 10) self.assertEqual(self.env.columns, 10) self.assertEqual(self.env.get_unoccupied_coords(), self.env.coords) self.assertEqual(self.env.get_occupied_coords(), []) self.assertEqual(self.env.grid_pawns, []) self.assertEqual(self.env.get_neighbor_coords(Coord(0, 0)), [Coord(0, 1), Coord(1, 0)]) self.assertTrue( all( list( filter( lambda x: x in self.env.get_neighbor_coords(Coord( 3, 3)), [Coord(3, 2), Coord(3, 4), Coord(2, 3), Coord(4, 3)]))))
def test_get_naive_best_move(self): pred1 = Predator('1', Coord(0, 0), self.env, perception_radius=8) prey1 = Prey('1', Coord(0, 3), self.env, perception_radius=10) prey1.perceive() best_naive_move = prey1.get_naive_best_move(pred1.current_coord) print('best move: {}'.format(best_naive_move)) self.assertEqual(best_naive_move, self.env._get_coord(Coord(0, 4))) pred2 = Predator('2', Coord(5, 5), self.env, perception_radius=8) prey2 = Prey('2', Coord(6, 9), self.env, perception_radius=10) prey2.perceive() self.assertEqual(prey2.get_naive_best_move(pred2.current_coord), self.env._get_coord(Coord(7, 9))) pred3 = Predator('3', Coord(9, 9), self.env, perception_radius=8) prey2.perceive() nearest_pred = prey2.find_nearest_predator() self.assertEqual(nearest_pred, pred3) self.assertEqual(prey2.get_naive_best_move(nearest_pred.current_coord), self.env._get_coord(Coord(5, 9)))
def test_add_multiple_pawns(self): pawn1 = GridPawn('1', Coord(0, 0), self.env) pawn2 = GridPawn('2', Coord(0, 1), self.env) pawn3 = GridPawn('3', Coord(9, 9), self.env) self.assertEqual(self.env.grid_pawns, [pawn1, pawn2, pawn3]) self.assertEqual( self.env.get_occupied_coords(), [Coord(0, 0), Coord(0, 1), Coord(9, 9)]) self.env.remove_pawn(pawn1) self.assertTrue(set(self.env.grid_pawns) == set([pawn2, pawn3])) self.assertEqual(self.env.get_unoccupied_coords(), [ x for x in self.env.coords if x not in self.env.get_occupied_coords() ])
def test_perceive_pawnAgent(self): pawn1 = GridPawnAgent('1', Coord(4, 4), self.env, speed=10) pawn2 = GridPawnAgent('1', Coord(6, 6), self.env) pawn1.perceive() self.assertTrue(pawn1._beliefs, {}) pawn1.perceive() self.assertEqual(pawn1._beliefs, {pawn2: self.env._get_coord(Coord(6, 6))}) print(pawn1._beliefs) print(pawn2._beliefs) self.assertTrue(pawn2._beliefs == {}) pawn2.perceive() self.assertEqual(pawn2._beliefs, {pawn1: self.env._get_coord(Coord(4, 4))}) pawn1.move(Coord(0, 0)) pawn1.perceive() self.assertEqual(pawn1._beliefs, {pawn2: None}) self.assertEqual(pawn2._beliefs, {pawn1: self.env._get_coord(Coord(4, 4))}) pawn2.perceive() self.assertEqual(pawn2._beliefs, {pawn1: None})
def test_y(self): new_coord = Coord(1, 1) self.assertEqual(new_coord.get_y(), 1) new_coord.set_y(5) self.assertEqual(new_coord.get_y(), 5)
def test_get_neighbor_coords(self): '''makes sure that the get_neighbor_coords method can detect neightboring nodes as long as another predator is not occupying them''' self.assertTrue( all( list( filter( lambda x: x in self.env.get_neighbor_coords(Coord( 0, 0)), [Coord(0, 1), Coord(1, 0)])))) pred1 = Predator('1', Coord(1, 1), self.env) pred2 = Predator('2', Coord(0, 1), self.env) self.assertTrue( all( list( filter( lambda x: x in self.env.get_neighbor_coords( pred1.current_coord), [Coord(1, 0), Coord(2, 1), Coord(1, 2)])))) prey1 = Prey('3', Coord(2, 1), self.env) self.assertTrue( all( list( filter( lambda x: x in self.env.get_neighbor_coords( pred1.current_coord), [Coord(1, 0), Coord(2, 1), Coord(1, 2)])))) #all(list(filter(lambda x: x in self.env.get_neighbor_coords(Coord(3,3)), [Coord(3,2),Coord(3,4),Coord(2,3),Coord(4,3)])))) pred3 = Predator('4', Coord(1, 2), self.env) self.assertTrue( all( list( filter( lambda x: x in self.env.get_neighbor_coords( pred1.current_coord), [Coord(1, 0), Coord(2, 1)]))))
def test_add_single_prey(self): pred1 = Predator('1', Coord(0, 0), self.env, 6) prey1 = Prey('2', Coord(2, 3), self.env, speed=3, perception_radius=6) prey1.perceive() self.assertEqual(prey1.find_nearest_predator(), pred1)
def test_add_pawnAgent(self): pawn1 = GridPawnAgent('1', Coord(0, 0), self.env) self.assertEqual(self.env.occupied_coords, [self.env._get_coord(Coord(0, 0))]) self.assertEqual(self.env.grid_pawns, [pawn1])
def test_pawnAgent_actuate(self): pawn1 = GridPawnAgent('1', Coord(0, 0), self.env) with self.assertRaises(NotImplementedError): pawn1.actuate()
def test_best_move_different_speeds(self): '''Ensures that predators can move at different speeds to chase prey''' pred1 = Predator('1', Coord(0, 0), self.env, 5, 1) pred2 = Predator('2', Coord(0, 1), self.env, 5, speed=2) prey1 = Prey('1', Coord(0, 4), self.env) pred2.perceive() pred2_nearest_prey = pred2.find_nearest_prey() self.assertEqual(pred2_nearest_prey, prey1) pred2_prey_path = pred2.get_prey_path(pred2_nearest_prey) print('pred2 speed: {}'.format(pred2.speed)) print('pred2_prey_path: {}'.format(pred2_prey_path)) print('pred2 available moves: {}'.format(pred2.find_available_moves())) self.assertTrue( all(x in list( map(lambda x: self.env._get_coord(x), [ Coord(0, 2), Coord(0, 3), Coord(1, 0), Coord(1, 1), Coord(1, 2), Coord(2, 1) ])) for x in pred2.find_available_moves())) self.assertTrue( all(x in pred2.find_available_moves() for x in list( map(lambda x: self.env._get_coord(x), [ Coord(0, 2), Coord(0, 3), Coord(1, 0), Coord(1, 1), Coord(1, 2), Coord(2, 1) ])))) best_move = pred2.get_best_move( pred2.get_prey_path(pred2_nearest_prey)) self.assertEqual(best_move, self.env._get_coord(Coord(0, 3)))
def test_add_single_pawn(self): pawn1 = GridPawn('1', Coord(0, 0), self.env) self.assertEqual(pawn1.current_coord, Coord(0, 0)) self.assertEqual(self.env.get_occupied_coords(), [Coord(0, 0)]) self.assertEqual(self.env.grid_pawns, [pawn1])
def test_x(self): new_coord = Coord(2, 2) self.assertEqual(new_coord.get_x(), 2) new_coord.set_x(10) self.assertEqual(new_coord.get_x(), 10) self.assertFalse(new_coord.get_occupied())
def test_str(self): new_coord = Coord(1, 1) self.assertEqual(new_coord.__str__(), str(new_coord.get_x()) + ',' + str(new_coord.get_y()))
def test_pawnAgent_available_squares(self): pawn1 = GridPawnAgent('1', Coord(0, 0), self.env) pawn2 = GridPawnAgent('2', Coord(4, 4), self.env) self.assertTrue( all([ x in list( map(lambda x: self.env._get_coord(x), [Coord(4, 3), Coord(4, 5), Coord(3, 4), Coord(5, 4)])) for x in pawn2._find_available_squares(1) ])) self.assertTrue( all([ x in list( map(lambda x: self.env._get_coord(x), [ Coord(4, 3), Coord(4, 5), Coord(3, 4), Coord(5, 4), Coord(2, 4), Coord(6, 4), Coord(4, 2), Coord(4, 6), Coord(3, 5), Coord(5, 5), Coord(3, 3), Coord(5, 3) ])) for x in pawn2._find_available_squares(2) ])) self.assertTrue( all([ x in pawn2._find_available_squares(2) for x in list( map(lambda x: self.env._get_coord(x), [ Coord(4, 3), Coord(4, 5), Coord(3, 4), Coord(5, 4), Coord(2, 4), Coord(6, 4), Coord(4, 2), Coord(4, 6), Coord(3, 5), Coord(5, 5), Coord(3, 3), Coord(5, 3) ])) ])) self.assertFalse( all([ x in list( map(lambda x: self.env._get_coord(x), [ Coord(4, 3), Coord(4, 5), Coord(3, 4), Coord(5, 4), Coord(2, 4), Coord(6, 4), Coord(4, 2), Coord(4, 6), Coord(3, 5), Coord(5, 5), Coord(3, 3), Coord(5, 3) ])) for x in pawn2._find_available_squares(3) ])) pawn2.move(Coord(4, 5)) pawn3 = GridPawnAgent('3', Coord(4, 4), self.env, 5, 2) self.assertTrue( all([ x in pawn3.find_available_moves() for x in list( map(lambda x: self.env._get_coord(x), [ Coord(4, 3), Coord(3, 4), Coord(5, 4), Coord(2, 4), Coord(6, 4), Coord(4, 2), Coord(4, 6), Coord(3, 5), Coord(5, 5), Coord(3, 3), Coord(5, 3) ])) ])) self.assertFalse( all([ x in pawn3.find_available_moves() for x in list( map(lambda x: self.env._get_coord(x), [ Coord(4, 3), Coord(4, 5), Coord(3, 4), Coord(5, 4), Coord(2, 4), Coord(6, 4), Coord(4, 2), Coord(4, 6), Coord(3, 5), Coord(5, 5), Coord(3, 3), Coord(5, 3) ])) ]))