-
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
Add the K86R (MoreGrofman) Strategy #1124
Changes from 5 commits
a93d74a
aca96ae
1a9fa68
6ffdaea
bc50c84
900e885
29de9ba
f3625e8
e870730
2480ca7
79f0499
8f64aad
37664eb
1334535
251da15
60ac912
64efcae
ad55ec5
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 |
---|---|---|
|
@@ -199,3 +199,62 @@ def strategy(self, opponent: Player) -> Action: | |
else: | ||
# Play TFT | ||
return opponent.history[-1] | ||
|
||
|
||
class MoreGrofman(Player): | ||
''' | ||
Submitted to Axelrod's second tournament by Bernard Grofman. | ||
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 add the Fortran rule name (K86R) to the docstring? |
||
|
||
This strategy has 3 phases: | ||
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. Also need a space here.
|
||
1. First it cooperates on the first two rounds | ||
2. For rounds 3-7 inclusive, it plays the same as the opponent's last move | ||
3. Thereafter, it applies the following logic | ||
- If its own previous move was C and the opponent has defected less than | ||
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. CI is failing because this isn't building in the documentation. Need a space:
|
||
twice in the last 7 rounds, cooperate | ||
- If its own previous move was C and the opponent has defected twice or | ||
more in the last 7 rounds, defect | ||
- If its own previous move was D and the opponent has defected only once | ||
or not at all in the last 7 rounds, cooperate | ||
- If its own previous move was D and the opponent has defected more than | ||
once in the last 7 rounds, defect | ||
|
||
Names: | ||
- Grofman's strategy: [Axelrod1980b]_ | ||
''' | ||
name = 'MoreGrofman' | ||
classifier = { | ||
'memory_depth': 7, | ||
'stochastic': False, | ||
'makes_use_of': set(), | ||
'long_run_time': False, | ||
'inspects_source': False, | ||
'manipulates_source': False, | ||
'manipulates_state': False | ||
} | ||
|
||
def __init__(self) -> None: | ||
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. I don't think we need this |
||
super().__init__() | ||
|
||
def strategy(self, opponent: Player) -> Action: | ||
# Cooperate on the first two moves | ||
if not self.history or len(self.history) in [1]: | ||
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.
|
||
return C | ||
# For rounds 3-7, play the opponent's last move | ||
elif 2 <= len(self.history) <= 6: | ||
return opponent.history[-1] | ||
# Logic for the rest of the game | ||
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 comment isn't necessary |
||
else: | ||
opponent_defections_last_7_rounds = 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.
|
||
for action in opponent.history[-7:]: | ||
if action == D: | ||
opponent_defections_last_7_rounds += 1 | ||
if self.history[-1] == C: | ||
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 this be simplified to:
? |
||
if opponent_defections_last_7_rounds < 2: | ||
return C | ||
else: | ||
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. no need to have an Similarly below (line 264). (Not a big deal: just a stylistic suggestion.) |
||
return D | ||
else: | ||
if opponent_defections_last_7_rounds in [0, 1]: | ||
return C | ||
else: | ||
return D |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -158,3 +158,56 @@ def test_strategy(self): | |
actions = [(D, C), (C, C), (C, D), (C, D), (D, D)] | ||
self.versus_test(opponent, expected_actions=actions, | ||
attrs={'patsy': False}) | ||
|
||
|
||
class TestMoreGrofman(TestPlayer): | ||
|
||
name = "MoreGrofman" | ||
player = axelrod.MoreGrofman | ||
expected_classifier = { | ||
'memory_depth': 7, | ||
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 now needs to be 8. |
||
'stochastic': False, | ||
'makes_use_of': set(), | ||
'long_run_time': False, | ||
'inspects_source': False, | ||
'manipulates_source': False, | ||
'manipulates_state': False | ||
} | ||
|
||
def test_strategy(self): | ||
# Cooperate for the first two rounds | ||
actions = [(C, C), (C, C)] | ||
self.versus_test(axelrod.Cooperator(), expected_actions=actions) | ||
|
||
# Cooperate for the first two rounds, then play tit for tat for 3-7 | ||
actions = [(C, C), (C, D), (D, C), (C, D), (D, C), (C, D), (D, C)] | ||
self.versus_test(axelrod.Alternator(), expected_actions=actions) | ||
|
||
# Demonstrate MoreGrofman Logic | ||
# Own previous move was C, opponent defected less than twice in last 7 | ||
moregrofman_actions = [C] * 7 + [C] | ||
opponent_actions = [C] * 6 + [D] * 2 | ||
opponent = axelrod.MockPlayer(actions=opponent_actions) | ||
actions = list(zip(moregrofman_actions, opponent_actions)) | ||
self.versus_test(opponent, expected_actions=actions) | ||
|
||
# Own previous move was C, opponent defected more than twice in last 7 | ||
moregrofman_actions = ([C] * 3 + [D] * 3 + [C]) + [D] | ||
opponent_actions = ([C] * 2 + [D] * 3 + [C] * 2) + [D] | ||
opponent = axelrod.MockPlayer(actions=opponent_actions) | ||
actions = list(zip(moregrofman_actions, opponent_actions)) | ||
self.versus_test(opponent, expected_actions=actions) | ||
|
||
# Own previous move was D, opponent defected once in last 7 | ||
moregrofman_actions = ([C] * 6 + [D]) + [C] | ||
opponent_actions = ([C] * 5 + [D] * 1 + [C]) + [D] | ||
opponent = axelrod.MockPlayer(actions=opponent_actions) | ||
actions = list(zip(moregrofman_actions, opponent_actions)) | ||
self.versus_test(opponent, expected_actions=actions) | ||
|
||
# Own previous move was D, opponent defected more than twice in last 7 | ||
moregrofman_actions = ([C] * 2 + [D] * 5) + [D] | ||
opponent_actions = ([D] * 7) + [D] | ||
opponent = axelrod.MockPlayer(actions=opponent_actions) | ||
actions = list(zip(moregrofman_actions, opponent_actions)) | ||
self.versus_test(opponent, expected_actions=actions) |
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.
Can we change these to
"