File: Movie_renege.py

package info (click to toggle)
python-simpy 2.3.1%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 11,868 kB
  • sloc: python: 11,171; makefile: 143
file content (68 lines) | stat: -rw-r--r-- 2,151 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"""
Movie_renege

Demo program to show event-based reneging by 
'yield (request,self,res),(waitevent,self,evt)'.

Scenario:
A movie theatre has one ticket counter selling tickets for 
three movies (next show only). When a movie is sold out, all
people waiting to buy ticket for that movie renege (leave queue).
"""
from SimPy.Simulation import *
from random import *

## Model components ------------------------
class MovieGoer(Process):
    def getTickets(self,whichMovie,nrTickets):
        yield (request,self,ticketCounter),(waitevent,self,soldOut[whichMovie])
        if self.acquired(ticketCounter):
            if available[whichMovie]>=nrTickets:
                available[whichMovie]-=nrTickets
                if available[whichMovie]<2:
                    soldOut[whichMovie].signal()
                    whenSoldOut[whichMovie]=now()
                    available[whichMovie]=0
                yield hold,self,1
            else:
                yield hold,self,0.5
            yield release,self,ticketCounter
        else:
            nrRenegers[whichMovie]+=1
  
class CustomerArrivals(Process):
    def traffic(self):
        while now()<120:
            yield hold,self,expovariate(1/0.5)
            movieChoice=choice(movies)
            nrTickets=randint(1,6)        
            if available[movieChoice]:
                m=MovieGoer()
                activate(m,m.getTickets(whichMovie=movieChoice,nrTickets=nrTickets))
        
## Experiment data -------------------------
seed(111333555)
movies=["Gone with the Windows","Hard Core Dump","Modern CPU Times"]

available={}
soldOut={}
nrRenegers={}
whenSoldOut={}
for film in movies:
    available[film]=50
    soldOut[film]=SimEvent(film)
    nrRenegers[film]=0
ticketCounter=Resource(capacity=1)

## Model/Experiment ------------------------------
print 'Movie_renege'
initialize()
c=CustomerArrivals()
activate(c,c.traffic())
simulate(until=120)

for f in movies:
    if soldOut[f]:
        print "Movie '%s' sold out %.0f minutes after ticket counter opening."%(f,int(whenSoldOut[f]))
        print "\tNr people leaving queue when film '%s' sold out: %s"%(f,nrRenegers[f])