コード例 #1
0
    def __init__(
        self,
        restaurants: List[Restaurant],
        customers: List[Customer],
        sessions: List[Session],
        couriers: List[Courier],
        assigner: BaseAssigner,
        metrics: List[BaseMetric],
        maps: Optional[Maps] = None,
    ) -> None:

        self.env = Env(maps)
        self.env.register(assigner)
        self.reporting = Reporting(metrics)
        self.reporting.create_connection(
            self.env.addon.data_lake)

        for restaurant in restaurants:
            self.env.register(restaurant)

        for customer in customers:
            self.env.register(customer)

        for session in sessions:
            self.env.publish(
                'customer_session_started', data={
                    'customer_id': session.customer_id,
                    'restaurant_id': session.restaurant_id,
                }, delay=session.timestamp)

        for courier in couriers:
            self.env.register(courier)
コード例 #2
0
def test_env():
    class MockClass(Registrable):
        pass

    env = Env()
    mock_object = MockClass()
    env.register(mock_object)
    mock_object.env.publish('test_topic', {})
    topic, data = next(env.run())
    assert topic == 'test_topic'
    assert data == {}
コード例 #3
0
def test_customer_customer_session_started():

    env = Env()

    customer = Customer('test_customer_id', 0, 0, TimeDist('fixed', 10))

    env.register(customer)

    input_data = {
        'restaurant_id': 'test_restaurant_id',
        'customer_id': 'test_customer_id'
    }

    env.publish('customer_session_started', input_data)
    for topic, output_data in env.run():
        if topic == 'order_placed':
            assert env.now == timedelta(minutes=0)
            assert input_data == output_data
            return

    assert False
コード例 #4
0
def test_mock_restaurant():

    env = Env()

    restaurant = Restaurant('test_id', 0, 0, timedelta(0), timedelta(1),
                            TimeDist('fixed', 10, unit='minutes'))

    env.register(restaurant)

    input_data = {
        'order_id': '1',
        'restaurant_id': restaurant.id,
        'customer_id': '3',
        'courier_id': '4'
    }

    env.publish('courier_at_restaurant', input_data)
    for topic, output_data in env.run():
        if topic == 'restaurant_order_ready':
            assert env.now == timedelta(minutes=10)
            assert input_data == output_data
            return

    assert False
コード例 #5
0
def mock_magic_agent():
    return MockMagicAgent(Env())
コード例 #6
0
def mock_agent():
    return MockAgent(Env())
コード例 #7
0
class Simulation:
    """Simulation."""

    def __init__(
        self,
        restaurants: List[Restaurant],
        customers: List[Customer],
        sessions: List[Session],
        couriers: List[Courier],
        assigner: BaseAssigner,
        metrics: List[BaseMetric],
        maps: Optional[Maps] = None,
    ) -> None:

        self.env = Env(maps)
        self.env.register(assigner)
        self.reporting = Reporting(metrics)
        self.reporting.create_connection(
            self.env.addon.data_lake)

        for restaurant in restaurants:
            self.env.register(restaurant)

        for customer in customers:
            self.env.register(customer)

        for session in sessions:
            self.env.publish(
                'customer_session_started', data={
                    'customer_id': session.customer_id,
                    'restaurant_id': session.restaurant_id,
                }, delay=session.timestamp)

        for courier in couriers:
            self.env.register(courier)

    def run(
            self, verbose: int = 0,
            until: Optional[timedelta] = None
    ) -> dict:
        """Run."""
        self.env.publish('simulation_started')
        # get event with earliest time
        for topic, data in self.env.run(until):
            if verbose > 0 and topic is not None and data is not None:
                print(f"{self.env.now} {topic or '':<16} {data or ''}")
        return self.reporting.get_results()