Skip to content

Commit cbb21bd

Browse files
author
margaret
committed
Review all commits
Fix bug - reverse self.turned_defector condition. Refine docstring. Tweak formatting. Update parameter name in reset method. Rewrite tests to loop over set of histories.
1 parent 9a1ab8f commit cbb21bd

File tree

2 files changed

+57
-44
lines changed

2 files changed

+57
-44
lines changed

axelrod/strategies/prober.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,15 @@ def strategy(self, opponent):
104104

105105
class Prober4(Player):
106106
"""
107-
Plays fixed initial sequence of 20 moves.
108-
Counts just (retaliated) and unjust defections of the opponent.
109-
If the absolute difference between just and unjust defections
110-
is greater than 2, defects forever.
111-
Otherwise cooperates for the next 5 moves, and plays TFT afterwards.
107+
Plays C, C, D, C, D, D, D, C, C, D, C, D, C, C, D, C, D, D, C, D initially.
108+
Counts retaliating and provocative defections of the opponent.
109+
If the absolute difference between the counts is smaller or equal to 2,
110+
defects forever.
111+
Otherwise plays C for the next 5 turns and TFT for the rest of the game.
112+
113+
Names:
114+
115+
- prober4: [PRISON1998]_
112116
"""
113117

114118
name = 'Prober 4'
@@ -125,18 +129,19 @@ class Prober4(Player):
125129
@init_args
126130
def __init__(self):
127131
Player.__init__(self)
128-
self.init_sequence = [C, C, D, C, D, D, D, C, C, D,
129-
C, D, C, C, D, C, D, D, C, D]
132+
self.init_sequence = [
133+
C, C, D, C, D, D, D, C, C, D, C, D, C, C, D, C, D, D, C, D
134+
]
130135
self.just_Ds = 0
131136
self.unjust_Ds = 0
132-
self.cooperation_pool = 5 * [C]
137+
self.cooperation_pool = [C] * 5
133138
self.turned_defector = False
134139

135140
def strategy(self, opponent):
136141
if len(opponent.history) == 0:
137-
return C
142+
return self.init_sequence[0]
138143
if len(self.history) == 0:
139-
return C
144+
return self.init_sequence[0]
140145
else:
141146
turn = len(self.history)
142147
if turn < len(self.init_sequence):
@@ -148,7 +153,7 @@ def strategy(self, opponent):
148153
return self.init_sequence[turn]
149154
if turn == len(self.init_sequence):
150155
diff_in_Ds = abs(self.just_Ds - self.unjust_Ds)
151-
self.turned_defector = (diff_in_Ds > 2)
156+
self.turned_defector = (diff_in_Ds <= 2)
152157
if self.turned_defector:
153158
return D
154159
if not self.turned_defector:
@@ -161,8 +166,8 @@ def reset(self):
161166
Player.reset(self)
162167
self.just_Ds = 0
163168
self.unjust_Ds = 0
164-
self.cooperation_pool = [C, C, C, C, C]
165-
self.became_defector = False
169+
self.cooperation_pool = [C] * 5
170+
self.turned_defector = False
166171

167172

168173
class HardProber(Player):

axelrod/tests/unit/test_prober.py

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -112,56 +112,64 @@ class TestProber4(TestPlayer):
112112
'manipulates_source': False,
113113
'manipulates_state': False
114114
}
115-
initial_sequence = [C, C, D, C, D, D, D, C, C, D,
116-
C, D, C, C, D, C, D, D, C, D]
115+
initial_sequence = [
116+
C, C, D, C, D, D, D, C, C, D, C, D, C, C, D, C, D, D, C, D
117+
]
117118
cooperation_pool = [C] * 5
118119

119120
def test_initial_strategy(self):
120121
"""Starts by playing CCDCDDDCCDCDCCDCDDCD."""
121122
self.responses_test([], [], self.initial_sequence)
122123

123124
def test_strategy(self):
124-
# Defects forever if the opponent played D for C
125-
# at least 3 times more than D for D
126-
history1 = self.initial_sequence
127-
history2 = self.initial_sequence
128-
responses = [D] * 10
129-
self.responses_test(history1, history2, responses)
125+
# After playing the initial sequence defects forever
126+
# if the absolute difference in the number of retaliating
127+
# and provocative defections of the opponent is smaller or equal to 2
128+
129+
provocative_histories = [
130+
[C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C],
131+
[C, D, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C],
132+
[C, D, C, D, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C],
133+
[C, C, D, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C],
134+
[C, C, D, C, D, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C],
135+
[D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D],
136+
]
130137

131-
# or if the opponent played D for D
132-
# at least 3 times more than D for C
133138
history1 = self.initial_sequence
134-
history2 = list(map(lambda x: D if x is C else C,
135-
self.initial_sequence))
136139
responses = [D] * 10
137-
self.responses_test(history1, history2, responses)
140+
for history2 in provocative_histories:
141+
self.responses_test(history1, history2, responses)
138142

139143
# Otherwise cooperates for 5 rounds
144+
unprovocative_histories = [
145+
[C, C, D, C, D, D, D, C, C, D, C, D, C, C, D, C, D, D, C, D],
146+
[D, D, C, D, C, C, C, D, D, C, D, C, D, D, C, D, C, C, D, C],
147+
[C, C, D, C, D, D, C, C, C, C, C, C, C, C, C, C, C, C, C, C],
148+
[C, C, D, C, D, D, C, C, D, C, C, C, C, C, C, D, D, D, C, C],
149+
[C, C, C, C, D, D, C, C, D, C, C, D, D, C, D, C, D, C, C, C],
150+
]
151+
140152
history1 = self.initial_sequence
141-
history2 = [C] * len(history1)
142153
responses = self.cooperation_pool
143-
self.responses_test(history1, history2, responses)
154+
for history2 in unprovocative_histories:
155+
self.responses_test(history1, history2, responses)
144156

145157
# and plays like TFT afterwards
146-
history1 = self.initial_sequence + self.cooperation_pool
147-
history2 = [C] * (len(history1) - 1) + [D]
148-
self.responses_test(history1, history2, [D])
149-
150-
history1 = self.initial_sequence + self.cooperation_pool + [D]
151-
history2 = [C] * len(history1)
152-
self.responses_test(history1, history2, [C])
158+
history1 += self.cooperation_pool
159+
history2 += self.cooperation_pool
160+
self.responses_test(history1, history2, [C])
153161

154-
history1 = self.initial_sequence + self.cooperation_pool
155-
history2 = [C] * len(history1)
156-
self.responses_test(history1, history2, [C])
162+
history1 += [C]
163+
history2 += [D]
164+
self.responses_test(history1, history2, [D])
157165

158-
history1 = self.initial_sequence + self.cooperation_pool + [C]
159-
history2 = [C] * (len(history1) - 1) + [D]
160-
self.responses_test(history1, history2, [D])
166+
history1 += [D]
167+
history2 += [C]
168+
self.responses_test(history1, history2, [C])
161169

162-
history1 = self.initial_sequence + self.cooperation_pool + [C]
163-
history2 = [C] * (len(history1) - 1) + [D]
164-
self.responses_test(history1, history2, [D])
170+
history1 += [C]
171+
history2 += [D]
172+
self.responses_test(history1, history2, [D])
165173

166174

167175
class TestHardProber(TestPlayer):

0 commit comments

Comments
 (0)