尝试通过进行Monte Carlo模拟(不使用e)在python中编写秘书问题。问题的实质在这里:https : //en.wikipedia.org/wiki/Secretary_problem
描述为:想象一个管理员想要从n个可排名的申请人中聘请最好的秘书来担任职位。申请人按随机顺序逐一面试。面试后应立即就每个特定申请人做出决定。一旦被拒绝,申请人将无法被召回。在面试过程中,管理员可以将申请人划分为目前为止所有面试的申请人,但不知道尚未见面的申请人的质量。问题是关于最大化选择最佳申请人的可能性的最佳策略(停止规则)。摘自:https : //www.geeksforgeeks.org/secretary-problem-optimal-stopping-problem/
到目前为止,这是我的python代码:
n = 7; # of applicants
m = 10000; # of repeats
plot = np.zeros(1);
for i in range (2,m): #multiple runs
array = np.random.randint(1,1000,n);
for j in range(2,n): #over range of array
test = 0;
if array[j] > array[1] and array[j] == array.max():
plot=plot+1
test = 1;
break
if array[j]> array[1]:
test = 2;
break
print(plot/m)
print(array)
print("j = ",j)
print("test = ",test)
我在这里的代码做错了,我无法复制表。在上面的代码中,我尝试完成7 =申请人数,并在'2'之后选出最佳申请人。
plot/m
在给出申请人数的情况下,应当在第三栏中输出百分比,并且“紧随其后”。
回答了!如下。
附加代码:
import numpy as np
import matplotlib.pyplot as plt
import time
plt.style.use('seaborn-whitegrid')
n = 150 #total number of applicants
nplot = np.empty([1,1])
#take = 3 #not necessary, turned into J below:
for k in range(2,n):
m = 10000 #number of repeats
plot = np.empty([1,1]);
for j in range(1,k):
passed = 0
for i in range (0,m): #multiple runs
array = np.random.rand(k);
picked = np.argmax(array[j:]>max(array[0:j])) + j
best = np.argmax(array)
if best == picked:
passed = passed+1
#print(passed/m)
plot = np.append(plot,[passed/m])
#print(plot)
plot = plot[1:];
x = range(1,k);
y = plot
#print("N = ",k)
print("Check ",plot.argmax()," if you have ",k," applicants", round(100* plot.max(),2),"% chance of finding the best applicant")
nplot =np.append(nplot,plot.max())
# Plot:
nplot = nplot[1:];
x = range(2,n);
y = nplot
plt.plot(x, y, 'o', color='black');
plt.xlabel("Number of Applicants")
plt.ylabel("Probability of Best Applicant")
这似乎可以完成工作,并且比较简单。评论:
因此:
import numpy as np
n = 7
take = 2
m = 100000
passed = 0
for i in range (0,m): #multiple runs
array = np.random.rand(n);
picked = np.argmax(array[take:]>max(array[0:take])) + take
best = np.argmax(array)
if best == picked:
passed = passed+1
print(passed/m)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句