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

In [16]:
import numpy as np
import hyperopt
import simpy
import scipy.stats as spst
In [17]:
lmbda = 20
T = 2

def generate_arrivals(u=None):
if u is None:
u = np.random.rand()

n = int(spst.poisson.ppf(u, lmbda*T))
arr = np.insert(np.sort(np.random.rand(n)*T), 0, 0)
return np.diff(arr), u

def arrivals(env, data):
for a in data[‘arrivals’]:
yield env.timeout(a)
if len(data[‘professor’].queue) > 3:
data[‘abandoned’] += 1
continue
env.process(service(env, data))

def service(env, data):
rqt = data[‘professor’].request()
yield rqt

num_questions = int(np.random.rand()*4 + 2)
time = 0
for i in range(num_questions):
time += -1/data[‘e’] * np.log(np.random.rand())

yield env.timeout(time)

def system(u=None,e=20):
env = simpy.Environment()
data = {
‘professor’: simpy.Resource(env),
‘abandoned’: 0,
‘e’: e
}

data[‘arrivals’], u = generate_arrivals(u)

env.process(arrivals(env, data))
env.run(until=T)

return data[‘abandoned’], u

def antithetic():
abandoned_1, u = system()
abandoned_2, _ = system(1 – u)
return (abandoned_1 + abandoned_2) / 2

def objective(e):
output = [system(e=e)[0] for i in range(100)]
s = np.mean(output)
return -1* (100 – 5*s – .05*e**2)

best = hyperopt.fmin(fn=objective,
space=hyperopt.hp.uniform(‘e’, 0, 40),
algo=hyperopt.tpe.suggest,
max_evals=100)

print(best)

100%|██████████| 100/100 [00:02<00:00, 36.86trial/s, best loss: 70.5430134878673] {'e': 3.8549020425097282}