-
Notifications
You must be signed in to change notification settings - Fork 272
New Strategy: Implementation of Adaptor #1215
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3bae266
Initial implementatino of Adaptor
marcharper 6b83b04
Update tests for Adaptor, mostly
marcharper a9dfaba
Remove default value of delta dictionary in AbstractAdaptor
marcharper ae51ba4
Update variable names for Adaptor and update docstring
marcharper File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
from typing import Dict, Tuple | ||
|
||
from axelrod.action import Action | ||
from axelrod.player import Player | ||
from axelrod.random_ import random_choice | ||
|
||
from numpy import heaviside | ||
|
||
C, D = Action.C, Action.D | ||
|
||
|
||
class AbstractAdaptor(Player): | ||
""" | ||
An adaptive strategy that updates an internal state based on the last | ||
round of play. Using this state the player Cooperates with a probability | ||
derived from the state. | ||
|
||
s, float: | ||
the internal state, initially 0 | ||
perr, float: | ||
an error threshold for misinterpreted moves | ||
delta, a dictionary of floats: | ||
additive update values for s depending on the last round's outcome | ||
|
||
Names: | ||
|
||
- Adaptor: [Hauert2002]_ | ||
|
||
""" | ||
|
||
name = "AbstractAdaptor" | ||
classifier = { | ||
"memory_depth": float("inf"), # Long memory | ||
"stochastic": True, | ||
"makes_use_of": set(), | ||
"long_run_time": False, | ||
"inspects_source": False, | ||
"manipulates_source": False, | ||
"manipulates_state": False, | ||
} | ||
|
||
def __init__(self, delta: Dict[Tuple[Action, Action], float], | ||
perr: float = 0.01) -> None: | ||
meatballs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
super().__init__() | ||
self.perr = perr | ||
self.delta = delta | ||
self.s = 0. | ||
|
||
def strategy(self, opponent: Player) -> Action: | ||
if self.history: | ||
# Update internal state from the last play | ||
last_round = (self.history[-1], opponent.history[-1]) | ||
self.s += self.delta[last_round] | ||
|
||
# Compute probability of Cooperation | ||
p = self.perr + (1.0 - 2 * self.perr) * ( | ||
heaviside(self.s + 1, 1) - heaviside(self.s - 1, 1)) | ||
# Draw action | ||
action = random_choice(p) | ||
return action | ||
|
||
|
||
class AdaptorBrief(AbstractAdaptor): | ||
""" | ||
An Adaptor trained on short interactions. | ||
|
||
Names: | ||
|
||
- AdaptorBrief: [Hauert2002]_ | ||
|
||
""" | ||
|
||
name = "AdaptorBrief" | ||
|
||
def __init__(self) -> None: | ||
delta = { | ||
(C, C): 0., # R | ||
(C, D): -1.001505, # S | ||
(D, C): 0.992107, # T | ||
(D, D): -0.638734 # P | ||
} | ||
super().__init__(delta=delta) | ||
|
||
|
||
class AdaptorLong(AbstractAdaptor): | ||
""" | ||
An Adaptor trained on long interactions. | ||
|
||
Names: | ||
|
||
- AdaptorLong: [Hauert2002]_ | ||
|
||
""" | ||
|
||
name = "AdaptorLong" | ||
|
||
def __init__(self) -> None: | ||
delta = { | ||
(C, C): 0., # R | ||
(C, D): 1.888159, # S | ||
(D, C): 1.858883, # T | ||
(D, D): -0.995703 # P | ||
} | ||
super().__init__(delta=delta) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
"""Tests for the adaptor""" | ||
|
||
import unittest | ||
|
||
import axelrod | ||
from axelrod import Game | ||
|
||
from .test_player import TestPlayer, test_four_vector | ||
|
||
C, D = axelrod.Action.C, axelrod.Action.D | ||
|
||
|
||
class TestAdaptorBrief(TestPlayer): | ||
|
||
name = "AdaptorBrief" | ||
player = axelrod.AdaptorBrief | ||
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): | ||
# No error. | ||
actions = [(C, C), (C, C), (C, C), (C, C)] | ||
self.versus_test( | ||
opponent=axelrod.AdaptorBrief(), expected_actions=actions, seed=0 | ||
) | ||
|
||
# Error corrected. | ||
actions = [(C, C), (C, D), (D, C), (C, C)] | ||
self.versus_test( | ||
opponent=axelrod.AdaptorBrief(), expected_actions=actions, seed=22 | ||
) | ||
|
||
# Error corrected, example 2 | ||
actions = [(D, C), (C, D), (D, C), (C, D), (C, C)] | ||
self.versus_test( | ||
opponent=axelrod.AdaptorBrief(), expected_actions=actions, seed=925 | ||
) | ||
|
||
# Versus Cooperator | ||
actions = [(C, C)] * 8 | ||
self.versus_test( | ||
opponent=axelrod.Cooperator(), expected_actions=actions, seed=0 | ||
) | ||
|
||
# Versus Defector | ||
actions = [(C, D), (D, D), (D, D), (D, D), (D, D), (D, D), (D, D)] | ||
self.versus_test( | ||
opponent=axelrod.Defector(), expected_actions=actions, seed=0 | ||
) | ||
|
||
|
||
class TestAdaptorLong(TestPlayer): | ||
|
||
name = "AdaptorLong" | ||
player = axelrod.AdaptorLong | ||
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): | ||
# No error. | ||
actions = [(C, C), (C, C), (C, C), (C, C)] | ||
self.versus_test( | ||
opponent=axelrod.AdaptorLong(), expected_actions=actions, seed=0 | ||
) | ||
|
||
# Error corrected. | ||
actions = [(C, C), (C, D), (D, D), (C, C), (C, C)] | ||
self.versus_test( | ||
opponent=axelrod.AdaptorLong(), expected_actions=actions, seed=22 | ||
) | ||
|
||
# Versus Cooperator | ||
actions = [(C, C)] * 8 | ||
self.versus_test( | ||
opponent=axelrod.Cooperator(), expected_actions=actions, seed=0 | ||
) | ||
|
||
# Versus Defector | ||
actions = [(C, D), (D, D), (C, D), (D, D), (D, D), (C, D), (D, D)] | ||
self.versus_test( | ||
opponent=axelrod.Defector(), expected_actions=actions, seed=0 | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.