-
Notifications
You must be signed in to change notification settings - Fork 272
Add new strategy SelfSteem #866
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
Changes from all commits
57a94d7
b58b6b9
15463da
21ad4d7
a50750f
f7654bc
04ed431
d00e419
6623e00
aacf48e
09a94eb
90b8bd8
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from axelrod.actions import Action, Actions | ||
from axelrod.player import Player | ||
from axelrod.random_ import random_choice | ||
|
||
from math import pi, sin | ||
|
||
C, D = Actions.C, Actions.D | ||
|
||
|
||
class SelfSteem(Player): | ||
""" | ||
This strategy is based on the feeling with the same name. | ||
It is modeled on the sine curve(f = sin( 2* pi * n / 10 )), which varies | ||
with the current iteration. | ||
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. current iteration 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. Yes. |
||
|
||
If f > 0.95, 'ego' of the algorithm is inflated; always defects. | ||
If 0.95 > abs(f) > 0.3, rational behavior; follows TitForTat algortithm. | ||
If 0.3 > f > -0.3; random behavior. | ||
If f < -0.95, algorithm is at rock bottom; always cooperates. | ||
|
||
Futhermore, the algorithm implements a retaliation policy, if the opponent | ||
defects; the sin curve is shifted. But due to lack of further information, | ||
this implementation does not include a sin phase change. | ||
Names: | ||
|
||
- SelfSteem: [Andre2013]_ | ||
""" | ||
|
||
name = 'SelfSteem' | ||
classifier = { | ||
'memory_depth': float("inf"), | ||
'stochastic': True, | ||
'makes_use_of': set(), | ||
'long_run_time': False, | ||
'inspects_source': False, | ||
'manipulates_source': False, | ||
'manipulates_state': False | ||
} | ||
|
||
def strategy(self, opponent: Player) -> Action: | ||
turns_number = len(self.history) | ||
sine_value = sin(2 * pi * turns_number / 10) | ||
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. Just checking -- is this integer or float division in the reference? 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. In python3, doesn't it return a float by default? 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. It does, I'm just making sure that's correct. 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. Yes, we do need a float division. |
||
|
||
if sine_value > 0.95: | ||
return D | ||
|
||
if abs(sine_value) < 0.95 and abs(sine_value) > 0.3: | ||
return opponent.history[-1] | ||
|
||
if sine_value < 0.3 and sine_value > -0.3: | ||
return random_choice() | ||
|
||
return C |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
"""Tests for the SelfSteem strategy.""" | ||
|
||
import axelrod | ||
import random | ||
from .test_player import TestPlayer | ||
|
||
C, D = axelrod.Actions.C, axelrod.Actions.D | ||
|
||
class TestSelfSteem(TestPlayer): | ||
|
||
name = "SelfSteem" | ||
player = axelrod.SelfSteem | ||
expected_classifier = { | ||
'memory_depth': float("inf"), | ||
'stochastic': True, | ||
'makes_use_of': set(), | ||
'inspects_source': False, | ||
'manipulates_source': False, | ||
'manipulates_state': False | ||
} | ||
|
||
def test_strategy(self): | ||
# Check for f > 0.95 | ||
self.responses_test([D], [C] * 2 , [C] * 2) | ||
self.responses_test([D], [C] * 13, [C] * 13) | ||
|
||
# Check for f < -0.95 | ||
self.responses_test([C], [C] * 7, [C] * 7) | ||
self.responses_test([C], [C] * 18, [D] * 18) | ||
|
||
# Check for -0.3 < f < 0.3 | ||
self.responses_test([C], [C] * 20, [C] * 20, seed=6) | ||
self.responses_test([D], [C] * 20, [D] * 20, seed=5) | ||
|
||
# Check for 0.95 > abs(f) > 0.3 | ||
self.responses_test([C], [C], [C]) | ||
self.responses_test([D], [C] * 16, [D] * 16) | ||
self.responses_test([C], [D] * 9, [C] * 9) |
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.
self esteem?
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.
It means self esteem, that's how its named in the paper. I decided not to change it.