Пример #1
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())
Пример #2
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())
Пример #3
0
    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())