Reputation: 1
I wrote a script to simulate a simple process to obtain information about the output (dock) and waiting times (dock and tug) if one of the resources is used/blocked. The process starts at the transshipment Point. Every 5 secondes, a new component entering the process (def component_generator). Each component will request a tug to be transported to a dock (capacity =1). The tug will be blocked during the transportation und process. After that, the tug have to be released (def transport_and_process). There are some malfunctions in the script. There is only one dock available. Ideally the tug must be wait until the dock completed a process. Thats not the case, see component and component1. Component 1 is processed at the same time as component 0 (t=10).
import simpy
import itertools
import random
sim_time = 20
T_INTER = [5, 5] # Interval between car arrivals [min, max] (seconds)
def transport_and_process(name, env, dock,tug):
tug_time = 0
print(f'{env.now:6.1f} s: {name} arrived Transshipment Point')
tug_req = tug.request() #request a tug for transportation
tug_time = env.now
yield tug_req # waiting for a tug
print(f'{env.now:6.1f} s: {name} got tug und start with transportation')
yield env.timeout(2) # travel time to the dock
print(f'{env.now:6.1f} s: {name} arrives the dock')
with dock.request() as dock_req: # at arrival, the tug requested a dock and have to wait until assignment
print(f'{env.now:6.1f} s: {name} has been assigned to the dock')
yield env.process(process_at_dock(name, env, tug_req, dock_req,tug_time)) # dock assigned and the process starts. the tug is blocked during processing
def process_at_dock(name, env, tug_req, dock_req,tug_time):
yield env.timeout(10)
yield tug.release(tug_req) # the tug will be released after processing
tug_time = env.now - tug_time
print(f'{env.now:6.1f} s: {name} dock processed')
print(f'{env.now:6.1f} s: {name} tug released')
print(f'{env.now:6.1f} s: {name} blocked the tug for:', tug_time)
""" car insert in process """
def component_generator(env, dock, tug):
"""Generate new cars that arrive at the gas station."""
for i in itertools.count():
yield env.timeout(random.randint(*T_INTER))
env.process(transport_and_process(f'Component {i}', env, dock,tug))
env = simpy.Environment()
dock = simpy.Resource(env, capacity=1)
tug = simpy.Resource(env, capacity=2)
env.process(transport_and_process('Component', env, dock, tug))
env.process(component_generator(env, dock,tug))
env.run(until=sim_time)
Upvotes: 0
Views: 53
Reputation: 1969
Where is your yield to the tug_req?
with dock.request() as dock_req:
creates a resouce request but it does not do the yield to dock_reg
Try updating you with clause to :
with dock.request() as dock_req: # at arrival, the tug requested a dock and have to wait until assignment
yield dock_req
print(f'{env.now:6.1f} s: {name} has been assigned to the dock')
yield env.process(process_at_dock(name, env, tug_req, dock_req,tug_time)) # dock assigned and the process starts. the tug is blocked during processing
Upvotes: 0