-
Notifications
You must be signed in to change notification settings - Fork 280
add freqAnalyzer strategy #1444
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
Merged
Changes from 12 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
4423875
add freqAnalyzer strat
miller-ian 5ca735c
cleanup + wip
miller-ian 3b0ac2c
remove spurious changes
miller-ian d706204
fixed freq analyzer test
miller-ian a4fa0a5
Delete axelrod/tests/strategies/test_freqanalyzer.py
miller-ian b8ced55
bump num strats
miller-ian 563a6bf
remove unnecessary lines
miller-ian 96eef3b
fix type error
miller-ian 40e867e
fix test
miller-ian 8384c36
formatting
miller-ian c2e38a3
python black with correct options
miller-ian 16170fe
add strat to index
miller-ian 5cb6c32
code cleanup
miller-ian 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,116 @@ | ||
| from axelrod.action import Action, actions_to_str | ||
| from axelrod.player import Player | ||
| from axelrod.strategy_transformers import ( | ||
| FinalTransformer, | ||
| TrackHistoryTransformer, | ||
| ) | ||
|
|
||
| C, D = Action.C, Action.D | ||
|
|
||
|
|
||
| class FrequencyAnalyzer(Player): | ||
| """ | ||
| A player starts by playing TitForTat for the first 30 turns (dataset generation phase). | ||
|
|
||
| Take the matrix of last 2 moves by both Player and Opponent. | ||
|
|
||
| While in dataset generation phase, construct a dictionary d, where keys are each 4 move sequence | ||
| and the corresponding value for each key is a list of the subsequent Opponent move. The 4 move sequence | ||
| starts with the Opponent move. | ||
|
|
||
| For example, if a game at turn 5 looks like this: | ||
|
|
||
| Opp: C, C, D, C, D | ||
| Player: C, C, C, D, C | ||
|
|
||
| d should look like this: | ||
|
|
||
| { [CCCC]: [D], | ||
| [CCDC]: [C], | ||
| [DCCD]: [D] } | ||
|
|
||
| During dataset generation phase, Player will play TitForTat. After end of dataset generation phase, | ||
| Player will switch strategies. Upon encountering a particular 4-move sequence in the game, Player will look up history | ||
| of subsequent Opponent move. If ratio of defections to total moves exceeds p, Player will defect. Otherwise, | ||
| Player will cooperate. | ||
|
|
||
| Could fall under "Hunter" class of strategies. | ||
| More likely falls under LookerUp class of strategies. | ||
|
|
||
| Names: | ||
|
|
||
| - FrequencyAnalyzer (FREQ): Original by Ian Miller | ||
| """ | ||
|
|
||
| # These are various properties for the strategy | ||
| name = "FrequencyAnalyzer" | ||
| classifier = { | ||
| "memory_depth": float("inf"), | ||
| "stochastic": False, | ||
| "long_run_time": False, | ||
| "inspects_source": False, | ||
| "manipulates_source": False, | ||
| "manipulates_state": False, | ||
| } | ||
|
|
||
| def __init__(self) -> None: | ||
| """ | ||
| Parameters | ||
| ---------- | ||
| p, float | ||
| The probability to cooperate | ||
| """ | ||
| super().__init__() | ||
| self.minimum_cooperation_ratio = 0.25 | ||
| self.frequency_table = dict() | ||
| self.last_sequence = "" | ||
| self.current_sequence = "" | ||
|
|
||
| def strategy(self, opponent: Player) -> Action: | ||
| """This is the actual strategy""" | ||
| if len(self.history) > 5: | ||
| self.last_sequence = ( | ||
| str(opponent.history[-3]) | ||
| + str(self.history[-3]) | ||
| + str(opponent.history[-2]) | ||
| + str(self.history[-2]) | ||
| ) | ||
| self.current_sequence = ( | ||
| str(opponent.history[-2]) | ||
| + str(self.history[-2]) | ||
| + str(opponent.history[-1]) | ||
| + str(self.history[-1]) | ||
| ) | ||
| self.update_table(opponent) | ||
|
|
||
| if len(self.history) < 30: | ||
| # Play TitForTat | ||
| # First move | ||
| if not self.history: | ||
| return C | ||
| # React to the opponent's last move | ||
miller-ian marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if opponent.history[-1] == D: | ||
| return D | ||
| return C | ||
| else: | ||
miller-ian marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| try: | ||
| results = self.frequency_table[self.current_sequence] | ||
| cooperates = results.count(C) | ||
| if ( | ||
| cooperates / len(self.history) | ||
| ) > self.minimum_cooperation_ratio: | ||
| return C | ||
| return D | ||
| except: | ||
marcharper marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # React to the opponent's last move | ||
| if opponent.history[-1] == D: | ||
| return D | ||
| return C | ||
|
|
||
| def update_table(self, opponent: Player): | ||
| if self.last_sequence in self.frequency_table.keys(): | ||
| results = self.frequency_table[self.last_sequence] | ||
| results.append(opponent.history[-1]) | ||
| self.frequency_table[self.last_sequence] = results | ||
| else: | ||
| self.frequency_table[self.last_sequence] = [opponent.history[-1]] | ||
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,159 @@ | ||
| """Tests for the FrequencyAnalyzer strategy.""" | ||
|
|
||
| import axelrod as axl | ||
|
|
||
| from .test_player import TestPlayer | ||
|
|
||
| C, D = axl.Action.C, axl.Action.D | ||
|
|
||
|
|
||
| class Test(TestPlayer): | ||
|
|
||
| name = "FrequencyAnalyzer" | ||
| player = axl.FrequencyAnalyzer | ||
| expected_classifier = { | ||
| "memory_depth": float("inf"), | ||
| "stochastic": False, | ||
| "long_run_time": False, | ||
| "makes_use_of": set(), | ||
| "inspects_source": False, | ||
| "manipulates_source": False, | ||
| "manipulates_state": False, | ||
| } | ||
|
|
||
| def test_strategy_early(self): | ||
| # Test games that end while still in dataset generation phase (<30 turns) | ||
| opponent_actions = [C, C, D, C, D] | ||
| expected = [(C, C), (C, C), (C, D), (D, C), (C, D)] | ||
| self.versus_test( | ||
| axl.MockPlayer(opponent_actions), expected_actions=expected, seed=4 | ||
| ) | ||
|
|
||
| def test_strategy_defector(self): | ||
| # Test against all defections | ||
| opponent_actions = [D] * 30 | ||
| expected = [(C, D)] + [(D, D)] * 29 | ||
| self.versus_test( | ||
| axl.MockPlayer(opponent_actions), expected_actions=expected, seed=4 | ||
| ) | ||
|
|
||
| def test_strategy_cooperator(self): | ||
| # Test games that end while still in dataset generation phase (<30 turns) | ||
| opponent_actions = [C] * 30 | ||
| expected = [(C, C)] * 30 | ||
| self.versus_test( | ||
| axl.MockPlayer(opponent_actions), expected_actions=expected, seed=4 | ||
| ) | ||
|
|
||
| def test_strategy_random(self): | ||
| # Test of 50 turns against random strategy | ||
| opponent_actions = [ | ||
| C, | ||
| D, | ||
| D, | ||
| D, | ||
| D, | ||
| D, | ||
| D, | ||
| C, | ||
| D, | ||
| C, | ||
| D, | ||
| C, | ||
| D, | ||
| C, | ||
| D, | ||
| D, | ||
| C, | ||
| D, | ||
| C, | ||
| D, | ||
| D, | ||
| C, | ||
| D, | ||
| D, | ||
| D, | ||
| D, | ||
| D, | ||
| C, | ||
| C, | ||
| D, | ||
| D, | ||
| C, | ||
| C, | ||
| C, | ||
| D, | ||
| D, | ||
| C, | ||
| D, | ||
| C, | ||
| C, | ||
| C, | ||
| D, | ||
| D, | ||
| C, | ||
| C, | ||
| C, | ||
| D, | ||
| C, | ||
| D, | ||
| D, | ||
| ] | ||
| expected = [ | ||
| (C, C), | ||
| (C, D), | ||
| (D, D), | ||
| (D, D), | ||
| (D, D), | ||
| (D, D), | ||
| (D, D), | ||
| (D, C), | ||
| (C, D), | ||
| (D, C), | ||
| (C, D), | ||
| (D, C), | ||
| (C, D), | ||
| (D, C), | ||
| (C, D), | ||
| (D, D), | ||
| (D, C), | ||
| (C, D), | ||
| (D, C), | ||
| (C, D), | ||
| (D, D), | ||
| (D, C), | ||
| (C, D), | ||
| (D, D), | ||
| (D, D), | ||
| (D, D), | ||
| (D, D), | ||
| (D, C), | ||
| (C, C), | ||
| (C, D), # rd 30 (end of dataset generation phase) | ||
| (D, D), | ||
| (D, C), | ||
| ( | ||
| D, | ||
| C, | ||
| ), # example of non TFT (by this point, FrequencyAnalyzer is generally distrustful of opponent) | ||
| (C, C), | ||
| (D, D), | ||
| (D, D), | ||
| (D, C), | ||
| (D, D), | ||
| (D, C), | ||
| (D, C), | ||
| (D, C), | ||
| (D, D), | ||
| (D, D), | ||
| (D, C), | ||
| (D, C), | ||
| (D, C), | ||
| (D, D), | ||
| (D, C), | ||
| (D, D), | ||
| (D, D), | ||
| ] | ||
| self.versus_test( | ||
| axl.MockPlayer(opponent_actions), expected_actions=expected, seed=4 | ||
| ) |
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
Oops, something went wrong.
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.