Example #1
0
    def test_courier_available_event(self):
        """Test to verify the mechanics of how the dispatcher sets a courier to available"""

        # Constants
        initial_time = hour_to_sec(14)
        on_time = time(14, 0, 0)
        off_time = time(15, 0, 0)
        service_time = min_to_sec(6)

        # Services
        env = Environment(initial_time=initial_time)
        dispatcher = Dispatcher(env=env, matching_policy=DummyMatchingPolicy())

        # Creates a courier and sets it to the picking state
        courier = Courier(dispatcher=dispatcher, env=env, courier_id=32, on_time=on_time, off_time=off_time)
        dispatcher.idle_couriers = {courier.courier_id: courier}
        env.process(courier._picking_up_state(
            orders={
                21: Order(drop_off_service_time=service_time, ready_time=time(14, 20, 0))
            }
        ))
        env.run(until=initial_time + min_to_sec(10))

        # Verify courier properties are modified and it is allocated correctly
        self.assertEqual(courier.condition, 'picking_up')
        self.assertIn(courier.courier_id, dispatcher.picking_up_couriers.keys())
        self.assertEqual(dispatcher.idle_couriers, {})
Example #2
0
    def test_movement_state(self, osrm):
        """Test to evaluate how a courier moves with dummy movement"""

        # Constants
        random.seed(365)
        on_time = time(0, 0, 0)
        off_time = time(5, 0, 0)

        env = Environment()
        dispatcher = Dispatcher(env=env, matching_policy=DummyMatchingPolicy())

        # Creates a courier and runs a simulation
        courier = Courier(
            acceptance_policy=self.acceptance_policy,
            dispatcher=dispatcher,
            env=env,
            movement_evaluation_policy=self.movement_evaluation_policy,
            movement_policy=self.movement_policy,
            courier_id=self.courier_id,
            vehicle=self.vehicle,
            location=self.start_location,
            on_time=on_time,
            off_time=off_time)
        env.run(until=hour_to_sec(4) + min_to_sec(5))

        # Asserts that the courier moved and is is in a different location
        self.assertEqual(courier.condition, 'moving')
        self.assertNotEqual(courier.location, self.start_location)
        self.assertIn(courier.courier_id, dispatcher.moving_couriers.keys())
Example #3
0
    def assign_courier(_user: User, _env: Environment, _dispatcher: Dispatcher):
        """Dummy method to assign a courier"""

        yield _env.timeout(delay=min_to_sec(8))
        _user.order.courier_id = 999
        del _dispatcher.unassigned_orders[_user.order.order_id]
        _dispatcher.assigned_orders[_user.order.order_id] = _user.order
Example #4
0
    def test_prepositioning_notification_rejected_event(self):
        """Test to verify the mechanics of a prepositioning notification being rejected by a courier"""

        # Constants
        initial_time = hour_to_sec(14)
        on_time = time(14, 0, 0)
        off_time = time(15, 0, 0)

        # Services
        env = Environment(initial_time=initial_time)
        dispatcher = Dispatcher(env=env, matching_policy=DummyMatchingPolicy())

        # Creates a prepositioning notification, a courier and sends the rejected event
        instruction = Route(
            stops=[
                Stop(position=0, type=StopType.PREPOSITION),
                Stop(position=1, type=StopType.PREPOSITION)
            ]
        )
        courier = Courier(dispatcher=dispatcher, env=env, courier_id=981, on_time=on_time, off_time=off_time)
        notification = Notification(courier=courier, instruction=instruction, type=NotificationType.PREPOSITIONING)
        dispatcher.notification_rejected_event(notification=notification, courier=courier)
        env.run(until=initial_time + min_to_sec(30))

        # Verify order and courier properties are modified and it is allocated correctly
        self.assertIsNone(courier.active_route)
Example #5
0
    def test_courier_busy_event(self, *args):
        """Test to verify the mechanics of how the dispatcher sets a courier to busy"""

        # Constants
        initial_time = hour_to_sec(14)
        courier_id = 14
        time_delta = min_to_sec(10)
        on_time = time(14, 0, 0)
        off_time = time(15, 0, 0)
        service_time = min_to_sec(7)

        # Verifies 2 test cases for how the courier transitions to being busy
        # For each test case, assert the courier starts in a set and ends up in the busy set

        # Test 1: courier starts dropping off state
        env = Environment(initial_time=initial_time)
        dispatcher = Dispatcher(env=env, matching_policy=DummyMatchingPolicy())
        courier = Courier(dispatcher=dispatcher, env=env, courier_id=courier_id, on_time=on_time, off_time=off_time)
        env.process(courier._dropping_off_state(
            orders={
                21: Order(drop_off_service_time=service_time, ready_time=time(12, 20, 0))
            }
        ))
        env.run(until=initial_time + time_delta)
        self.assertEqual(courier.condition, 'dropping_off')
        self.assertEqual(dispatcher.dropping_off_couriers, {courier.courier_id: courier})
        self.assertEqual(dispatcher.idle_couriers, {})

        # Test 2: courier start moving state
        env = Environment(initial_time=initial_time)
        dispatcher = Dispatcher(env=env, matching_policy=DummyMatchingPolicy())
        courier = Courier(
            dispatcher=dispatcher,
            env=env,
            courier_id=courier_id,
            location=Location(lat=4.690296, lng=-74.043929),
            on_time=on_time,
            off_time=off_time
        )
        env.process(courier._moving_state(destination=Location(lat=4.689697, lng=-74.055495)))
        env.run(until=initial_time + time_delta)
        self.assertEqual(courier.condition, 'moving')
        self.assertEqual(dispatcher.moving_couriers, {courier.courier_id: courier})
        self.assertEqual(dispatcher.idle_couriers, {})
Example #6
0
    def test_notify_prepositioning_event_accept_idle(self, osrm):
        """Test to evaluate how a courier handles a prepositioning notification while being idle and accepts it"""

        # Constants
        random.seed(348)
        initial_time = hour_to_sec(17)
        time_delta = min_to_sec(10)
        on_time = time(17, 0, 0)
        off_time = time(17, 30, 0)

        # Services
        env = Environment(initial_time=initial_time)
        dispatcher = Dispatcher(env=env, matching_policy=DummyMatchingPolicy())

        # Creates a courier with high acceptance rate and immediately send a prepositioning notification
        courier = Courier(
            acceptance_policy=self.acceptance_policy,
            dispatcher=dispatcher,
            env=env,
            movement_evaluation_policy=self.movement_evaluation_policy,
            movement_policy=self.movement_policy,
            courier_id=self.courier_id,
            vehicle=self.vehicle,
            location=self.start_location,
            acceptance_rate=0.99,
            on_time=on_time,
            off_time=off_time)

        instruction = Route(orders=None,
                            stops=[
                                Stop(location=self.pick_up_at,
                                     position=0,
                                     orders=None,
                                     type=StopType.PREPOSITION,
                                     visited=False)
                            ])
        notification = Notification(courier=courier,
                                    instruction=instruction,
                                    type=NotificationType.PREPOSITIONING)
        env.process(courier.notification_event(notification))
        env.run(until=initial_time + time_delta)

        # Asserts that the courier fulfilled the route and is at a different start location
        self.assertIsNone(courier.active_route)
        self.assertIsNone(courier.active_stop)
        self.assertEqual(dispatcher.fulfilled_orders, {})
        self.assertNotEqual(courier.location, self.start_location)
        self.assertEqual(courier.condition, 'idle')
        self.assertIn(courier.courier_id, dispatcher.idle_couriers.keys())
Example #7
0
    def test_courier_idle_event(self, *args):
        """Test to verifiy the mechanics of how a courier is set to idle by the dispatcher"""

        # Constants
        initial_time = hour_to_sec(14)
        courier_id = 85
        time_delta = min_to_sec(10)
        random.seed(26)
        on_time = time(14, 0, 0)
        off_time = time(15, 0, 0)

        # Verifies 3 test cases: when the courier is busy, available or idle.
        # For each test case, assert the courier starts in a set and ends up in the idle set

        # Test 1: courier is busy
        env = Environment(initial_time=initial_time)
        dispatcher = Dispatcher(env=env, matching_policy=DummyMatchingPolicy())
        courier = Courier(dispatcher=dispatcher, env=env, courier_id=courier_id, on_time=on_time, off_time=off_time)
        dispatcher.moving_couriers = {courier.courier_id: courier}
        dispatcher.courier_idle_event(courier)
        env.run(until=initial_time + time_delta)
        self.assertIn(courier.courier_id, dispatcher.idle_couriers.keys())
        self.assertEqual(dispatcher.moving_couriers, {})
        self.assertEqual(dispatcher.picking_up_couriers, {})

        # Test 2: courier is available
        env = Environment(initial_time=initial_time)
        dispatcher = Dispatcher(env=env, matching_policy=DummyMatchingPolicy())
        courier = Courier(dispatcher=dispatcher, env=env, courier_id=courier_id, on_time=on_time, off_time=off_time)
        dispatcher.picking_up_couriers = {courier.courier_id: courier}
        dispatcher.courier_idle_event(courier)
        env.run(until=initial_time + time_delta)
        self.assertIn(courier.courier_id, dispatcher.idle_couriers.keys())
        self.assertEqual(dispatcher.moving_couriers, {})
        self.assertEqual(dispatcher.picking_up_couriers, {})

        # Test 3: courier is idle
        env = Environment(initial_time=initial_time)
        dispatcher = Dispatcher(env=env, matching_policy=DummyMatchingPolicy())
        courier = Courier(dispatcher=dispatcher, env=env, courier_id=courier_id, on_time=on_time, off_time=off_time)
        dispatcher.idle_couriers = {courier.courier_id: courier}
        dispatcher.courier_idle_event(courier)
        env.run(until=initial_time + time_delta)
        self.assertIn(courier.courier_id, dispatcher.idle_couriers.keys())
        self.assertEqual(dispatcher.moving_couriers, {})
        self.assertEqual(dispatcher.picking_up_couriers, {})
Example #8
0
    def test_buffer_event(self):
        """Test to verify how the mechanics of the dispatcher buffering orders work"""

        # Constants
        initial_time = hour_to_sec(16)
        placement_time = time(16, 0, 0)
        time_delta = min_to_sec(10)

        # Verifies two test cases for how the dispatcher buffers orders
        # For each test, assert the correct number of orders are buffered

        # Test 1: schedules the submission of three orders and assert that only two are buffered
        env = Environment(initial_time=initial_time)
        dispatcher = Dispatcher(
            env=env,
            buffering_policy=RollingBufferingPolicy(),
            matching_policy=DummyMatchingPolicy()
        )
        order_1 = Order(order_id=1, placement_time=placement_time)
        order_2 = Order(order_id=2, placement_time=placement_time)
        order_3 = Order(order_id=3, placement_time=placement_time)
        dispatcher.order_submitted_event(order_1, preparation_time=time(16, 0, 27), ready_time=time(16, 10, 0))
        dispatcher.order_submitted_event(order_2, preparation_time=time(16, 0, 43), ready_time=time(16, 10, 0))
        dispatcher.order_submitted_event(order_3, preparation_time=time(18, 0, 0), ready_time=time(18, 10, 0))

        env.run(until=initial_time + time_delta)
        self.assertEqual(len(dispatcher.unassigned_orders), 2)

        # Test 2: schedules the submission of three orders and assert that all three orders are buffered
        env = Environment(initial_time=initial_time)
        dispatcher = Dispatcher(
            env=env,
            buffering_policy=RollingBufferingPolicy(),
            matching_policy=DummyMatchingPolicy()
        )
        order_1 = Order(order_id=1, placement_time=placement_time)
        order_2 = Order(order_id=2, placement_time=placement_time)
        order_3 = Order(order_id=3, placement_time=placement_time)
        dispatcher.order_submitted_event(order_1, preparation_time=time(16, 0, 27), ready_time=time(16, 10, 0))
        dispatcher.order_submitted_event(order_2, preparation_time=time(16, 0, 43), ready_time=time(16, 10, 0))
        dispatcher.order_submitted_event(order_3, preparation_time=time(16, 4, 1), ready_time=time(16, 14, 0))
        env.run(until=initial_time + time_delta)
        self.assertEqual(len(dispatcher.unassigned_orders), 3)
Example #9
0
    def test_notification_accepted_event(self):
        """Test to verify the mechanics of a notification being accepted by a courier"""

        # Constants
        initial_time = hour_to_sec(14)
        on_time = time(14, 0, 0)
        off_time = time(15, 0, 0)

        # Services
        env = Environment(initial_time=initial_time)
        dispatcher = Dispatcher(env=env, matching_policy=DummyMatchingPolicy())

        # Creates an instruction with an order, a courier and sends the accepted event
        order = Order(order_id=45)
        instruction = Route(
            stops=[
                Stop(orders={order.order_id: order}, position=0),
                Stop(orders={order.order_id: order}, position=1)
            ],
            orders={order.order_id: order}
        )
        dispatcher.unassigned_orders[order.order_id] = order
        courier = Courier(dispatcher=dispatcher, env=env, courier_id=89, on_time=on_time, off_time=off_time)
        courier.condition = 'idle'
        notification = Notification(
            courier=courier,
            instruction=instruction
        )
        dispatcher.notification_accepted_event(notification=notification, courier=courier)
        env.run(until=initial_time + min_to_sec(10))

        # Verify order and courier properties are modified and it is allocated correctly
        self.assertEqual(order.state, 'in_progress')
        self.assertEqual(order.acceptance_time, sec_to_time(initial_time))
        self.assertEqual(order.courier_id, courier.courier_id)
        self.assertIn(order.order_id, dispatcher.assigned_orders.keys())
        self.assertIsNotNone(courier.active_route)
        self.assertEqual(courier.active_route, instruction)
        self.assertEqual(dispatcher.unassigned_orders, {})
Example #10
0
    def test_submit_cancel_order(self):
        """Test to verify how a user submits and decides to cancel an order"""

        # Constants
        random.seed(666)
        initial_time = hour_to_sec(12)
        time_delta = min_to_sec(10)

        # Services
        env = Environment(initial_time=initial_time)
        dispatcher = Dispatcher(env=env, matching_policy=DummyMatchingPolicy())

        # Create a user and have it submit an order immediately
        user = User(cancellation_policy=self.cancellation_policy,
                    dispatcher=dispatcher,
                    env=env)
        user.submit_order_event(
            order_id=self.order_id,
            pick_up_at=self.pick_up_at,
            drop_off_at=self.drop_off_at,
            placement_time=self.placement_time,
            expected_drop_off_time=self.expected_drop_off_time,
            preparation_time=self.preparation_time,
            ready_time=self.ready_time)
        env.run(until=initial_time + time_delta)

        # Verify order is created and canceled due to a courier not being assigned
        self.assertTrue(user.order)
        self.assertIsNone(user.order.courier_id)
        self.assertIsNotNone(user.order.cancellation_time)
        self.assertEqual(dispatcher.unassigned_orders, {})
        self.assertIn(self.order_id, dispatcher.canceled_orders.keys())
        self.assertEqual(
            user.order.cancellation_time,
            (datetime.combine(date.today(), self.placement_time) +
             timedelta(seconds=settings.USER_WAIT_TO_CANCEL)).time())
        self.assertEqual(user.condition, 'canceled')
Example #11
0
    def test_generate_matching_prospects_all(self, osrm):
        """Test to verify how prospects are created"""

        # Constants
        env_time = hour_to_sec(12) + min_to_sec(20)
        on_time = time(8, 0, 0)
        off_time = time(16, 0, 0)

        # Orders
        order_1 = Order(order_id=1,
                        pick_up_at=Location(lat=4.678759, lng=-74.055729),
                        drop_off_at=Location(lat=4.681694, lng=-74.044811),
                        ready_time=time(12, 30, 0),
                        expected_drop_off_time=time(12, 40, 0),
                        pick_up_service_time=0,
                        drop_off_service_time=0)
        order_2 = Order(order_id=2,
                        pick_up_at=Location(lat=4.678759, lng=-74.055729),
                        drop_off_at=Location(lat=4.695001, lng=-74.040737),
                        ready_time=time(12, 32, 0),
                        expected_drop_off_time=time(12, 42, 0),
                        pick_up_service_time=0,
                        drop_off_service_time=0)
        order_3 = Order(order_id=3,
                        pick_up_at=Location(lat=4.678759, lng=-74.055729),
                        drop_off_at=Location(lat=4.668742, lng=-74.056684),
                        ready_time=time(12, 33, 0),
                        expected_drop_off_time=time(12, 43, 0),
                        pick_up_service_time=0,
                        drop_off_service_time=0)
        order_4 = Order(order_id=4,
                        pick_up_at=Location(lat=4.678759, lng=-74.055729),
                        drop_off_at=Location(lat=4.661441, lng=-74.056955),
                        ready_time=time(12, 34, 0),
                        expected_drop_off_time=time(12, 44, 0),
                        pick_up_service_time=0,
                        drop_off_service_time=0)

        # Couriers
        courier_1 = Courier(courier_id=1,
                            on_time=on_time,
                            off_time=off_time,
                            condition='idle',
                            location=Location(lat=4.676854, lng=-74.057498))
        courier_2 = Courier(courier_id=2,
                            on_time=on_time,
                            off_time=off_time,
                            condition='idle',
                            location=Location(lat=4.679408, lng=-74.052524))

        # Routes
        policy = MyopicMatchingPolicy(assignment_updates=True,
                                      prospects=True,
                                      notification_filtering=False,
                                      mip_matcher=False)
        routes = policy._generate_routes(
            orders=[order_1, order_2, order_3, order_4],
            couriers=[courier_1, courier_2],
            env_time=env_time)

        # Generate prospects and assert expected behavior
        prospects = policy._generate_matching_prospects(
            routes=routes, couriers=[courier_1, courier_2], env_time=env_time)
        self.assertTrue(prospects.tolist())
        self.assertEqual(len(prospects), 8)
Example #12
0
    def test_notify_event_accept_idle(self, osrm):
        """Test to evaluate how a courier handles a notification while being idle and accepts it"""

        # Constants
        random.seed(126)
        initial_time = hour_to_sec(12)
        time_delta = min_to_sec(40)
        on_time = time(12, 0, 0)
        off_time = time(13, 0, 0)

        # Services
        env = Environment(initial_time=initial_time)
        dispatcher = Dispatcher(env=env, matching_policy=DummyMatchingPolicy())

        # Creates a courier with high acceptance rate and immediately send a new instruction, composed of a single order
        courier = Courier(
            acceptance_policy=self.acceptance_policy,
            dispatcher=dispatcher,
            env=env,
            movement_evaluation_policy=self.movement_evaluation_policy,
            movement_policy=self.movement_policy,
            courier_id=self.courier_id,
            vehicle=self.vehicle,
            location=self.start_location,
            acceptance_rate=0.99,
            on_time=on_time,
            off_time=off_time)

        order = Order(order_id=self.order_id,
                      drop_off_at=self.drop_off_at,
                      pick_up_at=self.pick_up_at,
                      placement_time=self.placement_time,
                      expected_drop_off_time=self.expected_drop_off_time,
                      preparation_time=self.preparation_time,
                      ready_time=self.ready_time,
                      user=User(env=env))
        dispatcher.unassigned_orders[order.order_id] = order
        instruction = Route(orders={self.order_id: order},
                            stops=[
                                Stop(location=self.pick_up_at,
                                     position=0,
                                     orders={self.order_id: order},
                                     type=StopType.PICK_UP,
                                     visited=False),
                                Stop(location=self.drop_off_at,
                                     position=1,
                                     orders={self.order_id: order},
                                     type=StopType.DROP_OFF,
                                     visited=False)
                            ])
        notification = Notification(courier=courier, instruction=instruction)
        env.process(courier.notification_event(notification))
        env.run(until=initial_time + time_delta)

        # Asserts that the courier fulfilled the route and is at a different start location
        self.assertIsNotNone(order.pick_up_time)
        self.assertIsNotNone(order.drop_off_time)
        self.assertEqual(order.courier_id, courier.courier_id)
        self.assertTrue(order.pick_up_time < order.drop_off_time)
        self.assertIsNone(courier.active_route)
        self.assertIsNone(courier.active_stop)
        self.assertEqual(dispatcher.fulfilled_orders, {order.order_id: order})
        self.assertEqual(order.state, 'dropped_off')
        self.assertNotEqual(courier.location, self.start_location)
        self.assertEqual(courier.condition, 'idle')
        self.assertIn(courier.courier_id, dispatcher.idle_couriers.keys())
Example #13
0
class TestsCourier(unittest.TestCase):
    """Tests for the Courier actor class"""

    # Properties to be reused
    courier_id = 56
    vehicle = Vehicle.MOTORCYCLE
    start_location = Location(lat=4.697893, lng=-74.051565)

    order_id = 0
    pick_up_at = Location(lat=4.689697, lng=-74.055495)
    drop_off_at = Location(lat=4.690296, lng=-74.043929)
    placement_time = time(12, 0, 0)
    expected_drop_off_time = time(12, 40, 0)
    preparation_time = time(12, 2, 0)
    ready_time = time(12, 12, 0)

    # Services to be reused
    acceptance_policy = UniformAcceptancePolicy()
    movement_evaluation_policy = NeighborsMoveEvalPolicy()
    movement_policy = OSRMMovementPolicy()

    @patch('settings.settings.COURIER_MOVEMENT_PROBABILITY', 0.01)
    @patch('services.osrm_service.OSRMService.get_route',
           side_effect=mocked_get_route)
    def test_always_idle(self, osrm):
        """Test to evaluate a courier never moving"""

        # Constants
        random.seed(187)
        on_time = time(0, 0, 0)
        off_time = time(5, 0, 0)

        # Services
        env = Environment()
        dispatcher = Dispatcher(env=env, matching_policy=DummyMatchingPolicy())

        # Creates a courier and runs a simulation
        courier = Courier(
            acceptance_policy=self.acceptance_policy,
            dispatcher=dispatcher,
            env=env,
            movement_evaluation_policy=self.movement_evaluation_policy,
            movement_policy=self.movement_policy,
            courier_id=self.courier_id,
            vehicle=self.vehicle,
            location=self.start_location,
            on_time=on_time,
            off_time=off_time)
        env.run(until=hour_to_sec(4))

        # Asserts that the courier is idle and never moved
        self.assertEqual(courier.condition, 'idle')
        self.assertEqual(courier.location, self.start_location)
        self.assertIn(courier.courier_id, dispatcher.idle_couriers.keys())

    @patch('services.osrm_service.OSRMService.get_route',
           side_effect=mocked_get_route)
    @patch('settings.settings.COURIER_MOVEMENT_PROBABILITY', 0.95)
    @patch('settings.settings.COURIER_WAIT_TO_MOVE', min_to_sec(7))
    def test_movement_state(self, osrm):
        """Test to evaluate how a courier moves with dummy movement"""

        # Constants
        random.seed(365)
        on_time = time(0, 0, 0)
        off_time = time(5, 0, 0)

        env = Environment()
        dispatcher = Dispatcher(env=env, matching_policy=DummyMatchingPolicy())

        # Creates a courier and runs a simulation
        courier = Courier(
            acceptance_policy=self.acceptance_policy,
            dispatcher=dispatcher,
            env=env,
            movement_evaluation_policy=self.movement_evaluation_policy,
            movement_policy=self.movement_policy,
            courier_id=self.courier_id,
            vehicle=self.vehicle,
            location=self.start_location,
            on_time=on_time,
            off_time=off_time)
        env.run(until=hour_to_sec(4) + min_to_sec(5))

        # Asserts that the courier moved and is is in a different location
        self.assertEqual(courier.condition, 'moving')
        self.assertNotEqual(courier.location, self.start_location)
        self.assertIn(courier.courier_id, dispatcher.moving_couriers.keys())

    @patch('services.osrm_service.OSRMService.get_route',
           side_effect=mocked_get_route)
    @patch('settings.settings.COURIER_MOVEMENT_PROBABILITY', 0.1)
    @patch('settings.settings.COURIER_MIN_ACCEPTANCE_RATE', 0.99)
    def test_notify_event_accept_idle(self, osrm):
        """Test to evaluate how a courier handles a notification while being idle and accepts it"""

        # Constants
        random.seed(126)
        initial_time = hour_to_sec(12)
        time_delta = min_to_sec(40)
        on_time = time(12, 0, 0)
        off_time = time(13, 0, 0)

        # Services
        env = Environment(initial_time=initial_time)
        dispatcher = Dispatcher(env=env, matching_policy=DummyMatchingPolicy())

        # Creates a courier with high acceptance rate and immediately send a new instruction, composed of a single order
        courier = Courier(
            acceptance_policy=self.acceptance_policy,
            dispatcher=dispatcher,
            env=env,
            movement_evaluation_policy=self.movement_evaluation_policy,
            movement_policy=self.movement_policy,
            courier_id=self.courier_id,
            vehicle=self.vehicle,
            location=self.start_location,
            acceptance_rate=0.99,
            on_time=on_time,
            off_time=off_time)

        order = Order(order_id=self.order_id,
                      drop_off_at=self.drop_off_at,
                      pick_up_at=self.pick_up_at,
                      placement_time=self.placement_time,
                      expected_drop_off_time=self.expected_drop_off_time,
                      preparation_time=self.preparation_time,
                      ready_time=self.ready_time,
                      user=User(env=env))
        dispatcher.unassigned_orders[order.order_id] = order
        instruction = Route(orders={self.order_id: order},
                            stops=[
                                Stop(location=self.pick_up_at,
                                     position=0,
                                     orders={self.order_id: order},
                                     type=StopType.PICK_UP,
                                     visited=False),
                                Stop(location=self.drop_off_at,
                                     position=1,
                                     orders={self.order_id: order},
                                     type=StopType.DROP_OFF,
                                     visited=False)
                            ])
        notification = Notification(courier=courier, instruction=instruction)
        env.process(courier.notification_event(notification))
        env.run(until=initial_time + time_delta)

        # Asserts that the courier fulfilled the route and is at a different start location
        self.assertIsNotNone(order.pick_up_time)
        self.assertIsNotNone(order.drop_off_time)
        self.assertEqual(order.courier_id, courier.courier_id)
        self.assertTrue(order.pick_up_time < order.drop_off_time)
        self.assertIsNone(courier.active_route)
        self.assertIsNone(courier.active_stop)
        self.assertEqual(dispatcher.fulfilled_orders, {order.order_id: order})
        self.assertEqual(order.state, 'dropped_off')
        self.assertNotEqual(courier.location, self.start_location)
        self.assertEqual(courier.condition, 'idle')
        self.assertIn(courier.courier_id, dispatcher.idle_couriers.keys())

    @patch('services.osrm_service.OSRMService.get_route',
           side_effect=mocked_get_route)
    @patch('settings.settings.COURIER_MOVEMENT_PROBABILITY', 0.1)
    @patch('settings.settings.COURIER_MIN_ACCEPTANCE_RATE', 0.99)
    def test_notify_event_reject_idle(self, osrm):
        """Test to evaluate how a courier handles a notification while being idle and rejects it"""

        # Constants
        random.seed(122)
        on_time = time(12, 0, 0)
        off_time = time(15, 0, 0)

        # Services
        env = Environment(initial_time=hour_to_sec(12))
        dispatcher = Dispatcher(env=env, matching_policy=DummyMatchingPolicy())

        # Creates a courier with low acceptance rate and immediately send a new instruction, composed of a single order
        courier = Courier(
            acceptance_policy=self.acceptance_policy,
            dispatcher=dispatcher,
            env=env,
            movement_evaluation_policy=self.movement_evaluation_policy,
            movement_policy=self.movement_policy,
            courier_id=self.courier_id,
            vehicle=self.vehicle,
            location=self.start_location,
            acceptance_rate=0.01,
            on_time=on_time,
            off_time=off_time)

        order = Order(order_id=self.order_id,
                      drop_off_at=self.drop_off_at,
                      pick_up_at=self.pick_up_at,
                      placement_time=self.placement_time,
                      expected_drop_off_time=self.expected_drop_off_time,
                      preparation_time=self.preparation_time,
                      ready_time=self.ready_time)
        dispatcher.unassigned_orders[order.order_id] = order
        instruction = Route(orders={self.order_id: order},
                            stops=[
                                Stop(location=self.pick_up_at,
                                     position=0,
                                     orders={self.order_id: order},
                                     type=StopType.PICK_UP,
                                     visited=False),
                                Stop(location=self.drop_off_at,
                                     position=1,
                                     orders={self.order_id: order},
                                     type=StopType.DROP_OFF,
                                     visited=False)
                            ])
        notification = Notification(courier=courier, instruction=instruction)
        env.process(courier.notification_event(notification))
        env.run(until=hour_to_sec(14))

        # Asserts that the courier didn't fulfill the route
        self.assertIsNone(order.pick_up_time)
        self.assertIsNone(order.drop_off_time)
        self.assertIsNone(order.courier_id)
        self.assertIsNone(courier.active_route)
        self.assertIsNone(courier.active_stop)
        self.assertIn(courier.courier_id, order.rejected_by)
        self.assertIn(order.order_id, courier.rejected_orders)
        self.assertEqual(dispatcher.unassigned_orders, {order.order_id: order})
        self.assertEqual(order.state, 'unassigned')
        self.assertEqual(courier.location, self.start_location)
        self.assertEqual(courier.condition, 'idle')
        self.assertIn(courier.courier_id, dispatcher.idle_couriers.keys())

    @patch('services.osrm_service.OSRMService.get_route',
           side_effect=mocked_get_route)
    @patch('settings.settings.COURIER_MOVEMENT_PROBABILITY', 0.99)
    @patch('settings.settings.COURIER_MIN_ACCEPTANCE_RATE', 0.99)
    def test_notify_event_accept_picking_up(self, osrm):
        """Test to evaluate how a courier handles a notification while picking up and accepts it"""

        # Constants
        random.seed(184)
        on_time = time(12, 0, 0)
        off_time = time(15, 0, 0)

        # Services
        env = Environment(initial_time=hour_to_sec(12) + min_to_sec(12))
        dispatcher = Dispatcher(env=env, matching_policy=DummyMatchingPolicy())

        # Creates a courier with high acceptance rate, an active route and in state of picking up.
        # Sends a new instruction, composed of a single new order
        active_order = Order(
            order_id=self.order_id,
            drop_off_at=self.drop_off_at,
            pick_up_at=self.pick_up_at,
            placement_time=self.placement_time,
            expected_drop_off_time=self.expected_drop_off_time,
            preparation_time=self.preparation_time,
            ready_time=self.ready_time,
            courier_id=self.courier_id,
            user=User(env=env))
        dispatcher.assigned_orders[active_order.order_id] = active_order
        new_order = Order(order_id=17,
                          drop_off_at=Location(lat=4.694627, lng=-74.038886),
                          pick_up_at=self.pick_up_at,
                          placement_time=self.placement_time,
                          expected_drop_off_time=self.expected_drop_off_time,
                          preparation_time=self.preparation_time,
                          ready_time=self.ready_time,
                          user=User(env=env))
        dispatcher.unassigned_orders[new_order.order_id] = new_order
        courier = Courier(
            acceptance_policy=self.acceptance_policy,
            dispatcher=dispatcher,
            env=env,
            movement_evaluation_policy=self.movement_evaluation_policy,
            movement_policy=self.movement_policy,
            courier_id=self.courier_id,
            vehicle=self.vehicle,
            location=active_order.pick_up_at,
            acceptance_rate=0.99,
            active_route=Route(orders={self.order_id: active_order},
                               stops=[
                                   Stop(location=self.pick_up_at,
                                        position=0,
                                        orders={self.order_id: active_order},
                                        type=StopType.PICK_UP,
                                        visited=False),
                                   Stop(location=self.drop_off_at,
                                        position=1,
                                        orders={self.order_id: active_order},
                                        type=StopType.DROP_OFF,
                                        visited=False)
                               ]),
            on_time=on_time,
            off_time=off_time)

        instruction = [
            Stop(location=new_order.drop_off_at,
                 position=1,
                 orders={new_order.order_id: new_order},
                 type=StopType.DROP_OFF,
                 visited=False)
        ]
        notification = Notification(courier=courier, instruction=instruction)
        courier.state.interrupt()
        courier.active_stop = courier.active_route.stops[0]
        courier.state = env.process(
            courier._picking_up_state(
                orders={active_order.order_id: active_order}))
        env.process(courier.notification_event(notification))
        env.run(until=hour_to_sec(13) + min_to_sec(12))

        # Asserts that the courier fulfilled the active and new order and is at a different start location
        self.assertIsNotNone(active_order.pick_up_time)
        self.assertIsNotNone(active_order.drop_off_time)
        self.assertEqual(active_order.courier_id, courier.courier_id)
        self.assertTrue(active_order.pick_up_time < active_order.drop_off_time)
        self.assertEqual(active_order.state, 'dropped_off')

        self.assertIsNotNone(new_order.pick_up_time)
        self.assertIsNotNone(new_order.drop_off_time)
        self.assertEqual(new_order.courier_id, courier.courier_id)
        self.assertTrue(new_order.pick_up_time < new_order.drop_off_time)
        self.assertEqual(new_order.state, 'dropped_off')

        self.assertIsNone(courier.active_route)
        self.assertIsNone(courier.active_stop)
        self.assertNotEqual(courier.location, self.start_location)
        self.assertEqual(dispatcher.fulfilled_orders, {
            active_order.order_id: active_order,
            new_order.order_id: new_order
        })
        self.assertEqual(courier.condition, 'idle')
        self.assertIn(courier.courier_id, dispatcher.idle_couriers.keys())

    @patch('services.osrm_service.OSRMService.get_route',
           side_effect=mocked_get_route)
    @patch('settings.settings.COURIER_MOVEMENT_PROBABILITY', 0.01)
    @patch('settings.settings.COURIER_MIN_ACCEPTANCE_RATE', 0.99)
    def test_notify_event_reject_picking_up(self, osrm):
        """Test to evaluate how a courier handles a notification while picking up and rejects it"""

        # Constants
        random.seed(4747474)
        on_time = time(12, 0, 0)
        off_time = time(15, 0, 0)

        # Services
        env = Environment(initial_time=hour_to_sec(12) + min_to_sec(12))
        dispatcher = Dispatcher(env=env, matching_policy=DummyMatchingPolicy())

        # Creates a courier with low acceptance rate, an active route and in state of picking up.
        # Sends a new instruction, composed of a single new order
        active_order = Order(
            order_id=self.order_id,
            drop_off_at=self.drop_off_at,
            pick_up_at=self.pick_up_at,
            placement_time=self.placement_time,
            expected_drop_off_time=self.expected_drop_off_time,
            preparation_time=self.preparation_time,
            ready_time=self.ready_time,
            courier_id=self.courier_id,
            user=User(env=env))
        dispatcher.assigned_orders[active_order.order_id] = active_order
        new_order = Order(order_id=17,
                          drop_off_at=Location(lat=4.694627, lng=-74.038886),
                          pick_up_at=self.pick_up_at,
                          placement_time=self.placement_time,
                          expected_drop_off_time=self.expected_drop_off_time,
                          preparation_time=self.preparation_time,
                          ready_time=self.ready_time,
                          user=User(env=env))
        dispatcher.unassigned_orders[new_order.order_id] = new_order
        courier = Courier(
            acceptance_policy=self.acceptance_policy,
            dispatcher=dispatcher,
            env=env,
            movement_evaluation_policy=self.movement_evaluation_policy,
            movement_policy=self.movement_policy,
            courier_id=self.courier_id,
            vehicle=self.vehicle,
            location=active_order.pick_up_at,
            acceptance_rate=0.01,
            active_route=Route(orders={self.order_id: active_order},
                               stops=[
                                   Stop(location=self.pick_up_at,
                                        position=0,
                                        orders={self.order_id: active_order},
                                        type=StopType.PICK_UP,
                                        visited=False),
                                   Stop(location=self.drop_off_at,
                                        position=1,
                                        orders={self.order_id: active_order},
                                        type=StopType.DROP_OFF,
                                        visited=False)
                               ]),
            on_time=on_time,
            off_time=off_time)

        instruction = Stop(location=new_order.drop_off_at,
                           position=1,
                           orders={new_order.order_id: new_order},
                           type=StopType.DROP_OFF,
                           visited=False)
        notification = Notification(courier=courier, instruction=instruction)
        courier.state.interrupt()
        courier.active_stop = courier.active_route.stops[0]
        courier.state = env.process(
            courier._picking_up_state(
                orders={active_order.order_id: active_order}))
        env.process(courier.notification_event(notification))
        env.run(until=hour_to_sec(14))

        # Asserts:
        # - the courier didn't fulfill the new order,
        # - fulfilled the active order and
        # - is at a different start location.
        self.assertIsNone(new_order.pick_up_time)
        self.assertIsNone(new_order.drop_off_time)
        self.assertIsNone(new_order.courier_id)
        self.assertIn(courier.courier_id, new_order.rejected_by)
        self.assertIn(new_order.order_id, courier.rejected_orders)
        self.assertEqual(new_order.state, 'unassigned')

        self.assertIsNotNone(active_order.pick_up_time)
        self.assertIsNotNone(active_order.drop_off_time)
        self.assertEqual(active_order.courier_id, courier.courier_id)
        self.assertTrue(active_order.pick_up_time < active_order.drop_off_time)
        self.assertEqual(active_order.state, 'dropped_off')

        self.assertIsNone(courier.active_route)
        self.assertIsNone(courier.active_stop)
        self.assertNotEqual(courier.location, self.start_location)
        self.assertEqual(dispatcher.fulfilled_orders,
                         {active_order.order_id: active_order})
        self.assertEqual(dispatcher.unassigned_orders,
                         {new_order.order_id: new_order})
        self.assertEqual(courier.condition, 'idle')
        self.assertIn(courier.courier_id, dispatcher.idle_couriers.keys())

    @patch('settings.settings.COURIER_MOVEMENT_PROBABILITY', 0.01)
    def test_log_off(self):
        """Test to evaluate the scheduling of the courier logging off works correctly"""

        # Constants
        random.seed(888)
        on_time = time(8, 0, 0)
        off_time = time(14, 0, 0)
        initial_time = hour_to_sec(8)

        # Verifies for two test cases the scheduling of the courier logging off works correctly.
        # In one case, the courier log off event doesn't yet happen. In the other test, it does

        # Test 1: the courier achieves the log off after a given time
        env = Environment(initial_time=initial_time)
        dispatcher = Dispatcher(env=env, matching_policy=DummyMatchingPolicy())
        courier = Courier(env=env,
                          dispatcher=dispatcher,
                          courier_id=84,
                          on_time=on_time,
                          off_time=off_time)
        env.run(until=initial_time + hour_to_sec(10))
        self.assertEqual(dispatcher.logged_off_couriers,
                         {courier.courier_id: courier})
        self.assertEqual(dispatcher.idle_couriers, {})
        self.assertEqual(courier.condition, 'logged_off')

        # Test 2: the courier doesn't achieve the log off because the simulation ends before
        env = Environment(initial_time=initial_time)
        dispatcher = Dispatcher(env=env, matching_policy=DummyMatchingPolicy())
        courier = Courier(env=env,
                          dispatcher=dispatcher,
                          courier_id=84,
                          on_time=on_time,
                          off_time=off_time)
        env.run(until=initial_time + hour_to_sec(2))
        self.assertEqual(dispatcher.idle_couriers,
                         {courier.courier_id: courier})
        self.assertEqual(dispatcher.logged_off_couriers, {})
        self.assertEqual(courier.condition, 'idle')

    @patch('settings.settings.COURIER_EARNINGS_PER_ORDER', 3)
    @patch('settings.settings.COURIER_EARNINGS_PER_HOUR', 8)
    @patch('settings.settings.COURIER_MOVEMENT_PROBABILITY', 0.01)
    def test_calculate_earnings(self):
        """Test to verify the mechanics of calculating the shift's earnings"""

        # Constants
        random.seed(523)
        on_time = time(0, 0, 0)
        off_time = time(2, 0, 0)

        # Services
        env = Environment()

        # Creates a two hour - shift courier
        courier = Courier(env=env, on_time=on_time, off_time=off_time)

        # Verifies for two scenarios how the earnings are calculated.
        # In the first test, raw earnings from orders are chosen.
        # In the second test, the hourly earnings rate is chosen.

        # Test 1. Creates courier earnings to select the raw earnings from orders.
        # Asserts that these earnings are selected over the hourly earnings rate
        courier.fulfilled_orders = [Order()] * 7
        courier.earnings = courier._calculate_earnings()
        self.assertEqual(
            courier.earnings,
            len(courier.fulfilled_orders) *
            settings.COURIER_EARNINGS_PER_ORDER)

        # Test 2. Creates courier earnings to select the hourly earnings rate.
        # Asserts that these earnings are selected over the order earnings
        courier.fulfilled_orders = [Order()] * 2
        courier.earnings = courier._calculate_earnings()
        self.assertEqual(
            courier.earnings,
            sec_to_hour(time_diff(courier.off_time, courier.on_time)) *
            settings.COURIER_EARNINGS_PER_HOUR)

    @patch('services.osrm_service.OSRMService.get_route',
           side_effect=mocked_get_route)
    @patch('settings.settings.COURIER_MOVEMENT_PROBABILITY', 0.1)
    def test_notify_prepositioning_event_accept_idle(self, osrm):
        """Test to evaluate how a courier handles a prepositioning notification while being idle and accepts it"""

        # Constants
        random.seed(348)
        initial_time = hour_to_sec(17)
        time_delta = min_to_sec(10)
        on_time = time(17, 0, 0)
        off_time = time(17, 30, 0)

        # Services
        env = Environment(initial_time=initial_time)
        dispatcher = Dispatcher(env=env, matching_policy=DummyMatchingPolicy())

        # Creates a courier with high acceptance rate and immediately send a prepositioning notification
        courier = Courier(
            acceptance_policy=self.acceptance_policy,
            dispatcher=dispatcher,
            env=env,
            movement_evaluation_policy=self.movement_evaluation_policy,
            movement_policy=self.movement_policy,
            courier_id=self.courier_id,
            vehicle=self.vehicle,
            location=self.start_location,
            acceptance_rate=0.99,
            on_time=on_time,
            off_time=off_time)

        instruction = Route(orders=None,
                            stops=[
                                Stop(location=self.pick_up_at,
                                     position=0,
                                     orders=None,
                                     type=StopType.PREPOSITION,
                                     visited=False)
                            ])
        notification = Notification(courier=courier,
                                    instruction=instruction,
                                    type=NotificationType.PREPOSITIONING)
        env.process(courier.notification_event(notification))
        env.run(until=initial_time + time_delta)

        # Asserts that the courier fulfilled the route and is at a different start location
        self.assertIsNone(courier.active_route)
        self.assertIsNone(courier.active_stop)
        self.assertEqual(dispatcher.fulfilled_orders, {})
        self.assertNotEqual(courier.location, self.start_location)
        self.assertEqual(courier.condition, 'idle')
        self.assertIn(courier.courier_id, dispatcher.idle_couriers.keys())

    @patch('services.osrm_service.OSRMService.get_route',
           side_effect=mocked_get_route)
    @patch('settings.settings.COURIER_MOVEMENT_PROBABILITY', 0.01)
    def test_notify_prepositioning_event_reject_idle(self, osrm):
        """Test to evaluate how a courier handles a prepositioning notification while being idle and rejects it"""

        # Constants
        random.seed(672)
        on_time = time(6, 0, 0)
        off_time = time(8, 0, 0)

        # Services
        env = Environment(initial_time=hour_to_sec(6))
        dispatcher = Dispatcher(env=env, matching_policy=DummyMatchingPolicy())

        # Creates a courier with low acceptance rate and immediately send a prepositioning notification
        courier = Courier(
            acceptance_policy=self.acceptance_policy,
            dispatcher=dispatcher,
            env=env,
            movement_evaluation_policy=self.movement_evaluation_policy,
            movement_policy=self.movement_policy,
            courier_id=self.courier_id,
            vehicle=self.vehicle,
            location=self.start_location,
            acceptance_rate=0.01,
            on_time=on_time,
            off_time=off_time)
        instruction = Route(orders=None,
                            stops=[
                                Stop(location=self.pick_up_at,
                                     position=0,
                                     orders=None,
                                     type=StopType.PREPOSITION,
                                     visited=False)
                            ])
        notification = Notification(courier=courier,
                                    instruction=instruction,
                                    type=NotificationType.PREPOSITIONING)
        env.process(courier.notification_event(notification))
        env.run(until=hour_to_sec(7))

        # Asserts that the courier didn't fulfill the route
        self.assertIsNone(courier.active_route)
        self.assertIsNone(courier.active_stop)
        self.assertEqual(courier.location, self.start_location)
        self.assertEqual(courier.condition, 'idle')
        self.assertIn(courier.courier_id, dispatcher.idle_couriers.keys())

    @patch('services.osrm_service.OSRMService.get_route',
           side_effect=mocked_get_route)
    @patch('settings.settings.COURIER_MOVEMENT_PROBABILITY', 0.01)
    def test_pick_up_waiting_time(self, osrm):
        """Test to verify the mechanics of the waiting time are correctly designed"""

        # Constants
        random.seed(290)
        on_time = time(6, 0, 0)
        off_time = time(8, 0, 0)

        # Services
        env = Environment(initial_time=hour_to_sec(6))
        dispatcher = Dispatcher(env=env, matching_policy=DummyMatchingPolicy())

        # Creates a courier and sets it to pick up stuff
        courier = Courier(
            acceptance_policy=self.acceptance_policy,
            dispatcher=dispatcher,
            env=env,
            movement_evaluation_policy=self.movement_evaluation_policy,
            movement_policy=self.movement_policy,
            courier_id=self.courier_id,
            vehicle=self.vehicle,
            location=self.start_location,
            acceptance_rate=0.01,
            on_time=on_time,
            off_time=off_time)
        order = Order(ready_time=time(6, 15, 0), order_id=23)
        stop = Stop(orders={order.order_id: order}, type=StopType.PICK_UP)
        env.process(courier._execute_stop(stop))
        dispatcher.state.interrupt()

        # Run until there are no more events and assert the courier experienced waiting time.
        env.run(until=hour_to_sec(7))
        self.assertTrue(order.pick_up_time >= time(
            6, int(order.ready_time.minute + order.pick_up_service_time / 60)))

        # For another test, if the order's ready time has expired, the courier doesn't experience waiting time
        env = Environment(initial_time=hour_to_sec(6))
        dispatcher = Dispatcher(env=env, matching_policy=DummyMatchingPolicy())
        courier = Courier(
            acceptance_policy=self.acceptance_policy,
            dispatcher=dispatcher,
            env=env,
            movement_evaluation_policy=self.movement_evaluation_policy,
            movement_policy=self.movement_policy,
            courier_id=self.courier_id,
            vehicle=self.vehicle,
            location=self.start_location,
            acceptance_rate=0.01,
            on_time=on_time,
            off_time=off_time)
        order = Order(ready_time=time(4, 0, 0), order_id=23)
        stop = Stop(orders={order.order_id: order}, type=StopType.PICK_UP)
        env.process(courier._execute_stop(stop))
        dispatcher.state.interrupt()
        env.run(until=hour_to_sec(7))
        self.assertTrue(
            time(order.pick_up_time.hour, order.pick_up_time.minute) <= time(
                6, int(order.pick_up_service_time / 60)))
Example #14
0
    def test_notify_event_reject_picking_up(self, osrm):
        """Test to evaluate how a courier handles a notification while picking up and rejects it"""

        # Constants
        random.seed(4747474)
        on_time = time(12, 0, 0)
        off_time = time(15, 0, 0)

        # Services
        env = Environment(initial_time=hour_to_sec(12) + min_to_sec(12))
        dispatcher = Dispatcher(env=env, matching_policy=DummyMatchingPolicy())

        # Creates a courier with low acceptance rate, an active route and in state of picking up.
        # Sends a new instruction, composed of a single new order
        active_order = Order(
            order_id=self.order_id,
            drop_off_at=self.drop_off_at,
            pick_up_at=self.pick_up_at,
            placement_time=self.placement_time,
            expected_drop_off_time=self.expected_drop_off_time,
            preparation_time=self.preparation_time,
            ready_time=self.ready_time,
            courier_id=self.courier_id,
            user=User(env=env))
        dispatcher.assigned_orders[active_order.order_id] = active_order
        new_order = Order(order_id=17,
                          drop_off_at=Location(lat=4.694627, lng=-74.038886),
                          pick_up_at=self.pick_up_at,
                          placement_time=self.placement_time,
                          expected_drop_off_time=self.expected_drop_off_time,
                          preparation_time=self.preparation_time,
                          ready_time=self.ready_time,
                          user=User(env=env))
        dispatcher.unassigned_orders[new_order.order_id] = new_order
        courier = Courier(
            acceptance_policy=self.acceptance_policy,
            dispatcher=dispatcher,
            env=env,
            movement_evaluation_policy=self.movement_evaluation_policy,
            movement_policy=self.movement_policy,
            courier_id=self.courier_id,
            vehicle=self.vehicle,
            location=active_order.pick_up_at,
            acceptance_rate=0.01,
            active_route=Route(orders={self.order_id: active_order},
                               stops=[
                                   Stop(location=self.pick_up_at,
                                        position=0,
                                        orders={self.order_id: active_order},
                                        type=StopType.PICK_UP,
                                        visited=False),
                                   Stop(location=self.drop_off_at,
                                        position=1,
                                        orders={self.order_id: active_order},
                                        type=StopType.DROP_OFF,
                                        visited=False)
                               ]),
            on_time=on_time,
            off_time=off_time)

        instruction = Stop(location=new_order.drop_off_at,
                           position=1,
                           orders={new_order.order_id: new_order},
                           type=StopType.DROP_OFF,
                           visited=False)
        notification = Notification(courier=courier, instruction=instruction)
        courier.state.interrupt()
        courier.active_stop = courier.active_route.stops[0]
        courier.state = env.process(
            courier._picking_up_state(
                orders={active_order.order_id: active_order}))
        env.process(courier.notification_event(notification))
        env.run(until=hour_to_sec(14))

        # Asserts:
        # - the courier didn't fulfill the new order,
        # - fulfilled the active order and
        # - is at a different start location.
        self.assertIsNone(new_order.pick_up_time)
        self.assertIsNone(new_order.drop_off_time)
        self.assertIsNone(new_order.courier_id)
        self.assertIn(courier.courier_id, new_order.rejected_by)
        self.assertIn(new_order.order_id, courier.rejected_orders)
        self.assertEqual(new_order.state, 'unassigned')

        self.assertIsNotNone(active_order.pick_up_time)
        self.assertIsNotNone(active_order.drop_off_time)
        self.assertEqual(active_order.courier_id, courier.courier_id)
        self.assertTrue(active_order.pick_up_time < active_order.drop_off_time)
        self.assertEqual(active_order.state, 'dropped_off')

        self.assertIsNone(courier.active_route)
        self.assertIsNone(courier.active_stop)
        self.assertNotEqual(courier.location, self.start_location)
        self.assertEqual(dispatcher.fulfilled_orders,
                         {active_order.order_id: active_order})
        self.assertEqual(dispatcher.unassigned_orders,
                         {new_order.order_id: new_order})
        self.assertEqual(courier.condition, 'idle')
        self.assertIn(courier.courier_id, dispatcher.idle_couriers.keys())
Example #15
0
    # Simulation Constants
    # --- time =  Simulate from this time on
    'SIMULATE_FROM': time(0, 0, 0),
    # --- time =  Simulate until this time
    'SIMULATE_UNTIL': time(10, 0, 0),
    # --- time =  Create new users to submit orders from this time
    'CREATE_USERS_FROM': time(9, 0, 0),
    # --- time =  Create new users to submit orders until this time
    'CREATE_USERS_UNTIL': time(9, 5, 0),
    # --- time =  Create new couriers to log on from this time
    'CREATE_COURIERS_FROM': time(0, 0, 0),
    # --- time = Create new couriers to log on until this time
    'CREATE_COURIERS_UNTIL': time(0, 5, 0),
    # --- float = Warm up time [sec] to achieve steady state simulation
    'WARM_UP_TIME': hour_to_sec(3) + min_to_sec(0),

    # Simulation Policies - Dispatcher
    # --- str = Policy for canceling orders. Options: ['static']
    'DISPATCHER_CANCELLATION_POLICY': 'static',
    # --- str = Policy for buffering orders. Options: ['rolling_horizon']
    'DISPATCHER_BUFFERING_POLICY': 'rolling_horizon',
    # --- str = Policy for deciding when to evaluate prepositioning. Options: ['fixed']
    'DISPATCHER_PREPOSITIONING_EVALUATION_POLICY': 'fixed',
    # --- str = Policy for executing prepositioning. Options: ['naive']
    'DISPATCHER_PREPOSITIONING_POLICY': 'naive',
    # --- str = Policy for matching. Options: ['greedy', 'mdrp', 'mdrp_graph', 'mdrp_graph_prospects', 'modified_mdrp']
    'DISPATCHER_MATCHING_POLICY': 'mdrp',

    # Simulation Policies - Courier
    # --- str = Policy for accepting a notification. Options: ['uniform', 'absolute']
Example #16
0
class TestsDispatcher(unittest.TestCase):
    """Tests for the Dispatcher actor class"""

    # Order properties to be reused
    order_id = 0
    pick_up_at = Location(lat=4.689697, lng=-74.055495)
    drop_off_at = Location(lat=4.690296, lng=-74.043929)
    placement_time = time(12, 0, 0)
    expected_drop_off_time = time(12, 40, 0)
    preparation_time = time(12, 1, 0)
    ready_time = time(12, 11, 0)

    # Services to be reused
    cancellation_policy = RandomCancellationPolicy()
    dispatcher_cancellation_policy = StaticCancellationPolicy()

    @staticmethod
    def assign_courier(_user: User, _env: Environment, _dispatcher: Dispatcher):
        """Dummy method to assign a courier"""

        yield _env.timeout(delay=min_to_sec(8))
        _user.order.courier_id = 999
        del _dispatcher.unassigned_orders[_user.order.order_id]
        _dispatcher.assigned_orders[_user.order.order_id] = _user.order

    @patch('settings.settings.USER_CANCELLATION_PROBABILITY', 0)
    @patch('settings.settings.USER_WAIT_TO_CANCEL', min_to_sec(10))
    @patch('settings.settings.DISPATCHER_WAIT_TO_CANCEL', min_to_sec(15))
    def test_cancel_order_event(self):
        """Test to verify how the dispatcher cancels an order after certain time"""

        random.seed(741)

        # Services
        env = Environment(initial_time=hour_to_sec(12))
        dispatcher = Dispatcher(
            env=env,
            cancellation_policy=self.dispatcher_cancellation_policy,
            matching_policy=DummyMatchingPolicy()
        )

        # Create a user and have it submit an order immediately, avoiding user cancellation
        user = User(cancellation_policy=self.cancellation_policy, dispatcher=dispatcher, env=env)
        user.submit_order_event(
            order_id=self.order_id,
            pick_up_at=self.pick_up_at,
            drop_off_at=self.drop_off_at,
            placement_time=self.placement_time,
            expected_drop_off_time=self.expected_drop_off_time,
            preparation_time=self.preparation_time,
            ready_time=self.ready_time
        )
        env.run(until=hour_to_sec(14))

        # Verify order is canceled by the dispatcher
        self.assertTrue(user.order)
        self.assertIsNone(user.order.courier_id)
        self.assertIsNotNone(user.order.cancellation_time)
        self.assertEqual(dispatcher.unassigned_orders, {})
        self.assertIn(self.order_id, dispatcher.canceled_orders.keys())
        self.assertEqual(
            user.order.cancellation_time,
            (
                    datetime.combine(date.today(), self.preparation_time) +
                    timedelta(seconds=settings.DISPATCHER_WAIT_TO_CANCEL)
            ).time()
        )
        self.assertEqual(user.condition, 'canceled')

    @patch('settings.settings.USER_CANCELLATION_PROBABILITY', 0)
    @patch('settings.settings.USER_WAIT_TO_CANCEL', min_to_sec(44))
    @patch('settings.settings.DISPATCHER_WAIT_TO_CANCEL', min_to_sec(55))
    def test_order_not_canceled(self):
        """
        Test to verify that the dispatcher doesn't cancel an order if it has a courier assigned.
        The user doesn't see a courier but decides not to cancel.
        """

        random.seed(192)

        # Services
        env = Environment(initial_time=hour_to_sec(12))
        dispatcher = Dispatcher(
            env=env,
            cancellation_policy=self.dispatcher_cancellation_policy,
            matching_policy=DummyMatchingPolicy()
        )

        # Create a user, have it submit an order immediately and after some minutes, assign a courier.
        # Courier is assigned after user cancellation time has expired
        user = User(cancellation_policy=self.cancellation_policy, dispatcher=dispatcher, env=env)
        user.submit_order_event(
            order_id=self.order_id,
            pick_up_at=self.pick_up_at,
            drop_off_at=self.drop_off_at,
            placement_time=self.placement_time,
            expected_drop_off_time=self.expected_drop_off_time,
            preparation_time=self.preparation_time,
            ready_time=self.ready_time
        )
        env.process(self.assign_courier(user, env, dispatcher))
        env.run(until=hour_to_sec(13))

        # Verify order is created but not canceled because a courier was assigned
        self.assertTrue(user.order)
        self.assertIsNotNone(user.order.courier_id)
        self.assertIsNone(user.order.cancellation_time)
        self.assertEqual(dispatcher.assigned_orders, {self.order_id: user.order})
        self.assertEqual(dispatcher.unassigned_orders, {})
        self.assertEqual(user.condition, 'waiting')

    def test_order_submitted_event(self):
        """Test to verify the mechanics of the order submitted event"""

        # Constants
        initial_time = hour_to_sec(15)
        placement_time = time(15, 0, 0)
        preparation_time = time(15, 1, 0)
        ready_time = time(15, 15, 0)

        # Services
        env = Environment(initial_time=initial_time)
        dispatcher = Dispatcher(env=env, matching_policy=DummyMatchingPolicy())

        # Creates an order and submits it to the dispatcher
        order = Order(order_id=32, placement_time=placement_time)
        dispatcher.order_submitted_event(order, preparation_time, ready_time)
        env.run(until=initial_time + 121)

        # Verify order properties are set and it is correctly allocated
        self.assertEqual(order.preparation_time, preparation_time)
        self.assertEqual(order.ready_time, ready_time)
        self.assertIn(order.order_id, dispatcher.unassigned_orders.keys())

    def test_orders_picked_up_event(self):
        """Test to verify the mechanics of orders being picked up"""

        # Constants
        initial_time = hour_to_sec(14)

        # Services
        env = Environment(initial_time=initial_time)
        dispatcher = Dispatcher(env=env, matching_policy=DummyMatchingPolicy())

        # Creates an order and sends the picked up event
        order = Order(order_id=45)
        dispatcher.orders_picked_up_event(orders={order.order_id: order})
        env.run(until=initial_time + hour_to_sec(1))

        # Verify order properties are modified
        self.assertEqual(order.state, 'picked_up')
        self.assertEqual(order.pick_up_time, sec_to_time(initial_time))

    @patch('settings.settings.COURIER_MOVEMENT_PROBABILITY', 0.01)
    def test_orders_dropped_off_event(self):
        """Test to verify the mechanics of orders being dropped off"""

        # Constants
        initial_time = hour_to_sec(14)
        on_time = time(14, 0, 0)
        off_time = time(16, 0, 0)

        # Services
        env = Environment(initial_time=initial_time)
        dispatcher = Dispatcher(env=env, matching_policy=DummyMatchingPolicy())

        # Creates an order and sends the picked up event
        order = Order(order_id=45, user=User(env=env))
        dispatcher.assigned_orders[order.order_id] = order
        courier = Courier(on_time=on_time, off_time=off_time)
        dispatcher.orders_dropped_off_event(orders={order.order_id: order}, courier=courier)
        env.run(until=initial_time + hour_to_sec(1))

        # Verify order properties are modified and it is allocated correctly
        self.assertEqual(order.state, 'dropped_off')
        self.assertEqual(order.drop_off_time, sec_to_time(initial_time))
        self.assertIn(order.order_id, dispatcher.fulfilled_orders.keys())
        self.assertEqual(dispatcher.assigned_orders, {})
        self.assertIn(order.order_id, courier.fulfilled_orders)

    def test_notification_accepted_event(self):
        """Test to verify the mechanics of a notification being accepted by a courier"""

        # Constants
        initial_time = hour_to_sec(14)
        on_time = time(14, 0, 0)
        off_time = time(15, 0, 0)

        # Services
        env = Environment(initial_time=initial_time)
        dispatcher = Dispatcher(env=env, matching_policy=DummyMatchingPolicy())

        # Creates an instruction with an order, a courier and sends the accepted event
        order = Order(order_id=45)
        instruction = Route(
            stops=[
                Stop(orders={order.order_id: order}, position=0),
                Stop(orders={order.order_id: order}, position=1)
            ],
            orders={order.order_id: order}
        )
        dispatcher.unassigned_orders[order.order_id] = order
        courier = Courier(dispatcher=dispatcher, env=env, courier_id=89, on_time=on_time, off_time=off_time)
        courier.condition = 'idle'
        notification = Notification(
            courier=courier,
            instruction=instruction
        )
        dispatcher.notification_accepted_event(notification=notification, courier=courier)
        env.run(until=initial_time + min_to_sec(10))

        # Verify order and courier properties are modified and it is allocated correctly
        self.assertEqual(order.state, 'in_progress')
        self.assertEqual(order.acceptance_time, sec_to_time(initial_time))
        self.assertEqual(order.courier_id, courier.courier_id)
        self.assertIn(order.order_id, dispatcher.assigned_orders.keys())
        self.assertIsNotNone(courier.active_route)
        self.assertEqual(courier.active_route, instruction)
        self.assertEqual(dispatcher.unassigned_orders, {})

    @patch('settings.settings.COURIER_MOVEMENT_PROBABILITY', 0.01)
    def test_notification_rejected_event(self):
        """Test to verify the mechanics of a notification being rejected by a courier"""

        # Constants
        initial_time = hour_to_sec(14)
        on_time = time(14, 0, 0)
        off_time = time(15, 0, 0)

        # Services
        env = Environment(initial_time=initial_time)
        dispatcher = Dispatcher(env=env, matching_policy=DummyMatchingPolicy())

        # Creates an instruction with an order, a courier and sends the rejected event
        order = Order(order_id=45)
        instruction = Route(
            stops=[
                Stop(orders={order.order_id: order}, position=0),
                Stop(orders={order.order_id: order}, position=1)
            ],
            orders={order.order_id: order}
        )
        dispatcher.unassigned_orders[order.order_id] = order
        courier = Courier(dispatcher=dispatcher, env=env, courier_id=89, on_time=on_time, off_time=off_time)
        notification = Notification(
            courier=courier,
            instruction=instruction
        )
        dispatcher.notification_rejected_event(notification=notification, courier=courier)
        env.run(until=initial_time + min_to_sec(30))

        # Verify order and courier properties are modified and it is allocated correctly
        self.assertEqual(order.state, 'unassigned')
        self.assertIsNone(order.acceptance_time)
        self.assertIsNone(order.courier_id)
        self.assertIn(order.order_id, dispatcher.unassigned_orders.keys())
        self.assertIsNone(courier.active_route)
        self.assertIn(courier.courier_id, order.rejected_by)
        self.assertIn(order.order_id, courier.rejected_orders)

    @patch('settings.settings.COURIER_MOVEMENT_PROBABILITY', 0.01)
    def test_courier_idle_event(self, *args):
        """Test to verifiy the mechanics of how a courier is set to idle by the dispatcher"""

        # Constants
        initial_time = hour_to_sec(14)
        courier_id = 85
        time_delta = min_to_sec(10)
        random.seed(26)
        on_time = time(14, 0, 0)
        off_time = time(15, 0, 0)

        # Verifies 3 test cases: when the courier is busy, available or idle.
        # For each test case, assert the courier starts in a set and ends up in the idle set

        # Test 1: courier is busy
        env = Environment(initial_time=initial_time)
        dispatcher = Dispatcher(env=env, matching_policy=DummyMatchingPolicy())
        courier = Courier(dispatcher=dispatcher, env=env, courier_id=courier_id, on_time=on_time, off_time=off_time)
        dispatcher.moving_couriers = {courier.courier_id: courier}
        dispatcher.courier_idle_event(courier)
        env.run(until=initial_time + time_delta)
        self.assertIn(courier.courier_id, dispatcher.idle_couriers.keys())
        self.assertEqual(dispatcher.moving_couriers, {})
        self.assertEqual(dispatcher.picking_up_couriers, {})

        # Test 2: courier is available
        env = Environment(initial_time=initial_time)
        dispatcher = Dispatcher(env=env, matching_policy=DummyMatchingPolicy())
        courier = Courier(dispatcher=dispatcher, env=env, courier_id=courier_id, on_time=on_time, off_time=off_time)
        dispatcher.picking_up_couriers = {courier.courier_id: courier}
        dispatcher.courier_idle_event(courier)
        env.run(until=initial_time + time_delta)
        self.assertIn(courier.courier_id, dispatcher.idle_couriers.keys())
        self.assertEqual(dispatcher.moving_couriers, {})
        self.assertEqual(dispatcher.picking_up_couriers, {})

        # Test 3: courier is idle
        env = Environment(initial_time=initial_time)
        dispatcher = Dispatcher(env=env, matching_policy=DummyMatchingPolicy())
        courier = Courier(dispatcher=dispatcher, env=env, courier_id=courier_id, on_time=on_time, off_time=off_time)
        dispatcher.idle_couriers = {courier.courier_id: courier}
        dispatcher.courier_idle_event(courier)
        env.run(until=initial_time + time_delta)
        self.assertIn(courier.courier_id, dispatcher.idle_couriers.keys())
        self.assertEqual(dispatcher.moving_couriers, {})
        self.assertEqual(dispatcher.picking_up_couriers, {})

    def test_courier_available_event(self):
        """Test to verify the mechanics of how the dispatcher sets a courier to available"""

        # Constants
        initial_time = hour_to_sec(14)
        on_time = time(14, 0, 0)
        off_time = time(15, 0, 0)
        service_time = min_to_sec(6)

        # Services
        env = Environment(initial_time=initial_time)
        dispatcher = Dispatcher(env=env, matching_policy=DummyMatchingPolicy())

        # Creates a courier and sets it to the picking state
        courier = Courier(dispatcher=dispatcher, env=env, courier_id=32, on_time=on_time, off_time=off_time)
        dispatcher.idle_couriers = {courier.courier_id: courier}
        env.process(courier._picking_up_state(
            orders={
                21: Order(drop_off_service_time=service_time, ready_time=time(14, 20, 0))
            }
        ))
        env.run(until=initial_time + min_to_sec(10))

        # Verify courier properties are modified and it is allocated correctly
        self.assertEqual(courier.condition, 'picking_up')
        self.assertIn(courier.courier_id, dispatcher.picking_up_couriers.keys())
        self.assertEqual(dispatcher.idle_couriers, {})

    @patch('services.osrm_service.OSRMService.get_route', side_effect=mocked_get_route)
    def test_courier_busy_event(self, *args):
        """Test to verify the mechanics of how the dispatcher sets a courier to busy"""

        # Constants
        initial_time = hour_to_sec(14)
        courier_id = 14
        time_delta = min_to_sec(10)
        on_time = time(14, 0, 0)
        off_time = time(15, 0, 0)
        service_time = min_to_sec(7)

        # Verifies 2 test cases for how the courier transitions to being busy
        # For each test case, assert the courier starts in a set and ends up in the busy set

        # Test 1: courier starts dropping off state
        env = Environment(initial_time=initial_time)
        dispatcher = Dispatcher(env=env, matching_policy=DummyMatchingPolicy())
        courier = Courier(dispatcher=dispatcher, env=env, courier_id=courier_id, on_time=on_time, off_time=off_time)
        env.process(courier._dropping_off_state(
            orders={
                21: Order(drop_off_service_time=service_time, ready_time=time(12, 20, 0))
            }
        ))
        env.run(until=initial_time + time_delta)
        self.assertEqual(courier.condition, 'dropping_off')
        self.assertEqual(dispatcher.dropping_off_couriers, {courier.courier_id: courier})
        self.assertEqual(dispatcher.idle_couriers, {})

        # Test 2: courier start moving state
        env = Environment(initial_time=initial_time)
        dispatcher = Dispatcher(env=env, matching_policy=DummyMatchingPolicy())
        courier = Courier(
            dispatcher=dispatcher,
            env=env,
            courier_id=courier_id,
            location=Location(lat=4.690296, lng=-74.043929),
            on_time=on_time,
            off_time=off_time
        )
        env.process(courier._moving_state(destination=Location(lat=4.689697, lng=-74.055495)))
        env.run(until=initial_time + time_delta)
        self.assertEqual(courier.condition, 'moving')
        self.assertEqual(dispatcher.moving_couriers, {courier.courier_id: courier})
        self.assertEqual(dispatcher.idle_couriers, {})

    @patch('settings.settings.DISPATCHER_WAIT_TO_CANCEL', min_to_sec(55))
    @patch('settings.settings.USER_WAIT_TO_CANCEL', min_to_sec(44))
    def test_buffer_event(self):
        """Test to verify how the mechanics of the dispatcher buffering orders work"""

        # Constants
        initial_time = hour_to_sec(16)
        placement_time = time(16, 0, 0)
        time_delta = min_to_sec(10)

        # Verifies two test cases for how the dispatcher buffers orders
        # For each test, assert the correct number of orders are buffered

        # Test 1: schedules the submission of three orders and assert that only two are buffered
        env = Environment(initial_time=initial_time)
        dispatcher = Dispatcher(
            env=env,
            buffering_policy=RollingBufferingPolicy(),
            matching_policy=DummyMatchingPolicy()
        )
        order_1 = Order(order_id=1, placement_time=placement_time)
        order_2 = Order(order_id=2, placement_time=placement_time)
        order_3 = Order(order_id=3, placement_time=placement_time)
        dispatcher.order_submitted_event(order_1, preparation_time=time(16, 0, 27), ready_time=time(16, 10, 0))
        dispatcher.order_submitted_event(order_2, preparation_time=time(16, 0, 43), ready_time=time(16, 10, 0))
        dispatcher.order_submitted_event(order_3, preparation_time=time(18, 0, 0), ready_time=time(18, 10, 0))

        env.run(until=initial_time + time_delta)
        self.assertEqual(len(dispatcher.unassigned_orders), 2)

        # Test 2: schedules the submission of three orders and assert that all three orders are buffered
        env = Environment(initial_time=initial_time)
        dispatcher = Dispatcher(
            env=env,
            buffering_policy=RollingBufferingPolicy(),
            matching_policy=DummyMatchingPolicy()
        )
        order_1 = Order(order_id=1, placement_time=placement_time)
        order_2 = Order(order_id=2, placement_time=placement_time)
        order_3 = Order(order_id=3, placement_time=placement_time)
        dispatcher.order_submitted_event(order_1, preparation_time=time(16, 0, 27), ready_time=time(16, 10, 0))
        dispatcher.order_submitted_event(order_2, preparation_time=time(16, 0, 43), ready_time=time(16, 10, 0))
        dispatcher.order_submitted_event(order_3, preparation_time=time(16, 4, 1), ready_time=time(16, 14, 0))
        env.run(until=initial_time + time_delta)
        self.assertEqual(len(dispatcher.unassigned_orders), 3)

    @patch('settings.settings.COURIER_MOVEMENT_PROBABILITY', 0.01)
    def test_courier_log_off(self):
        """Test to verify how the dispatcher handles a courier logging off"""

        # Constants
        random.seed(12)
        initial_time = hour_to_sec(12)
        time_delta = hour_to_sec(2)
        service_time = min_to_sec(4)

        # Tests 3 cases: when the courier is idle, busy or available.
        # For each test, assert that the courier ends up being logged off

        # Test 1: the courier is idle
        env = Environment(initial_time=initial_time)
        dispatcher = Dispatcher(env=env, matching_policy=DummyMatchingPolicy())
        courier = Courier(
            env=env,
            dispatcher=dispatcher,
            courier_id=69,
            on_time=time(12, 0, 0),
            off_time=time(13, 0, 0)
        )
        env.run(until=initial_time + time_delta)
        self.assertEqual(dispatcher.idle_couriers, {})
        self.assertEqual(dispatcher.moving_couriers, {})
        self.assertEqual(dispatcher.dropping_off_couriers, {})
        self.assertEqual(dispatcher.picking_up_couriers, {})
        self.assertEqual(dispatcher.logged_off_couriers, {courier.courier_id: courier})

        # Test 2: the courier is busy
        env = Environment(initial_time=initial_time)
        dispatcher = Dispatcher(env=env, matching_policy=DummyMatchingPolicy())
        courier = Courier(
            env=env,
            dispatcher=dispatcher,
            courier_id=69,
            on_time=time(12, 0, 0),
            off_time=time(13, 0, 0)
        )
        courier.state.interrupt()
        env.process(courier._dropping_off_state(
            orders={21: Order(drop_off_service_time=service_time)}
        ))
        env.run(until=initial_time + time_delta)
        self.assertEqual(dispatcher.idle_couriers, {})
        self.assertEqual(dispatcher.moving_couriers, {})
        self.assertEqual(dispatcher.dropping_off_couriers, {})
        self.assertEqual(dispatcher.picking_up_couriers, {})
        self.assertEqual(dispatcher.logged_off_couriers, {courier.courier_id: courier})

        # Test 3: the courier is available
        env = Environment(initial_time=initial_time)
        dispatcher = Dispatcher(env=env, matching_policy=DummyMatchingPolicy())
        courier = Courier(
            env=env,
            dispatcher=dispatcher,
            courier_id=69,
            on_time=time(12, 0, 0),
            off_time=time(13, 0, 0)
        )
        courier.state.interrupt()
        env.process(courier._picking_up_state(
            orders={
                21: Order(drop_off_service_time=service_time, ready_time=time(12, 20, 0))
            }
        ))
        env.run(until=initial_time + time_delta)
        self.assertEqual(dispatcher.idle_couriers, {})
        self.assertEqual(dispatcher.moving_couriers, {})
        self.assertEqual(dispatcher.dropping_off_couriers, {})
        self.assertEqual(dispatcher.picking_up_couriers, {})
        self.assertEqual(dispatcher.logged_off_couriers, {courier.courier_id: courier})

    def test_prepositioning_notification_accepted_event(self):
        """Test to verify the mechanics of a prepositioning notification being accepted by a courier"""

        # Constants
        initial_time = hour_to_sec(14)
        on_time = time(14, 0, 0)
        off_time = time(15, 0, 0)

        # Services
        env = Environment(initial_time=initial_time)
        dispatcher = Dispatcher(env=env, matching_policy=DummyMatchingPolicy())

        # Creates a prepositioning notification, a courier and sends the accepted event
        instruction = Route(
            stops=[
                Stop(position=0, type=StopType.PREPOSITION),
                Stop(position=1, type=StopType.PREPOSITION)
            ]
        )
        courier = Courier(dispatcher=dispatcher, env=env, courier_id=666, on_time=on_time, off_time=off_time)
        notification = Notification(courier=courier, instruction=instruction, type=NotificationType.PREPOSITIONING)
        dispatcher.notification_accepted_event(notification=notification, courier=courier)
        env.run(until=initial_time + min_to_sec(10))

        # Verify order and courier properties are modified and it is allocated correctly
        self.assertIsNotNone(courier.active_route)
        self.assertEqual(courier.active_route, instruction)

    @patch('settings.settings.COURIER_MOVEMENT_PROBABILITY', 0.01)
    def test_prepositioning_notification_rejected_event(self):
        """Test to verify the mechanics of a prepositioning notification being rejected by a courier"""

        # Constants
        initial_time = hour_to_sec(14)
        on_time = time(14, 0, 0)
        off_time = time(15, 0, 0)

        # Services
        env = Environment(initial_time=initial_time)
        dispatcher = Dispatcher(env=env, matching_policy=DummyMatchingPolicy())

        # Creates a prepositioning notification, a courier and sends the rejected event
        instruction = Route(
            stops=[
                Stop(position=0, type=StopType.PREPOSITION),
                Stop(position=1, type=StopType.PREPOSITION)
            ]
        )
        courier = Courier(dispatcher=dispatcher, env=env, courier_id=981, on_time=on_time, off_time=off_time)
        notification = Notification(courier=courier, instruction=instruction, type=NotificationType.PREPOSITIONING)
        dispatcher.notification_rejected_event(notification=notification, courier=courier)
        env.run(until=initial_time + min_to_sec(30))

        # Verify order and courier properties are modified and it is allocated correctly
        self.assertIsNone(courier.active_route)
Example #17
0
    def test_myopic_matching_policy_execute_mip_matcher(self, osrm):
        """Test to verify how the optimization model is solved with a MIP approach"""

        # Constants
        env_time = hour_to_sec(12) + min_to_sec(20)
        on_time = time(8, 0, 0)
        off_time = time(16, 0, 0)
        random.seed(45)

        # Orders
        order_1 = Order(order_id=1,
                        pick_up_at=Location(lat=4.678759, lng=-74.055729),
                        drop_off_at=Location(lat=4.681694, lng=-74.044811),
                        ready_time=time(12, 30, 0),
                        expected_drop_off_time=time(12, 40, 0),
                        pick_up_service_time=0,
                        drop_off_service_time=0)
        order_2 = Order(order_id=2,
                        pick_up_at=Location(lat=4.678759, lng=-74.055729),
                        drop_off_at=Location(lat=4.695001, lng=-74.040737),
                        ready_time=time(12, 32, 0),
                        expected_drop_off_time=time(12, 42, 0),
                        pick_up_service_time=0,
                        drop_off_service_time=0)
        order_3 = Order(order_id=3,
                        pick_up_at=Location(lat=4.678759, lng=-74.055729),
                        drop_off_at=Location(lat=4.668742, lng=-74.056684),
                        ready_time=time(12, 33, 0),
                        expected_drop_off_time=time(12, 43, 0),
                        pick_up_service_time=0,
                        drop_off_service_time=0)
        order_4 = Order(order_id=4,
                        pick_up_at=Location(lat=4.678759, lng=-74.055729),
                        drop_off_at=Location(lat=4.661441, lng=-74.056955),
                        ready_time=time(12, 34, 0),
                        expected_drop_off_time=time(12, 44, 0),
                        pick_up_service_time=0,
                        drop_off_service_time=0)

        # Couriers
        courier_1 = Courier(courier_id=1,
                            on_time=on_time,
                            off_time=off_time,
                            condition='idle',
                            location=Location(lat=4.676854, lng=-74.057498))
        courier_2 = Courier(courier_id=2,
                            on_time=on_time,
                            off_time=off_time,
                            condition='idle',
                            location=Location(lat=4.679408, lng=-74.052524))
        courier_3 = Courier(
            courier_id=3,
            on_time=on_time,
            off_time=off_time,
            condition='picking_up',
            location=order_3.pick_up_at,
            active_route=Route(orders={order_3.order_id: order_3},
                               stops=[
                                   Stop(location=order_3.pick_up_at,
                                        orders={order_3.order_id: order_3},
                                        position=0,
                                        type=StopType.PICK_UP),
                                   Stop(location=order_3.drop_off_at,
                                        orders={order_3.order_id: order_3},
                                        position=1,
                                        type=StopType.DROP_OFF)
                               ]),
            active_stop=Stop(location=order_3.pick_up_at,
                             orders={order_3.order_id: order_3},
                             position=0,
                             type=StopType.PICK_UP))

        # Get all the elements from the policy and assert their expected behavior
        policy = MyopicMatchingPolicy(assignment_updates=False,
                                      prospects=False,
                                      notification_filtering=False,
                                      mip_matcher=False)
        routes = policy._generate_routes(
            orders=[order_1, order_2, order_4],
            couriers=[courier_1, courier_2, courier_3],
            env_time=env_time)
        self.assertTrue(routes)
        self.assertEqual(len(routes), 2)
        self.assertEqual(len(routes[0].orders), 2)
        self.assertEqual(len(routes[1].orders), 1)

        prospects = policy._generate_matching_prospects(
            routes=routes,
            couriers=[courier_1, courier_2, courier_3],
            env_time=env_time)
        self.assertTrue(prospects.tolist())
        self.assertEqual(len(prospects), 4),
        self.assertEqual(len(prospects),
                         len(routes) * len([courier_1, courier_2]))

        costs = policy._generate_matching_costs(
            routes=routes,
            couriers=[courier_1, courier_2, courier_3],
            prospects=prospects,
            env_time=env_time)
        self.assertTrue(costs.tolist())
        self.assertEqual(len(prospects), len(costs))
        self.assertEqual(len(costs), 4)
        self.assertNotIn(0., costs)

        problem = MatchingProblemBuilder.build(
            routes=routes,
            couriers=[courier_1, courier_2, courier_3],
            prospects=prospects,
            costs=costs)
        self.assertTrue(problem)
        self.assertEqual(len(prospects), len(problem.prospects))
        self.assertEqual(len(prospects), len(problem.matching_prospects))
        self.assertEqual(len(prospects), len(problem.costs))
        self.assertEqual(routes, problem.routes)
        self.assertEqual(problem.couriers, [courier_1, courier_2, courier_3])
        self.assertNotIn(str(courier_3.courier_id),
                         problem.matching_prospects['i'])

        model_builder = MIPOptimizationModelBuilder(
            sense='max',
            model_constraints=[
                CourierAssignmentConstraint(),
                RouteAssignmentConstraint()
            ],
            optimizer='pulp')
        model = model_builder.build(problem)
        self.assertTrue(model)
        self.assertEqual(len(model.constraints),
                         len(problem.routes) + len([courier_1, courier_2]))
        self.assertEqual(len(model.variable_set),
                         len(problem.matching_prospects) + len(problem.routes))

        solution = model.solve()
        self.assertTrue(solution.tolist())
        self.assertEqual(len(solution),
                         len(problem.matching_prospects) + len(problem.routes))
        self.assertEqual(solution[0:len(problem.prospects)].sum(), 2)
        self.assertEqual(solution.sum(), 2)

        notifications = policy._process_solution(solution, problem, env_time)
        self.assertEqual(len(notifications), len(routes))
        self.assertIsInstance(notifications[0].instruction, Route)
        self.assertIsInstance(notifications[1].instruction, Route)
        self.assertEqual(notifications[0].courier, courier_1)
        self.assertEqual(notifications[1].courier, courier_2)
        self.assertIn(order_1.order_id,
                      notifications[1].instruction.orders.keys())
        self.assertIn(order_4.order_id,
                      notifications[1].instruction.orders.keys())
Example #18
0
    def test_courier_log_off(self):
        """Test to verify how the dispatcher handles a courier logging off"""

        # Constants
        random.seed(12)
        initial_time = hour_to_sec(12)
        time_delta = hour_to_sec(2)
        service_time = min_to_sec(4)

        # Tests 3 cases: when the courier is idle, busy or available.
        # For each test, assert that the courier ends up being logged off

        # Test 1: the courier is idle
        env = Environment(initial_time=initial_time)
        dispatcher = Dispatcher(env=env, matching_policy=DummyMatchingPolicy())
        courier = Courier(
            env=env,
            dispatcher=dispatcher,
            courier_id=69,
            on_time=time(12, 0, 0),
            off_time=time(13, 0, 0)
        )
        env.run(until=initial_time + time_delta)
        self.assertEqual(dispatcher.idle_couriers, {})
        self.assertEqual(dispatcher.moving_couriers, {})
        self.assertEqual(dispatcher.dropping_off_couriers, {})
        self.assertEqual(dispatcher.picking_up_couriers, {})
        self.assertEqual(dispatcher.logged_off_couriers, {courier.courier_id: courier})

        # Test 2: the courier is busy
        env = Environment(initial_time=initial_time)
        dispatcher = Dispatcher(env=env, matching_policy=DummyMatchingPolicy())
        courier = Courier(
            env=env,
            dispatcher=dispatcher,
            courier_id=69,
            on_time=time(12, 0, 0),
            off_time=time(13, 0, 0)
        )
        courier.state.interrupt()
        env.process(courier._dropping_off_state(
            orders={21: Order(drop_off_service_time=service_time)}
        ))
        env.run(until=initial_time + time_delta)
        self.assertEqual(dispatcher.idle_couriers, {})
        self.assertEqual(dispatcher.moving_couriers, {})
        self.assertEqual(dispatcher.dropping_off_couriers, {})
        self.assertEqual(dispatcher.picking_up_couriers, {})
        self.assertEqual(dispatcher.logged_off_couriers, {courier.courier_id: courier})

        # Test 3: the courier is available
        env = Environment(initial_time=initial_time)
        dispatcher = Dispatcher(env=env, matching_policy=DummyMatchingPolicy())
        courier = Courier(
            env=env,
            dispatcher=dispatcher,
            courier_id=69,
            on_time=time(12, 0, 0),
            off_time=time(13, 0, 0)
        )
        courier.state.interrupt()
        env.process(courier._picking_up_state(
            orders={
                21: Order(drop_off_service_time=service_time, ready_time=time(12, 20, 0))
            }
        ))
        env.run(until=initial_time + time_delta)
        self.assertEqual(dispatcher.idle_couriers, {})
        self.assertEqual(dispatcher.moving_couriers, {})
        self.assertEqual(dispatcher.dropping_off_couriers, {})
        self.assertEqual(dispatcher.picking_up_couriers, {})
        self.assertEqual(dispatcher.logged_off_couriers, {courier.courier_id: courier})
Example #19
0
class TestsMyopicMatchingPolicy(unittest.TestCase):
    """Tests for the greedy matching policy class"""
    @patch('settings.settings.DISPATCHER_GEOHASH_PRECISION_GROUPING', 7)
    def test_group_by_geohash(self):
        """Test to verify how the target bundle size is calculated"""

        # Create 3 orders to be grouped into 2 geohashes
        order_1 = Order(order_id=1,
                        pick_up_at=Location(lat=4.678417, lng=-74.054725))
        order_2 = Order(order_id=2,
                        pick_up_at=Location(lat=4.678417, lng=-74.054725))
        order_3 = Order(order_id=3,
                        pick_up_at=Location(lat=4.717045, lng=-74.036359))

        groups = MyopicMatchingPolicy._group_by_geohash(
            orders=[order_1, order_2, order_3])

        # Asserts only 2 groups are created from 3 groups
        self.assertEqual(len(groups), 2)
        self.assertIn(order_1, groups.get('d2g6dgd'))
        self.assertIn(order_2, groups.get('d2g6dgd'))
        self.assertIn(order_3, groups.get('d2g6g68'))

    @patch('settings.settings.DISPATCHER_MYOPIC_READY_TIME_SLACK', 0)
    def test_calculate_target_bundle_size(self):
        """"Test to verify the target bundle size is correctly calculated"""

        # Constants
        on_time = time(15, 0, 0)
        off_time = time(16, 0, 0)
        ready_time = time(15, 0, 0)
        env_time = time_to_sec(time(15, 20, 0))

        # Test case 1: create more couriers than orders
        courier_1 = Courier(courier_id=1,
                            on_time=on_time,
                            off_time=off_time,
                            condition='idle')
        courier_2 = Courier(courier_id=2,
                            on_time=on_time,
                            off_time=off_time,
                            condition='idle')
        courier_3 = Courier(courier_id=3,
                            on_time=on_time,
                            off_time=off_time,
                            condition='idle')

        order_1 = Order(order_id=1, ready_time=ready_time)

        target_size = MyopicMatchingPolicy._calculate_target_bundle_size(
            orders=[order_1],
            couriers=[courier_1, courier_2, courier_3],
            env_time=env_time)

        self.assertEqual(target_size, 1)

        # Test case 2: create more orders than couriers
        courier_1 = Courier(courier_id=1,
                            on_time=on_time,
                            off_time=off_time,
                            condition='idle')

        order_1 = Order(order_id=1, ready_time=ready_time)
        order_2 = Order(order_id=2, ready_time=ready_time)
        order_3 = Order(order_id=3, ready_time=ready_time)

        target_size = MyopicMatchingPolicy._calculate_target_bundle_size(
            orders=[order_1, order_2, order_3],
            couriers=[courier_1],
            env_time=env_time)

        self.assertEqual(target_size, 3)

        # Test case 3: create more orders than couriers but couriers are idle
        courier_1 = Courier(courier_id=1,
                            on_time=on_time,
                            off_time=off_time,
                            condition='moving')

        order_2 = Order(order_id=2, ready_time=ready_time)
        order_3 = Order(order_id=3, ready_time=ready_time)

        target_size = MyopicMatchingPolicy._calculate_target_bundle_size(
            orders=[order_1, order_2, order_3],
            couriers=[courier_1],
            env_time=env_time)

        self.assertEqual(target_size, 1)

    @patch('services.osrm_service.OSRMService.get_route',
           side_effect=mocked_get_route)
    @patch('settings.settings.DISPATCHER_PROSPECTS_MAX_ORDERS', 1)
    def test_generate_group_routes(self, osrm):
        """Test to verify how the heuristic to generate routes work"""

        # Constants
        order_1 = Order(order_id=1,
                        pick_up_at=Location(lat=4.678417, lng=-74.054725),
                        drop_off_at=Location(lat=4.717045, lng=-74.036359),
                        ready_time=time(12, 13, 0))
        order_2 = Order(order_id=2,
                        pick_up_at=Location(lat=4.678417, lng=-74.054725),
                        drop_off_at=Location(lat=4.723418, lng=-74.037067),
                        ready_time=time(12, 10, 0))
        order_3 = Order(order_id=3,
                        pick_up_at=Location(lat=4.678417, lng=-74.054725),
                        drop_off_at=Location(lat=4.723418, lng=-74.037067),
                        ready_time=time(12, 30, 0))
        old_order = Order(order_id=9898,
                          pick_up_at=Location(lat=4.678417, lng=-74.054725),
                          drop_off_at=Location(lat=4.727278, lng=-74.039299),
                          ready_time=time(11, 50, 0))
        target_size = 2

        # Case 1: orders routed without initial routes and courier slack
        num_idle_couriers = 4
        routes = MyopicMatchingPolicy._generate_group_routes(
            orders=[order_1, order_2],
            target_size=target_size,
            courier_routes=[],
            num_idle_couriers=num_idle_couriers)
        self.assertTrue(routes)
        self.assertEqual(len(routes), 2)

        routed_orders = [o for route in routes for o in route.orders.keys()]
        for order in [order_1, order_2]:
            self.assertIn(order.order_id, routed_orders)

        # Case 2: orders routed without initial routes and no courier slack
        num_idle_couriers = 0
        routes = MyopicMatchingPolicy._generate_group_routes(
            orders=[order_1, order_2],
            target_size=target_size,
            courier_routes=[],
            num_idle_couriers=num_idle_couriers)
        self.assertTrue(routes)
        self.assertEqual(len(routes), 1)

        routed_orders = [o for route in routes for o in route.orders.keys()]
        for order in [order_1, order_2]:
            self.assertIn(order.order_id, routed_orders)

        # Case 3: orders routed with initial routes and courier slack
        num_idle_couriers = 1
        initial_route = Route(orders={old_order.order_id: old_order},
                              stops=[
                                  Stop(orders={old_order.order_id: old_order},
                                       location=old_order.pick_up_at,
                                       position=0,
                                       type=StopType.PICK_UP),
                                  Stop(orders={old_order.order_id: old_order},
                                       location=old_order.drop_off_at,
                                       position=1,
                                       type=StopType.DROP_OFF)
                              ])
        routes = MyopicMatchingPolicy._generate_group_routes(
            orders=[order_1, order_2],
            target_size=2,
            courier_routes=[initial_route],
            num_idle_couriers=num_idle_couriers,
            max_orders=3,
            courier_ids=[3])
        self.assertTrue(routes)
        self.assertEqual(len(routes), 1)

        routed_orders = [o for route in routes for o in route.orders.keys()]
        for order in [order_1, order_2]:
            self.assertIn(order.order_id, routed_orders)

        self.assertIsNone(routes[0].initial_prospect)

        # Case 4: orders routed without initial routes and insufficient couriers
        num_idle_couriers = 0
        target_size = 5
        routes = MyopicMatchingPolicy._generate_group_routes(
            orders=[order_1, order_2, order_3],
            target_size=target_size,
            courier_routes=[],
            num_idle_couriers=num_idle_couriers)
        self.assertTrue(routes)
        self.assertEqual(len(routes), 1)

        routed_orders = [o for route in routes for o in route.orders.keys()]
        for order in [order_1, order_2, order_3]:
            self.assertIn(order.order_id, routed_orders)

    @patch('services.osrm_service.OSRMService.get_route',
           side_effect=mocked_get_route)
    @patch('settings.settings.DISPATCHER_MYOPIC_READY_TIME_SLACK',
           min_to_sec(20))
    def test_generate_routes_idle_couriers(self, osrm):
        """Test to verify how routes are created from test orders and couriers"""

        # Constants
        env_time = hour_to_sec(12) + min_to_sec(20)
        on_time = time(8, 0, 0)
        off_time = time(16, 0, 0)

        # Orders
        order_1 = Order(order_id=1,
                        pick_up_at=Location(lat=4.678759, lng=-74.055729),
                        drop_off_at=Location(lat=4.681694, lng=-74.044811),
                        ready_time=time(12, 30, 0),
                        expected_drop_off_time=time(12, 40, 0),
                        pick_up_service_time=0,
                        drop_off_service_time=0)
        order_2 = Order(order_id=2,
                        pick_up_at=Location(lat=4.678759, lng=-74.055729),
                        drop_off_at=Location(lat=4.695001, lng=-74.040737),
                        ready_time=time(12, 32, 0),
                        expected_drop_off_time=time(12, 42, 0),
                        pick_up_service_time=0,
                        drop_off_service_time=0)
        order_3 = Order(order_id=3,
                        pick_up_at=Location(lat=4.678759, lng=-74.055729),
                        drop_off_at=Location(lat=4.668742, lng=-74.056684),
                        ready_time=time(12, 33, 0),
                        expected_drop_off_time=time(12, 43, 0),
                        pick_up_service_time=0,
                        drop_off_service_time=0)
        order_4 = Order(order_id=4,
                        pick_up_at=Location(lat=4.678759, lng=-74.055729),
                        drop_off_at=Location(lat=4.661441, lng=-74.056955),
                        ready_time=time(12, 34, 0),
                        expected_drop_off_time=time(12, 44, 0),
                        pick_up_service_time=0,
                        drop_off_service_time=0)

        # Couriers
        courier_1 = Courier(courier_id=1,
                            on_time=on_time,
                            off_time=off_time,
                            condition='idle',
                            location=Location(lat=4.676854, lng=-74.057498))
        courier_2 = Courier(courier_id=2,
                            on_time=on_time,
                            off_time=off_time,
                            condition='idle',
                            location=Location(lat=4.679408, lng=-74.052524))

        # Get routes and assert expected behavior
        policy = MyopicMatchingPolicy(assignment_updates=True,
                                      prospects=True,
                                      notification_filtering=False,
                                      mip_matcher=False)
        routes = policy._generate_routes(
            orders=[order_1, order_2, order_3, order_4],
            couriers=[courier_1, courier_2],
            env_time=env_time)
        self.assertTrue(routes)
        self.assertEqual(len(routes), 2)
        self.assertIn(order_1.order_id, routes[0].orders.keys())
        self.assertIn(order_3.order_id, routes[0].orders.keys())
        self.assertIn(order_2.order_id, routes[1].orders.keys())
        self.assertIn(order_4.order_id, routes[1].orders.keys())

    @patch('services.osrm_service.OSRMService.get_route',
           side_effect=mocked_get_route)
    @patch('settings.settings.DISPATCHER_MYOPIC_READY_TIME_SLACK',
           min_to_sec(20))
    def test_generate_routes_picking_up_couriers(self, osrm):
        """Test to verify how routes are created from test orders and couriers"""

        # Constants
        env_time = hour_to_sec(12) + min_to_sec(20)
        on_time = time(8, 0, 0)
        off_time = time(16, 0, 0)
        random.seed(56)

        # Orders
        order_1 = Order(order_id=1,
                        pick_up_at=Location(lat=4.678759, lng=-74.055729),
                        drop_off_at=Location(lat=4.681694, lng=-74.044811),
                        ready_time=time(12, 30, 0),
                        expected_drop_off_time=time(12, 40, 0),
                        pick_up_service_time=0,
                        drop_off_service_time=0)
        order_2 = Order(order_id=2,
                        pick_up_at=Location(lat=4.678759, lng=-74.055729),
                        drop_off_at=Location(lat=4.695001, lng=-74.040737),
                        ready_time=time(12, 32, 0),
                        expected_drop_off_time=time(12, 42, 0),
                        pick_up_service_time=0,
                        drop_off_service_time=0)
        order_3 = Order(order_id=3,
                        pick_up_at=Location(lat=4.678759, lng=-74.055729),
                        drop_off_at=Location(lat=4.668742, lng=-74.056684),
                        ready_time=time(12, 33, 0),
                        expected_drop_off_time=time(12, 43, 0),
                        pick_up_service_time=0,
                        drop_off_service_time=0)

        # Couriers
        courier_3 = Courier(
            courier_id=3,
            on_time=on_time,
            off_time=off_time,
            condition='picking_up',
            location=order_3.pick_up_at,
            active_route=Route(orders={order_3.order_id: order_3},
                               stops=[
                                   Stop(location=order_3.pick_up_at,
                                        orders={order_3.order_id: order_3},
                                        position=0,
                                        type=StopType.PICK_UP),
                                   Stop(location=order_3.drop_off_at,
                                        orders={order_3.order_id: order_3},
                                        position=1,
                                        type=StopType.DROP_OFF)
                               ]),
            active_stop=Stop(location=order_3.pick_up_at,
                             orders={order_3.order_id: order_3},
                             position=0,
                             type=StopType.PICK_UP))

        # Get routes and assert expected behavior
        policy = MyopicMatchingPolicy(assignment_updates=True,
                                      prospects=True,
                                      notification_filtering=False,
                                      mip_matcher=False)
        routes = policy._generate_routes(orders=[order_1, order_2],
                                         couriers=[courier_3],
                                         env_time=env_time)
        self.assertTrue(routes)
        self.assertEqual(len(routes), 2)
        self.assertIsNone(routes[0].initial_prospect)
        self.assertIsNone(routes[1].initial_prospect)

    @patch('services.osrm_service.OSRMService.get_route',
           side_effect=mocked_get_route)
    @patch('settings.settings.DISPATCHER_PROSPECTS_MAX_STOP_OFFSET',
           min_to_sec(15))
    def test_generate_matching_prospects_all(self, osrm):
        """Test to verify how prospects are created"""

        # Constants
        env_time = hour_to_sec(12) + min_to_sec(20)
        on_time = time(8, 0, 0)
        off_time = time(16, 0, 0)

        # Orders
        order_1 = Order(order_id=1,
                        pick_up_at=Location(lat=4.678759, lng=-74.055729),
                        drop_off_at=Location(lat=4.681694, lng=-74.044811),
                        ready_time=time(12, 30, 0),
                        expected_drop_off_time=time(12, 40, 0),
                        pick_up_service_time=0,
                        drop_off_service_time=0)
        order_2 = Order(order_id=2,
                        pick_up_at=Location(lat=4.678759, lng=-74.055729),
                        drop_off_at=Location(lat=4.695001, lng=-74.040737),
                        ready_time=time(12, 32, 0),
                        expected_drop_off_time=time(12, 42, 0),
                        pick_up_service_time=0,
                        drop_off_service_time=0)
        order_3 = Order(order_id=3,
                        pick_up_at=Location(lat=4.678759, lng=-74.055729),
                        drop_off_at=Location(lat=4.668742, lng=-74.056684),
                        ready_time=time(12, 33, 0),
                        expected_drop_off_time=time(12, 43, 0),
                        pick_up_service_time=0,
                        drop_off_service_time=0)
        order_4 = Order(order_id=4,
                        pick_up_at=Location(lat=4.678759, lng=-74.055729),
                        drop_off_at=Location(lat=4.661441, lng=-74.056955),
                        ready_time=time(12, 34, 0),
                        expected_drop_off_time=time(12, 44, 0),
                        pick_up_service_time=0,
                        drop_off_service_time=0)

        # Couriers
        courier_1 = Courier(courier_id=1,
                            on_time=on_time,
                            off_time=off_time,
                            condition='idle',
                            location=Location(lat=4.676854, lng=-74.057498))
        courier_2 = Courier(courier_id=2,
                            on_time=on_time,
                            off_time=off_time,
                            condition='idle',
                            location=Location(lat=4.679408, lng=-74.052524))

        # Routes
        policy = MyopicMatchingPolicy(assignment_updates=True,
                                      prospects=True,
                                      notification_filtering=False,
                                      mip_matcher=False)
        routes = policy._generate_routes(
            orders=[order_1, order_2, order_3, order_4],
            couriers=[courier_1, courier_2],
            env_time=env_time)

        # Generate prospects and assert expected behavior
        prospects = policy._generate_matching_prospects(
            routes=routes, couriers=[courier_1, courier_2], env_time=env_time)
        self.assertTrue(prospects.tolist())
        self.assertEqual(len(prospects), 8)

    @patch('services.osrm_service.OSRMService.get_route',
           side_effect=mocked_get_route)
    @patch('settings.settings.DISPATCHER_PROSPECTS_MAX_STOP_OFFSET',
           min_to_sec(15))
    def test_generate_matching_prospects_picking_up_couriers(self, osrm):
        """Test to verify how prospects are created"""

        # Constants
        env_time = hour_to_sec(12) + min_to_sec(20)
        on_time = time(8, 0, 0)
        off_time = time(16, 0, 0)

        # Orders
        order_1 = Order(order_id=1,
                        pick_up_at=Location(lat=4.678759, lng=-74.055729),
                        drop_off_at=Location(lat=4.681694, lng=-74.044811),
                        ready_time=time(12, 30, 0),
                        expected_drop_off_time=time(12, 40, 0),
                        pick_up_service_time=0,
                        drop_off_service_time=0)
        order_2 = Order(order_id=2,
                        pick_up_at=Location(lat=4.678759, lng=-74.055729),
                        drop_off_at=Location(lat=4.695001, lng=-74.040737),
                        ready_time=time(12, 32, 0),
                        expected_drop_off_time=time(12, 42, 0),
                        pick_up_service_time=0,
                        drop_off_service_time=0)
        order_3 = Order(order_id=3,
                        pick_up_at=Location(lat=4.678759, lng=-74.055729),
                        drop_off_at=Location(lat=4.668742, lng=-74.056684),
                        ready_time=time(12, 33, 0),
                        expected_drop_off_time=time(12, 43, 0),
                        pick_up_service_time=0,
                        drop_off_service_time=0)

        # Couriers
        courier_3 = Courier(
            courier_id=3,
            on_time=on_time,
            off_time=off_time,
            condition='picking_up',
            location=order_3.pick_up_at,
            active_route=Route(orders={order_3.order_id: order_3},
                               stops=[
                                   Stop(location=order_3.pick_up_at,
                                        orders={order_3.order_id: order_3},
                                        position=0,
                                        type=StopType.PICK_UP),
                                   Stop(location=order_3.drop_off_at,
                                        orders={order_3.order_id: order_3},
                                        position=1,
                                        type=StopType.DROP_OFF)
                               ]),
            active_stop=Stop(location=order_3.pick_up_at,
                             orders={order_3.order_id: order_3},
                             position=0,
                             type=StopType.PICK_UP))

        # Get routes and assert expected behavior
        policy = MyopicMatchingPolicy(assignment_updates=True,
                                      prospects=True,
                                      notification_filtering=False,
                                      mip_matcher=False)
        routes = policy._generate_routes(orders=[order_1, order_2],
                                         couriers=[courier_3],
                                         env_time=env_time)

        # Generate prospects and assert expected behavior
        prospects = policy._generate_matching_prospects(routes=routes,
                                                        couriers=[courier_3],
                                                        env_time=env_time)
        self.assertFalse(prospects.tolist())

    @patch('services.osrm_service.OSRMService.get_route',
           side_effect=mocked_get_route)
    @patch('settings.settings.DISPATCHER_PROSPECTS_MAX_STOP_OFFSET',
           min_to_sec(15))
    @patch('settings.settings.DISPATCHER_MYOPIC_READY_TIME_SLACK',
           min_to_sec(20))
    def test_myopic_matching_policy_execute(self, osrm):
        """Test to verify how the optimization model is solved"""

        # Constants
        env_time = hour_to_sec(12) + min_to_sec(20)
        on_time = time(8, 0, 0)
        off_time = time(16, 0, 0)
        random.seed(45)

        # Orders
        order_1 = Order(order_id=1,
                        pick_up_at=Location(lat=4.678759, lng=-74.055729),
                        drop_off_at=Location(lat=4.681694, lng=-74.044811),
                        ready_time=time(12, 30, 0),
                        expected_drop_off_time=time(12, 40, 0),
                        pick_up_service_time=0,
                        drop_off_service_time=0)
        order_2 = Order(order_id=2,
                        pick_up_at=Location(lat=4.678759, lng=-74.055729),
                        drop_off_at=Location(lat=4.695001, lng=-74.040737),
                        ready_time=time(12, 32, 0),
                        expected_drop_off_time=time(12, 42, 0),
                        pick_up_service_time=0,
                        drop_off_service_time=0)
        order_3 = Order(order_id=3,
                        pick_up_at=Location(lat=4.678759, lng=-74.055729),
                        drop_off_at=Location(lat=4.668742, lng=-74.056684),
                        ready_time=time(12, 33, 0),
                        expected_drop_off_time=time(12, 43, 0),
                        pick_up_service_time=0,
                        drop_off_service_time=0)
        order_4 = Order(order_id=4,
                        pick_up_at=Location(lat=4.678759, lng=-74.055729),
                        drop_off_at=Location(lat=4.661441, lng=-74.056955),
                        ready_time=time(12, 34, 0),
                        expected_drop_off_time=time(12, 44, 0),
                        pick_up_service_time=0,
                        drop_off_service_time=0)

        # Couriers
        courier_1 = Courier(courier_id=1,
                            on_time=on_time,
                            off_time=off_time,
                            condition='idle',
                            location=Location(lat=4.676854, lng=-74.057498))
        courier_2 = Courier(courier_id=2,
                            on_time=on_time,
                            off_time=off_time,
                            condition='idle',
                            location=Location(lat=4.679408, lng=-74.052524))
        courier_3 = Courier(
            courier_id=3,
            on_time=on_time,
            off_time=off_time,
            condition='picking_up',
            location=order_3.pick_up_at,
            active_route=Route(orders={order_3.order_id: order_3},
                               stops=[
                                   Stop(location=order_3.pick_up_at,
                                        orders={order_3.order_id: order_3},
                                        position=0,
                                        type=StopType.PICK_UP),
                                   Stop(location=order_3.drop_off_at,
                                        orders={order_3.order_id: order_3},
                                        position=1,
                                        type=StopType.DROP_OFF)
                               ]),
            active_stop=Stop(location=order_3.pick_up_at,
                             orders={order_3.order_id: order_3},
                             position=0,
                             type=StopType.PICK_UP))

        # Get all the elements from the policy and assert their expected behavior
        policy = MyopicMatchingPolicy(assignment_updates=True,
                                      prospects=True,
                                      notification_filtering=False,
                                      mip_matcher=False)
        routes = policy._generate_routes(
            orders=[order_1, order_2, order_4],
            couriers=[courier_1, courier_2, courier_3],
            env_time=env_time)
        self.assertTrue(routes)
        self.assertEqual(len(routes), 3)
        self.assertEqual(routes[0].initial_prospect, courier_3.courier_id),
        self.assertIn(order_4.order_id, routes[0].orders)
        self.assertEqual(len(routes[0].orders), 1)
        self.assertEqual(len(routes[1].orders), 1)
        self.assertEqual(len(routes[2].orders), 1)

        prospects = policy._generate_matching_prospects(
            routes=routes,
            couriers=[courier_1, courier_2, courier_3],
            env_time=env_time)
        self.assertTrue(prospects.tolist())
        self.assertEqual(len(prospects), 7)
        courier_3_prospects = [
            prospect for prospect in prospects if prospect[0] == 2
        ]
        self.assertEqual(len(courier_3_prospects), 1)

        costs = policy._generate_matching_costs(
            routes=routes,
            couriers=[courier_1, courier_2, courier_3],
            prospects=prospects,
            env_time=env_time)
        self.assertTrue(costs.tolist())
        self.assertEqual(len(prospects), len(costs))
        self.assertEqual(len(costs), 7)
        self.assertNotIn(0., costs)

        problem = MatchingProblemBuilder.build(
            routes=routes,
            couriers=[courier_1, courier_2, courier_3],
            prospects=prospects,
            costs=costs)
        self.assertTrue(problem)
        self.assertEqual(len(prospects), len(problem.prospects))
        self.assertEqual(len(prospects), len(problem.matching_prospects))
        self.assertEqual(len(prospects), len(problem.costs))
        self.assertEqual(routes, problem.routes)
        self.assertEqual(problem.couriers, [courier_1, courier_2, courier_3])

        graph = GraphBuilder.build(problem)
        self.assertTrue(graph)
        self.assertEqual(len(graph.nodes), 7)
        self.assertEqual(len(graph.arcs), 13)

        model_builder = GraphOptimizationModelBuilder(
            sense='max',
            model_constraints=[BalanceConstraint()],
            optimizer='pulp')
        model = model_builder.build(graph)
        self.assertTrue(model)
        self.assertEqual(len(model.constraints), len(graph.nodes))
        self.assertEqual(len(model.variable_set), len(graph.arcs))

        solution = model.solve()
        self.assertTrue(solution.tolist())
        self.assertEqual(len(solution), len(graph.arcs))
        self.assertEqual(solution[0:len(problem.prospects)].sum(), 3)
        self.assertEqual(solution.sum(), 6)

        notifications = policy._process_solution(solution, problem, env_time)
        self.assertEqual(len(notifications), len(routes))
        self.assertIsInstance(notifications[0].instruction[0], Stop)
        self.assertIsInstance(notifications[1].instruction, Route)
        self.assertIsInstance(notifications[2].instruction, Route)
        self.assertEqual(notifications[0].courier, courier_3)
        self.assertIn(order_4.order_id,
                      notifications[0].instruction[0].orders.keys())

    @patch('services.osrm_service.OSRMService.get_route',
           side_effect=mocked_get_route)
    @patch('settings.settings.DISPATCHER_PROSPECTS_MAX_STOP_OFFSET',
           min_to_sec(15))
    @patch('settings.settings.DISPATCHER_MYOPIC_READY_TIME_SLACK',
           min_to_sec(20))
    def test_myopic_matching_policy_execute_mip_matcher(self, osrm):
        """Test to verify how the optimization model is solved with a MIP approach"""

        # Constants
        env_time = hour_to_sec(12) + min_to_sec(20)
        on_time = time(8, 0, 0)
        off_time = time(16, 0, 0)
        random.seed(45)

        # Orders
        order_1 = Order(order_id=1,
                        pick_up_at=Location(lat=4.678759, lng=-74.055729),
                        drop_off_at=Location(lat=4.681694, lng=-74.044811),
                        ready_time=time(12, 30, 0),
                        expected_drop_off_time=time(12, 40, 0),
                        pick_up_service_time=0,
                        drop_off_service_time=0)
        order_2 = Order(order_id=2,
                        pick_up_at=Location(lat=4.678759, lng=-74.055729),
                        drop_off_at=Location(lat=4.695001, lng=-74.040737),
                        ready_time=time(12, 32, 0),
                        expected_drop_off_time=time(12, 42, 0),
                        pick_up_service_time=0,
                        drop_off_service_time=0)
        order_3 = Order(order_id=3,
                        pick_up_at=Location(lat=4.678759, lng=-74.055729),
                        drop_off_at=Location(lat=4.668742, lng=-74.056684),
                        ready_time=time(12, 33, 0),
                        expected_drop_off_time=time(12, 43, 0),
                        pick_up_service_time=0,
                        drop_off_service_time=0)
        order_4 = Order(order_id=4,
                        pick_up_at=Location(lat=4.678759, lng=-74.055729),
                        drop_off_at=Location(lat=4.661441, lng=-74.056955),
                        ready_time=time(12, 34, 0),
                        expected_drop_off_time=time(12, 44, 0),
                        pick_up_service_time=0,
                        drop_off_service_time=0)

        # Couriers
        courier_1 = Courier(courier_id=1,
                            on_time=on_time,
                            off_time=off_time,
                            condition='idle',
                            location=Location(lat=4.676854, lng=-74.057498))
        courier_2 = Courier(courier_id=2,
                            on_time=on_time,
                            off_time=off_time,
                            condition='idle',
                            location=Location(lat=4.679408, lng=-74.052524))
        courier_3 = Courier(
            courier_id=3,
            on_time=on_time,
            off_time=off_time,
            condition='picking_up',
            location=order_3.pick_up_at,
            active_route=Route(orders={order_3.order_id: order_3},
                               stops=[
                                   Stop(location=order_3.pick_up_at,
                                        orders={order_3.order_id: order_3},
                                        position=0,
                                        type=StopType.PICK_UP),
                                   Stop(location=order_3.drop_off_at,
                                        orders={order_3.order_id: order_3},
                                        position=1,
                                        type=StopType.DROP_OFF)
                               ]),
            active_stop=Stop(location=order_3.pick_up_at,
                             orders={order_3.order_id: order_3},
                             position=0,
                             type=StopType.PICK_UP))

        # Get all the elements from the policy and assert their expected behavior
        policy = MyopicMatchingPolicy(assignment_updates=False,
                                      prospects=False,
                                      notification_filtering=False,
                                      mip_matcher=False)
        routes = policy._generate_routes(
            orders=[order_1, order_2, order_4],
            couriers=[courier_1, courier_2, courier_3],
            env_time=env_time)
        self.assertTrue(routes)
        self.assertEqual(len(routes), 2)
        self.assertEqual(len(routes[0].orders), 2)
        self.assertEqual(len(routes[1].orders), 1)

        prospects = policy._generate_matching_prospects(
            routes=routes,
            couriers=[courier_1, courier_2, courier_3],
            env_time=env_time)
        self.assertTrue(prospects.tolist())
        self.assertEqual(len(prospects), 4),
        self.assertEqual(len(prospects),
                         len(routes) * len([courier_1, courier_2]))

        costs = policy._generate_matching_costs(
            routes=routes,
            couriers=[courier_1, courier_2, courier_3],
            prospects=prospects,
            env_time=env_time)
        self.assertTrue(costs.tolist())
        self.assertEqual(len(prospects), len(costs))
        self.assertEqual(len(costs), 4)
        self.assertNotIn(0., costs)

        problem = MatchingProblemBuilder.build(
            routes=routes,
            couriers=[courier_1, courier_2, courier_3],
            prospects=prospects,
            costs=costs)
        self.assertTrue(problem)
        self.assertEqual(len(prospects), len(problem.prospects))
        self.assertEqual(len(prospects), len(problem.matching_prospects))
        self.assertEqual(len(prospects), len(problem.costs))
        self.assertEqual(routes, problem.routes)
        self.assertEqual(problem.couriers, [courier_1, courier_2, courier_3])
        self.assertNotIn(str(courier_3.courier_id),
                         problem.matching_prospects['i'])

        model_builder = MIPOptimizationModelBuilder(
            sense='max',
            model_constraints=[
                CourierAssignmentConstraint(),
                RouteAssignmentConstraint()
            ],
            optimizer='pulp')
        model = model_builder.build(problem)
        self.assertTrue(model)
        self.assertEqual(len(model.constraints),
                         len(problem.routes) + len([courier_1, courier_2]))
        self.assertEqual(len(model.variable_set),
                         len(problem.matching_prospects) + len(problem.routes))

        solution = model.solve()
        self.assertTrue(solution.tolist())
        self.assertEqual(len(solution),
                         len(problem.matching_prospects) + len(problem.routes))
        self.assertEqual(solution[0:len(problem.prospects)].sum(), 2)
        self.assertEqual(solution.sum(), 2)

        notifications = policy._process_solution(solution, problem, env_time)
        self.assertEqual(len(notifications), len(routes))
        self.assertIsInstance(notifications[0].instruction, Route)
        self.assertIsInstance(notifications[1].instruction, Route)
        self.assertEqual(notifications[0].courier, courier_1)
        self.assertEqual(notifications[1].courier, courier_2)
        self.assertIn(order_1.order_id,
                      notifications[1].instruction.orders.keys())
        self.assertIn(order_4.order_id,
                      notifications[1].instruction.orders.keys())
Example #20
0
    def test_generate_matching_prospects_picking_up_couriers(self, osrm):
        """Test to verify how prospects are created"""

        # Constants
        env_time = hour_to_sec(12) + min_to_sec(20)
        on_time = time(8, 0, 0)
        off_time = time(16, 0, 0)

        # Orders
        order_1 = Order(order_id=1,
                        pick_up_at=Location(lat=4.678759, lng=-74.055729),
                        drop_off_at=Location(lat=4.681694, lng=-74.044811),
                        ready_time=time(12, 30, 0),
                        expected_drop_off_time=time(12, 40, 0),
                        pick_up_service_time=0,
                        drop_off_service_time=0)
        order_2 = Order(order_id=2,
                        pick_up_at=Location(lat=4.678759, lng=-74.055729),
                        drop_off_at=Location(lat=4.695001, lng=-74.040737),
                        ready_time=time(12, 32, 0),
                        expected_drop_off_time=time(12, 42, 0),
                        pick_up_service_time=0,
                        drop_off_service_time=0)
        order_3 = Order(order_id=3,
                        pick_up_at=Location(lat=4.678759, lng=-74.055729),
                        drop_off_at=Location(lat=4.668742, lng=-74.056684),
                        ready_time=time(12, 33, 0),
                        expected_drop_off_time=time(12, 43, 0),
                        pick_up_service_time=0,
                        drop_off_service_time=0)

        # Couriers
        courier_3 = Courier(
            courier_id=3,
            on_time=on_time,
            off_time=off_time,
            condition='picking_up',
            location=order_3.pick_up_at,
            active_route=Route(orders={order_3.order_id: order_3},
                               stops=[
                                   Stop(location=order_3.pick_up_at,
                                        orders={order_3.order_id: order_3},
                                        position=0,
                                        type=StopType.PICK_UP),
                                   Stop(location=order_3.drop_off_at,
                                        orders={order_3.order_id: order_3},
                                        position=1,
                                        type=StopType.DROP_OFF)
                               ]),
            active_stop=Stop(location=order_3.pick_up_at,
                             orders={order_3.order_id: order_3},
                             position=0,
                             type=StopType.PICK_UP))

        # Get routes and assert expected behavior
        policy = MyopicMatchingPolicy(assignment_updates=True,
                                      prospects=True,
                                      notification_filtering=False,
                                      mip_matcher=False)
        routes = policy._generate_routes(orders=[order_1, order_2],
                                         couriers=[courier_3],
                                         env_time=env_time)

        # Generate prospects and assert expected behavior
        prospects = policy._generate_matching_prospects(routes=routes,
                                                        couriers=[courier_3],
                                                        env_time=env_time)
        self.assertFalse(prospects.tolist())
Example #21
0
class TestsUser(unittest.TestCase):
    """Tests for the User actor class"""

    # Order properties to be reused
    order_id = 0
    pick_up_at = Location(lat=4.689697, lng=-74.055495)
    drop_off_at = Location(lat=4.690296, lng=-74.043929)
    placement_time = time(12, 0, 0)
    expected_drop_off_time = time(12, 40, 0)
    preparation_time = time(12, 1, 0)
    ready_time = time(12, 11, 0)

    # Services to be reused
    cancellation_policy = RandomCancellationPolicy()

    @patch('settings.settings.USER_WAIT_TO_CANCEL', min_to_sec(5))
    @patch('settings.settings.USER_CANCELLATION_PROBABILITY', 0.99)
    @patch('settings.settings.DISPATCHER_WAIT_TO_CANCEL', min_to_sec(50))
    def test_submit_cancel_order(self):
        """Test to verify how a user submits and decides to cancel an order"""

        # Constants
        random.seed(666)
        initial_time = hour_to_sec(12)
        time_delta = min_to_sec(10)

        # Services
        env = Environment(initial_time=initial_time)
        dispatcher = Dispatcher(env=env, matching_policy=DummyMatchingPolicy())

        # Create a user and have it submit an order immediately
        user = User(cancellation_policy=self.cancellation_policy,
                    dispatcher=dispatcher,
                    env=env)
        user.submit_order_event(
            order_id=self.order_id,
            pick_up_at=self.pick_up_at,
            drop_off_at=self.drop_off_at,
            placement_time=self.placement_time,
            expected_drop_off_time=self.expected_drop_off_time,
            preparation_time=self.preparation_time,
            ready_time=self.ready_time)
        env.run(until=initial_time + time_delta)

        # Verify order is created and canceled due to a courier not being assigned
        self.assertTrue(user.order)
        self.assertIsNone(user.order.courier_id)
        self.assertIsNotNone(user.order.cancellation_time)
        self.assertEqual(dispatcher.unassigned_orders, {})
        self.assertIn(self.order_id, dispatcher.canceled_orders.keys())
        self.assertEqual(
            user.order.cancellation_time,
            (datetime.combine(date.today(), self.placement_time) +
             timedelta(seconds=settings.USER_WAIT_TO_CANCEL)).time())
        self.assertEqual(user.condition, 'canceled')

    @patch('settings.settings.USER_WAIT_TO_CANCEL', min_to_sec(40))
    @patch('settings.settings.DISPATCHER_WAIT_TO_CANCEL', min_to_sec(50))
    def test_submit_courier_assigned(self):
        """Test to verify how a user submits and order and doesn't cancel since a courier is assigned"""

        # Constants
        random.seed(666)

        # Services
        env = Environment(initial_time=hour_to_sec(12))
        dispatcher = Dispatcher(env=env, matching_policy=DummyMatchingPolicy())

        # Create a user, have it submit an order immediately and after some minutes, assign a courier
        user = User(cancellation_policy=self.cancellation_policy,
                    dispatcher=dispatcher,
                    env=env)
        user.submit_order_event(
            order_id=self.order_id,
            pick_up_at=self.pick_up_at,
            drop_off_at=self.drop_off_at,
            placement_time=self.placement_time,
            expected_drop_off_time=self.expected_drop_off_time,
            preparation_time=self.preparation_time,
            ready_time=self.ready_time)
        env.process(TestsDispatcher.assign_courier(user, env, dispatcher))
        env.run(until=hour_to_sec(13))

        # Verify order is created but not canceled because a courier was assigned
        self.assertTrue(user.order)
        self.assertIsNotNone(user.order.courier_id)
        self.assertIsNone(user.order.cancellation_time)
        self.assertEqual(dispatcher.assigned_orders,
                         {self.order_id: user.order})
        self.assertEqual(dispatcher.unassigned_orders, {})
        self.assertEqual(user.condition, 'waiting')

    @patch('settings.settings.USER_CANCELLATION_PROBABILITY', 0)
    @patch('settings.settings.DISPATCHER_WAIT_TO_CANCEL', min_to_sec(120))
    def test_submit_wait_for_order(self, *args):
        """Test to verify how a user submits an order but doesn't cancel even without courier, deciding to wait"""

        # Constants
        random.seed(157)

        # Services
        env = Environment(initial_time=hour_to_sec(12))
        dispatcher = Dispatcher(env=env, matching_policy=DummyMatchingPolicy())

        # Create a user and have it submit an order immediately
        user = User(cancellation_policy=self.cancellation_policy,
                    dispatcher=dispatcher,
                    env=env)
        user.submit_order_event(
            order_id=self.order_id,
            pick_up_at=self.pick_up_at,
            drop_off_at=self.drop_off_at,
            placement_time=self.placement_time,
            expected_drop_off_time=self.expected_drop_off_time,
            preparation_time=self.preparation_time,
            ready_time=self.ready_time)
        env.run(until=hour_to_sec(13))

        # Verify order is created but not canceled, disregarding the lack of a courier
        self.assertTrue(user.order)
        self.assertIsNone(user.order.courier_id)
        self.assertIsNone(user.order.cancellation_time)
        self.assertEqual(dispatcher.unassigned_orders,
                         {self.order_id: user.order})
        self.assertEqual(user.condition, 'waiting')