def test_crossover_dictionaries(self): dict1 = {'1': 1, '2': 2, '3': 3} dict2 = {'1': 'a', '2': 'b', '3': 'c'} rng = axl.RandomGenerator(seed=1) crossed = crossover_dictionaries(dict1, dict2, rng) self.assertEqual(crossed, {'1': 1, '2': 'b', '3': 'c'}) rng = axl.RandomGenerator(seed=2) crossed = crossover_dictionaries(dict1, dict2, rng) self.assertEqual(crossed, dict2)
def test_crossover_lists(self): list1 = [[0, C, 1, D], [0, D, 0, D], [1, C, 1, C], [1, D, 1, D]] list2 = [[0, D, 1, C], [0, C, 0, C], [1, D, 1, D], [1, C, 1, C]] rng = axl.RandomGenerator(seed=5) crossed = crossover_lists(list1, list2, rng) self.assertEqual(crossed, list1[:3] + list2[3:]) rng = axl.RandomGenerator(seed=1) crossed = crossover_lists(list1, list2, rng) self.assertEqual(crossed, list1[:1] + list2[1:])
def test_strategy(self): rng = axl.RandomGenerator(seed=7888) vector = [rng.random() for _ in range(16)] actions = [(C, C), (C, C), (D, D), (C, C), (D, C), (D, D), (D, C)] self.versus_test( opponent=axl.CyclerCCD(), expected_actions=actions, seed=0, init_kwargs={"sixteen_vector": vector}, ) actions = [(C, C), (C, C), (C, D), (C, C), (D, C), (D, D), (D, C)] self.versus_test( opponent=axl.CyclerCCD(), expected_actions=actions, seed=1, init_kwargs={"sixteen_vector": vector}, ) actions = [(C, C), (C, C), (D, C), (D, D), (D, D), (D, D), (D, D)] self.versus_test( opponent=axl.TitForTat(), expected_actions=actions, seed=0, init_kwargs={"sixteen_vector": vector}, ) actions = [(C, C), (C, C), (C, C), (D, C), (D, D), (D, D), (D, D)] self.versus_test( opponent=axl.TitForTat(), expected_actions=actions, seed=1, init_kwargs={"sixteen_vector": vector}, )
def test_vector_to_instance(self): num_states = 4 vector = [] rng = axl.RandomGenerator(seed=1111) for _ in range(2 * num_states): vector.extend(list(rng.random_vector(num_states))) for _ in range(num_states + 1): vector.append(rng.random()) player = self.player_class(num_states=num_states, seed=1) player.receive_vector(vector=vector) self.assertIsInstance(player, self.player_class)
def test_population_plot(self): # Test that can plot on a given matplotlib axes rng = axl.RandomGenerator(seed=15) players = [rng.choice(axl.demo_strategies)() for _ in range(5)] mp = axl.MoranProcess(players=players, turns=30, seed=20) mp.play() fig, axarr = plt.subplots(2, 2) ax = axarr[1, 0] mp.populations_plot(ax=ax) self.assertEqual(ax.get_xlim(), (-0.7000000000000001, 14.7)) self.assertEqual(ax.get_ylim(), (0, 5.25)) # Run without a given axis ax = mp.populations_plot() self.assertEqual(ax.get_xlim(), (-0.7000000000000001, 14.7)) self.assertEqual(ax.get_ylim(), (0, 5.25))
def test_crossover(self): """Test that crossover produces different strategies.""" rng = axl.RandomGenerator(seed=1) for _ in range(20): players = [] for _ in range(2): player = self.player(seed=rng.random_seed_int()) # Mutate to randomize player = player.mutate() players.append(player) player1, player2 = players crossed = player1.crossover(player2) if player1 != crossed and player2 != crossed and crossed == crossed.clone(): return # Should never get here unless a change breaks the test, so don't include in coverage. self.assertFalse(True) # pragma: no cover
def test_seeding_equality(self, seed): """Tests that a tournament with a given seed will return the same results each time. This specifically checks when running using multiple cores so as to confirm that https://github.com/Axelrod-Python/Axelrod/issues/1277 is fixed.""" rng = axl.RandomGenerator(seed=seed) players = [axl.Random(rng.random()) for _ in range(8)] tournament1 = axl.Tournament(name=self.test_name, players=players, game=self.game, turns=10, repetitions=100, seed=seed) tournament2 = axl.Tournament(name=self.test_name, players=players, game=self.game, turns=10, repetitions=100, seed=seed) for _ in range(4): results1 = tournament1.play(processes=2) results2 = tournament2.play(processes=2) self.assertEqual(results1.ranked_names, results2.ranked_names)
"""Test for the Gambler strategy. Most tests come from the LookerUp test suite. """ import copy import unittest import axelrod as axl from axelrod.load_data_ import load_pso_tables from axelrod.strategies.lookerup import create_lookup_table_keys from .test_evolvable_player import PartialClass, TestEvolvablePlayer from .test_lookerup import convert_original_to_current from .test_player import TestPlayer tables = load_pso_tables("pso_gambler.csv", directory="data") C, D = axl.Action.C, axl.Action.D random = axl.RandomGenerator() class TestGambler(TestPlayer): name = "Gambler" player = axl.Gambler expected_classifier = { "memory_depth": 1, "stochastic": True, "makes_use_of": set(), "long_run_time": False, "inspects_source": False, "manipulates_source": False, "manipulates_state": False,