| 1 |
""" Market_OO.py |
| 2 |
Model of a supermarket. |
| 3 |
""" |
| 4 |
from SimPy.Simulation import * |
| 5 |
import random |
| 6 |
from math import sqrt |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
class Customer(Process): |
| 12 |
def __init__(self,sim): |
| 13 |
Process.__init__(self,sim=sim) |
| 14 |
|
| 15 |
self.items = 1 + int(random.expovariate(1.0/AVGITEMS)) |
| 16 |
def checkout(self): |
| 17 |
start = self.sim.now() |
| 18 |
yield request, self, self.sim.checkout_aisle |
| 19 |
at_checkout = self.sim.now() |
| 20 |
self.sim.waittime.tally(at_checkout-start) |
| 21 |
yield hold, self, self.items*ITEMTIME |
| 22 |
leaving = self.sim.now() |
| 23 |
self.sim.checkouttime.tally(leaving-at_checkout) |
| 24 |
yield release, self, self.sim.checkout_aisle |
| 25 |
|
| 26 |
class Customer_Factory(Process): |
| 27 |
def run(self): |
| 28 |
while 1: |
| 29 |
c = Customer(sim=self.sim) |
| 30 |
self.sim.activate(c, c.checkout()) |
| 31 |
arrival = random.expovariate(float(AVGCUST)/CLOSING) |
| 32 |
yield hold, self, arrival |
| 33 |
|
| 34 |
class Monitor2(Monitor): |
| 35 |
def __init__(self,sim): |
| 36 |
Monitor.__init__(self,sim=sim) |
| 37 |
self.min, self.max = (sys.maxint,0) |
| 38 |
def tally(self, x): |
| 39 |
self.observe(x) |
| 40 |
self.min = min(self.min, x) |
| 41 |
self.max = max(self.max, x) |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
AISLES = 6 |
| 46 |
ITEMTIME = 0.1 |
| 47 |
AVGITEMS = 20 |
| 48 |
CLOSING = 60*12 |
| 49 |
AVGCUST = 1500 |
| 50 |
RUNS = 8 |
| 51 |
SEED = 111333555 |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
class MarketModel(Simulation): |
| 56 |
def runs(self): |
| 57 |
|
| 58 |
random.seed(SEED) |
| 59 |
print 'Market' |
| 60 |
for run in range(RUNS): |
| 61 |
self.initialize() |
| 62 |
self.waittime = Monitor2(sim=self) |
| 63 |
self.checkouttime = Monitor2(sim=self) |
| 64 |
self.checkout_aisle = Resource(capacity=AISLES,sim=self) |
| 65 |
|
| 66 |
cf = Customer_Factory(sim=self) |
| 67 |
self.activate(cf, cf.run(), 0.0) |
| 68 |
self.simulate(until=CLOSING) |
| 69 |
|
| 70 |
print "Waiting time average: %.1f" % self.waittime.mean(), \ |
| 71 |
"(std dev %.1f, maximum %.1f)" % (sqrt(self.waittime.var()),self.waittime.max) |
| 72 |
|
| 73 |
MarketModel().runs() |
| 74 |
print 'AISLES:', AISLES, ' ITEM TIME:', ITEMTIME |