Skip to content

Implemented a naive prober strategy (like tit for tat, but randomly defects). #629

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

Merged
merged 3 commits into from
Jun 13, 2016
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
1 change: 1 addition & 0 deletions axelrod/strategies/_strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
from .mindreader import MindReader, ProtectedMindReader, MirrorMindReader
from .oncebitten import OnceBitten, FoolMeOnce, ForgetfulFoolMeOnce, FoolMeForever
from .prober import Prober, Prober2, Prober3, HardProber
from .naiveprober import NaiveProber
from .punisher import Punisher, InversePunisher
from .qlearner import RiskyQLearner, ArrogantQLearner, HesitantQLearner, CautiousQLearner
from .rand import Random
Expand Down
46 changes: 46 additions & 0 deletions axelrod/strategies/naiveprober.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from axelrod import Actions, Player, init_args, random_choice

C, D = Actions.C, Actions.D


class NaiveProber(Player):
"""
Like tit-for-tat, but it occasionally defects with a small probability.
"""

name = 'Naive Prober'
classifier = {
'memory_depth': 1, # Four-Vector = (1.,0.,1.,0.)
'stochastic': True,
'makes_use_of': set(),
'inspects_source': False,
'manipulates_source': False,
'manipulates_state': False
}

@init_args
def __init__(self, p=0.1):
"""
Parameters
----------
p, float
The probability to defect randomly
"""
Player.__init__(self)
self.p = p
if (self.p == 0) or (self.p == 1):
self.classifier['stochastic'] = False

def strategy(self, opponent):
# First move
if len(self.history) == 0:
return C
# React to the opponent's last move
if opponent.history[-1] == D:
return D
# Otherwise cooperate, defect with a small probability
choice = random_choice(1 - self.p)
return choice

def __repr__(self):
return "%s: %s" % (self.name, round(self.p, 2))
40 changes: 40 additions & 0 deletions axelrod/tests/unit/test_naiveprober.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""Test for the Naive Prober strategy."""

import axelrod
from .test_player import TestPlayer, test_responses

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


class TestNaiveProber(TestPlayer):

name = "Naive Prober: 0.1"
player = axelrod.NaiveProber
expected_classifier = {
'memory_depth': 1,
'stochastic': True,
'makes_use_of': set(),
'inspects_source': False,
'manipulates_source': False,
'manipulates_state': False
}

def test_strategy(self):
"Randomly defects and always retaliates like tit for tat."
self.first_play_test(C)
# Always retaliate a defection
self.responses_test([C] * 2, [C, D], [D])

def test_random_defection(self):
# Random defection
player = self.player(0.4)
opponent = axelrod.Random()
test_responses(self, player, opponent, [C], [C], [D], random_seed=1)

def test_reduction_to_TFT(self):
player = self.player(0)
opponent = axelrod.Random()
test_responses(self, player, opponent, [C], [C], [C], random_seed=1)
test_responses(self, player, opponent, [C], [D], [D])
test_responses(self, player, opponent, [C, D], [D, C], [C])
test_responses(self, player, opponent, [C, D], [D, D], [D])
20 changes: 20 additions & 0 deletions docs/reference/overview_of_strategies.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1000,3 +1000,23 @@ HardProber is implemented in the library::
['D', 'D', 'C', 'C', 'D']
>>> p2.history
['C', 'C', 'C', 'C', 'C']

NaiveProber
^^^^^^^^^^^

NAIVE_PROBER is a modification of Tit For Tat strategy which with a small
probability randomly defects. Default value for a probability of defection is
0.1.

Here is how NaiveProber is implemented in the library::

>>> import axelrod
>>> p1 = axelrod.NaiveProber() # Create a Prober3 player
>>> p2 = axelrod.Defector() # Create a player that always defects
>>> for round in range(5):
... p1.play(p2)

>>> p1.history
['C', 'D', 'D', 'D', 'D']
>>> p2.history
['D', 'D', 'D', 'D', 'D']