資源簡介
囚徒困境的演化博弈的python實現,代碼可實現演化過程的圖像演示,可設置是否可以合作及背叛等條件,如有需要可自行下載,歡迎討論。

代碼片段和文件信息
#?-*-?coding:?utf-8?-*-
#?!/usr/bin/python3
import?random
from?collections?import?Counter
import?matplotlib.pyplot?as?plt
import?sys
import?inspect
BETRAY?=?True
COOPERATE?=?False
#?定義策略母類
class?Alg:
????def?get_score(self):
????????if?len(self.score)>50:
????????????self.score?=?[sum(self.score)]
????????return?self.score
????def?__init__(self):
????????self.my_history?=?[]
????????self.their_history?=?[]
????????self.score?=?[]
????def?play(self):
????????“““?@param?my_history?List?of?own?last?choices
????????????@param?their_history?List?of?last?choices?of?the?other
????????????@return?decision?{BETRAY?SILENT}
????????“““
????????raise?NotImplementedError
????def?__repr__(self):
????????return?“{}“.format(type(self).__name__)
????def?reinit_history(self):
????????self.my_history?=?[]
????????self.their_history?=?[]
????????return?self
def?partition(ls?size):
????“““
????Returns?a?new?list?with?elements
????of?which?is?a?list?of?certain?size.
????????>>>?partition([1?2?3?4]?3)
????????[[1?2?3]?[4]]
????“““
????return?[ls[i:i?+?size]?for?i?in?range(0?len(ls)?size)]
def?score(A_choice?B_choice):
????if?A_choice?==?B_choice:
????????if?A_choice?==?BETRAY:
????????????return?(1?1)
????????return?(3?3)
????if?A_choice?==?BETRAY:
????????return?(5?0)
????return?(0?5)
def?play(A?B):
????A_score?=?0
????B_score?=?0
????A_choice?=?A.play(A.my_history?B.my_history)
????B_choice?=?B.play(B.my_history?A.my_history)
????A_score_?B_score_?=?score(A_choice?B_choice)
????A.my_history.append(A_choice)
????B.my_history.append(B_choice)
????A.their_history.append(B_choice)
????B.their_history.append(A_choice)
????A.score.append(A_score_)
????B.score.append(B_score_)
????return?(A_score_?B_score_)
#?一期博弈過程,即所有樣本隨機兩兩匹配后博弈rounds次
def?simulate(persons?rounds?seed=42):
????“““
????:param?persons:?所有參與人對象列表
????:param?strategy_score:?不同策略得分,初始均為0
????:param?seed:
????:param?rounds:?當前期博弈次數
????:return:?strategy_score
????“““
????random.shuffle(persons)
????#?print?(persons)
????persons_pairs?=?partition(persons?2)
????#?print(partition(persons?2))
????for?i?in?range(rounds):
????????#?tmp?=?[]
????????for?pair?in?persons_pairs:
????????????play(pair[0]?pair[1])
#?自我繁殖過程,每隔1期進行一次自我繁殖
def?self_reproduction(persons?mutation_rate?out_rate):
????“““
????:param?persons:?博弈后樣本列表
????:param?mutation_rate:?變異率
????:param?out_rate:?淘汰率
????:return:?自我繁殖后樣本列表
????“““
????mutation_count?=?int(len(persons)?*?mutation_rate)
????out_count?=?int(len(persons)?*?out_rate)
????for?person?in?persons:
????????score_list?=?[sum(person.get_score())?for?person?in?persons]
????persons.sort(key=lambda?x:?sum(x.get_score())?reverse=True)
????#?print?(persons_sorted)
????score_sorted?=?[sum(person.get_score())?for?person?in?persons]
????#?獲取淘汰樣本及變異樣本
????out_persons?=?persons[-out_count:]
????random.shuffle(out_persons)
????mutation
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????11705??2019-11-06?20:38??run.py
- 上一篇:數據處理程序
- 下一篇:抓取CSDN博客文章的簡單爬蟲python源碼
評論
共有 條評論