運輸模型:定義產生器
做得好;你已經用 Python 方法定義了模型的輸入與輸出,以及所刻劃的模型流程。現在,該把產生器組合起來,依序串接你所有的模型流程。
本練習屬於課程
Python 的離散事件模擬
練習說明
- 呼叫之前建立的
road_travel()函式,用來計算道路行駛時間,並將其完成所需的時間納入時脈。 - 呼叫之前建立的
wait_traffic_light()函式,用來計算在紅綠燈的等待時間,並將其完成所需的時間納入時脈。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
def all_processes(env, inputs):
road_stretch, distance_total, traffic_light = 0, 0, 0
while True:
road_stretch += 1
# Call function calculates road travel time
distance, distance_total = ____(inputs, distance_total)
yield env.____(distance/inputs['Speed_limit_ms'])
print(f"> Road Stretch #{road_stretch} \nLength = {distance} m , Cumulative distance travelled = {distance_total} m , Total time elapsed = {env.now} sec")
traffic_light += 1
# Call function that calculates waiting time at a traffic light
waitTime_traffic_light_sec = ____(inputs, distance_total)
yield env.____(waitTime_traffic_light_sec)
print(f"> Traffic Light #{traffic_light} \nWait time = {waitTime_traffic_light_sec} sec, Time lapsed = {env.now} sec")