Skip to content

Commit

Permalink
Merge pull request #817 from Axelrod-Python/init
Browse files Browse the repository at this point in the history
Refactor Player.__new__ to automatically apply init_args
  • Loading branch information
drvinceknight committed Jan 16, 2017
2 parents 0fa8fe3 + 3c4658f commit e2f2961
Show file tree
Hide file tree
Showing 30 changed files with 50 additions and 127 deletions.
15 changes: 11 additions & 4 deletions axelrod/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def __init__(self, myarg1, ...)
def wrapper(self, *args, **kwargs):
r = func(self, *args, **kwargs)
self.init_args = args
self.init_kwargs = kwargs
return r
return wrapper

Expand All @@ -99,6 +100,13 @@ class Player(object):
'manipulates_state': None
}

def __new__(cls, *args, **kwargs):
"""Caches arguments for Player cloning."""
obj = super().__new__(cls)
obj.init_args = args
obj.init_kwargs = kwargs
return obj

def __init__(self):
"""Initiates an empty history and 0 score for a player."""
self.history = []
Expand All @@ -111,7 +119,6 @@ def __init__(self):
self.cooperations = 0
self.defections = 0
self.state_distribution = defaultdict(int)
self.init_args = ()
self.set_match_attributes()

def receive_match_attributes(self):
Expand Down Expand Up @@ -162,12 +169,12 @@ def clone(self):
"""Clones the player without history, reapplying configuration
parameters as necessary."""

# You may be tempted to reimplement using the `copy` module
# You may be tempted to re-implement using the `copy` module
# Note that this would require a deepcopy in some cases and there may
# be significant changes required throughout the library.
# Consider overriding in special cases only if necessary
# Override in special cases only if absolutely necessary
cls = self.__class__
new_player = cls(*self.init_args)
new_player = cls(*self.init_args, **self.init_kwargs)
new_player.match_attributes = copy.copy(self.match_attributes)
return new_player

Expand Down
3 changes: 1 addition & 2 deletions axelrod/strategies/adaptive.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from axelrod import Actions, Player, init_args
from axelrod import Actions, Player

C, D = Actions.C, Actions.D

Expand All @@ -24,7 +24,6 @@ class Adaptive(Player):
'manipulates_state': False
}

@init_args
def __init__(self, initial_plays=None):
super().__init__()
if not initial_plays:
Expand Down
6 changes: 1 addition & 5 deletions axelrod/strategies/ann.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Original Source: https://gist.github.com/mojones/550b32c46a8169bb3cd89d917b73111a#file-ann-strategy-test-L60
# Original Author: Martin Jones, @mojones

from axelrod import Actions, Player, init_args, load_weights
from axelrod import Actions, Player, load_weights

C, D = Actions.C, Actions.D
nn_weights = load_weights()
Expand Down Expand Up @@ -62,7 +62,6 @@ class ANN(Player):
'long_run_time': False
}

@init_args
def __init__(self, weights, num_features, num_hidden):
super().__init__()
(i2h, h2o, bias) = split_weights(weights, num_features, num_hidden)
Expand Down Expand Up @@ -189,7 +188,6 @@ class EvolvedANN(ANN):

name = "Evolved ANN"

@init_args
def __init__(self):
num_features, num_hidden, weights = nn_weights["Evolved ANN"]
super().__init__(weights, num_features, num_hidden)
Expand All @@ -207,7 +205,6 @@ class EvolvedANN5(ANN):

name = "Evolved ANN 5"

@init_args
def __init__(self):
num_features, num_hidden, weights = nn_weights["Evolved ANN 5"]
super().__init__(weights, num_features, num_hidden)
Expand All @@ -225,7 +222,6 @@ class EvolvedANNNoise05(ANN):

name = "Evolved ANN 5 Noise 05"

@init_args
def __init__(self):
num_features, num_hidden, weights = nn_weights["Evolved ANN 5 Noise 05"]
super().__init__(weights, num_features, num_hidden)
11 changes: 3 additions & 8 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 import Actions, Player, init_args, flip_action, random_choice
from axelrod import Actions, Player, flip_action, random_choice

from.memoryone import MemoryOnePlayer

Expand Down Expand Up @@ -34,7 +34,6 @@ class Davis(Player):
'manipulates_state': False
}

@init_args
def __init__(self, rounds_to_cooperate=10):
"""
Parameters
Expand Down Expand Up @@ -76,7 +75,6 @@ class RevisedDowning(Player):
'manipulates_state': False
}

@init_args
def __init__(self, revised=True):
super().__init__()
self.revised = revised
Expand Down Expand Up @@ -159,7 +157,6 @@ class Feld(Player):
'manipulates_state': False
}

@init_args
def __init__(self, start_coop_prob=1.0, end_coop_prob=0.5,
rounds_of_decay=200):
"""
Expand Down Expand Up @@ -248,7 +245,6 @@ class Joss(MemoryOnePlayer):

name = "Joss"

@init_args
def __init__(self, p=0.9):
"""
Parameters
Expand All @@ -269,7 +265,7 @@ class Nydegger(Player):
"""
Submitted to Axelrod's first tournament by Rudy Nydegger.
The program begins with tit for tat for the first three moves, except
The program begins with tit for tat for the first three moves, except
that if it was the only one to cooperate on the first move and the only one to defect on the second move, it defects on the third move. After the third move, its choice is determined from the 3 preceding outcomes in the following manner.
Let A be the sum formed by counting the other's defection as 2 points and one's own as 1 point, and giving weights of 16, 4, and 1 to the preceding three moves in chronological order. The choice can be described as defecting only when A equals 1, 6, 7, 17, 22, 23, 26, 29, 30, 31, 33, 38, 39, 45, 49, 54, 55, 58, or 61.
Expand Down Expand Up @@ -420,7 +416,6 @@ class Tullock(Player):
'manipulates_state': False
}

@init_args
def __init__(self, rounds_to_cooperate=11):
"""
Parameters
Expand Down Expand Up @@ -458,7 +453,7 @@ class UnnamedStrategy(Player):
score than the other player. Unfortunately, the complex process of adjustment
frequently left the probability of cooperation in the 30% to 70% range, and
therefore the rule appeared random to many other players.
Names:
- Unnamed Strategy: [Axelrod1980]_
Expand Down
1 change: 1 addition & 0 deletions axelrod/strategies/defector.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

C, D = Actions.C, Actions.D


class Defector(Player):
"""A player who only ever defects."""

Expand Down
15 changes: 1 addition & 14 deletions axelrod/strategies/finite_state_machines.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from axelrod import Actions, Player, init_args
from axelrod import Actions, Player

C, D = Actions.C, Actions.D

Expand Down Expand Up @@ -47,7 +47,6 @@ class FSMPlayer(Player):
'manipulates_state': False
}

@init_args
def __init__(self, transitions=None, initial_state=None,
initial_action=None):
if not transitions:
Expand Down Expand Up @@ -91,7 +90,6 @@ class Fortress3(FSMPlayer):
'manipulates_state': False
}

@init_args
def __init__(self):
transitions = (
(1, D, 2, D),
Expand Down Expand Up @@ -121,7 +119,6 @@ class Fortress4(FSMPlayer):
'manipulates_state': False
}

@init_args
def __init__(self):
transitions = (
(1, C, 1, D),
Expand Down Expand Up @@ -151,7 +148,6 @@ class Predator(FSMPlayer):
'manipulates_state': False
}

@init_args
def __init__(self):
transitions = (
(0, C, 0, D),
Expand Down Expand Up @@ -195,7 +191,6 @@ class Pun1(FSMPlayer):
'manipulates_state': False
}

@init_args
def __init__(self):
transitions = (
(1, C, 2, C),
Expand All @@ -221,7 +216,6 @@ class Raider(FSMPlayer):
'manipulates_state': False
}

@init_args
def __init__(self):
transitions = (
(0, C, 2, D),
Expand Down Expand Up @@ -251,7 +245,6 @@ class Ripoff(FSMPlayer):
'manipulates_state': False
}

@init_args
def __init__(self):
transitions = (
(1, C, 2, C),
Expand Down Expand Up @@ -279,7 +272,6 @@ class SolutionB1(FSMPlayer):
'manipulates_state': False
}

@init_args
def __init__(self):
transitions = (
(1, C, 2, D),
Expand Down Expand Up @@ -307,7 +299,6 @@ class SolutionB5(FSMPlayer):
'manipulates_state': False
}

@init_args
def __init__(self):
transitions = (
(1, C, 2, C),
Expand Down Expand Up @@ -341,7 +332,6 @@ class Thumper(FSMPlayer):
'manipulates_state': False
}

@init_args
def __init__(self):
transitions = (
(1, C, 1, C),
Expand Down Expand Up @@ -373,7 +363,6 @@ class EvolvedFSM4(FSMPlayer):
'manipulates_state': False
}

@init_args
def __init__(self):
transitions = (
(0, C, 0, C),
Expand Down Expand Up @@ -410,7 +399,6 @@ class EvolvedFSM16(FSMPlayer):
'manipulates_state': False
}

@init_args
def __init__(self):
transitions = (
(0, C, 0, C),
Expand Down Expand Up @@ -471,7 +459,6 @@ class EvolvedFSM16Noise05(FSMPlayer):
'manipulates_state': False
}

@init_args
def __init__(self):
transitions = (
(0, C, 8, C),
Expand Down
1 change: 1 addition & 0 deletions axelrod/strategies/forgiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

C, D = Actions.C, Actions.D


class Forgiver(Player):
"""
A player starts by cooperating however will defect if at any point
Expand Down
2 changes: 1 addition & 1 deletion axelrod/strategies/gambler.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
https://gist.github.com/GDKO/60c3d0fd423598f3c4e4
"""

from axelrod import Actions, random_choice, load_pso_tables
from axelrod import Actions, load_pso_tables, random_choice
from .lookerup import LookerUp, create_lookup_table_from_pattern


Expand Down
3 changes: 1 addition & 2 deletions axelrod/strategies/geller.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
# -*- coding: utf-8 -*-

import inspect

from axelrod import Actions, Player, random_choice

C, D = Actions.C, Actions.D


class Geller(Player):
"""Observes what the player will do in the next round and adjust.
Expand Down
Loading

0 comments on commit e2f2961

Please sign in to comment.