Examples Stores

=================================================================================================================================

http://heather.cs.ucdavis.edu/~matloff/simpy.html#install

Inv.py

# Inv.py, inventory simulation example

# each day during the daytime, a number of orders for widgets arrives,

# uniformly distributed on {0,1,2,...,k}; the number of widgets in an order

# is uniformly distributed on {1,2,...,m}; each evening, if the inventory has

# fallen below r during the day, the inventory is restocked to level s;

# inventory is initially s

# we will find E(N), where N is the number of days until the first

# instance of an unfilled order; N = 1,2,...

# usage: python Inv.py r s k m nreps

import random, sys

class g: # global variables

    r = 10 # restocking signal level

    s = 10 # restocking replenishment level

    k = 3 # range for distr of number of orders

    m = 5 # range for distr of number of widgets per order

    tottimetobl = 0 # total wait time for those msgs

    inv = None # inventory level

    rnd = random.Random(98765) # set seed

   

def simdaytime():

    norders = g.rnd.randint(0,g.k)

    for o in range(norders):

        nwidgets = g.rnd.randint(1,g.m)

        g.inv -= nwidgets

def simevening():

        if g.inv < g.r: g.inv = g.s

def main():

    nreps = 100

    for rep in range(nreps): # simulate nreps repetitions of the experiment

        day = 1

        g.inv = g.s # inventory full at first

        while True:

            simdaytime()

            if g.inv < 0: break

            simevening()

            day += 1

        g.tottimetobl += day

    print "mean time to get a backlog =", g.tottimetobl/float(nreps)

if __name__ == "__main__": main()

=================================================================================================================================

Example The following program illustrates the use of a Store to model the production and consumption of "widgets". The widgets are distinguished by their weight:

from SimPy.Simulation import *  class ProducerD(Process):     def __init__(self):         Process.__init__(self)     def produce(self):           # the ProducerD PEM         while True:             yield put,self,buf,[Widget(9),Widget(7)]             yield hold,self,10  class ConsumerD(Process):             def __init__(self):         Process.__init__(self)     def consume(self):           # the ConsumerD PEM         while True:             toGet=3             yield get,self,buf,toGet             assert len(self.got)==toGet             print now(),'Get widget weights',\                  [x.weight for x in self.got]             yield hold,self,11  class Widget(Lister):     def __init__(self,weight=0):         self.weight=weight  widgbuf=[] for i in range(10):     widgbuf.append(Widget(5))  initialize()  buf=Store(capacity=11,initialBuffered=widgbuf,monitored=True) for i in range(3):       # define and activate 3 producer objects     p=ProducerD()     activate(p,p.produce()) for i in range(3):       # define and activate 3 consumer objects     c=ConsumerD()     activate(c,c.consume())  simulate(until=50)  print 'LenBuffer:',buf.bufferMon     # length of buffer print 'getQ:',buf.getQMon            # length of getQ print 'putQ',buf.putQMon             # length of putQ 

This program produces the following outputs (some lines may be formatted differently):

0 Got widget weights [5, 5, 5] 0 Got widget weights [5, 5, 5] 0 Got widget weights [5, 5, 5] 11 Got widget weights [5, 9, 7] 11 Got widget weights [9, 7, 9] 11 Got widget weights [7, 9, 7] 22 Got widget weights [9, 7, 9] 22 Got widget weights [7, 9, 7] 22 Got widget weights [9, 7, 9] 33 Got widget weights [7, 9, 7] 33 Got widget weights [9, 7, 9] 40 Got widget weights [7, 9, 7] 44 Got widget weights [9, 7, 9] 50 Got widget weights [7, 9, 7] LenBuffer: [[0, 10], [0, 7], [0, 9], [0, 11], [0, 8], [0, 10], [0, 7],     [10, 9], [10, 11], [11, 8], [11, 10], [11, 7], [11, 4],     [20, 6], [20, 8], [21, 10], [22, 7], [22, 4], [22, 1],     [30, 3], [30, 5], [31, 7], [33, 4], [33, 1],     [40, 3], [40, 0], [40, 2], [41, 4], [44, 1], [50, 3], [50, 0], [50, 2]] getQ: [[0, 0], [33, 1], [40, 0], [44, 1], [50, 0]] putQ [[0, 0], [0, 1], [0, 2], [0, 3], [0, 2], [0, 1], [0, 0], [10, 1],\     [11, 0]]