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 ], ])
def test_shift(self): c = env.Car(0, 0, 1) c.shift_forward(rel_speed=env.Car.SpeedUnitsPerPos) self.assertEqual(c.pos_y, 9) self.assertEqual(c.cell_y, 0)
def test_overlaps(self): c = env.Car(0, 0, 0) self.assertTrue(c.overlaps(c)) self.assertTrue(c.overlaps(env.Car(0, 0, 1))) self.assertTrue(c.overlaps(env.Car(0, 0, 2))) self.assertTrue(c.overlaps(env.Car(0, 0, 3))) self.assertTrue(c.overlaps(env.Car(0, 0, 4))) self.assertFalse(c.overlaps(env.Car(0, 0, 5))) self.assertTrue(c.overlaps(env.Car(0, 0, 5), safety_dist=1)) self.assertFalse(c.overlaps(env.Car(0, 1, 2))) c = env.Car(0, 3, 20) self.assertFalse(c.overlaps(env.Car(0, 3, 15))) self.assertTrue(c.overlaps(env.Car(0, 3, 16))) self.assertTrue(c.overlaps(env.Car(0, 3, 17))) self.assertTrue(c.overlaps(env.Car(0, 3, 18))) self.assertTrue(c.overlaps(env.Car(0, 3, 19))) self.assertTrue(c.overlaps(env.Car(0, 3, 24))) self.assertFalse(c.overlaps(env.Car(0, 3, 25)))
def test_is_inside(self): c = env.Car(0, 0, 2) self.assertTrue(c.is_inside(y_cells=6)) self.assertFalse(c.is_inside(y_cells=5)) self.assertFalse(env.Car(0, 0, -1).is_inside(y_cells=100))