Debian Codegraph @

docs/examples/callCenter.py

raw symbols SCIP
1 """callCenter.py
2 Model shows use of get command with a filter function.
3
4 Scenario:
5 A call center runs around the clock. It has a number of agents online with
6 different skills/competences.
7 Calls by clients with different questions arrive at an expected rate of callrate
8 per minute (expo. distribution). An agent only deals with clients with questions
9 in his competence areas. The number of agents online and their skills remain constant --
10 when an agent goes offline, he is replaced by one withe thesame skills.
11 The expected service time tService[i] per question
12 follows an exponential distribution.
13 Clients are impatient and renege if they don't get service within time
14 tImpatience.
15
16 * Determine the waiting times of clients.
17 * Determine the percentage renegers
18 * Determine the percentage load on agents.
19 """
20 from SimPy.Simulation import *
21 import random as r
22 ## Model components -----------------------------------------------------------
23 class Client(Process):
24 def __init__(self,need):
25 Process.__init__(self)
26 self.need=need
27 def getServed(self,callCenter):
28 self.done=SimEvent()
29 callsWaiting=callCenter.calls
30 self.served=False
31 self.tArrive=now()
32 yield put,self,callsWaiting,[self]
33 yield hold,self,tImpatience
34 # get here either after renege or after interrupt of renege==successful call
35 if self.interrupted():
36 #success, got service
37 callCenter.renegeMoni.observe(success)
38 # wait for completion of service
39 yield waitevent,self,self.done
40 else:
41 #renege
42 callCenter.renegeMoni.observe(renege)
43 callsWaiting.theBuffer.remove(self)
44 callCenter.waitMoni.observe(now()-self.tArrive)
45 if callsWaiting.monitored:
46 callsWaiting.bufferMon.observe(y=len(callsWaiting.theBuffer))
47
48 class CallGenerator(Process):
49 def __init__(self,name,center):
50 Process.__init__(self,name)
51 self.buffer=center.calls
52 self.center=center
53 def generate(self):
54 while now()<=endTime:
55 yield hold,self,r.expovariate(callrate)
56 ran=r.random()
57 for aNeed in clientNeeds:
58 if ran<probNeeds[aNeed]:
59 need=aNeed
60 break
61 c=Client(need=need)
62 activate(c,c.getServed(callCenter=self.center))
63 class Agent(Process):
64 def __init__(self,name,skills):
65 Process.__init__(self,name)
66 self.skills=skills
67 self.busyMon=Monitor(name="Load on %s"%self.name)
68 def work(self,callCtr):
69 incoming=callCtr.calls
70 def mySkills(buffer):
71 ret=[]
72 for client in buffer:
73 if client.need in self.skills:
74 ret.append(client)
75 break
76 return ret
77 self.started=now()
78 while True:
79 self.busyMon.observe(idle)
80 yield get,self,incoming,mySkills
81 self.busyMon.observe(busy)
82 theClient=self.got[0]
83 callCtr.waitMoni.observe(now()-theClient.tArrive)
84 self.interrupt(theClient) # interrupt the timeout renege
85 yield hold,self,tService[theClient.need]
86 theClient.done.signal()
87
88 class Callcenter:
89 def __init__(self,name):
90 self.calls=Store(name=name,unitName="call",monitored=True)
91 self.waitMoni=Monitor("Caller waiting time")
92 self.agents=[]
93 self.renegeMoni=Monitor("Renegers")
94
95 renege=1
96 success=0
97 busy=1
98 idle=0
99
100 ## Experiment data ------------------------------------------------------------
101 centerName="SimCityBank"
102 clientNeeds=["loan","insurance","credit card","other"]
103 aSkills=[["loan"],["loan","credit card"],["insurance"],["insurance","other"]]
104 nrAgents={0:1,1:2,2:2,3:2} #skill:nr agents of that skill
105 probNeeds={"loan":0.1,"insurance":0.2,"credit card":0.5,"other":1.0}
106 tService={"loan":3.,"insurance":4.,"credit card":2.,"other":3.} # minutes
107 tImpatience=3 # minutes
108 callrate=7./10 # Callers per minute
109 endTime=10*24*60 # minutes (10 days)
110 r.seed(12345)
111
112 ## Model ----------------------------------------------------------------------
113 def model():
114 initialize()
115 callC=Callcenter(name=centerName)
116 for i in nrAgents.keys(): #loop over skills
117 for j in range(nrAgents[i]): # loop over nr agents of that skill
118 a=Agent(name="Agent type %s"%i,skills=aSkills[i])
119 callC.agents.append(a)
120 activate(a,a.work(callCtr=callC))
121 cg=CallGenerator(name="Call generator",center=callC)#buffer=callC.calls)
122 activate(cg,cg.generate())
123 simulate(until=endTime)
124 return callC
125
126 for tImpatience in (0.5,1.,2.,):
127 ## Experiment ------------------------------------------------------------------
128 callCenter=model()
129 ## Analysis/output -------------------------------------------------------------
130 print "\ntImpatience=%s minutes"%tImpatience
131 print "=================="
132 callCenter.waitMoni.setHistogram(low=0.0,high=float(tImpatience))
133 try:
134 print callCenter.waitMoni.printHistogram(fmt="%6.1f")
135 except:
136 pass
137 renegers=[1 for x in callCenter.renegeMoni.yseries() if x==renege]
138 print "\nPercentage reneging callers: %4.1f\n"\
139 %(100.0*sum(renegers)/callCenter.renegeMoni.count())
140 for agent in callCenter.agents:
141 print "Load on %s (skills= %s): %4.1f percent"\
142 %(agent.name,agent.skills,agent.busyMon.timeAverage()*100)
143
144