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

Remove dynamic classes and tidy imports #852

Merged
merged 14 commits into from
Feb 10, 2017
Merged
2 changes: 1 addition & 1 deletion axelrod/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions axelrod/_strategy_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
19 changes: 0 additions & 19 deletions axelrod/data/lookup_tables.csv

This file was deleted.

2 changes: 1 addition & 1 deletion axelrod/interaction_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 0 additions & 10 deletions axelrod/load_data_.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion axelrod/match.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
4 changes: 2 additions & 2 deletions axelrod/mock_player.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion axelrod/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
3 changes: 1 addition & 2 deletions axelrod/random_.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
2 changes: 1 addition & 1 deletion axelrod/result_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 1 addition & 6 deletions axelrod/strategies/_strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
3 changes: 2 additions & 1 deletion axelrod/strategies/adaptive.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions axelrod/strategies/alternator.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
39 changes: 3 additions & 36 deletions axelrod/strategies/ann.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion axelrod/strategies/apavlov.py
Original file line number Diff line number Diff line change
@@ -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

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

Expand Down
11 changes: 5 additions & 6 deletions axelrod/strategies/averagecopier.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -21,8 +22,7 @@ class AverageCopier(Player):
'manipulates_state': False
}

@staticmethod
def strategy(opponent: Player) -> Action:
def strategy(self, opponent: Player) -> Action:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because of the random?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was being pointed out as an error by mypy something to do with the method not being in line with the parent class.

if len(opponent.history) == 0:
# Randomly picks a strategy (not affected by history).
return random_choice(0.5)
Expand All @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion axelrod/strategies/axelrod_first.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions axelrod/strategies/axelrod_second.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion axelrod/strategies/backstabber.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
4 changes: 3 additions & 1 deletion axelrod/strategies/better_and_better.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 2 additions & 1 deletion axelrod/strategies/calculator.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions axelrod/strategies/cooperator.py
Original file line number Diff line number Diff line change
@@ -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

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

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

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

Expand Down
3 changes: 2 additions & 1 deletion axelrod/strategies/doubler.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
3 changes: 2 additions & 1 deletion axelrod/strategies/finite_state_machines.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
9 changes: 4 additions & 5 deletions axelrod/strategies/forgiver.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
"""
Expand All @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion axelrod/strategies/gambler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
4 changes: 3 additions & 1 deletion axelrod/strategies/geller.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
3 changes: 2 additions & 1 deletion axelrod/strategies/gobymajority.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from axelrod import Actions, Player
from axelrod.actions import Actions
from axelrod.player import Player

import copy

Expand Down
3 changes: 2 additions & 1 deletion axelrod/strategies/gradualkiller.py
Original file line number Diff line number Diff line change
@@ -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

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

Expand Down
Loading