Debian Codegraph @

docs/examples/bakery.py

raw symbols SCIP
1 """bakery.py
2 Scenario:
3 The Patisserie Francaise bakery has three ovens baking their renowned
4 baguettes for retail and restaurant customers. They start baking one
5 hour before the shop opens and stop at closing time.
6 They bake batches of 40 breads at a time,
7 taking 25..30 minutes (uniformly distributed) per batch. Retail customers
8 arrive at a rate of 40 per hour (exponentially distributed). They buy
9 1, 2 or 3 baguettes with equal probability. Restaurant buyers arrive
10 at a rate of 4 per hour (exponentially dist.). They buy 20,40 or 60
11 baguettes with equal probability.
12 Simulate this operation for 100 days of 8 hours shop opening time.
13 a) What is the mean waiting time for retail and restaurant buyers?
14 b) What is their maximum waiting time?
15 b) What percentage of customer has to wait longer than 15 minutes??
16 c) Plot the number of baguettes over time for an arbitrary day.
17 (use PLOTTING=True to do this)
18
19 """
20 from SimPy.Simulation import *
21 from SimPy.SimPlot import *
22 import random
23 ## Model components
24 class Bakery:
25 def __init__(self,nrOvens,toMonitor):
26 self.stock=Level(name="baguette stock",monitored=toMonitor)
27 for i in range(nrOvens):
28 ov=Oven()
29 activate(ov,ov.bake(capacity=batchsize,bakery=self))
30 class Oven(Process):
31 def bake(self,capacity,bakery):
32 while now()+tBakeMax<tEndBake:
33 yield hold,self,r.uniform(tBakeMin,tBakeMax)
34 yield put,self,bakery.stock,capacity
35 class Customer(Process):
36 def buyBaguette(self,cusType,bakery):
37 tIn=now()
38 yield get,self,bakery.stock,r.choice(buy[cusType])
39 waits[cusType].append(now()-tIn)
40 class CustomerGenerator(Process):
41 def generate(self,cusType,bakery):
42 while True:
43 yield hold,self,r.expovariate(1.0/tArrivals[cusType])
44 if now()<(tShopOpen+tBeforeOpen):
45 c=Customer(cusType)
46 activate(c,c.buyBaguette(cusType,bakery=bakery))
47 ## Model
48 def model():
49 toMonitor=False
50 initialize()
51 if day==(nrDays-1): toMoni=True
52 else: toMoni=False
53 b=Bakery(nrOvens=nrOvens,toMonitor=toMoni)
54 for cType in ["retail","restaurant"]:
55 cg=CustomerGenerator()
56 activate(cg,cg.generate(cusType=cType,bakery=b),delay=tBeforeOpen)
57 simulate(until=tBeforeOpen+tShopOpen)
58 return b
59 ## Experiment data
60 nrOvens=3
61 batchsize=40 #nr baguettes
62 tBakeMin=25/60.; tBakeMax=30/60. #hours
63 tArrivals={"retail":1.0/40,"restaurant":1.0/4} #hours
64 buy={"retail":[1,2,3],"restaurant":[20,40,60]} #nr baguettes
65 tShopOpen=8; tBeforeOpen=1; tEndBake=tBeforeOpen+tShopOpen #hours
66 nrDays=100
67 r=random.Random(12371)
68 PLOTTING=True
69 ## Experiment
70 waits={}
71 waits["retail"]=[]; waits["restaurant"]=[]
72 for day in range(nrDays):
73 bakery=model()
74 ## Analysis/output
75 print 'bakery'
76 for cType in ["retail","restaurant"]:
77 print "Average wait for %s customers: %4.2f hours"\
78 %(cType,(1.0*sum(waits[cType]))/len(waits[cType]))
79 print "Longest wait for %s customers: %4.1f hours"%(cType,max(waits[cType]))
80 nrLong=len([1 for x in waits[cType] if x>0.25])
81 nrCust=len(waits[cType])
82 print "Percentage of %s customers having to wait for more than 0.25 hours: %s"\
83 %(cType,100*nrLong/nrCust)
84
85
86 if PLOTTING:
87 plt=SimPlot()
88 plt.plotStep(bakery.stock.bufferMon,
89 title="Number of baguettes in stock during arbitrary day",color="blue")
90 plt.mainloop()