Пример #1
0
def test_board():
    bumps = [Bump(-1, -1), Bump(0, 0), Bump(1, 3)]
    pedestrians = [Pedestrian(-1, -1), Pedestrian(0, 1), Pedestrian(1, 2)]
    headlight_range = 4
    speed = 1
    road = Road(headlight_range, Car(1, speed=speed), bumps + pedestrians)
    patient = road.board()

    assert patient.dtype == 'uint8'

    x_board = np.full([headlight_range + 1, road._stage_width],
                      _byte(' '),
                      dtype='uint8')

    x_board[:, 0] = _byte('|')
    x_board[:, -2] = _byte('|')

    x_board[:, 1] = _byte('d')
    x_board[:, -3] = _byte('d')

    x_board[-speed:, -1] = _byte('^')

    x_board[0, 1] = bumps[0].to_byte()
    x_board[1, 4] = bumps[0].to_byte()

    x_board[0, 2] = pedestrians[0].to_byte()
    x_board[1, 3] = pedestrians[0].to_byte()

    x_board[headlight_range, 1 + 1] = _byte('C')

    np.testing.assert_array_equal(patient, x_board)
Пример #2
0
def test_probabilities_error():
    state = dg_road.LaneAndDitchRoad(headlight_range=3,
                                     car=Car(1, 1),
                                     obstacles=[Bump(-1, -1),
                                                Bump(-1, -1)])
    probs = [p for (s, p) in state.successors(NO_OP)]
    assert sum(probs) == pytest.approx(1.0)
Пример #3
0
def test_no_collision_if_obstacle_is_under_car():
    hr = 3
    car = Car(2, 4)
    s = Road(headlight_range=hr, car=car, obstacles=[Bump(hr, 2)])
    s_p = Road(headlight_range=hr, car=car, obstacles=[Bump(hr + 1, 2)])
    num_collisions = s.count_obstacle_collisions(
        s_p, lambda obs: int(isinstance(obs, Bump))).pop()
    assert num_collisions == 0
Пример #4
0
def test_to_s():
    bumps = [Bump(-1, -1), Bump(0, 0), Bump(1, 1)]
    pedestrians = [Pedestrian(-1, -1), Pedestrian(0, 1), Pedestrian(1, 0)]
    headlight_range = 4
    speed = 1
    patient = dg_road.LaneAndDitchRoad(headlight_range, Car(1, speed=speed),
                                       bumps + pedestrians).to_s()
    assert patient == '|bp| \n|pb| \n| d| \n| d| \n| C|^'
Пример #5
0
def test_to_s():
    bumps = [Bump(-1, -1), Bump(0, 0), Bump(1, 3)]
    pedestrians = [Pedestrian(-1, -1), Pedestrian(0, 1), Pedestrian(1, 2)]
    headlight_range = 4
    speed = 1
    patient = Road(headlight_range, Car(1, speed=speed),
                   bumps + pedestrians).to_s()
    assert patient == '|bp d| \n|d pb| \n|d  d| \n|d  d| \n|dC d|^'
Пример #6
0
def test_simple_collision():
    hr = 3
    car = Car(2, 4)
    s = Road(headlight_range=hr, car=car, obstacles=[Bump(-1, -1)])
    s_p = Road(headlight_range=hr, car=car, obstacles=[Bump(hr, 2)])

    num_collisions = s.count_obstacle_collisions(
        s_p, lambda obs: int(isinstance(obs, Bump))).pop()
    assert num_collisions == 1
Пример #7
0
def test_collision_if_car_changes_lanes_into_obstacle():
    hr = 3
    s = Road(headlight_range=hr, car=Car(2, 1), obstacles=[Bump(hr, 1)])
    s_p = Road(headlight_range=hr, car=Car(1, 1), obstacles=[Bump(hr, 1)])
    num_collisions = s.count_obstacle_collisions(
        s_p, lambda obs: int(isinstance(obs, Bump))).pop()
    assert num_collisions == 1

    num_collisions = s_p.count_obstacle_collisions(
        s, lambda obs: int(isinstance(obs, Bump))).pop()
    assert num_collisions == 0
Пример #8
0
def test_collision_if_car_drives_at_full_speed():
    hr = 3
    s = Road(headlight_range=hr, car=Car(2, hr), obstacles=[Bump(hr + 1, 2)])
    s_p = Road(headlight_range=hr, car=Car(2, hr), obstacles=[Bump(hr, 2)])
    num_collisions = s.count_obstacle_collisions(
        s_p, lambda obs: int(isinstance(obs, Bump))).pop()
    assert num_collisions == 1

    num_collisions = s_p.count_obstacle_collisions(
        s, lambda obs: int(isinstance(obs, Bump))).pop()
    assert num_collisions == 0
Пример #9
0
def test_no_collision_if_obstacle_is_removed_from_the_road():
    hr = 3
    car = Car(2, 4)
    s_p = dg_road.LaneAndDitchRoad(headlight_range=hr,
                                   car=car,
                                   obstacles=[Bump(-1, -1)])
    s = dg_road.LaneAndDitchRoad(headlight_range=hr,
                                 car=car,
                                 obstacles=[Bump(hr, 2)])

    num_collisions = s.count_obstacle_collisions(
        s_p, lambda obs: int(isinstance(obs, Bump))).pop()
    assert num_collisions == 0
Пример #10
0
def test_successor_function_with_identical_states(p):
    expected_probs = [
        (1 - p)**2
    ] + [2 * p * (1 - p) / 4] * 4 + [2 * p / 3.0 * p / 4.0] * 6
    expected_probs = [
        pytest.approx(prob) for prob in expected_probs if prob > 0
    ]
    state = Road(headlight_range=3,
                 car=Car(1, 1),
                 obstacles=[
                     Bump(-1, -1, prob_of_appearing=p),
                     Bump(-1, -1, prob_of_appearing=p)
                 ])
    probs = [p for (s, p) in state.successors(NO_OP)]
    assert sum(probs) == pytest.approx(1.0)
    assert probs == expected_probs
Пример #11
0
def test_layers():
    bumps = [Bump(-1, -1), Bump(0, 0), Bump(1, 3)]
    pedestrians = [Pedestrian(-1, -1), Pedestrian(0, 1), Pedestrian(1, 2)]
    headlight_range = 4
    speed = 1
    patient = Road(headlight_range, Car(1, speed=speed),
                   bumps + pedestrians).layers()

    assert len(patient) == 7

    x_bump_layer = np.full([headlight_range + 1, 7], False)
    x_bump_layer[0, 1] = True
    x_bump_layer[1, 4] = True
    np.testing.assert_array_equal(patient[str(bumps[0])], x_bump_layer)

    x_pedestrian_layer = np.full([headlight_range + 1, 7], False)
    x_pedestrian_layer[0, 2] = True
    x_pedestrian_layer[1, 3] = True
    np.testing.assert_array_equal(patient[str(pedestrians[0])],
                                  x_pedestrian_layer)

    x_car_layer = np.full([headlight_range + 1, 7], False)
    x_car_layer[headlight_range, 1 + 1] = True
    np.testing.assert_array_equal(patient['C'], x_car_layer)

    x_wall_layer = np.full([headlight_range + 1, 7], False)
    x_wall_layer[:, 0] = True
    x_wall_layer[:, -2] = True
    np.testing.assert_array_equal(patient['|'], x_wall_layer)

    x_ditch_layer = np.full([headlight_range + 1, 7], False)
    x_ditch_layer[:, 1] = True
    x_ditch_layer[:, -3] = True
    np.testing.assert_array_equal(patient['d'], x_ditch_layer)

    x_speedometer_layer = np.full([headlight_range + 1, 7], False)
    x_speedometer_layer[-speed:, -1] = True
    np.testing.assert_array_equal(patient['^'], x_speedometer_layer)

    x_empty_layer = np.logical_not(
        np.logical_or(
            x_speedometer_layer,
            np.logical_or(
                x_ditch_layer,
                np.logical_or(np.logical_or(x_bump_layer, x_pedestrian_layer),
                              np.logical_or(x_car_layer, x_wall_layer)))))
    np.testing.assert_array_equal(patient[' '], x_empty_layer)
Пример #12
0
def test_obstacles_outside_headlight_range_are_hidden():
    bumps = [Bump(-1, 0)]
    headlight_range = 4
    patient = Road(headlight_range, Car(1, 1), bumps).obstacle_layers()

    assert len(patient) == 1

    x_bump_layer = np.full([headlight_range + 1, 7], False)
    np.testing.assert_array_equal(patient[str(bumps[0])], x_bump_layer)
Пример #13
0
 def new_road():
     if one_way:
         return dg_road.LaneAndDitchRoad(
             headlight_range,
             Car(0, 0),
             obstacles=[
                 Bump(-1, -1, prob_of_appearing=1.0),
             ],
             allowed_obstacle_appearance_columns=[{0, 1}],
             allow_crashing=allow_crashing)
     else:
         return dg_road.Road(headlight_range,
                             Car(2, 0),
                             obstacles=[
                                 Bump(-1, -1, prob_of_appearing=1.0),
                             ],
                             allowed_obstacle_appearance_columns=[{0, 1}],
                             allow_crashing=allow_crashing)
Пример #14
0
def new_road(headlight_range=3):
    return Road(
        headlight_range,
        Car(2, 0),
        obstacles=[
            Bump(-1, -1, prob_of_appearing=0.16),
            Pedestrian(-1, -1, speed=1, prob_of_appearing=0.13)
        ],
        allowed_obstacle_appearance_columns=[{2}, {1}],
        allow_crashing=True)
Пример #15
0
def test_obstacle_layers():
    bumps = [Bump(-1, -1), Bump(0, 0), Bump(1, 3)]
    pedestrians = [Pedestrian(-1, -1), Pedestrian(0, 1), Pedestrian(1, 2)]
    headlight_range = 4
    patient = Road(headlight_range, Car(1, 1),
                   bumps + pedestrians).obstacle_layers()

    assert len(patient) == 2

    x_bump_layer = np.full([headlight_range + 1, 7], False)
    x_bump_layer[0, 1] = True
    x_bump_layer[1, 4] = True
    np.testing.assert_array_equal(patient[str(bumps[0])], x_bump_layer)

    x_pedestrian_layer = np.full([headlight_range + 1, 7], False)
    x_pedestrian_layer[0, 2] = True
    x_pedestrian_layer[1, 3] = True
    np.testing.assert_array_equal(patient[str(pedestrians[0])],
                                  x_pedestrian_layer)
Пример #16
0
def test_obstacle_appearance_prob(p):
    car_col = 0
    s = dg_road.LaneAndDitchRoad(headlight_range=3,
                                 car=Car(col=car_col, speed=1),
                                 obstacles=[Bump(-1, -1, prob_of_appearing=p)],
                                 allowed_obstacle_appearance_columns=[{1}])
    successors = [(sp.to_key(), prob) for sp, prob in s.successors(NO_OP)]

    if p < 1:
        assert len(successors) == 2
        assert successors[0] == ((car_col, 1, frozenset()), 1.0 - p)
    else:
        assert len(successors) == 1
    assert successors[int(p < 1) * 1] == ((car_col, 1,
                                           frozenset([('b', 0, 1, 0, 0)])), p)

    s = dg_road.LaneAndDitchRoad(headlight_range=3,
                                 car=Car(col=car_col, speed=1),
                                 obstacles=[
                                     Bump(-1, -1, prob_of_appearing=p),
                                     Pedestrian(-1, -1, prob_of_appearing=p)
                                 ],
                                 allowed_obstacle_appearance_columns=[{0},
                                                                      {1}])
    successors = [(sp.to_key(), prob) for sp, prob in s.successors(NO_OP)]

    if p < 1:
        assert len(successors) == 4

        assert successors[1] == ((car_col, 1, frozenset([('b', 0, 0, 0, 0)])),
                                 pytest.approx(p * (1 - p)))
        assert successors[2] == ((car_col, 1, frozenset([('p', 0, 1, 0, 0)])),
                                 pytest.approx(p * (1 - p)))
        assert successors[0] == ((car_col, 1, frozenset()),
                                 pytest.approx(1 - 2 * p * (1 - p) - p * p))
    else:
        assert len(successors) == 1
    assert successors[int(p < 1) * 3] == ((car_col, 1,
                                           frozenset([('b', 0, 0, 0, 0),
                                                      ('p', 0, 1, 0, 0)])),
                                          pytest.approx(p * p))
Пример #17
0
def test_safety_information():
    patient = Road(headlight_range=1, car=Car(2, 0), obstacles=[Bump(-1, -1)])
    counts, state_indices = patient.safety_information()

    counts = np.array(counts)

    assert len(counts.shape) == 4

    assert counts.shape[0] == len(state_indices)
    assert counts.shape[1] == len(ACTIONS)
    assert counts.shape[2] == len(state_indices)
    assert counts.shape[3] == 7
Пример #18
0
def test_to_key():
    bumps = [Bump(0, 2)]
    headlight_range = 1
    car = Car(2, 1)
    patient = Road(headlight_range, car, bumps).to_key()
    assert patient == (2, 1, frozenset([('b', 0, 2, 0, 0)]))

    obstacles = [
        Bump(-1, 0),
        Bump(-1, -1),
        Bump(0, 2),
        Pedestrian(1, 1),
        Pedestrian(1, 2),
        Pedestrian(2, 3)
    ]
    headlight_range = 1
    car = Car(2, 1)
    patient = Road(headlight_range, car, obstacles).to_key()
    assert patient == (
        2,
        1,
        frozenset([('b', 0, 2, 0, 0), ('p', 1, 1, 0, 0), ('p', 1, 2, 0, 0)]))  # yapf:disable
Пример #19
0
def test_to_key():
    bumps = [Bump(0, 1)]
    headlight_range = 1
    car = Car(col=0, speed=1)
    patient = dg_road.LaneAndDitchRoad(headlight_range, car, bumps).to_key()
    assert patient == (0, 1, frozenset([('b', 0, 1, 0, 0)]))

    obstacles = [
        Bump(-1, 0),
        Bump(-1, -1),
        Bump(0, 1),
        Pedestrian(1, 1),
        Pedestrian(1, 0),
        Pedestrian(2, 0)
    ]
    headlight_range = 1
    patient = dg_road.LaneAndDitchRoad(headlight_range, car,
                                       obstacles).to_key()
    assert patient == (
        0,
        1,
        frozenset([('b', 0, 1, 0, 0), ('p', 1, 1, 0, 0), ('p', 1, 0, 0, 0)]))  # yapf:disable
Пример #20
0
def test_tabulate_single_reward():
    patient = Road(headlight_range=1, car=Car(2, 0), obstacles=[Bump(-1, -1)])
    transitions, rewards, state_indices = patient.tabulate(dg_rewards.reward)

    transitions = np.array(transitions)
    rewards = np.array(rewards)

    assert len(transitions.shape) == 3
    assert len(rewards.shape) == 2

    assert transitions.shape[0] == len(state_indices)
    assert transitions.shape[0] == rewards.shape[0]
    assert transitions.shape[1] == rewards.shape[1]

    for t in transitions:
        for ta in t:
            assert sum(ta) == pytest.approx(1.0)
Пример #21
0
def simple_road_factory(headlight_range=5,
                        num_bumps=3,
                        num_pedestrians=3,
                        speed=1,
                        discount=0.99,
                        bump_appearance_prob=0.2,
                        pedestrian_appearance_prob=0.2,
                        car_col=2):
    car = Car(car_col, speed)
    initial_bumps = [
        Bump(-1, -1, prob_of_appearing=bump_appearance_prob)
        for _ in range(num_bumps)
    ]
    initial_pedestrians = [
        Pedestrian(-1, -1, prob_of_appearing=pedestrian_appearance_prob)
        for _ in range(num_pedestrians)
    ]
    return Road(headlight_range, car, initial_bumps + initial_pedestrians)
Пример #22
0
def test_obstacles_cannot_appear_in_the_same_position(p):
    s = Road(headlight_range=3,
             car=Car(2, 1),
             obstacles=[
                 Bump(-1, -1, prob_of_appearing=p),
                 Pedestrian(-1, -1, prob_of_appearing=p)
             ],
             allowed_obstacle_appearance_columns=[{1}, {1}])
    successors = [(sp.to_key(), prob) for sp, prob in s.successors(NO_OP)]

    if p < 1:
        assert len(successors) == 3
        assert successors[2] == ((2, 1, frozenset([('p', 0, 1, 0, 0)])),
                                 p * (1 - p))
        assert successors[0] == ((2, 1, frozenset()),
                                 pytest.approx(1 - p - p * (1 - p)))
    else:
        assert len(successors) == 1
    assert successors[int(p < 1) * 1] == ((2, 1, frozenset([('b', 0, 1, 0, 0)
                                                            ])), p)
    assert sum(prob for _, prob in successors) == pytest.approx(1)
Пример #23
0
def test_car_speeds_up_if_obstacle_not_visible():
    headlight_range = 4
    obstacles = [Bump(headlight_range + 1, 2)]
    road = Road(headlight_range, Car(2, 2), obstacles)
    action_taken = hand_coded_data_gathering_policy(road)
    assert action_taken == UP
Пример #24
0
def test_single_bump_in_front_of_car():
    obstacles = [Bump(3, 2)]
    headlight_range = 4
    road = Road(headlight_range, Car(2, 1), obstacles)
    action_taken = hand_coded_data_gathering_policy(road)
    assert action_taken == LEFT
Пример #25
0
def test_obstacles_appear_over_dirt_and_pavement(col):
    bumps = [Bump(0, col)]
    headlight_range = 4
    patient = Road(headlight_range, Car(2, 1), bumps).board()
    assert patient[0, col + 1] == bumps[0].to_byte()
Пример #26
0
def test_successor_probabilities(speed):
    state = Road(headlight_range=6,
                 car=Car(1, speed),
                 obstacles=[Bump(-1, -1)])
    probs = [p for (s, p) in state.successors(UP)]
    assert sum(probs) == pytest.approx(1.0)
Пример #27
0
def test_two_adjacent_bumps():
    headlight_range = 4
    obstacles = [Bump(0, 1), Bump(0, 2)]
    road = Road(headlight_range, Car(2, 2), obstacles)
    action_taken = hand_coded_data_gathering_policy(road)
    assert action_taken == DOWN
Пример #28
0
from driving_gridworld.obstacles import Pedestrian
from driving_gridworld.car import Car
from driving_gridworld.actions import ACTIONS, RIGHT, LEFT, UP, DOWN, NO_OP
import driving_gridworld.rewards as dg_rewards
import pytest


@pytest.mark.parametrize("action", ACTIONS)
def test_transition_probs_without_obstacles_are_always_1(action):
    headlight_range = 4
    patient = Road(headlight_range, Car(0, 1))
    for next_state, prob in patient.successors(action):
        assert prob == 1.0


@pytest.mark.parametrize("obst", [Bump(0, 0), Pedestrian(0, 0)])
def test_no_obstacles_revealed_is_the_only_valid_set_of_revealed_obstacles_when_all_obstacles_already_on_road(
        obst):
    headlight_range = 2
    road_test = Road(headlight_range, Car(1, 1), [obst])
    patient = list(road_test.every_combination_of_revealed_obstacles(1))
    assert patient == [{}]


@pytest.mark.parametrize("obst", [Bump(0, 0), Pedestrian(0, 0)])
@pytest.mark.parametrize("action", ACTIONS)
def test_transition_probs_with_one_obstacle_are_1(obst, action):
    headlight_range = 2
    road_test = Road(headlight_range, Car(1, 1), [obst])
    probs = [prob for next_state, prob in road_test.successors(action)]
    assert probs == [1.0]
Пример #29
0
def test_car_appears_over_dirt_and_pavement(col):
    bumps = [Bump(row=1, col=1)]
    headlight_range = 4
    c = Car(col=col, speed=1)
    patient = dg_road.LaneAndDitchRoad(headlight_range, c, bumps).board()
    assert patient[headlight_range, col + 1] == c.to_byte()
Пример #30
0
def test_car_appears_over_dirt_and_pavement(col):
    bumps = [Bump(0, 2)]
    headlight_range = 4
    c = Car(col, 1)
    patient = Road(headlight_range, c, bumps).board()
    assert patient[headlight_range, col + 1] == c.to_byte()