def test_init(self): tournament = axelrod.ProbEndTournament( name=self.test_name, players=self.players, game=self.game, prob_end=self.test_prob_end, noise=0.2) self.assertEqual(tournament.match_generator.prob_end, tournament.prob_end) self.assertEqual(len(tournament.players), len(test_strategies)) self.assertEqual(tournament.game.score(('C', 'C')), (3, 3)) self.assertEqual(tournament.turns, float("inf")) self.assertEqual(tournament.repetitions, 10) self.assertEqual(tournament.name, 'test') self.assertEqual(tournament._processes, None) self.assertTrue(tournament._with_morality) self.assertIsInstance(tournament._logger, logging.Logger) self.assertEqual(tournament.deterministic_cache, {}) self.assertEqual(tournament.noise, 0.2) self.assertEqual(tournament._parallel_repetitions, 10) anonymous_tournament = axelrod.Tournament(players=self.players) self.assertEqual(anonymous_tournament.name, 'axelrod') # Test init when passing a cache: cache = axelrod.DeterministicCache() tournament = axelrod.ProbEndTournament( name=self.test_name, players=self.players, game=self.game, prob_end=self.test_prob_end, processes=4, noise=0.2, deterministic_cache=cache) self.assertEqual(tournament.deterministic_cache, cache)
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_build_cache_required(self): # Noisy empty deterministic cache cache = axelrod.DeterministicCache() tournament = axelrod.Tournament( name=self.test_name, players=self.players, game=self.game, processes=4, noise=0.2, deterministic_cache=cache) self.assertFalse(tournament._build_cache_required()) # Not noisy, deterministic cache has content key = (axelrod.TitForTat, axelrod.Defector, 3) cache[key] = [('C', 'D'), ('D', 'D'), ('D', 'D')] tournament = axelrod.Tournament( name=self.test_name, players=self.players, game=self.game, processes=4, noise=0.2, deterministic_cache=cache) self.assertFalse(tournament._build_cache_required()) # Not noisy, deterministic cache has content tournament = axelrod.Tournament( name=self.test_name, players=self.players, game=self.game, processes=4, deterministic_cache=cache) self.assertFalse(tournament._build_cache_required()) # Not noisy, empty deterministic cache cache = axelrod.DeterministicCache() tournament = axelrod.Tournament( name=self.test_name, players=self.players, game=self.game, processes=4, deterministic_cache=cache) self.assertTrue(tournament._build_cache_required())
def test_init(self): players = axelrod.Cooperator(), axelrod.Defector() mp = MoranProcess(players) self.assertEqual(mp.turns, axelrod.DEFAULT_TURNS) self.assertIsNone(mp.prob_end) self.assertIsNone(mp.game) self.assertEqual(mp.noise, 0) self.assertEqual(mp.initial_players, players) self.assertEqual(mp.players, list(players)) self.assertEqual(mp.populations, [Counter({ "Cooperator": 1, "Defector": 1 })]) self.assertIsNone(mp.winning_strategy_name) self.assertEqual(mp.mutation_rate, 0) self.assertEqual(mp.mode, "bd") self.assertEqual(mp.deterministic_cache, axelrod.DeterministicCache()) self.assertEqual(mp.mutation_targets, { "Cooperator": [players[1]], "Defector": [players[0]] }) self.assertEqual(mp.interaction_graph._edges, [(0, 1), (1, 0)]) self.assertEqual(mp.reproduction_graph._edges, [(0, 1), (1, 0), (0, 0), (1, 1)]) self.assertEqual(mp.fitness_transformation, None) self.assertEqual(mp.locations, [0, 1]) self.assertEqual(mp.index, {0: 0, 1: 1}) # Test non default graph cases players = axelrod.Cooperator(), axelrod.Defector(), axelrod.TitForTat() edges = [(0, 1), (2, 0), (1, 2)] graph = axelrod.graph.Graph(edges, directed=True) mp = MoranProcess(players, interaction_graph=graph) self.assertEqual(mp.interaction_graph._edges, [(0, 1), (2, 0), (1, 2)]) self.assertEqual( sorted(mp.reproduction_graph._edges), sorted([(0, 1), (2, 0), (1, 2), (0, 0), (1, 1), (2, 2)]), ) mp = MoranProcess(players, interaction_graph=graph, reproduction_graph=graph) self.assertEqual(mp.interaction_graph._edges, [(0, 1), (2, 0), (1, 2)]) self.assertEqual(mp.reproduction_graph._edges, [(0, 1), (2, 0), (1, 2)])
def test_init(self): tournament = axelrod.Tournament( name=self.test_name, players=self.players, game=self.game, turns=self.test_turns, processes=4, noise=0.2) self.assertEqual(len(tournament.players), len(test_strategies)) self.assertIsInstance( tournament.players[0].match_attributes['game'], axelrod.Game ) self.assertEqual(tournament.game.score(('C', 'C')), (3, 3)) self.assertEqual(tournament.turns, self.test_turns) self.assertEqual(tournament.repetitions, 10) self.assertEqual(tournament.name, 'test') self.assertEqual(tournament._processes, 4) self.assertTrue(tournament._with_morality) self.assertIsInstance(tournament._logger, logging.Logger) self.assertEqual(tournament.deterministic_cache, {}) self.assertEqual(tournament.noise, 0.2) self.assertEqual(tournament._parallel_repetitions, 10) anonymous_tournament = axelrod.Tournament(players=self.players) self.assertEqual(anonymous_tournament.name, 'axelrod') # Test init when passing a cache: cache = axelrod.DeterministicCache() tournament = axelrod.Tournament( name=self.test_name, players=self.players, game=self.game, turns=self.test_turns, processes=4, noise=0.2, deterministic_cache=cache) self.assertEqual(tournament.deterministic_cache, cache)
def test_init_from_file(self): loaded_cache = axl.DeterministicCache(file_name=self.test_load_file) self.assertEqual(loaded_cache[self.test_key], self.test_value)
def setUp(self): self.cache = axl.DeterministicCache()