def test_safety_system(self): ts = env.TrafficState(width_lanes=3, height_cells=20, cars=0) my_car = env.Car(5, 1, 10) c1 = env.Car(15, 0, 14) s = ts._render_state(my_car, [c1]) np.testing.assert_array_equal(s, [ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 10, 10, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], ]) # should be refused ts._apply_action(my_car, env.Actions.goLeft, [c1]) self.assertEqual(my_car.cell, (1, 10)) c1 = env.Car(15, 0, 15) # should move ts._apply_action(my_car, env.Actions.goLeft, [c1]) self.assertEqual(my_car.cell, (0, 10)) s = ts._render_state(my_car, [c1]) np.testing.assert_array_equal(s, [ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 10, 10, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], ]) ts._update_safe_speed(my_car, [c1]) self.assertEqual(c1.safe_speed, 2) # check in front of the car ts = env.TrafficState(width_lanes=3, height_cells=20, cars=0) my_car = env.Car(5, 1, 10) c1 = env.Car(15, 0, 6) s = ts._render_state(my_car, [c1]) np.testing.assert_array_equal(s, [ [0, 0, 0, 0, 0, 0, 10, 10, 10, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +0, +0, +0, +0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], ]) # should be refused ts._apply_action(my_car, env.Actions.goLeft, [c1]) self.assertEqual(my_car.cell, (1, 10)) # should be refused c1 = env.Car(15, 0, 1) ts._apply_action(my_car, env.Actions.goLeft, [c1]) self.assertEqual(my_car.cell, (1, 10)) s = ts._render_state(my_car, [c1]) np.testing.assert_array_equal(s, [ [0, 10, 10, 10, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +0, +0, +0, +0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], ]) # should be accepted c1 = env.Car(15, 0, 0) ts._apply_action(my_car, env.Actions.goLeft, [c1]) self.assertEqual(my_car.cell, (0, 10))
def test_render_state(self): ts = env.TrafficState(width_lanes=3, height_cells=20, cars=0) my_car = env.Car(5, 1, 10) c1 = env.Car(15, 0, 10) s = ts._render_state(my_car, [c1]) np.testing.assert_array_almost_equal( s, np.array([ [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0.0075, 0.0075, 0.0075, 0.0075, 1, 1, 1, 1, 1, 1 ], [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0.0025, 0.0025, 0.0025, 0.0025, 1, 1, 1, 1, 1, 1 ], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], ])) c2 = env.Car(0, 1, 0) s = ts._render_state(my_car, [c1, c2]) np.testing.assert_array_almost_equal(s, [ [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0.0075, 0.0075, 0.0075, 0.0075, 1, 1, 1, 1, 1, 1 ], [ 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0.0025, 0.0025, 0.0025, 0.0025, 1, 1, 1, 1, 1, 1 ], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], ])
def test_check_collisions(self): ts = env.TrafficState() # history = [ts.snapshot()] for _ in range(1000): c = ts.is_collision() if c is not None: print("Collision! %s with %s" % c) # with open("collision.txt", 'wt', encoding='utf-8') as fd: # pprint.pprint(history, fd) assert False ts.tick()
def test_update_safe_speed(self): ts = env.TrafficState(width_lanes=3, height_cells=20, cars=0) # nothing should change my_car = env.Car(5, 1, 10) c1 = env.Car(15, 0, 10) ts._update_safe_speed(my_car, [c1]) self.assertEqual(my_car.safe_speed, 5) self.assertEqual(c1.safe_speed, 15) # our car should be slowed down to the speed of upfront car my_car = env.Car(20, 1, 10) c1 = env.Car(10, 1, 2) ts._update_safe_speed(my_car, [c1]) self.assertEqual(my_car.safe_speed, 10) self.assertEqual(c1.safe_speed, 10)
def test_move_cars(self): ts = env.TrafficState(width_lanes=3, height_cells=20, cars=0) my_car = env.Car(5, 1, 10) c1 = env.Car(15, 0, 10) # relative speed is 10, so c1 should move 1 cell forward ts._move_cars(my_car, [c1]) self.assertEqual(c1.cell_y, 9) self.assertEqual(c1.pos_y, 98) self.assertEqual(my_car.cell_y, 10) c1 = env.Car(0, 0, 10) # relative speed is -5, so, it should keep the current cell (due to rounding) ts._move_cars(my_car, [c1]) self.assertEqual(c1.cell_y, 10) self.assertEqual(c1.pos_y, 101) ts._move_cars(my_car, [c1]) self.assertEqual(c1.cell_y, 10) self.assertEqual(c1.pos_y, 102)
def test_render(self): random.seed(10) ts = env.TrafficState(state_render_view=(1, 10, 5)) self.assertEqual(ts.state.shape, (3, 15)) np.testing.assert_array_almost_equal(ts.state, [ [0.0325, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0.04, 0.04, 0.04, 0.04, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 0.0325, 0.0325, 0.0325, 0.0325, 1, 1], ]) ts.tick(env.Actions.goLeft) np.testing.assert_array_almost_equal(ts.state, [ [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0.0325, 0.0325, 0.0325, 0.0325, 1], [0.0325, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0.04, 0.04, 0.04, 0.04, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], ]) for step in range(16): ts.tick(env.Actions.noAction) ts.tick(env.Actions.goLeft) ts.tick(env.Actions.goLeft) np.testing.assert_array_almost_equal(ts.state, [ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0.04, 0.04, 0.04, 0.04, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], ])
def test_init(self): ts = env.TrafficState() self.assertEqual(len(ts.cars), ts.cars_count) self.assertEqual(ts.my_car.cell_x, 3) self.assertEqual(ts.my_car.cell_y, 46)
def test_apply_action(self): ts = env.TrafficState(width_lanes=3, height_cells=20, cars=0) my_car = env.Car(5, 1, 10) c1 = env.Car(15, 0, 10) s = ts._render_state(my_car, [c1]) np.testing.assert_array_almost_equal(s, [ [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0.0075, 0.0075, 0.0075, 0.0075, 1, 1, 1, 1, 1, 1 ], [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0.0025, 0.0025, 0.0025, 0.0025, 1, 1, 1, 1, 1, 1 ], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], ]) ts._apply_action(c1, env.Actions.accelerate, [my_car]) self.assertEqual(c1.speed, 16) ts._update_safe_speed(my_car, [c1]) s = ts._render_state(my_car, [c1]) np.testing.assert_array_almost_equal(s, [ [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0.008, 0.008, 0.008, 0.008, 1, 1, 1, 1, 1, 1 ], [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0.0025, 0.0025, 0.0025, 0.0025, 1, 1, 1, 1, 1, 1 ], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], ]) # should be ignored - central car is on the way ts._apply_action(c1, env.Actions.goRight, [my_car]) self.assertEqual(c1.cell_x, 0) s = ts._render_state(my_car, [c1]) np.testing.assert_array_almost_equal(s, [ [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0.008, 0.008, 0.008, 0.008, 1, 1, 1, 1, 1, 1 ], [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0.0025, 0.0025, 0.0025, 0.0025, 1, 1, 1, 1, 1, 1 ], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], ]) # move central car to the right ts._apply_action(my_car, env.Actions.goRight, [c1]) self.assertEqual(my_car.cell_x, 2) s = ts._render_state(my_car, [c1]) np.testing.assert_array_almost_equal(s, [ [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0.008, 0.008, 0.008, 0.008, 1, 1, 1, 1, 1, 1 ], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0.0025, 0.0025, 0.0025, 0.0025, 1, 1, 1, 1, 1, 1 ], ]) # should now change lanes ts._apply_action(c1, env.Actions.goRight, [my_car]) self.assertEqual(c1.cell_x, 1) s = ts._render_state(my_car, [c1]) np.testing.assert_array_almost_equal(s, [ [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0.008, 0.008, 0.008, 0.008, 1, 1, 1, 1, 1, 1 ], [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0.0025, 0.0025, 0.0025, 0.0025, 1, 1, 1, 1, 1, 1 ], ])