@@ -28,7 +28,7 @@ def __init__(
28
28
fitness_transformation : Callable = None ,
29
29
mutation_method = "transition" ,
30
30
stop_on_fixation = True ,
31
- seed = None
31
+ seed = None ,
32
32
) -> None :
33
33
"""
34
34
An agent based Moran process class. In each round, each player plays a
@@ -93,7 +93,9 @@ def __init__(
93
93
if m in ["atomic" , "transition" ]:
94
94
self .mutation_method = m
95
95
else :
96
- raise ValueError ("Invalid mutation method {}" .format (mutation_method ))
96
+ raise ValueError (
97
+ "Invalid mutation method {}" .format (mutation_method )
98
+ )
97
99
assert (mutation_rate >= 0 ) and (mutation_rate <= 1 )
98
100
assert (noise >= 0 ) and (noise <= 1 )
99
101
mode = mode .lower ()
@@ -127,7 +129,9 @@ def __init__(
127
129
d [str (p )] = p
128
130
mutation_targets = dict ()
129
131
for key in sorted (keys ):
130
- mutation_targets [key ] = [v for (k , v ) in sorted (d .items ()) if k != key ]
132
+ mutation_targets [key ] = [
133
+ v for (k , v ) in sorted (d .items ()) if k != key
134
+ ]
131
135
self .mutation_targets = mutation_targets
132
136
133
137
if interaction_graph is None :
@@ -146,14 +150,18 @@ def __init__(
146
150
self .fitness_transformation = fitness_transformation
147
151
# Map players to graph vertices
148
152
self .locations = sorted (interaction_graph .vertices )
149
- self .index = dict (zip (sorted (interaction_graph .vertices ), range (len (players ))))
153
+ self .index = dict (
154
+ zip (sorted (interaction_graph .vertices ), range (len (players )))
155
+ )
150
156
self .fixated = self .fixation_check ()
151
157
152
158
def set_players (self ) -> None :
153
159
"""Copy the initial players into the first population, setting seeds as needed."""
154
160
self .players = []
155
161
for player in self .initial_players :
156
- if (self .mutation_method == "atomic" ) and issubclass (player .__class__ , EvolvablePlayer ):
162
+ if (self .mutation_method == "atomic" ) and issubclass (
163
+ player .__class__ , EvolvablePlayer
164
+ ):
157
165
# For reproducibility, we generate random seeds for evolvable players.
158
166
seed = next (self ._bulk_random )
159
167
new_player = player .create_new (seed = seed )
@@ -163,8 +171,9 @@ def set_players(self) -> None:
163
171
self .players .append (player )
164
172
self .populations = [self .population_distribution ()]
165
173
166
- def fitness_proportionate_selection (self ,
167
- scores : List , fitness_transformation : Callable = None ) -> int :
174
+ def fitness_proportionate_selection (
175
+ self , scores : List , fitness_transformation : Callable = None
176
+ ) -> int :
168
177
"""Randomly selects an individual proportionally to score.
169
178
170
179
Parameters
@@ -200,7 +209,9 @@ def mutate(self, index: int) -> Player:
200
209
201
210
if self .mutation_method == "atomic" :
202
211
if not issubclass (self .players [index ].__class__ , EvolvablePlayer ):
203
- raise TypeError ("Player is not evolvable. Use a subclass of EvolvablePlayer." )
212
+ raise TypeError (
213
+ "Player is not evolvable. Use a subclass of EvolvablePlayer."
214
+ )
204
215
return self .players [index ].mutate ()
205
216
206
217
# Assuming mutation_method == "transition"
@@ -237,7 +248,9 @@ def death(self, index: int = None) -> int:
237
248
# Select locally
238
249
# index is not None in this case
239
250
vertex = self ._random .choice (
240
- sorted (self .reproduction_graph .out_vertices (self .locations [index ]))
251
+ sorted (
252
+ self .reproduction_graph .out_vertices (self .locations [index ])
253
+ )
241
254
)
242
255
i = self .index [vertex ]
243
256
return i
@@ -370,7 +383,7 @@ def score_all(self) -> List:
370
383
noise = self .noise ,
371
384
game = self .game ,
372
385
deterministic_cache = self .deterministic_cache ,
373
- seed = next (self ._bulk_random )
386
+ seed = next (self ._bulk_random ),
374
387
)
375
388
match .play ()
376
389
match_scores = match .final_score_per_turn ()
@@ -484,8 +497,11 @@ class ApproximateMoranProcess(MoranProcess):
484
497
"""
485
498
486
499
def __init__ (
487
- self , players : List [Player ], cached_outcomes : dict , mutation_rate : float = 0 ,
488
- seed : Optional [int ] = None
500
+ self ,
501
+ players : List [Player ],
502
+ cached_outcomes : dict ,
503
+ mutation_rate : float = 0 ,
504
+ seed : Optional [int ] = None ,
489
505
) -> None :
490
506
"""
491
507
Parameters
@@ -503,7 +519,7 @@ def __init__(
503
519
noise = 0 ,
504
520
deterministic_cache = None ,
505
521
mutation_rate = mutation_rate ,
506
- seed = seed
522
+ seed = seed ,
507
523
)
508
524
self .cached_outcomes = cached_outcomes
509
525
@@ -529,7 +545,9 @@ def score_all(self) -> List:
529
545
scores = [0 ] * N
530
546
for i in range (N ):
531
547
for j in range (i + 1 , N ):
532
- player_names = tuple ([str (self .players [i ]), str (self .players [j ])])
548
+ player_names = tuple (
549
+ [str (self .players [i ]), str (self .players [j ])]
550
+ )
533
551
cached_score = self ._get_scores_from_cache (player_names )
534
552
scores [i ] += cached_score [0 ]
535
553
scores [j ] += cached_score [1 ]
0 commit comments