CS计算机代考程序代写 In [5]:

In [5]:
import numpy as np
import simpy
In [6]:
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
env.process(service(i ))
print(i,”arrival”, env.now)

def service(i ):
#define priority
if np.random.rand()>0.5:
priority=1
else:
priority=0
#preempt
if np.random.rand()>0.5:
preempt=True
else:
preempt=False

#requesting the server
rqt=desk.request(priority=priority,preempt=preempt)
#occupy/process the server request
yield 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(50)
env=simpy.Environment()
env.process(arrival())

desk=simpy.PreemptiveResource(env)

T=1
env.run(until=T)

1 arrival 0.07040025968926293
1 start the service 0.07040025968926293
2 arrival 0.21820478201329913
3 arrival 0.31567224578616293
4 arrival 0.34156305086309896
5 arrival 0.4475384059566313
6 arrival 0.45030999357807466
1 end the service 0.47040025968926297
2 start the service 0.47040025968926297
7 arrival 0.5662766434066673
8 arrival 0.6601590526820138
9 arrival 0.6676313073214255
2 end the service 0.870400259689263
3 start the service 0.870400259689263
10 arrival 0.9627862138217247
In [7]:

In [8]: