| 1 |
"""bakery_OO.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 |
|
| 24 |
class Bakery: |
| 25 |
def __init__(self,nrOvens,toMonitor,sim): |
| 26 |
self.stock = Level(name="baguette stock",monitored=toMonitor,sim=sim) |
| 27 |
for i in range(nrOvens): |
| 28 |
ov=Oven(sim=sim) |
| 29 |
sim.activate(ov,ov.bake(capacity=batchsize,bakery=self)) |
| 30 |
class Oven(Process): |
| 31 |
def bake(self,capacity,bakery): |
| 32 |
while self.sim.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 = self.sim.now() |
| 38 |
yield get,self,bakery.stock,r.choice(buy[cusType]) |
| 39 |
waits[cusType].append(self.sim.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 self.sim.now()<(tShopOpen+tBeforeOpen): |
| 45 |
c=Customer(cusType,sim=self.sim) |
| 46 |
self.sim.activate(c,c.buyBaguette(cusType,bakery=bakery)) |
| 47 |
|
| 48 |
class BakeryModel(Simulation): |
| 49 |
def run(self): |
| 50 |
|
| 51 |
self.initialize() |
| 52 |
toMoni = day==(nrDays-1) |
| 53 |
b = Bakery(nrOvens=nrOvens,toMonitor=toMoni,sim=self) |
| 54 |
for cType in ["retail","restaurant"]: |
| 55 |
cg = CustomerGenerator(sim=self) |
| 56 |
self.activate(cg,cg.generate(cusType=cType,bakery=b),delay=tBeforeOpen) |
| 57 |
self.simulate(until=tBeforeOpen+tShopOpen) |
| 58 |
return b |
| 59 |
|
| 60 |
nrOvens = 3 |
| 61 |
batchsize = 40 |
| 62 |
tBakeMin = 25/60.; tBakeMax=30/60. |
| 63 |
tArrivals = {"retail":1.0/40,"restaurant":1.0/4} |
| 64 |
buy = {"retail":[1,2,3],"restaurant":[20,40,60]} |
| 65 |
tShopOpen = 8; tBeforeOpen = 1; tEndBake = tBeforeOpen+tShopOpen |
| 66 |
nrDays=100 |
| 67 |
r=random.Random(12371) |
| 68 |
PLOTTING=True |
| 69 |
|
| 70 |
waits={} |
| 71 |
waits["retail"]=[]; waits["restaurant"]=[] |
| 72 |
bakMod = BakeryModel() |
| 73 |
for day in range(nrDays): |
| 74 |
bakery = bakMod.run() |
| 75 |
|
| 76 |
print 'bakery_OO' |
| 77 |
for cType in ["retail","restaurant"]: |
| 78 |
print "Average wait for %s customers: %4.2f hours"\ |
| 79 |
%(cType,(1.0*sum(waits[cType]))/len(waits[cType])) |
| 80 |
print "Longest wait for %s customers: %4.1f hours"%(cType,max(waits[cType])) |
| 81 |
nrLong = len([1 for x in waits[cType] if x>0.25]) |
| 82 |
nrCust = len(waits[cType]) |
| 83 |
print "Percentage of %s customers having to wait for more than 0.25 hours: %s"\ |
| 84 |
%(cType,100*nrLong/nrCust) |
| 85 |
|
| 86 |
|
| 87 |
if PLOTTING: |
| 88 |
plt = SimPlot() |
| 89 |
plt.plotStep(bakery.stock.bufferMon, |
| 90 |
title="Number of baguettes in stock during arbitrary day",color="blue") |
| 91 |
plt.mainloop() |