2
2
import inspect
3
3
4
4
from axelrod import Actions , Player , RoundRobin , update_history
5
+ from .cycler import Cycler
5
6
6
7
C , D = Actions .C , Actions .D
7
8
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
+
8
18
def simulate_match (player_1 , player_2 , strategy , rounds = 10 ):
9
19
"""Simulates a number of matches."""
10
20
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 )
24
22
25
23
def look_ahead (player_1 , player_2 , game , rounds = 10 ):
26
24
"""Looks ahead for `rounds` and selects the next strategy appropriately."""
@@ -29,14 +27,16 @@ def look_ahead(player_1, player_2, game, rounds=10):
29
27
# Simulate plays for `rounds` rounds
30
28
strategies = [C , D ]
31
29
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 )
37
35
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 ])
40
40
41
41
return strategies [results .index (max (results ))]
42
42
0 commit comments