Skip to content

Commit cb3bccc

Browse files
authored
Merge pull request #997 from souravsingh/add-strategy
Add Alexei strategy
2 parents bd594cf + 54c54fc commit cb3bccc

File tree

5 files changed

+70
-3
lines changed

5 files changed

+70
-3
lines changed

axelrod/strategies/_strategies.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
TitForTat, TitFor2Tats, TwoTitsForTat, Bully, SneakyTitForTat,
7777
SuspiciousTitForTat, AntiTitForTat, HardTitForTat, HardTitFor2Tats,
7878
OmegaTFT, Gradual, ContriteTitForTat, SlowTitForTwoTats, AdaptiveTitForTat,
79-
SpitefulTitForTat, SlowTitForTwoTats2)
79+
SpitefulTitForTat, SlowTitForTwoTats2, Alexei)
8080
from .verybad import VeryBad
8181
from .worse_and_worse import (WorseAndWorse, KnowledgeableWorseAndWorse,
8282
WorseAndWorse2, WorseAndWorse3)
@@ -87,6 +87,7 @@
8787
Adaptive,
8888
AdaptiveTitForTat,
8989
Aggravater,
90+
Alexei,
9091
ALLCorALLD,
9192
Alternator,
9293
AlternatorHunter,

axelrod/strategies/titfortat.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from axelrod.actions import Actions, Action
22
from axelrod.player import Player
3-
from axelrod.strategy_transformers import TrackHistoryTransformer
3+
from axelrod.strategy_transformers import TrackHistoryTransformer, FinalTransformer
44

55
C, D = Actions.C, Actions.D
66

@@ -637,3 +637,31 @@ def strategy(self, opponent: Player) -> Action:
637637

638638
# Otherwise play previous move
639639
return self.history[-1]
640+
641+
@FinalTransformer((D,), name_prefix=None)
642+
class Alexei(Player):
643+
"""
644+
Plays similar to Tit-for-Tat, but always defect on last turn.
645+
646+
Names:
647+
648+
- Alexei's Strategy: [LessWrong2011]_
649+
"""
650+
651+
name = 'Alexei'
652+
classifier = {
653+
'memory_depth': float('inf'),
654+
'stochastic': False,
655+
'makes_use_of': {'length'},
656+
'long_run_time': False,
657+
'inspects_source': False,
658+
'manipulates_source': False,
659+
'manipulates_state': False
660+
}
661+
662+
def strategy(self, opponent: Player) -> Action:
663+
if not self.history:
664+
return C
665+
if opponent.history[-1] == D:
666+
return D
667+
return C

axelrod/tests/strategies/test_titfortat.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,3 +563,40 @@ def test_strategy(self):
563563
actions = [(C, C), (C, C), (C, D), (C, D), (D, C), (D, D), (D, D),
564564
(D, C), (D, C), (C, D), (C, D)]
565565
self.versus_test(opponent, expected_actions=actions)
566+
567+
class TestAlexei(TestPlayer):
568+
"""
569+
Tests for the Alexei strategy
570+
"""
571+
572+
name = "Alexei: ('D',)"
573+
player = axelrod.Alexei
574+
expected_classifier = {
575+
'memory_depth': float('inf'),
576+
'stochastic': False,
577+
'makes_use_of': {'length'},
578+
'inspects_source': False,
579+
'manipulates_source': False,
580+
'manipulates_state': False
581+
}
582+
583+
def test_strategy(self):
584+
self.first_play_test(C)
585+
self.second_play_test(rCC=C, rCD=D, rDC=C, rDD=D)
586+
587+
actions = [(C, C), (C, C), (C, C), (C, C), (D, C)]
588+
self.versus_test(axelrod.Cooperator(), expected_actions=actions)
589+
590+
actions = [(C, D), (D, D), (D, D), (D, D), (D, D)]
591+
self.versus_test(axelrod.Defector(), expected_actions=actions)
592+
593+
actions = [(C, C), (C, D), (D, C), (C, D), (D, C), (C, D)]
594+
self.versus_test(axelrod.Alternator(), expected_actions=actions,
595+
match_attributes={"length": -1})
596+
597+
actions = [(C, C), (C, D), (D, C), (C, D), (D, C), (D, D)]
598+
self.versus_test(axelrod.Alternator(), expected_actions=actions)
599+
600+
opponent = axelrod.MockPlayer(actions=[C, C, D, D, C, D])
601+
actions = [(C, C), (C, C), (C, D), (D, D), (D, C), (D, D)]
602+
self.versus_test(opponent, expected_actions=actions)

docs/reference/bibliography.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ documentation.
2626
.. [Frean1994] Frean, Marcus R. “The Prisoner's Dilemma without Synchrony.” Proceedings: Biological Sciences, vol. 257, no. 1348, 1994, pp. 75–79. www.jstor.org/stable/50253.
2727
.. [Hilde2013] Hilbe, C., Nowak, M.A. and Traulsen, A. (2013). Adaptive dynamics of extortion and compliance, PLoS ONE, 8(11), p. e77886. doi: 10.1371/journal.pone.0077886.
2828
.. [Kraines1989] Kraines, D. & Kraines, V. Theor Decis (1989) 26: 47. doi:10.1007/BF00134056
29+
.. [LessWrong2011] Zoo of Strategies (2011) LessWrong. Available at: http://lesswrong.com/lw/7f2/prisoners_dilemma_tournament_results/
2930
.. [Li2009] Li, J. & Kendall, G. (2009). A Strategy with Novel Evolutionary Features for the Iterated Prisoner’s Dilemma. Evolutionary Computation 17(2): 257–274.
3031
.. [Li2011] Li, J., Hingston, P., Member, S., & Kendall, G. (2011). Engineering Design of Strategies for Winning Iterated Prisoner ’ s Dilemma Competitions, 3(4), 348–360.
3132
.. [Li2016] Li, J. and Kendall, G. (2016). The Effect of Memory Size on the Evolutionary Stability of Strategies in Iterated Prisoner's Dilemma. IEEE Transactions on Evolutionary Computation, 18(6) 819-826

docs/tutorials/advanced/classification_of_strategies.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ length of each match of the tournament::
8080
... }
8181
>>> strategies = axl.filtered_strategies(filterset)
8282
>>> len(strategies)
83-
25
83+
26
8484

8585
Note that in the filterset dictionary, the value for the 'makes_use_of' key
8686
must be a list. Here is how we might identify the number of strategies that use

0 commit comments

Comments
 (0)