Debian Codegraph @

docs/examples/Market_OO.py

raw symbols SCIP
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 ## Model components ------------------------
10
11 class Customer(Process):
12 def __init__(self,sim):
13 Process.__init__(self,sim=sim)
14 # Randomly pick how many items this customer is buying
15 self.items = 1 + int(random.expovariate(1.0/AVGITEMS))
16 def checkout(self):
17 start = self.sim.now() # Customer decides to check out
18 yield request, self, self.sim.checkout_aisle
19 at_checkout = self.sim.now() # Customer gets to front of line
20 self.sim.waittime.tally(at_checkout-start)
21 yield hold, self, self.items*ITEMTIME
22 leaving = self.sim.now() # Customer completes purchase
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 ## Experiment data -------------------------
44
45 AISLES = 6 # Number of open aisles
46 ITEMTIME = 0.1 # Time to ring up one item
47 AVGITEMS = 20 # Average number of items purchased
48 CLOSING = 60*12 # Minutes from store open to store close
49 AVGCUST = 1500 # Average number of daily customers
50 RUNS = 8 # Number of times to run the simulation
51 SEED = 111333555 # seed value for random numbers
52
53
54 ## Model
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 ## Analysis/output -------------
70 print "Waiting time average: %.1f" % self.waittime.mean(), \
71 "(std dev %.1f, maximum %.1f)" % (sqrt(self.waittime.var()),self.waittime.max)
72 ## Experiment ------------------------------
73 MarketModel().runs()
74 print 'AISLES:', AISLES, ' ITEM TIME:', ITEMTIME