简体   繁体   English

编写秘书问题 (Monte Carlo) - python 代码问题

[英]Coding a secretary problem (Monte Carlo) - problems with python code

Trying to code the secretary problem in python by doing a Monte Carlo simulation (without using e).尝试通过进行蒙特卡罗模拟(不使用 e)来编写 python 中的秘书问题。 The essence of the problem is here: https://en.wikipedia.org/wiki/Secretary_problem问题的本质在这里: https://en.wikipedia.org/wiki/Secretary_problem

Described as:Imagine an administrator who wants to hire the best secretary out of n rankable applicants for a position.描述为:想象一个管理员想从 n 个可排名的申请人中聘请最好的秘书来申请 position。 The applicants are interviewed one by one in random order.应聘者以随机顺序一一进行面试。 A decision about each particular applicant is to be made immediately after the interview.面试后将立即对每个特定申请人做出决定。 Once rejected, an applicant cannot be recalled.一旦被拒绝,申请人不能被召回。 During the interview, the administrator can rank the applicant among all applicants interviewed so far but is unaware of the quality of yet unseen applicants.在面试过程中,管理员可以在迄今为止面试的所有申请人中对申请人进行排名,但不知道尚未见过的申请人的质量。 The question is about the optimal strategy (stopping rule) to maximize the probability of selecting the best applicant.问题是关于最大化选择最佳申请人的概率的最佳策略(停止规则)。 Taken from: https://www.geeksforgeeks.org/secretary-problem-optimal-stopping-problem/取自: https://www.geeksforgeeks.org/secretary-problem-optimal-stopping-problem/

Table that I'm checking my code against:我正在检查我的代码的表: 从算法到生活

Here is my python code so far:到目前为止,这是我的 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)

I am doing something wrong with my code here that I'm unable to replicate the table.我在这里的代码做错了,我无法复制表。 In the above code I've tried to do 7 = number of applicants and take the best applicant after '2'.在上面的代码中,我尝试做 7 = 申请人数,并在“2”之后选择最好的申请人。

The plot/m should output the percentage in column three given the number of applicants and 'take the best after'. plot/m应该 output 第三列中的百分比给定申请人的数量和“采取最好的之后”。

Answered.回答。 As below.如下。

Additional code:附加代码:

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")

Here is something that seems to do the job and is a bit simpler.这似乎可以完成这项工作并且更简单一些。 Comments:注释:

  1. Use argmax to determine who is the best secretary, or to pick the first that has a better grade than another group使用 argmax 来确定谁是最好的秘书,或者选择第一个比另一组成绩更好的秘书
  2. Draw from a real-valued function to reduce the odds of having 2 secretaries having the same grade.从实值 function 中提取,以减少 2 个秘书具有相同等级的几率。

Hence:因此:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM