Skip to content

Implement prober4 strategy from PRISON project #743

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 11 commits into from
Oct 16, 2016
Merged
Show file tree
Hide file tree
Changes from 9 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
3 changes: 2 additions & 1 deletion axelrod/strategies/_strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
from .mindreader import MindReader, ProtectedMindReader, MirrorMindReader
from .mutual import Desperate, Hopeless, Willing
from .oncebitten import OnceBitten, FoolMeOnce, ForgetfulFoolMeOnce, FoolMeForever
from .prober import (Prober, Prober2, Prober3, HardProber,
from .prober import (Prober, Prober2, Prober3, Prober4, HardProber,
NaiveProber, RemorsefulProber)
from .punisher import Punisher, InversePunisher
from .qlearner import RiskyQLearner, ArrogantQLearner, HesitantQLearner, CautiousQLearner
Expand Down Expand Up @@ -164,6 +164,7 @@
Prober,
Prober2,
Prober3,
Prober4,
ProtectedMindReader,
Punisher,
Raider,
Expand Down
68 changes: 68 additions & 0 deletions axelrod/strategies/prober.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,74 @@ def strategy(self, opponent):
return D if opponent.history[-1:] == [D] else C


class Prober4(Player):
"""
Plays C, C, D, C, D, D, D, C, C, D, C, D, C, C, D, C, D, D, C, D initially.
Counts retaliating and provocative defections of the opponent.
If the absolute difference between the counts is smaller or equal to 2,
defects forever.
Otherwise plays C for the next 5 turns and TFT for the rest of the game.

Names:

- prober4: [PRISON1998]_
"""

name = 'Prober 4'
classifier = {
'stochastic': False,
'memory_depth': 1,
Copy link
Member

Choose a reason for hiding this comment

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

memory_depth: float('inf')

'makes_use_of': set(),
'long_run_time': False,
'inspects_source': False,
'manipulates_source': False,
'manipulates_state': False
}

@init_args
def __init__(self):
Player.__init__(self)
self.init_sequence = [
C, C, D, C, D, D, D, C, C, D, C, D, C, C, D, C, D, D, C, D
]
self.just_Ds = 0
Copy link
Member

Choose a reason for hiding this comment

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

This is a meta comment for everyone (@drvinceknight @meatballs): we could consider counting CC, CD, DC, DD in the player class like we do for cooperations and defections, since multiple players make use of it.

Copy link
Member

Choose a reason for hiding this comment

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

👍 We should open an issue for that.

Copy link
Member

Choose a reason for hiding this comment

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

self.unjust_Ds = 0
self.cooperation_pool = [C] * 5
self.turned_defector = False

def strategy(self, opponent):
if len(opponent.history) == 0:
return self.init_sequence[0]
if len(self.history) == 0:
Copy link
Member

Choose a reason for hiding this comment

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

You can just check one of the histories, they should never be of differing lengths.

Copy link
Member

Choose a reason for hiding this comment

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

This might be better as simply:

if not self.history:

return self.init_sequence[0]
else:
Copy link
Member

Choose a reason for hiding this comment

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

You can drop the else here since the above if statements return if activated

turn = len(self.history)
if turn < len(self.init_sequence):
if opponent.history[-1] == D:
if self.history[-1] == D:
self.just_Ds += 1
if self.history[-1] == C:
self.unjust_Ds += 1
return self.init_sequence[turn]
if turn == len(self.init_sequence):
diff_in_Ds = abs(self.just_Ds - self.unjust_Ds)
self.turned_defector = (diff_in_Ds <= 2)
if self.turned_defector:
return D
if not self.turned_defector:
if self.cooperation_pool:
return self.cooperation_pool.pop()
else:
Copy link
Member

Choose a reason for hiding this comment

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

Can drop the else

return D if opponent.history[-1] == D else C

def reset(self):
Player.reset(self)
self.just_Ds = 0
self.unjust_Ds = 0
self.cooperation_pool = [C] * 5
Copy link
Member

Choose a reason for hiding this comment

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

(minor) I think you could rewrite to eliminate the need for the cooperation_pool by checking if turns < len(self.init_sequence) + 5

self.turned_defector = False


class HardProber(Player):
"""
Plays D, D, C, C initially. Defects forever if opponent cooperated in moves
Expand Down
73 changes: 73 additions & 0 deletions axelrod/tests/unit/test_prober.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,79 @@ def test_strategy(self):
self.responses_test([D, C, C, D, C], [C, D, C, C], [C])


class TestProber4(TestPlayer):

name = "Prober 4"
player = axelrod.Prober4
expected_classifier = {
'stochastic': False,
'memory_depth': 1,
Copy link
Member

Choose a reason for hiding this comment

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

float('inf')

'makes_use_of': set(),
'long_run_time': False,
'inspects_source': False,
'manipulates_source': False,
'manipulates_state': False
}
initial_sequence = [
C, C, D, C, D, D, D, C, C, D, C, D, C, C, D, C, D, D, C, D
]
cooperation_pool = [C] * 5

def test_initial_strategy(self):
"""Starts by playing CCDCDDDCCDCDCCDCDDCD."""
self.responses_test([], [], self.initial_sequence)

def test_strategy(self):
# After playing the initial sequence defects forever
# if the absolute difference in the number of retaliating
# and provocative defections of the opponent is smaller or equal to 2

provocative_histories = [
[C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C],
[C, D, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C],
[C, D, C, D, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C],
[C, C, D, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C],
[C, C, D, C, D, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C],
[D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D],
]

history1 = self.initial_sequence
responses = [D] * 10
for history2 in provocative_histories:
self.responses_test(history1, history2, responses)

# Otherwise cooperates for 5 rounds
unprovocative_histories = [
[C, C, D, C, D, D, D, C, C, D, C, D, C, C, D, C, D, D, C, D],
[D, D, C, D, C, C, C, D, D, C, D, C, D, D, C, D, C, C, D, C],
[C, C, D, C, D, D, C, C, C, C, C, C, C, C, C, C, C, C, C, C],
[C, C, D, C, D, D, C, C, D, C, C, C, C, C, C, D, D, D, C, C],
[C, C, C, C, D, D, C, C, D, C, C, D, D, C, D, C, D, C, C, C],
]

history1 = self.initial_sequence
responses = self.cooperation_pool
for history2 in unprovocative_histories:
self.responses_test(history1, history2, responses)

# and plays like TFT afterwards
history1 += self.cooperation_pool
history2 += self.cooperation_pool
self.responses_test(history1, history2, [C])

history1 += [C]
history2 += [D]
self.responses_test(history1, history2, [D])

history1 += [D]
history2 += [C]
self.responses_test(history1, history2, [C])

history1 += [C]
history2 += [D]
self.responses_test(history1, history2, [D])


Copy link
Member

Choose a reason for hiding this comment

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

Can we test that turned_defector is being set correctly? responses_test can take an attrs parameter (a dictionary) that checks the state of internal variables on a player.

class TestHardProber(TestPlayer):

name = "Hard Prober"
Expand Down
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 @@ -58,7 +58,7 @@ make a decision::
... }
>>> strategies = axl.filtered_strategies(filterset)
>>> len(strategies)
24
25

Multiple filters can be specified within the filterset dictionary. To specify a
range of memory_depth values, we can use the 'min_memory_depth' and
Expand All @@ -70,7 +70,7 @@ range of memory_depth values, we can use the 'min_memory_depth' and
... }
>>> strategies = axl.filtered_strategies(filterset)
>>> len(strategies)
41
42

We can also identify strategies that make use of particular properties of the
tournament. For example, here is the number of strategies that make use of the
Expand Down