In [1]:
import simpy
import numpy as np
In [2]:
def arrival():
lmbda=10
i=0
while True:
inter_arrival=-1/lmbda*np.log(np.random.rand())
yield env.timeout(inter_arrival)
#one new arrival
i+=1
#this id will be shared with the serivce process
s1=env.process(service(i ))
env.process(call(i,s1))
print(i,”arrival”, env.now)
def call(i, s1):
#generating phone calls
while True:
yield env.timeout(0.2)
#if the customer is still waiting in line
if rqt_list[i-1].processed!=True:
s1.interrupt(“call received”)
#if the customer is no longer waiting in line, phone call not do anything anymore
else:
#break
return
def service(i ):
#requesting the server
rqt=desk.request()
rqt_list.append(rqt)
#occupy/process the server request
while True:
try:
yield rqt
break
except simpy.Interrupt:
print(i, “received a call”)
#get out the queue?
rqt.cancel()
#take the call
yield env.timeout(np.random.rand()*0.05)
rqt=desk.request()
rqt_list[i-1]=rqt
print(i, “start the service”, env.now)
yield env.timeout(0.4)
print(i, “end the service”, env.now)
desk.release(rqt)
np.random.seed(40)
env=simpy.Environment()
env.process(arrival())
rqt_list=[]
desk=simpy.Resource(env)
T=1
env.run(until=T)
1 arrival 0.08972554869790635
1 start the service 0.08972554869790635
2 arrival 0.37910443545365835
3 arrival 0.402862299539031
1 end the service 0.48972554869790635
2 start the service 0.48972554869790635
4 arrival 0.5275833261945984
5 arrival 0.6073562179341228
6 arrival 0.7264578267806205
7 arrival 0.7906273070468477
8 arrival 0.8378178967262908
9 arrival 0.8630782923838921
2 end the service 0.8897255486979063
3 start the service 0.8897255486979063
10 arrival 0.9007308384400814
11 arrival 0.9026553526669091
12 arrival 0.9536019920913013
13 arrival 0.9741853507929144
In [3]:
def arrival():
lmbda=10
i=0
while True:
inter_arrival=-1/lmbda*np.log(np.random.rand())
yield env.timeout(inter_arrival)
#one new arrival
i+=1
#this id will be shared with the serivce process
s1=env.process(service(i ))
env.process(call(i,s1))
print(i,”arrival”, env.now)
def call(i, s1):
#generating phone calls
while True:
yield env.timeout(0.2)
#if the customer is still waiting in line
if rqt_list[i-1].processed!=True:
s1.interrupt(“call received”)
#if the customer is no longer waiting in line, phone call not do anything anymore
else:
#break
return
def service(i ):
#requesting the server
rqt=desk.request()
rqt_list.append(rqt)
#occupy/process the server request
while True:
try:
yield rqt
break
except simpy.Interrupt:
print(i, “received a call”)
#get out the queue?
rqt.cancel()
#take the call
#if another phone call arrives, we ignore the new phone call and still talk to the previous person
Total_time=np.random.rand()*0.5
while True:
try:
start=env.now
yield env.timeout(Total_time)
break
except simpy.Interrupt:
Total_time-=env.now-start
rqt=desk.request()
rqt_list[i-1]=rqt
print(i, “start the service”, env.now)
yield env.timeout(0.4)
print(i, “end the service”, env.now)
desk.release(rqt)
np.random.seed(40)
env=simpy.Environment()
env.process(arrival())
rqt_list=[]
desk=simpy.Resource(env)
T=1
env.run(until=T)
1 arrival 0.08972554869790635
1 start the service 0.08972554869790635
2 arrival 0.37910443545365835
3 arrival 0.402862299539031
1 end the service 0.48972554869790635
2 start the service 0.48972554869790635
4 arrival 0.5275833261945984
3 received a call 0.602862299539031
5 arrival 0.6073562179341228
6 arrival 0.6715256982003499
7 arrival 0.718716287879793
4 received a call 0.7275833261945983
8 arrival 0.7439766835373943
9 arrival 0.745901197764222
10 arrival 0.7968478371886142
3 received a call 0.8028622995390311
5 received a call 0.8073562179341227
11 arrival 0.8174311958902273
12 arrival 0.8274942326888731
6 received a call 0.8715256982003499
2 end the service 0.8897255486979063
7 start the service 0.8897255486979063
13 arrival 0.9073661487122043
14 arrival 0.9253625613702876
4 received a call 0.9275833261945983
8 received a call 0.9439766835373944
9 received a call 0.945901197764222
10 received a call 0.9968478371886142
In [0]:
def arrival():
lmbda=10
i=0
while True:
inter_arrival=-1/lmbda*np.log(np.random.rand())
yield env.timeout(inter_arrival)
#one new arrival
i+=1
#this id will be shared with the serivce process
s1=env.process(service(i ))
env.process(call(i,s1))
print(i,”arrival”, env.now)
def call(i, s1):
#generating phone calls
while True:
yield env.timeout(0.2)
#if the customer is still waiting in line
if rqt_list[i-1].processed!=True:
s1.interrupt(“call received”)
#if the customer is no longer waiting in line, phone call not do anything anymore
else:
#break
return
def service(i ):
#requesting the server
rqt=desk.request()
rqt_list.append(rqt)
#occupy/process the server request
while True:
try:
yield rqt
break
except simpy.Interrupt:
print(i, “received a call”)
#get out the queue?
rqt.cancel()
#take the call
#if another phone call arrives, we ignore the new phone call and still talk to the previous person
Total_time=np.random.rand()*0.5
service=env.timeout(Total_time)
while True:
try:
yield service
break
except simpy.Interrupt:
pass
rqt=desk.request()
rqt_list[i-1]=rqt
print(i, “start the service”, env.now)
yield env.timeout(0.4)
print(i, “end the service”, env.now)
desk.release(rqt)
np.random.seed(40)
env=simpy.Environment()
env.process(arrival())
rqt_list=[]
desk=simpy.Resource(env)
T=1
env.run(until=T)
In [0]:
In [0]: