Recently I learned about SIMPY and I think it can be very helpful in providing my work with some information and provide a background to make decisions. Please note I am quite new to programming with python, I know the basics but I am learning as I go.
The problem: I am working on a container terminal where we have three types of moves:
All of these moves are being 'generated' at the same time with the described intervals. The vessel and warehouse both have their own transport. However the destination for all of the above steps is the same. The container stack works with a FIFO principle (first in first out) and can handle four moves at a time. A move roughly takes 270 seconds.
What I am trying to achieve is by changing the amount of resources (number of transport on the vessel and warehouse and the gate moves) to see the effect on the business within the container stack.
I got the basic simulation to work for one of the flows (VESSEL) and I am now trying to add the warehouse flow however I am stuck. When I am using the below code, only moves from the vessel are being generated:
def move_generator(env):
while True:
yield env.timeout(120)
print("Vessel move generated at: %.1f" % (env.now))
while True:
yield env.timeout(960)
print("Warehouse move generated at: %.1f" % (env.now))
import simpy
env = simpy.Environment()
env.process(move_generator(env))
env.run(until=3600)
The output of the above code is:
Vessel move generated at: 120.0
Vessel move generated at: 240.0
Vessel move generated at: 360.0
Vessel move generated at: 480.0
Vessel move generated at: 600.0
Vessel move generated at: 720.0
Vessel move generated at: 840.0
Vessel move generated at: 960.0
Vessel move generated at: 1080.0
Vessel move generated at: 1200.0
Vessel move generated at: 1320.0
Vessel move generated at: 1440.0
Vessel move generated at: 1560.0
Vessel move generated at: 1680.0
Vessel move generated at: 1800.0
Vessel move generated at: 1920.0
Vessel move generated at: 2040.0
Vessel move generated at: 2160.0
Vessel move generated at: 2280.0
Vessel move generated at: 2400.0
Vessel move generated at: 2520.0
Vessel move generated at: 2640.0
Vessel move generated at: 2760.0
Vessel move generated at: 2880.0
Vessel move generated at: 3000.0
Vessel move generated at: 3120.0
Vessel move generated at: 3240.0
Vessel move generated at: 3360.0
Vessel move generated at: 3480.0
However the desired output would be:
Vessel move generated at: 120.0
Vessel move generated at: 240.0
Vessel move generated at: 360.0
Vessel move generated at: 480.0
Vessel move generated at: 600.0
Vessel move generated at: 720.0
Vessel move generated at: 840.0
Vessel move generated at: 960.0
Warehouse move generated at: 960.0
Vessel move generated at: 1080.0
Vessel move generated at: 1200.0
Vessel move generated at: 1320.0
Vessel move generated at: 1440.0
Vessel move generated at: 1560.0
Vessel move generated at: 1680.0
Vessel move generated at: 1800.0
Vessel move generated at: 1920.0
Warehouse move generated at: 1920.0
Vessel move generated at: 2040.0
Vessel move generated at: 2160.0
Vessel move generated at: 2280.0
Vessel move generated at: 2400.0
Vessel move generated at: 2520.0
Vessel move generated at: 2640.0
Vessel move generated at: 2760.0
Vessel move generated at: 2880.0
Warehouse move generated at: 2880.0
Vessel move generated at: 3000.0
Vessel move generated at: 3120.0
Vessel move generated at: 3240.0
Vessel move generated at: 3360.0
Vessel move generated at: 3480.0
I think the problem is in the two while True statements, but I cannot figure out how to deal with this correctly. I have been using Google (probably don't know the right term to find the answer) but with no success.
As you noted in your comment, the problem lies in your use of python, not of simpy.
while True:
<do anything>
# already unreachable
while True:
# also unreachable
For your example, try looking at https://simpy.readthedocs.io/en/latest/simpy_intro/shared_resources.html
for i in range(4):
env.process(car(env, 'Car %d' % i, bcs, i*2, 5))
env.run()
You can see that process
can be called multiple times before run
, allowing for "parallel" work like you desire.
import simpy
def vessel(env):
while True:
yield env.timeout(120)
print("Vessel move generated at: %.1f" % (env.now))
def warehouse(env):
while True:
yield env.timeout(960)
print("Warehouse move generated at: %.1f" % (env.now))
def main():
env = simpy.Environment()
env.process(vessel(env))
env.process(warehouse(env))
env.run(until=3600)
if __name__ == '__main__':
main()
This gives
Vessel move generated at: 120.0
Vessel move generated at: 240.0
Vessel move generated at: 360.0
Vessel move generated at: 480.0
Vessel move generated at: 600.0
Vessel move generated at: 720.0
Vessel move generated at: 840.0
Warehouse move generated at: 960.0
Vessel move generated at: 960.0
Vessel move generated at: 1080.0
Vessel move generated at: 1200.0
Vessel move generated at: 1320.0
Vessel move generated at: 1440.0
Vessel move generated at: 1560.0
Vessel move generated at: 1680.0
Vessel move generated at: 1800.0
Warehouse move generated at: 1920.0
Vessel move generated at: 1920.0
Vessel move generated at: 2040.0
Vessel move generated at: 2160.0
Vessel move generated at: 2280.0
Vessel move generated at: 2400.0
Vessel move generated at: 2520.0
Vessel move generated at: 2640.0
Vessel move generated at: 2760.0
Warehouse move generated at: 2880.0
Vessel move generated at: 2880.0
Vessel move generated at: 3000.0
Vessel move generated at: 3120.0
Vessel move generated at: 3240.0
Vessel move generated at: 3360.0
Vessel move generated at: 3480.0