| 1 |
from random import normalvariate, seed |
| 2 |
from SimPy.Simulation import (Level, Process, activate, get, initialize, hold, |
| 3 |
now, put, simulate) |
| 4 |
|
| 5 |
|
| 6 |
class Deliver(Process): |
| 7 |
def deliver(self): |
| 8 |
while True: |
| 9 |
lead = 10.0 |
| 10 |
delivery = 10.0 |
| 11 |
yield put, self, stock, delivery |
| 12 |
print('at %6.4f, add %6.4f units, now amount = %6.4f' % |
| 13 |
(now(), delivery, stock.amount)) |
| 14 |
yield hold, self, lead |
| 15 |
|
| 16 |
|
| 17 |
class Demand(Process): |
| 18 |
stockout = 0.0 |
| 19 |
|
| 20 |
def demand(self): |
| 21 |
day = 1.0 |
| 22 |
while True: |
| 23 |
yield hold, self, day |
| 24 |
dd = normalvariate(1.20, 0.20) |
| 25 |
ds = dd - stock.amount |
| 26 |
|
| 27 |
if dd > stock.amount: |
| 28 |
yield get, self, stock, stock.amount |
| 29 |
|
| 30 |
self.stockout += ds |
| 31 |
|
| 32 |
print('day %6.4f, demand = %6.4f, shortfall = %6.4f' % |
| 33 |
(now(), dd, -ds)) |
| 34 |
else: |
| 35 |
yield get, self, stock, dd |
| 36 |
print('day %6.4f, supplied %6.4f, now amount = %6.4f' % |
| 37 |
(now(), dd, stock.amount)) |
| 38 |
|
| 39 |
|
| 40 |
stock = Level(monitored=True) |
| 41 |
|
| 42 |
seed(99999) |
| 43 |
initialize() |
| 44 |
|
| 45 |
offeror = Deliver() |
| 46 |
activate(offeror, offeror.deliver()) |
| 47 |
requester = Demand() |
| 48 |
activate(requester, requester.demand()) |
| 49 |
|
| 50 |
simulate(until=49.9) |
| 51 |
|
| 52 |
result = (stock.bufferMon.mean(), requester.stockout) |
| 53 |
print('') |
| 54 |
print('Summary of results through end of day %6.4f:' % int(now())) |
| 55 |
print('average stock = %6.4f, cumulative stockout = %6.4f' % result) |