Debian Codegraph @

docs/examples/Market.py

raw symbols SCIP
1 """ Market.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):
13 Process.__init__(self)
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 = now() # Customer decides to check out
18 yield request, self, checkout_aisle
19 at_checkout = now() # Customer gets to front of line
20 waittime.tally(at_checkout-start)
21 yield hold, self, self.items*ITEMTIME
22 leaving = now() # Customer completes purchase
23 checkouttime.tally(leaving-at_checkout)
24 yield release, self, checkout_aisle
25
26 class Customer_Factory(Process):
27 def run(self):
28 while 1:
29 c = Customer()
30 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):
36 Monitor.__init__(self)
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/Experiment ------------------------------
55
56 random.seed(SEED)
57 print 'Market'
58 for run in range(RUNS):
59 waittime = Monitor2()
60 checkouttime = Monitor2()
61 checkout_aisle = Resource(AISLES)
62 initialize()
63 cf = Customer_Factory()
64 activate(cf, cf.run(), 0.0)
65 simulate(until=CLOSING)
66
67 print "Waiting time average: %.1f" % waittime.mean(), \
68 "(std dev %.1f, maximum %.1f)" % (sqrt(waittime.var()),waittime.max)
69
70 ## Analysis/output -------------------------
71
72 print 'AISLES:', AISLES, ' ITEM TIME:', ITEMTIME