Skip to content

Commit cad5746

Browse files
committed
Merge branch 'master' of github.com:Axelrod-Python/Axelrod
2 parents 387dc87 + 0a7d181 commit cad5746

4 files changed

Lines changed: 83 additions & 2 deletions

File tree

axelrod/strategies/_strategies.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
from .oncebitten import OnceBitten, FoolMeOnce, ForgetfulFoolMeOnce, FoolMeForever
6666
from .prober import (CollectiveStrategy, Prober, Prober2, Prober3, Prober4,
6767
HardProber, NaiveProber, RemorsefulProber)
68-
from .punisher import Punisher, InversePunisher, LevelPunisher
68+
from .punisher import Punisher, InversePunisher, LevelPunisher, TrickyLevelPunisher
6969
from .qlearner import (
7070
RiskyQLearner, ArrogantQLearner, HesitantQLearner, CautiousQLearner)
7171
from .rand import Random
@@ -279,6 +279,7 @@
279279
Tranquilizer,
280280
TrickyCooperator,
281281
TrickyDefector,
282+
TrickyLevelPunisher,
282283
Tullock,
283284
TwoTitsForTat,
284285
VeryBad,

axelrod/strategies/punisher.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,40 @@ def strategy(self, opponent: Player) -> Action:
143143
return D
144144
else:
145145
return C
146+
147+
148+
class TrickyLevelPunisher(Player):
149+
"""
150+
A player starts by cooperating however, after 10, 50 and 100 rounds
151+
will defect if at any point the percentage of defections
152+
by an opponent is greater than 20%, 10% and 5% respectively.
153+
154+
Names:
155+
156+
- Tricky Level Punisher: [Eckhart2015]_
157+
"""
158+
159+
name = 'Tricky Level Punisher'
160+
classifier = {
161+
'memory_depth': float('inf'), # Long Memory
162+
'stochastic': False,
163+
'makes_use_of': set(),
164+
'long_run_time': False,
165+
'inspects_source': False,
166+
'manipulates_source': False,
167+
'manipulates_state': False
168+
}
169+
170+
def strategy(self, opponent: Player) -> Action:
171+
if len(opponent.history) == 0:
172+
return C
173+
if len(opponent.history) < 10:
174+
if opponent.defections / len(opponent.history) > 0.2:
175+
return D
176+
if len(opponent.history) < 50:
177+
if opponent.defections / len(opponent.history) > 0.1:
178+
return D
179+
if len(opponent.history) < 100:
180+
if opponent.defections / len(opponent.history) > 0.05:
181+
return D
182+
return C

axelrod/tests/strategies/test_meta.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ def test_strategy(self):
335335
self.versus_test(opponent=axelrod.Alternator(),
336336
expected_actions=actions, seed=0)
337337

338-
actions = [(C, C), (C, D), (C, C), (C, D), (D, C)]
338+
actions = [(C, C), (C, D), (D, C), (C, D), (D, C)]
339339
self.versus_test(opponent=axelrod.Alternator(),
340340
expected_actions=actions, seed=1)
341341

axelrod/tests/strategies/test_punisher.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,46 @@ def test_strategy(self):
130130
opponent = axelrod.MockPlayer([C] * 4 + [D] + [C] * 4 + [D])
131131
actions = [(C, C)] * 4 + [(C, D)] + [(C, C)] * 4 + [(C, D), (C, C)]
132132
self.versus_test(opponent=opponent, expected_actions=actions)
133+
134+
135+
class TestTrickyLevelPunisher(TestPlayer):
136+
137+
name = "Level Punisher"
138+
player = axelrod.LevelPunisher
139+
expected_classifier = {
140+
'memory_depth': float('inf'), # Long memory
141+
'stochastic': False,
142+
'makes_use_of': set(),
143+
'long_run_time': False,
144+
'inspects_source': False,
145+
'manipulates_source': False,
146+
'manipulates_state': False
147+
}
148+
149+
def test_strategy(self):
150+
# Cooperates if the turns played are less than 10.
151+
actions = [(C, C)] * 9
152+
self.versus_test(opponent=axelrod.Cooperator(),
153+
expected_actions=actions)
154+
155+
# After 10 rounds
156+
# Check if number of defections by opponent is greater than 20%
157+
opponent = axelrod.MockPlayer([C] * 4 + [D] * 2 + [C] * 3 + [D])
158+
actions = [(C, C)] * 4 + [(C, D)] * 2 + [(C, C)] * 3 + [(C, D), (D, C)]
159+
self.versus_test(opponent=opponent, expected_actions=actions)
160+
161+
# Check if number of defections by opponent is greater than 10%
162+
opponent = axelrod.MockPlayer([C] * 4 + [D] + [C] * 4 + [D])
163+
actions = [(C, C)] * 4 + [(C, D)] + [(C, C)] * 4 + [(C, D), (C, C)]
164+
self.versus_test(opponent=opponent, expected_actions=actions)
165+
166+
# After 10 rounds
167+
# Check if number of defections by opponent is greater than 5%
168+
opponent = axelrod.MockPlayer([C] * 4 + [D] + [C] * 5)
169+
actions = [(C, C)] * 4 + [(C, D)] + [(C, C)] * 5
170+
self.versus_test(opponent=opponent, expected_actions=actions)
171+
172+
# Check if number of defections by opponent is less than 5%
173+
opponent = axelrod.MockPlayer([C]*10)
174+
actions = [(C, C)] * 5
175+
self.versus_test(opponent=opponent, expected_actions=actions)

0 commit comments

Comments
 (0)