Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Add type for Moran" #867

Merged
merged 1 commit into from
Feb 19, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 17 additions & 18 deletions axelrod/moran.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from collections import Counter
import random
from typing import Sequence, List, Tuple

import numpy as np

Expand All @@ -9,7 +8,7 @@
from .random_ import randrange


def fitness_proportionate_selection(scores: Sequence[int]) -> int :
def fitness_proportionate_selection(scores):
"""Randomly selects an individual proportionally to score.

Parameters
Expand All @@ -31,8 +30,8 @@ def fitness_proportionate_selection(scores: Sequence[int]) -> int :


class MoranProcess(object):
def __init__(self, players, turns: int=100, noise: float=0, deterministic_cache=None,
mutation_rate: float=0., mode: str='bd', match_class=Match):
def __init__(self, players, turns=100, noise=0, deterministic_cache=None,
mutation_rate=0., mode='bd', match_class=Match):
"""
An agent based Moran process class. In each round, each player plays a
Match with each other player. Players are assigned a fitness score by
Expand Down Expand Up @@ -108,7 +107,7 @@ def set_players(self):
self.players.append(player)
self.populations = [self.population_distribution()]

def mutate(self, index: int) -> List[str] :
def mutate(self, index):
"""Mutate the player at index."""
# Choose another strategy at random from the initial population
r = random.random()
Expand All @@ -122,7 +121,7 @@ def mutate(self, index: int) -> List[str] :
new_player = self.players[index].clone()
return new_player

def death(self, index: int=None) -> int:
def death(self, index=None):
"""Selects the player to be removed. Note that the in the birth-death
case, the player that is reproducing may also be replaced. However in
the death-birth case, this player will be excluded from the choices.
Expand All @@ -132,7 +131,7 @@ def death(self, index: int=None) -> int:
i = randrange(0, len(self.players))
return i

def birth(self, index: int=None) -> int:
def birth(self, index=None):
"""The birth event."""
# Compute necessary fitnesses.
scores = self.score_all()
Expand All @@ -148,7 +147,7 @@ def birth(self, index: int=None) -> int:
j = fitness_proportionate_selection(scores)
return j

def fixation_check(self) -> bool:
def fixation_check(self):
"""Is the population of a single type?"""
if self.mutation_rate > 0:
return False
Expand Down Expand Up @@ -191,7 +190,7 @@ def __next__(self):
self.fixation_check()
return self

def _matchup_indices(self) -> Tuple[str]:
def _matchup_indices(self):
"""Generate the matchup pairs."""
indices = []
N = len(self.players)
Expand All @@ -204,7 +203,7 @@ def _matchup_indices(self) -> Tuple[str]:
indices.append((i, j))
return indices

def score_all(self) -> Tuple[str]:
def score_all(self):
"""Plays the next round of the process. Every player is paired up
against every other player and the total scores are recorded."""
N = len(self.players)
Expand All @@ -222,7 +221,7 @@ def score_all(self) -> Tuple[str]:
self.score_history.append(scores)
return scores

def population_distribution(self) -> int:
def population_distribution(self):
"""Returns the population distribution of the last iteration."""
player_names = [str(player) for player in self.players]
counter = Counter(player_names)
Expand All @@ -249,14 +248,14 @@ def play(self):
break
return self.populations

def __len__(self) -> int:
def __len__(self):
return len(self.populations)


class MoranProcessGraph(MoranProcess):
def __init__(self, players, interaction_graph, reproduction_graph=None,
turns: int=100, noise: float =0, deterministic_cache=None,
mutation_rate: float=0., mode: str='bd', match_class=Match):
turns=100, noise=0, deterministic_cache=None,
mutation_rate=0., mode='bd', match_class=Match):
"""
An agent based Moran process class. In each round, each player plays a
Match with each neighboring player according to the interaction graph.
Expand Down Expand Up @@ -321,7 +320,7 @@ def __init__(self, players, interaction_graph, reproduction_graph=None,
self.index = dict(zip(interaction_graph.vertices(),
range(len(players))))

def birth(self, index: int=None) -> int:
def birth(self, index=None):
"""Compute the birth index."""
scores = self.score_all()
if index:
Expand All @@ -335,7 +334,7 @@ def birth(self, index: int=None) -> int:
j = fitness_proportionate_selection(scores)
return j

def death(self, index: int=None) -> int:
def death(self, index=None):
"""Selects the player to be removed."""
if self.mode == "db":
# Select a player to be replaced globally
Expand All @@ -350,7 +349,7 @@ def death(self, index: int=None) -> int:
i = self.index[vertex]
return i

def _matchup_indices(self) -> Tuple[str]:
def _matchup_indices(self):
"""Generate the matchup pairs"""
indices = set()
# For death-birth we only want the neighbors of the dead node
Expand All @@ -373,7 +372,7 @@ def _matchup_indices(self) -> Tuple[str]:
indices.add((i, j))
return indices

def population_distribution(self) -> int:
def population_distribution(self):
"""Returns the population distribution of the last iteration."""
player_names = [str(player) for player in self.players]
counter = Counter(player_names)
Expand Down