From 73f0440933b73799169a1c0f7613fd9cd9b5d519 Mon Sep 17 00:00:00 2001 From: E Shaw Date: Sat, 1 Jul 2017 23:14:53 +0800 Subject: [PATCH 1/2] removed flip_action. note 2 docstrings in strategy_transformers.py --- axelrod/__init__.py | 2 +- axelrod/actions.py | 6 ------ axelrod/player.py | 6 +++--- axelrod/strategies/axelrod_first.py | 4 ++-- axelrod/strategies/axelrod_second.py | 4 ++-- axelrod/strategies/negation.py | 4 ++-- axelrod/strategy_transformers.py | 12 ++++++------ axelrod/tests/strategies/test_calculator.py | 3 +-- axelrod/tests/unit/test_actions.py | 9 +-------- axelrod/tests/unit/test_strategy_transformers.py | 7 +++---- 10 files changed, 21 insertions(+), 36 deletions(-) diff --git a/axelrod/__init__.py b/axelrod/__init__.py index d0195df4f..740f4ad0c 100644 --- a/axelrod/__init__.py +++ b/axelrod/__init__.py @@ -7,7 +7,7 @@ from .version import __version__ from .load_data_ import load_pso_tables, load_weights from . import graph -from .actions import Actions, flip_action +from .actions import Actions from .random_ import random_choice, seed, Pdf from .plot import Plot from .game import DefaultGame, Game diff --git a/axelrod/actions.py b/axelrod/actions.py index cc549654f..3e28b752e 100644 --- a/axelrod/actions.py +++ b/axelrod/actions.py @@ -55,12 +55,6 @@ def from_char(cls, character): Action = Actions -def flip_action(action: Action) -> Action: - if not isinstance(action, Action): - raise UnknownActionError('Not an Action') - return action.flip() - - def str_to_actions(actions: str) -> tuple: """Takes a string like 'CCDD' and returns a tuple of the appropriate actions.""" diff --git a/axelrod/player.py b/axelrod/player.py index 74149c67e..092ca7e04 100644 --- a/axelrod/player.py +++ b/axelrod/player.py @@ -6,7 +6,7 @@ import numpy as np -from axelrod.actions import Actions, flip_action +from axelrod.actions import Actions from .game import DefaultGame import types @@ -208,10 +208,10 @@ def __repr__(self): def _add_noise(noise, s1, s2): r = random.random() if r < noise: - s1 = flip_action(s1) + s1 = s1.flip() r = random.random() if r < noise: - s2 = flip_action(s2) + s2 = s2.flip() return s1, s2 def strategy(self, opponent): diff --git a/axelrod/strategies/axelrod_first.py b/axelrod/strategies/axelrod_first.py index 0620a4245..a2aad10bd 100644 --- a/axelrod/strategies/axelrod_first.py +++ b/axelrod/strategies/axelrod_first.py @@ -4,7 +4,7 @@ import random -from axelrod.actions import Actions, flip_action, Action +from axelrod.actions import Actions, Action from axelrod.player import Player from axelrod.random_ import random_choice from axelrod.strategy_transformers import FinalTransformer @@ -124,7 +124,7 @@ def strategy(self, opponent: Player) -> Action: if (c >= 0 and c >= alt): move = C elif (c >= 0 and c < alt) or (alt >= 0): - move = flip_action(self.history[-1]) + move = self.history[-1].flip() else: move = D return move diff --git a/axelrod/strategies/axelrod_second.py b/axelrod/strategies/axelrod_second.py index 0615a5bf5..525aaaa32 100644 --- a/axelrod/strategies/axelrod_second.py +++ b/axelrod/strategies/axelrod_second.py @@ -4,7 +4,7 @@ import random -from axelrod.actions import Actions, Action, flip_action +from axelrod.actions import Actions, Action from axelrod.player import Player from axelrod.random_ import random_choice @@ -139,7 +139,7 @@ def strategy(self, opponent: Player) -> Action: if len(self.history) in [1, 2]: return C # Alternate C and D - return flip_action(self.history[-1]) + return self.history[-1].flip() def reset(self): super().reset() diff --git a/axelrod/strategies/negation.py b/axelrod/strategies/negation.py index c623e8d8d..6fb3e0424 100644 --- a/axelrod/strategies/negation.py +++ b/axelrod/strategies/negation.py @@ -1,4 +1,4 @@ -from axelrod.actions import Actions, flip_action, Action +from axelrod.actions import Actions, Action from axelrod.player import Player from axelrod.random_ import random_choice @@ -31,4 +31,4 @@ def strategy(self, opponent: Player) -> Action: if not self.history: return random_choice() # Act opposite of opponent otherwise - return flip_action(opponent.history[-1]) + return opponent.history[-1].flip() diff --git a/axelrod/strategy_transformers.py b/axelrod/strategy_transformers.py index da0d30985..a5079ccd5 100644 --- a/axelrod/strategy_transformers.py +++ b/axelrod/strategy_transformers.py @@ -10,7 +10,7 @@ import inspect import random from numpy.random import choice -from .actions import Actions, flip_action +from .actions import Actions from .random_ import random_choice @@ -200,8 +200,8 @@ def generic_strategy_wrapper(player, opponent, proposed_action, *args, def flip_wrapper(player, opponent, action): - """Applies flip_action at the class level.""" - return flip_action(action) + """Applies Action.flip() at the class level.""" + return action.flip() FlipTransformer = StrategyTransformerFactory( @@ -233,17 +233,17 @@ def dual_wrapper(player, opponent, proposed_action): action = player.original_player.strategy(opponent) player.original_player.history.append(action) - return flip_action(action) + return action.flip() DualTransformer = StrategyTransformerFactory(dual_wrapper, name_prefix="Dual") def noisy_wrapper(player, opponent, action, noise=0.05): - """Applies flip_action at the class level.""" + """Applies Action.flip() at the class level.""" r = random.random() if r < noise: - return flip_action(action) + return action.flip() return action def noisy_reclassifier(original_classifier, noise): diff --git a/axelrod/tests/strategies/test_calculator.py b/axelrod/tests/strategies/test_calculator.py index 595ab754e..3aab8761d 100644 --- a/axelrod/tests/strategies/test_calculator.py +++ b/axelrod/tests/strategies/test_calculator.py @@ -2,7 +2,6 @@ import axelrod from .test_player import TestPlayer -from axelrod.actions import flip_action from axelrod._strategy_utils import detect_cycle C, D = axelrod.Actions.C, axelrod.Actions.D @@ -104,7 +103,7 @@ def get_joss_strategy_actions(opponent_moves: list, indices_to_flip: list) -> li if index == 0: out.append((C, action)) elif index in indices_to_flip: - out.append((flip_action(previous_action), action)) + out.append((previous_action.flip(), action)) else: out.append((previous_action, action)) return out diff --git a/axelrod/tests/unit/test_actions.py b/axelrod/tests/unit/test_actions.py index 977a6d37a..09e02a354 100644 --- a/axelrod/tests/unit/test_actions.py +++ b/axelrod/tests/unit/test_actions.py @@ -1,5 +1,5 @@ import unittest -from axelrod import Actions, flip_action +from axelrod import Actions from axelrod.actions import str_to_actions, UnknownActionError, actions_to_str C, D = Actions.C, Actions.D @@ -39,13 +39,6 @@ def test_from_char_error(self): self.assertRaises(UnknownActionError, Actions.from_char, 'd') self.assertRaises(UnknownActionError, Actions.from_char, 'A') - def test_flip_action(self): - self.assertEqual(flip_action(D), C) - self.assertEqual(flip_action(C), D) - - def test_flip_action_error(self): - self.assertRaises(UnknownActionError, flip_action, 'R') - def test_str_to_actions(self): self.assertEqual(str_to_actions(''), ()) self.assertEqual(str_to_actions('C'), (C, )) diff --git a/axelrod/tests/unit/test_strategy_transformers.py b/axelrod/tests/unit/test_strategy_transformers.py index 299ee8626..527bcc03d 100644 --- a/axelrod/tests/unit/test_strategy_transformers.py +++ b/axelrod/tests/unit/test_strategy_transformers.py @@ -2,7 +2,6 @@ import axelrod from axelrod.strategy_transformers import * -from axelrod.actions import flip_action from axelrod.tests.strategies.test_titfortat import TestTitForTat from axelrod.tests.strategies.test_cooperator import TestCooperator @@ -102,7 +101,7 @@ def test_dual_wsls_transformer(self): for _ in range(10): p2.play(p3) - self.assertEqual(p1.history, [flip_action(x) for x in p2.history]) + self.assertEqual(p1.history, [x.flip() for x in p2.history]) def test_dual_tft_transformer(self): """Tests that DualTransformer produces the opposite results when faced @@ -119,7 +118,7 @@ def test_dual_tft_transformer(self): for _ in range(10): p2.play(p3) - self.assertEqual(p1.history, [flip_action(x) for x in p2.history]) + self.assertEqual(p1.history, [x.flip() for x in p2.history]) def test_dual_majority_transformer(self): """Tests that DualTransformer produces the opposite results when faced @@ -136,7 +135,7 @@ def test_dual_majority_transformer(self): for _ in range(10): p2.play(p3) - self.assertEqual(p1.history, [flip_action(x) for x in p2.history]) + self.assertEqual(p1.history, [x.flip() for x in p2.history]) def test_jossann_transformer(self): """Tests the JossAnn transformer. From 2ecdf8ce8fcd5512a4f17d818a8538349c062a95 Mon Sep 17 00:00:00 2001 From: E Shaw Date: Mon, 3 Jul 2017 00:40:46 +0800 Subject: [PATCH 2/2] two docstring changes --- axelrod/strategy_transformers.py | 4 ++-- docs/tutorials/further_topics/noisy_tournaments.rst | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/axelrod/strategy_transformers.py b/axelrod/strategy_transformers.py index a5079ccd5..502a03f86 100644 --- a/axelrod/strategy_transformers.py +++ b/axelrod/strategy_transformers.py @@ -200,7 +200,7 @@ def generic_strategy_wrapper(player, opponent, proposed_action, *args, def flip_wrapper(player, opponent, action): - """Applies Action.flip() at the class level.""" + """Flips the player's original actions.""" return action.flip() @@ -240,7 +240,7 @@ def dual_wrapper(player, opponent, proposed_action): def noisy_wrapper(player, opponent, action, noise=0.05): - """Applies Action.flip() at the class level.""" + """Flips the player's actions with probability: `noise`.""" r = random.random() if r < noise: return action.flip() diff --git a/docs/tutorials/further_topics/noisy_tournaments.rst b/docs/tutorials/further_topics/noisy_tournaments.rst index 9e41b658d..40238935d 100644 --- a/docs/tutorials/further_topics/noisy_tournaments.rst +++ b/docs/tutorials/further_topics/noisy_tournaments.rst @@ -3,7 +3,7 @@ Noisy tournaments A common variation on iterated prisoner’s dilemma tournaments is to add stochasticity in the choice of actions, simply called noise. This noise is -introduced by flipping plays between ‘C’ and ‘D’ with some probability that is +introduced by flipping plays between C and D with some probability that is applied to all plays after they are delivered by the player [Bendor1993]_. The presence of this persistent background noise causes some strategies to