2017-10-08 70 views
1

使用python axelrod庫,我正在通過以下(和優秀)的博客爲我自己:http://mojones.net/evolving-strategies-for-an-iterated-prisoners-dilemma-tournament.html。在嘗試實施lookerup策略時,出現以下錯誤:我如何解決蟒蛇axelrod Lookerup策略中的關鍵錯誤

KeyError: Plays(self_plays=(C, C), op_plays=(C, D), op_openings=(C, D)) 

如何解決此錯誤?我是否錯誤地實施了lookerup策略?我試圖通過github上的策略代碼來追蹤錯誤,但我只是沒有看到這個問題。以下代碼提供了我的問題的示例。如果我換掉另一個(例如另一個交流發電機)的lookerup策略,則axelrod遊戲會按我的預期執行。

import axelrod 
import random 
import itertools 
def get_random_table(): 
    strings = [''.join(x) for x in itertools.product('CD', repeat=2)] 
    keys = list(itertools.product(strings, strings, strings)) 
    values = ''.join([random.choice('CD') for _ in keys]) 
    return dict(zip(keys, values)) 
player_1 = axelrod.LookerUp(random_table) 
#player_1 = axelrod.Alternator() 
player_2 = axelrod.Alternator() 

g = axelrod.Game() 
iterations = 10 
for turn in range(iterations): 
    player_1.play(player_2) 

(其它標記可能包括「阿克塞爾羅德」和「prisoner's-困境」。)

+0

您的示例代碼已損壞:未定義'random_table'。 – lxop

回答

1

的問題是,您使用字符串來表示的動作,當你需要使用Action秒。修復很簡單:

import axelrod 
from axelrod.action import Action 
import itertools 
import random 


def get_random_table(): 
    action_pairs = list(itertools.product((Action.C, Action.D), repeat=2)) # <- Action 
    keys = list(itertools.product(action_pairs, action_pairs, action_pairs)) 
    values = [random.choice((Action.C, Action.D)) for _ in keys] # <- Actions instead of strings 
    return dict(zip(keys, values)) 


player_1 = axelrod.LookerUp(get_random_table()) 
# player_1 = axelrod.Alternator() 
player_2 = axelrod.Alternator() 

g = axelrod.Game() 
iterations = 10 
for turn in range(iterations): 
    player_1.play(player_2)