-
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
N tit(s) for M tat(s) #1084
Merged
Merged
N tit(s) for M tat(s) #1084
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
f2c276b
NTitsForMTats player added
Chadys 66ec9c3
tests for NTitsForMTats
Chadys 94ce88e
Remove SlowTitForTwoTats as duplicate of TitFor2Tats (#1073)
Chadys 5690732
docstring update
Chadys d6e98c3
update from upstream
Chadys a49d448
update count of memory one strategies in example
Chadys 69723ee
added memory depth change test
Chadys 9fe1ada
Revert "update count of memory one strategies in example"
Chadys f0736f1
remove NTitsForMTats from strategies to avoid having two instances of…
Chadys 7dc22d8
update number of strategies with short memory length because of the r…
Chadys 9deeb08
Change NTitsForMTats default values
Chadys 0b676d2
correct NTitsForMTats tests to reflect changes in default values
Chadys 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 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 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 |
---|---|---|
|
@@ -55,6 +55,7 @@ class TitFor2Tats(Player): | |
Names: | ||
|
||
- Tit for two Tats: [Axelrod1984]_ | ||
- Slow tit for two tats: Original name by Ranjini Das | ||
""" | ||
|
||
name = "Tit For 2 Tats" | ||
|
@@ -496,41 +497,6 @@ def reset(self): | |
self._recorded_history = [] | ||
|
||
|
||
class SlowTitForTwoTats(Player): | ||
""" | ||
A player plays C twice, then if the opponent plays the same move twice, | ||
plays that move. | ||
|
||
Names: | ||
|
||
- Slow tit for two tats: Original name by Ranjini Das | ||
""" | ||
|
||
name = 'Slow Tit For Two Tats' | ||
classifier = { | ||
'memory_depth': 2, | ||
'stochastic': False, | ||
'makes_use_of': set(), | ||
'long_run_time': False, | ||
'inspects_source': False, | ||
'manipulates_source': False, | ||
'manipulates_state': False | ||
} | ||
|
||
def strategy(self, opponent: Player) -> Action: | ||
|
||
# Start with two cooperations | ||
if len(self.history) < 2: | ||
return C | ||
|
||
# Mimic if opponent plays the same move twice | ||
if opponent.history[-2] == opponent.history[-1]: | ||
return opponent.history[-1] | ||
|
||
# Otherwise cooperate | ||
return C | ||
|
||
|
||
class AdaptiveTitForTat(Player): | ||
"""ATFT - Adaptive Tit For Tat (Basic Model) | ||
|
||
|
@@ -750,3 +716,59 @@ def strategy(self, opponent: Player) -> Action: | |
def reset(self): | ||
super().reset() | ||
self.is_defector = False | ||
|
||
|
||
class NTitsForMTats(Player): | ||
""" | ||
A parameterizable Tit-for-Tat, | ||
The arguments are : | ||
1) the number of defection before retaliation | ||
2) the number of retaliations | ||
|
||
Names: | ||
|
||
- N Tit(s) For M Tat(s): Original name by Marc Harper | ||
""" | ||
|
||
name = 'N Tit(s) For M Tat(s)' | ||
classifier = { | ||
'memory_depth': float('inf'), | ||
'stochastic': False, | ||
'makes_use_of': set(), | ||
'long_run_time': False, | ||
'inspects_source': False, | ||
'manipulates_source': False, | ||
'manipulates_state': False | ||
} | ||
|
||
def __init__(self, N: int=1, M: int=1) -> None: | ||
""" | ||
Parameters | ||
---------- | ||
N, int | ||
Number of retaliations | ||
M, int | ||
Number of defection before retaliation | ||
|
||
Special Cases | ||
------------- | ||
NTitsForMTats(1,1) is equivalent to TitForTat | ||
NTitsForMTats(1,2) is equivalent to TitFor2Tats | ||
NTitsForMTats(2,1) is equivalent to TwoTitsForTat | ||
NTitsForMTats(0,*) is equivalent to Cooperator | ||
NTitsForMTats(*,0) is equivalent to Defector | ||
""" | ||
super().__init__() | ||
self.N = N | ||
self.M = M | ||
self.classifier['memory_depth'] = max([M,N]) | ||
self.retaliate_count = 0 | ||
|
||
def strategy(self, opponent: Player) -> Action: | ||
# if opponent defected consecutively M times, start the retaliation | ||
if not self.M or opponent.history[-self.M:].count(D) == self.M: | ||
self.retaliate_count = self.N | ||
if self.retaliate_count: | ||
self.retaliate_count-=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. PEP8: |
||
return D | ||
return C |
This file contains 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 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.
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.
Because of the type hints let's drop the type in the docstrings (in case at some point they are changed and we forget to change them in the docstrings).