예제 #1
1
    def test_quadrant_angles(self):
        """> Check if drone cannot force itself through a wall."""
        # Folder must be root to load in make_net properly
        if os.getcwd().split('\\')[-1] == 'tests': os.chdir('..')

        # Create the lines
        zero = Vec2dCy(0, 0)
        a = Vec2dCy(1, 0)
        b = Vec2dCy(0, 1)
        c = Vec2dCy(-1, 0)
        d = Vec2dCy(0, -1)
        line1 = Line2dCy(zero, a)
        line2 = Line2dCy(zero, b)
        line3 = Line2dCy(zero, c)
        line4 = Line2dCy(zero, d)

        # Tests
        self.assertTrue(0 - EPSILON <= line1.get_orientation() %
                        (2 * pi) <= 0 + EPSILON)
        self.assertTrue(pi / 2 - EPSILON <= line2.get_orientation() %
                        (2 * pi) <= pi / 2 + EPSILON)
        self.assertTrue(pi - EPSILON <= line3.get_orientation() %
                        (2 * pi) <= pi + EPSILON)
        self.assertTrue(3 * pi / 2 - EPSILON <= line4.get_orientation() %
                        (2 * pi) <= 3 * pi / 2 + EPSILON)
예제 #2
1
    def test_negative_component(self):
        """> Test for line-segments with negative components."""
        # Folder must be root to load in make_net properly
        if os.getcwd().split('\\')[-1] == 'tests': os.chdir('..')

        # Create simple lines
        a = Vec2dCy(1, 1)
        b = Vec2dCy(1, -1)
        c = Vec2dCy(-1, -1)
        d = Vec2dCy(-1, 1)
        line1 = Line2dCy(a, b)
        line2 = Line2dCy(b, c)
        line3 = Line2dCy(c, d)
        line4 = Line2dCy(d, a)
        diag1 = Line2dCy(a, c)
        diag2 = Line2dCy(b, d)

        # Test the length
        self.assertTrue(2 - EPSILON <= line1.get_length() <= 2 + EPSILON)
        self.assertTrue(2 - EPSILON <= line2.get_length() <= 2 + EPSILON)
        self.assertTrue(2 - EPSILON <= line3.get_length() <= 2 + EPSILON)
        self.assertTrue(2 - EPSILON <= line4.get_length() <= 2 + EPSILON)
        self.assertTrue(
            sqrt(8) - EPSILON <= diag1.get_length() <= sqrt(8.) + EPSILON)
        self.assertTrue(
            sqrt(8) - EPSILON <= diag2.get_length() <= sqrt(8.) + EPSILON)
예제 #3
1
    def test_negative(self):
        """> Test the distance when the position of the agent is negative."""
        # Folder must be root to load in make_net properly
        if os.getcwd().split('\\')[-1] == 'tests': os.chdir('..')

        # Create empty game
        game = get_game()

        # Update player and target position
        game.target = Vec2dCy(1, 1)
        game.set_player_init_pos(Vec2dCy(-1, -1))
        game.set_player_init_angle(0)

        # Ask for the distance
        game.get_observation()
        self.assertAlmostEqual(game.get_distance_to_target(),
                               sqrt(8),
                               delta=EPSILON_DISTANCE)
예제 #4
0
    def test_none_zero_drive(self):
        """Test if the delta-distance sensor is non-zero when driving with high frame-rate."""
        # Folder must be root to load in make_net properly
        if os.getcwd().split('\\')[-1] == 'tests': os.chdir('..')

        # Create empty game
        game = get_game()
        game.game_config.fps = 100  # Put greater FPS to test the extremes

        # Update player and target position
        game.target = Vec2dCy(10, 1)
        game.set_player_init_pos(Vec2dCy(1, 1))
        game.set_player_init_angle(0)

        # Update the sensors used to only include the delta-distance sensor
        game.player.sensors = dict()
        game.player.add_delta_distance_sensor()
        game.player.add_distance_sensor(
        )  # Last sensor must always be the distance sensor

        # Drive forward for 10 simulated seconds
        start = True
        for _ in range(10 * game.game_config.fps):
            obs = game.step(l=1, r=1)
            if start:  # Cold start, reading of 0
                self.assertAlmostEqual(obs[D_SENSOR_LIST][0],
                                       0.0,
                                       delta=EPSILON_DISTANCE)
                start = False
            else:
                self.assertGreater(obs[D_SENSOR_LIST][0],
                                   0.0)  # Must be strictly greater than 0
예제 #5
0
    def test_equal_side(self):
        """> Test distance sensor when target on the sides with equal distance."""
        # Folder must be root to load in make_net properly
        if os.getcwd().split('\\')[-1] == 'tests': os.chdir('..')

        # Create empty game
        game = get_game()

        # Update player and target position
        game.target = Vec2dCy(1, 1)
        game.set_player_init_pos(Vec2dCy(0.999, 0))
        game.set_player_init_angle(0)

        # Update the sensors used to only include the delta-distance sensor
        game.player.sensors = dict()
        game.player.add_delta_distance_sensor()

        # Initially the sensor should read zero
        sensor_values = game.player.get_sensor_readings()
        self.assertAlmostEqual(sensor_values[0], 0.0, delta=EPSILON_DISTANCE)

        # Advance the player's position to a symmetric position with equal distance
        game.player.pos = Vec2dCy(1.001, 0)
        sensor_values = game.player.get_sensor_readings()
        self.assertAlmostEqual(sensor_values[0], 0.0, delta=EPSILON_DISTANCE)
예제 #6
0
    def test_front(self):
        """> Test the distance sensor when target straight in the front."""
        # Folder must be root to load in make_net properly
        if os.getcwd().split('\\')[-1] == 'tests': os.chdir('..')

        # Create empty game
        game = get_game()

        # Update player and target position
        game.target = Vec2dCy(1, 0)
        game.set_player_init_pos(Vec2dCy(0, 0))
        game.set_player_init_angle(0)

        # Update the sensors used to only include the delta-distance sensor
        game.player.sensors = dict()
        game.player.add_delta_distance_sensor()

        # Initially the sensor should read zero
        sensor_values = game.player.get_sensor_readings()
        self.assertAlmostEqual(sensor_values[0], 0.0, delta=EPSILON_DISTANCE)

        # Advance the player's position by 0.1 meters and test sensor-reading
        game.player.pos = Vec2dCy(0.1, 0)
        sensor_values = game.player.get_sensor_readings()
        self.assertAlmostEqual(sensor_values[0], 0.1, delta=EPSILON_DISTANCE)

        # Advance the player's position by 0.001 meters backwards and test sensor-reading
        game.player.pos = Vec2dCy(0.0999999, 0)
        sensor_values = game.player.get_sensor_readings()
        self.assertAlmostEqual(sensor_values[0],
                               -0.0000001,
                               delta=EPSILON_DISTANCE)
예제 #7
0
    def test_other_angles(self):
        """> Check if drone cannot force itself through a wall."""
        # Folder must be root to load in make_net properly
        if os.getcwd().split('\\')[-1] == 'tests': os.chdir('..')

        # Create the lines
        zero = Vec2dCy(0, 0)
        a = Vec2dCy(1, 1)
        line1 = Line2dCy(zero, a)

        # Tests
        self.assertTrue(pi / 4 - EPSILON <= line1.get_orientation() %
                        (2 * pi) <= pi / 4 + EPSILON)
예제 #8
0
    def test_simple_length(self):
        """> Test for simple line-segments."""
        # Folder must be root to load in make_net properly
        if os.getcwd().split('\\')[-1] == 'tests': os.chdir('..')

        # Create simple lines
        a = Vec2dCy(1, 1)
        b = Vec2dCy(1, 2)
        c = Vec2dCy(2, 2)
        line1 = Line2dCy(a, b)
        line2 = Line2dCy(b, c)
        line3 = Line2dCy(a, c)

        # Test the length
        self.assertTrue(1 - EPSILON <= line1.get_length() <= 1 + EPSILON)
        self.assertTrue(1 - EPSILON <= line2.get_length() <= 1 + EPSILON)
        self.assertTrue(
            sqrt(2) - EPSILON <= line3.get_length() <= sqrt(2) + EPSILON)
예제 #9
0
    def test_left_angle(self):
        """> Test distance sensor when target under an angle (towards the left)."""
        # Folder must be root to load in make_net properly
        if os.getcwd().split('\\')[-1] == 'tests': os.chdir('../..')

        # Create empty game
        game = get_game()

        # Update player and target position
        game.target = Vec2dCy(1, 1)
        game.set_player_init_pos(Vec2dCy(0, 0))
        game.set_player_init_angle(0)

        # Ask for the distance
        game.get_observation()
        self.assertAlmostEqual(game.get_distance_to_target(),
                               sqrt(2),
                               delta=EPSILON_DISTANCE)
예제 #10
0
    def test_front(self):
        """> Test angular sensors when target straight in the front."""
        # Folder must be root to load in make_net properly
        if os.getcwd().split('\\')[-1] == 'tests': os.chdir('../..')

        # Create empty game
        game = get_game()

        # Update player and target position
        game.target = Vec2dCy(1, 0)
        game.set_player_init_pos(Vec2dCy(0, 0))
        game.set_player_init_angle(0)

        # Update the player's sensor-set
        game.player.sensors = dict()
        game.player.add_angular_sensors(clockwise=True)
        game.player.add_angular_sensors(clockwise=False)

        # The third and second last sensors
        sensor_values = game.player.get_sensor_readings()
        self.assertEqual(len(sensor_values), 2)
        for s in sensor_values:
            self.assertAlmostEqual(s, 0.0, delta=EPSILON_ANGLE)
예제 #11
0
    def test_left_angle(self):
        """> Test the angular sensors when target on the left."""
        # Folder must be root to load in make_net properly
        if os.getcwd().split('\\')[-1] == 'tests': os.chdir('../..')

        # Create empty game
        game = get_game()

        # Update player and target position
        game.target = Vec2dCy(1, 1)
        game.set_player_init_pos(Vec2dCy(0, 0))
        game.set_player_init_angle(0)  # Looking to the right

        # Update the player's sensor-set
        game.player.sensors = dict()
        game.player.add_angular_sensors(clockwise=True)
        game.player.add_angular_sensors(clockwise=False)

        sensor_values = game.player.get_sensor_readings()
        self.assertEqual(len(sensor_values), 2)
        self.assertAlmostEqual(sensor_values[0], (-pi / 4) / pi,
                               delta=EPSILON_ANGLE)  # Clockwise
        self.assertAlmostEqual(sensor_values[1], (pi / 4) / pi,
                               delta=EPSILON_ANGLE)  # Anti-clockwise
예제 #12
0
def get_game():
    cfg = Config()
    cfg.game.fps = 60
    cfg.game.x_axis = 1
    cfg.game.y_axis = 1
    cfg.update()
    spawn = SpawnSimple(game_config=cfg)
    spawn.add_location((10, 10))  # Dummy location
    game = GameCy(
        config=cfg,
        game_id=0,
        noise=False,
        overwrite=True,
        save_path="tests/games_db/",
        silent=True,
        spawn_func=spawn,
        wall_bound=True,
    )
    game.set_player_init_pos(Vec2dCy(0.5, 0.5))
    game.sample_target()
    game.reset()
    return game
예제 #13
0
    def test_remain_in_box(self):
        """> Set drone in small box in the middle of the game to check if it stays in this box."""
        # Folder must be root to load in make_net properly
        if os.getcwd().split('\\')[-1] == 'tests': os.chdir('../..')

        # Create empty game
        game = get_game()

        # Do 10 different initialized tests
        for _ in range(10):
            # Set player init positions
            game.set_player_init_pos(Vec2dCy(0.5, 0.5))
            game.player.angle = random() * np.pi

            # Run for one twenty seconds
            for _ in range(20 * game.game_config.fps):
                l = random() * 1.5 - 0.5  # [-0.5..1]
                r = random() * 1.5 - 0.5  # [-0.5..1]
                game.step(l=l, r=r)

            # Check if bot still in box
            self.assertTrue(0 <= game.player.pos.x <= 1)
            self.assertTrue(0 <= game.player.pos.y <= 1)