11from collections import Counter
22import random
3- from typing import Sequence , List , Tuple
43
54import numpy as np
65
98from .random_ import randrange
109
1110
12- def fitness_proportionate_selection (scores : Sequence [ int ]) -> int :
11+ def fitness_proportionate_selection (scores ) :
1312 """Randomly selects an individual proportionally to score.
1413
1514 Parameters
@@ -31,8 +30,8 @@ def fitness_proportionate_selection(scores: Sequence[int]) -> int :
3130
3231
3332class MoranProcess (object ):
34- def __init__ (self , players , turns : int = 100 , noise : float = 0 , deterministic_cache = None ,
35- mutation_rate : float = 0. , mode : str = 'bd' , match_class = Match ):
33+ def __init__ (self , players , turns = 100 , noise = 0 , deterministic_cache = None ,
34+ mutation_rate = 0. , mode = 'bd' , match_class = Match ):
3635 """
3736 An agent based Moran process class. In each round, each player plays a
3837 Match with each other player. Players are assigned a fitness score by
@@ -108,7 +107,7 @@ def set_players(self):
108107 self .players .append (player )
109108 self .populations = [self .population_distribution ()]
110109
111- def mutate (self , index : int ) -> List [ str ] :
110+ def mutate (self , index ) :
112111 """Mutate the player at index."""
113112 # Choose another strategy at random from the initial population
114113 r = random .random ()
@@ -122,7 +121,7 @@ def mutate(self, index: int) -> List[str] :
122121 new_player = self .players [index ].clone ()
123122 return new_player
124123
125- def death (self , index : int = None ) -> int :
124+ def death (self , index = None ):
126125 """Selects the player to be removed. Note that the in the birth-death
127126 case, the player that is reproducing may also be replaced. However in
128127 the death-birth case, this player will be excluded from the choices.
@@ -132,7 +131,7 @@ def death(self, index: int=None) -> int:
132131 i = randrange (0 , len (self .players ))
133132 return i
134133
135- def birth (self , index : int = None ) -> int :
134+ def birth (self , index = None ):
136135 """The birth event."""
137136 # Compute necessary fitnesses.
138137 scores = self .score_all ()
@@ -148,7 +147,7 @@ def birth(self, index: int=None) -> int:
148147 j = fitness_proportionate_selection (scores )
149148 return j
150149
151- def fixation_check (self ) -> bool :
150+ def fixation_check (self ):
152151 """Is the population of a single type?"""
153152 if self .mutation_rate > 0 :
154153 return False
@@ -191,7 +190,7 @@ def __next__(self):
191190 self .fixation_check ()
192191 return self
193192
194- def _matchup_indices (self ) -> Tuple [ str ] :
193+ def _matchup_indices (self ):
195194 """Generate the matchup pairs."""
196195 indices = []
197196 N = len (self .players )
@@ -204,7 +203,7 @@ def _matchup_indices(self) -> Tuple[str]:
204203 indices .append ((i , j ))
205204 return indices
206205
207- def score_all (self ) -> Tuple [ str ] :
206+ def score_all (self ):
208207 """Plays the next round of the process. Every player is paired up
209208 against every other player and the total scores are recorded."""
210209 N = len (self .players )
@@ -222,7 +221,7 @@ def score_all(self) -> Tuple[str]:
222221 self .score_history .append (scores )
223222 return scores
224223
225- def population_distribution (self ) -> int :
224+ def population_distribution (self ):
226225 """Returns the population distribution of the last iteration."""
227226 player_names = [str (player ) for player in self .players ]
228227 counter = Counter (player_names )
@@ -249,14 +248,14 @@ def play(self):
249248 break
250249 return self .populations
251250
252- def __len__ (self ) -> int :
251+ def __len__ (self ):
253252 return len (self .populations )
254253
255254
256255class MoranProcessGraph (MoranProcess ):
257256 def __init__ (self , players , interaction_graph , reproduction_graph = None ,
258- turns : int = 100 , noise : float = 0 , deterministic_cache = None ,
259- mutation_rate : float = 0. , mode : str = 'bd' , match_class = Match ):
257+ turns = 100 , noise = 0 , deterministic_cache = None ,
258+ mutation_rate = 0. , mode = 'bd' , match_class = Match ):
260259 """
261260 An agent based Moran process class. In each round, each player plays a
262261 Match with each neighboring player according to the interaction graph.
@@ -321,7 +320,7 @@ def __init__(self, players, interaction_graph, reproduction_graph=None,
321320 self .index = dict (zip (interaction_graph .vertices (),
322321 range (len (players ))))
323322
324- def birth (self , index : int = None ) -> int :
323+ def birth (self , index = None ):
325324 """Compute the birth index."""
326325 scores = self .score_all ()
327326 if index :
@@ -335,7 +334,7 @@ def birth(self, index: int=None) -> int:
335334 j = fitness_proportionate_selection (scores )
336335 return j
337336
338- def death (self , index : int = None ) -> int :
337+ def death (self , index = None ):
339338 """Selects the player to be removed."""
340339 if self .mode == "db" :
341340 # Select a player to be replaced globally
@@ -350,7 +349,7 @@ def death(self, index: int=None) -> int:
350349 i = self .index [vertex ]
351350 return i
352351
353- def _matchup_indices (self ) -> Tuple [ str ] :
352+ def _matchup_indices (self ):
354353 """Generate the matchup pairs"""
355354 indices = set ()
356355 # For death-birth we only want the neighbors of the dead node
@@ -373,7 +372,7 @@ def _matchup_indices(self) -> Tuple[str]:
373372 indices .add ((i , j ))
374373 return indices
375374
376- def population_distribution (self ) -> int :
375+ def population_distribution (self ):
377376 """Returns the population distribution of the last iteration."""
378377 player_names = [str (player ) for player in self .players ]
379378 counter = Counter (player_names )
0 commit comments