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

New Strategy From the PRISON (http://www.lifl.fr/IPD/ipd.frame.html) #720

Closed
wants to merge 6 commits into from
Closed
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
4 changes: 3 additions & 1 deletion axelrod/strategies/_strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
TitForTat, TitFor2Tats, TwoTitsForTat, Bully, SneakyTitForTat,
SuspiciousTitForTat, AntiTitForTat, HardTitForTat, HardTitFor2Tats,
OmegaTFT, Gradual, ContriteTitForTat, SlowTitForTwoTats, AdaptiveTitForTat)
from .worseandworse import WorseAndWorse


# Note: Meta* strategies are handled in .__init__.py
Expand Down Expand Up @@ -195,12 +196,13 @@
Willing,
WinShiftLoseStay,
WinStayLoseShift,
WorseAndWorse,
ZDExtort2,
ZDExtort2v2,
ZDExtort4,
ZDGTFT2,
ZDGen2,
ZDSet2,
e,

]
35 changes: 35 additions & 0 deletions axelrod/strategies/worseandworse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from axelrod import Actions, Player
from random import randint

class WorseAndWorse (Player):
"""
Defects with probability of 'current turn / total no. of turns'. Therefore
it is more and more likely to defect as the round goes on.

Names:
- worse_and_worse: [PRISON1998]

"""

name = 'Worse and worse'
classifier = {
'memory_depth': float('inf'),
'stochastic': True,
'makes_use_of': set(['length']),
'long_run_time': False,
'inspects_source': False,
'manipulates_source': False,
'manipulates_state': False
}

def strategy(self, opponent):
current_round = len(self.history) + 1
expected_length = self.match_attributes['length']
try:
if randint(0, expected_length) < (current_round):
return Actions.D
return Actions.C
except:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't reviewed the strategy properly (once all tests are sorted I will) but this general except is not great: better to catch the actual error. Also, not sure why you're hardcoding 200, is that just for some tests when the match_attributes isn't passed? I think I'd prefer a different default behaviour there, perhaps just cooperate or defect or randomly cooperate or defect...

Copy link
Member Author

@AdamPohl AdamPohl Sep 25, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@drvinceknight would you prefer it to be an if/else statement? The reason why travis is failing is because random.seed(1) and random.seed(2) are different for python 2.7 and python 3

if randint(0, 200) < (current_round):
return Actions.D
return Actions.C
33 changes: 33 additions & 0 deletions axelrod/tests/unit/test_worse_and_worse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""Test for the Worse and Worse strategy."""

import axelrod

from .test_player import TestPlayer

C, D = axelrod.Actions.C, axelrod.Actions.D


class TestWorseAndWorse(TestPlayer):

name = "Worse and worse"
player = axelrod.WorseAndWorse
expected_classifier = {
'memory_depth': float('inf'),
'stochastic': True,
'makes_use_of': set(['length']),
'long_run_time': False,
'inspects_source': False,
'manipulates_source': False,
'manipulates_state': False
}

def test_strategy(self):
"""
Test that the stratergy chooses to defect according to the correct
probability.
"""
self.responses_test([], [], [C, C, D, D, D], random_seed=1,
tournament_length=5)

self.responses_test([], [], [D, D, D, D, D], random_seed=2,
tournament_length=5)
7 changes: 5 additions & 2 deletions docs/reference/all_strategies.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ Here are the docstrings of all the strategies in the library.
.. automodule:: axelrod.strategies.gobymajority
:members:
:undoc-members:
.. automodule:: axelrod.strategies.gradualkiller
:members:
:undoc-members:
.. automodule:: axelrod.strategies.grudger
:members:
:undoc-members:
Expand Down Expand Up @@ -120,6 +123,6 @@ Here are the docstrings of all the strategies in the library.
.. automodule:: axelrod.strategies.titfortat
:members:
:undoc-members:
.. automodule:: axelrod.strategies.gradualkiller
.. automodule:: axelrod.strategies.worseandworse
:members:
:undoc-members:
:undoc-members:
2 changes: 1 addition & 1 deletion docs/reference/bibliography.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ documentation.
.. [Stewart2012] Stewart, a. J., & Plotkin, J. B. (2012). Extortion and cooperation in the Prisoner’s Dilemma. Proceedings of the National Academy of Sciences, 109(26), 10134–10135. http://doi.org/10.1073/pnas.1208087109
.. [Szabó1992] Szabó, G., & Fáth, G. (2007). Evolutionary games on graphs. Physics Reports, 446(4-6), 97–216. http://doi.org/10.1016/j.physrep.2007.04.004
.. [Tzafestas2000] Tzafestas, E. (2000). Toward adaptive cooperative behavior. From Animals to Animals: Proceedings of the 6th International Conference on the Simulation of Adaptive Behavior {(SAB-2000)}, 2, 334–340.
.. [PRISON1998] LIFL (1998) PRISON. Available at: http://www.lifl.fr/IPD/ipd.frame.html (Accessed: 19 September 2016).
.. [PRISON1998] LIFL (1998) PRISON. Available at: http://www.lifl.fr/IPD/ipd.frame.html (Accessed: 19 September 2016).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like you deleted this line but this is in the master branch so best go with it.

4 changes: 2 additions & 2 deletions docs/tutorials/advanced/classification_of_strategies.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ strategies::
... }
>>> strategies = axl.filtered_strategies(filterset)
>>> len(strategies)
35
36


Or, to find out how many strategies only use 1 turn worth of memory to
Expand Down Expand Up @@ -81,7 +81,7 @@ length of each match of the tournament::
... }
>>> strategies = axl.filtered_strategies(filterset)
>>> len(strategies)
4
5

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