Skip to content

Commit

Permalink
Revert "Run black on all files"
Browse files Browse the repository at this point in the history
This reverts commit 6e31d2a.
  • Loading branch information
drvinceknight committed Apr 8, 2020
1 parent 6e31d2a commit 562be02
Show file tree
Hide file tree
Showing 60 changed files with 676 additions and 762 deletions.
33 changes: 23 additions & 10 deletions axelrod/compute_finite_state_machine_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ def __hash__(self):

def __eq__(self, other_memit) -> bool:
"""In action and out actions are the same."""
return self.in_act == other_memit.in_act and self.out_act == other_memit.out_act
return (
self.in_act == other_memit.in_act
and self.out_act == other_memit.out_act
)

def __lt__(self, other_memit) -> bool:
return repr(self) < repr(other_memit)
Expand Down Expand Up @@ -107,10 +110,9 @@ def get_accessible_transitions(
accessible_transitions = dict()
for trans in transition_iterator(transitions):
if trans.state in accessible_states:
accessible_transitions[(trans.state, trans.last_opponent_action)] = (
trans.next_state,
trans.next_action,
)
accessible_transitions[
(trans.state, trans.last_opponent_action)
] = (trans.next_state, trans.next_action)

return accessible_transitions

Expand Down Expand Up @@ -177,7 +179,9 @@ def get_memory_from_transitions(
transitions = get_accessible_transitions(transitions, initial_state)

# Get the incoming actions for each state.
incoming_action_by_state = defaultdict(set) # type: DefaultDict[int, Set[Action]]
incoming_action_by_state = defaultdict(
set
) # type: DefaultDict[int, Set[Action]]
for trans in transition_iterator(transitions):
incoming_action_by_state[trans.next_state].add(trans.next_action)

Expand All @@ -189,17 +193,23 @@ def get_memory_from_transitions(
# That is to say that the opponent could do anything
for out_action in all_actions:
# More recent in action history
starting_node = Memit(trans.next_action, trans.next_state, out_action)
starting_node = Memit(
trans.next_action, trans.next_state, out_action
)
# All incoming paths to current state
for in_action in incoming_action_by_state[trans.state]:
# Less recent in action history
ending_node = Memit(in_action, trans.state, trans.last_opponent_action)
ending_node = Memit(
in_action, trans.state, trans.last_opponent_action
)
memit_edges[starting_node].add(ending_node)

all_memits = list(memit_edges.keys())

pair_nodes = set()
pair_edges = defaultdict(set) # type: DefaultDict[MemitPair, Set[MemitPair]]
pair_edges = defaultdict(
set
) # type: DefaultDict[MemitPair, Set[MemitPair]]
# Loop through all pairs of memits.
for x, y in [(x, y) for x in all_memits for y in all_memits]:
if x == y and x.state == y.state:
Expand All @@ -226,7 +236,9 @@ def get_memory_from_transitions(
next_action_by_memit = dict()
for trans in transition_iterator(transitions):
for in_action in incoming_action_by_state[trans.state]:
memit_key = Memit(in_action, trans.state, trans.last_opponent_action)
memit_key = Memit(
in_action, trans.state, trans.last_opponent_action
)
next_action_by_memit[memit_key] = trans.next_action

# Calculate the longest path.
Expand All @@ -251,3 +263,4 @@ def get_memory_from_transitions(
if len(next_action_set) == 1:
return 0
return 1

3 changes: 1 addition & 2 deletions axelrod/evolvable_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

class InsufficientParametersError(Exception):
"""Error indicating that insufficient parameters were specified to initialize an Evolvable Player."""

def __init__(self, *args):
super().__init__(*args)

Expand Down Expand Up @@ -39,7 +38,7 @@ def create_new(self, **kwargs):
def serialize_parameters(self):
"""Serialize parameters."""
pickled = dumps(self.init_kwargs) # bytes
s = base64.b64encode(pickled).decode("utf8") # string
s = base64.b64encode(pickled).decode('utf8') # string
return s

@classmethod
Expand Down
3 changes: 2 additions & 1 deletion axelrod/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ def attached_complete_graphs(length, loops=True, directed=False):
for cluster in range(2):
for i in range(length):
for j in range(i + 1, length):
edges.append(("{}:{}".format(cluster, i), "{}:{}".format(cluster, j)))
edges.append(("{}:{}".format(cluster, i),
"{}:{}".format(cluster, j)))
# Attach at one node
edges.append(("0:0", "1:0"))
graph = Graph(directed=directed, edges=edges)
Expand Down
6 changes: 2 additions & 4 deletions axelrod/moran.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def __init__(
reproduction_graph: Graph = None,
fitness_transformation: Callable = None,
mutation_method="transition",
stop_on_fixation=True,
stop_on_fixation=True
) -> None:
"""
An agent based Moran process class. In each round, each player plays a
Expand Down Expand Up @@ -193,9 +193,7 @@ def mutate(self, index: int) -> Player:

if self.mutation_method == "atomic":
if not issubclass(self.players[index].__class__, EvolvablePlayer):
raise TypeError(
"Player is not evolvable. Use a subclass of EvolvablePlayer."
)
raise TypeError("Player is not evolvable. Use a subclass of EvolvablePlayer.")
return self.players[index].mutate()

# Assuming mutation_method == "transition"
Expand Down
20 changes: 9 additions & 11 deletions axelrod/strategies/adaptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,12 @@ class AbstractAdaptor(Player):
"manipulates_state": False,
}

def __init__(
self, delta: Dict[Tuple[Action, Action], float], perr: float = 0.01
) -> None:
def __init__(self, delta: Dict[Tuple[Action, Action], float],
perr: float = 0.01) -> None:
super().__init__()
self.perr = perr
self.delta = delta
self.s = 0.0
self.s = 0.

def strategy(self, opponent: Player) -> Action:
if self.history:
Expand All @@ -55,8 +54,7 @@ def strategy(self, opponent: Player) -> Action:

# Compute probability of Cooperation
p = self.perr + (1.0 - 2 * self.perr) * (
heaviside(self.s + 1, 1) - heaviside(self.s - 1, 1)
)
heaviside(self.s + 1, 1) - heaviside(self.s - 1, 1))
# Draw action
action = random_choice(p)
return action
Expand All @@ -76,10 +74,10 @@ class AdaptorBrief(AbstractAdaptor):

def __init__(self) -> None:
delta = {
(C, C): 0.0, # R
(C, C): 0., # R
(C, D): -1.001505, # S
(D, C): 0.992107, # T
(D, D): -0.638734, # P
(D, C): 0.992107, # T
(D, D): -0.638734 # P
}
super().__init__(delta=delta)

Expand All @@ -98,9 +96,9 @@ class AdaptorLong(AbstractAdaptor):

def __init__(self) -> None:
delta = {
(C, C): 0.0, # R
(C, C): 0., # R
(C, D): 1.888159, # S
(D, C): 1.858883, # T
(D, D): -0.995703, # P
(D, D): -0.995703 # P
}
super().__init__(delta=delta)
73 changes: 28 additions & 45 deletions axelrod/strategies/ann.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@
import numpy.random as random
from axelrod.action import Action
from axelrod.load_data_ import load_weights
from axelrod.evolvable_player import (
EvolvablePlayer,
InsufficientParametersError,
crossover_lists,
)
from axelrod.evolvable_player import EvolvablePlayer, InsufficientParametersError, crossover_lists
from axelrod.player import Player


Expand Down Expand Up @@ -195,7 +191,8 @@ class ANN(Player):
}

def __init__(
self, num_features: int, num_hidden: int, weights: List[float] = None
self, num_features: int, num_hidden: int,
weights: List[float] = None
) -> None:
super().__init__()
self.num_features = num_features
Expand Down Expand Up @@ -225,57 +222,43 @@ def strategy(self, opponent: Player) -> Action:

class EvolvableANN(ANN, EvolvablePlayer):
"""Evolvable version of ANN."""

name = "EvolvableANN"

def __init__(
self,
num_features: int,
num_hidden: int,
self, num_features: int, num_hidden: int,
weights: List[float] = None,
mutation_probability: float = None,
mutation_distance: int = 5,
) -> None:
(
num_features,
num_hidden,
weights,
mutation_probability,
) = self._normalize_parameters(
num_features, num_hidden, weights, mutation_probability
)
ANN.__init__(
self, num_features=num_features, num_hidden=num_hidden, weights=weights
)
num_features, num_hidden, weights, mutation_probability = self._normalize_parameters(
num_features, num_hidden, weights, mutation_probability)
ANN.__init__(self,
num_features=num_features,
num_hidden=num_hidden,
weights=weights)
EvolvablePlayer.__init__(self)
self.mutation_probability = mutation_probability
self.mutation_distance = mutation_distance
self.overwrite_init_kwargs(
num_features=num_features,
num_hidden=num_hidden,
weights=weights,
mutation_probability=mutation_probability,
)
mutation_probability=mutation_probability)

@classmethod
def _normalize_parameters(
cls, num_features=None, num_hidden=None, weights=None, mutation_probability=None
):
def _normalize_parameters(cls, num_features=None, num_hidden=None, weights=None, mutation_probability=None):
if not (num_features and num_hidden):
raise InsufficientParametersError(
"Insufficient Parameters to instantiate EvolvableANN"
)
raise InsufficientParametersError("Insufficient Parameters to instantiate EvolvableANN")
size = num_weights(num_features, num_hidden)
if not weights:
weights = [random.uniform(-1, 1) for _ in range(size)]
if mutation_probability is None:
mutation_probability = 10.0 / size
mutation_probability = 10. / size
return num_features, num_hidden, weights, mutation_probability

@staticmethod
def mutate_weights(
weights, num_features, num_hidden, mutation_probability, mutation_distance
):
def mutate_weights(weights, num_features, num_hidden, mutation_probability,
mutation_distance):
size = num_weights(num_features, num_hidden)
randoms = random.random(size)
for i, r in enumerate(randoms):
Expand All @@ -286,12 +269,8 @@ def mutate_weights(

def mutate(self):
weights = self.mutate_weights(
self.weights,
self.num_features,
self.num_hidden,
self.mutation_probability,
self.mutation_distance,
)
self.weights, self.num_features, self.num_hidden,
self.mutation_probability, self.mutation_distance)
return self.create_new(weights=weights)

def crossover(self, other):
Expand Down Expand Up @@ -319,8 +298,9 @@ class EvolvedANN(ANN):
def __init__(self) -> None:
num_features, num_hidden, weights = nn_weights["Evolved ANN"]
super().__init__(
num_features=num_features, num_hidden=num_hidden, weights=weights
)
num_features=num_features,
num_hidden=num_hidden,
weights=weights)


class EvolvedANN5(ANN):
Expand All @@ -341,8 +321,9 @@ class EvolvedANN5(ANN):
def __init__(self) -> None:
num_features, num_hidden, weights = nn_weights["Evolved ANN 5"]
super().__init__(
num_features=num_features, num_hidden=num_hidden, weights=weights
)
num_features=num_features,
num_hidden=num_hidden,
weights=weights)


class EvolvedANNNoise05(ANN):
Expand All @@ -363,5 +344,7 @@ class EvolvedANNNoise05(ANN):
def __init__(self) -> None:
num_features, num_hidden, weights = nn_weights["Evolved ANN 5 Noise 05"]
super().__init__(
num_features=num_features, num_hidden=num_hidden, weights=weights
)
num_features=num_features,
num_hidden=num_hidden,
weights=weights)

Loading

0 comments on commit 562be02

Please sign in to comment.