def test_set_players(self): """Test that set players resets all players""" players = axelrod.Cooperator(), axelrod.Defector() mp = MoranProcess(players) players[0].cooperations += 1 mp.set_players() self.assertEqual(players[0].cooperations, 0)
def test_fixation_check(self): players = axelrod.Cooperator(), axelrod.Cooperator() mp = MoranProcess(players) self.assertTrue(mp.fixation_check()) players = axelrod.Cooperator(), axelrod.Defector() mp = MoranProcess(players) self.assertFalse(mp.fixation_check())
def test_property_players(self, strategies): """Hypothesis test that randomly checks players""" players = [s() for s in strategies] mp = MoranProcess(players) populations = mp.play() self.assertEqual(populations, mp.populations) self.assertIn(mp.winning_strategy_name, [str(p) for p in players])
def test_different_game(self): # Possible for Cooperator to become fixed when using a different game p1, p2 = axelrod.Cooperator(), axelrod.Defector() axelrod.seed(0) game = axelrod.Game(r=4, p=2, s=1, t=6) mp = MoranProcess((p1, p2), turns=5, game=game) populations = mp.play() self.assertEqual(mp.winning_strategy_name, str(p1))
def test_two_prob_end(self): p1, p2 = axelrod.Random(), axelrod.TitForTat() axelrod.seed(0) mp = MoranProcess((p1, p2), prob_end=0.5) populations = mp.play() self.assertEqual(len(mp), 4) self.assertEqual(len(populations), 4) self.assertEqual(populations, mp.populations) self.assertEqual(mp.winning_strategy_name, str(p1))
def test_three_players(self): players = [axelrod.Cooperator(), axelrod.Cooperator(), axelrod.Defector()] axelrod.seed(11) mp = MoranProcess(players) populations = mp.play() self.assertEqual(len(mp), 7) self.assertEqual(len(populations), 7) self.assertEqual(populations, mp.populations) self.assertEqual(mp.winning_strategy_name, str(axelrod.Defector()))
def test_two_players(self): p1, p2 = axelrod.Cooperator(), axelrod.Defector() random.seed(5) mp = MoranProcess((p1, p2)) populations = mp.play() self.assertEqual(len(mp), 5) self.assertEqual(len(populations), 5) self.assertEqual(populations, mp.populations) self.assertEqual(mp.winning_strategy_name, str(p2))
def test_two_random_players(self): p1, p2 = axelrod.Random(p=0.5), axelrod.Random(p=0.25) axelrod.seed(0) mp = MoranProcess((p1, p2)) populations = mp.play() self.assertEqual(len(mp), 2) self.assertEqual(len(populations), 2) self.assertEqual(populations, mp.populations) self.assertEqual(mp.winning_strategy_name, str(p2))
def test_cache(self): p1, p2 = axelrod.Cooperator(), axelrod.Defector() mp = MoranProcess((p1, p2)) mp.play() self.assertEqual(len(mp.deterministic_cache), 1) # Check that can pass a pre built cache cache = axelrod.DeterministicCache() mp = MoranProcess((p1, p2), deterministic_cache=cache) self.assertEqual(cache, mp.deterministic_cache)
def test_matchup_indices(self): players = axelrod.Cooperator(), axelrod.Defector() mp = MoranProcess(players) self.assertEqual(mp._matchup_indices(), {(0, 1)}) players = axelrod.Cooperator(), axelrod.Defector(), axelrod.TitForTat() edges = [(0, 1), (2, 0), (1, 2)] graph = axelrod.graph.Graph(edges, directed=True) mp = MoranProcess(players, mode="bd", interaction_graph=graph) self.assertEqual(mp._matchup_indices(), {(0, 1), (1, 2), (2, 0)})
def test_four_players(self): players = [axelrod.Cooperator() for _ in range(3)] players.append(axelrod.Defector()) random.seed(10) mp = MoranProcess(players) populations = mp.play() self.assertEqual(len(mp), 9) self.assertEqual(len(populations), 9) self.assertEqual(populations, mp.populations) self.assertEqual(mp.winning_strategy_name, str(axelrod.Defector()))
def test_mutate(self): """Test that a mutated player is returned""" players = axelrod.Cooperator(), axelrod.Defector(), axelrod.TitForTat() mp = MoranProcess(players, mutation_rate=0.5) axelrod.seed(0) self.assertEqual(mp.mutate(0), players[0]) axelrod.seed(1) self.assertEqual(mp.mutate(0), players[2]) axelrod.seed(4) self.assertEqual(mp.mutate(0), players[1])
def test_death_in_bd(self): players = axelrod.Cooperator(), axelrod.Defector(), axelrod.TitForTat() edges = [(0, 1), (2, 0), (1, 2)] graph = axelrod.graph.Graph(edges, directed=True) mp = MoranProcess(players, mode="bd", interaction_graph=graph) axelrod.seed(1) self.assertEqual(mp.death(0), 0) axelrod.seed(5) self.assertEqual(mp.death(0), 1) axelrod.seed(2) self.assertEqual(mp.death(0), 0)
def test_death_in_db(self): players = axelrod.Cooperator(), axelrod.Defector(), axelrod.TitForTat() mp = MoranProcess(players, mutation_rate=0.5, mode="db") axelrod.seed(1) self.assertEqual(mp.death(), 0) self.assertEqual(mp.dead, 0) axelrod.seed(5) self.assertEqual(mp.death(), 1) self.assertEqual(mp.dead, 1) axelrod.seed(2) self.assertEqual(mp.death(), 2) self.assertEqual(mp.dead, 2)
def test_reset(self): p1, p2 = axelrod.Cooperator(), axelrod.Defector() random.seed(8) mp = MoranProcess((p1, p2)) mp.play() self.assertEqual(len(mp), 4) self.assertEqual(len(mp.score_history), 3) mp.reset() self.assertEqual(len(mp), 1) self.assertEqual(mp.winning_strategy_name, None) self.assertEqual(mp.score_history, []) # Check that players reset for player, intial_player in zip(mp.players, mp.initial_players): self.assertEqual(str(player), str(intial_player))
def test_cooperator_can_win_with_fitness_transformation(self): axelrod.seed(689) players = ( axelrod.Cooperator(), axelrod.Defector(), axelrod.Defector(), axelrod.Defector(), ) w = 0.95 fitness_transformation = lambda score: 1 - w + w * score mp = MoranProcess( players, turns=10, fitness_transformation=fitness_transformation ) populations = mp.play() self.assertEqual(mp.winning_strategy_name, "Cooperator")
def test_constant_fitness_case(self): # Scores between an Alternator and Defector will be: (1, 6) axelrod.seed(0) players = ( axelrod.Alternator(), axelrod.Alternator(), axelrod.Defector(), axelrod.Defector(), ) mp = MoranProcess(players, turns=2) winners = [] for _ in range(100): mp.play() winners.append(mp.winning_strategy_name) mp.reset() winners = Counter(winners) self.assertEqual(winners["Defector"], 88)
def test_three_players_with_mutation(self): p1 = axelrod.Cooperator() p2 = axelrod.Random() p3 = axelrod.Defector() players = [p1, p2, p3] mp = MoranProcess(players, mutation_rate=0.2) self.assertDictEqual( mp.mutation_targets, {str(p1): [p3, p2], str(p2): [p1, p3], str(p3): [p1, p2]}, ) # Test that mutation causes the population to alternate between # fixations counters = [Counter({"Cooperator": 3}), Counter({"Defector": 3})] for counter in counters: for _ in itertools.takewhile( lambda x: x.population_distribution() != counter, mp ): pass self.assertEqual(mp.population_distribution(), counter)
def test_two_players_with_mutation(self): p1, p2 = axelrod.Cooperator(), axelrod.Defector() axelrod.seed(5) mp = MoranProcess((p1, p2), mutation_rate=0.2) self.assertDictEqual(mp.mutation_targets, {str(p1): [p2], str(p2): [p1]}) # Test that mutation causes the population to alternate between # fixations counters = [ Counter({"Cooperator": 2}), Counter({"Defector": 2}), Counter({"Cooperator": 2}), Counter({"Defector": 2}), ] for counter in counters: for _ in itertools.takewhile( lambda x: x.population_distribution() != counter, mp ): pass self.assertEqual(mp.population_distribution(), counter)
def test_asymmetry(self): """Asymmetry in interaction and reproduction should sometimes produce different results.""" seeds = [(1, True), (21, False)] players = [] N = 6 graph1 = axelrod.graph.cycle(N) graph2 = axelrod.graph.complete_graph(N) for _ in range(N // 2): players.append(axelrod.Cooperator()) for _ in range(N // 2): players.append(axelrod.Defector()) for seed, outcome in seeds: axelrod.seed(seed) mp = MoranProcess( players, interaction_graph=graph1, reproduction_graph=graph2 ) mp.play() winner = mp.winning_strategy_name axelrod.seed(seed) mp = MoranProcess( players, interaction_graph=graph2, reproduction_graph=graph1 ) mp.play() winner2 = mp.winning_strategy_name self.assertEqual((winner == winner2), outcome)
def test_atomic_mutation_fsm(self): players = [ axl.EvolvableFSMPlayer(num_states=2, initial_state=1, initial_action=C, seed=4) for _ in range(5) ] mp = MoranProcess(players, turns=10, mutation_method="atomic", seed=12) rounds = 10 for _ in range(rounds): next(mp) self.assertEqual( list(sorted(mp.populations[-1].items()))[0][0], 'EvolvableFSMPlayer: ((0, C, 0, C), (0, D, 1, C), (1, C, 1, D), (1, D, 1, D)), 0, D, 2, 0.1, 2240802643' ) self.assertEqual(len(mp.populations), 11) self.assertFalse(mp.fixated)
def test_death_birth_outcomes(self): """Show that birth-death and death-birth can produce different outcomes.""" seeds = [(1, True), (23, False)] players = [] N = 6 for _ in range(N // 2): players.append(axelrod.Cooperator()) players.append(axelrod.Defector()) for seed, outcome in seeds: axelrod.seed(seed) mp = MoranProcess(players, mode="bd") mp.play() winner = mp.winning_strategy_name axelrod.seed(seed) mp = MoranProcess(players, mode="db") mp.play() winner2 = mp.winning_strategy_name self.assertEqual((winner == winner2), outcome)
def test_cycle(self): """A cycle should sometimes produce different results vs. the default case.""" seeds = [(1, True), (3, False)] players = [] N = 6 graph = axl.graph.cycle(N) for _ in range(N // 2): players.append(axl.Cooperator()) for _ in range(N // 2): players.append(axl.Defector()) for seed, outcome in seeds: mp = MoranProcess(players, seed=seed) mp.play() winner = mp.winning_strategy_name mp = MoranProcess(players, interaction_graph=graph, seed=seed) mp.play() winner2 = mp.winning_strategy_name self.assertEqual((winner == winner2), outcome)
def test_reset(self): p1, p2 = axl.Cooperator(), axl.Defector() mp = MoranProcess((p1, p2), seed=45) mp.play() self.assertEqual(len(mp), 2) self.assertEqual(len(mp.score_history), 1) mp.reset() self.assertEqual(len(mp), 1) self.assertEqual(mp.winning_strategy_name, None) self.assertEqual(mp.score_history, []) # Check that players reset for player, initial_player in zip(mp.players, mp.initial_players): self.assertEqual(str(player), str(initial_player))
def test_constant_fitness_case(self): # Scores between an Alternator and Defector will be: (1, 6) axelrod.seed(0) players = (axelrod.Alternator(), axelrod.Alternator(), axelrod.Defector(), axelrod.Defector()) mp = MoranProcess(players, turns=2) winners = [] for _ in range(100): mp.play() winners.append(mp.winning_strategy_name) mp.reset() winners = Counter(winners) self.assertEqual(winners["Defector"], 88)
def test_complete(self): """A complete graph should produce the same results as the default case.""" seeds = range(0, 5) players = [] N = 6 graph = axelrod.graph.complete_graph(N) for _ in range(N // 2): players.append(axelrod.Cooperator()) players.append(axelrod.Defector()) for seed in seeds: axelrod.seed(seed) mp = MoranProcess(players) mp.play() winner = mp.winning_strategy_name axelrod.seed(seed) mp = MoranProcess(players, interaction_graph=graph) mp.play() winner2 = mp.winning_strategy_name self.assertEqual(winner, winner2)
def test_cycle_death_birth(self): """Test that death-birth can have different outcomes in the graph case.""" seeds = [(1, True), (5, False)] players = [] N = 6 graph = axelrod.graph.cycle(N) for _ in range(N // 2): players.append(axelrod.Cooperator()) for _ in range(N // 2): players.append(axelrod.Defector()) for seed, outcome in seeds: axelrod.seed(seed) mp = MoranProcess(players, interaction_graph=graph, mode="bd") mp.play() winner = mp.winning_strategy_name axelrod.seed(seed) mp = MoranProcess(players, interaction_graph=graph, mode="db") mp.play() winner2 = mp.winning_strategy_name self.assertEqual((winner == winner2), outcome)
def test_standard_fixation(self): """Test a traditional Moran process with a MockMatch.""" axelrod.seed(0) players = (axelrod.Cooperator(), axelrod.Cooperator(), axelrod.Defector(), axelrod.Defector()) mp = MoranProcess(players, match_class=MockMatch) winners = [] for i in range(100): mp.play() winner = mp.winning_strategy_name winners.append(winner) mp.reset() winners = Counter(winners) self.assertEqual(winners["Cooperator"], 82)
def test_cycle(self): """A cycle should sometimes produce different results vs. the default case.""" seeds = [(1, True), (8, False)] players = [] N = 6 graph = axelrod.graph.cycle(N) for _ in range(N // 2): players.append(axelrod.Cooperator()) for _ in range(N // 2): players.append(axelrod.Defector()) for seed, outcome in seeds: axelrod.seed(seed) mp = MoranProcess(players) mp.play() winner = mp.winning_strategy_name axelrod.seed(seed) mp = MoranProcess(players, interaction_graph=graph) mp.play() winner2 = mp.winning_strategy_name self.assertEqual((winner == winner2), outcome)
def test_iter(self): p1, p2 = axelrod.Cooperator(), axelrod.Defector() mp = MoranProcess((p1, p2)) self.assertEqual(mp.__iter__(), mp)
def test_birth_in_db(self): players = axelrod.Cooperator(), axelrod.Defector(), axelrod.TitForTat() mp = MoranProcess(players, mode="db") axelrod.seed(1) self.assertEqual(mp.death(), 0) self.assertEqual(mp.birth(0), 2)
def test_fps(self): players = axl.Cooperator(), axl.Defector() mp = MoranProcess(players, seed=1) self.assertEqual(mp.fitness_proportionate_selection([0, 0, 1]), 2) self.assertEqual(mp.fitness_proportionate_selection([1, 1, 1]), 2) self.assertEqual(mp.fitness_proportionate_selection([1, 1, 1]), 0)
def test_play_exception(self): p1, p2 = axelrod.Cooperator(), axelrod.Defector() mp = MoranProcess((p1, p2), mutation_rate=0.2) with self.assertRaises(ValueError): mp.play()
def test_next(self): players = axelrod.Cooperator(), axelrod.Defector() mp = MoranProcess(players) self.assertIsInstance(next(mp), MoranProcess)
def test_exit_condition(self): p1, p2 = axelrod.Cooperator(), axelrod.Cooperator() mp = MoranProcess((p1, p2)) mp.play() self.assertEqual(len(mp), 1)
def test_multiple_copies(recwarn): players = [Player('ktitfortatc') for _ in range(5)] + [Player('k42r') for _ in range(5)] mp = MoranProcess(players) mp.play() mp.populations_plot()
def test_birth_in_bd(self): players = axl.Cooperator(), axl.Defector(), axl.TitForTat() mp = MoranProcess(players, mode="bd", seed=2) self.assertEqual(mp.birth(), 0)
def test_birth_in_db(self): players = axl.Cooperator(), axl.Defector(), axl.TitForTat() mp = MoranProcess(players, mode="db", seed=1) self.assertEqual(mp.death(), 2) self.assertEqual(mp.birth(0), 2)