การจำลองเหตุการณ์แบบไม่ต่อเนื่องด้วย Python
Dr Diogo Costa
Adjunct Professor, University of Saskatchewan, Canada & CEO of ImpactBLUE-Scientific
SimPy มีทรัพยากร 3 ประเภท:
resource
containerstore simpy.Resource()
simpy.Container()
simpy.Store()
การสร้างทรัพยากร
resource_1 = simpy.Resource(env, capacity=8)
capacity=8)การร้องขอทรัพยากร
# Open resource request with resource_1.request() as request:# Make resource request yield request# Once resource is avaiable yield env.timeout(event_duration)
รูปแบบต่อยอดจากทรัพยากรก่อนหน้า
resource_1 = simpy.PreemptiveResource(
env, capacity=1)
บางกระบวนการอาจมีลำดับความสำคัญสูงกว่ากระบวนการอื่น
ตัวอย่าง: ช่างซ่อม
การใช้งานคล้ายกับ simpy.Resource()
ร้องขอทรัพยากรที่มีลำดับความสำคัญสูงสุด
with resource_1.request(priority=1) as
request:
yield request
yield env.timeout(process_duration)
ร้องขอทรัพยากรที่มีลำดับความสำคัญต่ำกว่า
with resource_1.request(priority=2) as
request:
yield request
yield env.timeout(process_duration)
เหมาะสำหรับจำลองการผลิต/บริโภคสิ่งที่เป็นเนื้อเดียวกัน
อาจเป็นแบบต่อเนื่อง (เช่น น้ำ) หรือไม่ต่อเนื่อง (เช่น แอปเปิล)
ตัวอย่าง: ปริมาณน้ำมันสำรองในสถานีบริการ
tank_1 = simpy.Container(
env, tank_size, init=tank_size)
เพิ่มและลดปริมาณใน container/ถัง
yield tank_1.put(add_quantity)
yield tank_1.get(remove_quantity)
ดูปริมาณปัจจุบันในถัง
tank_1.level
ดูความจุรวมของถัง
tank_1.capacity
สร้าง store
store_1 = simpy.Store(
env, capacity=2)
คล้ายกับ simpy.Container() แต่สามารถเก็บออบเจกต์ที่แตกต่างกันได้
ต่างจาก container ที่เก็บเป็น "ปริมาณ" แบบนามธรรม
yield store_1.put("item_1")
yield store_1.get()
ดูรายการออบเจกต์ที่มีอยู่
store_1.items
ดูความจุของ store
store_1.capacity
ตัวอย่าง
สถานการณ์ปัจจุบัน: แคชเชียร์ 1 คน
simpy.Resource(env, capacity=1)...
time: 23.7 min | Customer 30 > Finished
สถานการณ์จำลอง: แคชเชียร์ n คน
simpy.Resource(env, capacity=2)...
time: 13.2 min | Customer 30 > Finished
simpy.Resource(env, capacity=3)...
time: 8.6 min | Customer 30 > Finished
simpy.Resource(env, capacity=4)...
time: 8.6 min | Customer 30 > Finished
การจำลองเหตุการณ์แบบไม่ต่อเนื่องด้วย Python