diff --git a/axelrod/__init__.py b/axelrod/__init__.py index 0cdd856db..7a9d50a55 100644 --- a/axelrod/__init__.py +++ b/axelrod/__init__.py @@ -4,7 +4,7 @@ # The order of imports matters! from .version import __version__ -from .load_data_ import load_lookerup_tables, load_pso_tables, load_weights +from .load_data_ import load_pso_tables, load_weights from . import graph from .actions import Actions, flip_action from .random_ import random_choice, seed diff --git a/axelrod/_strategy_utils.py b/axelrod/_strategy_utils.py index 72dcaab69..2df966719 100644 --- a/axelrod/_strategy_utils.py +++ b/axelrod/_strategy_utils.py @@ -2,8 +2,8 @@ import itertools from functools import lru_cache -from axelrod import update_history -from axelrod import Actions +from axelrod.player import update_history +from axelrod.actions import Actions from axelrod.strategies.cycler import Cycler diff --git a/axelrod/data/lookup_tables.csv b/axelrod/data/lookup_tables.csv deleted file mode 100644 index 231966900..000000000 --- a/axelrod/data/lookup_tables.csv +++ /dev/null @@ -1,19 +0,0 @@ -# Name (string), plays (int), opp_plays(int), starting_plays(int), pattern (str) -, 0, 0, 1, C, CD -, 0, 0, 2, CC, CDCD -, 0, 0, 3, CCC, CDDDCDDD -, 1, 1, 0, C, CDDD -, 1, 1, 1, C, CDDDDDCD -, 1, 1, 2, CC, CDDDDDDDDCCDCDDD -, 1, 1, 3, CCC, CDDDDDDDDDDDCDDDDCCDCDDDDCDDDDDD -, 1, 1, 4, CCCC, CDCCDDDDCDDDDDCCDCDCDDDDDCDDDDDDDCCDCDCDDDDDCDDDDCDDDDDDCDDDCDDD -, 2, 2, 0, CC, CDDDCDCDDCDDDDDD -, 2, 2, 1, C, CDCCCCDDDCCCDDDCCCDDCDCDDCCCDDDD -, 2, 2, 2, CC, CDCCDCCCDCDDDCCCDCDDDDDDDCDDDCDCDDDDCCDCCCCDDDDCCDDDDCCDCDDDDDDD -, 2, 2, 3, CCC, CDCCDCCCDCDCDDDCCDCCCDCCDDDCDDDDDDCCCDDCDCDCDDDCCDCDCCDDCCCCDDDCDDCDCCDDCCDDDDDDCDDDDDCDCDDD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CCCDDDDCDCCDDDDDCCDCDCCDCCDDDCCCDDDCDDCDDCCCDCCDCDCDDCDDDCDDDDDDCDDDCDDCCCDDDCCCCDDCDCDCDCCDDCCCDDCDCCDCCDCDCDDCCCDDCDDCCCCDCDDDCCDDDCCDDDCDDCDDDCDDDDDDCCCCCCDCDCCDDCCDDDDCDCCDCCCDCDCCDDCCDCDDDDCDDCCCCCCDDDDCCCCDCCDCCCDCDCCCCDDDCCCCCDDCDDCCCDDDDDDDDDDDDDDDDCCDDDDCDDCCDDDDDCDDDDCDDDCDCDDDDDCCCCCDCDDCDCCCDCCDDDCDDDCDCDDCDDDCDCDDDCCDDDCCDCCCCCCDCCDCDCCDCCDCDCCCDDCDCCCDDCCCCDDDDCCDCDCCDCCCDCCDCCDCCDDCCCDCCDDDDCDDCCCDDCCDDCCDCDDCCCDCDDDDCDCDCDDCDCDDDCCDDCDCCCDCDCCDDDCDCDCDCCDCDDCCCCDCDCCDDDCDCDDDDDDDDDDDDDDDDDDDDCDDDCDCDCCCDCDCDCDCCDDDDDCDDCCCDDDDCDDDDDDDCDDDCCCDCCCCDCCDDDDDDDDDCDDDCDDDDCDCDCCCDDDDCCCDCDCCCDCCCDCDDDCCDDDCDDDDCCCCDCCDCDDDDCCCCDDCDDCDDDCDCDDCDDCDCCDCCDCCDCDCDCCDCCDCCDCCCDDDDDCCDDDDDDCDCCCCCDDCDCCDCDDDDDCDDDDDDCDDDDDDDCDCDDDDDCCDDCCCCDDDDDDDDDDDDDDDD diff --git a/axelrod/interaction_utils.py b/axelrod/interaction_utils.py index b2bf20b89..78f0c8f39 100644 --- a/axelrod/interaction_utils.py +++ b/axelrod/interaction_utils.py @@ -10,7 +10,7 @@ from collections import Counter import csv -from axelrod import Actions +from axelrod.actions import Actions from .game import Game import tqdm diff --git a/axelrod/load_data_.py b/axelrod/load_data_.py index ded3c536a..cfd3e32a8 100644 --- a/axelrod/load_data_.py +++ b/axelrod/load_data_.py @@ -29,16 +29,6 @@ def load_weights(filename="ann_weights.csv", directory="data"): return d -def load_lookerup_tables(filename="lookup_tables.csv", directory="data"): - """Load lookup tables.""" - rows = load_file(filename, directory) - d = dict() - for row in rows: - name, a, b, c, initial, pattern = row - d[(name, int(a), int(b), int(c))] = (initial, pattern) - return d - - def load_pso_tables(filename="pso_gambler.csv", directory="data"): """Load lookup tables.""" rows = load_file(filename, directory) diff --git a/axelrod/match.py b/axelrod/match.py index bf0e88a3e..ea35440b7 100644 --- a/axelrod/match.py +++ b/axelrod/match.py @@ -1,4 +1,5 @@ -from axelrod import Actions, Game +from axelrod.actions import Actions +from axelrod.game import Game import axelrod.interaction_utils as iu from .deterministic_cache import DeterministicCache diff --git a/axelrod/mock_player.py b/axelrod/mock_player.py index 7b56a81d1..9396d591e 100644 --- a/axelrod/mock_player.py +++ b/axelrod/mock_player.py @@ -1,6 +1,6 @@ import warnings -from axelrod import Actions, Player, update_history, update_state_distribution -from axelrod.actions import Action +from axelrod.actions import Actions, Action +from axelrod.player import Player, update_history, update_state_distribution from collections import defaultdict from typing import List, Tuple diff --git a/axelrod/player.py b/axelrod/player.py index 56f40680a..284c089b6 100644 --- a/axelrod/player.py +++ b/axelrod/player.py @@ -3,7 +3,7 @@ import random import copy -from axelrod import Actions, flip_action +from axelrod.actions import Actions, flip_action from .game import DefaultGame diff --git a/axelrod/random_.py b/axelrod/random_.py index 35f0de547..bb0afb5aa 100644 --- a/axelrod/random_.py +++ b/axelrod/random_.py @@ -1,7 +1,6 @@ import random import numpy -from axelrod import Actions -from axelrod.actions import Action +from axelrod.actions import Action, Actions def random_choice(p: float = 0.5) -> Action: diff --git a/axelrod/result_set.py b/axelrod/result_set.py index a0501681e..73254bedc 100644 --- a/axelrod/result_set.py +++ b/axelrod/result_set.py @@ -4,7 +4,7 @@ from numpy import mean, nanmedian, std import tqdm -from axelrod import Actions +from axelrod.actions import Actions import axelrod.interaction_utils as iu from . import eigen from .game import Game diff --git a/axelrod/strategies/_strategies.py b/axelrod/strategies/_strategies.py index 44bfa5665..322b3f251 100644 --- a/axelrod/strategies/_strategies.py +++ b/axelrod/strategies/_strategies.py @@ -44,12 +44,7 @@ MathConstantHunter, RandomHunter, EventualCycleHunter) from .inverse import Inverse from .lookerup import (LookerUp, - EvolvedLookerUp0_0_1, EvolvedLookerUp0_0_2, EvolvedLookerUp0_0_3, - EvolvedLookerUp1_1_0, EvolvedLookerUp1_1_1, EvolvedLookerUp1_1_2, - EvolvedLookerUp1_1_3, EvolvedLookerUp1_1_4, EvolvedLookerUp2_2_0, - EvolvedLookerUp2_2_1, EvolvedLookerUp2_2_2, EvolvedLookerUp2_2_3, - EvolvedLookerUp2_2_4, EvolvedLookerUp3_3_1, EvolvedLookerUp3_3_2, - EvolvedLookerUp3_3_3, EvolvedLookerUp4_4_1, EvolvedLookerUp4_4_2, + EvolvedLookerUp1_1_1, EvolvedLookerUp2_2_2, Winner12, Winner21) from .mathematicalconstants import Golden, Pi, e from .memoryone import ( diff --git a/axelrod/strategies/adaptive.py b/axelrod/strategies/adaptive.py index 630d0cdae..1844e5179 100644 --- a/axelrod/strategies/adaptive.py +++ b/axelrod/strategies/adaptive.py @@ -1,4 +1,5 @@ -from axelrod import Actions, Player +from axelrod.actions import Actions +from axelrod.player import Player import axelrod from typing import List diff --git a/axelrod/strategies/alternator.py b/axelrod/strategies/alternator.py index 5e066ce2c..7fa292cae 100644 --- a/axelrod/strategies/alternator.py +++ b/axelrod/strategies/alternator.py @@ -1,5 +1,5 @@ -from axelrod import Actions, Player -from axelrod.actions import Action +from axelrod.actions import Action, Actions +from axelrod.player import Player C, D = Actions.C, Actions.D diff --git a/axelrod/strategies/ann.py b/axelrod/strategies/ann.py index 1ed8a6f38..25892da0a 100644 --- a/axelrod/strategies/ann.py +++ b/axelrod/strategies/ann.py @@ -6,7 +6,9 @@ import numpy as np -from axelrod import Actions, Player, load_weights +from axelrod.actions import Actions +from axelrod.player import Player +from axelrod.load_data_ import load_weights C, D = Actions.C, Actions.D nn_weights = load_weights() @@ -141,41 +143,6 @@ def split_weights(weights, num_features, num_hidden): return (input2hidden, hidden2output, bias) -def activate(bias, hidden, output, inputs): - """ - Compute the output of the neural network: - output = relu(inputs * hidden_weights + bias) * output_weights - """ - inputs = np.array(inputs) - hidden_values = bias + np.dot(hidden, inputs) - hidden_values = relu(hidden_values) - output_value = np.dot(hidden_values, output) - return output_value - - -def split_weights(weights, num_features, num_hidden): - """Splits the input vector into the the NN bias weights and layer - parameters.""" - # Check weights is the right length - expected_length = num_hidden * 2 + num_features * num_hidden - if expected_length != len(weights): - raise ValueError("NN weights array has an incorrect size.") - - number_of_input_to_hidden_weights = num_features * num_hidden - number_of_hidden_to_output_weights = num_hidden - - input2hidden = [] - for i in range(0, number_of_input_to_hidden_weights, num_features): - input2hidden.append(weights[i:i + num_features]) - - start = number_of_input_to_hidden_weights - end = number_of_input_to_hidden_weights + number_of_hidden_to_output_weights - - hidden2output = weights[start: end] - bias = weights[end:] - return (input2hidden, hidden2output, bias) - - class ANN(Player): """A single layer neural network based strategy, with the following features: diff --git a/axelrod/strategies/apavlov.py b/axelrod/strategies/apavlov.py index 201c139bb..8b144f47c 100644 --- a/axelrod/strategies/apavlov.py +++ b/axelrod/strategies/apavlov.py @@ -1,4 +1,5 @@ -from axelrod import Actions, Player +from axelrod.actions import Actions +from axelrod.player import Player C, D = Actions.C, Actions.D diff --git a/axelrod/strategies/appeaser.py b/axelrod/strategies/appeaser.py index 806050b61..3d273d7e4 100644 --- a/axelrod/strategies/appeaser.py +++ b/axelrod/strategies/appeaser.py @@ -1,5 +1,5 @@ -from axelrod import Actions, Player -from axelrod.actions import Action +from axelrod.actions import Action, Actions +from axelrod.player import Player C, D = Actions.C, Actions.D diff --git a/axelrod/strategies/averagecopier.py b/axelrod/strategies/averagecopier.py index 3568997a1..af38abcf3 100644 --- a/axelrod/strategies/averagecopier.py +++ b/axelrod/strategies/averagecopier.py @@ -1,5 +1,6 @@ -from axelrod import Actions, Player, random_choice -from axelrod.actions import Action +from axelrod.actions import Actions, Action +from axelrod.player import Player +from axelrod.random_ import random_choice C, D = Actions.C, Actions.D @@ -21,8 +22,7 @@ class AverageCopier(Player): 'manipulates_state': False } - @staticmethod - def strategy(opponent: Player) -> Action: + def strategy(self, opponent: Player) -> Action: if len(opponent.history) == 0: # Randomly picks a strategy (not affected by history). return random_choice(0.5) @@ -44,8 +44,7 @@ class NiceAverageCopier(Player): 'manipulates_state': False } - @staticmethod - def strategy(opponent: Player) -> Action: + def strategy(self, opponent: Player) -> Action: if len(opponent.history) == 0: return C p = opponent.cooperations // len(opponent.history) diff --git a/axelrod/strategies/axelrod_first.py b/axelrod/strategies/axelrod_first.py index 6a014b3a3..8668aad75 100644 --- a/axelrod/strategies/axelrod_first.py +++ b/axelrod/strategies/axelrod_first.py @@ -4,7 +4,9 @@ import random -from axelrod import Actions, Player, flip_action, random_choice +from axelrod.actions import Actions, flip_action +from axelrod.player import Player +from axelrod.random_ import random_choice from.memoryone import MemoryOnePlayer C, D = Actions.C, Actions.D diff --git a/axelrod/strategies/axelrod_second.py b/axelrod/strategies/axelrod_second.py index e35223433..0615a5bf5 100644 --- a/axelrod/strategies/axelrod_second.py +++ b/axelrod/strategies/axelrod_second.py @@ -4,8 +4,9 @@ import random -from axelrod import Actions, Player, flip_action, random_choice -from axelrod.actions import Action +from axelrod.actions import Actions, Action, flip_action +from axelrod.player import Player +from axelrod.random_ import random_choice C, D = Actions.C, Actions.D diff --git a/axelrod/strategies/backstabber.py b/axelrod/strategies/backstabber.py index e8f27f5a7..b6b892f8a 100644 --- a/axelrod/strategies/backstabber.py +++ b/axelrod/strategies/backstabber.py @@ -1,4 +1,5 @@ -from axelrod import Actions, Player +from axelrod.actions import Actions +from axelrod.player import Player from axelrod.strategy_transformers import FinalTransformer from axelrod.actions import Action diff --git a/axelrod/strategies/better_and_better.py b/axelrod/strategies/better_and_better.py index 19259f371..35a66fdfc 100644 --- a/axelrod/strategies/better_and_better.py +++ b/axelrod/strategies/better_and_better.py @@ -1,4 +1,6 @@ -from axelrod import Actions, Player, random_choice +from axelrod.actions import Actions +from axelrod.player import Player +from axelrod.random_ import random_choice from axelrod.actions import Action C, D = Actions.C, Actions.D diff --git a/axelrod/strategies/calculator.py b/axelrod/strategies/calculator.py index 413232a40..8fdc4b2b4 100644 --- a/axelrod/strategies/calculator.py +++ b/axelrod/strategies/calculator.py @@ -1,4 +1,5 @@ -from axelrod import Actions, Player +from axelrod.actions import Actions +from axelrod.player import Player from .axelrod_first import Joss from axelrod._strategy_utils import detect_cycle from axelrod.actions import Action diff --git a/axelrod/strategies/cooperator.py b/axelrod/strategies/cooperator.py index 4ed0cdf08..239a56711 100644 --- a/axelrod/strategies/cooperator.py +++ b/axelrod/strategies/cooperator.py @@ -1,5 +1,5 @@ -from axelrod import Actions, Player -from axelrod.actions import Action +from axelrod.actions import Actions, Action +from axelrod.player import Player C, D = Actions.C, Actions.D diff --git a/axelrod/strategies/cycler.py b/axelrod/strategies/cycler.py index 3acde1841..728a911b8 100644 --- a/axelrod/strategies/cycler.py +++ b/axelrod/strategies/cycler.py @@ -1,5 +1,5 @@ -from axelrod import Actions, Player, init_args -from axelrod.actions import Action +from axelrod.actions import Actions, Action +from axelrod.player import Player, init_args import copy diff --git a/axelrod/strategies/darwin.py b/axelrod/strategies/darwin.py index 1d76ad815..2790e90a1 100644 --- a/axelrod/strategies/darwin.py +++ b/axelrod/strategies/darwin.py @@ -1,6 +1,6 @@ import inspect -from axelrod import Actions, Player -from axelrod.actions import Action +from axelrod.actions import Action, Actions +from axelrod.player import Player from typing import List diff --git a/axelrod/strategies/defector.py b/axelrod/strategies/defector.py index 4f50794c0..ff6a4a5fd 100644 --- a/axelrod/strategies/defector.py +++ b/axelrod/strategies/defector.py @@ -1,5 +1,5 @@ -from axelrod import Actions, Player -from axelrod.actions import Action +from axelrod.actions import Actions, Action +from axelrod.player import Player C, D = Actions.C, Actions.D diff --git a/axelrod/strategies/doubler.py b/axelrod/strategies/doubler.py index 3f9c1b7b2..7fc3d998e 100644 --- a/axelrod/strategies/doubler.py +++ b/axelrod/strategies/doubler.py @@ -1,4 +1,5 @@ -from axelrod import Actions, Player +from axelrod.actions import Actions +from axelrod.player import Player C, D = Actions.C, Actions.D diff --git a/axelrod/strategies/finite_state_machines.py b/axelrod/strategies/finite_state_machines.py index 2ff850b6a..fc013d1b7 100644 --- a/axelrod/strategies/finite_state_machines.py +++ b/axelrod/strategies/finite_state_machines.py @@ -1,4 +1,5 @@ -from axelrod import Actions, Player +from axelrod.actions import Actions +from axelrod.player import Player C, D = Actions.C, Actions.D diff --git a/axelrod/strategies/forgiver.py b/axelrod/strategies/forgiver.py index 268aded56..4c9ed9e7a 100644 --- a/axelrod/strategies/forgiver.py +++ b/axelrod/strategies/forgiver.py @@ -1,4 +1,5 @@ -from axelrod import Actions, Player +from axelrod.actions import Actions +from axelrod.player import Player from axelrod.actions import Action C, D = Actions.C, Actions.D @@ -21,8 +22,7 @@ class Forgiver(Player): 'manipulates_state': False } - @staticmethod - def strategy(opponent: Player) -> Action: + def strategy(self, opponent: Player) -> Action: """ Begins by playing C, then plays D if the opponent has defected more than 10 percent of the time """ @@ -49,8 +49,7 @@ class ForgivingTitForTat(Player): 'manipulates_state': False } - @staticmethod - def strategy(opponent: Player) -> Action: + def strategy(self, opponent: Player) -> Action: """ Begins by playing C, then plays D if, the opponent has defected more than 10 percent of the time, diff --git a/axelrod/strategies/gambler.py b/axelrod/strategies/gambler.py index 442aa2339..9ee1a599e 100644 --- a/axelrod/strategies/gambler.py +++ b/axelrod/strategies/gambler.py @@ -5,7 +5,9 @@ https://gist.github.com/GDKO/60c3d0fd423598f3c4e4 """ -from axelrod import Actions, load_pso_tables, random_choice +from axelrod.actions import Actions +from axelrod.load_data_ import load_pso_tables +from axelrod.random_ import random_choice from .lookerup import LookerUp, create_lookup_table_from_pattern diff --git a/axelrod/strategies/geller.py b/axelrod/strategies/geller.py index 08b4f3838..4c61b62b0 100644 --- a/axelrod/strategies/geller.py +++ b/axelrod/strategies/geller.py @@ -1,6 +1,8 @@ import inspect -from axelrod import Actions, Player, random_choice +from axelrod.actions import Actions +from axelrod.player import Player +from axelrod.random_ import random_choice C, D = Actions.C, Actions.D diff --git a/axelrod/strategies/gobymajority.py b/axelrod/strategies/gobymajority.py index 7634ac861..e96078350 100644 --- a/axelrod/strategies/gobymajority.py +++ b/axelrod/strategies/gobymajority.py @@ -1,4 +1,5 @@ -from axelrod import Actions, Player +from axelrod.actions import Actions +from axelrod.player import Player import copy diff --git a/axelrod/strategies/gradualkiller.py b/axelrod/strategies/gradualkiller.py index e8b4ff285..4a07d5dbf 100644 --- a/axelrod/strategies/gradualkiller.py +++ b/axelrod/strategies/gradualkiller.py @@ -1,4 +1,5 @@ -from axelrod import Actions, Player +from axelrod.actions import Actions +from axelrod.player import Player from axelrod.strategy_transformers import InitialTransformer from axelrod.actions import Action diff --git a/axelrod/strategies/grudger.py b/axelrod/strategies/grudger.py index f66167d33..a7303abc4 100644 --- a/axelrod/strategies/grudger.py +++ b/axelrod/strategies/grudger.py @@ -1,5 +1,5 @@ -from axelrod import Actions, Player -from axelrod.actions import Action +from axelrod.actions import Actions, Action +from axelrod.player import Player C, D = Actions.C, Actions.D diff --git a/axelrod/strategies/grumpy.py b/axelrod/strategies/grumpy.py index edbf889bb..c7d64ec28 100644 --- a/axelrod/strategies/grumpy.py +++ b/axelrod/strategies/grumpy.py @@ -1,5 +1,5 @@ -from axelrod import Actions, Player -from axelrod.actions import Action +from axelrod.actions import Actions, Action +from axelrod.player import Player C, D = Actions.C, Actions.D diff --git a/axelrod/strategies/handshake.py b/axelrod/strategies/handshake.py index eef6f6043..c5239910f 100644 --- a/axelrod/strategies/handshake.py +++ b/axelrod/strategies/handshake.py @@ -1,4 +1,5 @@ -from axelrod import Actions, Player +from axelrod.actions import Actions +from axelrod.player import Player C, D = Actions.C, Actions.D diff --git a/axelrod/strategies/hmm.py b/axelrod/strategies/hmm.py index a4887def5..c35c17fd6 100644 --- a/axelrod/strategies/hmm.py +++ b/axelrod/strategies/hmm.py @@ -1,6 +1,8 @@ from numpy.random import choice -from axelrod import Actions, Player, random_choice +from axelrod.actions import Actions +from axelrod.player import Player +from axelrod.random_ import random_choice C, D = Actions.C, Actions.D diff --git a/axelrod/strategies/human.py b/axelrod/strategies/human.py index 0965fa2b9..13176a132 100644 --- a/axelrod/strategies/human.py +++ b/axelrod/strategies/human.py @@ -1,5 +1,6 @@ from os import linesep -from axelrod import Actions, Player +from axelrod.actions import Actions +from axelrod.player import Player from prompt_toolkit import prompt from prompt_toolkit.token import Token from prompt_toolkit.styles import style_from_dict diff --git a/axelrod/strategies/hunter.py b/axelrod/strategies/hunter.py index 5d663e3eb..5f5494c82 100644 --- a/axelrod/strategies/hunter.py +++ b/axelrod/strategies/hunter.py @@ -1,4 +1,5 @@ -from axelrod import Actions, Player +from axelrod.actions import Actions +from axelrod.player import Player from axelrod._strategy_utils import detect_cycle C, D = Actions.C, Actions.D diff --git a/axelrod/strategies/inverse.py b/axelrod/strategies/inverse.py index 03f407b16..7690b0dc7 100644 --- a/axelrod/strategies/inverse.py +++ b/axelrod/strategies/inverse.py @@ -1,4 +1,6 @@ -from axelrod import Actions, Player, random_choice +from axelrod.actions import Actions +from axelrod.player import Player +from axelrod.random_ import random_choice C, D = Actions.C, Actions.D diff --git a/axelrod/strategies/lookerup.py b/axelrod/strategies/lookerup.py index a1b1ad742..36b949af6 100644 --- a/axelrod/strategies/lookerup.py +++ b/axelrod/strategies/lookerup.py @@ -1,17 +1,10 @@ -from functools import partial from itertools import product -import sys -from axelrod import Actions, Player, init_args, load_lookerup_tables +from axelrod.actions import Actions +from axelrod.player import Player -module = sys.modules[__name__] C, D = Actions.C, Actions.D -# Dictionary of table patterns -# Keys are (name, plays, op_plays, op_start_plays) -patterns = load_lookerup_tables() - - def create_lookup_table_keys(plays, op_plays, op_start_plays): """Creates the keys for a lookup table.""" self_histories = [''.join(x) for x in product('CD', repeat=plays)] @@ -99,12 +92,20 @@ class LookerUp(Player): 'manipulates_state': False } - def __init__(self, lookup_table=None, initial_actions=None): + def __init__(self, lookup_table=None, initial_actions=None, + lookup_pattern=None, parameters=None): """ If no lookup table is provided to the constructor, then use the TFT one. """ super().__init__() + if lookup_pattern is not None: + plays, op_plays, op_start_plays = parameters + lookup_table = create_lookup_table_from_pattern(plays, + op_plays, + op_start_plays, + pattern=lookup_pattern) + if not lookup_table: lookup_table = { ('', 'C', 'D'): D, @@ -165,23 +166,31 @@ def strategy(self, opponent): return action -# Create several classes at runtime based on the data in the look up tables -# loaded above, one for each key. +class EvolvedLookerUp1_1_1(LookerUp): + """ + A 1 1 1 Lookerup trained with an evolutionary algorithm. + + Names: + - Evolved Lookerup 1 1 1: Original name by Marc Harper + """ + name = "EvolvedLookerUp1_1_1" + def __init__(self): + super().__init__(parameters=(1, 1, 1), lookup_pattern='CDDDDDCD', + initial_actions=(C,)) + + +class EvolvedLookerUp2_2_2(LookerUp): + """ + A 2 2 2 Lookerup trained with an evolutionary algorithm. -for k, (initial, pattern) in patterns.items(): - name, plays, op_plays, op_start_plays = k - table = create_lookup_table_from_pattern( - plays, op_plays, op_start_plays, pattern) - class_name = "EvolvedLookerUp{}{}_{}_{}".format( - name, plays, op_plays, op_start_plays) - # Dynamically create the class - new_class = type(class_name, (LookerUp,), {}) - new_class.__init__ = init_args(partial( - new_class.__init__, lookup_table=table, initial_actions=initial)) - new_class.name = class_name - # Add the generated class to the module dictionary so it can be - # imported into other modules - setattr(module, class_name, new_class) + Names: + - Evolved Lookerup 2 2 2: Original name by Marc Harper + """ + name = "EvolvedLookerUp2_2_2" + def __init__(self): + pattern = 'CDCCDCCCDCDDDCCCDCDDDDDDDCDDDCDCDDDDCCDCCCCDDDDCCDDDDCCDCDDDDDDD' + super().__init__(parameters=(2, 2, 2), lookup_pattern=pattern, + initial_actions=(C, C)) class Winner12(LookerUp): @@ -192,16 +201,10 @@ class Winner12(LookerUp): - Winner12 [Mathieu2015]_ """ name = "Winner12" - def __init__(self): - lookup_table_keys = create_lookup_table_keys( - plays=1, op_plays=2, op_start_plays=0) - pattern = 'CDCDDCDD' - # Zip together the keys and the action pattern to get the lookup table. - lookup_table = dict(zip(lookup_table_keys, pattern)) - super().__init__(lookup_table=lookup_table, - initial_actions=(C, C)) + super().__init__(parameters=(1, 2, 0), lookup_pattern=pattern, + initial_actions=(C, C)) class Winner21(LookerUp): @@ -212,13 +215,7 @@ class Winner21(LookerUp): - Winner21 [Mathieu2015]_ """ name = "Winner21" - def __init__(self): - lookup_table_keys = create_lookup_table_keys( - plays=2, op_plays=1, op_start_plays=0) - pattern = 'CDCDCDDD' - # Zip together the keys and the action pattern to get the lookup table. - lookup_table = dict(zip(lookup_table_keys, pattern)) - super().__init__(lookup_table=lookup_table, - initial_actions=(D, C)) + super().__init__(parameters=(1, 2, 0), lookup_pattern=pattern, + initial_actions=(D, C)) diff --git a/axelrod/strategies/mathematicalconstants.py b/axelrod/strategies/mathematicalconstants.py index d6b7bca14..6c21ac7f3 100644 --- a/axelrod/strategies/mathematicalconstants.py +++ b/axelrod/strategies/mathematicalconstants.py @@ -1,6 +1,6 @@ -from math import e, pi, sqrt - -from axelrod import Actions, Player +import math +from axelrod.actions import Actions +from axelrod.player import Player class CotoDeRatio(Player): @@ -37,7 +37,7 @@ class Golden(CotoDeRatio): defections closer to the golden mean""" name = '$\phi$' - ratio = (1 + sqrt(5)) / 2 + ratio = (1 + math.sqrt(5)) / 2 class Pi(CotoDeRatio): @@ -45,7 +45,7 @@ class Pi(CotoDeRatio): defections closer to the pi""" name = '$\pi$' - ratio = pi + ratio = math.pi class e(CotoDeRatio): @@ -53,4 +53,4 @@ class e(CotoDeRatio): defections closer to the e""" name = '$e$' - ratio = e + ratio = math.e diff --git a/axelrod/strategies/memoryone.py b/axelrod/strategies/memoryone.py index e574ecb05..9a9ac5246 100644 --- a/axelrod/strategies/memoryone.py +++ b/axelrod/strategies/memoryone.py @@ -1,4 +1,6 @@ -from axelrod import Actions, Player, random_choice +from axelrod.actions import Actions +from axelrod.player import Player +from axelrod.random_ import random_choice C, D = Actions.C, Actions.D diff --git a/axelrod/strategies/memorytwo.py b/axelrod/strategies/memorytwo.py index fce8993a4..9b51ed969 100644 --- a/axelrod/strategies/memorytwo.py +++ b/axelrod/strategies/memorytwo.py @@ -1,4 +1,5 @@ -from axelrod import Actions, Player +from axelrod.actions import Actions +from axelrod.player import Player from .titfortat import TitForTat, TitFor2Tats from .defector import Defector diff --git a/axelrod/strategies/meta.py b/axelrod/strategies/meta.py index f4779ae08..04842e7e2 100644 --- a/axelrod/strategies/meta.py +++ b/axelrod/strategies/meta.py @@ -1,4 +1,5 @@ -from axelrod import Actions, Player, obey_axelrod +from axelrod.actions import Actions +from axelrod.player import Player, obey_axelrod from axelrod.strategy_transformers import NiceTransformer from ._strategies import all_strategies from .hunter import ( diff --git a/axelrod/strategies/mindcontrol.py b/axelrod/strategies/mindcontrol.py index 8be00050b..02db2b544 100644 --- a/axelrod/strategies/mindcontrol.py +++ b/axelrod/strategies/mindcontrol.py @@ -1,4 +1,5 @@ -from axelrod import Actions, Player +from axelrod.actions import Actions +from axelrod.player import Player C, D = Actions.C, Actions.D diff --git a/axelrod/strategies/mindreader.py b/axelrod/strategies/mindreader.py index 7cbb77483..1e524616b 100644 --- a/axelrod/strategies/mindreader.py +++ b/axelrod/strategies/mindreader.py @@ -1,6 +1,7 @@ import inspect -from axelrod import Actions, Player +from axelrod.actions import Actions +from axelrod.player import Player from axelrod._strategy_utils import look_ahead diff --git a/axelrod/strategies/mutual.py b/axelrod/strategies/mutual.py index 53c5a3e72..ed5e725ae 100644 --- a/axelrod/strategies/mutual.py +++ b/axelrod/strategies/mutual.py @@ -1,4 +1,5 @@ -from axelrod import Actions, Player +from axelrod.actions import Actions +from axelrod.player import Player from axelrod.random_ import random_choice C, D = Actions.C, Actions.D diff --git a/axelrod/strategies/negation.py b/axelrod/strategies/negation.py index 248143eee..8304d8fc4 100644 --- a/axelrod/strategies/negation.py +++ b/axelrod/strategies/negation.py @@ -1,4 +1,6 @@ -from axelrod import Actions, Player, random_choice, flip_action +from axelrod.actions import Actions, flip_action +from axelrod.player import Player +from axelrod.random_ import random_choice C, D = Actions.C, Actions.D diff --git a/axelrod/strategies/oncebitten.py b/axelrod/strategies/oncebitten.py index 497352406..d36c36207 100644 --- a/axelrod/strategies/oncebitten.py +++ b/axelrod/strategies/oncebitten.py @@ -1,5 +1,6 @@ import random -from axelrod import Actions, Player +from axelrod.actions import Actions +from axelrod.player import Player C, D = Actions.C, Actions.D diff --git a/axelrod/strategies/prober.py b/axelrod/strategies/prober.py index 66c77a540..a95c2f603 100644 --- a/axelrod/strategies/prober.py +++ b/axelrod/strategies/prober.py @@ -1,4 +1,6 @@ -from axelrod import Actions, Player, random_choice +from axelrod.actions import Actions +from axelrod.player import Player +from axelrod.random_ import random_choice import random diff --git a/axelrod/strategies/punisher.py b/axelrod/strategies/punisher.py index 30184ae08..b72722c86 100644 --- a/axelrod/strategies/punisher.py +++ b/axelrod/strategies/punisher.py @@ -1,4 +1,5 @@ -from axelrod import Actions, Player +from axelrod.actions import Actions +from axelrod.player import Player C, D = Actions.C, Actions.D diff --git a/axelrod/strategies/qlearner.py b/axelrod/strategies/qlearner.py index 7e4c396e3..1e995f605 100644 --- a/axelrod/strategies/qlearner.py +++ b/axelrod/strategies/qlearner.py @@ -1,7 +1,9 @@ from collections import OrderedDict import random -from axelrod import Actions, Player, random_choice +from axelrod.actions import Actions +from axelrod.player import Player +from axelrod.random_ import random_choice C, D = Actions.C, Actions.D diff --git a/axelrod/strategies/rand.py b/axelrod/strategies/rand.py index 530cb1f04..2fc4b0b21 100644 --- a/axelrod/strategies/rand.py +++ b/axelrod/strategies/rand.py @@ -1,4 +1,5 @@ -from axelrod import Player, random_choice +from axelrod.player import Player +from axelrod.random_ import random_choice class Random(Player): diff --git a/axelrod/strategies/retaliate.py b/axelrod/strategies/retaliate.py index 614da0cfb..499bc1ff4 100644 --- a/axelrod/strategies/retaliate.py +++ b/axelrod/strategies/retaliate.py @@ -1,6 +1,7 @@ from collections import defaultdict -from axelrod import Actions, Player +from axelrod.actions import Actions +from axelrod.player import Player C, D = Actions.C, Actions.D diff --git a/axelrod/strategies/sequence_player.py b/axelrod/strategies/sequence_player.py index 7ddb765d8..59bd4f2c3 100644 --- a/axelrod/strategies/sequence_player.py +++ b/axelrod/strategies/sequence_player.py @@ -1,4 +1,5 @@ -from axelrod import Actions, Player +from axelrod.actions import Actions +from axelrod.player import Player from axelrod._strategy_utils import thue_morse_generator C, D = Actions.C, Actions.D diff --git a/axelrod/strategies/titfortat.py b/axelrod/strategies/titfortat.py index 196e0fe6a..89b29ec84 100644 --- a/axelrod/strategies/titfortat.py +++ b/axelrod/strategies/titfortat.py @@ -1,4 +1,5 @@ -from axelrod import Actions, Player +from axelrod.actions import Actions +from axelrod.player import Player from axelrod.strategy_transformers import TrackHistoryTransformer C, D = Actions.C, Actions.D diff --git a/axelrod/strategies/worse_and_worse.py b/axelrod/strategies/worse_and_worse.py index 890e1e493..346d37deb 100644 --- a/axelrod/strategies/worse_and_worse.py +++ b/axelrod/strategies/worse_and_worse.py @@ -1,4 +1,6 @@ -from axelrod import Actions, Player, random_choice +from axelrod.actions import Actions +from axelrod.player import Player +from axelrod.random_ import random_choice C, D = Actions.C, Actions.D diff --git a/axelrod/tests/unit/test_gradualkiller.py b/axelrod/tests/unit/test_gradualkiller.py index f400f33a4..cf900f61b 100644 --- a/axelrod/tests/unit/test_gradualkiller.py +++ b/axelrod/tests/unit/test_gradualkiller.py @@ -77,7 +77,7 @@ def test_effect_of_strategy_with_history_DC(self): P2.history = [C, C, C, C, C, D, C, C, D, C] self.assertEqual(P1.strategy(P2), 'C') - def test_effect_of_strategy_with_history_CC(self): + def test_effect_of_strategy_with_history_DD(self): """Continues with D if opponent played DD on 6 and 7.""" P1 = axelrod.GradualKiller() P2 = axelrod.Player() diff --git a/axelrod/tests/unit/test_lookerup.py b/axelrod/tests/unit/test_lookerup.py index 001a2aeed..327040092 100755 --- a/axelrod/tests/unit/test_lookerup.py +++ b/axelrod/tests/unit/test_lookerup.py @@ -10,28 +10,6 @@ C, D = axelrod.Actions.C, axelrod.Actions.D -class TestCreateTables(unittest.TestCase): - def test_create_lookup_table_keys(self): - table = create_lookup_table_keys(1, 1, 1) - self.assertEqual(8, len(table)) - table = create_lookup_table_keys(0, 1, 1) - self.assertEqual(4, len(table)) - table = create_lookup_table_keys(1, 0, 1) - self.assertEqual(4, len(table)) - table = create_lookup_table_keys(1, 1, 0) - self.assertEqual(4, len(table)) - table = create_lookup_table_keys(2, 2, 2) - self.assertEqual(2**2 * 2**2 * 2**2, len(table)) - - def test_create_table_from_pattern(self): - with self.assertRaises(ValueError): - pattern = ''.join([C] * 8) - create_lookup_table_from_pattern(2, 2, 2, pattern) - # Doesn't Raise - pattern = ''.join([C] * 8) - create_lookup_table_from_pattern(1, 1, 1, pattern) - - class TestLookerUp(TestPlayer): name = "LookerUp" @@ -69,6 +47,20 @@ def test_init(self): with self.assertRaises(ValueError): player = self.player(table) + def test_pattern_init(self): + # Test empty table + pattern = "CCCC" + parameters = (1, 1, 0) + # Test default table + player = self.player(lookup_pattern=pattern, parameters=parameters) + expected_lookup_table = { + ('', 'C', 'D'): C, + ('', 'D', 'D'): C, + ('', 'C', 'C'): C, + ('', 'D', 'C'): C, + } + self.assertEqual(player.lookup_table, expected_lookup_table) + def test_strategy(self): self.second_play_test(C, D, C, D) # TFT self.responses_test([C], [C] * 4, [C, C, C, C]) @@ -139,6 +131,26 @@ def test_starting_move(self): self.responses_test([D], [C, C, D], [D, D, C]) +class TestEvolvedLookerUp1_1_1(TestPlayer): + + name = "EvolvedLookerUp1_1_1" + player = axelrod.EvolvedLookerUp1_1_1 + + expected_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 test_strategy(self): + """Starts by cooperating.""" + self.first_play_test(C) + + class TestEvolvedLookerUp2_2_2(TestPlayer): name = "EvolvedLookerUp2_2_2" @@ -168,18 +180,6 @@ def test_strategy(self): self.first_play_test(C) -class TestGeneratedPlayers(unittest.TestCase): - def test_created_players(self): - """Test the importing of the autogenerated lookerups. If there is an - issue then AttributeError will be raised.""" - patterns = axelrod.load_lookerup_tables() - for k, v in patterns.items(): - name, plays, op_plays, op_start_plays = k - class_name = "EvolvedLookerUp{}{}_{}_{}".format( - name, plays, op_plays, op_start_plays) - getattr(axelrod, class_name) - - # Some heads up tests for EvolvedLookerUp class EvolvedLookerUpvsDefector(TestMatch): def test_vs(self): diff --git a/type_tests.sh b/type_tests.sh index ab8e89215..a85bda04a 100755 --- a/type_tests.sh +++ b/type_tests.sh @@ -8,17 +8,18 @@ mypy --ignore-missing-imports --follow-imports skip axelrod/moran.py mypy --ignore-missing-imports --follow-imports skip axelrod/plot.py mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/adaptive.py mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/alternator.py +mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/ann.py mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/appeaser.py mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/averagecopier.py -mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/calculator.py -mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/forgiver.py -mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/better_and_better.py -mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/darwin.py -mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/cycler.py -mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/cooperator.py mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/axelrod_second.py mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/backstabber.py +mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/better_and_better.py +mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/calculator.py +mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/cooperator.py +mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/cycler.py +mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/darwin.py mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/defector.py +mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/forgiver.py mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/gradualkiller.py mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/grudger.py mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/grumpy.py