餐厅模型:管理餐桌与等待时间
设想您要在旧金山一处热门地段开一家餐厅。确定餐桌数量和后厨产能至关重要,这能在尽量降低前期投入和运营成本的同时,确保尽可能多的顾客得到服务。离散事件模型可以通过模拟餐桌占用水平、顾客等待时间,以及因等待过久而离开的顾客人数,来辅助做出投资决策。
我们先定义一个生成器,用于模拟餐桌请求,以及顾客根据等待时间选择继续等待或离开的决策。在下一个练习中,您将搭建模型、运行模型,并分析结果。模型中的时间单位为分钟。
本练习是课程的一部分
Python 中的离散事件模拟
练习说明
- 当顾客到达餐厅时,以
req打开一个餐桌请求。 - 使用按位或运算符,等待直到有空桌(
req)或顾客失去耐心(env.timeout(patience))。 - 产出顾客占用餐桌的时间,即变量
time_at_tables所给出的时长。
交互式实操练习
通过完成这段示例代码来试试这个练习。
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