博弈论(1)——枪手博弈
枪手博弈的规则:各枪手向当前存活的枪手中命中率最高的枪手开枪,最厉害的枪手先开枪然后从强向弱轮换。
1 代码实现
下面是枪手博弈的python运行代码:
1.1 需要引用的工具包:
import copy
import random
1.2 枪手博弈函数主体:
def Gunner_game(Game_num, Gunner_num=3, max_hit_rate=0.9, min_hit_rate=0.1):
hitrate_interval = (max_hit_rate - min_hit_rate)/(Gunner_num + 1)
Gunner_list = [(Gunner_id, max_hit_rate - hitrate_interval * (Gunner_id + 1)) for Gunner_id in range(Gunner_num)]
ret = []
for exma_num in range(Game_num):
losser_list = []
winer = None
while len(Gunner_list) - len(losser_list) > 1:
for Gunner_id, Hit_rate in Gunner_list:
if (Gunner_id, Hit_rate) in losser_list:
continue
Gunner_list_x = copy.deepcopy(Gunner_list)
Gunner_list_x.remove((Gunner_id, Hit_rate))
for losser_x in losser_list:
Gunner_list_x.remove(losser_x)
Gunner_list_x.sort(key=lambda x: x[1], reverse=True)
Gunner_target = Gunner_list_x.pop(0)
if random.random() <= Hit_rate:
losser_list.append(Gunner_target)
if len(Gunner_list) - len(losser_list) == 1:
winer = (Gunner_id, Hit_rate)
break
ret.append(winer[0])
for Gunner_id, Hit_rate in Gunner_list:
winnum = len([retx for retx in ret if retx == Gunner_id])
print(f'Guner_{Gunner_id};hit_rate is:{Hit_rate}; win_num is :{winnum};win rate:{winnum/Game_num}')
1.3 main函数
if __name__ == "__main__":
Gunner_game(100000, 3, 1.0, 0.5)
2 实验结果
2.1 代码输出
Guner_0;hit_rate is:0.875; win_num is :30528;win rate:0.30528
Guner_1;hit_rate is:0.75; win_num is :4594;win rate:0.04594
Guner_2;hit_rate is:0.625; win_num is :64878;win rate:0.64878
2.2 结果分析
实验结果显示,100000组实验的结果中,老三的虽然命中率最差,但胜率最高。老二最惨,胜率只有0.045。