Get startedGet started for free

Restaurant model: Managing tables and waiting times

Imagine you want to open a restaurant in a popular area of San Francisco. Deciding the number of tables and kitchen capacity is extremely important to ensure the maximum number of customers are served while minimizing initial investment and running costs. A discrete-event model can help in this investment decision by simulating the level of table occupancy, customer waiting times, and the number of customers leaving the queue due to excessive waiting times.

Let's first define the generator that simulates table requests and customer decisions to wait or leave based on waiting times. In the next exercise, you will set up the model, run it and analyze the results. The time in the model is in minutes.

This exercise is part of the course

Discrete Event Simulation in Python

View Course

Exercise instructions

  • Open a table request as req when a customer arrives at the restaurant.
  • Use a bitwise-or operator to wait until there is a table free (req) or until the customer runs out of patience (env.timeout(patience)).
  • Yield the time the table is occupied by the customer, which is given by variable time_at_tables.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

def customer(env, name, tables):

  global customers_served, customers_quiting_waiting
  arrive = env.now

  # Request a table
  with tables.request() as ____:
    patience = random.uniform(MIN_PATIENCE, MAX_PATIENCE)

    # Wait until a table is free or the customer runs out of patience
    results = yield ____ | ____
    wait = env.now - arrive

    if req in results:

      print(f"{env.now:7.4f} {name} > Waited {wait:6.3f} minutes for a table!")
      time_at_tables = random.uniform(MIN_SEATING_TIME, MAX_SEATING_TIME)

      # Yield the time the table is occupied by the customer
      ____ env.timeout(time_at_tables)
      print(f"{env.now:7.4f} {name} > Finished meal :)")
      costumers_served += 1

    else:
      print(f"{env.now:7.4f} {name} > Gave up waiting and left after waiting {wait:7.4f} minutes :(")
      customers_quiting_waiting += 1
Edit and Run Code