22import inspect
33
44from axelrod import Actions , Player , RoundRobin , update_history
5+ from .cycler import Cycler
56
67C , D = Actions .C , Actions .D
78
9+ def limited_simulate_play (player_1 , player_2 , h1 ):
10+ """Here we want to replay player_1's history to player_2, allowing
11+ player_2's strategy method to set any internal variables as needed. If you
12+ need a more complete simulation, see `simulate_play` in player.py. This
13+ function is specifically designed for the needs of MindReader."""
14+ h2 = player_2 .strategy (player_1 )
15+ update_history (player_1 , h1 )
16+ update_history (player_2 , h2 )
17+
818def simulate_match (player_1 , player_2 , strategy , rounds = 10 ):
919 """Simulates a number of matches."""
1020 for match in range (rounds ):
11- play_1 , play_2 = strategy , player_2 .strategy (player_1 )
12- # Update histories and counts
13- update_history (player_1 , play_1 )
14- update_history (player_2 , play_2 )
15-
16- def roll_back_history (player , rounds ):
17- """Undo the last `rounds` rounds as sufficiently as possible."""
18- for i in range (rounds ):
19- play = player .history .pop (- 1 )
20- if play == C :
21- player .cooperations -= 1
22- elif play == D :
23- player .defections -= 1
21+ limited_simulate_play (player_1 , player_2 , strategy )
2422
2523def look_ahead (player_1 , player_2 , game , rounds = 10 ):
2624 """Looks ahead for `rounds` and selects the next strategy appropriately."""
@@ -29,14 +27,16 @@ def look_ahead(player_1, player_2, game, rounds=10):
2927 # Simulate plays for `rounds` rounds
3028 strategies = [C , D ]
3129 for strategy in strategies :
32- opponent_ = copy . deepcopy ( player_2 ) # need deepcopy here
33- round_robin = RoundRobin ( players = [ player_1 , opponent_ ], game = game ,
34- turns = rounds )
35- simulate_match ( player_1 , opponent_ , strategy , rounds )
36- results . append ( round_robin . _calculate_scores ( player_1 , opponent_ )[ 0 ] )
30+ # Instead of a deepcopy, create a new opponent and play out the history
31+ opponent_ = player_2 . clone ()
32+ player_ = Cycler ( strategy ) # Either cooperator or defector
33+ for h1 in player_1 . history :
34+ limited_simulate_play ( player_ , opponent_ , h1 )
3735
38- # Restore histories and counts
39- roll_back_history (player_1 , rounds )
36+ round_robin = RoundRobin (players = [player_ , opponent_ ], game = game ,
37+ turns = rounds )
38+ simulate_match (player_ , opponent_ , strategy , rounds )
39+ results .append (round_robin ._calculate_scores (player_ , opponent_ )[0 ])
4040
4141 return strategies [results .index (max (results ))]
4242
0 commit comments