Skip to content

Commit e8817ca

Browse files
drvinceknightmarcharper
authored andcommitted
Add a Reactive Strategy.
Closes #1069 This is just wrapper for the Memory One Player. - Write tests for Reactive strategy. - Add a Reactive strategy.
1 parent 7d6cc8e commit e8817ca

File tree

4 files changed

+56
-1
lines changed

4 files changed

+56
-1
lines changed

axelrod/strategies/_strategies.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
from .memoryone import (
5252
MemoryOnePlayer, ALLCorALLD, FirmButFair, GTFT, SoftJoss,
5353
StochasticCooperator, StochasticWSLS, ZDExtort2, ZDExtort2v2, ZDExtort4,
54-
ZDGen2, ZDGTFT2, ZDSet2, WinStayLoseShift, WinShiftLoseStay)
54+
ZDGen2, ZDGTFT2, ZDSet2, WinStayLoseShift, WinShiftLoseStay, ReactivePlayer)
5555
from .memorytwo import MEM2
5656
from .mindcontrol import MindController, MindWarper, MindBender
5757
from .mindreader import MindReader, ProtectedMindReader, MirrorMindReader

axelrod/strategies/memoryone.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,3 +543,19 @@ def strategy(self, opponent: Player) -> Action:
543543
if len(self.history) == 0:
544544
return random_choice(0.6)
545545
return self.history[-1]
546+
547+
548+
class ReactivePlayer(MemoryOnePlayer):
549+
"""
550+
A generic reactive player. Defined by 2 probabilities conditional on the
551+
opponent's last move: P(C|C), P(C|D).
552+
553+
Names:
554+
555+
- Reactive: [Nowak1989]_
556+
"""
557+
name = "Reactive Player"
558+
def __init__(self, probabilities: Tuple[float, float]) -> None:
559+
four_vector = (*probabilities, *probabilities)
560+
super().__init__(four_vector)
561+
self.name = "%s: %s" % (self.name, probabilities)

axelrod/tests/strategies/test_memoryone.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,3 +490,41 @@ def test_strategy(self):
490490
actions = [(C, C)] * 10
491491
self.versus_test(opponent=axelrod.Cooperator(),
492492
expected_actions=actions, seed=1)
493+
494+
495+
class TestGenericReactiveStrategy(unittest.TestCase):
496+
"""
497+
Tests for the Reactive Strategy which.
498+
"""
499+
p1 = axelrod.ReactivePlayer(probabilities=(0, 0))
500+
p2 = axelrod.ReactivePlayer(probabilities=(1, 0))
501+
p3 = axelrod.ReactivePlayer(probabilities=(1, 0.5))
502+
503+
def test_name(self):
504+
self.assertEqual(self.p1.name,
505+
"Reactive Player: (0, 0)")
506+
self.assertEqual(self.p2.name,
507+
"Reactive Player: (1, 0)")
508+
self.assertEqual(self.p3.name,
509+
"Reactive Player: (1, 0.5)")
510+
511+
def test_four_vector(self):
512+
self.assertEqual(self.p1._four_vector,
513+
{(C, D): 0.0, (D, C): 0.0,
514+
(C, C): 0.0, (D, D): 0.0})
515+
self.assertEqual(self.p2._four_vector,
516+
{(C, D): 0.0, (D, C): 1.0,
517+
(C, C): 1.0, (D, D): 0.0})
518+
self.assertEqual(self.p3._four_vector,
519+
{(C, D): 0.5, (D, C): 1.0,
520+
(C, C): 1.0, (D, D): 0.5})
521+
522+
def test_stochastic_classification(self):
523+
self.assertFalse(self.p1.classifier['stochastic'])
524+
self.assertFalse(self.p2.classifier['stochastic'])
525+
self.assertTrue(self.p3.classifier['stochastic'])
526+
527+
def test_subclass(self):
528+
self.assertIsInstance(self.p1, MemoryOnePlayer)
529+
self.assertIsInstance(self.p2, MemoryOnePlayer)
530+
self.assertIsInstance(self.p3, MemoryOnePlayer)

docs/reference/bibliography.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ documentation.
4040
International Conference on Autonomous Agents and Multiagent Systems.
4141
.. [Mittal2009] Mittal, S., & Deb, K. (2009). Optimal strategies of the iterated prisoner’s dilemma problem for multiple conflicting objectives. IEEE Transactions on Evolutionary Computation, 13(3), 554–565. https://doi.org/10.1109/TEVC.2008.2009459
4242
.. [Nachbar1992] Nachbar J., Evolution in the finitely repeated prisoner’s dilemma, Journal of Economic Behavior & Organization, 19(3): 307-326, 1992.
43+
.. [Nowak1989] Nowak, Martin, and Karl Sigmund. "Game-dynamical aspects of the prisoner's dilemma." Applied Mathematics and Computation 30.3 (1989): 191-213.
4344
.. [Nowak1990] Nowak, M., & Sigmund, K. (1990). The evolution of stochastic strategies in the Prisoner's Dilemma. Acta Applicandae Mathematica. https://link.springer.com/article/10.1007/BF00049570
4445
.. [Nowak1992] Nowak, M.., & May, R. M. (1992). Evolutionary games and spatial chaos. Nature. http://doi.org/10.1038/359826a0
4546
.. [Nowak1993] Nowak, M., & Sigmund, K. (1993). A strategy of win-stay, lose-shift that outperforms tit-for-tat in the Prisoner’s Dilemma game. Nature, 364(6432), 56–58. http://doi.org/10.1038/364056a0

0 commit comments

Comments
 (0)