Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor tests for memory one #1032

Merged
merged 3 commits into from
Jun 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions axelrod/strategies/memoryone.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@


class MemoryOnePlayer(Player):
"""Uses a four-vector for strategies based on the last round of play,
"""
Uses a four-vector for strategies based on the last round of play,
(P(C|CC), P(C|CD), P(C|DC), P(C|DD)), defaults to Win-Stay Lose-Shift.
Intended to be used as an abstract base class or to at least be supplied
with a initializing four_vector."""
with a initializing four_vector.

Names

- Memory One: [Nowak1990]_
"""

name = 'Generic Memory One Player'
classifier = {
Expand Down Expand Up @@ -191,7 +197,12 @@ def __init__(self) -> None:


class StochasticCooperator(MemoryOnePlayer):
"""Stochastic Cooperator, http://www.nature.com/ncomms/2013/130801/ncomms3193/full/ncomms3193.html."""
"""Stochastic Cooperator.

Names:

- Stochastic Cooperator: [Adami2013]_
"""

name = 'Stochastic Cooperator'

Expand Down
166 changes: 130 additions & 36 deletions axelrod/tests/strategies/test_memoryone.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,18 @@ def test_four_vector(self):

def test_strategy(self):
self.first_play_test(C)
self.responses_test([C], [C], [C])
self.responses_test([D], [C], [D])
self.responses_test([C], [D], [C])
self.responses_test([C], [D], [D], seed=1)
self.responses_test([D], [D], [D], seed=2)

actions = [(C, C), (C, D), (D, C), (C, D), (D, C)]
self.versus_test(opponent=axelrod.Alternator(),
expected_actions=actions)

actions = [(C, D), (D, D), (D, D), (D, D), (C, D)]
self.versus_test(opponent=axelrod.Defector(),
expected_actions=actions, seed=0)

actions = [(C, D), (D, D), (C, D), (D, D), (D, D)]
self.versus_test(opponent=axelrod.Defector(),
expected_actions=actions, seed=1)


class TestStochasticCooperator(TestPlayer):
Expand All @@ -153,14 +160,22 @@ def test_four_vector(self):

def test_strategy(self):
self.first_play_test(C)
# With probability 0.065 will defect
self.responses_test([D, C, C, C], [C], [C], seed=15)
# With probability 0.266 will cooperate
self.responses_test([C], [C], [D], seed=1)
# With probability 0.42 will cooperate
self.responses_test([C], [D], [C], seed=3)
# With probability 0.229 will cooperate
self.responses_test([C], [D], [D], seed=13)

actions = [(C, C), (D, D), (C, C), (C, D), (C, C), (D, D)]
self.versus_test(opponent=axelrod.Alternator(),
expected_actions=actions, seed=15)

actions = [(C, C), (C, D), (D, C), (D, D), (C, C), (C, D)]
self.versus_test(opponent=axelrod.Alternator(),
expected_actions=actions, seed=1)

actions = [(C, C), (C, D), (D, C), (D, D), (D, C), (D, D)]
self.versus_test(opponent=axelrod.Alternator(),
expected_actions=actions, seed=3)

actions = [(C, C), (C, D), (D, C), (D, D), (D, C), (C, D)]
self.versus_test(opponent=axelrod.Alternator(),
expected_actions=actions, seed=13)


class TestStochasticWSLS(TestPlayer):
Expand All @@ -179,14 +194,22 @@ class TestStochasticWSLS(TestPlayer):

def test_strategy(self):
self.first_play_test(C)
# With probability 0.05 will defect
self.responses_test([D], [C], [C], seed=2)
# With probability 0.05 will cooperate
self.responses_test([C], [C], [D], seed=31)
# With probability 0.05 will cooperate
self.responses_test([C], [D], [C], seed=31)
# With probability 0.05 will defect
self.responses_test([D], [D], [D], seed=2)

actions = [(C, C), (D, D), (C, C), (C, D), (D, C), (D, D)]
self.versus_test(opponent=axelrod.Alternator(),
expected_actions=actions, seed=2)

actions = [(C, C), (C, D), (D, C), (D, D), (C, C), (C, D)]
self.versus_test(opponent=axelrod.Alternator(),
expected_actions=actions, seed=31)

actions = [(C, D), (D, C), (D, D), (C, C), (C, D), (D, C)]
self.versus_test(opponent=axelrod.CyclerDC(),
expected_actions=actions, seed=2)

actions = [(C, D), (C, C), (C, D), (D, C), (D, D), (C, C)]
self.versus_test(opponent=axelrod.CyclerDC(),
expected_actions=actions, seed=31)

def test_four_vector(self):
player = self.player()
Expand Down Expand Up @@ -240,10 +263,22 @@ def test_four_vector(self):

def test_strategy(self):
self.first_play_test(C)
self.responses_test([D, D, C, C], [C], [C], seed=2)
self.responses_test([D, D, C, C], [C], [D], seed=2)
self.responses_test([D, D, C, C], [D], [C], seed=2)
self.responses_test([D, D, C, C], [C], [D], seed=2)

actions = [(C, C), (D, D), (D, C), (D, D), (D, C), (C, D)]
self.versus_test(opponent=axelrod.Alternator(),
expected_actions=actions, seed=2)

actions = [(C, C), (C, D), (C, C), (C, D), (D, C), (C, D)]
self.versus_test(opponent=axelrod.Alternator(),
expected_actions=actions, seed=31)

actions = [(C, D), (D, C), (D, D), (D, C), (C, D), (C, C)]
self.versus_test(opponent=axelrod.CyclerDC(),
expected_actions=actions, seed=2)

actions = [(C, D), (C, C), (C, D), (C, C), (C, D), (C, C)]
self.versus_test(opponent=axelrod.CyclerDC(),
expected_actions=actions, seed=31)


class TestZDExtort2v2(TestPlayer):
Expand All @@ -268,6 +303,14 @@ def test_four_vector(self):
def test_strategy(self):
self.first_play_test(C)

actions = [(C, C), (D, D), (D, C), (D, D), (D, C), (C, D)]
self.versus_test(opponent=axelrod.Alternator(),
expected_actions=actions, seed=2)

actions = [(C, D), (D, C), (D, D), (D, C), (D, D), (D, C)]
self.versus_test(opponent=axelrod.CyclerDC(),
expected_actions=actions, seed=5)


class TestZDExtort4(TestPlayer):

Expand All @@ -291,6 +334,14 @@ def test_four_vector(self):
def test_strategy(self):
self.first_play_test(C)

actions = [(C, C), (D, D), (D, C), (D, D), (D, C), (C, D)]
self.versus_test(opponent=axelrod.Alternator(),
expected_actions=actions, seed=2)

actions = [(C, D), (D, C), (D, D), (D, C), (D, D), (D, C)]
self.versus_test(opponent=axelrod.CyclerDC(),
expected_actions=actions, seed=5)


class TestZDGen2(TestPlayer):

Expand All @@ -314,6 +365,22 @@ def test_four_vector(self):
def test_strategy(self):
self.first_play_test(C)

actions = [(C, C), (C, D), (D, C), (D, D), (C, C), (C, D)]
self.versus_test(opponent=axelrod.Alternator(),
expected_actions=actions, seed=2)

actions = [(C, C), (C, D), (C, C), (C, D), (C, C), (C, D)]
self.versus_test(opponent=axelrod.Alternator(),
expected_actions=actions, seed=31)

actions = [(C, D), (D, C), (D, D), (C, C), (C, D), (C, C)]
self.versus_test(opponent=axelrod.CyclerDC(),
expected_actions=actions, seed=2)

actions = [(C, D), (C, C), (C, D), (C, C), (C, D), (C, C)]
self.versus_test(opponent=axelrod.CyclerDC(),
expected_actions=actions, seed=31)


class TestZDGTFT2(TestPlayer):

Expand All @@ -335,10 +402,22 @@ def test_four_vector(self):

def test_strategy(self):
self.first_play_test(C)
self.responses_test([C, C, C, C], [C], [C], seed=2)
self.responses_test([D], [C], [D], seed=2)
self.responses_test([C, C, C, C], [D], [C], seed=2)
self.responses_test([D], [D], [D], seed=2)

actions = [(C, C), (C, D), (D, C), (C, D), (D, C), (C, D)]
self.versus_test(opponent=axelrod.Alternator(),
expected_actions=actions, seed=2)

actions = [(C, C), (C, D), (C, C), (C, D), (C, C), (C, D)]
self.versus_test(opponent=axelrod.Alternator(),
expected_actions=actions, seed=31)

actions = [(C, D), (D, C), (C, D), (D, C), (C, D), (C, C)]
self.versus_test(opponent=axelrod.CyclerDC(),
expected_actions=actions, seed=2)

actions = [(C, D), (C, C), (C, D), (C, C), (C, D), (D, C)]
self.versus_test(opponent=axelrod.CyclerDC(),
expected_actions=actions, seed=31)


class TestZDSet2(TestPlayer):
Expand All @@ -363,6 +442,14 @@ def test_four_vector(self):
def test_strategy(self):
self.first_play_test(C)

actions = [(C, C), (D, D), (D, C), (C, D), (C, C), (D, D)]
self.versus_test(opponent=axelrod.Alternator(),
expected_actions=actions, seed=2)

actions = [(C, D), (D, C), (D, D), (D, C), (D, D), (D, C)]
self.versus_test(opponent=axelrod.CyclerDC(),
expected_actions=actions, seed=5)


class TestSoftJoss(TestPlayer):

Expand All @@ -383,8 +470,13 @@ def test_four_vector(self):
test_four_vector(self, expected_dictionary)

def test_strategy(self):
self.responses_test([C], [C], [C], seed=2)
self.responses_test([D], [C], [D], seed=5)
actions = [(C, C), (C, D), (D, C), (C, D), (D, C), (C, D)]
self.versus_test(opponent=axelrod.Alternator(),
expected_actions=actions, seed=2)

actions = [(C, D), (D, C), (C, D), (D, C), (C, D), (D, C)]
self.versus_test(opponent=axelrod.CyclerDC(),
expected_actions=actions, seed=5)


class TestALLCorALLD(TestPlayer):
Expand All @@ -402,8 +494,10 @@ class TestALLCorALLD(TestPlayer):
}

def test_strategy(self):
self.responses_test([D] * 10, seed=2)
self.responses_test([C] * 10, seed=3)
self.responses_test([C] * 10, seed=4)
self.responses_test([D] * 10, seed=5)
self.responses_test([D] * 10, seed=6)

actions = [(D, C)] * 10
self.versus_test(opponent=axelrod.Cooperator(),
expected_actions=actions, seed=0)
actions = [(C, C)] * 10
self.versus_test(opponent=axelrod.Cooperator(),
expected_actions=actions, seed=1)
4 changes: 3 additions & 1 deletion docs/reference/bibliography.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Bibliography
This is a collection of various bibliographic items referenced in the
documentation.

.. [Adami2013] Adami C and Hintze A. (2013) Evolutionary instability of zero-determinant strategies demonstrates that winning is not everything. Nature communications. https://www.nature.com/articles/ncomms3193
.. [Andre2013] Andre L. C., Honovan P., Felipe T. and Frederico G. (2013). Iterated Prisoner’s Dilemma - An extended analysis, http://abricom.org.br/wp-content/uploads/2016/03/bricsccicbic2013_submission_202.pdf
.. [Ashlock2006] Ashlock, D., & Kim E. Y, & Leahy, N. (2006). Understanding Representational Sensitivity in the Iterated Prisoner’s Dilemma with Fingerprints. IEEE Transactions On Systems, Man, And Cybernetics, Part C: Applications And Reviews, 36 (4)
.. [Ashlock2006b] Ashlock, W. & Ashlock, D. (2006). Changes in Prisoner's Dilemma Strategies Over Evolutionary Time With Different Population Sizes 2006 IEEE International Conference on Evolutionary Computation. http://DOI.org/10.1109/CEC.2006.1688322
Expand Down Expand Up @@ -33,7 +34,8 @@ documentation.
for the Iterated Prisoner's Dilemma. Proceedings of the 2015
International Conference on Autonomous Agents and Multiagent Systems.
.. [Nachbar1992] Nachbar J., Evolution in the finitely repeated prisoner’s dilemma, Journal of Economic Behavior & Organization, 19(3): 307-326, 1992.
.. [Nowak1992] Nowak, M. a., & May, R. M. (1992). Evolutionary games and spatial chaos. Nature. http://doi.org/10.1038/359826a0
.. [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
.. [Nowak1992] Nowak, M.., & May, R. M. (1992). Evolutionary games and spatial chaos. Nature. http://doi.org/10.1038/359826a0
.. [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
.. [Press2012] Press, W. H., & Dyson, F. J. (2012). Iterated Prisoner’s Dilemma contains strategies that dominate any evolutionary opponent. Proceedings of the National Academy of Sciences, 109(26), 10409–10413. http://doi.org/10.1073/pnas.1206569109
.. [Prison1998] LIFL (1998) PRISON. Available at: http://www.lifl.fr/IPD/ipd.frame.html (Accessed: 19 September 2016).
Expand Down