Skip to content

Commit 439d172

Browse files
committed
Merge pull request #467 from Axelrod-Python/465
Adding in some examples for particular failures.
2 parents f8c635d + bc6ac64 commit 439d172

File tree

2 files changed

+30
-24
lines changed

2 files changed

+30
-24
lines changed

axelrod/strategies/mindreader.py

+20-20
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,23 @@
22
import inspect
33

44
from axelrod import Actions, Player, RoundRobin, update_history
5+
from .cycler import Cycler
56

67
C, 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+
818
def 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

2523
def 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

axelrod/tests/unit/test_tournament.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,7 @@ def test_serial_play(self):
106106
{'cooperation': [], 'payoff': []})
107107
self.assertFalse(tournament._run_parallel_repetitions.called)
108108

109-
#@given(s=lists(sampled_from(axelrod.strategies),
110-
# Removing this as Hypothesis seems to have found a py2 bug.
111-
# This is a temporary fix. For some reason mind reader seems to fail a test.
112-
@given(s=lists(sampled_from([s for s in axelrod.strategies if s not in axelrod.cheating_strategies]),
109+
@given(s=lists(sampled_from(axelrod.strategies),
113110
min_size=2, # Errors are returned if less than 2 strategies
114111
max_size=5, unique=True),
115112
turns=integers(min_value=2, max_value=50),
@@ -118,9 +115,18 @@ def test_serial_play(self):
118115
@settings(max_examples=50, timeout=0)
119116
@example(s=test_strategies, turns=test_turns, repetitions=test_repetitions,
120117
rm=random.seed(0))
118+
119+
# These two examples are to make sure #465 is fixed.
120+
# As explained there: https://github.com/Axelrod-Python/Axelrod/issues/465,
121+
# these two examples were identified by hypothesis.
122+
@example(s=[axelrod.BackStabber, axelrod.MindReader], turns=2, repetitions=1,
123+
rm=random.seed(0))
124+
@example(s=[axelrod.ThueMorse, axelrod.MindReader], turns=2, repetitions=1,
125+
rm=random.seed(0))
121126
def test_property_serial_play(self, s, turns, repetitions, rm):
122127
"""Test serial play using hypothesis"""
123128
# Test that we get an instance of ResultSet
129+
124130
players = [strat() for strat in s]
125131

126132
tournament = axelrod.Tournament(

0 commit comments

Comments
 (0)