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

In [3]:
import simpy
import numpy as np
In [4]:
Outcome={“arrival”:[],”depart”:[]}

def arrival():
lmbda=7
i=0
while True:
inter_arrival=-1/lmbda*np.log(np.random.rand())
yield env.timeout(inter_arrival)
i=i+1
Outcome[“arrival”].append(env.now)
env.process(service(i ))

def service(i ):
#requesting the server
rqt=desk.request()
#occupy/process the server request
yield rqt
yield env.timeout(0.2)
desk.release(rqt)
Outcome[“depart”].append(env.now)

np.random.seed(10)
env=simpy.Environment()
desk=simpy.Resource(env)
env.process(arrival())

T=1
env.run(until=T)
Outcome
Out[4]:
{‘arrival’: [0.037093016021343565,
0.590680886262285,
0.6558610738370955,
0.6971865265253847,
0.796634758364468],
‘depart’: [0.23709301602134358, 0.7906808862622849, 0.9906808862622849]}
In [1]:

In [2]:
#generalized servers

Outcome={“arrival”:[],”depart”:[]}

def arrival():
lmbda=7
i=0
while True:
inter_arrival=-1/lmbda*np.log(np.random.rand())
yield env.timeout(inter_arrival)
i=i+1
Outcome[“arrival”].append(env.now)
env.process(service(i ))

def service(i ):
#requesting the server1 and server2
rqt1=desk1.request()
rqt2=desk2.request()
print(“1”,env.__dict__[“_queue”])
Event=rqt1|rqt2
print(Event.triggered,Event.processed)
print(“2”,env.__dict__[“_queue”])
#wait for the servers to become available
result=yield Event
print(“3”,env.__dict__[“_queue”])
print(Event.triggered,Event.processed)
#if server1 is able to accept the customer, but server 2 is not
if (rqt1 in result) and (rqt2 not in result):
#delete the request from server 2 queue list
rqt2.cancel()
#move on with server 1
yield env.timeout(0.2)
desk1.release(rqt1)

#if server2 is able to accept the customer, but server 1 is not
elif (rqt2 in result) and (rqt1 not in result):
#delete the request from server 2 queue list
rqt1.cancel()
#move on with server 1
yield env.timeout(0.2)
desk2.release(rqt2)
#if both servers are available:
else:
#send to server 1:
if np.random.rand()>0.5:
#delete the request from server 2 user list
desk2.release(rqt2)
#move on with server 1
yield env.timeout(0.2)
desk1.release(rqt1)
#send to server 2:
else:
#delete the request from server 1 user list
desk1.release(rqt1)
#move on with server 1
yield env.timeout(0.2)
desk2.release(rqt2)

Outcome[“depart”].append(env.now)

np.random.seed(10)
env=simpy.Environment()
## creating two servers
desk1=simpy.Resource(env)
desk2=simpy.Resource(env)

env.process(arrival())

T=1
env.run(until=T)
Outcome
In [0]: