-
Notifications
You must be signed in to change notification settings - Fork 269
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
Implement prober4 strategy from PRISON project #743
Changes from 10 commits
dcccd15
0de0b6e
3a95e76
0e6b7d5
df394b4
49e22d1
9a1ab8f
cbb21bd
8e9126f
988b849
b3eb0ea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -102,6 +102,68 @@ 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': 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 | ||
self.unjust_Ds = 0 | ||
self.turned_defector = False | ||
|
||
def strategy(self, opponent): | ||
if len(self.history) == 0: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This might be better as simply:
|
||
return self.init_sequence[0] | ||
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 turn < len(self.init_sequence) + 5: | ||
return C | ||
return D if opponent.history[-1] == D else C | ||
|
||
def reset(self): | ||
Player.reset(self) | ||
self.just_Ds = 0 | ||
self.unjust_Ds = 0 | ||
self.turned_defector = False | ||
|
||
|
||
class HardProber(Player): | ||
""" | ||
Plays D, D, C, C initially. Defects forever if opponent cooperated in moves | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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': 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 | ||
] | ||
|
||
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 | ||
attrs = {'turned_defector': True} | ||
for history2 in provocative_histories: | ||
self.responses_test(history1, history2, responses, attrs=attrs) | ||
|
||
# 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], | ||
] | ||
|
||
responses = [C] * 5 | ||
attrs = {'turned_defector': False} | ||
for history2 in unprovocative_histories: | ||
self.responses_test(history1, history2, responses, attrs=attrs) | ||
|
||
# and plays like TFT afterwards | ||
history1 += responses | ||
history2 += responses | ||
self.responses_test(history1, history2, [C], attrs=attrs) | ||
|
||
history1 += [C] | ||
history2 += [D] | ||
self.responses_test(history1, history2, [D], attrs=attrs) | ||
|
||
history1 += [D] | ||
history2 += [C] | ||
self.responses_test(history1, history2, [C], attrs=attrs) | ||
|
||
history1 += [C] | ||
history2 += [D] | ||
self.responses_test(history1, history2, [D], attrs=attrs) | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we test that |
||
class TestHardProber(TestPlayer): | ||
|
||
name = "Hard Prober" | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#744