Hi Stefan,
Thank you for your quick respons ! I actually am running sub environments inside a general environment. I need the subenvironments because I have to perform 2 activities at the same time relative to the general environment.
So before the activities, I generate 2 subenvironments an perform the activities seperately (by just using timeout functions).
After the subenvironments are run, I delay my general environment with the longest activity duration and move on...
For these subenvironments I need to use or call upon the same resource as used previously in the general environment.
Any ideas on how I can fix this ? Either make the resource shareable over environments (which you claimed is impossible) or change the way the 2 activities can be processed at the same time only using the general environment.. I thought of multithreading, but I guess this will mess up my general environment
Thank you in advance,
Nils
Can you please provide an example on how I can simultaneously run multiple processes that use one same resource?Suppose your environment is at t=10 before you need to perform the 2 processes that start at the same time: following code will not allow correct processing because the resource that is requested delays the same environmentenv = simpy.Environment()res = simpy.Resource(env)#simulate activity1: # env starts at t=10use = res.request # res placed in queue, suppose queuetime = 5yield use # res assigned to environment, env delayed env.now = 15durationAct1=5 # Suppose activity 1 takes 5 time units to be processed, env not yet delayed#simulate activity2: # env starts at t=15, not at t=10use = res.request # res placed in queue, suppose queuetime = 5yield use # res assigned to environment, env delayed env.now = 20durationAct2 = 10 # Suppose activity 2 takes 10 time units to be processed, env not yet delayedif durationAct1<=durationAct2:env.timeout(durationAct2) #environment delayed, env.now will result in 30, not 25else:env.timeout(durationAct1)As you can see the processes are not run simultaneously. Due to queueing, the environment itself will be delayed before the activities are performed. This causes a shift of 5 time units due to delay in the other activity.Just removing the yield function and actually the entire activity 1 can fix this problem, but I want to be able to simulate the general case where activity1 takes longer than activity2 or vice versa.Please advice on how I can fix this.Best regards and thx for the help!
def activity1(env):
... # whatever activity1 does
yield env.timeout(5)
def activity2(env):
... # whatever activity2 does
yield env.timeout(10)
def activity_1_2_chooser(env, res):
with res.request() as req:
yield req
a1 = env.process(activity1(env))
a2 = env.process(activity2(env))
yield a1 | a2
# res is released here upon *either* activity1 or activity2 completing
# env.now will reflect the shorter of the two activities' durations
env = Environment(initial_time=10)
people = Resource(env, capacity=2)
other_stuff = Container(env)
ready_events = [env.event(), env.event()]
def activity1(env, people, ready_events, other_stuff):
with people.request() as person_req:
yield person_req
ready_events[0].succeed()
yield ready_events[1] & other_stuff.get(1)
yield env.timeout(5)
assert env.now == 20
def activity2(env, people, ready_events, other_stuff):
with people.request() as person_req:
yield person_req
ready_events[1].succeed()
yield ready_events[0] & other_stuff.get(1)
yield env.timeout(10)
assert env.now == 25
def mysterious_other_process(env, other_stuff):
yield env.timeout(5)
yield other_stuff.put(2)
env.process(activity1(env, people, ready_events, other_stuff))
env.process(activity2(env, people, ready_events, other_stuff))
env.process(mysterious_other_process(env, other_stuff))
env.run()