Example #1
0
def test_car_does_not_bounce_off_wall(col_action):
    col = col_action[0]
    action = col_action[1]
    car = Car(col, 1)
    speed_limit = 5
    next_car = car.next(action, speed_limit)
    assert next_car.col < 0 or next_car.col > 3
Example #2
0
def test_prob_of_moving_car_appearing():
    car_col = 0
    s = dg_road.LaneAndDitchRoad(
        headlight_range=3,
        car=Car(col=car_col, speed=0),
        obstacles=[Pedestrian(-1, -1, speed=1, prob_of_appearing=0.05)],
        allowed_obstacle_appearance_columns=[{1}])
    successors = [(sp.to_key(), prob) for sp, prob in s.successors(NO_OP)]
    assert len(successors) == 2
    assert successors[0] == ((car_col, 0, frozenset()), 0.95)
    assert successors[1] == ((car_col, 0, frozenset([('p', 0, 1, 1, 0)])),
                             0.05)

    s = dg_road.LaneAndDitchRoad(
        headlight_range=3,
        car=Car(col=car_col, speed=1),
        obstacles=[Pedestrian(-1, -1, speed=1, prob_of_appearing=0.05)],
        allowed_obstacle_appearance_columns=[{1}])
    successors = [(sp.to_key(), prob) for sp, prob in s.successors(NO_OP)]
    assert len(successors) == 3
    assert successors[0] == ((car_col, 1, frozenset()),
                             1 - (0.05 * (1 - 0.05) + 0.05))
    assert successors[1] == ((car_col, 1, frozenset([('p', 0, 1, 1, 0)])),
                             0.05 * (1 - 0.05))
    assert successors[2] == ((car_col, 1, frozenset([('p', 1, 1, 1, 0)])),
                             0.05)
Example #3
0
def test_car_offroad():
    headlight_range = 4
    road1 = Road(headlight_range, Car(0, 1), [])
    action_taken1 = hand_coded_data_gathering_policy(road1)
    assert action_taken1 == RIGHT

    road2 = Road(headlight_range, Car(3, 1), [])
    action_taken2 = hand_coded_data_gathering_policy(road2)
    assert action_taken2 == LEFT
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
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
def test_car_has_crashed(col):
    patient = Road(4, Car(col, 1), [])
    assert patient.has_crashed()
    assert patient.has_crashed(Car(col, 1))
    assert not patient.has_crashed(Car(0, 1))
    assert patient.to_key() == (-1, 0, frozenset())

    for action in ACTIONS:
        successors, probs = zip(*patient.successors(action))
        assert [s.to_key() for s in successors] == [(-1, 0, frozenset())]
        assert sum(probs) == pytest.approx(1.0)
def test_car_can_only_overdrive_headlights_by_one_unit():
    headlight_range = 2
    with pytest.raises(ValueError):
        patient = Road(headlight_range, Car(0, speed=headlight_range + 2))

    patient = Road(headlight_range, Car(0, headlight_range + 1))
    assert patient.speed_limit() == headlight_range + 1

    patient = next(patient.successors(UP))[0]
    assert patient.speed_limit() == headlight_range + 1
    patient._car.speed == patient.speed_limit()

    patient = next(patient.successors(DOWN))[0]
    patient._car.speed == headlight_range
Example #8
0
def test_fast_obstacles(p):
    car_col = 0
    s = dg_road.LaneAndDitchRoad(
        headlight_range=3,
        car=Car(col=car_col, speed=1),
        obstacles=[Pedestrian(-1, -1, speed=4, prob_of_appearing=p)],
        allowed_obstacle_appearance_columns=[{1}])
    successors = sorted([(sp.to_key(), prob)
                         for sp, prob in s.successors(NO_OP)],
                        key=lambda x: x[1])

    if p < 1:
        assert len(successors) == 5
        assert successors[0] == ((car_col, 1, frozenset([('p', 0, 1, 4, 0)])),
                                 (1 - p)**4 * p)
        assert successors[1] == ((car_col, 1, frozenset([('p', 1, 1, 4, 0)])),
                                 (1 - p)**3 * p)
        assert successors[2] == ((car_col, 1, frozenset([('p', 2, 1, 4, 0)])),
                                 (1 - p)**2 * p)
        assert successors[3] == ((car_col, 1, frozenset([('p', 3, 1, 4, 0)])),
                                 (1 - p)**1 * p)
    else:
        assert len(successors) == 1

    assert successors[-1] == ((car_col, 1, frozenset()),
                              pytest.approx((1 - p)**5 + p))
    assert sum(prob for _, prob in successors) == pytest.approx(1)
Example #9
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)
Example #10
0
def test_speedometer_layer(speed):
    headlight_range = 4
    patient = dg_road.LaneAndDitchRoad(headlight_range, Car(1, speed=speed),
                                       [])
    x = np.full([headlight_range + 1, 5], False)
    x[headlight_range + 1 - speed:, -1] = True
    np.testing.assert_array_equal(patient.speedometer_layer(), x)
Example #11
0
def test_number_of_successors_invisible_obstacle_and_variable_speeds(
        obst, action, speed):
    headlight_range = 2
    road_test = Road(headlight_range, Car(1, speed), [obst])
    probs = [prob for next_state, prob in road_test.successors(action)]
    assert (len(probs) == 4 *
            max(0, speed - int(action == LEFT or action == RIGHT)) + 1)
Example #12
0
def test_no_obstacles_revealed_is_the_only_valid_set_of_revealed_obstacles_when_all_obstacles_already_on_road(
        obst):
    headlight_range = 2
    lane_and_ditch = dg_road.LaneAndDitchRoad(headlight_range, Car(1, 1),
                                              [obst])
    patient = list(lane_and_ditch.every_combination_of_revealed_obstacles(1))
    assert patient == [{}]
Example #13
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)
Example #14
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
Example #15
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|^'
Example #16
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|^'
Example #17
0
def test_policy_speeds_up_but_does_not_overdrive_headlights():
    headlight_range = 4
    road = Road(headlight_range, Car(2, headlight_range + 1), [])
    action_taken = hand_coded_data_gathering_policy(road)
    assert action_taken == DOWN

    road = Road(headlight_range, Car(2, headlight_range), [])
    action_taken = hand_coded_data_gathering_policy(road)
    assert action_taken == NO_OP

    road = Road(headlight_range, Car(2, headlight_range - 1), [])
    action_taken = hand_coded_data_gathering_policy(road)
    assert action_taken == NO_OP

    road = Road(headlight_range, Car(2, headlight_range - 2), [])
    action_taken = hand_coded_data_gathering_policy(road)
    assert action_taken == UP
Example #18
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)
Example #19
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
Example #20
0
def test_crashing_into_right_wall():
    patient = Road(1, Car(3, 1), [])
    successors = list(patient.successors(RIGHT))
    assert len(successors) == 1
    s, p = successors[0]
    assert p == 1.0
    assert s.to_key() != patient.to_key()
    assert s.to_key() == (-1, 0, frozenset())
    assert s.to_s() == ' ' * 7 + '\n' + ' ' * 7
Example #21
0
def test_moving_car_appears_when_stopped():
    s = Road(headlight_range=3,
             car=Car(2, 0),
             obstacles=[Pedestrian(-1, -1, speed=1, prob_of_appearing=0.5)],
             allowed_obstacle_appearance_columns=[{1}])
    successors = [(sp.to_key(), prob) for sp, prob in s.successors(NO_OP)]
    assert len(successors) == 2
    assert successors[0] == ((2, 0, frozenset()), 0.5)
    assert successors[1] == ((2, 0, frozenset([('p', 0, 1, 1, 0)])), 0.5)
Example #22
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)
Example #23
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)
Example #24
0
def test_single_terminal_state():
    s = Road(headlight_range=2,
             car=Car(2, 0),
             obstacles=[Pedestrian(-1, -1, speed=1, prob_of_appearing=0.13)],
             allowed_obstacle_appearance_columns=[{1}])
    info, _ = s.safety_information()
    '''s X a X s X measurement'''
    info = np.array(info)
    terminal_states, sp = np.where(info[:, NO_OP, :, 0] == 1)
    assert np.all(terminal_states == sp)
    assert len(terminal_states) == 1
Example #25
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))
Example #26
0
def test_number_of_successors_invisible_obstacle_and_variable_speeds(
        obst, action, speed):
    headlight_range = 2
    lane_and_ditch = dg_road.LaneAndDitchRoad(headlight_range, Car(1, speed),
                                              [obst])
    probs = [prob for next_state, prob in lane_and_ditch.successors(action)]
    print(action)
    print(probs)
    if action == RIGHT:
        assert probs == [1.0]
    else:
        assert (len(probs) == 2 * max(0, speed - int(action == LEFT)) + 1)
Example #27
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
Example #28
0
def test_crashing_into_right_wall():
    patient = dg_road.LaneAndDitchRoad(headlight_range=1,
                                       car=Car(col=1, speed=1),
                                       obstacles=[])
    successors = list(patient.successors(RIGHT))
    assert len(successors) == 1
    s, p = successors[0]
    assert p == 1.0
    assert s.to_key() != patient.to_key()
    assert s.to_key() == (-1, 0, frozenset())
    assert s._stage_width == 5
    assert s.to_s() == ' ' * 5 + '\n' + ' ' * 5
Example #29
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
Example #30
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