我正在对集装箱码头进行 SimPy 模拟,船舶到达、停泊并使用起重机卸载集装箱,然后使用卡车进行运输。 问题陈述_pt1 、 问题陈述_pt2
我需要确保:
- 每台起重机一次仅卸载一个集装箱。
- 在有空卡车可用之前,起重机应继续其卸载过程,无论之前的卡车是否已完成转运。
- 如果没有卡车如果可用,则起重机需要等待才能开始卸载集装箱。
simulation.py
import simpy
from vessel import Vessel
from config import *
import random
class Simulation:
def __init__(self, simulation_time):
self.env = simpy.Environment()
self.simulation_time = simulation_time
self.berth_resource = simpy.Resource(self.env, capacity=NUM_BERTHS)
self.crane_resource = simpy.Resource(self.env, capacity=NUM_CRANES)
self.truck_resource = simpy.Resource(self.env, capacity=NUM_TRUCKS)
def generate_vessels(self):
vessel_id = 1
while True:
yield self.env.timeout(random.expovariate(1 / (AVERAGE_INTERARRIVAL_TIME * 60))) # time between arrivals
vessel = Vessel(self.env, vessel_id, self.berth_resource, self.crane_resource, self.truck_resource)
print(f"Time {self.env.now}: Vessel {vessel_id} arrives.")
self.env.process(vessel.process())
vessel_id += 1
print(f"Time {self.env.now}: Number of vessels waiting for berths: {len(self.berth_resource.queue)}")
print(f"Time {self.env.now}: Number of vessels currently being served at berths: {self.berth_resource.count}")
print(f"Time {self.env.now}: Number of cranes currently being used: {self.crane_resource.count}")
print(f"Time {self.env.now}: Number of trucks currently being used: {self.truck_resource.count}")
def run(self):
"""Run the simulation"""
self.env.process(self.generate_vessels())
self.env.run(until=self.simulation_time)
vessel.py
import random
import simpy
from config import NUM_CONTAINERS, AVERAGE_INTERARRIVAL_TIME, CRANE_LIFT_TIME, TRUCK_CYCLE_TIME
class Vessel:
def __init__(self, env, id, berth_resource, crane_resource, truck_resource):
self.env = env
self.id = id
self.containers = NUM_CONTAINERS
self.berth_resource = berth_resource
self.crane_resource = crane_resource
self.truck_resource = truck_resource
self.container_queue = simpy.Store(env) # Queue to hold unloaded containers
def berth(self):
"""Simulate vessel berthing"""
with self.berth_resource.request() as request:
yield request
print(f"Time {self.env.now}: Vessel {self.id} starts berthing.")
print(f"Time {self.env.now}: Vessel {self.id} finished berthing.")
yield self.env.process(self.unload())
def unload_container(self, container_id):
"""Simulate unloading a single container"""
print(f"Time {self.env.now}: Vessel {self.id} requests crane for container {container_id + 1}.")
with self.crane_resource.request() as crane_request:
yield crane_request
print(f"Time {self.env.now}: Quay crane starts unloading container {container_id + 1} from vessel {self.id}.")
yield self.env.timeout(CRANE_LIFT_TIME) # Crane time to unload one container
print(f"Time {self.env.now}: Quay crane finished unloading container {container_id + 1} from vessel {self.id}.")
yield self.container_queue.put(container_id) # Add the unloaded container to the queue
# Start the transport process for this container after unloading
self.env.process(self.transport_container(container_id))
def transport_container(self, container_id):
"""Simulate the transport of a container by a truck"""
print(f"Time {self.env.now}: Vessel {self.id} requests truck for container {container_id + 1}.")
with self.truck_resource.request() as truck_request:
yield truck_request
print(f"Time {self.env.now}: Truck starts transporting container {container_id + 1} from vessel {self.id}.")
yield self.env.timeout(TRUCK_CYCLE_TIME) # Truck time to transport the container
print(f"Time {self.env.now}: Truck finished transporting container {container_id + 1} from vessel {self.id}.")
def unload(self):
"""Unload all containers from the vessel"""
unload_processes = [self.env.process(self.unload_container(i)) for i in range(self.containers)]
for process in unload_processes:
yield process # Wait for all unload processes to finish
print(f"Time {self.env.now}: Vessel {self.id} has been fully unloaded and leaves the berth.")
def process(self):
"""Process the entire sequence of arrival, berthing, and unloading"""
yield self.env.process(self.berth())
输出:
Enter the number of hours to run the simulation: 3
Time 104.4311467616123: Vessel 1 arrives.
Time 104.4311467616123: Number of vessels waiting for berths: 0
Time 104.4311467616123: Number of vessels currently being served at berths: 0
Time 104.4311467616123: Number of cranes currently being used: 0
Time 104.4311467616123: Number of trucks currently being used: 0
Time 104.4311467616123: Vessel 1 starts berthing.
Time 104.4311467616123: Vessel 1 finished berthing.
Time 104.4311467616123: Vessel 1 requests crane for container 1.
Time 104.4311467616123: Vessel 1 requests crane for container 2.
Time 104.4311467616123: Vessel 1 requests crane for container 3.
Time 104.4311467616123: Vessel 1 requests crane for container 4.
Time 104.4311467616123: Vessel 1 requests crane for container 5.
Time 104.4311467616123: Vessel 1 requests crane for container 6.
Time 104.4311467616123: Vessel 1 requests crane for container 7.
Time 104.4311467616123: Vessel 1 requests crane for container 8.
Time 104.4311467616123: Vessel 1 requests crane for container 9.
Time 104.4311467616123: Vessel 1 requests crane for container 10.
Time 104.4311467616123: Quay crane starts unloading container 1 from vessel 1.
Time 104.4311467616123: Quay crane starts unloading container 2 from vessel 1.
Time 107.4311467616123: Quay crane finished unloading container 1 from vessel 1.
Time 107.4311467616123: Quay crane finished unloading container 2 from vessel 1.
Time 107.4311467616123: Vessel 1 requests truck for container 1.
Time 107.4311467616123: Vessel 1 requests truck for container 2.
Time 107.4311467616123: Truck starts transporting container 1 from vessel 1.
Time 107.4311467616123: Truck starts transporting container 2 from vessel 1.
Time 107.4311467616123: Quay crane starts unloading container 3 from vessel 1.
Time 107.4311467616123: Quay crane starts unloading container 4 from vessel 1.
Time 110.4311467616123: Quay crane finished unloading container 3 from vessel 1.
Time 110.4311467616123: Quay crane finished unloading container 4 from vessel 1.
Time 110.4311467616123: Vessel 1 requests truck for container 3.
Time 110.4311467616123: Vessel 1 requests truck for container 4.
Time 110.4311467616123: Truck starts transporting container 3 from vessel 1.
Time 110.4311467616123: Quay crane starts unloading container 5 from vessel 1.
Time 110.4311467616123: Quay crane starts unloading container 6 from vessel 1.
Time 113.4311467616123: Truck finished transporting container 1 from vessel 1.
Time 113.4311467616123: Truck finished transporting container 2 from vessel 1.
Time 113.4311467616123: Quay crane finished unloading container 5 from vessel 1.
Time 113.4311467616123: Quay crane finished unloading container 6 from vessel 1.
Time 113.4311467616123: Vessel 1 requests truck for container 5.
Time 113.4311467616123: Vessel 1 requests truck for container 6.
Time 113.4311467616123: Truck starts transporting container 4 from vessel 1.
Time 113.4311467616123: Truck starts transporting container 5 from vessel 1.
Time 113.4311467616123: Quay crane starts unloading container 7 from vessel 1.
Time 113.4311467616123: Quay crane starts unloading container 8 from vessel 1.
Time 116.4311467616123: Truck finished transporting container 3 from vessel 1.
Time 116.4311467616123: Quay crane finished unloading container 7 from vessel 1.
Time 116.4311467616123: Quay crane finished unloading container 8 from vessel 1.
Time 116.4311467616123: Vessel 1 requests truck for container 7.
Time 116.4311467616123: Vessel 1 requests truck for container 8.
Time 116.4311467616123: Truck starts transporting container 6 from vessel 1.
Time 116.4311467616123: Quay crane starts unloading container 9 from vessel 1.
Time 116.4311467616123: Quay crane starts unloading container 10 from vessel 1.
Time 119.4311467616123: Truck finished transporting container 4 from vessel 1.
Time 119.4311467616123: Truck finished transporting container 5 from vessel 1.
Time 119.4311467616123: Quay crane finished unloading container 9 from vessel 1.
Time 119.4311467616123: Quay crane finished unloading container 10 from vessel 1.
Time 119.4311467616123: Vessel 1 requests truck for container 9.
Time 119.4311467616123: Vessel 1 requests truck for container 10.
Time 119.4311467616123: Truck starts transporting container 7 from vessel 1.
Time 119.4311467616123: Truck starts transporting container 8 from vessel 1.
Time 119.4311467616123: Vessel 1 has been fully unloaded and leaves the berth.
Time 122.4311467616123: Truck finished transporting container 6 from vessel 1.
Time 122.4311467616123: Truck starts transporting container 9 from vessel 1.
Time 125.4311467616123: Truck finished transporting container 7 from vessel 1.
Time 125.4311467616123: Truck finished transporting container 8 from vessel 1.
Time 125.4311467616123: Truck starts transporting container 10 from vessel 1.
Time 128.4311467616123: Truck finished transporting container 9 from vessel 1.
Time 131.4311467616123: Truck finished transporting container 10 from vessel 1.
问题:
我无法同时满足要求 1 和2.最初我没有想到起重机在卡车转运时同时卸货,我能够在该限制下运行模拟。
提到的实现能够独立运行卸载和转运过程,但我无法解决两个集装箱能够同时使用起重机或卡车的问题(如输出中突出显示的)
我尝试过的:
顺序处理集装箱卸载和运输,但仍然面临同时卸载的问题。 任何有关如何纠正此问题的指导或建议将不胜感激。
所需的输出:
Enter the number of hours to run the simulation: 3
Time 1: Vessel 1 starts berthing.
Time 1: Vessel 1 finished berthing.
Time 1: Quay crane starts unloading container 1 from vessel 1.
Time 4: Quay crane finished unloading container 1 from vessel 1.
Time 4: Truck starts transporting container 1 from vessel 1.
Time 4: Quay crane starts unloading container 2 from vessel 1.
Time 7: Quay crane finished unloading container 2 from vessel 1.
Time 7: Quay crane starts unloading container 3 from vessel 1.
Time 7: Truck starts transporting container 2 from vessel 1.
Time 7: Quay crane finished unloading container 2 from vessel 1.
Time 7:Vessel 2 starts berthing.
Time 7: Vessel 2 finished berthing.
.
.
.
的问题在于,正在同时为所有集装箱请求起重机和卡车。这会导致起重机和卡车在它们实际可用之前就被“预订”,从而导致同时操作。
要解决此问题,需要按顺序请求每台起重机和卡车,在卸载一个集装箱并使用卡车将其运走后,再请求下一个集装箱的资源。
修改的
Vessel
类中的
unload
方法,如下所示:
def unload(self):
"""从船只上卸下所有集装箱"""
for i in range(self.containers):
# 请求起重机以卸载集装箱
yield self.env.process(self.unload_container(i))
# 请求卡车以运输集装箱
yield self.env.process(self.transport_container(i))
print(f"Time {self.env.now}: Vessel {self.id} has been fully unloaded and leaves the berth.")
在这个修改后的代码中:
- 我们循环遍历船只上的每个集装箱。
-
yield self.env.process(self.unload_container(i))
行确保在继续下一步之前完成一个集装箱的卸载过程。 -
卸下集装箱后,
yield self.env.process(self.transport_container(i))
行请求卡车并等待运输完成。
通过这种方式,强制执行以下顺序:
- 请求起重机
- 卸载集装箱
- 请求卡车
- 运输集装箱
- 对下一个集装箱重复步骤 1-4
这可以确保一次只使用一台起重机和一台卡车来处理一个集装箱,从而满足的要求。
标签:python,asynchronous,simulation,simpy From: 78790520