餐廳模型:管理桌位與等候時間
想像你要在舊金山一個熱門地段開餐廳。決定要有多少桌位與廚房產能非常關鍵,才能在把初始投資與營運成本壓低的同時,服務到最多的顧客。離散事件模型可以透過模擬桌位使用率、顧客等候時間,以及因等待過久而離隊的顧客數量,來協助你做出投資決策。
我們先定義一個產生器,用來模擬顧客提出桌位請求,並根據等候時間決定繼續等或離開。下一個練習中,你會建立模型、執行並分析結果。模型中的時間單位為分鐘。
本練習屬於課程
Python 的離散事件模擬
練習說明
- 當顧客抵達餐廳時,開啟一個桌位請求並命名為
req。 - 使用位元或運算子,等待直到有空桌(
req)或顧客耐心耗盡(env.timeout(patience))為止。 - 產生(yield)顧客佔用桌位的時間,即變數
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