Skip to content
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

removed flip_action, now use C.flip() and D.flip() #1053

Merged
merged 2 commits into from
Jul 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion axelrod/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 0 additions & 6 deletions axelrod/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
6 changes: 3 additions & 3 deletions axelrod/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions axelrod/strategies/axelrod_first.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions axelrod/strategies/axelrod_second.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions axelrod/strategies/negation.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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()
12 changes: 6 additions & 6 deletions axelrod/strategy_transformers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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)
"""Flips the player's original actions."""
return action.flip()


FlipTransformer = StrategyTransformerFactory(
Expand Down Expand Up @@ -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."""
"""Flips the player's actions with probability: `noise`."""
r = random.random()
if r < noise:
return flip_action(action)
return action.flip()
return action

def noisy_reclassifier(original_classifier, noise):
Expand Down
3 changes: 1 addition & 2 deletions axelrod/tests/strategies/test_calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
9 changes: 1 addition & 8 deletions axelrod/tests/unit/test_actions.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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, ))
Expand Down
7 changes: 3 additions & 4 deletions axelrod/tests/unit/test_strategy_transformers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion docs/tutorials/further_topics/noisy_tournaments.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down