def test_11x11_field(self): game = TilingPatternGame((11, 11), 2, RandomRobotReset(GripperRobot, 0), RandomWorldReset()) game.reset() self.assertEqual(36, game.num_tiles) self.assertEqual(36, np.sum(np.sum(game.grid)))
def test_move_off_screen_test_2(self): robot = GripperRobot(10, Heading.EAST, (1, 0)) game = TilingPatternGame((1, 1), 2, RandomRobotReset(GripperRobot, 0), RandomWorldReset()) robot.step([True, 0, False, False], game) self.assertEqual((1, 0), robot.location)
def test_one_by_one_field(self): game = ForagingGame((1, 1), 1, (2, 2, 1, 1), RandomRobotReset(ForagingRobot, 0), RandomWorldReset()) game.reset() self.assertEqual(1, game.num_tiles) self.assertTrue(game.grid[0][0])
def __init__(self, x_dim=7, y_dim=5, num_tiles=2, target_area=(0, 0, 1, 1), seed=None, num_robots=5, env_storage_path=None, game_cls=ForagingGame): super().__init__(seed) if env_storage_path is not None: env_storage = self.get_static_storage(env_storage_path) assert isinstance(env_storage, ForagingGameStorage) world_size = env_storage.grid.shape num_tiles = env_storage.num_tiles target_area = env_storage.target_area robot_reset = StaticRobotReset(ForagingRobot, env_storage.robot_pos) world_reset = StaticWorldReset(env_storage.grid) else: world_size = (x_dim, y_dim) robot_reset = RandomRobotReset(ForagingRobot, num_robots) world_reset = RandomWorldReset() self.game = game_cls(world_size, num_tiles, target_area, robot_reset, world_reset)
def test_inside_grid_true(self): game = TilingPatternGame((11, 11), 2, RandomRobotReset(GripperRobot, 0), RandomWorldReset()) self.assertTrue(game.inside_grid((0, 0))) self.assertTrue(game.inside_grid((10, 0))) self.assertTrue(game.inside_grid((10, 10))) self.assertTrue(game.inside_grid((0, 10)))
def test_pickup(self): robot = GripperRobot(10, Heading.WEST) game = TilingPatternGame((1, 1), 1, RandomRobotReset(GripperRobot, 0), RandomWorldReset()) game.reset() self.assertTrue(game.grid[0][0]) robot.pickup(game) self.assertTrue(robot.hold_object) self.assertFalse(game.grid[0][0])
def static_correct_storage(): game = TilingPatternGame((7, 5), 2, 0, RandomWorldReset()) game.grid = np.zeros((7, 5), dtype=int) for i in range(0, 7, 2): for j in range(0, 5, 2): game.grid[i][j] = 1 return TilingPatternGameStorage(game)
def test_inside_grid_false(self): game = TilingPatternGame((11, 11), 2, RandomRobotReset(GripperRobot, 0), RandomWorldReset()) self.assertFalse(game.inside_grid((-1, 0))) self.assertFalse(game.inside_grid((0, -1))) self.assertFalse(game.inside_grid((-1, -1))) self.assertFalse(game.inside_grid((11, 0))) self.assertFalse(game.inside_grid((0, 11))) self.assertFalse(game.inside_grid((11, 11))) self.assertFalse(game.inside_grid((-1, 11))) self.assertFalse(game.inside_grid((11, -1)))
def __init__(self, lattice_size=2, x_dim=7, y_dim=5, seed=None, num_robots=5, env_storage_path=None, game_cls=TilingPatternGame): super().__init__(seed) if env_storage_path is not None: env_storage = self.get_static_storage(env_storage_path) assert isinstance(env_storage, TilingPatternGameStorage) robot_reset = StaticRobotReset(GripperRobot, env_storage.robot_pos) world_reset = StaticWorldReset(env_storage.grid) world_size = env_storage.grid.shape lattice_size = env_storage.lattice_size else: robot_reset = RandomRobotReset(GripperRobot, num_robots) world_reset = RandomWorldReset() world_size = (x_dim, y_dim) self.game = game_cls(world_size, lattice_size, robot_reset, world_reset)
def test_drop(self): robot = ForagingRobot(10, Heading.WEST, (1, 1)) robot.hold_object = True game = ForagingGame((2, 2), 2, (0, 0, 1, 1), RandomRobotReset(ForagingRobot, 5), RandomWorldReset()) game.grid[1][1] = False robot.drop(game) self.assertFalse(robot.hold_object) self.assertTrue(game.grid[1][1])
def test_drop_foraging_area(self): robot = ForagingRobot(10, Heading.WEST) robot.hold_object = True game = ForagingGame((2, 2), 2, (0, 0, 1, 1), RandomRobotReset(ForagingRobot, 5), RandomWorldReset()) game.grid[0][0] = False self.assertEqual(0, game.get_fitness()) robot.drop(game) self.assertFalse(robot.hold_object) self.assertEqual(1, game.get_fitness())
def test_empty_observation(self): robot = ForagingRobot(10, Heading.NORTH, (5, 5)) game = ForagingGame((10, 10), 2, (0, 0, 1, 1), RandomRobotReset(ForagingRobot, 5), RandomWorldReset()) game.grid = np.zeros((10, 10)) observation = robot.get_observation(game) self.assertEqual(26, len(observation)) for i in range(len(observation)): if i in [18, 21, 22]: self.assertTrue(observation[i]) else: self.assertEqual(0, observation[i])
def test_5x5_field(self): game = TilingPatternGame((7, 5), 2, RandomRobotReset(GripperRobot, 0), RandomWorldReset()) self.assertEqual(12, game.num_tiles)
def test_one_by_one_field(self): game = TilingPatternGame((1, 1), 1, RandomRobotReset(GripperRobot, 0), RandomWorldReset()) game.reset() self.assertEqual(1, game.num_tiles) self.assertTrue(game.grid[0][0])
def default_game7x5(): return ForagingGame((7, 5), 1, (2, 2, 2, 2), RandomRobotReset(ForagingRobot, 5), RandomWorldReset())
def default_game10x10(): return TilingPatternGame((10, 10), 2, RandomRobotReset(GripperRobot, 0), RandomWorldReset())
def default_game3x3(): return ForagingGame((3, 3), 8, (2, 2, 1, 1), RandomRobotReset(ForagingRobot, 5), RandomWorldReset())
def test_drop_occupied(self): robot = ForagingRobot(10, Heading.WEST) robot.hold_object = True game = ForagingGame((2, 2), 2, (1, 1, 1, 1), RandomRobotReset(ForagingRobot, 5), RandomWorldReset()) game.grid[0][0] = True robot.drop(game) self.assertTrue(robot.hold_object) self.assertTrue(game.grid[0][0])
def default_game4x4(): return ForagingGame((4, 4), 8, (1, 2, 1, 1), RandomRobotReset(ForagingRobot, 5), RandomWorldReset())