Skip to content

Commit 6e31d2a

Browse files
committed
Run black on all files
1 parent 1becc9a commit 6e31d2a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+762
-676
lines changed

axelrod/compute_finite_state_machine_memory.py

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,7 @@ def __hash__(self):
4747

4848
def __eq__(self, other_memit) -> bool:
4949
"""In action and out actions are the same."""
50-
return (
51-
self.in_act == other_memit.in_act
52-
and self.out_act == other_memit.out_act
53-
)
50+
return self.in_act == other_memit.in_act and self.out_act == other_memit.out_act
5451

5552
def __lt__(self, other_memit) -> bool:
5653
return repr(self) < repr(other_memit)
@@ -110,9 +107,10 @@ def get_accessible_transitions(
110107
accessible_transitions = dict()
111108
for trans in transition_iterator(transitions):
112109
if trans.state in accessible_states:
113-
accessible_transitions[
114-
(trans.state, trans.last_opponent_action)
115-
] = (trans.next_state, trans.next_action)
110+
accessible_transitions[(trans.state, trans.last_opponent_action)] = (
111+
trans.next_state,
112+
trans.next_action,
113+
)
116114

117115
return accessible_transitions
118116

@@ -179,9 +177,7 @@ def get_memory_from_transitions(
179177
transitions = get_accessible_transitions(transitions, initial_state)
180178

181179
# Get the incoming actions for each state.
182-
incoming_action_by_state = defaultdict(
183-
set
184-
) # type: DefaultDict[int, Set[Action]]
180+
incoming_action_by_state = defaultdict(set) # type: DefaultDict[int, Set[Action]]
185181
for trans in transition_iterator(transitions):
186182
incoming_action_by_state[trans.next_state].add(trans.next_action)
187183

@@ -193,23 +189,17 @@ def get_memory_from_transitions(
193189
# That is to say that the opponent could do anything
194190
for out_action in all_actions:
195191
# More recent in action history
196-
starting_node = Memit(
197-
trans.next_action, trans.next_state, out_action
198-
)
192+
starting_node = Memit(trans.next_action, trans.next_state, out_action)
199193
# All incoming paths to current state
200194
for in_action in incoming_action_by_state[trans.state]:
201195
# Less recent in action history
202-
ending_node = Memit(
203-
in_action, trans.state, trans.last_opponent_action
204-
)
196+
ending_node = Memit(in_action, trans.state, trans.last_opponent_action)
205197
memit_edges[starting_node].add(ending_node)
206198

207199
all_memits = list(memit_edges.keys())
208200

209201
pair_nodes = set()
210-
pair_edges = defaultdict(
211-
set
212-
) # type: DefaultDict[MemitPair, Set[MemitPair]]
202+
pair_edges = defaultdict(set) # type: DefaultDict[MemitPair, Set[MemitPair]]
213203
# Loop through all pairs of memits.
214204
for x, y in [(x, y) for x in all_memits for y in all_memits]:
215205
if x == y and x.state == y.state:
@@ -236,9 +226,7 @@ def get_memory_from_transitions(
236226
next_action_by_memit = dict()
237227
for trans in transition_iterator(transitions):
238228
for in_action in incoming_action_by_state[trans.state]:
239-
memit_key = Memit(
240-
in_action, trans.state, trans.last_opponent_action
241-
)
229+
memit_key = Memit(in_action, trans.state, trans.last_opponent_action)
242230
next_action_by_memit[memit_key] = trans.next_action
243231

244232
# Calculate the longest path.
@@ -263,4 +251,3 @@ def get_memory_from_transitions(
263251
if len(next_action_set) == 1:
264252
return 0
265253
return 1
266-

axelrod/evolvable_player.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
class InsufficientParametersError(Exception):
99
"""Error indicating that insufficient parameters were specified to initialize an Evolvable Player."""
10+
1011
def __init__(self, *args):
1112
super().__init__(*args)
1213

@@ -38,7 +39,7 @@ def create_new(self, **kwargs):
3839
def serialize_parameters(self):
3940
"""Serialize parameters."""
4041
pickled = dumps(self.init_kwargs) # bytes
41-
s = base64.b64encode(pickled).decode('utf8') # string
42+
s = base64.b64encode(pickled).decode("utf8") # string
4243
return s
4344

4445
@classmethod

axelrod/graph.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,7 @@ def attached_complete_graphs(length, loops=True, directed=False):
155155
for cluster in range(2):
156156
for i in range(length):
157157
for j in range(i + 1, length):
158-
edges.append(("{}:{}".format(cluster, i),
159-
"{}:{}".format(cluster, j)))
158+
edges.append(("{}:{}".format(cluster, i), "{}:{}".format(cluster, j)))
160159
# Attach at one node
161160
edges.append(("0:0", "1:0"))
162161
graph = Graph(directed=directed, edges=edges)

axelrod/moran.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def __init__(
5757
reproduction_graph: Graph = None,
5858
fitness_transformation: Callable = None,
5959
mutation_method="transition",
60-
stop_on_fixation=True
60+
stop_on_fixation=True,
6161
) -> None:
6262
"""
6363
An agent based Moran process class. In each round, each player plays a
@@ -193,7 +193,9 @@ def mutate(self, index: int) -> Player:
193193

194194
if self.mutation_method == "atomic":
195195
if not issubclass(self.players[index].__class__, EvolvablePlayer):
196-
raise TypeError("Player is not evolvable. Use a subclass of EvolvablePlayer.")
196+
raise TypeError(
197+
"Player is not evolvable. Use a subclass of EvolvablePlayer."
198+
)
197199
return self.players[index].mutate()
198200

199201
# Assuming mutation_method == "transition"

axelrod/strategies/adaptor.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,13 @@ class AbstractAdaptor(Player):
3939
"manipulates_state": False,
4040
}
4141

42-
def __init__(self, delta: Dict[Tuple[Action, Action], float],
43-
perr: float = 0.01) -> None:
42+
def __init__(
43+
self, delta: Dict[Tuple[Action, Action], float], perr: float = 0.01
44+
) -> None:
4445
super().__init__()
4546
self.perr = perr
4647
self.delta = delta
47-
self.s = 0.
48+
self.s = 0.0
4849

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

5556
# Compute probability of Cooperation
5657
p = self.perr + (1.0 - 2 * self.perr) * (
57-
heaviside(self.s + 1, 1) - heaviside(self.s - 1, 1))
58+
heaviside(self.s + 1, 1) - heaviside(self.s - 1, 1)
59+
)
5860
# Draw action
5961
action = random_choice(p)
6062
return action
@@ -74,10 +76,10 @@ class AdaptorBrief(AbstractAdaptor):
7476

7577
def __init__(self) -> None:
7678
delta = {
77-
(C, C): 0., # R
79+
(C, C): 0.0, # R
7880
(C, D): -1.001505, # S
79-
(D, C): 0.992107, # T
80-
(D, D): -0.638734 # P
81+
(D, C): 0.992107, # T
82+
(D, D): -0.638734, # P
8183
}
8284
super().__init__(delta=delta)
8385

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

9799
def __init__(self) -> None:
98100
delta = {
99-
(C, C): 0., # R
101+
(C, C): 0.0, # R
100102
(C, D): 1.888159, # S
101103
(D, C): 1.858883, # T
102-
(D, D): -0.995703 # P
104+
(D, D): -0.995703, # P
103105
}
104106
super().__init__(delta=delta)

axelrod/strategies/ann.py

Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
import numpy.random as random
44
from axelrod.action import Action
55
from axelrod.load_data_ import load_weights
6-
from axelrod.evolvable_player import EvolvablePlayer, InsufficientParametersError, crossover_lists
6+
from axelrod.evolvable_player import (
7+
EvolvablePlayer,
8+
InsufficientParametersError,
9+
crossover_lists,
10+
)
711
from axelrod.player import Player
812

913

@@ -191,8 +195,7 @@ class ANN(Player):
191195
}
192196

193197
def __init__(
194-
self, num_features: int, num_hidden: int,
195-
weights: List[float] = None
198+
self, num_features: int, num_hidden: int, weights: List[float] = None
196199
) -> None:
197200
super().__init__()
198201
self.num_features = num_features
@@ -222,43 +225,57 @@ def strategy(self, opponent: Player) -> Action:
222225

223226
class EvolvableANN(ANN, EvolvablePlayer):
224227
"""Evolvable version of ANN."""
228+
225229
name = "EvolvableANN"
226230

227231
def __init__(
228-
self, num_features: int, num_hidden: int,
232+
self,
233+
num_features: int,
234+
num_hidden: int,
229235
weights: List[float] = None,
230236
mutation_probability: float = None,
231237
mutation_distance: int = 5,
232238
) -> None:
233-
num_features, num_hidden, weights, mutation_probability = self._normalize_parameters(
234-
num_features, num_hidden, weights, mutation_probability)
235-
ANN.__init__(self,
236-
num_features=num_features,
237-
num_hidden=num_hidden,
238-
weights=weights)
239+
(
240+
num_features,
241+
num_hidden,
242+
weights,
243+
mutation_probability,
244+
) = self._normalize_parameters(
245+
num_features, num_hidden, weights, mutation_probability
246+
)
247+
ANN.__init__(
248+
self, num_features=num_features, num_hidden=num_hidden, weights=weights
249+
)
239250
EvolvablePlayer.__init__(self)
240251
self.mutation_probability = mutation_probability
241252
self.mutation_distance = mutation_distance
242253
self.overwrite_init_kwargs(
243254
num_features=num_features,
244255
num_hidden=num_hidden,
245256
weights=weights,
246-
mutation_probability=mutation_probability)
257+
mutation_probability=mutation_probability,
258+
)
247259

248260
@classmethod
249-
def _normalize_parameters(cls, num_features=None, num_hidden=None, weights=None, mutation_probability=None):
261+
def _normalize_parameters(
262+
cls, num_features=None, num_hidden=None, weights=None, mutation_probability=None
263+
):
250264
if not (num_features and num_hidden):
251-
raise InsufficientParametersError("Insufficient Parameters to instantiate EvolvableANN")
265+
raise InsufficientParametersError(
266+
"Insufficient Parameters to instantiate EvolvableANN"
267+
)
252268
size = num_weights(num_features, num_hidden)
253269
if not weights:
254270
weights = [random.uniform(-1, 1) for _ in range(size)]
255271
if mutation_probability is None:
256-
mutation_probability = 10. / size
272+
mutation_probability = 10.0 / size
257273
return num_features, num_hidden, weights, mutation_probability
258274

259275
@staticmethod
260-
def mutate_weights(weights, num_features, num_hidden, mutation_probability,
261-
mutation_distance):
276+
def mutate_weights(
277+
weights, num_features, num_hidden, mutation_probability, mutation_distance
278+
):
262279
size = num_weights(num_features, num_hidden)
263280
randoms = random.random(size)
264281
for i, r in enumerate(randoms):
@@ -269,8 +286,12 @@ def mutate_weights(weights, num_features, num_hidden, mutation_probability,
269286

270287
def mutate(self):
271288
weights = self.mutate_weights(
272-
self.weights, self.num_features, self.num_hidden,
273-
self.mutation_probability, self.mutation_distance)
289+
self.weights,
290+
self.num_features,
291+
self.num_hidden,
292+
self.mutation_probability,
293+
self.mutation_distance,
294+
)
274295
return self.create_new(weights=weights)
275296

276297
def crossover(self, other):
@@ -298,9 +319,8 @@ class EvolvedANN(ANN):
298319
def __init__(self) -> None:
299320
num_features, num_hidden, weights = nn_weights["Evolved ANN"]
300321
super().__init__(
301-
num_features=num_features,
302-
num_hidden=num_hidden,
303-
weights=weights)
322+
num_features=num_features, num_hidden=num_hidden, weights=weights
323+
)
304324

305325

306326
class EvolvedANN5(ANN):
@@ -321,9 +341,8 @@ class EvolvedANN5(ANN):
321341
def __init__(self) -> None:
322342
num_features, num_hidden, weights = nn_weights["Evolved ANN 5"]
323343
super().__init__(
324-
num_features=num_features,
325-
num_hidden=num_hidden,
326-
weights=weights)
344+
num_features=num_features, num_hidden=num_hidden, weights=weights
345+
)
327346

328347

329348
class EvolvedANNNoise05(ANN):
@@ -344,7 +363,5 @@ class EvolvedANNNoise05(ANN):
344363
def __init__(self) -> None:
345364
num_features, num_hidden, weights = nn_weights["Evolved ANN 5 Noise 05"]
346365
super().__init__(
347-
num_features=num_features,
348-
num_hidden=num_hidden,
349-
weights=weights)
350-
366+
num_features=num_features, num_hidden=num_hidden, weights=weights
367+
)

0 commit comments

Comments
 (0)