Search code examples
pythonsimpy

Simpy Store - Dependent Events


I'm trying to model situations where getting something from a store is dependent upon something else.

Let's say I have a producer that is putting items into a limited capacity store S1. These items get shifted into a another store, S2, if and only if (1) there is an item in S1 and there is room in S2.

P -> S1 -> S2

The problem is that I need the removal of an item from S1 to be conditioned on there being space in S2.

In code:

from simpy import *

env = Environment()
S1 = Store(env, capacity=1)
S2 = Store(env, capacity=1)

def producer():
    i = 0
    while True:
        yield S1.put(i)
        print("Producer put item: %s"%i)
        i += 1

def s1_to_s2():
    while True:
        item = yield S1.get()
        yield S2.put(item)

env.process(producer())
env.process(s1_to_s2())
env.run(until=20)

which produces

Producer put item: 0
Producer put item: 1
Producer put item: 2

The way I've modeled this makes it look like there's 1 extra storage slot between S1 and S2, which I don't want. Since I have 2 storage units, each with capacity 1, the producer should only be able to insert 2 units.


Solution

  • I'm afraid this is not possible with SimPy out-of-the-box. You can, however, subclass Store and the according events to model inter-store-dependencies.