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)
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)
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|^'
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|^'
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)
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)
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)
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)
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)
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
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
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
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)
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))
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)
def test_fast_obstacles(p): s = Road(headlight_range=3, car=Car(2, 1), obstacles=[Pedestrian(-1, -1, speed=4, 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) == 5 assert successors[3] == ((2, 1, frozenset([('p', 0, 1, 4, 0)])), (1 - p)**4 * p) assert successors[2] == ((2, 1, frozenset([('p', 1, 1, 4, 0)])), (1 - p)**3 * p) assert successors[1] == ((2, 1, frozenset([('p', 2, 1, 4, 0)])), (1 - p)**2 * p) assert successors[4] == ((2, 1, frozenset([('p', 3, 1, 4, 0)])), (1 - p)**1 * p) else: assert len(successors) == 1 assert successors[0] == ((2, 1, frozenset()), pytest.approx((1 - p)**5 + p)) assert sum(prob for _, prob in successors) == pytest.approx(1)
def test_pedestrian_in_front_of_car(): obstacles = [Pedestrian(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
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]
def test_moving_pedestrian(car_speed, ped_speed): patient = Pedestrian(1, 1, speed=ped_speed) next_patient = patient.next(car_speed) assert next_patient.row == (patient.row + car_speed + patient.speed)
def test_is_in_a_ditch(): road = dg_road.LaneAndDitchRoad(2, Car(0, 1)) assert not road.is_in_a_ditch() road = dg_road.LaneAndDitchRoad(2, Car(1, 1)) assert road.is_in_a_ditch() @pytest.mark.parametrize("action", ACTIONS) def test_transition_probs_without_obstacles_are_always_1(action): headlight_range = 4 patient = dg_road.LaneAndDitchRoad(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 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 == [{}] @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 lane_and_ditch = dg_road.LaneAndDitchRoad(headlight_range, Car(1, 1), [obst])