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

In [20]:
import numpy as np
import matplotlib.pyplot as plt
import math
import scipy.special as spsp

import scipy.stats as spst
import sympy

import simpy
In [21]:
def arrival():
lmbda=7

for i in range(4) :
inter_arrival=-1/lmbda*np.log(np.random.rand())
yield env.timeout(inter_arrival)
env.process(service(i))

def service(i):
request=server.request()
print(“user “, i+1, ” arrived and requested”)
print(” request id:”, request)
print(” users:”, server.users)
print(” queue:”, server.queue)
print(” tasks:”, env.__dict__[“_queue”])
print(” time:”,env.now, request.triggered, request.processed)
yield request
servicetime=env.timeout(0.2)
print(“user “, i+1, ” got server and starts the service”)
print(” users:”, server.users)
print(” queue:”, server.queue)
print(” tasks:”, env.__dict__[“_queue”])
print(” time:”,env.now, request.triggered, request.processed)
yield servicetime
print(“users”, i+1, “finished receiving service”)
print(” users:”, server.users)
print(” queue:”, server.queue)
print(” tasks:”, env.__dict__[“_queue”])
print(” time:”,env.now, request.triggered, request.processed)
x=server.release(request)
print(“users”, i+1, “left”)
print(” users:”, server.users)
print(” queue:”, server.queue)
print(” tasks:”, env.__dict__[“_queue”])
print(” time:”,env.now, request.triggered, request.processed)

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

env.run()

user 1 arrived and requested
request id:
users: []
queue: []
tasks: [(0.037093016021343565, 1, 4, ), (0.590680886262285, 1, 3, )]
time: 0.037093016021343565 True False
user 1 got server and starts the service
users: []
queue: []
tasks: [(0.23709301602134358, 1, 5, ), (0.590680886262285, 1, 3, )]
time: 0.037093016021343565 True True
users 1 finished receiving service
users: []
queue: []
tasks: [(0.590680886262285, 1, 3, )]
time: 0.23709301602134358 True True
users 1 left
users: []
queue: []
tasks: [(0.23709301602134358, 1, 6, ), (0.590680886262285, 1, 3, )]
time: 0.23709301602134358 True True
user 2 arrived and requested
request id:
users: []
queue: []
tasks: [(0.590680886262285, 1, 10, ), (0.6558610738370955, 1, 9, )]
time: 0.590680886262285 True False
user 2 got server and starts the service
users: []
queue: []
tasks: [(0.6558610738370955, 1, 9, ), (0.7906808862622849, 1, 11, )]
time: 0.590680886262285 True True
user 3 arrived and requested
request id:
users: []
queue: []
tasks: [(0.6971865265253847, 1, 13, ), (0.7906808862622849, 1, 11, )]
time: 0.6558610738370955 False False
user 4 arrived and requested
request id:
users: []
queue: [, ]
tasks: [(0.6971865265253847, 1, 15, ), (0.7906808862622849, 1, 11, )]
time: 0.6971865265253847 False False
users 2 finished receiving service
users: []
queue: [, ]
tasks: []
time: 0.7906808862622849 True True
users 2 left
users: []
queue: [, ]
tasks: [(0.7906808862622849, 1, 16, )]
time: 0.7906808862622849 True True
user 3 got server and starts the service
users: []
queue: []
tasks: [(0.9906808862622849, 1, 19, )]
time: 0.7906808862622849 True True
users 3 finished receiving service
users: []
queue: []
tasks: []
time: 0.9906808862622849 True True
users 3 left
users: []
queue: []
tasks: [(0.9906808862622849, 1, 20, )]
time: 0.9906808862622849 True True
user 4 got server and starts the service
users: []
queue: []
tasks: [(1.1906808862622849, 1, 23, )]
time: 0.9906808862622849 True True
users 4 finished receiving service
users: []
queue: []
tasks: []
time: 1.1906808862622849 True True
users 4 left
users: []
queue: []
tasks: [(1.1906808862622849, 1, 24, )]
time: 1.1906808862622849 True True
In [10]:

In [11]:
def arrival():
lmbda=7
for i in range(4) :
inter_arrival=-1/lmbda*np.log(np.random.rand())
yield env.timeout(inter_arrival)
env.process(service(i))

def service(i):
request=server.request()
yield request
servicetime=env.timeout(0.2)
yield servicetime
x=server.release(request)

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

env.run()