Ejemplo n.º 1
0
    def test_trackPercentage(self):
        road = Mock()
        vehicles: typing.List[Vehicle] = []
        for x in range(100):
            vehicles.append(Mock(position=(x, 0)))
        road.getAllActiveVehicles = lambda: vehicles
        tracker = Tracker(simulator=Mock(road=road))

        def make_filter(xfilter: typing.Callable[[int], bool]) -> Filter:
            def f(vehicle: Vehicle) -> bool:
                x, _ = vehicle.position
                return xfilter(x)

            return f

        no_filter = make_filter(lambda _: True)
        x50_filter = make_filter(lambda x: x < 50)
        x25_filter = make_filter(lambda x: x < 25)

        # Check count working.
        result = tracker._trackPercentage(x50_filter, no_filter)
        expected = AverageResult(value=50, count=50)
        self.assertEqual(result, expected, '{} != {}'.format(str(result), str(expected)))
        # Check value working.
        result = tracker._trackPercentage(no_filter, x50_filter)
        expected = AverageResult(value=50, count=100)
        self.assertEqual(result, expected, '{} != {}'.format(str(result), str(expected)))
        # More potential count than values.
        result = tracker._trackPercentage(x50_filter, x25_filter)
        expected = AverageResult(value=25, count=50)
        self.assertEqual(result, expected, '{} != {}'.format(str(result), str(expected)))
        # More potential values than count.
        result = tracker._trackPercentage(x25_filter, x50_filter)
        expected = AverageResult(value=25, count=25)
        self.assertEqual(result, expected, '{} != {}'.format(str(result), str(expected)))
Ejemplo n.º 2
0
    def test_trackWaiting(self):
        road = Mock()
        vehicles: typing.List[Vehicle] = []
        for x in range(10):
            vehicle = Mock()
            vehicle.position = (x, 0)
            vehicle.last_position = (x, 0)
            vehicles.append(vehicle)
        road.getAllActiveVehicles = lambda: vehicles
        tracker = Tracker(simulator=Mock(road=road))
        # No vehicles matching the predicate.
        result = tracker._trackWaiting(lambda _: False)
        expected = AverageResult(value=0, count=0)
        self.assertEqual(result, expected, '{} != {}'.format(str(result), str(expected)))
        # All vehicles matching the predicate.
        result = tracker._trackWaiting(lambda _: True)
        expected = AverageResult(value=10, count=10)
        self.assertEqual(result, expected, '{} != {}'.format(str(result), str(expected)))

        # Some vehicles matching the predicate.
        def isEven(vehicle: Vehicle) -> bool:
            x, _ = vehicle.position
            return x % 2 == 0

        result = tracker._trackWaiting(isEven)
        expected = AverageResult(value=5, count=5)
        self.assertEqual(result, expected, '{} != {}'.format(str(result), str(expected)))
Ejemplo n.º 3
0
 def _initVelocity(self):
     self.velocity = \
         [[AverageResult(0, 0) for _ in range(self._road.length)]
          for _ in range(self._road.lanes_count)]
     self.velocity_autonomous = \
         [[AverageResult(0, 0) for _ in range(self._road.length)]
          for _ in range(self._road.lanes_count)]
     self.velocity_conventional = \
         [[AverageResult(0, 0) for _ in range(self._road.length)]
          for _ in range(self._road.lanes_count)]
Ejemplo n.º 4
0
 def test_add(self):
     a = AverageResult(1, 10)
     b = AverageResult(2, 20)
     c = a + b
     self.assertEqual(3, c.value)
     self.assertEqual(30, c.count)
     a = AverageResult(0, 0)
     b = AverageResult(1, 2)
     c = a + b
     self.assertEqual(1, c.value)
     self.assertEqual(2, c.count)
Ejemplo n.º 5
0
 def test_sub(self):
     a = AverageResult(1, 10)
     b = AverageResult(2, 20)
     c = a - b
     self.assertEqual(-1, c.value)
     self.assertEqual(-10, c.count)
     a = AverageResult(10, 20)
     b = AverageResult(1, 2)
     c = a - b
     self.assertEqual(9, c.value)
     self.assertEqual(18, c.count)
Ejemplo n.º 6
0
    def test_trackDecelerations(self):
        road = Mock()
        vehicles: typing.List[Vehicle] = []
        for velocity in range(10):
            vehicle = Mock(spec=Car)
            vehicle.velocity = velocity
            vehicle.path = [((0, 0), 12)]
            vehicles.append(vehicle)
        road.getAllActiveVehicles = lambda: vehicles
        tracker = Tracker(simulator=Mock(road=road))
        # No vehicles matching the predicate.
        result = tracker._trackDecelerations(lambda _: False)
        expected = AverageResult(value=0, count=0)
        self.assertEqual(result, expected, '{} != {}'.format(str(result), str(expected)))
        # All vehicles matching the predicate.
        result = tracker._trackDecelerations(lambda _: True)
        expected = AverageResult(value=10, count=10)
        self.assertEqual(result, expected, '{} != {}'.format(str(result), str(expected)))

        # Some vehicles matching the predicate.
        def isEven(vehicle: Vehicle) -> bool:
            return vehicle.velocity % 2 == 0

        result = tracker._trackDecelerations(isEven)
        expected = AverageResult(value=5, count=5)
        self.assertEqual(result, expected, '{} != {}'.format(str(result), str(expected)))

        # Test decelerations filter.
        road = Mock()
        vehicles: typing.List[Vehicle] = []
        # Vehicles which decelerated quickly.
        for _ in range(10):
            vehicle = Mock(spec=Car)
            vehicle.velocity = 5
            vehicle.path = [((0, 0), 10)]
            vehicles.append(vehicle)
        # Not cars, therefore should not be counted.
        for _ in range(10):
            vehicle = Mock(spec=Vehicle)
            vehicle.velocity = 5
            vehicle.path = [((0, 0), 10)]
            vehicles.append(vehicle)
        # Vehicles which did not decelerate quickly.
        for _ in range(10):
            vehicle = Mock(spec=Car)
            vehicle.velocity = 9
            vehicle.path = [((0, 0), 10)]
            vehicles.append(vehicle)
        road.getAllActiveVehicles = lambda: vehicles
        tracker = Tracker(simulator=Mock(road=road))
        result = tracker._trackDecelerations(lambda _: True)
        expected = AverageResult(value=10, count=20)
        self.assertEqual(result, expected, '{} != {}'.format(str(result), str(expected)))
Ejemplo n.º 7
0
 def _trackPercentage(self, count_filter: Filter,
                      value_filter) -> AverageResult:
     value, count = 0, 0
     for vehicle in filter(count_filter, self._road.getAllActiveVehicles()):
         if value_filter(vehicle):
             value += 1
         count += 1
     return AverageResult(value=value, count=count)
Ejemplo n.º 8
0
 def __init__(self, simulator: Simulator, buffer_size: int = 1):
     super().__init__(simulator=simulator)
     self.steps = 0
     self.velocity = {}
     self.throughput = {}
     self.decelerations = {}
     self.lane_changes = {}
     self.waiting = {}
     for vehicle_type in VehicleType:
         self.velocity[vehicle_type] = CumulativeList(
             buffer_size, AverageResult(0, 0))
         self.throughput[vehicle_type] = CumulativeList(buffer_size, 0)
         self.decelerations[vehicle_type] = CumulativeList(
             buffer_size, AverageResult(0, 0))
         self.lane_changes[vehicle_type] = CumulativeList(
             buffer_size, AverageResult(0, 0))
         self.waiting[vehicle_type] = CumulativeList(
             buffer_size, AverageResult(0, 0))
Ejemplo n.º 9
0
    def test_trackVelocity(self):
        vehicles: typing.List[Vehicle] = [Mock(velocity=v) for v in range(10)]
        road = Mock()
        road.getAllActiveVehicles = lambda: vehicles
        tracker = Tracker(simulator=Mock(road=road))
        # No vehicles matching the predicate.
        result = tracker._trackVelocity(lambda _: False)
        expected = AverageResult(value=0, count=0)
        self.assertEqual(result, expected, '{} != {}'.format(str(result), str(expected)))
        # All vehicles matching the predicate.
        result = tracker._trackVelocity(lambda _: True)
        expected = AverageResult(value=45, count=10)
        self.assertEqual(result, expected, '{} != {}'.format(str(result), str(expected)))

        # Some vehicles matching the predicate.
        def isEven(vehicle):
            return vehicle.velocity % 2 == 0

        result = tracker._trackVelocity(isEven)
        expected = AverageResult(value=20, count=5)
        self.assertEqual(result, expected, '{} != {}'.format(str(result), str(expected)))
Ejemplo n.º 10
0
 def _collectVelocity(self) -> None:
     for vehicle in self._road.getAllActiveVehicles():
         last_x, _ = self._road.getAbsolutePosition(vehicle.last_position)
         cur_x, lane = self._road.getAbsolutePosition(vehicle.position)
         for x in range(last_x, cur_x):
             if x >= self._road.length:
                 continue
             value = AverageResult(value=vehicle.velocity, count=1)
             if isCar(vehicle):
                 self.velocity[lane][x] += value
             if isAutonomous(vehicle):
                 self.velocity_autonomous[lane][x] += value
             if isConventional(vehicle):
                 self.velocity_conventional[lane][x] += value
Ejemplo n.º 11
0
 def test_eq(self):
     a = AverageResult(1, 10)
     b = AverageResult(1, 10)
     self.assertTrue(a == b)
     self.assertTrue(b == a)
     self.assertTrue(a == a)
     self.assertTrue(b == b)
     a = AverageResult(10, 10)
     b = AverageResult(20, 20)
     self.assertFalse(a == b)
     self.assertFalse(b == a)
     a = AverageResult(10, 10)
     b = AverageResult(10, 20)
     self.assertFalse(a == b)
     self.assertFalse(b == a)
     a = AverageResult(20, 10)
     b = AverageResult(20, 20)
     self.assertFalse(a == b)
     self.assertFalse(b == a)
Ejemplo n.º 12
0
 def test_toMaybeFloat(self):
     self.assertIsNone(AverageResult(42, 0).toMaybeFloat())
     self.assertEqual(.5, AverageResult(1, 2).toMaybeFloat())
     self.assertEqual(.5, AverageResult(2, 4).toMaybeFloat())
     self.assertEqual(2.5, AverageResult(5, 2).toMaybeFloat())
Ejemplo n.º 13
0
 def test_float(self):
     with self.assertRaises(ZeroDivisionError):
         _ = float(AverageResult(42, 0))
     self.assertEqual(.5, float(AverageResult(1, 2)))
     self.assertEqual(.5, float(AverageResult(2, 4)))
     self.assertEqual(2.5, float(AverageResult(5, 2)))
Ejemplo n.º 14
0
 def test_str(self):
     self.assertEqual('0/0', str(AverageResult(0, 0)))
     self.assertEqual('42/1', str(AverageResult(42, 1)))
     self.assertEqual('1/42', str(AverageResult(1, 42)))
Ejemplo n.º 15
0
 def _trackVelocity(self, predicate: Filter) -> AverageResult:
     velocity, count = 0, 0
     for vehicle in filter(predicate, self._road.getAllActiveVehicles()):
         velocity += vehicle.velocity
         count += 1
     return AverageResult(value=velocity, count=count)
Ejemplo n.º 16
0
 def test_toZeroFloat(self):
     self.assertEqual(0, AverageResult(42, 0).toZeroFloat())
     self.assertEqual(.5, AverageResult(1, 2).toZeroFloat())
     self.assertEqual(.5, AverageResult(2, 4).toZeroFloat())
     self.assertEqual(2.5, AverageResult(5, 2).toZeroFloat())
Ejemplo n.º 17
0
 def mapper(x: AverageResult) -> float:
     return x.toZeroFloat()