Skip to content

Commit 7edc44a

Browse files
authored
Merge pull request #1 from marcharper/314pe
Updates to tests and minor strategy refactor
2 parents 562589e + 3b65693 commit 7edc44a

File tree

3 files changed

+31
-13
lines changed

3 files changed

+31
-13
lines changed

axelrod/strategies/naiveprober.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
from axelrod import Actions, Player, init_args, random_choice
2-
from axelrod.strategy_transformers import RetaliateUntilApologyTransformer
32

43
C, D = Actions.C, Actions.D
54

65

7-
@RetaliateUntilApologyTransformer
86
class NaiveProber(Player):
97
"""
108
Like tit-for-tat, but it occasionally defects with a small probability.
@@ -26,13 +24,22 @@ def __init__(self, p=0.1):
2624
Parameters
2725
----------
2826
p, float
29-
The probability to defect without provoking
27+
The probability to defect randomly
3028
"""
3129
Player.__init__(self)
3230
self.p = p
31+
if (self.p == 0) or (self.p == 1):
32+
self.classifier['stochastic'] = False
3333

3434
def strategy(self, opponent):
35-
choice = random_choice(1-self.p)
35+
# First move
36+
if len(self.history) == 0:
37+
return C
38+
# React to the opponent's last move
39+
if opponent.history[-1] == D:
40+
return D
41+
# Otherwise cooperate, defect with a small probability
42+
choice = random_choice(1 - self.p)
3643
return choice
3744

3845
def __repr__(self):
Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
"""Test for the Naive Prober strategy."""
22

33
import axelrod
4-
from .test_player import TestPlayer
4+
from .test_player import TestPlayer, test_responses
55

66
C, D = axelrod.Actions.C, axelrod.Actions.D
77

88

99
class TestNaiveProber(TestPlayer):
1010

11-
name = "RUA Naive Prober: 0.1"
11+
name = "Naive Prober: 0.1"
1212
player = axelrod.NaiveProber
1313
expected_classifier = {
1414
'memory_depth': 1,
@@ -21,9 +21,20 @@ class TestNaiveProber(TestPlayer):
2121

2222
def test_strategy(self):
2323
"Randomly defects and always retaliates like tit for tat."
24-
self.first_play_test(C, random_seed=1)
25-
self.first_play_test(D, random_seed=2)
26-
# Random defection
27-
self.responses_test([C] * 10, [C] * 10, [D], random_seed=3)
24+
self.first_play_test(C)
2825
# Always retaliate a defection
2926
self.responses_test([C] * 2, [C, D], [D])
27+
28+
def test_random_defection(self):
29+
# Random defection
30+
player = self.player(0.4)
31+
opponent = axelrod.Random()
32+
test_responses(self, player, opponent, [C], [C], [D], random_seed=1)
33+
34+
def test_reduction_to_TFT(self):
35+
player = self.player(0)
36+
opponent = axelrod.Random()
37+
test_responses(self, player, opponent, [C], [C], [C], random_seed=1)
38+
test_responses(self, player, opponent, [C], [D], [D])
39+
test_responses(self, player, opponent, [C, D], [D, C], [C])
40+
test_responses(self, player, opponent, [C, D], [D, D], [D])

docs/reference/overview_of_strategies.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,11 +1012,11 @@ Here is how NaiveProber is implemented in the library::
10121012

10131013
>>> import axelrod
10141014
>>> p1 = axelrod.NaiveProber() # Create a Prober3 player
1015-
>>> p2 = axelrod.Random() # Create a player that always cooperates
1015+
>>> p2 = axelrod.Defector() # Create a player that always defects
10161016
>>> for round in range(5):
10171017
... p1.play(p2)
10181018

10191019
>>> p1.history
1020-
['C', 'C', 'C', 'C', 'D']
1020+
['C', 'D', 'D', 'D', 'D']
10211021
>>> p2.history
1022-
['C', 'C', 'C', 'C', 'D']
1022+
['D', 'D', 'D', 'D', 'D']

0 commit comments

Comments
 (0)