-
Notifications
You must be signed in to change notification settings - Fork 275
Adaptive Zero Determinant Strategy #1282
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
Open
dashiellfryer
wants to merge
14
commits into
Axelrod-Python:master
Choose a base branch
from
dashiellfryer:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+123
−2
Open
Changes from 5 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
8f9e0e0
Add files via upload
dashiellfryer 3627b9a
Update _strategies.py
dashiellfryer 863fb8a
Rename Adaptivezerodet.py to adaptivezerodet.py
dashiellfryer f678fff
Update _strategies.py
dashiellfryer 318e37e
Update all_strategies.rst
dashiellfryer 9c9f3a7
Rework AdaptiveZeroDet to be a subclass of LRPlayer
marcharper 60788b7
Remove earlier version of AdaptiveZeroDet and add a basic test
marcharper 29dbfdd
Merge pull request #1 from Axelrod-Python/1282
dashiellfryer f2c41e2
Update doc strings
marcharper 6982c05
Refactor strategy slightly and update a test
marcharper 9848f38
Update all_strategies.rst for the strategy move
marcharper 73e871f
Update doctest
marcharper 35de1e4
Fix test
marcharper 0d684b3
Update test for coverage
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
from axelrod.action import Action | ||
from axelrod.player import Player | ||
from axelrod.random_ import random_choice | ||
from typing import Tuple | ||
from typing import List | ||
|
||
C, D = Action.C, Action.D | ||
|
||
class AdaptiveZeroDet(Player): | ||
name = 'AdaptiveZeroDet' | ||
classifier = { | ||
'memory_depth': float('inf'), # Long memory | ||
'stochastic': True, | ||
'makes_use_of': set(["game"]), | ||
'long_run_time': False, | ||
'inspects_source': False, | ||
'manipulates_source': False, | ||
'manipulates_state': False | ||
} | ||
def __init__(self, phi: float = 0.125, s: float = 0.5, l: float = 3, four_vector: Tuple[float, float, float, float] = None, initial: Action = C) -> None: | ||
marcharper marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# This Keeps track of the parameter values (phi,s,l) as well as the four vector which makes final decisions. | ||
self.scores = {C: 0, D: 0} | ||
self.phi = phi | ||
self.s = s | ||
self.l = l | ||
self._initial = initial | ||
super().__init__() | ||
|
||
def set_four_vector(self, four_vector: Tuple[float, float, float, float]): | ||
# This checks the four vector is usable and allows previous matches' output to be input for next four vector | ||
if not all(0 <= p <= 1 for p in four_vector): | ||
raise ValueError("An element in the probability vector, {}, is not between 0 and 1.".format(str(four_vector))) | ||
self._four_vector = dict(zip([(C, C), (C, D), (D, C), (D, D)], map(float, four_vector))) | ||
self.classifier['stochastic'] = any(0 < x < 1 for x in set(four_vector)) | ||
|
||
def score_last_round(self, opponent: Player): | ||
# This gives the strategy the game attributes and allows the strategy to score itself properly | ||
marcharper marked this conversation as resolved.
Show resolved
Hide resolved
|
||
game = self.match_attributes["game"] | ||
if len(self.history): | ||
last_round = (self.history[-1], opponent.history[-1]) | ||
scores = game.score(last_round) | ||
self.scores[last_round[0]] += scores[0] | ||
|
||
def strategy(self, opponent: Player) -> Action: | ||
s = self.s | ||
phi = self.phi | ||
l = self.l | ||
d = randint(0, 9)/1000 # Selects random value to adjust s and l | ||
if self.scores[C] > self.scores[D] & len(self.history): | ||
# This checks scores to determine how to adjust s and l either up or down by d | ||
# This also checks if the length of the game is long enough to start adjusting | ||
self.l = l+d | ||
l = self.l | ||
# adjust l up | ||
self.s = s-d | ||
s = self.s | ||
# adjust s down | ||
R, P, S, T = self.match_attributes["game"].RPST() | ||
phi = self.phi | ||
s_min = - min((T - l) / (l - S), (l - S) / (T - l)) # Sets minimum for s | ||
if (l > R) or (s < s_min): | ||
# This checks that neither s nor l is leaving its range | ||
if (l > R): | ||
l = l-d | ||
self.l = (l+R)/2 | ||
l = self.l | ||
# If l would leave its range instead its distance from its max is halved | ||
if (s < s_min): | ||
s = s+d | ||
self.s = (s+s_min)/2 | ||
s = self.s | ||
# If s would leave its range instead its distance from its min is halved | ||
p1 = 1 - phi * (1 - s) * (R - l) | ||
p2 = 1 - phi * (s * (l - S) + (T - l)) | ||
p3 = phi * ((l - S) + s * (T - l)) | ||
p4 = phi * (1 - s) * (l - P) | ||
four_vector = [p1, p2, p3, p4] | ||
# Four vector is calculated with new parameters | ||
self.set_four_vector(four_vector) | ||
if not hasattr(self, "_four_vector"): | ||
raise ValueError("_four_vector not yet set") | ||
if len(opponent.history) == 0: | ||
return self._initial | ||
p = self._four_vector[(self.history[-1], opponent.history[-1])] | ||
return random_choice(p) | ||
else: | ||
# This adjusts s and l in the opposite direction | ||
self.l = l-d | ||
l = self.l | ||
# adjust l down | ||
self.s = s+d | ||
s = self.s | ||
# adjust s up | ||
R, P, S, T = self.match_attributes["game"].RPST() | ||
phi = self.phi | ||
if (l < P) or (s > 1): | ||
# This checks that neither s nor l is leaving its range | ||
if (l < P): | ||
l = l+d | ||
self.l = (l+P)/2 | ||
l = self.l | ||
# If l would leave its range instead its distance from its min is halved | ||
if (s > 1): | ||
s = s-d | ||
self.s = (s+1)/2 | ||
s = self.s | ||
# If s would leave its range instead its distance from its max is halved | ||
p1 = 1 - phi * (1 - s) * (R - l) | ||
p2 = 1 - phi * (s * (l - S) + (T - l)) | ||
p3 = phi * ((l - S) + s * (T - l)) | ||
p4 = phi * (1 - s) * (l - P) | ||
four_vector = [p1, p2, p3, p4] | ||
# Four vector is calculated with new parameters | ||
self.set_four_vector(four_vector) | ||
if not hasattr(self, "_four_vector"): | ||
raise ValueError("_four_vector not yet set") | ||
if len(opponent.history) == 0: | ||
return self._initial | ||
p = self._four_vector[(self.history[-1], opponent.history[-1])] | ||
return random_choice(p) | ||
marcharper marked this conversation as resolved.
Show resolved
Hide resolved
|
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.