Ejemplo n.º 1
0
    def test_complete(self):
        """A complete graph should produce the same results as the default
        case."""
        seeds = range(0, 5)
        players = []
        N = 6
        interaction_graph = axl.graph.complete_graph(N, loops=False)
        reproduction_graph = axl.graph.Graph(
            interaction_graph.edges, directed=interaction_graph.directed)
        reproduction_graph.add_loops()

        for _ in range(N // 2):
            players.append(axl.Cooperator())
            players.append(axl.Defector())
        for seed in seeds:
            mp = MoranProcess(players, seed=seed)
            mp.play()
            winner = mp.winning_strategy_name
            mp = MoranProcess(players,
                              interaction_graph=interaction_graph,
                              reproduction_graph=reproduction_graph,
                              seed=seed)
            mp.play()
            winner2 = mp.winning_strategy_name
            self.assertEqual(winner, winner2)
Ejemplo n.º 2
0
 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)
Ejemplo n.º 3
0
 def test_cycle_death_birth(self):
     """Test that death-birth can have different outcomes in the graph
     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,
                           interaction_graph=graph,
                           mode="bd",
                           seed=seed)
         mp.play()
         winner = mp.winning_strategy_name
         mp = MoranProcess(players,
                           interaction_graph=graph,
                           mode="db",
                           seed=seed)
         mp.play()
         winner2 = mp.winning_strategy_name
         self.assertEqual((winner == winner2), outcome)
Ejemplo n.º 4
0
 def test_seeding_inequality(self):
     players = [axl.Random(x) for x in (0.2, 0.4, 0.6, 0.8)]
     mp1 = MoranProcess(players, seed=0)
     mp1.play()
     mp2 = MoranProcess(players, seed=1)
     mp2.play()
     self.assertNotEqual(mp1, mp2)
Ejemplo n.º 5
0
 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)
Ejemplo n.º 6
0
 def test_seeding_equality(self, seed):
     players = [axl.Random(x) for x in (0.2, 0.4, 0.6, 0.8)]
     mp1 = MoranProcess(players, seed=seed)
     mp1.play()
     mp2 = MoranProcess(players, seed=seed)
     mp2.play()
     self.assertEqual(mp1.populations, mp2.populations)
Ejemplo n.º 7
0
    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)
Ejemplo n.º 8
0
 def test_death_birth(self):
     """Two player death-birth should fixate after one round."""
     p1, p2 = axl.Cooperator(), axl.Defector()
     seeds = range(0, 20)
     for seed in seeds:
         mp = MoranProcess((p1, p2), mode="db", seed=seed)
         mp.play()
     self.assertIsNotNone(mp.winning_strategy_name)
     # Number of populations is 2: the original and the one after the first round.
     self.assertEqual(len(mp.populations), 2)
Ejemplo n.º 9
0
    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)
Ejemplo n.º 10
0
 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))
Ejemplo n.º 11
0
 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)
Ejemplo n.º 12
0
 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)
Ejemplo n.º 13
0
 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))
Ejemplo n.º 14
0
 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])
Ejemplo n.º 15
0
 def test_different_game(self):
     # Possible for Cooperator to become fixed when using a different game
     p1, p2 = axl.Cooperator(), axl.Defector()
     game = axl.Game(r=4, p=2, s=1, t=6)
     mp = MoranProcess((p1, p2), turns=5, game=game, seed=88)
     populations = mp.play()
     self.assertEqual(mp.winning_strategy_name, str(p2))
Ejemplo n.º 16
0
 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])
Ejemplo n.º 17
0
 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(axl.Cooperator())
         players.append(axl.Defector())
     for seed, outcome in seeds:
         mp = MoranProcess(players, mode="bd", seed=seed)
         mp.play()
         winner = mp.winning_strategy_name
         mp = MoranProcess(players, mode="db", seed=seed)
         mp.play()
         winner2 = mp.winning_strategy_name
         self.assertEqual((winner == winner2), outcome)
Ejemplo n.º 18
0
 def test_two_random_players(self):
     p1, p2 = axl.Random(p=0.5), axl.Random(p=0.25)
     mp = MoranProcess((p1, p2), seed=66)
     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(p1))
Ejemplo n.º 19
0
 def test_three_players(self):
     players = [axl.Cooperator(), axl.Cooperator(), axl.Defector()]
     mp = MoranProcess(players, seed=11)
     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(axl.Defector()))
Ejemplo n.º 20
0
 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))
Ejemplo n.º 21
0
 def test_two_prob_end(self):
     p1, p2 = axl.Random(), axl.TitForTat()
     mp = MoranProcess((p1, p2), prob_end=0.5, seed=10)
     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(p1))
Ejemplo n.º 22
0
 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)
Ejemplo n.º 23
0
 def test_two_players(self):
     p1, p2 = axl.Cooperator(), axl.Defector()
     mp = MoranProcess((p1, p2), seed=99)
     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))
Ejemplo n.º 24
0
 def test_four_players(self):
     players = [axl.Cooperator() for _ in range(3)]
     players.append(axl.Defector())
     mp = MoranProcess(players, seed=29)
     populations = mp.play()
     self.assertEqual(len(mp), 8)
     self.assertEqual(len(populations), 8)
     self.assertEqual(populations, mp.populations)
     self.assertEqual(mp.winning_strategy_name, str(axl.Defector()))
Ejemplo n.º 25
0
 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))
Ejemplo n.º 26
0
 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))
Ejemplo n.º 27
0
 def test_two_prob_end(self):
     p1, p2 = axelrod.Random(), axelrod.TitForTat()
     axelrod.seed(0)
     mp = MoranProcess((p1, p2), prob_end=.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))
Ejemplo n.º 28
0
 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))
Ejemplo n.º 29
0
 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()))
Ejemplo n.º 30
0
 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))
Ejemplo n.º 31
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()))
Ejemplo n.º 32
0
 def test_atomic_mutation_cycler(self):
     axelrod.seed(10)
     cycle_length = 5
     players = [axelrod.EvolvableCycler(cycle_length=cycle_length)
                for _ in range(5)]
     mp = MoranProcess(players, turns=10, mutation_method="atomic")
     population = mp.play()
     self.assertEqual(mp.winning_strategy_name, 'EvolvableCycler: CDCDD, 5, 0.2, 1')
     self.assertEqual(len(mp.populations), 19)
     self.assertTrue(mp.fixated)
Ejemplo n.º 33
0
 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)
Ejemplo n.º 34
0
 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)
Ejemplo n.º 35
0
 def test_atomic_mutation_fsm(self):
     axelrod.seed(12)
     players = [axelrod.EvolvableFSMPlayer(num_states=2, initial_state=1, initial_action=C)
                for _ in range(5)]
     mp = MoranProcess(players, turns=10, mutation_method="atomic")
     population = mp.play()
     self.assertEqual(
         mp.winning_strategy_name,
         'EvolvableFSMPlayer: ((0, C, 1, D), (0, D, 1, C), (1, C, 0, D), (1, D, 1, C)), 1, C, 2, 0.1')
     self.assertEqual(len(mp.populations), 31)
     self.assertTrue(mp.fixated)
Ejemplo n.º 36
0
 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)
Ejemplo n.º 37
0
 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)
Ejemplo n.º 38
0
 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)
Ejemplo n.º 39
0
 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)
Ejemplo n.º 40
0
 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")
Ejemplo n.º 41
0
 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")
Ejemplo n.º 42
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()
Ejemplo n.º 43
0
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()
Ejemplo n.º 44
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()
Ejemplo n.º 45
0
 def test_exit_condition(self):
     p1, p2 = axelrod.Cooperator(), axelrod.Cooperator()
     mp = MoranProcess((p1, p2))
     mp.play()
     self.assertEqual(len(mp), 1)
Ejemplo n.º 46
0
 def test_exit_condition(self):
     p1, p2 = axelrod.Cooperator(), axelrod.Cooperator()
     mp = MoranProcess((p1, p2))
     mp.play()
     self.assertEqual(len(mp), 1)