目录

37%法则 - "非诚勿扰" 中的博弈

数学模型

如何求解

定义问题,用python模拟解决

# -*- coding: utf-8 -*-
# by whyx 2017/1
 
import numpy as np
import pylab as pl
 
#初始化选择的丈夫列表
hus = []
 
#生成一个 1-100 的顺序
L = np.arange(1,101,1)
 
 
for k in range(10000):
#随机打乱排序
    np.random.shuffle(L)
#选出37个前面最大的一个
    m37 = max(L[0:37])
 
    found = False
 
#是否能够找到比m37 大的?
    for i in L[37:]:
        if i > m37 :
            found= True
            hus.append(i)
            break
#找不到, 就只能选择最后一个
    if not found:
        hus.append(L[99])
 
print hus
 
#pl.plot(hus)
#柱状图, 100个格子
pl.hist(hus, bins=100)
pl.show()
# -*- coding: utf-8 -*-
# by whyx 2017/1
 
import numpy as np
import pylab as pl
 
import mpl_toolkits.mplot3d
import matplotlib.pyplot as plt
 
def sel_hus(n=37, bin=100):
    #初始化选择的丈夫列表
    hus = []
    #生成一个 1-100 的顺序
    L = np.arange(1,101,1)
 
    for k in range(10000):
    #随机打乱排序
        np.random.shuffle(L)
    #选出37个前面最大的一个
        m37 = max(L[0:n])
 
        found = False
 
    #是否能够找到比m37 大的?
        for i in L[n:]:
            if i > m37 :
                found= True
                hus.append(i)
                break
    #找不到, 就只能选择最后一个
        if not found:
            hus.append(L[99])
 
    # print hus
    bins = np.histogram(hus, bins=bin)[0]
    return bins
 
 
#参数
#number of bins
bin = 100
#尝试的百分比
x_array = range(20,60,4)
 
#print (sel_hus(37))
 
x, y  = np.mgrid[20:60:4, 0:bin]
print len(x), x
print len(y), y
#g = np.exp(-x**2 - y**2)
#print g
 
z = []
for i in x_array:
#  z = z + (list(sel_hus(i)))
   z.append(sel_hus(i, bin))
 
print len(z), z
 
pltz = np.array(z)
z = pltz.reshape(len(x_array),bin)
print len(z), z
 
 
ax = plt.subplot(111, projection = '3d')
ax.plot_surface(x,y,z, rstride=2, cstride=1, cmap = plt.cm.Blues_r)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('n')
 
#ax.legend()
plt.show()