diff --git a/axelrod/tests/integration/test_filtering.py b/axelrod/tests/integration/test_filtering.py index a0e1beb72..b1fa8982f 100644 --- a/axelrod/tests/integration/test_filtering.py +++ b/axelrod/tests/integration/test_filtering.py @@ -1,7 +1,8 @@ import unittest -from axelrod import filtered_strategies, seed, short_run_time_strategies +import axelrod as axl from axelrod.tests.property import strategy_lists + from hypothesis import example, given, settings from hypothesis.strategies import integers @@ -26,7 +27,7 @@ def test_boolean_filtering(self, strategies): for classifier in classifiers: comprehension = set([s for s in strategies if s.classifier[classifier]]) filterset = {classifier: True} - filtered = set(filtered_strategies(filterset, strategies=strategies)) + filtered = set(axl.filtered_strategies(filterset, strategies=strategies)) self.assertEqual(comprehension, filtered) @given( @@ -39,7 +40,7 @@ def test_boolean_filtering(self, strategies): min_memory_depth=float("inf"), max_memory_depth=float("inf"), memory_depth=float("inf"), - strategies=short_run_time_strategies, + strategies=axl.short_run_time_strategies, ) @settings(max_examples=5) def test_memory_depth_filtering( @@ -55,7 +56,7 @@ def test_memory_depth_filtering( ] ) min_filterset = {"min_memory_depth": min_memory_depth} - min_filtered = set(filtered_strategies(min_filterset, + min_filtered = set(axl.filtered_strategies(min_filterset, strategies=strategies)) self.assertEqual(min_comprehension, min_filtered) @@ -67,7 +68,7 @@ def test_memory_depth_filtering( ] ) max_filterset = {"max_memory_depth": max_memory_depth} - max_filtered = set(filtered_strategies(max_filterset, + max_filtered = set(axl.filtered_strategies(max_filterset, strategies=strategies)) self.assertEqual(max_comprehension, max_filtered) @@ -79,7 +80,7 @@ def test_memory_depth_filtering( ] ) filterset = {"memory_depth": memory_depth} - filtered = set(filtered_strategies(filterset, strategies=strategies)) + filtered = set(axl.filtered_strategies(filterset, strategies=strategies)) self.assertEqual(comprehension, filtered) @given(seed_=integers(min_value=0, max_value=4294967295), @@ -95,7 +96,7 @@ def test_makes_use_of_filtering(self, seed_, strategies): classifiers = [["game"], ["length"], ["game", "length"]] for classifier in classifiers: - seed(seed_) + axl.seed(seed_) comprehension = set( [ s @@ -104,9 +105,9 @@ def test_makes_use_of_filtering(self, seed_, strategies): ] ) - seed(seed_) + axl.seed(seed_) filterset = {"makes_use_of": classifier} - filtered = set(filtered_strategies(filterset, strategies=strategies)) + filtered = set(axl.filtered_strategies(filterset, strategies=strategies)) self.assertEqual( comprehension, filtered, msg="classifier: {}".format(classifier) diff --git a/axelrod/tests/integration/test_matches.py b/axelrod/tests/integration/test_matches.py index 3b7aa7faf..8e28fb043 100644 --- a/axelrod/tests/integration/test_matches.py +++ b/axelrod/tests/integration/test_matches.py @@ -1,19 +1,19 @@ """Tests for some expected match behaviours""" import unittest -import axelrod +import axelrod as axl from axelrod.tests.property import strategy_lists from hypothesis import given, settings from hypothesis.strategies import integers -C, D = axelrod.Action.C, axelrod.Action.D +C, D = axl.Action.C, axl.Action.D deterministic_strategies = [ - s for s in axelrod.short_run_time_strategies if not s().classifier["stochastic"] + s for s in axl.short_run_time_strategies if not s().classifier["stochastic"] ] stochastic_strategies = [ - s for s in axelrod.short_run_time_strategies if s().classifier["stochastic"] + s for s in axl.short_run_time_strategies if s().classifier["stochastic"] ] @@ -29,7 +29,7 @@ def test_outcome_repeats(self, strategies, turns): """A test that if we repeat 3 matches with deterministic and well behaved strategies then we get the same result""" players = [s() for s in strategies] - matches = [axelrod.Match(players, turns) for _ in range(3)] + matches = [axl.Match(players, turns) for _ in range(3)] self.assertEqual(matches[0].play(), matches[1].play()) self.assertEqual(matches[1].play(), matches[2].play()) @@ -46,9 +46,9 @@ def test_outcome_repeats_stochastic(self, strategies, turns, seed): same result""" results = [] for _ in range(3): - axelrod.seed(seed) + axl.seed(seed) players = [s() for s in strategies] - results.append(axelrod.Match(players, turns).play()) + results.append(axl.Match(players, turns).play()) self.assertEqual(results[0], results[1]) self.assertEqual(results[1], results[2]) @@ -57,15 +57,15 @@ def test_matches_with_det_player_for_stochastic_classes(self): """A test based on a bug found in the cache. See: https://github.com/Axelrod-Python/Axelrod/issues/779""" - p1 = axelrod.MemoryOnePlayer(four_vector=(0, 0, 0, 0)) - p2 = axelrod.MemoryOnePlayer(four_vector=(1, 0, 1, 0)) - p3 = axelrod.MemoryOnePlayer(four_vector=(1, 1, 1, 0)) + p1 = axl.MemoryOnePlayer(four_vector=(0, 0, 0, 0)) + p2 = axl.MemoryOnePlayer(four_vector=(1, 0, 1, 0)) + p3 = axl.MemoryOnePlayer(four_vector=(1, 1, 1, 0)) - m = axelrod.Match((p1, p2), turns=3) + m = axl.Match((p1, p2), turns=3) self.assertEqual(m.play(), [(C, C), (D, C), (D, D)]) - m = axelrod.Match((p2, p3), turns=3) + m = axl.Match((p2, p3), turns=3) self.assertEqual(m.play(), [(C, C), (C, C), (C, C)]) - m = axelrod.Match((p1, p3), turns=3) + m = axl.Match((p1, p3), turns=3) self.assertEqual(m.play(), [(C, C), (D, C), (D, C)]) diff --git a/axelrod/tests/integration/test_names.py b/axelrod/tests/integration/test_names.py index 3c1511314..04745b778 100644 --- a/axelrod/tests/integration/test_names.py +++ b/axelrod/tests/integration/test_names.py @@ -1,13 +1,13 @@ import unittest -from axelrod import all_strategies +import axelrod as axl class TestNames(unittest.TestCase): def test_all_strategies_have_names(self): - names = [s.name for s in all_strategies if s.name] - self.assertEqual(len(names), len(all_strategies)) + names = [s.name for s in axl.all_strategies if s.name] + self.assertEqual(len(names), len(axl.all_strategies)) def test_all_names_are_unique(self): - names = set(s.name for s in all_strategies) - self.assertEqual(len(names), len(all_strategies)) + names = set(s.name for s in axl.all_strategies) + self.assertEqual(len(names), len(axl.all_strategies)) diff --git a/axelrod/tests/integration/test_sample_tournaments.py b/axelrod/tests/integration/test_sample_tournaments.py index e0ff254bd..b35d46129 100644 --- a/axelrod/tests/integration/test_sample_tournaments.py +++ b/axelrod/tests/integration/test_sample_tournaments.py @@ -1,14 +1,14 @@ import unittest -import axelrod +import axelrod as axl -C, D = axelrod.Action.C, axelrod.Action.D +C, D = axl.Action.C, axl.Action.D class TestSampleTournaments(unittest.TestCase): @classmethod def setUpClass(cls): - cls.game = axelrod.Game() + cls.game = axl.Game() @classmethod def get_test_outcome(cls, outcome, turns=10): @@ -16,10 +16,10 @@ def get_test_outcome(cls, outcome, turns=10): # Extract the name of players from the outcome tuples, # and initiate the players by getting the classes from axelrod. names = [out[0] for out in outcome] - players = [getattr(axelrod, n)() for n in names] + players = [getattr(axl, n)() for n in names] # Play the tournament and build the actual outcome tuples. - tournament = axelrod.Tournament( + tournament = axl.Tournament( players=players, game=cls.game, turns=turns, repetitions=1 ) results = tournament.play(progress_bar=False) diff --git a/axelrod/tests/integration/test_tournament.py b/axelrod/tests/integration/test_tournament.py index 28d505416..48a31c085 100644 --- a/axelrod/tests/integration/test_tournament.py +++ b/axelrod/tests/integration/test_tournament.py @@ -1,23 +1,23 @@ -import filecmp import unittest -from hypothesis import given, settings +import filecmp -import axelrod +import axelrod as axl from axelrod.strategy_transformers import FinalTransformer from axelrod.tests.property import tournaments +from hypothesis import given, settings class TestTournament(unittest.TestCase): @classmethod def setUpClass(cls): - cls.game = axelrod.Game() + cls.game = axl.Game() cls.players = [ - axelrod.Cooperator(), - axelrod.TitForTat(), - axelrod.Defector(), - axelrod.Grudger(), - axelrod.GoByMajority(), + axl.Cooperator(), + axl.TitForTat(), + axl.Defector(), + axl.Grudger(), + axl.GoByMajority(), ] cls.player_names = [str(p) for p in cls.players] cls.test_name = "test" @@ -33,7 +33,7 @@ def setUpClass(cls): cls.expected_outcome.sort() @given(tournaments( - strategies=axelrod.short_run_time_strategies, + strategies=axl.short_run_time_strategies, min_size=10, max_size=30, min_turns=2, @@ -51,7 +51,7 @@ def test_big_tournaments(self, tournament): ) def test_serial_play(self): - tournament = axelrod.Tournament( + tournament = axl.Tournament( name=self.test_name, players=self.players, game=self.game, @@ -63,7 +63,7 @@ def test_serial_play(self): self.assertEqual(actual_outcome, self.expected_outcome) def test_parallel_play(self): - tournament = axelrod.Tournament( + tournament = axl.Tournament( name=self.test_name, players=self.players, game=self.game, @@ -78,12 +78,12 @@ def test_repeat_tournament_deterministic(self): """A test to check that tournament gives same results.""" deterministic_players = [ s() - for s in axelrod.short_run_time_strategies + for s in axl.short_run_time_strategies if not s().classifier["stochastic"] ] files = [] for _ in range(2): - tournament = axelrod.Tournament( + tournament = axl.Tournament( name="test", players=deterministic_players, game=self.game, @@ -100,13 +100,13 @@ def test_repeat_tournament_stochastic(self): """ files = [] for _ in range(2): - axelrod.seed(0) + axl.seed(0) stochastic_players = [ s() - for s in axelrod.short_run_time_strategies + for s in axl.short_run_time_strategies if s().classifier["stochastic"] ] - tournament = axelrod.Tournament( + tournament = axl.Tournament( name="test", players=stochastic_players, game=self.game, @@ -121,14 +121,14 @@ def test_repeat_tournament_stochastic(self): class TestNoisyTournament(unittest.TestCase): def test_noisy_tournament(self): # Defector should win for low noise - players = [axelrod.Cooperator(), axelrod.Defector()] - tournament = axelrod.Tournament(players, turns=5, repetitions=3, noise=0.0) + players = [axl.Cooperator(), axl.Defector()] + tournament = axl.Tournament(players, turns=5, repetitions=3, noise=0.0) results = tournament.play(progress_bar=False) self.assertEqual(results.ranked_names[0], "Defector") # If the noise is large enough, cooperator should win - players = [axelrod.Cooperator(), axelrod.Defector()] - tournament = axelrod.Tournament(players, turns=5, repetitions=3, noise=0.75) + players = [axl.Cooperator(), axl.Defector()] + tournament = axl.Tournament(players, turns=5, repetitions=3, noise=0.75) results = tournament.play(progress_bar=False) self.assertEqual(results.ranked_names[0], "Cooperator") @@ -138,10 +138,10 @@ def test_players_do_not_know_match_length(self): """Create two players who should cooperate on last two turns if they don't know when those last two turns are. """ - p1 = FinalTransformer(["D", "D"])(axelrod.Cooperator)() - p2 = FinalTransformer(["D", "D"])(axelrod.Cooperator)() + p1 = FinalTransformer(["D", "D"])(axl.Cooperator)() + p2 = FinalTransformer(["D", "D"])(axl.Cooperator)() players = [p1, p2] - tournament = axelrod.Tournament(players, prob_end=0.5, repetitions=1) + tournament = axl.Tournament(players, prob_end=0.5, repetitions=1) results = tournament.play(progress_bar=False) # Check that both plays always cooperated for rating in results.cooperating_rating: @@ -152,12 +152,12 @@ def test_matches_have_different_length(self): A match between two players should have variable length across the repetitions """ - p1 = axelrod.Cooperator() - p2 = axelrod.Cooperator() - p3 = axelrod.Cooperator() + p1 = axl.Cooperator() + p2 = axl.Cooperator() + p3 = axl.Cooperator() players = [p1, p2, p3] - axelrod.seed(0) - tournament = axelrod.Tournament(players, prob_end=0.5, repetitions=2) + axl.seed(0) + tournament = axl.Tournament(players, prob_end=0.5, repetitions=2) results = tournament.play(progress_bar=False) # Check that match length are different across the repetitions self.assertNotEqual(results.match_lengths[0], results.match_lengths[1]) diff --git a/axelrod/tests/property.py b/axelrod/tests/property.py index c2657499a..95d08e2b8 100644 --- a/axelrod/tests/property.py +++ b/axelrod/tests/property.py @@ -3,14 +3,14 @@ """ import itertools -from axelrod import Game, Match, Tournament, short_run_time_strategies, strategies +import axelrod as axl from hypothesis.strategies import composite, floats, integers, lists, sampled_from @composite def strategy_lists( - draw, strategies=short_run_time_strategies, min_size=1, max_size=len(strategies) + draw, strategies=axl.short_run_time_strategies, min_size=1, max_size=len(axl.strategies) ): """ A hypothesis decorator to return a list of strategies @@ -31,7 +31,7 @@ def strategy_lists( @composite def matches( draw, - strategies=short_run_time_strategies, + strategies=axl.short_run_time_strategies, min_turns=1, max_turns=200, min_noise=0, @@ -61,14 +61,14 @@ def matches( players = [s() for s in strategies] turns = draw(integers(min_value=min_turns, max_value=max_turns)) noise = draw(floats(min_value=min_noise, max_value=max_noise)) - match = Match(players, turns=turns, noise=noise) + match = axl.Match(players, turns=turns, noise=noise) return match @composite def tournaments( draw, - strategies=short_run_time_strategies, + strategies=axl.short_run_time_strategies, min_size=1, max_size=10, min_turns=1, @@ -108,14 +108,14 @@ def tournaments( repetitions = draw(integers(min_value=min_repetitions, max_value=max_repetitions)) noise = draw(floats(min_value=min_noise, max_value=max_noise)) - tournament = Tournament(players, turns=turns, repetitions=repetitions, noise=noise) + tournament = axl.Tournament(players, turns=turns, repetitions=repetitions, noise=noise) return tournament @composite def prob_end_tournaments( draw, - strategies=short_run_time_strategies, + strategies=axl.short_run_time_strategies, min_size=1, max_size=10, min_prob_end=0, @@ -155,7 +155,7 @@ def prob_end_tournaments( repetitions = draw(integers(min_value=min_repetitions, max_value=max_repetitions)) noise = draw(floats(min_value=min_noise, max_value=max_noise)) - tournament = Tournament( + tournament = axl.Tournament( players, prob_end=prob_end, repetitions=repetitions, noise=noise ) return tournament @@ -164,7 +164,7 @@ def prob_end_tournaments( @composite def spatial_tournaments( draw, - strategies=short_run_time_strategies, + strategies=axl.short_run_time_strategies, min_size=1, max_size=10, min_turns=1, @@ -223,7 +223,7 @@ def spatial_tournaments( repetitions = draw(integers(min_value=min_repetitions, max_value=max_repetitions)) noise = draw(floats(min_value=min_noise, max_value=max_noise)) - tournament = Tournament( + tournament = axl.Tournament( players, turns=turns, repetitions=repetitions, noise=noise, edges=edges ) return tournament @@ -232,7 +232,7 @@ def spatial_tournaments( @composite def prob_end_spatial_tournaments( draw, - strategies=short_run_time_strategies, + strategies=axl.short_run_time_strategies, min_size=1, max_size=10, min_prob_end=0, @@ -291,7 +291,7 @@ def prob_end_spatial_tournaments( repetitions = draw(integers(min_value=min_repetitions, max_value=max_repetitions)) noise = draw(floats(min_value=min_noise, max_value=max_noise)) - tournament = Tournament( + tournament = axl.Tournament( players, prob_end=prob_end, repetitions=repetitions, noise=noise, edges=edges ) return tournament @@ -331,5 +331,5 @@ def games(draw, prisoners_dilemma=True, max_value=100): r = draw(integers(max_value=max_value)) p = draw(integers(max_value=max_value)) - game = Game(r=r, s=s, t=t, p=p) + game = axl.Game(r=r, s=s, t=t, p=p) return game diff --git a/axelrod/tests/strategies/test_adaptive.py b/axelrod/tests/strategies/test_adaptive.py index 0468c7c67..1fbca26f4 100644 --- a/axelrod/tests/strategies/test_adaptive.py +++ b/axelrod/tests/strategies/test_adaptive.py @@ -1,16 +1,16 @@ """Tests for the Adaptive strategy.""" -import axelrod +import axelrod as axl from .test_player import TestMatch, TestPlayer -C, D = axelrod.Action.C, axelrod.Action.D +C, D = axl.Action.C, axl.Action.D class TestAdaptive(TestPlayer): name = "Adaptive" - player = axelrod.Adaptive + player = axl.Adaptive expected_classifier = { "memory_depth": float("inf"), "stochastic": False, @@ -23,24 +23,24 @@ class TestAdaptive(TestPlayer): def test_strategy(self): actions = [(C, C)] * 6 + [(D, C)] * 8 - self.versus_test(axelrod.Cooperator(), expected_actions=actions) + self.versus_test(axl.Cooperator(), expected_actions=actions) actions = [(C, D)] * 6 + [(D, D)] * 8 - self.versus_test(axelrod.Defector(), expected_actions=actions) + self.versus_test(axl.Defector(), expected_actions=actions) actions = [(C, C), (C, D)] * 3 + [(D, C), (D, D)] * 4 - self.versus_test(axelrod.Alternator(), expected_actions=actions) + self.versus_test(axl.Alternator(), expected_actions=actions) actions = [(C, C)] * 6 + [(D, C)] + [(D, D)] * 4 + [(C, D), (C, C)] - self.versus_test(axelrod.TitForTat(), expected_actions=actions) + self.versus_test(axl.TitForTat(), expected_actions=actions) def test_scoring(self): - player = axelrod.Adaptive() - opponent = axelrod.Cooperator() + player = axl.Adaptive() + opponent = axl.Cooperator() player.play(opponent) player.play(opponent) self.assertEqual(3, player.scores[C]) - game = axelrod.Game(-3, 10, 10, 10) + game = axl.Game(-3, 10, 10, 10) player.set_match_attributes(game=game) player.play(opponent) self.assertEqual(0, player.scores[C]) diff --git a/axelrod/tests/strategies/test_adaptor.py b/axelrod/tests/strategies/test_adaptor.py index e489bdd33..740fdb252 100644 --- a/axelrod/tests/strategies/test_adaptor.py +++ b/axelrod/tests/strategies/test_adaptor.py @@ -2,18 +2,17 @@ import unittest -import axelrod -from axelrod import Game +import axelrod as axl from .test_player import TestPlayer, test_four_vector -C, D = axelrod.Action.C, axelrod.Action.D +C, D = axl.Action.C, axl.Action.D class TestAdaptorBrief(TestPlayer): name = "AdaptorBrief" - player = axelrod.AdaptorBrief + player = axl.AdaptorBrief expected_classifier = { "memory_depth": float("inf"), "stochastic": True, @@ -27,38 +26,38 @@ def test_strategy(self): # No error. actions = [(C, C), (C, C), (C, C), (C, C)] self.versus_test( - opponent=axelrod.AdaptorBrief(), expected_actions=actions, seed=0 + opponent=axl.AdaptorBrief(), expected_actions=actions, seed=0 ) # Error corrected. actions = [(C, C), (C, D), (D, C), (C, C)] self.versus_test( - opponent=axelrod.AdaptorBrief(), expected_actions=actions, seed=22 + opponent=axl.AdaptorBrief(), expected_actions=actions, seed=22 ) # Error corrected, example 2 actions = [(D, C), (C, D), (D, C), (C, D), (C, C)] self.versus_test( - opponent=axelrod.AdaptorBrief(), expected_actions=actions, seed=925 + opponent=axl.AdaptorBrief(), expected_actions=actions, seed=925 ) # Versus Cooperator actions = [(C, C)] * 8 self.versus_test( - opponent=axelrod.Cooperator(), expected_actions=actions, seed=0 + opponent=axl.Cooperator(), expected_actions=actions, seed=0 ) # Versus Defector actions = [(C, D), (D, D), (D, D), (D, D), (D, D), (D, D), (D, D)] self.versus_test( - opponent=axelrod.Defector(), expected_actions=actions, seed=0 + opponent=axl.Defector(), expected_actions=actions, seed=0 ) class TestAdaptorLong(TestPlayer): name = "AdaptorLong" - player = axelrod.AdaptorLong + player = axl.AdaptorLong expected_classifier = { "memory_depth": float("inf"), "stochastic": True, @@ -72,23 +71,23 @@ def test_strategy(self): # No error. actions = [(C, C), (C, C), (C, C), (C, C)] self.versus_test( - opponent=axelrod.AdaptorLong(), expected_actions=actions, seed=0 + opponent=axl.AdaptorLong(), expected_actions=actions, seed=0 ) # Error corrected. actions = [(C, C), (C, D), (D, D), (C, C), (C, C)] self.versus_test( - opponent=axelrod.AdaptorLong(), expected_actions=actions, seed=22 + opponent=axl.AdaptorLong(), expected_actions=actions, seed=22 ) # Versus Cooperator actions = [(C, C)] * 8 self.versus_test( - opponent=axelrod.Cooperator(), expected_actions=actions, seed=0 + opponent=axl.Cooperator(), expected_actions=actions, seed=0 ) # Versus Defector actions = [(C, D), (D, D), (C, D), (D, D), (D, D), (C, D), (D, D)] self.versus_test( - opponent=axelrod.Defector(), expected_actions=actions, seed=0 + opponent=axl.Defector(), expected_actions=actions, seed=0 ) diff --git a/axelrod/tests/strategies/test_alternator.py b/axelrod/tests/strategies/test_alternator.py index c6a11d0fb..64a347c33 100644 --- a/axelrod/tests/strategies/test_alternator.py +++ b/axelrod/tests/strategies/test_alternator.py @@ -1,16 +1,16 @@ """Tests for the Alternator strategy.""" -import axelrod +import axelrod as axl from .test_player import TestPlayer -C, D = axelrod.Action.C, axelrod.Action.D +C, D = axl.Action.C, axl.Action.D class TestAlternator(TestPlayer): name = "Alternator" - player = axelrod.Alternator + player = axl.Alternator expected_classifier = { "memory_depth": 1, "stochastic": False, @@ -23,11 +23,11 @@ class TestAlternator(TestPlayer): def test_strategy(self): actions = [(C, C), (D, C)] * 5 - self.versus_test(axelrod.Cooperator(), expected_actions=actions) + self.versus_test(axl.Cooperator(), expected_actions=actions) actions = [(C, D), (D, D)] * 5 - self.versus_test(axelrod.Defector(), expected_actions=actions) + self.versus_test(axl.Defector(), expected_actions=actions) - opponent = axelrod.MockPlayer(actions=[D, C]) + opponent = axl.MockPlayer(actions=[D, C]) actions = [(C, D), (D, C)] * 5 self.versus_test(opponent, expected_actions=actions) diff --git a/axelrod/tests/strategies/test_ann.py b/axelrod/tests/strategies/test_ann.py index 32a52b2fb..3a63d2131 100644 --- a/axelrod/tests/strategies/test_ann.py +++ b/axelrod/tests/strategies/test_ann.py @@ -1,15 +1,16 @@ """Tests for the ANN strategy.""" import unittest -import axelrod +import axelrod as axl from axelrod.evolvable_player import InsufficientParametersError from axelrod.load_data_ import load_weights from axelrod.strategies.ann import split_weights + from .test_player import TestPlayer from .test_evolvable_player import PartialClass, TestEvolvablePlayer -C, D = axelrod.Action.C, axelrod.Action.D +C, D = axl.Action.C, axl.Action.D nn_weights = load_weights() num_features, num_hidden, weights = nn_weights["Evolved ANN 5"] @@ -26,7 +27,7 @@ def test_split_weights(self): class TestEvolvedANN(TestPlayer): name = "Evolved ANN" - player = axelrod.EvolvedANN + player = axl.EvolvedANN expected_classifier = { "memory_depth": float("inf"), "stochastic": False, @@ -39,19 +40,19 @@ class TestEvolvedANN(TestPlayer): def test_strategy(self): actions = [(C, C)] * 5 - self.versus_test(axelrod.Cooperator(), expected_actions=actions) + self.versus_test(axl.Cooperator(), expected_actions=actions) actions = [(C, D)] + [(D, D)] * 5 - self.versus_test(axelrod.Defector(), expected_actions=actions) + self.versus_test(axl.Defector(), expected_actions=actions) actions = [(C, C)] * 5 - self.versus_test(axelrod.TitForTat(), expected_actions=actions) + self.versus_test(axl.TitForTat(), expected_actions=actions) class TestEvolvedANN5(TestPlayer): name = "Evolved ANN 5" - player = axelrod.EvolvedANN5 + player = axl.EvolvedANN5 expected_classifier = { "memory_depth": float("inf"), "stochastic": False, @@ -64,16 +65,16 @@ class TestEvolvedANN5(TestPlayer): def test_strategy(self): actions = [(C, C)] * 5 - self.versus_test(axelrod.Cooperator(), expected_actions=actions) + self.versus_test(axl.Cooperator(), expected_actions=actions) actions = [(C, D)] + [(D, D)] * 4 - self.versus_test(axelrod.Defector(), expected_actions=actions) + self.versus_test(axl.Defector(), expected_actions=actions) class TestEvolvedANNNoise05(TestPlayer): name = "Evolved ANN 5 Noise 05" - player = axelrod.EvolvedANNNoise05 + player = axl.EvolvedANNNoise05 expected_classifier = { "memory_depth": float("inf"), "stochastic": False, @@ -86,15 +87,15 @@ class TestEvolvedANNNoise05(TestPlayer): def test_strategy(self): actions = [(C, C)] * 5 - self.versus_test(axelrod.Cooperator(), expected_actions=actions) + self.versus_test(axl.Cooperator(), expected_actions=actions) actions = [(C, D), (D, D), (D, D)] - self.versus_test(axelrod.Defector(), expected_actions=actions) + self.versus_test(axl.Defector(), expected_actions=actions) class TestEvolvableANN(unittest.TestCase): - player_class = axelrod.EvolvableANN + player_class = axl.EvolvableANN def test_normalized_parameters(self): # Must specify at least one of cycle or cycle_length @@ -111,16 +112,16 @@ def test_normalized_parameters(self): class TestEvolvableANN2(TestEvolvablePlayer): name = "EvolvableANN" - player_class = axelrod.EvolvableANN - parent_class = axelrod.ANN + player_class = axl.EvolvableANN + parent_class = axl.ANN parent_kwargs = ["num_features", "num_hidden", "weights"] init_parameters = {"num_features": 17, "num_hidden": 8} class TestEvolvableANN3(TestEvolvablePlayer): name = "EvolvableANN" - player_class = axelrod.EvolvableANN - parent_class = axelrod.ANN + player_class = axl.EvolvableANN + parent_class = axl.ANN parent_kwargs = ["num_features", "num_hidden", "weights"] init_parameters = { "num_features": nn_weights["Evolved ANN 5"][0], @@ -131,7 +132,7 @@ class TestEvolvableANN3(TestEvolvablePlayer): # Substitute EvolvableANN as a regular EvolvedANN5. EvolvableANNPlayerWithDefault = PartialClass( - axelrod.EvolvableANN, + axl.EvolvableANN, num_features=num_features, num_hidden=num_hidden, weights=weights diff --git a/axelrod/tests/strategies/test_apavlov.py b/axelrod/tests/strategies/test_apavlov.py index a2ea62970..e720dce56 100644 --- a/axelrod/tests/strategies/test_apavlov.py +++ b/axelrod/tests/strategies/test_apavlov.py @@ -1,15 +1,15 @@ """Tests APavlov strategies.""" -import axelrod +import axelrod as axl from .test_player import TestPlayer -C, D = axelrod.Action.C, axelrod.Action.D +C, D = axl.Action.C, axl.Action.D class TestAPavlov2006(TestPlayer): name = "Adaptive Pavlov 2006" - player = axelrod.APavlov2006 + player = axl.APavlov2006 expected_classifier = { "memory_depth": float("inf"), @@ -24,12 +24,12 @@ class TestAPavlov2006(TestPlayer): def test_strategy(self): actions = [(C, C)] * 7 self.versus_test( - axelrod.Cooperator(), + axl.Cooperator(), expected_actions=actions, attrs={"opponent_class": "Cooperative"}, ) - opponent = axelrod.MockPlayer(actions=[C] * 6 + [D]) + opponent = axl.MockPlayer(actions=[C] * 6 + [D]) actions = [(C, C)] * 6 + [(C, D), (D, C)] self.versus_test( opponent, expected_actions=actions, attrs={"opponent_class": "Cooperative"} @@ -37,12 +37,12 @@ def test_strategy(self): actions = [(C, D)] + [(D, D)] * 6 self.versus_test( - axelrod.Defector(), + axl.Defector(), expected_actions=actions, attrs={"opponent_class": "ALLD"}, ) - opponent = axelrod.MockPlayer(actions=[D, C, D, C, D, C]) + opponent = axl.MockPlayer(actions=[D, C, D, C, D, C]) actions = [ (C, D), (D, C), @@ -59,25 +59,25 @@ def test_strategy(self): opponent, expected_actions=actions, attrs={"opponent_class": "STFT"} ) - opponent = axelrod.MockPlayer(actions=[D, D, C, D, D, C]) + opponent = axl.MockPlayer(actions=[D, D, C, D, D, C]) actions = [(C, D), (D, D), (D, C), (C, D), (D, D), (D, C), (D, D)] self.versus_test( opponent, expected_actions=actions, attrs={"opponent_class": "PavlovD"} ) - opponent = axelrod.MockPlayer(actions=[D, D, C, D, D, C, D]) + opponent = axl.MockPlayer(actions=[D, D, C, D, D, C, D]) actions = [(C, D), (D, D), (D, C), (C, D), (D, D), (D, C), (D, D), (C, D)] self.versus_test( opponent, expected_actions=actions, attrs={"opponent_class": "PavlovD"} ) - opponent = axelrod.MockPlayer(actions=[C, C, C, D, D, D]) + opponent = axl.MockPlayer(actions=[C, C, C, D, D, D]) actions = [(C, C), (C, C), (C, C), (C, D), (D, D), (D, D), (D, C)] self.versus_test( opponent, expected_actions=actions, attrs={"opponent_class": "Random"} ) - opponent = axelrod.MockPlayer(actions=[D, D, D, C, C, C]) + opponent = axl.MockPlayer(actions=[D, D, D, C, C, C]) actions = [(C, D), (D, D), (D, D), (D, C), (C, C), (C, C), (D, D)] self.versus_test( opponent, expected_actions=actions, attrs={"opponent_class": "Random"} @@ -86,7 +86,7 @@ def test_strategy(self): class TestAPavlov2011(TestPlayer): name = "Adaptive Pavlov 2011" - player = axelrod.APavlov2011 + player = axl.APavlov2011 expected_classifier = { "memory_depth": float("inf"), @@ -102,61 +102,61 @@ def test_strategy(self): actions = [(C, C)] * 8 self.versus_test( - axelrod.Cooperator(), + axl.Cooperator(), expected_actions=actions, attrs={"opponent_class": "Cooperative"}, ) actions = [(C, D)] + [(D, D)] * 9 self.versus_test( - axelrod.Defector(), + axl.Defector(), expected_actions=actions, attrs={"opponent_class": "ALLD"}, ) - opponent = axelrod.MockPlayer(actions=[C, D, D, D, D, D, D]) + opponent = axl.MockPlayer(actions=[C, D, D, D, D, D, D]) actions = [(C, C), (C, D)] + [(D, D)] * 5 + [(D, C)] self.versus_test( opponent, expected_actions=actions, attrs={"opponent_class": "ALLD"} ) - opponent = axelrod.MockPlayer(actions=[C, C, D, D, D, D, D]) + opponent = axl.MockPlayer(actions=[C, C, D, D, D, D, D]) actions = [(C, C), (C, C), (C, D)] + [(D, D)] * 4 + [(D, C)] self.versus_test( opponent, expected_actions=actions, attrs={"opponent_class": "ALLD"} ) - opponent = axelrod.MockPlayer(actions=[C, D, D, C, D, D, D]) + opponent = axl.MockPlayer(actions=[C, D, D, C, D, D, D]) actions = [(C, C), (C, D), (D, D), (D, C), (C, D), (D, D), (D, D), (D, C)] self.versus_test( opponent, expected_actions=actions, attrs={"opponent_class": "ALLD"} ) - opponent = axelrod.MockPlayer(actions=[C, D, D, C, C, D, D]) + opponent = axl.MockPlayer(actions=[C, D, D, C, C, D, D]) actions = [(C, C), (C, D), (D, D), (D, C), (C, C), (C, D), (C, D), (D, C)] self.versus_test( opponent, expected_actions=actions, attrs={"opponent_class": "STFT"} ) - opponent = axelrod.MockPlayer(actions=[C, D, C, D, C, D, D]) + opponent = axl.MockPlayer(actions=[C, D, C, D, C, D, D]) actions = [(C, C), (C, D), (D, C), (C, D), (D, C), (C, D), (C, D), (D, C)] self.versus_test( opponent, expected_actions=actions, attrs={"opponent_class": "STFT"} ) - opponent = axelrod.MockPlayer(actions=[D, D, D, C, C, C, C]) + opponent = axl.MockPlayer(actions=[D, D, D, C, C, C, C]) actions = [(C, D), (D, D), (D, D), (D, C), (C, C), (C, C), (C, C), (C, D)] self.versus_test( opponent, expected_actions=actions, attrs={"opponent_class": "STFT"} ) - opponent = axelrod.MockPlayer(actions=[C, C, C, C, D, D]) + opponent = axl.MockPlayer(actions=[C, C, C, C, D, D]) actions = [(C, C), (C, C), (C, C), (C, C), (C, D), (D, D), (D, C), (D, C)] self.versus_test( opponent, expected_actions=actions, attrs={"opponent_class": "Random"} ) - opponent = axelrod.MockPlayer(actions=[D, D, C, C, C, C]) + opponent = axl.MockPlayer(actions=[D, D, C, C, C, C]) actions = [(C, D), (D, D), (D, C), (C, C), (C, C), (C, C), (D, D), (D, D)] self.versus_test( opponent, expected_actions=actions, attrs={"opponent_class": "Random"} diff --git a/axelrod/tests/strategies/test_appeaser.py b/axelrod/tests/strategies/test_appeaser.py index 03dd21933..ede79c1d5 100644 --- a/axelrod/tests/strategies/test_appeaser.py +++ b/axelrod/tests/strategies/test_appeaser.py @@ -1,16 +1,16 @@ """Tests for the Appeaser strategy.""" -import axelrod +import axelrod as axl from .test_player import TestPlayer -C, D = axelrod.Action.C, axelrod.Action.D +C, D = axl.Action.C, axl.Action.D class TestAppeaser(TestPlayer): name = "Appeaser" - player = axelrod.Appeaser + player = axl.Appeaser expected_classifier = { "memory_depth": float("inf"), # Depends on internal memory. "stochastic": False, @@ -23,15 +23,15 @@ class TestAppeaser(TestPlayer): def test_strategy(self): actions = [(C, C), (C, C), (C, C), (C, C), (C, C)] - self.versus_test(axelrod.Cooperator(), expected_actions=actions) + self.versus_test(axl.Cooperator(), expected_actions=actions) actions = [(C, D), (D, D), (C, D), (D, D), (C, D)] - self.versus_test(axelrod.Defector(), expected_actions=actions) + self.versus_test(axl.Defector(), expected_actions=actions) - opponent = axelrod.MockPlayer(actions=[C, C, D, D]) + opponent = axl.MockPlayer(actions=[C, C, D, D]) actions = [(C, C), (C, C), (C, D), (D, D), (C, C), (C, C)] self.versus_test(opponent, expected_actions=actions) - opponent = axelrod.MockPlayer(actions=[C, C, D, D, D]) + opponent = axl.MockPlayer(actions=[C, C, D, D, D]) actions = [(C, C), (C, C), (C, D), (D, D), (C, D), (D, C), (D, C)] self.versus_test(opponent, expected_actions=actions) diff --git a/axelrod/tests/strategies/test_averagecopier.py b/axelrod/tests/strategies/test_averagecopier.py index 16dfcfb7e..ae23667e7 100644 --- a/axelrod/tests/strategies/test_averagecopier.py +++ b/axelrod/tests/strategies/test_averagecopier.py @@ -1,16 +1,16 @@ """Tests for the AverageCopier strategies.""" -import axelrod +import axelrod as axl from .test_player import TestPlayer -C, D = axelrod.Action.C, axelrod.Action.D +C, D = axl.Action.C, axl.Action.D class TestAverageCopier(TestPlayer): name = "Average Copier" - player = axelrod.AverageCopier + player = axl.AverageCopier expected_classifier = { "memory_depth": float("inf"), # Long memory "stochastic": True, @@ -24,15 +24,15 @@ class TestAverageCopier(TestPlayer): def test_strategy(self): # Tests that if opponent has played all C then player chooses C. actions = [(C, C)] * 10 - self.versus_test(axelrod.Cooperator(), expected_actions=actions, seed=1) + self.versus_test(axl.Cooperator(), expected_actions=actions, seed=1) actions = [(D, C)] + [(C, C)] * 9 - self.versus_test(axelrod.Cooperator(), expected_actions=actions, seed=2) + self.versus_test(axl.Cooperator(), expected_actions=actions, seed=2) # Tests that if opponent has played all D then player chooses D. actions = [(C, D)] + [(D, D)] * 9 - self.versus_test(axelrod.Defector(), expected_actions=actions, seed=1) + self.versus_test(axl.Defector(), expected_actions=actions, seed=1) actions = [(D, D)] + [(D, D)] * 9 - self.versus_test(axelrod.Defector(), expected_actions=actions, seed=2) + self.versus_test(axl.Defector(), expected_actions=actions, seed=2) # Variable behaviour based on the history and stochastic @@ -48,7 +48,7 @@ def test_strategy(self): (D, C), (C, D), ] - self.versus_test(axelrod.Alternator(), expected_actions=actions, seed=1) + self.versus_test(axl.Alternator(), expected_actions=actions, seed=1) actions = [ (D, C), @@ -62,9 +62,9 @@ def test_strategy(self): (C, C), (D, D), ] - self.versus_test(axelrod.Alternator(), expected_actions=actions, seed=2) + self.versus_test(axl.Alternator(), expected_actions=actions, seed=2) - opponent = axelrod.MockPlayer(actions=[C, C, D, D, D, D]) + opponent = axl.MockPlayer(actions=[C, C, D, D, D, D]) actions = [ (C, C), (C, C), @@ -79,7 +79,7 @@ def test_strategy(self): ] self.versus_test(opponent, expected_actions=actions, seed=1) - opponent = axelrod.MockPlayer(actions=[C, C, C, D, D, D]) + opponent = axl.MockPlayer(actions=[C, C, C, D, D, D]) actions = [ (D, C), (C, C), @@ -98,7 +98,7 @@ def test_strategy(self): class TestNiceAverageCopier(TestPlayer): name = "Nice Average Copier" - player = axelrod.NiceAverageCopier + player = axl.NiceAverageCopier expected_classifier = { "memory_depth": float("inf"), # Long memory "stochastic": True, @@ -112,11 +112,11 @@ class TestNiceAverageCopier(TestPlayer): def test_strategy(self): # Tests that if opponent has played all C then player chooses C. actions = [(C, C)] * 10 - self.versus_test(axelrod.Cooperator(), expected_actions=actions, seed=1) + self.versus_test(axl.Cooperator(), expected_actions=actions, seed=1) # Tests that if opponent has played all D then player chooses D. actions = [(C, D)] + [(D, D)] * 9 - self.versus_test(axelrod.Defector(), expected_actions=actions, seed=1) + self.versus_test(axl.Defector(), expected_actions=actions, seed=1) # Variable behaviour based on the history and stochastic behaviour actions = [ @@ -131,7 +131,7 @@ def test_strategy(self): (D, C), (D, D), ] - self.versus_test(axelrod.Alternator(), expected_actions=actions, seed=1) + self.versus_test(axl.Alternator(), expected_actions=actions, seed=1) actions = [ (C, C), @@ -145,9 +145,9 @@ def test_strategy(self): (D, C), (C, D), ] - self.versus_test(axelrod.Alternator(), expected_actions=actions, seed=2) + self.versus_test(axl.Alternator(), expected_actions=actions, seed=2) - opponent = axelrod.MockPlayer(actions=[C, C, D, D, D, D]) + opponent = axl.MockPlayer(actions=[C, C, D, D, D, D]) actions = [ (C, C), (C, C), @@ -162,7 +162,7 @@ def test_strategy(self): ] self.versus_test(opponent, expected_actions=actions, seed=1) - opponent = axelrod.MockPlayer(actions=[C, C, C, D, D, D]) + opponent = axl.MockPlayer(actions=[C, C, C, D, D, D]) actions = [ (C, C), (C, C), diff --git a/axelrod/tests/strategies/test_axelrod_first.py b/axelrod/tests/strategies/test_axelrod_first.py index 9198141c2..35ee2d5f4 100644 --- a/axelrod/tests/strategies/test_axelrod_first.py +++ b/axelrod/tests/strategies/test_axelrod_first.py @@ -1,16 +1,16 @@ """Tests for the First Axelrod strategies.""" -import axelrod +import axelrod as axl from .test_player import TestPlayer, test_four_vector -C, D = axelrod.Action.C, axelrod.Action.D +C, D = axl.Action.C, axl.Action.D class TestFirstByDavis(TestPlayer): name = "First by Davis: 10" - player = axelrod.FirstByDavis + player = axl.FirstByDavis expected_classifier = { "memory_depth": float("inf"), "stochastic": False, @@ -24,21 +24,21 @@ class TestFirstByDavis(TestPlayer): def test_strategy(self): # Cooperates for the first ten rounds actions = [(C, C)] * 10 - self.versus_test(axelrod.Cooperator(), expected_actions=actions) + self.versus_test(axl.Cooperator(), expected_actions=actions) actions = [(C, D)] * 10 - self.versus_test(axelrod.Defector(), expected_actions=actions) + self.versus_test(axl.Defector(), expected_actions=actions) actions = [(C, C), (C, D)] * 5 - self.versus_test(axelrod.Alternator(), expected_actions=actions) + self.versus_test(axl.Alternator(), expected_actions=actions) # If opponent defects at any point then the player will defect forever # (after 10 rounds) - opponent = axelrod.MockPlayer(actions=[C] * 10 + [D]) + opponent = axl.MockPlayer(actions=[C] * 10 + [D]) actions = [(C, C)] * 10 + [(C, D), (D, C)] self.versus_test(opponent, expected_actions=actions) - opponent = axelrod.MockPlayer(actions=[C] * 15 + [D]) + opponent = axl.MockPlayer(actions=[C] * 15 + [D]) actions = [(C, C)] * 15 + [(C, D), (D, C)] self.versus_test(opponent, expected_actions=actions) @@ -46,7 +46,7 @@ def test_strategy(self): class TestFirstByDowning(TestPlayer): name = "First by Downing" - player = axelrod.FirstByDowning + player = axl.FirstByDowning expected_classifier = { "memory_depth": float("inf"), "stochastic": False, @@ -59,24 +59,24 @@ class TestFirstByDowning(TestPlayer): def test_strategy(self): actions = [(D, C), (D, C), (C, C)] - self.versus_test(axelrod.Cooperator(), expected_actions=actions) + self.versus_test(axl.Cooperator(), expected_actions=actions) actions = [(D, D), (D, D), (D, D)] - self.versus_test(axelrod.Defector(), expected_actions=actions) + self.versus_test(axl.Defector(), expected_actions=actions) - opponent = axelrod.MockPlayer(actions=[D, C, C]) + opponent = axl.MockPlayer(actions=[D, C, C]) actions = [(D, D), (D, C), (D, C), (D, D)] self.versus_test(opponent, expected_actions=actions) - opponent = axelrod.MockPlayer(actions=[D, D, C]) + opponent = axl.MockPlayer(actions=[D, D, C]) actions = [(D, D), (D, D), (D, C), (D, D)] self.versus_test(opponent, expected_actions=actions) - opponent = axelrod.MockPlayer(actions=[C, C, D, D, C, C]) + opponent = axl.MockPlayer(actions=[C, C, D, D, C, C]) actions = [(D, C), (D, C), (C, D), (D, D), (D, C), (D, C), (D, C)] self.versus_test(opponent, expected_actions=actions) - opponent = axelrod.MockPlayer(actions=[C, C, C, C, D, D]) + opponent = axl.MockPlayer(actions=[C, C, C, C, D, D]) actions = [(D, C), (D, C), (C, C), (D, C), (D, D), (C, D), (D, C)] self.versus_test(opponent, expected_actions=actions) @@ -84,7 +84,7 @@ def test_strategy(self): class TestFirstByFeld(TestPlayer): name = "First by Feld: 1.0, 0.5, 200" - player = axelrod.FirstByFeld + player = axl.FirstByFeld expected_classifier = { "memory_depth": 200, "stochastic": True, @@ -99,48 +99,48 @@ def test_cooperation_probability(self): # Test cooperation probabilities p1 = self.player(start_coop_prob=1.0, end_coop_prob=0.8, rounds_of_decay=100) self.assertEqual(1.0, p1._cooperation_probability()) - p2 = axelrod.Cooperator() - match = axelrod.Match((p1, p2), turns=50) + p2 = axl.Cooperator() + match = axl.Match((p1, p2), turns=50) match.play() self.assertEqual(0.9, p1._cooperation_probability()) - match = axelrod.Match((p1, p2), turns=100) + match = axl.Match((p1, p2), turns=100) match.play() self.assertEqual(0.8, p1._cooperation_probability()) # Test cooperation probabilities, second set of params p1 = self.player(start_coop_prob=1.0, end_coop_prob=0.5, rounds_of_decay=200) self.assertEqual(1.0, p1._cooperation_probability()) - match = axelrod.Match((p1, p2), turns=100) + match = axl.Match((p1, p2), turns=100) match.play() self.assertEqual(0.75, p1._cooperation_probability()) - match = axelrod.Match((p1, p2), turns=200) + match = axl.Match((p1, p2), turns=200) match.play() self.assertEqual(0.5, p1._cooperation_probability()) def test_decay(self): # Test beyond 200 rounds - for opponent in [axelrod.Cooperator(), axelrod.Defector()]: + for opponent in [axl.Cooperator(), axl.Defector()]: player = self.player() self.assertEqual(player._cooperation_probability(), player._start_coop_prob) - match = axelrod.Match((player, opponent), turns=201) + match = axl.Match((player, opponent), turns=201) match.play() self.assertEqual(player._cooperation_probability(), player._end_coop_prob) def test_strategy(self): actions = [(C, C)] * 41 + [(D, C)] - self.versus_test(axelrod.Cooperator(), expected_actions=actions, seed=1) + self.versus_test(axl.Cooperator(), expected_actions=actions, seed=1) actions = [(C, C)] * 16 + [(D, C)] - self.versus_test(axelrod.Cooperator(), expected_actions=actions, seed=2) + self.versus_test(axl.Cooperator(), expected_actions=actions, seed=2) actions = [(C, D)] + [(D, D)] * 20 - self.versus_test(axelrod.Defector(), expected_actions=actions) + self.versus_test(axl.Defector(), expected_actions=actions) class TestFirstByGraaskamp(TestPlayer): name = "First by Graaskamp: 0.05" - player = axelrod.FirstByGraaskamp + player = axl.FirstByGraaskamp expected_classifier = { "memory_depth": float("inf"), "stochastic": True, @@ -163,19 +163,19 @@ def test_strategy(self): actions += [(D, C)] # 51 turns actions += [(C, D), (D, C)] * 2 + [(C, D)] # 56 turns self.versus_test( - axelrod.Alternator(), expected_actions=actions, attrs=expected_attrs + axl.Alternator(), expected_actions=actions, attrs=expected_attrs ) # Against defector actions = [(C, D)] + [(D, D)] * 55 # 56 turns self.versus_test( - axelrod.Defector(), expected_actions=actions, attrs=expected_attrs + axl.Defector(), expected_actions=actions, attrs=expected_attrs ) # Against cooperator actions = [(C, C)] * 50 + [(D, C)] + [(C, C)] * 5 self.versus_test( - axelrod.Cooperator(), expected_actions=actions, attrs=expected_attrs + axl.Cooperator(), expected_actions=actions, attrs=expected_attrs ) # Test recognition of random player @@ -185,12 +185,12 @@ def test_strategy(self): } actions = [(C, C)] * 50 + [(D, C)] + [(C, C)] * 5 # 56 turns self.versus_test( - axelrod.Cooperator(), expected_actions=actions, attrs=expected_attrs + axl.Cooperator(), expected_actions=actions, attrs=expected_attrs ) expected_attrs = {"opponent_is_random": False, "next_random_defection_turn": 68} actions += [(C, C)] # 57 turns self.versus_test( - axelrod.Cooperator(), expected_actions=actions, attrs=expected_attrs + axl.Cooperator(), expected_actions=actions, attrs=expected_attrs ) expected_attrs = { @@ -202,11 +202,11 @@ def test_strategy(self): actions += [(C, D), (D, C)] * 3 # 57 turns actions += [(D, D)] self.versus_test( - axelrod.Alternator(), expected_actions=actions, attrs=expected_attrs + axl.Alternator(), expected_actions=actions, attrs=expected_attrs ) actions += [(D, C), (D, D)] * 5 self.versus_test( - axelrod.Alternator(), expected_actions=actions, attrs=expected_attrs + axl.Alternator(), expected_actions=actions, attrs=expected_attrs ) # Test versus TfT @@ -218,27 +218,27 @@ def test_strategy(self): actions += [(C, D), (D, C)] * 3 # 56 turns actions += [(C, D), (D, C)] * 50 self.versus_test( - axelrod.TitForTat(), expected_actions=actions, seed=0, attrs=expected_attrs + axl.TitForTat(), expected_actions=actions, seed=0, attrs=expected_attrs ) # Test random defections expected_attrs = {"opponent_is_random": False, "next_random_defection_turn": 78} actions = [(C, C)] * 50 + [(D, C)] + [(C, C)] * 16 + [(D, C)] + [(C, C)] self.versus_test( - axelrod.Cooperator(), expected_actions=actions, seed=0, attrs=expected_attrs + axl.Cooperator(), expected_actions=actions, seed=0, attrs=expected_attrs ) expected_attrs = {"opponent_is_random": False, "next_random_defection_turn": 77} actions = [(C, C)] * 50 + [(D, C)] + [(C, C)] * 12 + [(D, C)] + [(C, C)] self.versus_test( - axelrod.Cooperator(), expected_actions=actions, seed=1, attrs=expected_attrs + axl.Cooperator(), expected_actions=actions, seed=1, attrs=expected_attrs ) class TestFirstByGrofman(TestPlayer): name = "First by Grofman" - player = axelrod.FirstByGrofman + player = axl.FirstByGrofman expected_classifier = { "memory_depth": 1, "stochastic": True, @@ -251,16 +251,16 @@ class TestFirstByGrofman(TestPlayer): def test_strategy(self): actions = [(C, C)] * 7 - self.versus_test(axelrod.Cooperator(), expected_actions=actions) + self.versus_test(axl.Cooperator(), expected_actions=actions) actions = [(C, C), (C, D), (D, C)] - self.versus_test(axelrod.Alternator(), expected_actions=actions) + self.versus_test(axl.Alternator(), expected_actions=actions) - opponent = axelrod.MockPlayer(actions=[D] * 8) + opponent = axl.MockPlayer(actions=[D] * 8) actions = [(C, D), (C, D), (D, D), (C, D), (D, D), (C, D), (C, D), (D, D)] self.versus_test(opponent, expected_actions=actions, seed=1) - opponent = axelrod.MockPlayer(actions=[D] * 8) + opponent = axl.MockPlayer(actions=[D] * 8) actions = [(C, D), (D, D), (C, D), (D, D), (C, D), (C, D), (C, D), (D, D)] self.versus_test(opponent, expected_actions=actions, seed=2) @@ -268,7 +268,7 @@ def test_strategy(self): class TestFirstByJoss(TestPlayer): name = "First by Joss: 0.9" - player = axelrod.FirstByJoss + player = axl.FirstByJoss expected_classifier = { "memory_depth": 1, "stochastic": True, @@ -285,22 +285,22 @@ def test_four_vector(self): def test_strategy(self): actions = [(C, C), (C, C), (C, C), (C, C)] - self.versus_test(axelrod.Cooperator(), expected_actions=actions, seed=1) + self.versus_test(axl.Cooperator(), expected_actions=actions, seed=1) actions = [(C, C), (D, C), (D, C), (C, C)] - self.versus_test(axelrod.Cooperator(), expected_actions=actions, seed=2) + self.versus_test(axl.Cooperator(), expected_actions=actions, seed=2) actions = [(C, D), (D, D), (D, D), (D, D)] - self.versus_test(axelrod.Defector(), expected_actions=actions, seed=1) + self.versus_test(axl.Defector(), expected_actions=actions, seed=1) actions = [(C, D), (D, D), (D, D), (D, D)] - self.versus_test(axelrod.Defector(), expected_actions=actions, seed=2) + self.versus_test(axl.Defector(), expected_actions=actions, seed=2) class TestFirstByNydegger(TestPlayer): name = "First by Nydegger" - player = axelrod.FirstByNydegger + player = axl.FirstByNydegger expected_classifier = { "memory_depth": 3, "stochastic": False, @@ -335,15 +335,15 @@ def test_strategy(self): # Test trailing post-round 3 play actions = [(C, C)] * 9 - self.versus_test(axelrod.Cooperator(), expected_actions=actions) + self.versus_test(axl.Cooperator(), expected_actions=actions) actions = [(C, D), (D, D), (D, D), (C, D), (C, D), (C, D), (C, D), (C, D)] - self.versus_test(axelrod.Defector(), expected_actions=actions) + self.versus_test(axl.Defector(), expected_actions=actions) actions = [(C, C), (C, D), (D, C), (C, D), (D, C), (C, D), (D, C), (C, D)] - self.versus_test(axelrod.Alternator(), expected_actions=actions) + self.versus_test(axl.Alternator(), expected_actions=actions) - opponent = axelrod.MockPlayer(actions=[D, C]) + opponent = axl.MockPlayer(actions=[D, C]) actions = [(C, D), (D, C), (D, D), (D, C), (D, D), (D, C), (D, D), (D, C)] self.versus_test(opponent, expected_actions=actions) @@ -351,7 +351,7 @@ def test_strategy(self): class TestFirstByShubik(TestPlayer): name = "First by Shubik" - player = axelrod.FirstByShubik + player = axl.FirstByShubik expected_classifier = { "memory_depth": float("inf"), "stochastic": False, @@ -364,24 +364,24 @@ class TestFirstByShubik(TestPlayer): def test_strategy(self): actions = [(C, C), (C, C), (C, C)] - self.versus_test(axelrod.Cooperator(), expected_actions=actions) + self.versus_test(axl.Cooperator(), expected_actions=actions) actions = [(C, C), (C, D), (D, C)] - self.versus_test(axelrod.Alternator(), expected_actions=actions) + self.versus_test(axl.Alternator(), expected_actions=actions) - opponent = axelrod.MockPlayer(actions=[D, C, C]) + opponent = axl.MockPlayer(actions=[D, C, C]) actions = [(C, D), (D, C), (C, C), (C, D)] self.versus_test(opponent, expected_actions=actions) - opponent = axelrod.MockPlayer(actions=[D, C, D, C, C]) + opponent = axl.MockPlayer(actions=[D, C, D, C, C]) actions = [(C, D), (D, C), (C, D), (D, C), (D, C), (C, D), (D, C)] self.versus_test(opponent, expected_actions=actions) - opponent = axelrod.MockPlayer(actions=[D, C, D, D, C]) + opponent = axl.MockPlayer(actions=[D, C, D, D, C]) actions = [(C, D), (D, C), (C, D), (D, D), (D, C), (C, D), (D, C)] self.versus_test(opponent, expected_actions=actions) - opponent = axelrod.MockPlayer(actions=[D, C, D, C, C, D]) + opponent = axl.MockPlayer(actions=[D, C, D, C, C, D]) actions = [ (C, D), (D, C), @@ -400,7 +400,7 @@ def test_strategy(self): class TestFirstByTullock(TestPlayer): name = "First by Tullock" - player = axelrod.FirstByTullock + player = axl.FirstByTullock expected_classifier = { "memory_depth": float("inf"), "stochastic": True, @@ -414,29 +414,29 @@ class TestFirstByTullock(TestPlayer): def test_strategy(self): """Cooperates for first ten rounds""" actions = [(C, C), (C, D)] * 5 - self.versus_test(axelrod.Alternator(), expected_actions=actions) + self.versus_test(axl.Alternator(), expected_actions=actions) actions = [(C, D)] * 11 + [(D, D)] * 2 - self.versus_test(axelrod.Defector(), expected_actions=actions) + self.versus_test(axl.Defector(), expected_actions=actions) - opponent = axelrod.MockPlayer(actions=[D] * 10 + [C]) + opponent = axl.MockPlayer(actions=[D] * 10 + [C]) actions = [(C, D)] * 10 + [(C, C), (D, D)] self.versus_test(opponent, expected_actions=actions) # Test beyond 10 rounds - opponent = axelrod.MockPlayer(actions=[D] * 5 + [C] * 6) + opponent = axl.MockPlayer(actions=[D] * 5 + [C] * 6) actions = [(C, D)] * 5 + [(C, C)] * 6 + [(D, D)] * 4 self.versus_test(opponent, expected_actions=actions, seed=20) - opponent = axelrod.MockPlayer(actions=[D] * 5 + [C] * 6) + opponent = axl.MockPlayer(actions=[D] * 5 + [C] * 6) actions = [(C, D)] * 5 + [(C, C)] * 6 + [(C, D), (D, D), (D, D), (C, D)] self.versus_test(opponent, expected_actions=actions, seed=1) - opponent = axelrod.MockPlayer(actions=[C] * 9 + [D] * 2) + opponent = axl.MockPlayer(actions=[C] * 9 + [D] * 2) actions = [(C, C)] * 9 + [(C, D)] * 2 + [(C, C), (D, C), (D, C), (C, C)] self.versus_test(opponent, expected_actions=actions, seed=1) - opponent = axelrod.MockPlayer(actions=[C] * 9 + [D] * 2) + opponent = axl.MockPlayer(actions=[C] * 9 + [D] * 2) actions = [(C, C)] * 9 + [(C, D)] * 2 + [(D, C), (D, C), (C, C), (C, C)] self.versus_test(opponent, expected_actions=actions, seed=2) @@ -444,7 +444,7 @@ def test_strategy(self): class TestFirstByAnonymous(TestPlayer): name = "First by Anonymous" - player = axelrod.FirstByAnonymous + player = axl.FirstByAnonymous expected_classifier = { "memory_depth": 0, "stochastic": True, @@ -457,16 +457,16 @@ class TestFirstByAnonymous(TestPlayer): def test_strategy(self): actions = [(D, C), (C, C), (C, C), (D, C), (C, C), (C, C)] - self.versus_test(axelrod.Cooperator(), expected_actions=actions, seed=1) + self.versus_test(axl.Cooperator(), expected_actions=actions, seed=1) actions = [(C, C), (C, C), (D, C), (C, C), (C, C), (D, C)] - self.versus_test(axelrod.Cooperator(), expected_actions=actions, seed=10) + self.versus_test(axl.Cooperator(), expected_actions=actions, seed=10) class TestFirstBySteinAndRapoport(TestPlayer): name = "First by Stein and Rapoport: 0.05: (D, D)" - player = axelrod.FirstBySteinAndRapoport + player = axl.FirstBySteinAndRapoport expected_classifier = { "memory_depth": float("inf"), "long_run_time": False, @@ -490,7 +490,7 @@ def test_strategy(self): # Our Player (SteinAndRapoport) vs Cooperator # After 15th round (pvalue < alpha) still plays TitForTat. # Note it always defects on the last two rounds. - opponent = axelrod.Cooperator() + opponent = axl.Cooperator() actions = [(C, C)] * 17 + [(D, C)] * 2 self.versus_test( opponent, expected_actions=actions, attrs={"opponent_is_random": False} @@ -506,7 +506,7 @@ def test_strategy(self): # SteinAndRapoport vs Defector # After 15th round (p-value < alpha) still plays TitForTat. - opponent = axelrod.Defector() + opponent = axl.Defector() actions = [(C, D)] * 4 + [(D, D)] * 15 self.versus_test( opponent, expected_actions=actions, attrs={"opponent_is_random": False} @@ -514,7 +514,7 @@ def test_strategy(self): # SteinAndRapoport vs Alternator # After 15th round (p-value > alpha) starts defecting. - opponent = axelrod.Alternator() + opponent = axl.Alternator() actions = [(C, C), (C, D), (C, C), (C, D)] # On 15th round carry out chi-square test. @@ -530,7 +530,7 @@ def test_strategy(self): # The test is carried out again every 15 rounds. # If the strategy alternates for the first 12 rounds and then cooperates # it is no longer recognised as random. - opponent = axelrod.MockPlayer([C, D] * 6 + [C] * 50) + opponent = axl.MockPlayer([C, D] * 6 + [C] * 50) actions = [(C, C), (C, D), (C, C), (C, D)] # On 15th round carry out chi-square test. @@ -549,7 +549,7 @@ def test_strategy(self): class TestFirstByTidemanAndChieruzzi(TestPlayer): name = "First by Tideman and Chieruzzi: (D, D)" - player = axelrod.FirstByTidemanAndChieruzzi + player = axl.FirstByTidemanAndChieruzzi expected_classifier = { "memory_depth": float("inf"), "stochastic": False, @@ -562,23 +562,23 @@ class TestFirstByTidemanAndChieruzzi(TestPlayer): def test_strategy(self): # Cooperator Test - opponent = axelrod.Cooperator() + opponent = axl.Cooperator() actions = [(C, C), (C, C), (D, C), (D, C)] self.versus_test(opponent, expected_actions=actions) # Cooperator Test does noot defect if game length is unknown - opponent = axelrod.Cooperator() + opponent = axl.Cooperator() actions = [(C, C), (C, C), (C, C), (C, C)] self.versus_test(opponent, expected_actions=actions, match_attributes={"length": float("inf")}) # Defector Test - opponent = axelrod.Defector() + opponent = axl.Defector() actions = [(C, D), (D, D), (D, D), (D, D)] self.versus_test(opponent, expected_actions=actions) # Test increasing retaliation - opponent = axelrod.MockPlayer([D, C]) + opponent = axl.MockPlayer([D, C]) actions = [ (C, D), (D, C), @@ -601,7 +601,7 @@ def test_strategy(self): }, ) - opponent = axelrod.Cycler("DDCDD") + opponent = axl.Cycler("DDCDD") actions = [ (C, D), (D, D), @@ -637,7 +637,7 @@ def test_strategy(self): ) # When the length is given this strategy will not give a fresh start - opponent = axelrod.Cycler("DDCDD") + opponent = axl.Cycler("DDCDD") actions = [ (C, D), (D, D), @@ -675,7 +675,7 @@ def test_strategy(self): ) # When the length is not given this strategy will give a fresh start. - opponent = axelrod.Cycler("DDCDD") + opponent = axl.Cycler("DDCDD") actions = [ (C, D), (D, D), @@ -717,7 +717,7 @@ def test_strategy(self): # Check standard deviation conditions. # The opponent is similar to the one above except the stddev condition # is not met, therefore no fresh start will be given. - opponent = axelrod.Cycler("DDCDDDDCDDCDCCC") + opponent = axl.Cycler("DDCDDDDCDDCDCCC") actions = [ (C, D), (D, D), @@ -751,14 +751,14 @@ def test_strategy(self): ) # Check the fresh start condition - opponent = axelrod.TitForTat() + opponent = axl.TitForTat() actions = [(C, C), (C, C), (D, C), (D, D)] self.versus_test( opponent, expected_actions=actions, attrs={"fresh_start": False} ) # check the fresh start condition: least 20 rounds since the last ‘fresh start’ - opponent = axelrod.Cycler("CCCCD") + opponent = axl.Cycler("CCCCD") actions = [ (C, C), (C, C), diff --git a/axelrod/tests/strategies/test_axelrod_second.py b/axelrod/tests/strategies/test_axelrod_second.py index 5a7415f35..7c9122d8d 100644 --- a/axelrod/tests/strategies/test_axelrod_second.py +++ b/axelrod/tests/strategies/test_axelrod_second.py @@ -2,17 +2,18 @@ import random -import axelrod +import axelrod as axl + import numpy as np from .test_player import TestPlayer -C, D = axelrod.Action.C, axelrod.Action.D +C, D = axl.Action.C, axl.Action.D class TestChampion(TestPlayer): name = "Second by Champion" - player = axelrod.SecondByChampion + player = axl.SecondByChampion expected_classifier = { "memory_depth": float("inf"), "stochastic": True, @@ -27,27 +28,27 @@ def test_strategy(self): # Cooperates for first 10 rounds actions = [(C, C), (C, D)] * 5 # Cooperate for ten rounds - self.versus_test(axelrod.Alternator(), expected_actions=actions) + self.versus_test(axl.Alternator(), expected_actions=actions) # Mirror partner for next phase actions += [(D, C), (C, D)] * 7 # Mirror opponent afterwards - self.versus_test(axelrod.Alternator(), expected_actions=actions) + self.versus_test(axl.Alternator(), expected_actions=actions) # Cooperate unless the opponent defected, has defected at least 40% of actions_1 = actions + [(D, C), (C, D), (C, C), (C, D)] - self.versus_test(axelrod.Alternator(), expected_actions=actions_1, seed=0) + self.versus_test(axl.Alternator(), expected_actions=actions_1, seed=0) actions_2 = actions + [(D, C), (C, D), (D, C), (C, D)] - self.versus_test(axelrod.Alternator(), expected_actions=actions_2, seed=1) + self.versus_test(axl.Alternator(), expected_actions=actions_2, seed=1) actions_3 = actions + [(D, C), (C, D), (C, C), (C, D)] - self.versus_test(axelrod.Alternator(), expected_actions=actions_3, seed=2) + self.versus_test(axl.Alternator(), expected_actions=actions_3, seed=2) class TestEatherley(TestPlayer): name = "Second by Eatherley" - player = axelrod.SecondByEatherley + player = axl.SecondByEatherley expected_classifier = { "memory_depth": float("inf"), "stochastic": True, @@ -61,22 +62,22 @@ class TestEatherley(TestPlayer): def test_strategy(self): # Test cooperate after opponent cooperates actions = [(C, C)] * 5 - self.versus_test(axelrod.Cooperator(), expected_actions=actions) + self.versus_test(axl.Cooperator(), expected_actions=actions) # If opponent only defects then probability of cooperating is 0. actions = [(C, D), (D, D), (D, D), (D, D), (D, D)] - self.versus_test(axelrod.Defector(), expected_actions=actions) + self.versus_test(axl.Defector(), expected_actions=actions) # Stochastic response to defect actions = [(C, C), (C, D), (D, C), (C, D), (D, C)] - self.versus_test(axelrod.Alternator(), expected_actions=actions, seed=0) + self.versus_test(axl.Alternator(), expected_actions=actions, seed=0) actions = [(C, C), (C, D), (C, C), (C, D), (D, C)] - self.versus_test(axelrod.Alternator(), expected_actions=actions, seed=1) + self.versus_test(axl.Alternator(), expected_actions=actions, seed=1) - opponent = axelrod.MockPlayer(actions=[D, C, C, D]) + opponent = axl.MockPlayer(actions=[D, C, C, D]) actions = [(C, D), (D, C), (C, C), (C, D), (C, D)] self.versus_test(opponent, expected_actions=actions, seed=8) - opponent = axelrod.MockPlayer(actions=[D, C, C, D]) + opponent = axl.MockPlayer(actions=[D, C, C, D]) actions = [(C, D), (D, C), (C, C), (C, D), (D, D)] self.versus_test(opponent, expected_actions=actions, seed=2) @@ -84,7 +85,7 @@ def test_strategy(self): class TestTester(TestPlayer): name = "Second by Tester" - player = axelrod.SecondByTester + player = axl.SecondByTester expected_classifier = { "memory_depth": float("inf"), "stochastic": False, @@ -99,17 +100,17 @@ def test_strategy(self): # Alternate after 3rd round if opponent only cooperates actions = [(D, C)] + [(C, C), (C, C)] + [(D, C), (C, C)] * 4 self.versus_test( - axelrod.Cooperator(), expected_actions=actions, attrs={"is_TFT": False} + axl.Cooperator(), expected_actions=actions, attrs={"is_TFT": False} ) # Cooperate after initial defection and become TfT actions = [(D, C), (C, D), (C, C)] self.versus_test( - axelrod.Alternator(), expected_actions=actions, attrs={"is_TFT": True} + axl.Alternator(), expected_actions=actions, attrs={"is_TFT": True} ) # Now play TfT - opponent = axelrod.MockPlayer(actions=[C, D, C, D, D, C]) + opponent = axl.MockPlayer(actions=[C, D, C, D, D, C]) actions = [(D, C), (C, D), (C, C), (C, D), (D, D), (D, C), (C, C)] self.versus_test(opponent, expected_actions=actions, attrs={"is_TFT": True}) @@ -117,7 +118,7 @@ def test_strategy(self): class TestGladstein(TestPlayer): name = "Second by Gladstein" - player = axelrod.SecondByGladstein + player = axl.SecondByGladstein expected_classifier = { "memory_depth": float("inf"), "stochastic": False, @@ -132,27 +133,27 @@ def test_strategy(self): # Cooperates and begins to play TFT when Alternator defects actions = [(D, C), (C, D), (C, C), (C, D), (D, C)] self.versus_test( - axelrod.Alternator(), expected_actions=actions, attrs={"patsy": False} + axl.Alternator(), expected_actions=actions, attrs={"patsy": False} ) # Cooperation ratio will always be less than 0.5 actions = [(D, C), (C, C), (C, C), (D, C), (C, C)] self.versus_test( - axelrod.Cooperator(), expected_actions=actions, attrs={"patsy": True} + axl.Cooperator(), expected_actions=actions, attrs={"patsy": True} ) # Apologizes immediately and plays TFT actions = [(D, D), (C, D), (D, D), (D, D), (D, D)] self.versus_test( - axelrod.Defector(), expected_actions=actions, attrs={"patsy": False} + axl.Defector(), expected_actions=actions, attrs={"patsy": False} ) # Ratio is 1/3 when MockPlayer defected for the first time. - opponent = axelrod.MockPlayer(actions=[C, C, C, D, D]) + opponent = axl.MockPlayer(actions=[C, C, C, D, D]) actions = [(D, C), (C, C), (C, C), (D, D), (C, D)] self.versus_test(opponent, expected_actions=actions, attrs={"patsy": False}) - opponent = axelrod.AntiTitForTat() + opponent = axl.AntiTitForTat() actions = [(D, C), (C, C), (C, D), (C, D), (D, D)] self.versus_test(opponent, expected_actions=actions, attrs={"patsy": False}) @@ -160,7 +161,7 @@ def test_strategy(self): class TestTranquilizer(TestPlayer): name = "Second by Tranquilizer" - player = axelrod.SecondByTranquilizer + player = axl.SecondByTranquilizer expected_classifier = { "memory_depth": float("inf"), "stochastic": True, @@ -175,7 +176,7 @@ class TestTranquilizer(TestPlayer): def test_init(self): - player = axelrod.SecondByTranquilizer() + player = axl.SecondByTranquilizer() self.assertEqual(player.num_turns_after_good_defection, 0) self.assertEqual(player.opponent_consecutive_defections, 0) @@ -186,7 +187,7 @@ def test_init(self): def test_strategy(self): - opponent = axelrod.Bully() + opponent = axl.Bully() actions = [(C, D), (D, D), (D, C), (C, C), (C, D), (D, D), (D, C), (C, C)] expected_attrs = { "num_turns_after_good_defection": 0, @@ -199,7 +200,7 @@ def test_strategy(self): # Tests whether TitForTat is played given score is below 1.75 - opponent = axelrod.Defector() + opponent = axl.Defector() actions = [(C, D)] + [(D, D)] * 20 expected_attrs = { "num_turns_after_good_defection": 0, @@ -210,7 +211,7 @@ def test_strategy(self): } self.versus_test(opponent, expected_actions=actions, attrs=expected_attrs) - opponent = axelrod.MockPlayer([C] * 2 + [D] * 8 + [C] * 4) + opponent = axl.MockPlayer([C] * 2 + [D] * 8 + [C] * 4) actions = [(C, C), (C, C)] + [(C, D)] + [(D, D)] * 7 + [(D, C)] + [(C, C)] * 3 expected_attrs = { "num_turns_after_good_defection": 0, @@ -223,7 +224,7 @@ def test_strategy(self): # If score is between 1.75 and 2.25, may cooperate or defect - opponent = axelrod.MockPlayer(actions=[D] * 3 + [C] * 4 + [D] * 2) + opponent = axl.MockPlayer(actions=[D] * 3 + [C] * 4 + [D] * 2) actions = [(C, D)] + [(D, D)] * 2 + [(D, C)] + [(C, C)] * 3 + [(C, D)] actions += [(C, D)] # <-- Random expected_attrs = { @@ -237,7 +238,7 @@ def test_strategy(self): opponent, expected_actions=actions, seed=0, attrs=expected_attrs ) - opponent = axelrod.MockPlayer(actions=[D] * 3 + [C] * 4 + [D] * 2) + opponent = axl.MockPlayer(actions=[D] * 3 + [C] * 4 + [D] * 2) actions = [(C, D)] + [(D, D)] * 2 + [(D, C)] + [(C, C)] * 3 + [(C, D)] actions += [(D, D)] # <-- Random expected_attrs = { @@ -254,7 +255,7 @@ def test_strategy(self): """If score is greater than 2.25 either cooperate or defect, if turn number <= 5; cooperate""" - opponent = axelrod.MockPlayer(actions=[C] * 5) + opponent = axl.MockPlayer(actions=[C] * 5) actions = [(C, C)] * 5 expected_attrs = { "num_turns_after_good_defection": 0, @@ -267,7 +268,7 @@ def test_strategy(self): opponent, expected_actions=actions, seed=1, attrs=expected_attrs ) - opponent = axelrod.MockPlayer(actions=[C] * 5) + opponent = axl.MockPlayer(actions=[C] * 5) actions = [(C, C)] * 4 + [(D, C)] expected_attrs = { "num_turns_after_good_defection": 1, @@ -283,7 +284,7 @@ def test_strategy(self): """ Given score per turn is greater than 2.25, Tranquilizer will never defect twice in a row""" - opponent = axelrod.MockPlayer(actions=[C] * 6) + opponent = axl.MockPlayer(actions=[C] * 6) actions = [(C, C)] * 4 + [(D, C), (C, C)] expected_attrs = { "num_turns_after_good_defection": 2, @@ -298,7 +299,7 @@ def test_strategy(self): # Tests cooperation after update_state - opponent = axelrod.MockPlayer(actions=[C] * 5) + opponent = axl.MockPlayer(actions=[C] * 5) actions = [(C, C)] * 4 + [(D, C)] + [(C, C)] expected_attrs = { "num_turns_after_good_defection": 2, @@ -313,7 +314,7 @@ def test_strategy(self): # Ensures FD1 values are calculated - opponent = axelrod.MockPlayer(actions=[C] * 6) + opponent = axl.MockPlayer(actions=[C] * 6) actions = [(C, C)] * 4 + [(D, C), (C, C)] expected_attrs = { "num_turns_after_good_defection": 2, @@ -328,7 +329,7 @@ def test_strategy(self): # Ensures FD2 values are calculated - opponent = axelrod.MockPlayer(actions=[C] * 6) + opponent = axl.MockPlayer(actions=[C] * 6) actions = [(C, C)] * 4 + [(D, C)] + [(C, C)] * 2 expected_attrs = { "num_turns_after_good_defection": 0, @@ -343,7 +344,7 @@ def test_strategy(self): # Ensures scores are being counted - opponent = axelrod.Defector() + opponent = axl.Defector() actions = [(C, D)] + [(D, D)] * 19 expected_attrs = { "num_turns_after_good_defection": 0, @@ -358,7 +359,7 @@ def test_strategy(self): class TestGrofman(TestPlayer): name = "Second by Grofman" - player = axelrod.SecondByGrofman + player = axl.SecondByGrofman expected_classifier = { "memory_depth": 8, "stochastic": False, @@ -372,43 +373,43 @@ class TestGrofman(TestPlayer): def test_strategy(self): # Cooperate for the first two rounds actions = [(C, C), (C, C)] - self.versus_test(axelrod.Cooperator(), expected_actions=actions) + self.versus_test(axl.Cooperator(), expected_actions=actions) # Cooperate for the first two rounds, then play tit for tat for 3-7 actions = [(C, C), (C, D), (D, C), (C, D), (D, C), (C, D), (D, C)] - self.versus_test(axelrod.Alternator(), expected_actions=actions) + self.versus_test(axl.Alternator(), expected_actions=actions) # Demonstrate Grofman Logic # Own previous move was C, opponent defected less than 3 times in last 8 moregrofman_actions = [C] * 7 + [C] opponent_actions = [C] * 6 + [D] * 2 - opponent = axelrod.MockPlayer(actions=opponent_actions) + opponent = axl.MockPlayer(actions=opponent_actions) actions = list(zip(moregrofman_actions, opponent_actions)) self.versus_test(opponent, expected_actions=actions) # Own previous move was C, opponent defected 3 or more times in last 8 moregrofman_actions = ([C] * 3 + [D] * 3 + [C]) + [D] opponent_actions = ([C] * 2 + [D] * 3 + [C] * 2) + [D] - opponent = axelrod.MockPlayer(actions=opponent_actions) + opponent = axl.MockPlayer(actions=opponent_actions) actions = list(zip(moregrofman_actions, opponent_actions)) self.versus_test(opponent, expected_actions=actions) # Own previous move was D, opponent defected once or less in last 8 moregrofman_actions = ([C] * 6 + [D]) + [C] opponent_actions = ([C] * 5 + [D] * 1 + [C]) + [D] - opponent = axelrod.MockPlayer(actions=opponent_actions) + opponent = axl.MockPlayer(actions=opponent_actions) actions = list(zip(moregrofman_actions, opponent_actions)) self.versus_test(opponent, expected_actions=actions) # Own previous move was D, opponent defected more than once in last 8 moregrofman_actions = ([C] * 2 + [D] * 5) + [D] opponent_actions = ([D] * 7) + [D] - opponent = axelrod.MockPlayer(actions=opponent_actions) + opponent = axl.MockPlayer(actions=opponent_actions) actions = list(zip(moregrofman_actions, opponent_actions)) self.versus_test(opponent, expected_actions=actions) # Test to make sure logic matches Fortran (discrepancy found 8/23/2017) - opponent = axelrod.AntiTitForTat() + opponent = axl.AntiTitForTat() # Actions come from a match run by Axelrod Fortran using Player('k86r') actions = [ (C, C), @@ -430,7 +431,7 @@ def test_strategy(self): self.versus_test(opponent, expected_actions=actions) # Test to match the Fortran implementation for 30 rounds - opponent = axelrod.AntiTitForTat() + opponent = axl.AntiTitForTat() actions = [ (C, C), (C, D), @@ -466,7 +467,7 @@ def test_strategy(self): self.versus_test(opponent, expected_actions=actions) # Test to match the Fortran implementation for 60 rounds - opponent = axelrod.AntiTitForTat() + opponent = axl.AntiTitForTat() actions = [ (C, C), (C, D), @@ -534,7 +535,7 @@ def test_strategy(self): class TestKluepfel(TestPlayer): name = "Second by Kluepfel" - player = axelrod.SecondByKluepfel + player = axl.SecondByKluepfel expected_classifier = { "memory_depth": float("inf"), "stochastic": True, @@ -547,7 +548,7 @@ class TestKluepfel(TestPlayer): def test_strategy(self): actions = [(C, C)] * 100 # Cooperate forever - self.versus_test(axelrod.Cooperator(), expected_actions=actions) + self.versus_test(axl.Cooperator(), expected_actions=actions) # Since never two in a row, will respond in kind with 70% if # coop and 60% otherwise, after first couple @@ -560,13 +561,13 @@ def test_strategy(self): (C, C), (C, D), ] - self.versus_test(axelrod.Alternator(), expected_actions=actions, seed=1) + self.versus_test(axl.Alternator(), expected_actions=actions, seed=1) actions = [(C, C), (C, D), (C, C), (D, D), (D, C), (C, D)] - self.versus_test(axelrod.Alternator(), expected_actions=actions, seed=2) + self.versus_test(axl.Alternator(), expected_actions=actions, seed=2) actions = [(C, C), (C, D), (D, C), (C, D), (D, C), (C, D), (C, C)] - self.versus_test(axelrod.Alternator(), expected_actions=actions, seed=3) + self.versus_test(axl.Alternator(), expected_actions=actions, seed=3) # Now we have to test the detect-random logic, which doesn't pick up # until after 26 turns. So we need a big sample. @@ -623,12 +624,12 @@ def test_strategy(self): (D, C), (D, D), ] - self.versus_test(axelrod.Random(0.5), expected_actions=actions, seed=10) + self.versus_test(axl.Random(0.5), expected_actions=actions, seed=10) class TestBorufsen(TestPlayer): name = "Second by Borufsen" - player = axelrod.SecondByBorufsen + player = axl.SecondByBorufsen expected_classifier = { "memory_depth": float("inf"), "stochastic": False, @@ -641,13 +642,13 @@ class TestBorufsen(TestPlayer): def test_strategy(self): actions = [(C, C)] * 100 # Cooperate forever - self.versus_test(axelrod.Cooperator(), expected_actions=actions) + self.versus_test(axl.Cooperator(), expected_actions=actions) # Tries to cooperate every third time until detecting defective actions = ( [(C, D), (D, D), (D, D), (D, D)] * 6 + [(C, D), (D, D)] + [(D, D)] * 100 ) - self.versus_test(axelrod.Defector(), expected_actions=actions) + self.versus_test(axl.Defector(), expected_actions=actions) # Alternates with additional coop, every sixth turn # Won't be labeled as random, since 2/3 of opponent's C follow @@ -658,7 +659,7 @@ def test_strategy(self): # marked as echoes, and the third isn't marked that way until the # fourth turn. actions = [(C, C), (C, D), (D, C), (C, D), (D, C), (C, D)] * 20 - self.versus_test(axelrod.Alternator(), expected_actions=actions) + self.versus_test(axl.Alternator(), expected_actions=actions) # Basically does tit-for-tat against Win-Shift, Lose-Stay D # After 26 turns, will detect random since half of opponent's C follow @@ -673,12 +674,12 @@ def test_strategy(self): + [(C, C), (C, D), (D, C)] * 8 + [(D, C)] * 25 ) - self.versus_test(axelrod.WinShiftLoseStay(D), expected_actions=actions) + self.versus_test(axl.WinShiftLoseStay(D), expected_actions=actions) class TestCave(TestPlayer): name = "Second by Cave" - player = axelrod.SecondByCave + player = axl.SecondByCave expected_classifier = { "memory_depth": float("inf"), "stochastic": True, @@ -691,7 +692,7 @@ class TestCave(TestPlayer): def test_strategy(self): actions = [(C, C)] * 100 - self.versus_test(axelrod.Cooperator(), expected_actions=actions) + self.versus_test(axl.Cooperator(), expected_actions=actions) # It will take until turn 18 to respond decide to repond D->D actions = [(C, D)] @@ -715,12 +716,12 @@ def test_strategy(self): (C, D), ] # Randomly choose actions += [(D, D)] * 30 # Defect - self.versus_test(axelrod.Defector(), expected_actions=actions, seed=1) + self.versus_test(axl.Defector(), expected_actions=actions, seed=1) # Highly-defective opponent # It will take until turn 20 to respond decide to repond D to C opponent_actions = [D] * 17 + [C, C, C, C] - almost_defector = axelrod.MockPlayer(actions=opponent_actions) + almost_defector = axl.MockPlayer(actions=opponent_actions) actions = [(C, D)] actions += [ @@ -791,12 +792,12 @@ def test_strategy(self): (D, C), ] # 17 D have come, so tit for tat for a while actions += [(D, D), (D, C)] * 100 # Random finally detected - self.versus_test(axelrod.Alternator(), expected_actions=actions, seed=2) + self.versus_test(axl.Alternator(), expected_actions=actions, seed=2) class TestWmAdams(TestPlayer): name = "Second by WmAdams" - player = axelrod.SecondByWmAdams + player = axl.SecondByWmAdams expected_classifier = { "memory_depth": float("inf"), "stochastic": True, @@ -809,11 +810,11 @@ class TestWmAdams(TestPlayer): def test_strategy(self): actions = [(C, C)] * 100 # Cooperate forever - self.versus_test(axelrod.Cooperator(), expected_actions=actions) + self.versus_test(axl.Cooperator(), expected_actions=actions) # Will ignore the first four defects opponent_actions = [D] * 4 + [C] * 100 - defect_four = axelrod.MockPlayer(actions=opponent_actions) + defect_four = axl.MockPlayer(actions=opponent_actions) actions = [(C, D)] * 4 + [(C, C)] * 100 self.versus_test(defect_four, expected_actions=actions) @@ -835,7 +836,7 @@ def test_strategy(self): (D, D), (D, D), ] - self.versus_test(axelrod.Defector(), expected_actions=actions, seed=1) + self.versus_test(axl.Defector(), expected_actions=actions, seed=1) actions = [ (C, D), (C, D), @@ -854,11 +855,11 @@ def test_strategy(self): (D, D), (D, D), ] - self.versus_test(axelrod.Defector(), expected_actions=actions, seed=2) + self.versus_test(axl.Defector(), expected_actions=actions, seed=2) # After responding to the 11th D (counted as 10 D), just start cooperating opponent_actions = [D] * 11 + [C] * 100 - changed_man = axelrod.MockPlayer(actions=opponent_actions) + changed_man = axl.MockPlayer(actions=opponent_actions) actions = [ (C, D), (C, D), @@ -879,7 +880,7 @@ def test_strategy(self): class TestGraaskampKatzen(TestPlayer): name = "Second by GraaskampKatzen" - player = axelrod.SecondByGraaskampKatzen + player = axl.SecondByGraaskampKatzen expected_classifier = { "memory_depth": float("inf"), "stochastic": False, @@ -892,11 +893,11 @@ class TestGraaskampKatzen(TestPlayer): def test_strategy(self): actions = [(C, C)] * 100 # Cooperate forever - self.versus_test(axelrod.Cooperator(), expected_actions=actions) + self.versus_test(axl.Cooperator(), expected_actions=actions) # GK does not great against opponent_actions = [C, D, D] * 100 - GK_Foil = axelrod.MockPlayer(actions=opponent_actions) + GK_Foil = axl.MockPlayer(actions=opponent_actions) actions = [(C, C), (C, D), (D, D)] actions += [(D, C), (C, D), (D, D)] * 2 actions += [(D, C)] @@ -905,7 +906,7 @@ def test_strategy(self): # Fail on second checkpoint opponent_actions = [C] * 10 + [C, D, D] * 100 - Delayed_GK_Foil = axelrod.MockPlayer(actions=opponent_actions) + Delayed_GK_Foil = axl.MockPlayer(actions=opponent_actions) actions = [(C, C)] * 10 actions += [(C, C), (C, D), (D, D)] actions += [(D, C), (C, D), (D, D)] * 2 @@ -916,7 +917,7 @@ def test_strategy(self): class TestWeiner(TestPlayer): name = "Second by Weiner" - player = axelrod.SecondByWeiner + player = axl.SecondByWeiner expected_classifier = { "memory_depth": float("inf"), "stochastic": False, @@ -929,7 +930,7 @@ class TestWeiner(TestPlayer): def test_strategy(self): actions = [(C, C)] * 100 # Cooperate forever - self.versus_test(axelrod.Cooperator(), expected_actions=actions) + self.versus_test(axl.Cooperator(), expected_actions=actions) actions = [(C, C)] actions += [(C, D), (D, C)] # Tit-for-Tat @@ -946,11 +947,11 @@ def test_strategy(self): # This is the 5th opponent defect, won't be counted for 2 turns actions += [(D, C)] # Tit-for-Tat. actions += [(D, D), (D, C)] * 100 # Defect now on. - self.versus_test(axelrod.Alternator(), expected_actions=actions) + self.versus_test(axl.Alternator(), expected_actions=actions) # Build an opponent that will cause a wasted flag. opponent_actions = [C, D, C, C, C, C, D, D] - Flag_Waster_1 = axelrod.MockPlayer(actions=opponent_actions) + Flag_Waster_1 = axl.MockPlayer(actions=opponent_actions) actions = [(C, C), (C, D), (D, C)] actions += [(C, C)] # Raise flag, like in Alternator actions += [(C, C)] # Use flag, but don't change outcome @@ -961,7 +962,7 @@ def test_strategy(self): # Demonstrate that grudge is not incremented on wasted flag. opponent_actions = [C, D, C, C, C, C, D, C, D, C] - Flag_Waster_2 = axelrod.MockPlayer(actions=opponent_actions) + Flag_Waster_2 = axl.MockPlayer(actions=opponent_actions) actions = [(C, C), (C, D), (D, C)] actions += [(C, C)] # Raise flag, like in Alternator actions += [(C, C)] # Use flag, but don't change outcome @@ -972,7 +973,7 @@ def test_strategy(self): # Show grudge passing over time opponent_actions = [C, D, C, D, C] + [C] * 11 + [C, D, C, D, C] - Time_Passer = axelrod.MockPlayer(actions=opponent_actions) + Time_Passer = axl.MockPlayer(actions=opponent_actions) actions = [(C, C), (C, D), (D, C)] actions += [(C, D)] # Raise flag actions += [(C, C)] # Use flag to change outcome @@ -985,7 +986,7 @@ def test_strategy(self): class TestHarrington(TestPlayer): name = "Second by Harrington" - player = axelrod.SecondByHarrington + player = axl.SecondByHarrington expected_classifier = { "memory_depth": float("inf"), "stochastic": True, @@ -1000,7 +1001,7 @@ def test_strategy(self): # Build an opponent that will cooperate the first 36 turns and # defect on the 37th turn opponent_actions = [C] * 36 + [D] + [C] * 100 - Defect37 = axelrod.MockPlayer(actions=opponent_actions) + Defect37 = axl.MockPlayer(actions=opponent_actions) # Activate the Fair-weather flag actions = [(C, C)] * 36 + [(D, D)] + [(C, C)] * 100 self.versus_test( @@ -1010,7 +1011,7 @@ def test_strategy(self): # Defect on 37th turn to activate Fair-weather, then later defect to # exit Fair-weather opponent_actions = [C] * 36 + [D] + [C] * 100 + [D] + [C] * 4 - Defect37_big = axelrod.MockPlayer(actions=opponent_actions) + Defect37_big = axl.MockPlayer(actions=opponent_actions) actions = [(C, C)] * 36 + [(D, D)] + [(C, C)] * 100 actions += [(C, D)] # Immediately exit Fair-weather @@ -1028,7 +1029,7 @@ def test_strategy(self): # Opponent defects on 1st turn opponent_actions = [D] + [C] * 46 - Defect1 = axelrod.MockPlayer(actions=opponent_actions) + Defect1 = axl.MockPlayer(actions=opponent_actions) # Tit-for-Tat on the first, but no streaks, no Fair-weather flag. actions = [(C, D), (D, C)] + [(C, C)] * 34 + [(D, C)] # Two cooperations scheduled after the 37-turn defection @@ -1042,7 +1043,7 @@ def test_strategy(self): # Defection on turn 37 by opponent doesn't have an effect here opponent_actions = [D] + [C] * 35 + [D] + [C] * 10 - Defect1_37 = axelrod.MockPlayer(actions=opponent_actions) + Defect1_37 = axl.MockPlayer(actions=opponent_actions) actions = [(C, D), (D, C)] + [(C, C)] * 34 + [(D, D)] actions += [(C, C)] * 2 actions += [(C, C)] * 2 @@ -1052,7 +1053,7 @@ def test_strategy(self): # However a defect on turn 38 would be considered a burn. opponent_actions = [D] + [C] * 36 + [D] + [C] * 9 - Defect1_38 = axelrod.MockPlayer(actions=opponent_actions) + Defect1_38 = axl.MockPlayer(actions=opponent_actions) # Tit-for-Tat on the first, but no streaks, no Fair-weather flag. actions = [(C, D), (D, C)] + [(C, C)] * 34 + [(D, C)] # Two cooperations scheduled after the 37-turn defection @@ -1082,11 +1083,11 @@ def test_strategy(self): # Repeat. Notice that the last turn is the 37th move, but we do not # defect. actions += [(C, D), (D, C), (C, D), (D, C), (C, D), (C, C)] - self.versus_test(axelrod.Alternator(), expected_actions=actions) + self.versus_test(axl.Alternator(), expected_actions=actions) # Test for parity limit shortening. opponent_actions = [D, C] * 1000 - AsyncAlternator = axelrod.MockPlayer(actions=opponent_actions) + AsyncAlternator = axl.MockPlayer(actions=opponent_actions) actions = [(C, D), (D, C), (C, D), (D, C), (C, D), (C, C)] * 6 # Defect on 37th move actions += [(D, D)] @@ -1115,7 +1116,7 @@ def test_strategy(self): # doesn't reset. This logic comes before parity streaks or the turn- # based logic. self.versus_test( - axelrod.Defector(), + axl.Defector(), expected_actions=actions, attrs={"recorded_defects": 119}, ) @@ -1156,7 +1157,7 @@ def test_strategy(self): expected_actions += [(D, C)] random.seed(10) player = self.player() - match = axelrod.Match((player, axelrod.Random()), turns=len(expected_actions)) + match = axl.Match((player, axl.Random()), turns=len(expected_actions)) # The history matrix will be [[0, 2], [5, 6], [3, 6], [4, 2]] actions = match.play() self.assertEqual(actions, expected_actions) @@ -1198,7 +1199,7 @@ def test_strategy(self): C, ] opponent_actions += [D] * 16 - Rand_Then_Def = axelrod.MockPlayer(actions=opponent_actions) + Rand_Then_Def = axl.MockPlayer(actions=opponent_actions) actions = [ (C, D), (D, C), @@ -1245,7 +1246,7 @@ def test_strategy(self): class TestTidemanAndChieruzzi(TestPlayer): name = "Second by Tideman and Chieruzzi" - player = axelrod.SecondByTidemanAndChieruzzi + player = axl.SecondByTidemanAndChieruzzi expected_classifier = { "memory_depth": float("inf"), "stochastic": False, @@ -1258,24 +1259,24 @@ class TestTidemanAndChieruzzi(TestPlayer): def test_strategy(self): actions = [(C, C)] * 100 - self.versus_test(axelrod.Cooperator(), expected_actions=actions) + self.versus_test(axl.Cooperator(), expected_actions=actions) actions = [(C, D)] + [(D, D)] * 8 self.versus_test( - axelrod.Defector(), expected_actions=actions, attrs={"score_to_beat_inc": 5} + axl.Defector(), expected_actions=actions, attrs={"score_to_beat_inc": 5} ) actions = [(C, D)] + [(D, D)] * 8 # On tenth turn, try a fresh start actions += [(C, D), (C, D)] + [(D, D)] * 2 self.versus_test( - axelrod.Defector(), expected_actions=actions, attrs={"last_fresh_start": 11} + axl.Defector(), expected_actions=actions, attrs={"last_fresh_start": 11} ) actions = [(C, C), (C, D)] # Scores and score_to_beat variables are a turn behind self.versus_test( - axelrod.Alternator(), + axl.Alternator(), expected_actions=actions, attrs={ "current_score": 3, @@ -1286,7 +1287,7 @@ def test_strategy(self): ) actions += [(D, C), (C, D)] self.versus_test( - axelrod.Alternator(), + axl.Alternator(), expected_actions=actions, attrs={ "current_score": 8, @@ -1297,7 +1298,7 @@ def test_strategy(self): ) actions += [(D, C), (D, D)] self.versus_test( - axelrod.Alternator(), + axl.Alternator(), expected_actions=actions, attrs={ "current_score": 13, @@ -1308,7 +1309,7 @@ def test_strategy(self): ) actions += [(D, C), (D, D)] self.versus_test( - axelrod.Alternator(), + axl.Alternator(), expected_actions=actions, attrs={ "current_score": 19, @@ -1319,7 +1320,7 @@ def test_strategy(self): ) actions += [(D, C), (D, D)] self.versus_test( - axelrod.Alternator(), + axl.Alternator(), expected_actions=actions, attrs={ "current_score": 25, @@ -1332,7 +1333,7 @@ def test_strategy(self): # Build an opponent who will cause us to consider a Fresh Start, but # will fail the binomial test. opponent_actions = [C] * 5 + [D] * 5 - C5D5_player = axelrod.MockPlayer(actions=opponent_actions) + C5D5_player = axl.MockPlayer(actions=opponent_actions) actions = [(C, C)] * 5 + [(C, D)] + [(D, D)] * 3 actions += [(D, D)] # No Defection here means no Fresh Start. self.versus_test(C5D5_player, expected_actions=actions) @@ -1340,7 +1341,7 @@ def test_strategy(self): class TestGetzler(TestPlayer): name = "Second by Getzler" - player = axelrod.SecondByGetzler + player = axl.SecondByGetzler expected_classifier = { "memory_depth": float("inf"), "stochastic": True, @@ -1353,11 +1354,11 @@ class TestGetzler(TestPlayer): def test_strategy(self): actions = [(C, C)] * 100 - self.versus_test(axelrod.Cooperator(), expected_actions=actions) + self.versus_test(axl.Cooperator(), expected_actions=actions) actions = [(C, D), (C, D), (D, D), (D, D), (D, D)] self.versus_test( - axelrod.Defector(), + axl.Defector(), expected_actions=actions, seed=1, attrs={"flack": 15.0 / 16.0}, @@ -1365,7 +1366,7 @@ def test_strategy(self): actions = [(C, C), (C, D), (C, C), (C, D), (D, C), (C, D)] self.versus_test( - axelrod.Alternator(), + axl.Alternator(), expected_actions=actions, seed=4, attrs={"flack": 5.0 / 16.0}, @@ -1374,7 +1375,7 @@ def test_strategy(self): class TestLeyvraz(TestPlayer): name = "Second by Leyvraz" - player = axelrod.SecondByLeyvraz + player = axl.SecondByLeyvraz expected_classifier = { "memory_depth": 3, "stochastic": True, @@ -1387,12 +1388,12 @@ class TestLeyvraz(TestPlayer): def test_strategy(self): actions = [(C, C)] * 100 - self.versus_test(axelrod.Cooperator(), expected_actions=actions) + self.versus_test(axl.Cooperator(), expected_actions=actions) actions = [(C, D), (C, D), (D, D), (D, D)] - self.versus_test(axelrod.Defector(), expected_actions=actions, seed=1) + self.versus_test(axl.Defector(), expected_actions=actions, seed=1) actions = [(C, D), (D, D), (D, D), (C, D)] - self.versus_test(axelrod.Defector(), expected_actions=actions, seed=2) + self.versus_test(axl.Defector(), expected_actions=actions, seed=2) actions = [ (C, D), @@ -1406,18 +1407,18 @@ def test_strategy(self): (C, D), ] self.versus_test( - axelrod.SuspiciousTitForTat(), expected_actions=actions, seed=1 + axl.SuspiciousTitForTat(), expected_actions=actions, seed=1 ) actions = [(C, C), (C, D), (D, C)] + [(D, D), (C, C)] * 3 - self.versus_test(axelrod.Alternator(), expected_actions=actions, seed=2) + self.versus_test(axl.Alternator(), expected_actions=actions, seed=2) actions = [(C, C), (C, D), (C, C)] + [(D, D), (C, C)] * 3 - self.versus_test(axelrod.Alternator(), expected_actions=actions, seed=3) + self.versus_test(axl.Alternator(), expected_actions=actions, seed=3) class TestWhite(TestPlayer): name = "Second by White" - player = axelrod.SecondByWhite + player = axl.SecondByWhite expected_classifier = { "memory_depth": float("inf"), "stochastic": False, @@ -1430,10 +1431,10 @@ class TestWhite(TestPlayer): def test_strategy(self): actions = [(C, C)] * 30 - self.versus_test(axelrod.Cooperator(), expected_actions=actions) + self.versus_test(axl.Cooperator(), expected_actions=actions) actions = [(C, D)] * 10 + [(D, D)] * 20 - self.versus_test(axelrod.Defector(), expected_actions=actions) + self.versus_test(axl.Defector(), expected_actions=actions) actions = [ (C, D), @@ -1457,7 +1458,7 @@ def test_strategy(self): (D, D), (D, C), ] - self.versus_test(axelrod.Random(0.5), expected_actions=actions, seed=6) + self.versus_test(axl.Random(0.5), expected_actions=actions, seed=6) actions = [ (C, C), (C, D), @@ -1480,12 +1481,12 @@ def test_strategy(self): (C, C), (C, D), ] - self.versus_test(axelrod.Random(0.5), expected_actions=actions, seed=12) + self.versus_test(axl.Random(0.5), expected_actions=actions, seed=12) class TestBlack(TestPlayer): name = "Second by Black" - player = axelrod.SecondByBlack + player = axl.SecondByBlack expected_classifier = { "memory_depth": 5, "stochastic": True, @@ -1498,7 +1499,7 @@ class TestBlack(TestPlayer): def test_strategy(self): actions = [(C, C)] * 30 - self.versus_test(axelrod.Cooperator(), expected_actions=actions) + self.versus_test(axl.Cooperator(), expected_actions=actions) actions = [(C, D)] * 5 actions += [ @@ -1513,7 +1514,7 @@ def test_strategy(self): (D, D), (C, D), ] - self.versus_test(axelrod.Defector(), expected_actions=actions, seed=1) + self.versus_test(axl.Defector(), expected_actions=actions, seed=1) actions = [(C, D)] * 5 actions += [ @@ -1528,12 +1529,12 @@ def test_strategy(self): (D, D), (D, D), ] - self.versus_test(axelrod.Defector(), expected_actions=actions, seed=15) + self.versus_test(axl.Defector(), expected_actions=actions, seed=15) class TestRichardHufford(TestPlayer): name = "Second by RichardHufford" - player = axelrod.SecondByRichardHufford + player = axl.SecondByRichardHufford expected_classifier = { "memory_depth": float("inf"), "stochastic": False, @@ -1547,7 +1548,7 @@ class TestRichardHufford(TestPlayer): def test_strategy(self): actions = [(C, C)] * 19 + [(D, C), (C, C), (C, C)] self.versus_test( - axelrod.Cooperator(), expected_actions=actions, attrs={"streak_needed": 14} + axl.Cooperator(), expected_actions=actions, attrs={"streak_needed": 14} ) actions = [(C, C)] * 19 + [(D, C), (C, C)] @@ -1556,11 +1557,11 @@ def test_strategy(self): ] # This is the first Cooperation that gets counted on the new streak actions += [(C, C)] * 13 + [(D, C), (C, C), (C, C)] self.versus_test( - axelrod.Cooperator(), expected_actions=actions, attrs={"streak_needed": 11} + axl.Cooperator(), expected_actions=actions, attrs={"streak_needed": 11} ) opponent_actions = [C] * 20 + [D] - BoredCooperator = axelrod.MockPlayer(actions=opponent_actions) + BoredCooperator = axl.MockPlayer(actions=opponent_actions) actions = [(C, C)] * 19 + [(D, C), (C, D), (C, C)] self.versus_test( BoredCooperator, expected_actions=actions, attrs={"streak_needed": 31} @@ -1576,7 +1577,7 @@ def test_strategy(self): actions += [(D, D)] # Three of last four are disagreements. actions += [(D, D)] # Now there are 5/9 disagreements, so Defect. self.versus_test( - axelrod.WinShiftLoseStay(), + axl.WinShiftLoseStay(), expected_actions=actions, attrs={"num_agreements": 5}, ) @@ -1584,7 +1585,7 @@ def test_strategy(self): class TestYamachi(TestPlayer): name = "Second by Yamachi" - player = axelrod.SecondByYamachi + player = axl.SecondByYamachi expected_classifier = { "memory_depth": float("inf"), "stochastic": False, @@ -1597,7 +1598,7 @@ class TestYamachi(TestPlayer): def test_strategy(self): actions = [(C, C)] * 100 - self.versus_test(axelrod.Cooperator(), expected_actions=actions) + self.versus_test(axl.Cooperator(), expected_actions=actions) actions = [ (C, D) @@ -1609,7 +1610,7 @@ def test_strategy(self): actions += [(D, D)] # (D, C, *) gets updated, then checked. actions += [(C, D)] # (D, C, *) gets updated, but (D, D, *) checked. actions += [(D, D)] * 30 # (D, D, *) gets updated and checked from here on. - self.versus_test(axelrod.Defector(), expected_actions=actions) + self.versus_test(axl.Defector(), expected_actions=actions) actions = [(C, C), (C, D)] actions += [(C, C)] # Increment (C, C, C). Check (C, C, *). Cooperate. @@ -1630,7 +1631,7 @@ def test_strategy(self): (D, C), (D, D), ] * 30 # Defect from turn 41 on, since near 50% Defections. - self.versus_test(axelrod.Alternator(), expected_actions=actions) + self.versus_test(axl.Alternator(), expected_actions=actions) # Rip-off is the most interesting interaction. actions = [ @@ -1685,7 +1686,7 @@ def test_strategy(self): (D, D, C): 17, (D, D, D): 0, } - RipoffPlayer = axelrod.Ripoff() + RipoffPlayer = axl.Ripoff() self.versus_test( RipoffPlayer, expected_actions=actions, @@ -1709,12 +1710,12 @@ def test_strategy(self): (D, C), (C, D), ] # Takes a turn to fall back into the cycle. - self.versus_test(axelrod.Ripoff(), expected_actions=actions) + self.versus_test(axl.Ripoff(), expected_actions=actions) class TestColbert(TestPlayer): name = "Second by Colbert" - player = axelrod.SecondByColbert + player = axl.SecondByColbert expected_classifier = { "memory_depth": 4, "stochastic": False, @@ -1727,14 +1728,14 @@ class TestColbert(TestPlayer): def test_strategy(self): actions = [(C, C)] * 5 + [(D, C)] + [(C, C)] * 30 - self.versus_test(axelrod.Cooperator(), expected_actions=actions) + self.versus_test(axl.Cooperator(), expected_actions=actions) actions = [(C, D)] * 5 + [(D, D)] + [(C, D)] * 2 actions += [(D, D), (D, D), (C, D), (C, D)] * 20 - self.versus_test(axelrod.Defector(), expected_actions=actions) + self.versus_test(axl.Defector(), expected_actions=actions) opponent_actions = [C] * 8 + [C, C, D, C, C, C, C, C] - OddBall = axelrod.MockPlayer(actions=opponent_actions) + OddBall = axl.MockPlayer(actions=opponent_actions) actions = [(C, C)] * 5 + [(D, C)] + [(C, C)] * 4 actions += [(C, D)] + [(D, C), (D, C), (C, C), (C, C)] + [(C, C)] self.versus_test(OddBall, expected_actions=actions) @@ -1742,7 +1743,7 @@ def test_strategy(self): class TestMikkelson(TestPlayer): name = "Second by Mikkelson" - player = axelrod.SecondByMikkelson + player = axl.SecondByMikkelson expected_classifier = { "memory_depth": float("inf"), "stochastic": False, @@ -1756,60 +1757,60 @@ class TestMikkelson(TestPlayer): def test_strategy(self): actions = [(C, C)] * 30 self.versus_test( - axelrod.Cooperator(), expected_actions=actions, attrs={"credit": 8} + axl.Cooperator(), expected_actions=actions, attrs={"credit": 8} ) actions = [(C, D), (C, D), (C, D), (C, D)] self.versus_test( - axelrod.Defector(), expected_actions=actions, attrs={"credit": 1} + axl.Defector(), expected_actions=actions, attrs={"credit": 1} ) # Defect then reset to 4 actions += [(D, D)] self.versus_test( - axelrod.Defector(), expected_actions=actions, attrs={"credit": 4} + axl.Defector(), expected_actions=actions, attrs={"credit": 4} ) # Repeat actions += [(C, D), (D, D)] * 2 self.versus_test( - axelrod.Defector(), expected_actions=actions, attrs={"credit": 4} + axl.Defector(), expected_actions=actions, attrs={"credit": 4} ) # With ten turns passed, keep defecting now actions += [(C, D), (D, D)] self.versus_test( - axelrod.Defector(), expected_actions=actions, attrs={"credit": 0} + axl.Defector(), expected_actions=actions, attrs={"credit": 0} ) # With ten turns passed, keep defecting now actions += [(D, D)] * 30 self.versus_test( - axelrod.Defector(), expected_actions=actions, attrs={"credit": -7} + axl.Defector(), expected_actions=actions, attrs={"credit": -7} ) actions = [(C, D), (C, D), (C, C)] self.versus_test( - axelrod.Cycler("DDC"), expected_actions=actions, attrs={"credit": 3} + axl.Cycler("DDC"), expected_actions=actions, attrs={"credit": 3} ) actions += [(C, D), (C, D)] self.versus_test( - axelrod.Cycler("DDC"), expected_actions=actions, attrs={"credit": 2} + axl.Cycler("DDC"), expected_actions=actions, attrs={"credit": 2} ) actions += [(D, C)] self.versus_test( - axelrod.Cycler("DDC"), expected_actions=actions, attrs={"credit": 4} + axl.Cycler("DDC"), expected_actions=actions, attrs={"credit": 4} ) actions += [(C, D)] self.versus_test( - axelrod.Cycler("DDC"), expected_actions=actions, attrs={"credit": 5} + axl.Cycler("DDC"), expected_actions=actions, attrs={"credit": 5} ) actions += [(C, D)] self.versus_test( - axelrod.Cycler("DDC"), expected_actions=actions, attrs={"credit": 3} + axl.Cycler("DDC"), expected_actions=actions, attrs={"credit": 3} ) opponent_actions = [C] * 100 + [D] * 10 - Change_of_Heart = axelrod.MockPlayer(actions=opponent_actions) + Change_of_Heart = axl.MockPlayer(actions=opponent_actions) actions = [(C, C)] * 100 + [(C, D)] * 4 self.versus_test(Change_of_Heart, expected_actions=actions, attrs={"credit": 2}) - Change_of_Heart = axelrod.MockPlayer(actions=opponent_actions) + Change_of_Heart = axl.MockPlayer(actions=opponent_actions) actions += [(C, D)] * 2 self.versus_test( Change_of_Heart, expected_actions=actions, attrs={"credit": -2} @@ -1818,7 +1819,7 @@ def test_strategy(self): class TestRowsam(TestPlayer): name = "Second by Rowsam" - player = axelrod.SecondByRowsam + player = axl.SecondByRowsam expected_classifier = { "memory_depth": float("inf"), "stochastic": False, @@ -1832,22 +1833,22 @@ class TestRowsam(TestPlayer): def test_strategy(self): # Should always cooperate with Cooperator actions = [(C, C)] * 100 - self.versus_test(axelrod.Cooperator(), expected_actions=actions) + self.versus_test(axl.Cooperator(), expected_actions=actions) # Against a Defector should eventually enter Defect mode actions = [(C, D)] * 5 actions += [(D, D), (C, D), (D, D)] # Do a Coop-Def cycle - self.versus_test(axelrod.Defector(), expected_actions=actions, attrs={ + self.versus_test(axl.Defector(), expected_actions=actions, attrs={ "distrust_points": 5}) actions += [(C, D)] * 3 # Continue for now actions += [(D, D)] * 100 # Now Defect mode - self.versus_test(axelrod.Defector(), expected_actions=actions, attrs={ + self.versus_test(axl.Defector(), expected_actions=actions, attrs={ "distrust_points": 10, "mode": "Defect"}) # Test specific score scenarios # 5 Defects opponent_actions = [D] * 5 + [C] * 100 - custom_opponent = axelrod.MockPlayer(actions=opponent_actions) + custom_opponent = axl.MockPlayer(actions=opponent_actions) actions = [(C, D)] * 5 actions += [(D, C)] self.versus_test(custom_opponent, expected_actions=actions, attrs={ @@ -1855,7 +1856,7 @@ def test_strategy(self): # 3 Defects opponent_actions = [D] * 3 + [C] * 100 - custom_opponent = axelrod.MockPlayer(actions=opponent_actions) + custom_opponent = axl.MockPlayer(actions=opponent_actions) actions = [(C, D)] * 3 actions += [(C, C)] * 2 actions += [(D, C)] @@ -1864,7 +1865,7 @@ def test_strategy(self): # 2 Defects opponent_actions = [D] * 2 + [C] * 100 - custom_opponent = axelrod.MockPlayer(actions=opponent_actions) + custom_opponent = axl.MockPlayer(actions=opponent_actions) actions = [(C, D)] * 2 actions += [(C, C)] * 3 actions += [(D, C)] @@ -1873,7 +1874,7 @@ def test_strategy(self): # 1 Defect opponent_actions = [D] * 1 + [C] * 100 - custom_opponent = axelrod.MockPlayer(actions=opponent_actions) + custom_opponent = axl.MockPlayer(actions=opponent_actions) actions = [(C, D)] * 1 actions += [(C, C)] * 4 actions += [(D, C)] @@ -1882,28 +1883,28 @@ def test_strategy(self): # Test that some distrust_points wear off. opponent_actions = [D] * 3 + [C] * 100 - custom_opponent = axelrod.MockPlayer(actions=opponent_actions) + custom_opponent = axl.MockPlayer(actions=opponent_actions) actions = [(C, D)] * 3 actions += [(C, C)] * 2 actions += [(D, C)] self.versus_test(custom_opponent, expected_actions=actions, attrs={ "distrust_points": 3, "current_score": 6}) - custom_opponent = axelrod.MockPlayer(actions=opponent_actions) + custom_opponent = axl.MockPlayer(actions=opponent_actions) actions += [(C, C), (D, C)] # Complete Coop-Def cycle actions += [(C, C)] * 3 actions += [(D, C)] self.versus_test(custom_opponent, expected_actions=actions, attrs={ "distrust_points": 4, "current_score": 28}) - custom_opponent = axelrod.MockPlayer(actions=opponent_actions) + custom_opponent = axl.MockPlayer(actions=opponent_actions) actions += [(C, C), (D, C)] # Complete Coop-Def cycle actions += [(C, C)] * 4 # No defect or cycle this time. self.versus_test(custom_opponent, expected_actions=actions, attrs={ "distrust_points": 3, "current_score": 50}) # One point wears off. - custom_opponent = axelrod.MockPlayer(actions=opponent_actions) + custom_opponent = axl.MockPlayer(actions=opponent_actions) actions += [(C, C)] * 18 self.versus_test(custom_opponent, expected_actions=actions, attrs={ "distrust_points": 2}) # Second point wears off - custom_opponent = axelrod.MockPlayer(actions=opponent_actions) + custom_opponent = axl.MockPlayer(actions=opponent_actions) actions += [(C, C)] * 18 self.versus_test(custom_opponent, expected_actions=actions, attrs={ "distrust_points": 2}) # But no more @@ -1911,7 +1912,7 @@ def test_strategy(self): class TestAppold(TestPlayer): name = "Second by Appold" - player = axelrod.SecondByAppold + player = axl.SecondByAppold expected_classifier = { "memory_depth": float("inf"), "stochastic": True, @@ -1925,9 +1926,9 @@ class TestAppold(TestPlayer): def test_strategy(self): # Should cooperate 100% of the time with the cooperator actions = [(C, C)] * 100 - self.versus_test(axelrod.Cooperator(), expected_actions=actions) + self.versus_test(axl.Cooperator(), expected_actions=actions) - opponent = axelrod.Defector() + opponent = axl.Defector() # Cooperate always the first 4 turns actions = [(C, D)] * 4 # Should cooperate because we forgive the first_opp_def after the fourth @@ -1971,7 +1972,7 @@ def test_strategy(self): # An opponent who defects for a long time, then tries cooperating opponent_actions = [C] * 30 + [D] + [C] * 10 - MostlyCooperates = axelrod.MockPlayer(actions=opponent_actions) + MostlyCooperates = axl.MockPlayer(actions=opponent_actions) # Cooperate always at first actions = [(C, C)] * 30 # The opponent defects once @@ -1980,7 +1981,7 @@ def test_strategy(self): actions += [(C, C)] * 10 self.versus_test(MostlyCooperates, expected_actions=actions) - opponent = axelrod.CyclerDC() + opponent = axl.CyclerDC() # First three opponent actions get counted as reactions to C. Fourth # action will get counted on next turn. actions = [(C, D), (C, C), (C, D), (C, C)] @@ -2029,6 +2030,6 @@ def test_strategy(self): (C, D), (C, C), (D, C)] - self.versus_test(axelrod.Random(0.5), expected_actions=actions, seed=7) + self.versus_test(axl.Random(0.5), expected_actions=actions, seed=7) diff --git a/axelrod/tests/strategies/test_backstabber.py b/axelrod/tests/strategies/test_backstabber.py index f04006053..1f580c9cf 100644 --- a/axelrod/tests/strategies/test_backstabber.py +++ b/axelrod/tests/strategies/test_backstabber.py @@ -1,14 +1,16 @@ """Tests for BackStabber and DoubleCrosser.""" -import axelrod + +import axelrod as axl + from .test_player import TestPlayer -C, D = axelrod.Action.C, axelrod.Action.D +C, D = axl.Action.C, axl.Action.D class TestBackStabber(TestPlayer): name = "BackStabber: (D, D)" - player = axelrod.BackStabber + player = axl.BackStabber expected_classifier = { "memory_depth": float("inf"), "stochastic": False, @@ -23,13 +25,13 @@ def test_defects_after_four_defections(self): # Forgives three defections defector_actions = [(C, D), (C, D), (C, D), (C, D), (D, D), (D, D)] self.versus_test( - axelrod.Defector(), + axl.Defector(), expected_actions=defector_actions, match_attributes={"length": 200}, ) alternator_actions = [(C, C), (C, D)] * 4 + [(D, C), (D, D)] * 2 self.versus_test( - axelrod.Alternator(), + axl.Alternator(), expected_actions=alternator_actions, match_attributes={"length": 200}, ) @@ -37,27 +39,27 @@ def test_defects_after_four_defections(self): def test_defects_on_last_two_rounds_by_match_len(self): actions = [(C, C)] * 198 + [(D, C), (D, C)] self.versus_test( - axelrod.Cooperator(), + axl.Cooperator(), expected_actions=actions, match_attributes={"length": 200}, ) actions = [(C, C)] * 10 + [(D, C), (D, C)] self.versus_test( - axelrod.Cooperator(), + axl.Cooperator(), expected_actions=actions, match_attributes={"length": 12}, ) # Test that exceeds tournament length. actions = [(C, C)] * 198 + [(D, C), (D, C), (C, C), (C, C)] self.versus_test( - axelrod.Cooperator(), + axl.Cooperator(), expected_actions=actions, match_attributes={"length": 200}, ) # But only if the tournament is known. actions = [(C, C)] * 202 self.versus_test( - axelrod.Cooperator(), + axl.Cooperator(), expected_actions=actions, match_attributes={"length": -1}, ) @@ -71,7 +73,7 @@ class TestDoubleCrosser(TestBackStabber): """ name = "DoubleCrosser: (D, D)" - player = axelrod.DoubleCrosser + player = axl.DoubleCrosser expected_classifier = { "memory_depth": float("inf"), "stochastic": False, @@ -93,7 +95,7 @@ def test_when_alt_strategy_is_triggered(self): opponent_actions = starting_cooperation + [D, D, C, D] expected_actions = starting_rounds + [(C, D), (C, D), (D, C), (C, D)] self.versus_test( - axelrod.MockPlayer(actions=opponent_actions), + axl.MockPlayer(actions=opponent_actions), expected_actions=expected_actions, match_attributes={"length": 200}, ) @@ -108,7 +110,7 @@ def test_when_alt_strategy_is_triggered(self): (C, D), ] self.versus_test( - axelrod.MockPlayer(actions=opponent_actions), + axl.MockPlayer(actions=opponent_actions), expected_actions=expected_actions, match_attributes={"length": 200}, ) @@ -124,7 +126,7 @@ def test_starting_defect_keeps_alt_strategy_from_triggering(self): actions = defects_on_first + opponent_actions_suffix expected_actions = defects_on_first_actions + expected_actions_suffix self.versus_test( - axelrod.MockPlayer(actions=actions), + axl.MockPlayer(actions=actions), expected_actions=expected_actions, match_attributes={"length": 200}, ) @@ -142,7 +144,7 @@ def test_starting_defect_keeps_alt_strategy_from_triggering(self): actions = defects_in_middle + opponent_actions_suffix expected_actions = defects_in_middle_actions + expected_actions_suffix self.versus_test( - axelrod.MockPlayer(actions=actions), + axl.MockPlayer(actions=actions), expected_actions=expected_actions, match_attributes={"length": 200}, ) @@ -152,7 +154,7 @@ def test_starting_defect_keeps_alt_strategy_from_triggering(self): actions = defects_on_last + opponent_actions_suffix expected_actions = defects_on_last_actions + expected_actions_suffix self.versus_test( - axelrod.MockPlayer(actions=actions), + axl.MockPlayer(actions=actions), expected_actions=expected_actions, match_attributes={"length": 200}, ) @@ -163,7 +165,7 @@ def test_alt_strategy_stops_after_round_180(self): opponent_actions = one_eighty_opponent_actions + [C] * 6 expected_actions = one_eighty_expected_actions + [(D, C)] * 6 self.versus_test( - axelrod.MockPlayer(actions=opponent_actions), + axl.MockPlayer(actions=opponent_actions), expected_actions=expected_actions, match_attributes={"length": 200}, ) diff --git a/axelrod/tests/strategies/test_better_and_better.py b/axelrod/tests/strategies/test_better_and_better.py index 8dbb21223..f07060975 100644 --- a/axelrod/tests/strategies/test_better_and_better.py +++ b/axelrod/tests/strategies/test_better_and_better.py @@ -1,16 +1,16 @@ """Tests for the BetterAndBetter strategy.""" -import axelrod +import axelrod as axl from .test_player import TestPlayer -C, D = axelrod.Action.C, axelrod.Action.D +C, D = axl.Action.C, axl.Action.D class TestBetterAndBetter(TestPlayer): name = "Better and Better" - player = axelrod.BetterAndBetter + player = axl.BetterAndBetter expected_classifier = { "memory_depth": float("inf"), "stochastic": True, @@ -24,7 +24,7 @@ class TestBetterAndBetter(TestPlayer): def test_strategy(self): """Tests that the strategy gives expected behaviour.""" self.versus_test( - axelrod.Defector(), + axl.Defector(), expected_actions=[ (D, D), (D, D), @@ -39,7 +39,7 @@ def test_strategy(self): seed=6, ) self.versus_test( - axelrod.Cooperator(), + axl.Cooperator(), expected_actions=[ (D, C), (D, C), @@ -54,7 +54,7 @@ def test_strategy(self): seed=8, ) self.versus_test( - axelrod.Defector(), + axl.Defector(), expected_actions=[ (C, D), (D, D), @@ -91,4 +91,4 @@ def test_strategy(self): actions.append((C, D)) else: actions.append((D, D)) - self.versus_test(axelrod.Defector(), expected_actions=actions, seed=8) + self.versus_test(axl.Defector(), expected_actions=actions, seed=8) diff --git a/axelrod/tests/strategies/test_bush_mosteller.py b/axelrod/tests/strategies/test_bush_mosteller.py index 799a686a8..2cc881a57 100644 --- a/axelrod/tests/strategies/test_bush_mosteller.py +++ b/axelrod/tests/strategies/test_bush_mosteller.py @@ -1,14 +1,14 @@ -import axelrod +import axelrod as axl from .test_player import TestPlayer -C, D = axelrod.Action.C, axelrod.Action.D +C, D = axl.Action.C, axl.Action.D class TestBushMostellar(TestPlayer): name = "Bush Mosteller: 0.5, 0.5, 3.0, 0.5" - player = axelrod.BushMosteller + player = axl.BushMosteller expected_classifier = { "memory_depth": float("inf"), "stochastic": True, @@ -22,7 +22,7 @@ class TestBushMostellar(TestPlayer): def test_strategy(self): actions = [(C, C), (D, C), (D, C)] self.versus_test( - axelrod.Cooperator(), + axl.Cooperator(), expected_actions=actions, attrs={"_stimulus": 1}, seed=1, @@ -31,7 +31,7 @@ def test_strategy(self): # Making sure probabilities changes following payoffs actions = [(C, C), (D, D)] self.versus_test( - axelrod.Alternator(), + axl.Alternator(), expected_actions=actions, attrs={"_stimulus": 0.4, "_c_prob": 0.6, "_d_prob": 0.5}, seed=1, @@ -39,7 +39,7 @@ def test_strategy(self): actions = [(C, D), (D, D), (D, D)] self.versus_test( - axelrod.Defector(), + axl.Defector(), expected_actions=actions, attrs={ "_stimulus": -0.20000000000000004, @@ -52,7 +52,7 @@ def test_strategy(self): # Testing that stimulus never goes under -1 actions = [(C, C), (D, C), (D, C)] self.versus_test( - axelrod.Cooperator(), + axl.Cooperator(), expected_actions=actions, attrs={"_stimulus": -1}, init_kwargs={"aspiration_level_divider": 0.1}, @@ -62,7 +62,7 @@ def test_strategy(self): # Ensures that the player will never play C or D if his probability is equal to 0 actions = [(C, C)] * 100 self.versus_test( - axelrod.Cooperator(), + axl.Cooperator(), expected_actions=actions, init_kwargs={"d_prob": 0.0}, seed=1, @@ -70,7 +70,7 @@ def test_strategy(self): actions = [(D, C)] * 100 self.versus_test( - axelrod.Cooperator(), + axl.Cooperator(), expected_actions=actions, init_kwargs={"c_prob": 0.0}, seed=1, diff --git a/axelrod/tests/strategies/test_calculator.py b/axelrod/tests/strategies/test_calculator.py index e81d85340..3b2dc66c8 100644 --- a/axelrod/tests/strategies/test_calculator.py +++ b/axelrod/tests/strategies/test_calculator.py @@ -1,17 +1,17 @@ """Tests for Calculator strategy.""" -import axelrod +import axelrod as axl from axelrod._strategy_utils import detect_cycle from .test_player import TestPlayer -C, D = axelrod.Action.C, axelrod.Action.D +C, D = axl.Action.C, axl.Action.D class TestCalculator(TestPlayer): name = "Calculator" - player = axelrod.Calculator + player = axl.Calculator expected_classifier = { "memory_depth": float("inf"), "stochastic": True, @@ -33,10 +33,10 @@ def test_twenty_rounds_joss_then_defects_for_cyclers(self): expected_actions = twenty_test_actions + [(D, C), (D, D), (D, C), (D, D)] self.versus_test( - axelrod.Alternator(), expected_actions=twenty_test_actions, seed=seed + axl.Alternator(), expected_actions=twenty_test_actions, seed=seed ) self.versus_test( - axelrod.Alternator(), expected_actions=expected_actions, seed=seed + axl.Alternator(), expected_actions=expected_actions, seed=seed ) def test_twenty_rounds_joss_then_tit_for_tat_for_non_cyclers(self): @@ -85,12 +85,12 @@ def test_twenty_rounds_joss_then_tit_for_tat_for_non_cyclers(self): opponent_actions = twenty_non_cyclical_actions + subsequent_opponent_actions test_actions = twenty_test_actions + subsequent_test_actions self.versus_test( - axelrod.MockPlayer(actions=twenty_non_cyclical_actions), + axl.MockPlayer(actions=twenty_non_cyclical_actions), expected_actions=twenty_test_actions, seed=seed, ) self.versus_test( - axelrod.MockPlayer(actions=opponent_actions), + axl.MockPlayer(actions=opponent_actions), expected_actions=test_actions, seed=seed, ) @@ -106,7 +106,7 @@ def test_edge_case_calculator_sees_cycles_of_size_ten(self): opponent_actions = ten_length_cycle * 2 + [C, D, C] expected = ten_cycle_twenty_rounds + [(D, C), (D, D), (D, C)] self.versus_test( - axelrod.MockPlayer(actions=opponent_actions), + axl.MockPlayer(actions=opponent_actions), expected_actions=expected, seed=seed, ) @@ -126,7 +126,7 @@ def test_edge_case_calculator_ignores_cycles_gt_len_ten(self): uses_tit_for_tat_after_twenty_rounds = twenty_rounds + [(D, C), (C, D)] self.versus_test( - axelrod.MockPlayer(actions=opponent_actions), + axl.MockPlayer(actions=opponent_actions), expected_actions=uses_tit_for_tat_after_twenty_rounds, seed=seed, ) diff --git a/axelrod/tests/strategies/test_cooperator.py b/axelrod/tests/strategies/test_cooperator.py index 79d22c951..aca2290ae 100644 --- a/axelrod/tests/strategies/test_cooperator.py +++ b/axelrod/tests/strategies/test_cooperator.py @@ -1,16 +1,16 @@ """Tests for the Cooperator strategy.""" -import axelrod +import axelrod as axl from .test_player import TestPlayer -C, D = axelrod.Action.C, axelrod.Action.D +C, D = axl.Action.C, axl.Action.D class TestCooperator(TestPlayer): name = "Cooperator" - player = axelrod.Cooperator + player = axl.Cooperator expected_classifier = { "memory_depth": 0, "stochastic": False, @@ -23,13 +23,13 @@ class TestCooperator(TestPlayer): def test_strategy(self): # Cooperates always. actions = [(C, C)] + [(C, D), (C, C)] * 9 - self.versus_test(opponent=axelrod.Alternator(), expected_actions=actions) + self.versus_test(opponent=axl.Alternator(), expected_actions=actions) class TestTrickyCooperator(TestPlayer): name = "Tricky Cooperator" - player = axelrod.TrickyCooperator + player = axl.TrickyCooperator expected_classifier = { "memory_depth": 10, "stochastic": False, @@ -41,12 +41,12 @@ class TestTrickyCooperator(TestPlayer): def test_strategy(self): # Test if it tries to trick opponent. - self.versus_test(axelrod.Cooperator(), [(C, C), (C, C), (C, C), (D, C), (D, C)]) + self.versus_test(axl.Cooperator(), [(C, C), (C, C), (C, C), (D, C), (D, C)]) opponent_actions = [C, C, C, C, D, D] expected_actions = [(C, C), (C, C), (C, C), (D, C), (D, D), (C, D)] self.versus_test( - axelrod.MockPlayer(actions=opponent_actions), + axl.MockPlayer(actions=opponent_actions), expected_actions=expected_actions, ) @@ -55,7 +55,7 @@ def test_strategy(self): [(C, C), (C, C), (C, C), (D, C)] + [(D, D), (C, D)] + [(C, C)] * 10 ) self.versus_test( - axelrod.MockPlayer(actions=opponent_actions), + axl.MockPlayer(actions=opponent_actions), expected_actions=expected_actions, ) @@ -63,17 +63,17 @@ def test_cooperates_in_first_three_rounds(self): against_defector = [(C, D)] * 3 against_cooperator = [(C, C)] * 3 against_alternator = [(C, C), (C, D), (C, C)] - self.versus_test(axelrod.Defector(), expected_actions=against_defector) - self.versus_test(axelrod.Cooperator(), expected_actions=against_cooperator) - self.versus_test(axelrod.Alternator(), expected_actions=against_alternator) + self.versus_test(axl.Defector(), expected_actions=against_defector) + self.versus_test(axl.Cooperator(), expected_actions=against_cooperator) + self.versus_test(axl.Alternator(), expected_actions=against_alternator) def test_defects_after_three_rounds_if_opponent_only_cooperated_in_max_history_depth_ten( self ): against_cooperator = [(C, C)] * 3 + [(D, C)] * 20 - self.versus_test(axelrod.Cooperator(), expected_actions=against_cooperator) + self.versus_test(axl.Cooperator(), expected_actions=against_cooperator) def test_defects_when_opponent_has_no_defections_to_history_depth_ten(self): opponent_actions = [D] + [C] * 10 + [D, C] expected_actions = [(C, D)] + [(C, C)] * 10 + [(D, D), (C, C)] - self.versus_test(axelrod.MockPlayer(actions=opponent_actions), expected_actions) + self.versus_test(axl.MockPlayer(actions=opponent_actions), expected_actions) diff --git a/axelrod/tests/strategies/test_cycler.py b/axelrod/tests/strategies/test_cycler.py index 148183d14..63c27c965 100644 --- a/axelrod/tests/strategies/test_cycler.py +++ b/axelrod/tests/strategies/test_cycler.py @@ -1,10 +1,9 @@ """Tests for the Cycler strategies.""" +import unittest import itertools import random -import unittest -import axelrod -from axelrod import AntiCycler, Cycler, EvolvableCycler +import axelrod as axl from axelrod._strategy_utils import detect_cycle from axelrod.action import Action, str_to_actions from axelrod.evolvable_player import InsufficientParametersError @@ -18,7 +17,7 @@ class TestAntiCycler(TestPlayer): name = "AntiCycler" - player = axelrod.AntiCycler + player = axl.AntiCycler expected_classifier = { "memory_depth": float("inf"), "stochastic": False, @@ -31,9 +30,9 @@ class TestAntiCycler(TestPlayer): def test_has_no_cycles(self): test_range = 100 - player = AntiCycler() + player = axl.AntiCycler() for _ in range(test_range): - player.play(axelrod.Cooperator()) + player.play(axl.Cooperator()) contains_no_cycles = player.history for slice_at in range(1, len(contains_no_cycles) + 1): @@ -70,13 +69,13 @@ def test_strategy(self): against_defector = list(zip(anticycler_rounds, [D] * num_elements)) against_cooperator = list(zip(anticycler_rounds, [C] * num_elements)) - self.versus_test(axelrod.Defector(), expected_actions=against_defector) - self.versus_test(axelrod.Cooperator(), expected_actions=against_cooperator) + self.versus_test(axl.Defector(), expected_actions=against_defector) + self.versus_test(axl.Cooperator(), expected_actions=against_cooperator) class TestBasicCycler(TestPlayer): name = "Cycler: CCD" - player = Cycler + player = axl.Cycler expected_classifier = { "memory_depth": 2, "stochastic": False, @@ -90,26 +89,26 @@ class TestBasicCycler(TestPlayer): def test_memory_depth_is_len_cycle_minus_one(self): len_ten = "DCDCDDCDCD" len_five = "DCDDC" - depth_nine = Cycler(cycle=len_ten) - depth_four = Cycler(cycle=len_five) + depth_nine = axl.Cycler(cycle=len_ten) + depth_four = axl.Cycler(cycle=len_five) self.assertEqual(depth_nine.classifier["memory_depth"], 9) self.assertEqual(depth_four.classifier["memory_depth"], 4) def test_cycler_works_as_expected(self): expected = [(C, D), (D, D), (D, D), (C, D)] * 2 self.versus_test( - axelrod.Defector(), expected_actions=expected, init_kwargs={"cycle": "CDDC"} + axl.Defector(), expected_actions=expected, init_kwargs={"cycle": "CDDC"} ) def test_cycle_raises_value_error_on_bad_cycle_str(self): - self.assertRaises(ValueError, Cycler, cycle="CdDC") + self.assertRaises(ValueError, axl.Cycler, cycle="CdDC") def test_cycler_factory(cycle_str): class TestCyclerChild(TestPlayer): name = "Cycler %s" % cycle_str - player = getattr(axelrod, "Cycler%s" % cycle_str) + player = getattr(axl, "Cycler%s" % cycle_str) expected_classifier = { "memory_depth": len(cycle_str) - 1, "stochastic": False, @@ -125,7 +124,7 @@ def test_strategy(self): match_len = 20 actions_generator = _get_actions_cycle_against_cooperator(cycle_str) test_actions = [next(actions_generator) for _ in range(match_len)] - self.versus_test(axelrod.Cooperator(), expected_actions=test_actions) + self.versus_test(axl.Cooperator(), expected_actions=test_actions) return TestCyclerChild @@ -149,7 +148,7 @@ def _get_actions_cycle_against_cooperator(cycle_string: str): class TestEvolvableCycler(unittest.TestCase): - player_class = EvolvableCycler + player_class = axl.EvolvableCycler def test_normalized_parameters(self): # Must specify at least one of cycle or cycle_length @@ -183,7 +182,7 @@ def test_crossover_even_length(self): player1 = self.player_class(cycle=cycle1) player2 = self.player_class(cycle=cycle2) - axelrod.seed(3) + axl.seed(3) crossed = player1.crossover(player2) self.assertEqual(cross_cycle, crossed.cycle) @@ -194,30 +193,30 @@ def test_crossover_odd_length(self): player1 = self.player_class(cycle=cycle1) player2 = self.player_class(cycle=cycle2) - axelrod.seed(3) + axl.seed(3) crossed = player1.crossover(player2) self.assertEqual(cross_cycle, crossed.cycle) class TestEvolvableCycler2(TestEvolvablePlayer): name = "EvolvableCycler" - player_class = EvolvableCycler - parent_class = Cycler + player_class = axl.EvolvableCycler + parent_class = axl.Cycler parent_kwargs = ["cycle"] init_parameters = {"cycle_length": 100} class TestEvolvableCycler3(TestEvolvablePlayer): name = "EvolvableCycler" - player_class = EvolvableCycler - parent_class = Cycler + player_class = axl.EvolvableCycler + parent_class = axl.Cycler parent_kwargs = ["cycle"] init_parameters = {"cycle": "".join(random.choice(("C", "D")) for _ in range(50)), "mutation_potency": 10} # Substitute EvolvedCycler as a regular Cycler. -EvolvableCyclerWithDefault = PartialClass(EvolvableCycler, cycle="CCD") +EvolvableCyclerWithDefault = PartialClass(axl.EvolvableCycler, cycle="CCD") class EvolvableCyclerAsCycler(TestBasicCycler): diff --git a/axelrod/tests/strategies/test_darwin.py b/axelrod/tests/strategies/test_darwin.py index 1c354b93e..8aa0b6391 100644 --- a/axelrod/tests/strategies/test_darwin.py +++ b/axelrod/tests/strategies/test_darwin.py @@ -1,16 +1,16 @@ """Tests for the Darwin PD strategy.""" -import axelrod +import axelrod as axl from .test_player import TestPlayer -C, D = axelrod.Action.C, axelrod.Action.D +C, D = axl.Action.C, axl.Action.D class TestDarwin(TestPlayer): name = "Darwin" - player = axelrod.Darwin + player = axl.Darwin expected_classifier = { "memory_depth": float("inf"), "stochastic": False, @@ -45,38 +45,38 @@ def test_strategy(self): p1.reset() self.versus_test( - axelrod.Cooperator(), + axl.Cooperator(), expected_actions=[(C, C)] * 5, attrs={"genome": [C] * 5}, ) expected_genome = [D] * 4 + [C] self.versus_test( - axelrod.Defector(), + axl.Defector(), expected_actions=[(C, D)] * 5, attrs={"genome": expected_genome}, ) # uses genome expected_actions = [(C, C)] + [(D, C)] * 3 + [(C, C)] * 2 - self.versus_test(axelrod.Cooperator(), expected_actions) + self.versus_test(axl.Cooperator(), expected_actions) def test_against_geller_and_mindreader(self): self.versus_test( - axelrod.GellerCooperator(), + axl.GellerCooperator(), expected_actions=[(C, C)] * 2, attrs={"genome": [C, C]}, ) self.versus_test( - axelrod.MindReader(), + axl.MindReader(), expected_actions=[(C, D)] * 2, attrs={"genome": [D, C]}, ) def test_reset_history_and_attributes(self): # Overwrite this method because Darwin does not reset - self.versus_test(axelrod.Defector(), expected_actions=[(C, D)] + [(D, D)] * 4) + self.versus_test(axl.Defector(), expected_actions=[(C, D)] + [(D, D)] * 4) p1 = self.player() self.assertEqual(p1.genome, [D, C, C, C, D]) @@ -89,7 +89,7 @@ def test_all_darwin_instances_share_one_genome(self): p2 = self.player() self.assertIs(p1.genome, p2.genome) - self.versus_test(axelrod.Defector(), expected_actions=[(C, D)] + [(D, D)] * 4) + self.versus_test(axl.Defector(), expected_actions=[(C, D)] + [(D, D)] * 4) self.assertEqual(p2.genome, [D, C, C, C, D]) self.assertIs(p1.genome, p2.genome) @@ -97,7 +97,7 @@ def test_all_darwin_instances_share_one_genome(self): self.assertIs(p3.genome, p2.genome) def test_reset_genome(self): - self.versus_test(axelrod.Defector(), expected_actions=[(C, D)] + [(D, D)] * 4) + self.versus_test(axl.Defector(), expected_actions=[(C, D)] + [(D, D)] * 4) self.player.reset_genome() self.assertEqual(self.player().genome, [C]) diff --git a/axelrod/tests/strategies/test_dbs.py b/axelrod/tests/strategies/test_dbs.py index eed3e6789..93d61e41a 100644 --- a/axelrod/tests/strategies/test_dbs.py +++ b/axelrod/tests/strategies/test_dbs.py @@ -2,12 +2,12 @@ import unittest -import axelrod +import axelrod as axl from axelrod.strategies import dbs from .test_player import TestPlayer -C, D = axelrod.Action.C, axelrod.Action.D +C, D = axl.Action.C, axl.Action.D class TestNode(unittest.TestCase): @@ -198,7 +198,7 @@ def test_move_gen_grudger(self): class TestDBS(TestPlayer): name = "DBS: 0.75, 3, 4, 3, 5" - player = axelrod.DBS + player = axl.DBS expected_classifier = { "memory_depth": float("inf"), @@ -222,7 +222,7 @@ def test_strategy(self): # Test that DBS always cooperate against Cooperator. actions = [(C, C)] * 7 self.versus_test( - opponent=axelrod.Cooperator(), + opponent=axl.Cooperator(), expected_actions=actions, init_kwargs=default_init_kwargs, ) @@ -230,7 +230,7 @@ def test_strategy(self): # Test if it correctly learns Alternator strategy. actions = [(C, C), (C, D)] * 3 + [(D, C), (C, D)] * 3 self.versus_test( - opponent=axelrod.Alternator(), + opponent=axl.Alternator(), expected_actions=actions, init_kwargs=default_init_kwargs, ) @@ -240,7 +240,7 @@ def test_strategy(self): mock_actions = [C, C, C, D, D, D, D, D, D, D] exp_actions = [(C, C)] * 3 + [(C, D)] * 4 + [(D, D)] * 3 self.versus_test( - opponent=axelrod.MockPlayer(actions=mock_actions), + opponent=axl.MockPlayer(actions=mock_actions), expected_actions=exp_actions, init_kwargs=default_init_kwargs, ) @@ -256,7 +256,7 @@ def test_strategy(self): mock_actions = [C, C, C, D, D, D, D, D, D, D] exp_actions = [(C, C)] * 3 + [(C, D)] * 3 + [(D, D)] * 4 self.versus_test( - opponent=axelrod.MockPlayer(actions=mock_actions), + opponent=axl.MockPlayer(actions=mock_actions), expected_actions=exp_actions, init_kwargs=init_kwargs_2, ) @@ -277,7 +277,7 @@ def test_strategy(self): exp_actions += [(D, D), (C, D)] * 3 + [(D, D)] mock_actions = [C, D, C, D, C, D, C, D, C, D, C, D, D, D, D, D, D, D, D] self.versus_test( - opponent=axelrod.MockPlayer(actions=mock_actions), + opponent=axl.MockPlayer(actions=mock_actions), expected_actions=exp_actions, init_kwargs=init_kwargs_3, ) diff --git a/axelrod/tests/strategies/test_defector.py b/axelrod/tests/strategies/test_defector.py index 0d1813c87..d9dffa48d 100644 --- a/axelrod/tests/strategies/test_defector.py +++ b/axelrod/tests/strategies/test_defector.py @@ -1,16 +1,16 @@ """Tests for the Defector strategy.""" -import axelrod +import axelrod as axl from .test_player import TestPlayer -C, D = axelrod.Action.C, axelrod.Action.D +C, D = axl.Action.C, axl.Action.D class TestDefector(TestPlayer): name = "Defector" - player = axelrod.Defector + player = axl.Defector expected_classifier = { "memory_depth": 0, "stochastic": False, @@ -24,13 +24,13 @@ class TestDefector(TestPlayer): def test_strategy(self): # Test that always defects. actions = [(D, C)] + [(D, D), (D, C)] * 9 - self.versus_test(opponent=axelrod.Alternator(), expected_actions=actions) + self.versus_test(opponent=axl.Alternator(), expected_actions=actions) class TestTrickyDefector(TestPlayer): name = "Tricky Defector" - player = axelrod.TrickyDefector + player = axl.TrickyDefector expected_classifier = { "memory_depth": float("inf"), # Long memory "stochastic": False, @@ -45,19 +45,19 @@ def test_cooperates_if_opponent_history_has_C_and_last_three_are_D(self): opponent_actions = [D, C] + [D] * 5 actions = [(D, D), (D, C)] + [(D, D)] * 3 + [(C, D)] * 2 self.versus_test( - axelrod.MockPlayer(actions=opponent_actions), expected_actions=actions + axl.MockPlayer(actions=opponent_actions), expected_actions=actions ) def test_defects_if_opponent_never_cooperated(self): opponent_actions = [D] * 7 actions = [(D, D)] * 7 self.versus_test( - axelrod.MockPlayer(actions=opponent_actions), expected_actions=actions + axl.MockPlayer(actions=opponent_actions), expected_actions=actions ) def test_defects_if_opponent_last_three_are_not_D(self): opponent_actions = [C] + [D] * 3 + [C, D] actions = [(D, C)] + [(D, D)] * 3 + [(C, C), (D, D)] self.versus_test( - axelrod.MockPlayer(actions=opponent_actions), expected_actions=actions + axl.MockPlayer(actions=opponent_actions), expected_actions=actions ) diff --git a/axelrod/tests/strategies/test_doubler.py b/axelrod/tests/strategies/test_doubler.py index 83b9b743c..e3d436302 100644 --- a/axelrod/tests/strategies/test_doubler.py +++ b/axelrod/tests/strategies/test_doubler.py @@ -1,16 +1,16 @@ """Tests for the Doubler strategy.""" -import axelrod +import axelrod as axl from .test_player import TestPlayer -C, D = axelrod.Action.C, axelrod.Action.D +C, D = axl.Action.C, axl.Action.D class TestDoubler(TestPlayer): name = "Doubler" - player = axelrod.Doubler + player = axl.Doubler expected_classifier = { "memory_depth": float("inf"), "stochastic": False, @@ -27,7 +27,7 @@ def test_defects_if_opponent_last_play_is_D_and_defections_gt_two_times_cooperat opponent_plays = [C] * 7 + [D] * 4 + [C] actions = [(C, C)] * 7 + [(C, D)] * 4 + [(D, C)] self.versus_test( - axelrod.MockPlayer(actions=opponent_plays), expected_actions=actions + axl.MockPlayer(actions=opponent_plays), expected_actions=actions ) def test_defects_if_opponent_last_play_D_and_defections_equal_two_times_cooperations( @@ -36,7 +36,7 @@ def test_defects_if_opponent_last_play_D_and_defections_equal_two_times_cooperat opponent_plays = [C] * 8 + [D] * 4 + [C] actions = [(C, C)] * 8 + [(C, D)] * 4 + [(D, C)] self.versus_test( - axelrod.MockPlayer(actions=opponent_plays), expected_actions=actions + axl.MockPlayer(actions=opponent_plays), expected_actions=actions ) def test_cooperates_if_opponent_last_play_is_C(self): @@ -45,5 +45,5 @@ def test_cooperates_if_opponent_last_play_is_C(self): opponent_plays = opponent_first_five + [C] + [D] actions = actions_first_five + [(D, C)] + [(C, D)] self.versus_test( - axelrod.MockPlayer(actions=opponent_plays), expected_actions=actions + axl.MockPlayer(actions=opponent_plays), expected_actions=actions ) diff --git a/axelrod/tests/strategies/test_evolvable_player.py b/axelrod/tests/strategies/test_evolvable_player.py index a3a4e7923..ccb6ac37d 100644 --- a/axelrod/tests/strategies/test_evolvable_player.py +++ b/axelrod/tests/strategies/test_evolvable_player.py @@ -1,9 +1,8 @@ +import unittest import functools import random -import unittest import axelrod as axl -from axelrod import EvolvablePlayer, seed from axelrod.action import Action from axelrod.evolvable_player import copy_lists, crossover_lists, crossover_dictionaries from .test_player import TestPlayer @@ -20,7 +19,7 @@ class PartialedClass(cls): return PartialedClass -class EvolvableTestOpponent(EvolvablePlayer): +class EvolvableTestOpponent(axl.EvolvablePlayer): name = "EvolvableTestOpponent" def __init__(self, value=None): @@ -74,13 +73,13 @@ def test_randomization(self): """Test that randomization on initialization produces different strategies.""" if self.init_parameters: return - seed(0) + axl.seed(0) player1 = self.player() - seed(0) + axl.seed(0) player2 = self.player() self.assertEqual(player1, player2) for seed_ in range(2, 20): - seed(seed_) + axl.seed(seed_) player2 = self.player() if player1 != player2: return @@ -91,7 +90,7 @@ def test_mutate_variations(self): """Generate many variations to test that mutate produces different strategies.""" if not self.init_parameters: return - seed(100) + axl.seed(100) variants_produced = False for _ in range(2, 400): player = self.player() @@ -102,7 +101,7 @@ def test_mutate_variations(self): def test_mutate_and_clone(self): """Test that mutated players clone properly.""" - seed(0) + axl.seed(0) player = self.player() mutant = player.clone().mutate() clone = mutant.clone() @@ -111,7 +110,7 @@ def test_mutate_and_clone(self): def test_crossover(self): """Test that crossover produces different strategies.""" for seed_ in range(20): - seed(seed_) + axl.seed(seed_) players = [] for _ in range(2): player = self.player() @@ -133,7 +132,7 @@ def test_crossover_mismatch(self): def test_serialization(self): """Serializing and deserializing should return the original player.""" - seed(0) + axl.seed(0) player = self.player() serialized = player.serialize_parameters() deserialized_player = player.__class__.deserialize_parameters(serialized) @@ -142,7 +141,7 @@ def test_serialization(self): def test_serialization_csv(self): """Serializing and deserializing should return the original player.""" - seed(0) + axl.seed(0) player = self.player() serialized = player.serialize_parameters() s = "0, 1, {}, 3".format(serialized) diff --git a/axelrod/tests/strategies/test_finite_state_machines.py b/axelrod/tests/strategies/test_finite_state_machines.py index 0575d2a1e..12fe52e5d 100644 --- a/axelrod/tests/strategies/test_finite_state_machines.py +++ b/axelrod/tests/strategies/test_finite_state_machines.py @@ -1,8 +1,10 @@ """Tests for Finite State Machine Strategies.""" -import random + import unittest -import axelrod +import random + +import axelrod as axl from axelrod.compute_finite_state_machine_memory import get_memory_from_transitions from axelrod.evolvable_player import InsufficientParametersError from axelrod.strategies.finite_state_machines import EvolvableFSMPlayer, FSMPlayer, SimpleFSM @@ -10,7 +12,7 @@ from .test_player import TestPlayer from .test_evolvable_player import PartialClass, TestEvolvablePlayer -C, D = axelrod.Action.C, axelrod.Action.D +C, D = axl.Action.C, axl.Action.D class TestSimpleFSM(unittest.TestCase): @@ -95,7 +97,7 @@ class TestSampleFSMPlayer(TestPlayer): working as intended.""" name = "FSM Player: ((1, C, 1, C), (1, D, 1, D)), 1, C" - player = axelrod.FSMPlayer + player = axl.FSMPlayer expected_classifier = { "memory_depth": 1, @@ -116,7 +118,7 @@ def test_cooperator(self): "initial_action": C, } self.versus_test( - axelrod.Alternator(), + axl.Alternator(), expected_actions=[(C, C), (C, D)] * 5, init_kwargs=cooperator_init_kwargs, ) @@ -130,7 +132,7 @@ def test_defector(self): "initial_action": D, } self.versus_test( - axelrod.Alternator(), + axl.Alternator(), expected_actions=[(D, C), (D, D)] * 5, init_kwargs=defector_init_kwargs, ) @@ -144,7 +146,7 @@ def test_tft(self): "initial_action": C, } self.versus_test( - axelrod.Alternator(), + axl.Alternator(), expected_actions=[(C, C)] + [(C, D), (D, C)] * 5, init_kwargs=tft_init_kwargs, ) @@ -159,7 +161,7 @@ def test_wsls(self): } expected = [(C, C), (C, D), (D, C), (D, D)] * 3 self.versus_test( - axelrod.Alternator(), + axl.Alternator(), expected_actions=expected, init_kwargs=wsls_init_kwargs, ) @@ -167,7 +169,7 @@ def test_wsls(self): class TestFSMPlayer(TestPlayer): name = "FSM Player: ((1, C, 1, C), (1, D, 1, D)), 1, C" - player = axelrod.FSMPlayer + player = axl.FSMPlayer expected_classifier = { "memory_depth": 1, @@ -210,7 +212,7 @@ def transitions_test(self, state_and_action): ) self.versus_test( - axelrod.MockPlayer(actions=opponent_actions), + axl.MockPlayer(actions=opponent_actions), expected_actions=expected_actions, ) @@ -223,7 +225,7 @@ def verify_against_finite_state_machine( self.assertEqual(test_fsm.state, expected_state) def test_transitions_with_default_fsm(self): - if self.player is axelrod.FSMPlayer: + if self.player is axl.FSMPlayer: state_action = [(1, C), (1, D)] self.transitions_test(state_action) @@ -265,7 +267,7 @@ def test_strategy(self): (7, C, 7, D), (7, D, 5, C), ) - opponent = axelrod.MockPlayer([D, D, C, C, D]) + opponent = axl.MockPlayer([D, D, C, C, D]) actions = [(C, D), (C, D), (C, C), (D, C), (C, D)] self.versus_test( opponent, expected_actions=actions, init_kwargs={"transitions": transitions} @@ -282,7 +284,7 @@ def test_memory(self): class TestFortress3(TestFSMPlayer): name = "Fortress3" - player = axelrod.Fortress3 + player = axl.Fortress3 expected_classifier = { "memory_depth": 2, "stochastic": False, @@ -319,7 +321,7 @@ def test_incorrect_transitions(self): class TestFortress4(TestFSMPlayer): name = "Fortress4" - player = axelrod.Fortress4 + player = axl.Fortress4 expected_classifier = { "memory_depth": 3, "stochastic": False, @@ -365,7 +367,7 @@ def test_strategy(self): class TestPredator(TestFSMPlayer): name = "Predator" - player = axelrod.Predator + player = axl.Predator expected_classifier = { "memory_depth": float("inf"), "stochastic": False, @@ -427,7 +429,7 @@ def test_strategy(self): class TestPun1(TestFSMPlayer): name = "Pun1" - player = axelrod.Pun1 + player = axl.Pun1 expected_classifier = { "memory_depth": float("inf"), "stochastic": False, @@ -455,7 +457,7 @@ def test_strategy(self): class TestRaider(TestFSMPlayer): name = "Raider" - player = axelrod.Raider + player = axl.Raider expected_classifier = { "memory_depth": float("inf"), "stochastic": False, @@ -493,7 +495,7 @@ def test_strategy(self): class TestRipoff(TestFSMPlayer): name = "Ripoff" - player = axelrod.Ripoff + player = axl.Ripoff expected_classifier = { "memory_depth": 3, "stochastic": False, @@ -525,7 +527,7 @@ def test_strategy(self): class TestUsuallyCooperates(TestFSMPlayer): name = "UsuallyCooperates" - player = axelrod.UsuallyCooperates + player = axl.UsuallyCooperates expected_classifier = { "memory_depth": float("inf"), "stochastic": False, @@ -558,7 +560,7 @@ def test_strategy(self): class TestUsuallyDefects(TestFSMPlayer): name = "UsuallyDefects" - player = axelrod.UsuallyDefects + player = axl.UsuallyDefects expected_classifier = { "memory_depth": float("inf"), "stochastic": False, @@ -592,7 +594,7 @@ def test_strategy(self): class TestSolutionB1(TestFSMPlayer): name = "SolutionB1" - player = axelrod.SolutionB1 + player = axl.SolutionB1 expected_classifier = { "memory_depth": 2, "stochastic": False, @@ -625,7 +627,7 @@ def test_strategy(self): class TestSolutionB5(TestFSMPlayer): name = "SolutionB5" - player = axelrod.SolutionB5 + player = axl.SolutionB5 expected_classifier = { "memory_depth": float("inf"), "stochastic": False, @@ -675,7 +677,7 @@ def test_strategy(self): class TestThumper(TestFSMPlayer): name = "Thumper" - player = axelrod.Thumper + player = axl.Thumper expected_classifier = { "memory_depth": float("inf"), "stochastic": False, @@ -704,7 +706,7 @@ def test_strategy(self): class TestEvolvedFSM4(TestFSMPlayer): name = "Evolved FSM 4" - player = axelrod.EvolvedFSM4 + player = axl.EvolvedFSM4 expected_classifier = { "memory_depth": float("inf"), "stochastic": False, @@ -749,7 +751,7 @@ def test_strategy(self): class TestEvolvedFSM16(TestFSMPlayer): name = "Evolved FSM 16" - player = axelrod.EvolvedFSM16 + player = axl.EvolvedFSM16 expected_classifier = { "memory_depth": float("inf"), "stochastic": False, @@ -863,7 +865,7 @@ def test_strategy(self): class TestEvolvedFSM16Noise05(TestFSMPlayer): name = "Evolved FSM 16 Noise 05" - player = axelrod.EvolvedFSM16Noise05 + player = axl.EvolvedFSM16Noise05 expected_classifier = { "memory_depth": float("inf"), "stochastic": False, @@ -989,7 +991,7 @@ def test_strategy(self): class TestTF1(TestFSMPlayer): name = "TF1" - player = axelrod.TF1 + player = axl.TF1 expected_classifier = { "memory_depth": float("inf"), "stochastic": False, @@ -1002,12 +1004,12 @@ class TestTF1(TestFSMPlayer): def test_strategy(self): actions = [(C, C), (C, D), (D, C), (D, D), (D, C)] - self.versus_test(axelrod.Alternator(), expected_actions=actions) + self.versus_test(axl.Alternator(), expected_actions=actions) class TestTF2(TestFSMPlayer): name = "TF2" - player = axelrod.TF2 + player = axl.TF2 expected_classifier = { "memory_depth": float("inf"), "stochastic": False, @@ -1020,12 +1022,12 @@ class TestTF2(TestFSMPlayer): def test_strategy(self): actions = [(C, C), (D, D), (D, C), (C, D), (D, C)] - self.versus_test(axelrod.Alternator(), expected_actions=actions) + self.versus_test(axl.Alternator(), expected_actions=actions) class TestTF3(TestFSMPlayer): name = "TF3" - player = axelrod.TF3 + player = axl.TF3 expected_classifier = { "memory_depth": float("inf"), "stochastic": False, @@ -1038,7 +1040,7 @@ class TestTF3(TestFSMPlayer): def test_strategy(self): actions = [(C, C), (C, D), (C, C), (D, D), (D, C)] - self.versus_test(axelrod.Alternator(), expected_actions=actions) + self.versus_test(axl.Alternator(), expected_actions=actions) class TestEvolvableFSMPlayer(unittest.TestCase): @@ -1058,7 +1060,7 @@ def test_normalized_parameters(self): def test_init(self): transitions = [[0, C, 1, D], [0, D, 0, D], [1, C, 1, C], [1, D, 1, D]] - player = axelrod.EvolvableFSMPlayer( + player = axl.EvolvableFSMPlayer( transitions=transitions, initial_action=D, initial_state=1 @@ -1071,9 +1073,9 @@ def test_init(self): def test_vector_to_instance(self): num_states = 4 vector = [random.random() for _ in range(num_states * 4 + 1)] - player = axelrod.EvolvableFSMPlayer(num_states=num_states) + player = axl.EvolvableFSMPlayer(num_states=num_states) player.receive_vector(vector) - self.assertIsInstance(player, axelrod.EvolvableFSMPlayer) + self.assertIsInstance(player, axl.EvolvableFSMPlayer) serialized = player.serialize_parameters() deserialized_player = player.__class__.deserialize_parameters(serialized) @@ -1082,7 +1084,7 @@ def test_vector_to_instance(self): def test_create_vector_bounds(self): num_states = 4 - player = axelrod.EvolvableFSMPlayer(num_states=num_states) + player = axl.EvolvableFSMPlayer(num_states=num_states) lb, ub = player.create_vector_bounds() self.assertEqual(lb, [0] * (4 * num_states + 1)) self.assertEqual(ub, [1] * (4 * num_states + 1)) @@ -1090,7 +1092,7 @@ def test_create_vector_bounds(self): class TestEvolvableFSMPlayer2(TestEvolvablePlayer): name = "EvolvableFSMPlayer" - player_class = axelrod.EvolvableFSMPlayer + player_class = axl.EvolvableFSMPlayer parent_class = FSMPlayer parent_kwargs = ["transitions", "initial_action", "initial_state"] init_parameters = {"num_states": 4} @@ -1098,7 +1100,7 @@ class TestEvolvableFSMPlayer2(TestEvolvablePlayer): class TestEvolvableFSMPlayer3(TestEvolvablePlayer): name = "EvolvableFSMPlayer" - player_class = axelrod.EvolvableFSMPlayer + player_class = axl.EvolvableFSMPlayer parent_class = FSMPlayer parent_kwargs = ["transitions", "initial_action", "initial_state"] init_parameters = {"num_states": 16} @@ -1106,7 +1108,7 @@ class TestEvolvableFSMPlayer3(TestEvolvablePlayer): class TestEvolvableFSMPlayer4(TestEvolvablePlayer): name = "EvolvableFSMPlayer" - player_class = axelrod.EvolvableFSMPlayer + player_class = axl.EvolvableFSMPlayer parent_class = FSMPlayer parent_kwargs = ["transitions", "initial_action", "initial_state"] init_parameters = { diff --git a/axelrod/tests/strategies/test_forgiver.py b/axelrod/tests/strategies/test_forgiver.py index 2e965030d..e83b86d76 100644 --- a/axelrod/tests/strategies/test_forgiver.py +++ b/axelrod/tests/strategies/test_forgiver.py @@ -1,16 +1,16 @@ """Tests for the forgiver strategies.""" -import axelrod +import axelrod as axl from .test_player import TestPlayer -C, D = axelrod.Action.C, axelrod.Action.D +C, D = axl.Action.C, axl.Action.D class TestForgiver(TestPlayer): name = "Forgiver" - player = axelrod.Forgiver + player = axl.Forgiver expected_classifier = { "memory_depth": float("inf"), "stochastic": False, @@ -23,9 +23,9 @@ class TestForgiver(TestPlayer): def test_strategy(self): # If opponent has defected more than 10 percent of the time, defect. - self.versus_test(axelrod.Cooperator(), expected_actions=[(C, C)] * 10) + self.versus_test(axl.Cooperator(), expected_actions=[(C, C)] * 10) - self.versus_test(axelrod.Defector(), expected_actions=[(C, D)] + [(D, D)] * 10) + self.versus_test(axl.Defector(), expected_actions=[(C, D)] + [(D, D)] * 10) def test_cooperates_if_opponent_defections_is_ten_pct_and_defects_if_opponent_defections_gt_ten_pct( self @@ -33,7 +33,7 @@ def test_cooperates_if_opponent_defections_is_ten_pct_and_defects_if_opponent_de final_action_lowers_defections_to_ten_percent = [D] + [C] * 9 expected = [(C, D)] + [(D, C)] * 9 self.versus_test( - axelrod.MockPlayer(actions=final_action_lowers_defections_to_ten_percent), + axl.MockPlayer(actions=final_action_lowers_defections_to_ten_percent), expected_actions=expected * 5, ) @@ -41,7 +41,7 @@ def test_never_defects_if_opponent_defections_le_ten_percent(self): defections_always_le_ten_percent = [C] * 9 + [D] expected = [(C, C)] * 9 + [(C, D)] self.versus_test( - axelrod.MockPlayer(actions=defections_always_le_ten_percent), + axl.MockPlayer(actions=defections_always_le_ten_percent), expected_actions=expected * 5, ) @@ -49,7 +49,7 @@ def test_never_defects_if_opponent_defections_le_ten_percent(self): class TestForgivingTitForTat(TestPlayer): name = "Forgiving Tit For Tat" - player = axelrod.ForgivingTitForTat + player = axl.ForgivingTitForTat expected_classifier = { "memory_depth": float("inf"), "stochastic": False, @@ -61,17 +61,17 @@ class TestForgivingTitForTat(TestPlayer): } def test_strategy(self): - self.versus_test(axelrod.Cooperator(), expected_actions=[(C, C)] * 5) - self.versus_test(axelrod.Defector(), expected_actions=[(C, D)] + [(D, D)] * 5) + self.versus_test(axl.Cooperator(), expected_actions=[(C, C)] * 5) + self.versus_test(axl.Defector(), expected_actions=[(C, D)] + [(D, D)] * 5) self.versus_test( - axelrod.Alternator(), expected_actions=[(C, C)] + [(C, D), (D, C)] * 5 + axl.Alternator(), expected_actions=[(C, C)] + [(C, D), (D, C)] * 5 ) def test_never_defects_if_opponent_defections_le_ten_percent(self): defections_always_le_ten_percent = [C] * 9 + [D] expected = [(C, C)] * 9 + [(C, D)] self.versus_test( - axelrod.MockPlayer(actions=defections_always_le_ten_percent), + axl.MockPlayer(actions=defections_always_le_ten_percent), expected_actions=expected * 5, ) @@ -79,13 +79,13 @@ def test_plays_tit_for_tat_while_defections_gt_ten_percent(self): before_tft = (18 * [C] + [D]) * 3 + [D, D, D] only_cooperates = ([(C, C)] * 18 + [(C, D)]) * 3 + [(C, D), (C, D), (C, D)] self.versus_test( - axelrod.MockPlayer(actions=before_tft), expected_actions=only_cooperates + axl.MockPlayer(actions=before_tft), expected_actions=only_cooperates ) now_alternator = before_tft + [D, C, D, C] now_tft = only_cooperates + [(C, D), (D, C), (C, D), (D, C)] self.versus_test( - axelrod.MockPlayer(actions=now_alternator), expected_actions=now_tft + axl.MockPlayer(actions=now_alternator), expected_actions=now_tft ) def test_reverts_to_cooperator_if_defections_become_le_ten_percent(self): @@ -97,6 +97,6 @@ def test_reverts_to_cooperator_if_defections_become_le_ten_percent(self): maintain_ten_pct = defections_at_ten_pct + ([C] * 9 + [D]) * 3 now_cooperates = tft + ([(C, C)] * 9 + [(C, D)]) * 3 self.versus_test( - axelrod.MockPlayer(actions=maintain_ten_pct), + axl.MockPlayer(actions=maintain_ten_pct), expected_actions=now_cooperates, ) diff --git a/axelrod/tests/strategies/test_gambler.py b/axelrod/tests/strategies/test_gambler.py index b9b725b07..4a3b3c5fb 100755 --- a/axelrod/tests/strategies/test_gambler.py +++ b/axelrod/tests/strategies/test_gambler.py @@ -1,25 +1,28 @@ """Test for the Gambler strategy. Most tests come from the LookerUp test suite. """ +import unittest import copy + import random -import unittest -import axelrod + +import axelrod as axl from axelrod.load_data_ import load_pso_tables from axelrod.strategies.lookerup import create_lookup_table_keys + from .test_lookerup import convert_original_to_current from .test_player import TestPlayer from .test_evolvable_player import PartialClass, TestEvolvablePlayer tables = load_pso_tables("pso_gambler.csv", directory="data") -C, D = axelrod.Action.C, axelrod.Action.D +C, D = axl.Action.C, axl.Action.D class TestGambler(TestPlayer): name = "Gambler" - player = axelrod.Gambler + player = axl.Gambler expected_classifier = { "memory_depth": 1, @@ -37,7 +40,7 @@ class TestGambler(TestPlayer): def test_strategy(self): tft_table = {((), (D,), ()): 0, ((), (C,), ()): 1} self.versus_test( - axelrod.Alternator(), + axl.Alternator(), expected_actions=[(C, C)] + [(C, D), (D, C)] * 5, init_kwargs={"lookup_dict": tft_table}, ) @@ -46,7 +49,7 @@ def test_stochastic_values(self): stochastic_lookup = {((), (), ()): 0.3} expected_actions = [(C, C), (D, C), (D, C), (C, C), (D, C)] self.versus_test( - axelrod.Cooperator(), + axl.Cooperator(), expected_actions=expected_actions, init_kwargs={"lookup_dict": stochastic_lookup}, seed=1, @@ -56,7 +59,7 @@ def test_stochastic_values(self): class TestPSOGamblerMem1(TestPlayer): name = "PSO Gambler Mem1" - player = axelrod.PSOGamblerMem1 + player = axl.PSOGamblerMem1 expected_classifier = { "memory_depth": 1, @@ -82,14 +85,14 @@ def test_new_data(self): def test_strategy(self): vs_cooperator = [(C, C)] * 5 - self.versus_test(axelrod.Cooperator(), expected_actions=vs_cooperator) + self.versus_test(axl.Cooperator(), expected_actions=vs_cooperator) def test_defects_forever_with_correct_conditions(self): seed = 1 opponent_actions = [D, D] + [C] * 10 expected = [(C, D), (C, D), (D, C)] + [(D, C)] * 9 self.versus_test( - axelrod.MockPlayer(actions=opponent_actions), + axl.MockPlayer(actions=opponent_actions), expected_actions=expected, seed=seed, ) @@ -98,7 +101,7 @@ def test_defects_forever_with_correct_conditions(self): class TestPSOGambler1_1_1(TestPlayer): name = "PSO Gambler 1_1_1" - player = axelrod.PSOGambler1_1_1 + player = axl.PSOGambler1_1_1 expected_classifier = { "memory_depth": float("inf"), @@ -129,7 +132,7 @@ def test_cooperate_forever(self): opponent = [D] * 3 + [C] * 10 expected = [(C, D), (D, D), (D, D)] + [(C, C)] * 10 self.versus_test( - axelrod.MockPlayer(opponent), expected_actions=expected, seed=seed + axl.MockPlayer(opponent), expected_actions=expected, seed=seed ) def test_defect_forever(self): @@ -137,20 +140,20 @@ def test_defect_forever(self): opponent_actions = [C] + [D] + [C] * 10 expected = [(C, C), (C, D)] + [(D, C)] * 10 self.versus_test( - axelrod.MockPlayer(opponent_actions), expected_actions=expected, seed=seed + axl.MockPlayer(opponent_actions), expected_actions=expected, seed=seed ) opponent_actions = [D] + [C] * 10 expected = [(C, D)] + [(D, C)] * 10 self.versus_test( - axelrod.MockPlayer(opponent_actions), expected_actions=expected, seed=seed + axl.MockPlayer(opponent_actions), expected_actions=expected, seed=seed ) class TestPSOGambler2_2_2(TestPlayer): name = "PSO Gambler 2_2_2" - player = axelrod.PSOGambler2_2_2 + player = axl.PSOGambler2_2_2 expected_classifier = { "memory_depth": float("inf"), @@ -234,16 +237,16 @@ def test_new_data(self): def test_vs_defector(self): expected = [(C, D), (C, D)] + [(D, D)] * 10 - self.versus_test(axelrod.Defector(), expected_actions=expected) + self.versus_test(axl.Defector(), expected_actions=expected) def test_vs_cooperator(self): expected = [(C, C)] * 10 - self.versus_test(axelrod.Cooperator(), expected_actions=expected) + self.versus_test(axl.Cooperator(), expected_actions=expected) def test_vs_alternator(self): seed = 1 expected = [(C, C), (C, D), (C, C), (D, D), (D, C), (D, D), (D, C)] - self.versus_test(axelrod.Alternator(), expected_actions=expected, seed=seed) + self.versus_test(axl.Alternator(), expected_actions=expected, seed=seed) def test_vs_DCDDC(self): seed = 2 @@ -261,7 +264,7 @@ def test_vs_DCDDC(self): (C, C), ] self.versus_test( - axelrod.MockPlayer(actions=opponent_actions), + axl.MockPlayer(actions=opponent_actions), expected_actions=expected, seed=seed, ) @@ -269,7 +272,7 @@ def test_vs_DCDDC(self): new_seed = 139 # First seed with different result. expected[5] = (C, D) self.versus_test( - axelrod.MockPlayer(actions=opponent_actions), + axl.MockPlayer(actions=opponent_actions), expected_actions=expected, seed=new_seed, ) @@ -277,7 +280,7 @@ def test_vs_DCDDC(self): class TestPSOGambler2_2_2_Noise05(TestPlayer): name = "PSO Gambler 2_2_2 Noise 05" - player = axelrod.PSOGambler2_2_2_Noise05 + player = axl.PSOGambler2_2_2_Noise05 expected_classifier = { "memory_depth": float("inf"), @@ -361,21 +364,21 @@ def test_new_data(self): def test_vs_defector(self): expected = [(C, D), (C, D)] + [(D, D)] * 10 - self.versus_test(axelrod.Defector(), expected_actions=expected) + self.versus_test(axl.Defector(), expected_actions=expected) def test_vs_cooperator(self): expected = [(C, C)] * 10 - self.versus_test(axelrod.Cooperator(), expected_actions=expected) + self.versus_test(axl.Cooperator(), expected_actions=expected) def test_vs_alternator(self): seed = 2 expected = [(C, C), (C, D), (C, C), (D, D), (D, C), (D, D), (C, C)] - self.versus_test(axelrod.Alternator(), expected_actions=expected, seed=seed) + self.versus_test(axl.Alternator(), expected_actions=expected, seed=seed) new_seed = 1 expected[4] = (C, C) expected[6] = (D, C) - self.versus_test(axelrod.Alternator(), expected_actions=expected, seed=new_seed) + self.versus_test(axl.Alternator(), expected_actions=expected, seed=new_seed) def test_vs_DCDDC(self): opponent_actions = [D, C, D, D, C] @@ -393,13 +396,13 @@ def test_vs_DCDDC(self): (C, D), ] self.versus_test( - axelrod.MockPlayer(opponent_actions), expected_actions=expected, seed=seed + axl.MockPlayer(opponent_actions), expected_actions=expected, seed=seed ) new_seed = 3 expected[8] = (D, D) self.versus_test( - axelrod.MockPlayer(opponent_actions), + axl.MockPlayer(opponent_actions), expected_actions=expected, seed=new_seed, ) @@ -407,7 +410,7 @@ def test_vs_DCDDC(self): new_seed = 2 new_expected = expected[:6] + [(C, C), (D, D), (D, D)] self.versus_test( - axelrod.MockPlayer(opponent_actions), + axl.MockPlayer(opponent_actions), expected_actions=new_expected, seed=new_seed, ) @@ -415,7 +418,7 @@ def test_vs_DCDDC(self): class TestZDMem2(TestPlayer): name = "ZD-Mem2" - player = axelrod.ZDMem2 + player = axl.ZDMem2 expected_classifier = { "memory_depth": 2, @@ -464,7 +467,7 @@ def test_vs_defector(self): (D, D), ] - self.versus_test(axelrod.Defector(), expected_actions=expected, seed=seed) + self.versus_test(axl.Defector(), expected_actions=expected, seed=seed) def test_vs_cooperator(self): seed = 5 @@ -481,26 +484,26 @@ def test_vs_cooperator(self): (C, C), ] - self.versus_test(axelrod.Cooperator(), expected_actions=expected, seed=seed) + self.versus_test(axl.Cooperator(), expected_actions=expected, seed=seed) def test_vs_alternator(self): seed = 2 expected = [(C, C), (C, D), (D, C), (D, D), (C, C), (C, D), (D, C)] - self.versus_test(axelrod.Alternator(), expected_actions=expected, seed=seed) + self.versus_test(axl.Alternator(), expected_actions=expected, seed=seed) new_seed = 1 expected = [(C, C), (C, D), (C, C), (D, D), (D, C), (C, D), (D, C)] - self.versus_test(axelrod.Alternator(), expected_actions=expected, seed=new_seed) + self.versus_test(axl.Alternator(), expected_actions=expected, seed=new_seed) class TestEvolvableGambler(unittest.TestCase): def test_receive_vector(self): plays, op_plays, op_start_plays = 1, 1, 1 - player = axelrod.EvolvableGambler( + player = axl.EvolvableGambler( parameters=(plays, op_plays, op_start_plays)) - self.assertRaises(AttributeError, axelrod.EvolvableGambler.__getattribute__, + self.assertRaises(AttributeError, axl.EvolvableGambler.__getattribute__, *[player, 'vector']) vector = [random.random() for _ in range(8)] @@ -509,7 +512,7 @@ def test_receive_vector(self): def test_vector_to_instance(self): plays, op_plays, op_start_plays = 1, 1, 1 - player = axelrod.EvolvableGambler( + player = axl.EvolvableGambler( parameters=(plays, op_plays, op_start_plays)) vector = [random.random() for _ in range(8)] @@ -521,7 +524,7 @@ def test_vector_to_instance(self): def test_create_vector_bounds(self): plays, op_plays, op_start_plays = 1, 1, 1 - player = axelrod.EvolvableGambler( + player = axl.EvolvableGambler( parameters=(plays, op_plays, op_start_plays)) lb, ub = player.create_vector_bounds() self.assertIsInstance(lb, list) @@ -530,14 +533,14 @@ def test_create_vector_bounds(self): self.assertEqual(len(ub), 8) def test_mutate_value_bounds(self): - self.assertEqual(axelrod.EvolvableGambler.mutate_value(2), 1) - self.assertEqual(axelrod.EvolvableGambler.mutate_value(-2), 0) + self.assertEqual(axl.EvolvableGambler.mutate_value(2), 1) + self.assertEqual(axl.EvolvableGambler.mutate_value(-2), 0) class TestEvolvableGambler2(TestEvolvablePlayer): name = "EvolvableGambler" - player_class = axelrod.EvolvableGambler - parent_class = axelrod.Gambler + player_class = axl.EvolvableGambler + parent_class = axl.Gambler parent_kwargs = ["lookup_dict"] init_parameters = {"parameters": (1, 1, 1), "initial_actions": (C,)} @@ -545,8 +548,8 @@ class TestEvolvableGambler2(TestEvolvablePlayer): class TestEvolvableGambler3(TestEvolvablePlayer): name = "EvolvableGambler" - player_class = axelrod.EvolvableGambler - parent_class = axelrod.Gambler + player_class = axl.EvolvableGambler + parent_class = axl.Gambler parent_kwargs = ["lookup_dict"] init_parameters = {"parameters": (3, 2, 1), "initial_actions": (C, C, C,)} @@ -554,8 +557,8 @@ class TestEvolvableGambler3(TestEvolvablePlayer): class TestEvolvableGambler4(TestEvolvablePlayer): name = "EvolvableGambler" - player_class = axelrod.EvolvableGambler - parent_class = axelrod.Gambler + player_class = axl.EvolvableGambler + parent_class = axl.Gambler parent_kwargs = ["lookup_dict"] init_parameters = {"parameters": (2, 2, 2), "pattern": [random.random() for _ in range(64)], @@ -564,7 +567,7 @@ class TestEvolvableGambler4(TestEvolvablePlayer): # Substitute EvolvableHMMPlayer as a regular HMMPlayer. EvolvableGamblerWithDefault = PartialClass( - axelrod.EvolvableGambler, + axl.EvolvableGambler, pattern=tables[("PSO Gambler 2_2_2", 2, 2, 2)], parameters=(2, 2, 2), initial_actions=(C, C,) diff --git a/axelrod/tests/strategies/test_geller.py b/axelrod/tests/strategies/test_geller.py index 8967ef79e..e2b7bdf67 100644 --- a/axelrod/tests/strategies/test_geller.py +++ b/axelrod/tests/strategies/test_geller.py @@ -1,16 +1,16 @@ """Tests for the Geller strategy.""" -import axelrod +import axelrod as axl from .test_player import TestPlayer -C, D = axelrod.Action.C, axelrod.Action.D +C, D = axl.Action.C, axl.Action.D class TestGeller(TestPlayer): name = "Geller" - player = axelrod.Geller + player = axl.Geller expected_classifier = { "memory_depth": float("inf"), "stochastic": True, @@ -24,16 +24,16 @@ class TestGeller(TestPlayer): @classmethod def tearDownClass(cls): """After all tests have run, makes sure the Darwin genome is reset.""" - axelrod.Darwin.reset_genome() + axl.Darwin.reset_genome() super(TestGeller, cls).tearDownClass() def setUp(self): """Each test starts with the basic Darwin genome.""" - axelrod.Darwin.reset_genome() + axl.Darwin.reset_genome() super(TestGeller, self).setUp() def test_foil_strategy_inspection(self): - axelrod.seed(2) + axl.seed(2) player = self.player() self.assertEqual(player.foil_strategy_inspection(), D) self.assertEqual(player.foil_strategy_inspection(), D) @@ -41,9 +41,9 @@ def test_foil_strategy_inspection(self): def test_strategy(self): """Should cooperate against cooperators and defect against defectors.""" - self.versus_test(axelrod.Defector(), expected_actions=[(D, D)] * 5) - self.versus_test(axelrod.Cooperator(), expected_actions=[(C, C)] * 5) - self.versus_test(axelrod.Alternator(), expected_actions=[(C, C), (D, D)] * 5) + self.versus_test(axl.Defector(), expected_actions=[(D, D)] * 5) + self.versus_test(axl.Cooperator(), expected_actions=[(C, C)] * 5) + self.versus_test(axl.Alternator(), expected_actions=[(C, C), (D, D)] * 5) def test_strategy_against_lookerup_players(self): """ @@ -51,30 +51,30 @@ def test_strategy_against_lookerup_players(self): https://github.com/Axelrod-Python/Axelrod/issues/1185 """ self.versus_test( - axelrod.EvolvedLookerUp1_1_1(), expected_actions=[(C, C), (C, C)] + axl.EvolvedLookerUp1_1_1(), expected_actions=[(C, C), (C, C)] ) self.versus_test( - axelrod.EvolvedLookerUp2_2_2(), expected_actions=[(C, C), (C, C)] + axl.EvolvedLookerUp2_2_2(), expected_actions=[(C, C), (C, C)] ) def test_returns_foil_inspection_strategy_of_opponent(self): self.versus_test( - axelrod.GellerDefector(), + axl.GellerDefector(), expected_actions=[(D, D), (D, D), (D, C), (D, C)], seed=2, ) - self.versus_test(axelrod.Darwin(), expected_actions=[(C, C), (C, C), (C, C)]) + self.versus_test(axl.Darwin(), expected_actions=[(C, C), (C, C), (C, C)]) self.versus_test( - axelrod.MindReader(), expected_actions=[(D, D), (D, D), (D, D)], seed=1 + axl.MindReader(), expected_actions=[(D, D), (D, D), (D, D)], seed=1 ) class TestGellerCooperator(TestGeller): name = "Geller Cooperator" - player = axelrod.GellerCooperator + player = axl.GellerCooperator expected_classifier = { "memory_depth": float("inf"), "stochastic": False, @@ -91,20 +91,20 @@ def test_foil_strategy_inspection(self): def test_returns_foil_inspection_strategy_of_opponent(self): self.versus_test( - axelrod.GellerDefector(), expected_actions=[(D, C), (D, C), (D, C), (D, C)] + axl.GellerDefector(), expected_actions=[(D, C), (D, C), (D, C), (D, C)] ) - self.versus_test(axelrod.Darwin(), expected_actions=[(C, C), (C, C), (C, C)]) + self.versus_test(axl.Darwin(), expected_actions=[(C, C), (C, C), (C, C)]) self.versus_test( - axelrod.MindReader(), expected_actions=[(D, D), (D, D), (D, D)] + axl.MindReader(), expected_actions=[(D, D), (D, D), (D, D)] ) class TestGellerDefector(TestGeller): name = "Geller Defector" - player = axelrod.GellerDefector + player = axl.GellerDefector expected_classifier = { "memory_depth": float("inf"), "stochastic": False, @@ -122,11 +122,11 @@ def test_foil_strategy_inspection(self): def test_returns_foil_inspection_strategy_of_opponent(self): self.versus_test( - axelrod.GellerDefector(), expected_actions=[(D, D), (D, D), (D, D), (D, D)] + axl.GellerDefector(), expected_actions=[(D, D), (D, D), (D, D), (D, D)] ) - self.versus_test(axelrod.Darwin(), expected_actions=[(C, C), (C, C), (C, C)]) + self.versus_test(axl.Darwin(), expected_actions=[(C, C), (C, C), (C, C)]) self.versus_test( - axelrod.MindReader(), expected_actions=[(D, D), (D, D), (D, D)] + axl.MindReader(), expected_actions=[(D, D), (D, D), (D, D)] ) diff --git a/axelrod/tests/strategies/test_gobymajority.py b/axelrod/tests/strategies/test_gobymajority.py index 491cb86bc..6cd553880 100644 --- a/axelrod/tests/strategies/test_gobymajority.py +++ b/axelrod/tests/strategies/test_gobymajority.py @@ -1,17 +1,16 @@ """Tests for the GoByMajority strategies.""" -import axelrod -from axelrod import MockPlayer +import axelrod as axl from .test_player import TestPlayer -C, D = axelrod.Action.C, axelrod.Action.D +C, D = axl.Action.C, axl.Action.D class TestHardGoByMajority(TestPlayer): name = "Hard Go By Majority" - player = axelrod.HardGoByMajority + player = axl.HardGoByMajority default_soft = False expected_classifier = { @@ -38,7 +37,7 @@ def test_memory_depth_infinite_soft_is_false(self): + [(D, C)] * 51 + [(C, C)] ) - opponent = MockPlayer(actions=opponent_actions) + opponent = axl.MockPlayer(actions=opponent_actions) self.versus_test(opponent, expected_actions=actions, init_kwargs=init_kwargs) def test_memory_depth_even_soft_is_false(self): @@ -47,7 +46,7 @@ def test_memory_depth_even_soft_is_false(self): if self.default_soft: init_kwargs["soft"] = False - opponent = MockPlayer(actions=[C] * memory_depth + [D] * memory_depth) + opponent = axl.MockPlayer(actions=[C] * memory_depth + [D] * memory_depth) actions = ( [(D, C)] + [(C, C)] * 3 @@ -65,7 +64,7 @@ def test_memory_depth_odd(self): first_action = [(C, C)] else: first_action = [(D, C)] - opponent = MockPlayer(actions=[C] * memory_depth + [D] * memory_depth) + opponent = axl.MockPlayer(actions=[C] * memory_depth + [D] * memory_depth) actions = ( first_action + [(C, C)] * 4 @@ -85,7 +84,7 @@ def test_default_values(self): class TestGoByMajority(TestHardGoByMajority): name = "Soft Go By Majority" - player = axelrod.GoByMajority + player = axl.GoByMajority default_soft = True def test_memory_depth_infinite_soft_is_true(self): @@ -93,14 +92,14 @@ def test_memory_depth_infinite_soft_is_true(self): actions = ( [(C, C)] * 50 + [(C, D)] * 51 + [(D, D)] * 49 + [(D, C)] * 50 + [(C, C)] * 2 ) - opponent = MockPlayer(actions=opponent_actions) + opponent = axl.MockPlayer(actions=opponent_actions) self.versus_test(opponent, expected_actions=actions) def test_memory_depth_even_soft_is_true(self): memory_depth = 4 init_kwargs = {"memory_depth": memory_depth} - opponent = MockPlayer([C] * memory_depth + [D] * memory_depth) + opponent = axl.MockPlayer([C] * memory_depth + [D] * memory_depth) actions = [(C, C)] * 4 + [(C, D)] * 3 + [(D, D)] + [(D, C)] * 2 + [(C, C)] * 2 self.versus_test(opponent, expected_actions=actions, init_kwargs=init_kwargs) @@ -135,7 +134,7 @@ def factory_TestGoByRecentMajority(memory_depth, soft=True): class TestGoByRecentMajority(TestPlayer): name = "{} Go By Majority: {}".format(prefix, memory_depth) - player = getattr(axelrod, "{}GoByMajority{}".format(prefix2, memory_depth)) + player = getattr(axl, "{}GoByMajority{}".format(prefix2, memory_depth)) expected_classifier = { "stochastic": False, @@ -152,7 +151,7 @@ def test_strategy(self): # soft actions = [(C, C), (C, C), (C, D), (C, D)] # hard actions = [(D, C), (C, C), (C, D), (D, D)] opponent_actions = [C] * memory_depth + [D] * memory_depth - opponent = MockPlayer(actions=opponent_actions) + opponent = axl.MockPlayer(actions=opponent_actions) if soft: first_player_action = [C] else: diff --git a/axelrod/tests/strategies/test_handshake.py b/axelrod/tests/strategies/test_handshake.py index 4c85a700f..a6dbacc38 100644 --- a/axelrod/tests/strategies/test_handshake.py +++ b/axelrod/tests/strategies/test_handshake.py @@ -1,16 +1,16 @@ """Tests for the Handshake strategy.""" -import axelrod +import axelrod as axl from .test_player import TestPlayer -C, D = axelrod.Action.C, axelrod.Action.D +C, D = axl.Action.C, axl.Action.D class TestHandshake(TestPlayer): name = "Handshake" - player = axelrod.Handshake + player = axl.Handshake expected_classifier = { "memory_depth": float("inf"), "stochastic": False, @@ -23,14 +23,14 @@ class TestHandshake(TestPlayer): def test_strategy(self): actions = [(C, C), (D, D)] + [(C, C), (C, D)] * 10 - self.versus_test(axelrod.Alternator(), expected_actions=actions) + self.versus_test(axl.Alternator(), expected_actions=actions) actions = [(C, C), (D, C)] + [(D, C)] * 20 - self.versus_test(axelrod.Cooperator(), expected_actions=actions) + self.versus_test(axl.Cooperator(), expected_actions=actions) - opponent = axelrod.MockPlayer([D, C]) + opponent = axl.MockPlayer([D, C]) actions = [(C, D), (D, C)] + [(D, D), (D, C)] * 10 self.versus_test(opponent, expected_actions=actions) actions = [(C, D), (D, D)] + [(D, D)] * 20 - self.versus_test(axelrod.Defector(), expected_actions=actions) + self.versus_test(axl.Defector(), expected_actions=actions) diff --git a/axelrod/tests/strategies/test_headsup.py b/axelrod/tests/strategies/test_headsup.py index 333a3b237..9b9282724 100644 --- a/axelrod/tests/strategies/test_headsup.py +++ b/axelrod/tests/strategies/test_headsup.py @@ -1,10 +1,10 @@ """Strategy match tests.""" -import axelrod +import axelrod as axl from .test_player import TestMatch -C, D = axelrod.Action.C, axelrod.Action.D +C, D = axl.Action.C, axl.Action.D class TestTFTvsWSLS(TestMatch): @@ -12,7 +12,7 @@ class TestTFTvsWSLS(TestMatch): def test_rounds(self): self.versus_test( - axelrod.TitForTat(), axelrod.WinStayLoseShift(), [C, C, C, C], [C, C, C, C] + axl.TitForTat(), axl.WinStayLoseShift(), [C, C, C, C], [C, C, C, C] ) @@ -21,8 +21,8 @@ class TestTFTvSTFT(TestMatch): def test_rounds(self): self.versus_test( - axelrod.TitForTat(), - axelrod.SuspiciousTitForTat(), + axl.TitForTat(), + axl.SuspiciousTitForTat(), [C, D, C, D, C, D], [D, C, D, C, D, C], ) @@ -33,7 +33,7 @@ class TestTFTvsBully(TestMatch): def test_rounds(self): self.versus_test( - axelrod.TitForTat(), axelrod.Bully(), [C, D, D, C, C, D], [D, D, C, C, D, D] + axl.TitForTat(), axl.Bully(), [C, D, D, C, C, D], [D, D, C, C, D, D] ) @@ -42,8 +42,8 @@ class TestTF2TvsBully(TestMatch): def test_rounds(self): self.versus_test( - axelrod.TitFor2Tats(), - axelrod.Bully(), + axl.TitFor2Tats(), + axl.Bully(), [C, C, D, D, C, C, C, D], [D, D, D, C, C, D, D, D], ) @@ -54,8 +54,8 @@ class TestZDGTFT2vsBully(TestMatch): def test_rounds(self): self.versus_test( - axelrod.ZDGTFT2(), - axelrod.Bully(), + axl.ZDGTFT2(), + axl.Bully(), [C, D, D, C, C, C], [D, D, C, C, D, D], seed=2, @@ -67,8 +67,8 @@ class TestZDExtort2vsTFT(TestMatch): def test_rounds(self): self.versus_test( - axelrod.ZDExtort2(), - axelrod.TitForTat(), + axl.ZDExtort2(), + axl.TitForTat(), [C, D, D, D, D, D], [C, C, D, D, D, D], seed=2, @@ -80,8 +80,8 @@ class FoolMeOncevsBully(TestMatch): def test_rounds(self): self.versus_test( - axelrod.FoolMeOnce(), - axelrod.Bully(), + axl.FoolMeOnce(), + axl.Bully(), [C, C, D, D, D, D], [D, D, D, C, C, C], ) @@ -92,7 +92,7 @@ class FoolMeOncevsSTFT(TestMatch): def test_rounds(self): self.versus_test( - axelrod.FoolMeOnce(), axelrod.SuspiciousTitForTat(), [C] * 9, [D] + [C] * 8 + axl.FoolMeOnce(), axl.SuspiciousTitForTat(), [C] * 9, [D] + [C] * 8 ) @@ -101,8 +101,8 @@ class GrudgervsSTFT(TestMatch): def test_rounds(self): self.versus_test( - axelrod.Grudger(), - axelrod.SuspiciousTitForTat(), + axl.Grudger(), + axl.SuspiciousTitForTat(), [C] + [D] * 9, [D, C] + [D] * 8, ) @@ -113,8 +113,8 @@ class TestWSLSvsBully(TestMatch): def test_rounds(self): self.versus_test( - axelrod.WinStayLoseShift(), - axelrod.Bully(), + axl.WinStayLoseShift(), + axl.Bully(), [C, D, C, C, D], [D, D, C, D, D], ) diff --git a/axelrod/tests/strategies/test_hmm.py b/axelrod/tests/strategies/test_hmm.py index 3ce33a6cf..328ef2e69 100644 --- a/axelrod/tests/strategies/test_hmm.py +++ b/axelrod/tests/strategies/test_hmm.py @@ -1,14 +1,16 @@ """Tests for Hidden Markov Model Strategies.""" -import random + import unittest -import axelrod +import random + +import axelrod as axl from axelrod.evolvable_player import InsufficientParametersError from axelrod.strategies.hmm import EvolvableHMMPlayer, HMMPlayer, SimpleHMM, is_stochastic_matrix, random_vector from .test_player import TestMatch, TestPlayer from .test_evolvable_player import PartialClass, TestEvolvablePlayer -C, D = axelrod.Action.C, axelrod.Action.D +C, D = axl.Action.C, axl.Action.D class TestHMMPlayers(unittest.TestCase): @@ -33,7 +35,7 @@ def test_cooperator(self): t_C = [[1]] t_D = [[1]] p = [1] - player = axelrod.HMMPlayer( + player = axl.HMMPlayer( transitions_C=t_C, transitions_D=t_D, emission_probabilities=p, @@ -42,7 +44,7 @@ def test_cooperator(self): ) self.assertFalse(player.is_stochastic()) self.assertFalse(player.classifier["stochastic"]) - opponent = axelrod.Alternator() + opponent = axl.Alternator() for i in range(6): player.play(opponent) self.assertEqual(opponent.history, [C, D] * 3) @@ -54,7 +56,7 @@ def test_defector(self): t_C = [[1]] t_D = [[1]] p = [0] - player = axelrod.HMMPlayer( + player = axl.HMMPlayer( transitions_C=t_C, transitions_D=t_D, emission_probabilities=p, @@ -63,7 +65,7 @@ def test_defector(self): ) self.assertFalse(player.is_stochastic()) self.assertFalse(player.classifier["stochastic"]) - opponent = axelrod.Alternator() + opponent = axl.Alternator() for i in range(6): player.play(opponent) self.assertEqual(opponent.history, [C, D] * 3) @@ -75,7 +77,7 @@ def test_tft(self): t_C = [[1, 0], [1, 0]] t_D = [[0, 1], [0, 1]] p = [1, 0] - player = axelrod.HMMPlayer( + player = axl.HMMPlayer( transitions_C=t_C, transitions_D=t_D, emission_probabilities=p, @@ -84,7 +86,7 @@ def test_tft(self): ) self.assertFalse(player.is_stochastic()) self.assertFalse(player.classifier["stochastic"]) - opponent = axelrod.Alternator() + opponent = axl.Alternator() for i in range(6): player.play(opponent) self.assertEqual(opponent.history, [C, D] * 3) @@ -96,7 +98,7 @@ def test_wsls(self): t_C = [[1, 0], [0, 1]] t_D = [[0, 1], [1, 0]] p = [1, 0] - player = axelrod.HMMPlayer( + player = axl.HMMPlayer( transitions_C=t_C, transitions_D=t_D, emission_probabilities=p, @@ -105,7 +107,7 @@ def test_wsls(self): ) self.assertFalse(player.is_stochastic()) self.assertFalse(player.classifier["stochastic"]) - opponent = axelrod.Alternator() + opponent = axl.Alternator() for i in range(6): player.play(opponent) self.assertEqual(opponent.history, [C, D] * 3) @@ -143,7 +145,7 @@ def test_malformed_params(self): class TestHMMPlayer(TestPlayer): name = "HMM Player: 0, C" - player = axelrod.HMMPlayer + player = axl.HMMPlayer expected_classifier = { "memory_depth": 1, @@ -170,7 +172,7 @@ def test_reset(self): class TestEvolvedHMM5(TestPlayer): name = "Evolved HMM 5" - player = axelrod.EvolvedHMM5 + player = axl.EvolvedHMM5 expected_classifier = { "memory_depth": 5, @@ -184,18 +186,18 @@ class TestEvolvedHMM5(TestPlayer): def test_strategy(self): actions = [(C, C), (C, D), (D, C), (D, D), (D, C)] - self.versus_test(opponent=axelrod.Alternator(), expected_actions=actions) + self.versus_test(opponent=axl.Alternator(), expected_actions=actions) class TestEvolvedHMM5vsCooperator(TestMatch): def test_rounds(self): - self.versus_test(axelrod.EvolvedHMM5(), axelrod.Cooperator(), [C] * 5, [C] * 5) + self.versus_test(axl.EvolvedHMM5(), axl.Cooperator(), [C] * 5, [C] * 5) class TestEvolvedHMM5vsDefector(TestMatch): def test_rounds(self): self.versus_test( - axelrod.EvolvedHMM5(), axelrod.Defector(), [C, C, D], [D, D, D] + axl.EvolvedHMM5(), axl.Defector(), [C, C, D], [D, D, D] ) diff --git a/axelrod/tests/strategies/test_human.py b/axelrod/tests/strategies/test_human.py index 97e5a016b..46c1f3f8a 100644 --- a/axelrod/tests/strategies/test_human.py +++ b/axelrod/tests/strategies/test_human.py @@ -1,14 +1,15 @@ -from os import linesep from unittest import TestCase from unittest.mock import patch -from axelrod import Action, Cooperator, Player +from os import linesep + +import axelrod as axl from axelrod.strategies.human import ActionValidator, Human from prompt_toolkit.validation import ValidationError from .test_player import TestPlayer -C, D = Action.C, Action.D +C, D = axl.Action.C, axl.Action.D class TestDocument(object): @@ -80,7 +81,7 @@ def test_status_messages(self): def test_get_human_input_c(self): with patch("axelrod.human.prompt", return_value="c") as prompt_: actions = [(C, C)] * 5 - self.versus_test(Cooperator(), expected_actions=actions) + self.versus_test(axl.Cooperator(), expected_actions=actions) self.assertEqual( prompt_.call_args[0], ("Turn 5 action [C or D] for human: ",) ) @@ -88,7 +89,7 @@ def test_get_human_input_c(self): def test_get_human_input_C(self): with patch("axelrod.human.prompt", return_value="C") as prompt_: actions = [(C, C)] * 5 - self.versus_test(Cooperator(), expected_actions=actions) + self.versus_test(axl.Cooperator(), expected_actions=actions) self.assertEqual( prompt_.call_args[0], ("Turn 5 action [C or D] for human: ",) ) @@ -96,7 +97,7 @@ def test_get_human_input_C(self): def test_get_human_input_d(self): with patch("axelrod.human.prompt", return_value="d") as prompt_: actions = [(D, C)] * 5 - self.versus_test(Cooperator(), expected_actions=actions) + self.versus_test(axl.Cooperator(), expected_actions=actions) self.assertEqual( prompt_.call_args[0], ("Turn 5 action [C or D] for human: ",) ) @@ -104,7 +105,7 @@ def test_get_human_input_d(self): def test_get_human_input_D(self): with patch("axelrod.human.prompt", return_value="D") as prompt_: actions = [(D, C)] * 5 - self.versus_test(Cooperator(), expected_actions=actions) + self.versus_test(axl.Cooperator(), expected_actions=actions) self.assertEqual( prompt_.call_args[0], ("Turn 5 action [C or D] for human: ",) ) @@ -112,7 +113,7 @@ def test_get_human_input_D(self): def test_strategy(self): human = Human() expected_action = C - actual_action = human.strategy(Player(), lambda: C) + actual_action = human.strategy(axl.Player(), lambda: C) self.assertEqual(actual_action, expected_action) def test_reset_history_and_attributes(self): diff --git a/axelrod/tests/strategies/test_hunter.py b/axelrod/tests/strategies/test_hunter.py index f205a9f19..7c2912494 100644 --- a/axelrod/tests/strategies/test_hunter.py +++ b/axelrod/tests/strategies/test_hunter.py @@ -1,14 +1,15 @@ """Tests for the Hunter strategy.""" -import random import unittest -import axelrod +import random + +import axelrod as axl from axelrod.strategies.hunter import detect_cycle from .test_player import TestPlayer -C, D = axelrod.Action.C, axelrod.Action.D +C, D = axl.Action.C, axl.Action.D class TestCycleDetection(unittest.TestCase): @@ -34,7 +35,7 @@ def test_noncycles(self): class TestDefectorHunter(TestPlayer): name = "Defector Hunter" - player = axelrod.DefectorHunter + player = axl.DefectorHunter expected_classifier = { "memory_depth": float("inf"), # Long memory "stochastic": False, @@ -47,16 +48,16 @@ class TestDefectorHunter(TestPlayer): def test_strategy(self): actions = [(C, D)] * 4 + [(D, D)] * 10 - self.versus_test(opponent=axelrod.Defector(), expected_actions=actions) + self.versus_test(opponent=axl.Defector(), expected_actions=actions) actions = [(C, C)] * 14 - self.versus_test(opponent=axelrod.Cooperator(), expected_actions=actions) + self.versus_test(opponent=axl.Cooperator(), expected_actions=actions) class TestCooperatorHunter(TestPlayer): name = "Cooperator Hunter" - player = axelrod.CooperatorHunter + player = axl.CooperatorHunter expected_classifier = { "memory_depth": float("inf"), # Long memory "stochastic": False, @@ -69,16 +70,16 @@ class TestCooperatorHunter(TestPlayer): def test_strategy(self): actions = [(C, C)] * 4 + [(D, C)] * 10 - self.versus_test(opponent=axelrod.Cooperator(), expected_actions=actions) + self.versus_test(opponent=axl.Cooperator(), expected_actions=actions) actions = [(C, D)] * 14 - self.versus_test(opponent=axelrod.Defector(), expected_actions=actions) + self.versus_test(opponent=axl.Defector(), expected_actions=actions) class TestAlternatorHunter(TestPlayer): name = "Alternator Hunter" - player = axelrod.AlternatorHunter + player = axl.AlternatorHunter expected_classifier = { "memory_depth": float("inf"), # Long memory "stochastic": False, @@ -92,14 +93,14 @@ class TestAlternatorHunter(TestPlayer): def test_strategy(self): actions = [(C, C), (C, D)] * 3 + [(D, C), (D, D)] * 5 self.versus_test( - opponent=axelrod.Alternator(), + opponent=axl.Alternator(), expected_actions=actions, attrs={"is_alt": True}, ) actions = [(C, D)] * 14 self.versus_test( - opponent=axelrod.Defector(), + opponent=axl.Defector(), expected_actions=actions, attrs={"is_alt": False}, ) @@ -114,7 +115,7 @@ def test_reset_attr(self): class TestCycleHunter(TestPlayer): name = "Cycle Hunter" - player = axelrod.CycleHunter + player = axl.CycleHunter expected_classifier = { "memory_depth": float("inf"), # Long memory "stochastic": False, @@ -129,22 +130,22 @@ def test_strategy(self): player = self.player() # Test against cyclers for opponent in [ - axelrod.CyclerCCD(), - axelrod.CyclerCCCD(), - axelrod.CyclerCCCCCD(), - axelrod.Alternator(), + axl.CyclerCCD(), + axl.CyclerCCCD(), + axl.CyclerCCCCCD(), + axl.Alternator(), ]: player.reset() for i in range(30): player.play(opponent) self.assertEqual(player.history[-1], D) # Test against non-cyclers - axelrod.seed(40) + axl.seed(40) for opponent in [ - axelrod.Random(), - axelrod.AntiCycler(), - axelrod.Cooperator(), - axelrod.Defector(), + axl.Random(), + axl.AntiCycler(), + axl.Cooperator(), + axl.Defector(), ]: player.reset() for i in range(30): @@ -161,7 +162,7 @@ def test_reset_attr(self): class TestEventualCycleHunter(TestPlayer): name = "Eventual Cycle Hunter" - player = axelrod.EventualCycleHunter + player = axl.EventualCycleHunter expected_classifier = { "memory_depth": float("inf"), # Long memory "stochastic": False, @@ -176,22 +177,22 @@ def test_strategy(self): player = self.player() # Test against cyclers for opponent in [ - axelrod.CyclerCCD(), - axelrod.CyclerCCCD(), - axelrod.CyclerCCCCCD(), - axelrod.Alternator(), + axl.CyclerCCD(), + axl.CyclerCCCD(), + axl.CyclerCCCCCD(), + axl.Alternator(), ]: player.reset() for i in range(50): player.play(opponent) self.assertEqual(player.history[-1], D) # Test against non-cyclers and cooperators - axelrod.seed(43) + axl.seed(43) for opponent in [ - axelrod.Random(), - axelrod.AntiCycler(), - axelrod.DoubleCrosser(), - axelrod.Cooperator(), + axl.Random(), + axl.AntiCycler(), + axl.DoubleCrosser(), + axl.Cooperator(), ]: player.reset() for i in range(50): @@ -208,7 +209,7 @@ def test_reset_attr(self): class TestMathConstantHunter(TestPlayer): name = "Math Constant Hunter" - player = axelrod.MathConstantHunter + player = axl.MathConstantHunter expected_classifier = { "memory_depth": float("inf"), # Long memory "stochastic": False, @@ -220,7 +221,7 @@ class TestMathConstantHunter(TestPlayer): } def test_strategy(self): - opponent = axelrod.MockPlayer([C] * 7 + [D] * 3) + opponent = axl.MockPlayer([C] * 7 + [D] * 3) actions = [(C, C)] * 7 + [(C, D)] self.versus_test(opponent=opponent, expected_actions=actions) @@ -228,7 +229,7 @@ def test_strategy(self): class TestRandomHunter(TestPlayer): name = "Random Hunter" - player = axelrod.RandomHunter + player = axl.RandomHunter expected_classifier = { "memory_depth": float("inf"), # Long memory "stochastic": False, @@ -244,21 +245,21 @@ def test_strategy(self): # We should catch the alternator here. actions = [(C, C), (C, D)] * 5 + [(C, C), (D, D), (D, C)] self.versus_test( - opponent=axelrod.Alternator(), + opponent=axl.Alternator(), expected_actions=actions, attrs={"countCC": 5, "countDD": 0}, ) actions = [(C, D)] * 14 self.versus_test( - opponent=axelrod.Defector(), + opponent=axl.Defector(), expected_actions=actions, attrs={"countCC": 0, "countDD": 0}, ) def test_reset(self): player = self.player() - opponent = axelrod.Cooperator() + opponent = axl.Cooperator() for _ in range(100): player.play(opponent) self.assertFalse(player.countCC == 0) diff --git a/axelrod/tests/strategies/test_inverse.py b/axelrod/tests/strategies/test_inverse.py index c19e473c5..3eaee2a89 100644 --- a/axelrod/tests/strategies/test_inverse.py +++ b/axelrod/tests/strategies/test_inverse.py @@ -1,16 +1,16 @@ """Tests for the inverse strategy.""" -import axelrod +import axelrod as axl from .test_player import TestPlayer -C, D = axelrod.Action.C, axelrod.Action.D +C, D = axl.Action.C, axl.Action.D class TestInverse(TestPlayer): name = "Inverse" - player = axelrod.Inverse + player = axl.Inverse expected_classifier = { "memory_depth": float("inf"), # Long memory "stochastic": True, @@ -24,10 +24,10 @@ class TestInverse(TestPlayer): def test_strategy(self): # Test that as long as the opponent has not defected the player will # cooperate. - self.versus_test(axelrod.Cooperator(), expected_actions=[(C, C)]) + self.versus_test(axl.Cooperator(), expected_actions=[(C, C)]) # Tests that if opponent has played all D then player chooses D. - self.versus_test(axelrod.Defector(), expected_actions=[(C, D)] + [(D, D)] * 9) + self.versus_test(axl.Defector(), expected_actions=[(C, D)] + [(D, D)] * 9) expected_actions = [ (C, D), @@ -42,7 +42,7 @@ def test_strategy(self): (D, D), ] self.versus_test( - axelrod.MockPlayer(actions=[a[1] for a in expected_actions]), + axl.MockPlayer(actions=[a[1] for a in expected_actions]), expected_actions=expected_actions, seed=0, ) diff --git a/axelrod/tests/strategies/test_lookerup.py b/axelrod/tests/strategies/test_lookerup.py index 48602ea58..1002446ad 100755 --- a/axelrod/tests/strategies/test_lookerup.py +++ b/axelrod/tests/strategies/test_lookerup.py @@ -1,9 +1,12 @@ """Test for the Looker Up strategy.""" + +import unittest + import copy + import random -import unittest -import axelrod +import axelrod as axl from axelrod.action import str_to_actions from axelrod.evolvable_player import InsufficientParametersError from axelrod.strategies.lookerup import ( @@ -18,7 +21,7 @@ from .test_player import TestPlayer from .test_evolvable_player import PartialClass, TestEvolvablePlayer -C, D = axelrod.Action.C, axelrod.Action.D +C, D = axl.Action.C, axl.Action.D class TestLookupTable(unittest.TestCase): @@ -188,7 +191,7 @@ def test_create_lookup_table_keys(self): class TestLookerUp(TestPlayer): name = "LookerUp" - player = axelrod.LookerUp + player = axl.LookerUp expected_classifier = { "memory_depth": 1, # Default TFT @@ -212,7 +215,7 @@ def test_default_init(self): def test_pattern_and_params_init_pattern_is_string(self): pattern = "CCCC" parameters = Plays(1, 1, 0) - player = axelrod.LookerUp(pattern=pattern, parameters=parameters) + player = axl.LookerUp(pattern=pattern, parameters=parameters) expected_lookup_table = { Plays((C,), (D,), ()): C, Plays((D,), (D,), ()): C, @@ -224,7 +227,7 @@ def test_pattern_and_params_init_pattern_is_string(self): def test_pattern_and_params_init_pattern_is_tuple(self): pattern = (C, C, C, C) parameters = Plays(1, 1, 0) - player = axelrod.LookerUp(pattern=pattern, parameters=parameters) + player = axl.LookerUp(pattern=pattern, parameters=parameters) expected_lookup_table = { Plays((C,), (D,), ()): C, Plays((D,), (D,), ()): C, @@ -236,7 +239,7 @@ def test_pattern_and_params_init_pattern_is_tuple(self): def test_pattern_and_params_init_can_still_use_regular_tuple(self): pattern = (C, C) parameters = (1, 0, 0) - player = axelrod.LookerUp(pattern=pattern, parameters=parameters) + player = axl.LookerUp(pattern=pattern, parameters=parameters) expected_lookup_table = {Plays((C,), (), ()): C, Plays((D,), (), ()): C} self.assertEqual(player.lookup_dict, expected_lookup_table) @@ -244,8 +247,8 @@ def test_pattern_and_params_init_only_happens_if_both_are_present(self): default = {Plays((), (D,), ()): D, Plays((), (C,), ()): C} pattern = "CC" parameters = Plays(self_plays=0, op_plays=1, op_openings=0) - player1 = axelrod.LookerUp(pattern=pattern) - player2 = axelrod.LookerUp(parameters=parameters) + player1 = axl.LookerUp(pattern=pattern) + player2 = axl.LookerUp(parameters=parameters) self.assertEqual(player1.lookup_dict, default) self.assertEqual(player2.lookup_dict, default) @@ -257,7 +260,7 @@ def test_lookup_table_init(self): ((C,), (C,), ()): C, ((D,), (C,), ()): C, } - player = axelrod.LookerUp(lookup_dict=lookup_table) + player = axl.LookerUp(lookup_dict=lookup_table) self.assertEqual(player.lookup_dict, lookup_table) self.assertIsInstance(next(iter(player.lookup_dict)), Plays) @@ -270,7 +273,7 @@ def test_lookup_table_init_supersedes_pattern_init(self): } pattern = "CCCCCCCC" parameters = Plays(self_plays=1, op_plays=1, op_openings=1) - player = axelrod.LookerUp( + player = axl.LookerUp( lookup_dict=lookup_table, pattern=pattern, parameters=parameters ) @@ -279,24 +282,24 @@ def test_lookup_table_init_supersedes_pattern_init(self): def test_init_raises_errors(self): mismatch_dict = {((C,), (C,), ()): C, ((D, D), (D, D), ()): C} with self.assertRaises(ValueError): - axelrod.LookerUp(lookup_dict=mismatch_dict) + axl.LookerUp(lookup_dict=mismatch_dict) incomplete_lookup_dict = {((C,), (C,), ()): C, ((D,), (D,), ()): C} with self.assertRaises(ValueError): - axelrod.LookerUp(lookup_dict=incomplete_lookup_dict) + axl.LookerUp(lookup_dict=incomplete_lookup_dict) too_short_pattern = "CC" with self.assertRaises(ValueError): - axelrod.LookerUp(pattern=too_short_pattern, parameters=(3, 3, 3)) + axl.LookerUp(pattern=too_short_pattern, parameters=(3, 3, 3)) def test_initial_actions_set_to_max_table_depth(self): initial_actions = (D, D, D) - table_depth_one = axelrod.LookerUp(initial_actions=initial_actions) + table_depth_one = axl.LookerUp(initial_actions=initial_actions) self.assertEqual(table_depth_one.initial_actions, (D,)) def test_initial_actions_makes_up_missing_actions_with_c(self): initial_actions = (D,) - table_depth_three = axelrod.LookerUp( + table_depth_three = axl.LookerUp( initial_actions=initial_actions, pattern="CCCCCCCC", parameters=Plays(3, 0, 0), @@ -304,27 +307,27 @@ def test_initial_actions_makes_up_missing_actions_with_c(self): self.assertEqual(table_depth_three.initial_actions, (D, C, C)) def test_set_memory_depth(self): - mem_depth_1 = axelrod.LookerUp(pattern="CC", parameters=Plays(1, 0, 0)) + mem_depth_1 = axl.LookerUp(pattern="CC", parameters=Plays(1, 0, 0)) self.assertEqual(mem_depth_1.classifier["memory_depth"], 1) - mem_depth_3 = axelrod.LookerUp(pattern="C" * 16, parameters=Plays(1, 3, 0)) + mem_depth_3 = axl.LookerUp(pattern="C" * 16, parameters=Plays(1, 3, 0)) self.assertEqual(mem_depth_3.classifier["memory_depth"], 3) - mem_depth_inf = axelrod.LookerUp(pattern="CC", parameters=Plays(0, 0, 1)) + mem_depth_inf = axl.LookerUp(pattern="CC", parameters=Plays(0, 0, 1)) self.assertEqual(mem_depth_inf.classifier["memory_depth"], float("inf")) def test_strategy(self): actions = [(C, C), (C, D), (D, C), (C, D)] - self.versus_test(axelrod.Alternator(), expected_actions=actions) + self.versus_test(axl.Alternator(), expected_actions=actions) actions = [(C, D), (D, D), (D, D)] - self.versus_test(axelrod.Defector(), expected_actions=actions) + self.versus_test(axl.Defector(), expected_actions=actions) def test_cooperator_table(self): lookup_table = {((), (), ()): C} actions = [(C, D)] * 5 self.versus_test( - axelrod.Defector(), + axl.Defector(), expected_actions=actions, init_kwargs={"lookup_dict": lookup_table}, ) @@ -341,7 +344,7 @@ def test_defector_table_with_initial_cooperate(self): } actions = [(C, C)] + [(D, D), (D, C)] * 4 self.versus_test( - axelrod.Alternator(), + axl.Alternator(), expected_actions=actions, init_kwargs={"lookup_dict": defector_table}, ) @@ -353,7 +356,7 @@ def test_zero_tables(self): tft_vs_alternator = [(C, C)] + [(D, D), (C, C)] * 5 self.versus_test( - axelrod.Alternator(), + axl.Alternator(), expected_actions=tft_vs_alternator, init_kwargs={"parameters": parameters, "pattern": anti_tft_pattern}, ) @@ -364,13 +367,13 @@ def test_opponent_starting_moves_table(self): vs_alternator = [(C, C), (C, D)] * 5 self.versus_test( - axelrod.Alternator(), + axl.Alternator(), expected_actions=vs_alternator, init_kwargs={"lookup_dict": first_move_table}, ) vs_initial_defector = [(C, D)] + [(D, C), (D, D)] * 10 - opponent = axelrod.MockPlayer(actions=[D, C]) + opponent = axl.MockPlayer(actions=[D, C]) self.versus_test( opponent, expected_actions=vs_initial_defector, @@ -378,7 +381,7 @@ def test_opponent_starting_moves_table(self): ) def test_lookup_table_display(self): - player = axelrod.LookerUp( + player = axl.LookerUp( pattern="CCCC", parameters=Plays(self_plays=2, op_plays=0, op_openings=0) ) self.assertEqual( @@ -396,7 +399,7 @@ def test_lookup_table_display(self): class TestEvolvedLookerUp1_1_1(TestPlayer): name = "EvolvedLookerUp1_1_1" - player = axelrod.EvolvedLookerUp1_1_1 + player = axl.EvolvedLookerUp1_1_1 expected_classifier = { "memory_depth": float("inf"), @@ -426,21 +429,21 @@ def test_vs_initial_defector(self): opponent = [D, C, C, D, D, C] expected = [(C, D), (D, C), (C, C), (D, D), (D, D), (D, C)] self.versus_test( - axelrod.MockPlayer(actions=opponent), expected_actions=expected + axl.MockPlayer(actions=opponent), expected_actions=expected ) def test_vs_initial_cooperator(self): opponent = [C, D, D, C, C, D] expected = [(C, C), (C, D), (D, D), (D, C), (D, C), (D, D)] self.versus_test( - axelrod.MockPlayer(actions=opponent), expected_actions=expected + axl.MockPlayer(actions=opponent), expected_actions=expected ) class TestEvolvedLookerUp2_2_2(TestPlayer): name = "EvolvedLookerUp2_2_2" - player = axelrod.EvolvedLookerUp2_2_2 + player = axl.EvolvedLookerUp2_2_2 expected_classifier = { "memory_depth": float("inf"), @@ -526,20 +529,20 @@ def test_vs_initial_defector(self): opponent_actions = [D, D] + [C, D] * 3 expected = [(C, D), (C, D)] + [(D, C), (C, D)] * 3 self.versus_test( - axelrod.MockPlayer(actions=opponent_actions), expected_actions=expected + axl.MockPlayer(actions=opponent_actions), expected_actions=expected ) def test_vs_initial_d_c(self): opponent_actions = [D, C] + [C, D] * 3 expected = [(C, D), (C, C)] + [(D, C), (C, D), (C, C), (D, D), (C, C), (C, D)] self.versus_test( - axelrod.MockPlayer(actions=opponent_actions), expected_actions=expected + axl.MockPlayer(actions=opponent_actions), expected_actions=expected ) class TestWinner12(TestPlayer): name = "Winner12" - player = axelrod.Winner12 + player = axl.Winner12 expected_classifier = { "memory_depth": 2, @@ -571,18 +574,18 @@ def test_new_data(self): def test_strategy(self): """Starts by cooperating twice.""" vs_alternator = [(C, C), (C, D), (D, C), (D, D)] * 5 - self.versus_test(axelrod.Alternator(), expected_actions=vs_alternator) + self.versus_test(axl.Alternator(), expected_actions=vs_alternator) - self.versus_test(axelrod.Cooperator(), expected_actions=[(C, C)] * 10) + self.versus_test(axl.Cooperator(), expected_actions=[(C, C)] * 10) self.versus_test( - axelrod.Defector(), expected_actions=([(C, D), (C, D)] + [(D, D)] * 10) + axl.Defector(), expected_actions=([(C, D), (C, D)] + [(D, D)] * 10) ) class TestWinner21(TestPlayer): name = "Winner21" - player = axelrod.Winner21 + player = axl.Winner21 expected_classifier = { "memory_depth": 2, @@ -614,14 +617,14 @@ def test_new_data(self): def test_strategy(self): """Starts by cooperating twice.""" vs_alternator = [(D, C), (C, D)] + [(D, C), (D, D)] * 5 - self.versus_test(axelrod.Alternator(), expected_actions=vs_alternator) + self.versus_test(axl.Alternator(), expected_actions=vs_alternator) self.versus_test( - axelrod.Cooperator(), expected_actions=[(D, C)] + [(C, C)] * 10 + axl.Cooperator(), expected_actions=[(D, C)] + [(C, C)] * 10 ) self.versus_test( - axelrod.Defector(), expected_actions=([(D, D), (C, D)] + [(D, D)] * 10) + axl.Defector(), expected_actions=([(D, D), (C, D)] + [(D, D)] * 10) ) @@ -694,24 +697,24 @@ def test_normalized_parameters(self): class TestEvolvableLookerUp2(TestEvolvablePlayer): name = "EvolvableLookerUp" - player_class = axelrod.EvolvableLookerUp - parent_class = axelrod.LookerUp + player_class = axl.EvolvableLookerUp + parent_class = axl.LookerUp parent_kwargs = ["lookup_dict", "initial_actions"] init_parameters = {"parameters": (1, 1, 1)} class TestEvolvableLookerUp3(TestEvolvablePlayer): name = "EvolvableLookerUp" - player_class = axelrod.EvolvableLookerUp - parent_class = axelrod.LookerUp + player_class = axl.EvolvableLookerUp + parent_class = axl.LookerUp parent_kwargs = ["lookup_dict", "initial_actions"] init_parameters = {"parameters": (2, 1, 3)} class TestEvolvableLookerUp4(TestEvolvablePlayer): name = "EvolvableLookerUp" - player_class = axelrod.EvolvableLookerUp - parent_class = axelrod.LookerUp + player_class = axl.EvolvableLookerUp + parent_class = axl.LookerUp parent_kwargs = ["lookup_dict", "initial_actions"] init_parameters = {"parameters": (2, 2, 2), "pattern": "".join([random.choice(('C', 'D')) for _ in range(64)]), @@ -720,8 +723,8 @@ class TestEvolvableLookerUp4(TestEvolvablePlayer): class TestEvolvableLookerUp5(TestEvolvablePlayer): name = "EvolvableLookerUp" - player_class = axelrod.EvolvableLookerUp - parent_class = axelrod.LookerUp + player_class = axl.EvolvableLookerUp + parent_class = axl.LookerUp parent_kwargs = ["lookup_dict", "initial_actions"] init_parameters = { "initial_actions": (C, C,), diff --git a/axelrod/tests/strategies/test_mathematicalconstants.py b/axelrod/tests/strategies/test_mathematicalconstants.py index 2bc784284..64d5ec850 100644 --- a/axelrod/tests/strategies/test_mathematicalconstants.py +++ b/axelrod/tests/strategies/test_mathematicalconstants.py @@ -1,16 +1,16 @@ """Tests for the golden and other mathematical strategies.""" -import axelrod +import axelrod as axl from .test_player import TestPlayer -C, D = axelrod.Action.C, axelrod.Action.D +C, D = axl.Action.C, axl.Action.D class TestGolden(TestPlayer): name = "$\phi$" - player = axelrod.Golden + player = axl.Golden expected_classifier = { "memory_depth": float("inf"), # Long memory "stochastic": False, @@ -23,19 +23,19 @@ class TestGolden(TestPlayer): def test_strategy(self): actions = [(C, C), (D, D), (C, C), (D, D), (C, C)] - self.versus_test(opponent=axelrod.Alternator(), expected_actions=actions) + self.versus_test(opponent=axl.Alternator(), expected_actions=actions) actions = [(C, C), (D, C), (D, C), (D, C), (D, C)] - self.versus_test(opponent=axelrod.Cooperator(), expected_actions=actions) + self.versus_test(opponent=axl.Cooperator(), expected_actions=actions) actions = [(C, D), (C, D), (C, D), (C, D), (C, D)] - self.versus_test(opponent=axelrod.Defector(), expected_actions=actions) + self.versus_test(opponent=axl.Defector(), expected_actions=actions) class TestPi(TestPlayer): name = "$\pi$" - player = axelrod.Pi + player = axl.Pi expected_classifier = { "memory_depth": float("inf"), # Long memory "stochastic": False, @@ -48,19 +48,19 @@ class TestPi(TestPlayer): def test_strategy(self): actions = [(C, C), (D, D), (C, C), (C, D), (C, C)] - self.versus_test(opponent=axelrod.Alternator(), expected_actions=actions) + self.versus_test(opponent=axl.Alternator(), expected_actions=actions) actions = [(C, C), (D, C), (D, C), (D, C), (D, C)] - self.versus_test(opponent=axelrod.Cooperator(), expected_actions=actions) + self.versus_test(opponent=axl.Cooperator(), expected_actions=actions) actions = [(C, D), (C, D), (C, D), (C, D), (C, D)] - self.versus_test(opponent=axelrod.Defector(), expected_actions=actions) + self.versus_test(opponent=axl.Defector(), expected_actions=actions) class Teste(TestPlayer): name = "$e$" - player = axelrod.e + player = axl.e expected_classifier = { "memory_depth": float("inf"), # Long memory "stochastic": False, @@ -73,10 +73,10 @@ class Teste(TestPlayer): def test_strategy(self): actions = [(C, C), (D, D), (C, C), (C, D), (C, C)] - self.versus_test(opponent=axelrod.Alternator(), expected_actions=actions) + self.versus_test(opponent=axl.Alternator(), expected_actions=actions) actions = [(C, C), (D, C), (D, C), (D, C), (D, C)] - self.versus_test(opponent=axelrod.Cooperator(), expected_actions=actions) + self.versus_test(opponent=axl.Cooperator(), expected_actions=actions) actions = [(C, D), (C, D), (C, D), (C, D), (C, D)] - self.versus_test(opponent=axelrod.Defector(), expected_actions=actions) + self.versus_test(opponent=axl.Defector(), expected_actions=actions) diff --git a/axelrod/tests/strategies/test_memoryone.py b/axelrod/tests/strategies/test_memoryone.py index e05c0ffa2..550ac7941 100644 --- a/axelrod/tests/strategies/test_memoryone.py +++ b/axelrod/tests/strategies/test_memoryone.py @@ -1,24 +1,22 @@ """Tests for the Memoryone strategies.""" - import unittest import warnings -import axelrod -from axelrod import Game +import axelrod as axl from axelrod.strategies.memoryone import MemoryOnePlayer from .test_player import TestPlayer, test_four_vector -C, D = axelrod.Action.C, axelrod.Action.D +C, D = axl.Action.C, axl.Action.D class TestGenericPlayerOne(unittest.TestCase): """A class to test the naming and classification of generic memory one players.""" - p1 = axelrod.MemoryOnePlayer(four_vector=(0, 0, 0, 0)) - p2 = axelrod.MemoryOnePlayer(four_vector=(1, 0, 1, 0)) - p3 = axelrod.MemoryOnePlayer(four_vector=(1, 0.5, 1, 0.5)) + p1 = axl.MemoryOnePlayer(four_vector=(0, 0, 0, 0)) + p2 = axl.MemoryOnePlayer(four_vector=(1, 0, 1, 0)) + p3 = axl.MemoryOnePlayer(four_vector=(1, 0.5, 1, 0.5)) def test_name(self): self.assertEqual(self.p1.name, "Generic Memory One Player: (0, 0, 0, 0)") @@ -34,7 +32,7 @@ def test_stochastic_classification(self): class TestWinStayLoseShift(TestPlayer): name = "Win-Stay Lose-Shift: C" - player = axelrod.WinStayLoseShift + player = axl.WinStayLoseShift expected_classifier = { "memory_depth": 1, "stochastic": False, @@ -51,13 +49,13 @@ def test_class_classification(self): def test_strategy(self): # Check that switches if does not get best payoff. actions = [(C, C), (C, D), (D, C), (D, D), (C, C)] - self.versus_test(opponent=axelrod.Alternator(), expected_actions=actions) + self.versus_test(opponent=axl.Alternator(), expected_actions=actions) class TestWinShiftLoseStayTestPlayer(TestPlayer): name = "Win-Shift Lose-Stay: D" - player = axelrod.WinShiftLoseStay + player = axl.WinShiftLoseStay expected_classifier = { "memory_depth": 1, "stochastic": False, @@ -71,14 +69,14 @@ class TestWinShiftLoseStayTestPlayer(TestPlayer): def test_strategy(self): # Check that switches if does not get best payoff. actions = [(D, C), (C, D), (C, C), (D, D), (D, C)] - self.versus_test(opponent=axelrod.Alternator(), + self.versus_test(opponent=axl.Alternator(), expected_actions=actions) class TestGTFT(TestPlayer): name = "GTFT: 0.33" - player = axelrod.GTFT + player = axl.GTFT expected_classifier = { "memory_depth": 1, "stochastic": True, @@ -91,16 +89,16 @@ class TestGTFT(TestPlayer): def test_strategy(self): actions = [(C, C), (C, D), (D, C), (C, D), (D, C)] self.versus_test( - opponent=axelrod.Alternator(), expected_actions=actions, seed=0 + opponent=axl.Alternator(), expected_actions=actions, seed=0 ) actions = [(C, C), (C, D), (C, C), (C, D), (D, C)] self.versus_test( - opponent=axelrod.Alternator(), expected_actions=actions, seed=1 + opponent=axl.Alternator(), expected_actions=actions, seed=1 ) def test_four_vector(self): - (R, P, S, T) = Game().RPST() + (R, P, S, T) = axl.Game().RPST() p = min(1 - (T - R) / (R - S), (R - P) / (T - P)) expected_dictionary = {(C, C): 1.0, (C, D): p, (D, C): 1.0, (D, D): p} test_four_vector(self, expected_dictionary) @@ -114,7 +112,7 @@ def test_allow_for_zero_probability(self): class TestFirmButFair(TestPlayer): name = "Firm But Fair" - player = axelrod.FirmButFair + player = axl.FirmButFair expected_classifier = { "memory_depth": 1, "stochastic": True, @@ -132,19 +130,19 @@ def test_four_vector(self): def test_strategy(self): actions = [(C, C), (C, D), (D, C), (C, D), (D, C)] - self.versus_test(opponent=axelrod.Alternator(), expected_actions=actions) + self.versus_test(opponent=axl.Alternator(), expected_actions=actions) actions = [(C, D), (D, D), (D, D), (D, D), (C, D)] - self.versus_test(opponent=axelrod.Defector(), expected_actions=actions, seed=0) + self.versus_test(opponent=axl.Defector(), expected_actions=actions, seed=0) actions = [(C, D), (D, D), (C, D), (D, D), (D, D)] - self.versus_test(opponent=axelrod.Defector(), expected_actions=actions, seed=1) + self.versus_test(opponent=axl.Defector(), expected_actions=actions, seed=1) class TestStochasticCooperator(TestPlayer): name = "Stochastic Cooperator" - player = axelrod.StochasticCooperator + player = axl.StochasticCooperator expected_classifier = { "memory_depth": 1, "stochastic": True, @@ -167,29 +165,29 @@ def test_four_vector(self): def test_strategy(self): actions = [(C, C), (D, D), (C, C), (C, D), (C, C), (D, D)] self.versus_test( - opponent=axelrod.Alternator(), expected_actions=actions, seed=15 + opponent=axl.Alternator(), expected_actions=actions, seed=15 ) actions = [(C, C), (C, D), (D, C), (D, D), (C, C), (C, D)] self.versus_test( - opponent=axelrod.Alternator(), expected_actions=actions, seed=1 + opponent=axl.Alternator(), expected_actions=actions, seed=1 ) actions = [(C, C), (C, D), (D, C), (D, D), (D, C), (D, D)] self.versus_test( - opponent=axelrod.Alternator(), expected_actions=actions, seed=3 + opponent=axl.Alternator(), expected_actions=actions, seed=3 ) actions = [(C, C), (C, D), (D, C), (D, D), (D, C), (C, D)] self.versus_test( - opponent=axelrod.Alternator(), expected_actions=actions, seed=13 + opponent=axl.Alternator(), expected_actions=actions, seed=13 ) class TestStochasticWSLS(TestPlayer): name = "Stochastic WSLS: 0.05" - player = axelrod.StochasticWSLS + player = axl.StochasticWSLS expected_classifier = { "memory_depth": 1, "stochastic": True, @@ -203,19 +201,19 @@ class TestStochasticWSLS(TestPlayer): def test_strategy(self): actions = [(C, C), (D, D), (C, C), (C, D), (D, C), (D, D)] self.versus_test( - opponent=axelrod.Alternator(), expected_actions=actions, seed=2 + opponent=axl.Alternator(), expected_actions=actions, seed=2 ) actions = [(C, C), (C, D), (D, C), (D, D), (C, C), (C, D)] self.versus_test( - opponent=axelrod.Alternator(), expected_actions=actions, seed=31 + opponent=axl.Alternator(), expected_actions=actions, seed=31 ) actions = [(C, D), (D, C), (D, D), (C, C), (C, D), (D, C)] - self.versus_test(opponent=axelrod.CyclerDC(), expected_actions=actions, seed=2) + self.versus_test(opponent=axl.CyclerDC(), expected_actions=actions, seed=2) actions = [(C, D), (C, C), (C, D), (D, C), (D, D), (C, C)] - self.versus_test(opponent=axelrod.CyclerDC(), expected_actions=actions, seed=31) + self.versus_test(opponent=axl.CyclerDC(), expected_actions=actions, seed=31) def test_four_vector(self): player = self.player() @@ -258,7 +256,7 @@ def test_exception_if_probability_vector_outside_valid_values(self): class TestSoftJoss(TestPlayer): name = "Soft Joss: 0.9" - player = axelrod.SoftJoss + player = axl.SoftJoss expected_classifier = { "memory_depth": 1, "stochastic": True, @@ -276,17 +274,17 @@ def test_four_vector(self): def test_strategy(self): actions = [(C, C), (C, D), (D, C), (C, D), (D, C), (C, D)] self.versus_test( - opponent=axelrod.Alternator(), expected_actions=actions, seed=2 + opponent=axl.Alternator(), expected_actions=actions, seed=2 ) actions = [(C, D), (D, C), (C, D), (D, C), (C, D), (D, C)] - self.versus_test(opponent=axelrod.CyclerDC(), expected_actions=actions, seed=5) + self.versus_test(opponent=axl.CyclerDC(), expected_actions=actions, seed=5) class TestALLCorALLD(TestPlayer): name = "ALLCorALLD" - player = axelrod.ALLCorALLD + player = axl.ALLCorALLD expected_classifier = { "memory_depth": 1, "stochastic": True, @@ -300,11 +298,11 @@ class TestALLCorALLD(TestPlayer): def test_strategy(self): actions = [(D, C)] * 10 self.versus_test( - opponent=axelrod.Cooperator(), expected_actions=actions, seed=0 + opponent=axl.Cooperator(), expected_actions=actions, seed=0 ) actions = [(C, C)] * 10 self.versus_test( - opponent=axelrod.Cooperator(), expected_actions=actions, seed=1 + opponent=axl.Cooperator(), expected_actions=actions, seed=1 ) @@ -313,9 +311,9 @@ class TestGenericReactiveStrategy(unittest.TestCase): Tests for the Reactive Strategy which. """ - p1 = axelrod.ReactivePlayer(probabilities=(0, 0)) - p2 = axelrod.ReactivePlayer(probabilities=(1, 0)) - p3 = axelrod.ReactivePlayer(probabilities=(1, 0.5)) + p1 = axl.ReactivePlayer(probabilities=(0, 0)) + p2 = axl.ReactivePlayer(probabilities=(1, 0)) + p3 = axl.ReactivePlayer(probabilities=(1, 0.5)) def test_name(self): self.assertEqual(self.p1.name, "Reactive Player: (0, 0)") diff --git a/axelrod/tests/strategies/test_memorytwo.py b/axelrod/tests/strategies/test_memorytwo.py index fbfd14028..17693f06e 100644 --- a/axelrod/tests/strategies/test_memorytwo.py +++ b/axelrod/tests/strategies/test_memorytwo.py @@ -1,15 +1,17 @@ """Tests for the Memorytwo strategies.""" -import random import unittest + +import random + import warnings -import axelrod +import axelrod as axl from axelrod.strategies.memorytwo import MemoryTwoPlayer from .test_player import TestPlayer -C, D = axelrod.Action.C, axelrod.Action.D +C, D = axl.Action.C, axl.Action.D class TestGenericPlayerTwo(unittest.TestCase): @@ -142,7 +144,7 @@ class TestMemoryStochastic(TestPlayer): name = ( "Generic Memory Two Player: (1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1): C" ) - player = axelrod.MemoryTwoPlayer + player = axl.MemoryTwoPlayer expected_classifier = { "memory_depth": 2, # Memory-two Sixteen-Vector "stochastic": False, @@ -154,12 +156,12 @@ class TestMemoryStochastic(TestPlayer): } def test_strategy(self): - axelrod.seed(0) + axl.seed(0) vector = [random.random() for _ in range(16)] actions = [(C, C), (C, C), (D, D), (D, C), (C, C), (C, D), (C, C)] self.versus_test( - opponent=axelrod.CyclerCCD(), + opponent=axl.CyclerCCD(), expected_actions=actions, seed=0, init_kwargs={"sixteen_vector": vector}, @@ -167,7 +169,7 @@ def test_strategy(self): actions = [(C, C), (C, C), (C, D), (D, C), (C, C), (C, D), (C, C)] self.versus_test( - opponent=axelrod.CyclerCCD(), + opponent=axl.CyclerCCD(), expected_actions=actions, seed=1, init_kwargs={"sixteen_vector": vector}, @@ -175,7 +177,7 @@ def test_strategy(self): actions = [(C, C), (C, C), (D, C), (D, D), (C, D), (C, C), (D, C)] self.versus_test( - opponent=axelrod.TitForTat(), + opponent=axl.TitForTat(), expected_actions=actions, seed=0, init_kwargs={"sixteen_vector": vector}, @@ -183,7 +185,7 @@ def test_strategy(self): actions = [(C, C), (C, C), (C, C), (D, C), (D, D), (C, D), (C, C)] self.versus_test( - opponent=axelrod.TitForTat(), + opponent=axl.TitForTat(), expected_actions=actions, seed=1, init_kwargs={"sixteen_vector": vector}, @@ -193,7 +195,7 @@ def test_strategy(self): class TestAON2(TestPlayer): name = "AON2" - player = axelrod.AON2 + player = axl.AON2 expected_classifier = { "memory_depth": 2, "stochastic": False, @@ -207,31 +209,31 @@ class TestAON2(TestPlayer): def test_strategy(self): # tests states 2, 7, 14 and 15 actions = [(C, C), (C, D), (D, C), (D, D), (D, C), (D, D)] - self.versus_test(opponent=axelrod.Alternator(), expected_actions=actions) + self.versus_test(opponent=axl.Alternator(), expected_actions=actions) # tests states 4, 16 and 11 actions = [(C, D), (C, D), (D, C), (D, D), (D, D), (C, C), (C, D)] - self.versus_test(opponent=axelrod.CyclerDDC(), expected_actions=actions) + self.versus_test(opponent=axl.CyclerDDC(), expected_actions=actions) # tests states 3, 5 and 12 actions = [(C, D), (C, C), (D, C), (D, D), (D, D), (C, D)] self.versus_test( - opponent=axelrod.SuspiciousTitForTat(), expected_actions=actions + opponent=axl.SuspiciousTitForTat(), expected_actions=actions ) # tests state 1 actions = [(C, C), (C, C), (C, C), (C, C)] - self.versus_test(opponent=axelrod.Cooperator(), expected_actions=actions) + self.versus_test(opponent=axl.Cooperator(), expected_actions=actions) # tests state 6 actions = [(C, D), (C, C), (D, D), (C, C)] - self.versus_test(opponent=axelrod.CyclerDC(), expected_actions=actions) + self.versus_test(opponent=axl.CyclerDC(), expected_actions=actions) class TestDelayedAON1(TestPlayer): name = "Delayed AON1" - player = axelrod.DelayedAON1 + player = axl.DelayedAON1 expected_classifier = { "memory_depth": 2, "stochastic": False, @@ -245,25 +247,25 @@ class TestDelayedAON1(TestPlayer): def test_strategy_mutually_cooperative(self): # tests states 2, 7, 14 and 11 actions = [(C, C), (C, D), (D, C), (D, D), (C, C), (C, D)] - self.versus_test(opponent=axelrod.Alternator(), expected_actions=actions) + self.versus_test(opponent=axl.Alternator(), expected_actions=actions) # tests states 1, 4 and 8 actions = [(C, D), (C, D), (D, D), (C, C), (C, C), (C, D)] self.versus_test( - opponent=axelrod.Cycler(["D", "D", "D", "C", "C"]), expected_actions=actions + opponent=axl.Cycler(["D", "D", "D", "C", "C"]), expected_actions=actions ) # tests states 3, 5 actions = [(C, D), (C, C), (D, C), (D, D), (C, D)] self.versus_test( - opponent=axelrod.SuspiciousTitForTat(), expected_actions=actions + opponent=axl.SuspiciousTitForTat(), expected_actions=actions ) class TestMEM2(TestPlayer): name = "MEM2" - player = axelrod.MEM2 + player = axl.MEM2 expected_classifier = { "memory_depth": float("inf"), "stochastic": False, @@ -278,18 +280,18 @@ def test_strategy(self): # Start with TFT actions = [(C, C), (C, C)] self.versus_test( - opponent=axelrod.Cooperator(), + opponent=axl.Cooperator(), expected_actions=actions, attrs={"play_as": "TFT", "shift_counter": 1, "alld_counter": 0}, ) actions = [(C, D), (D, D)] self.versus_test( - opponent=axelrod.Defector(), + opponent=axl.Defector(), expected_actions=actions, attrs={"play_as": "TFT", "shift_counter": 1, "alld_counter": 0}, ) # TFTT if C, D and D, C - opponent = axelrod.MockPlayer([D, C, D, D]) + opponent = axl.MockPlayer([D, C, D, D]) actions = [(C, D), (D, C), (C, D), (C, D)] self.versus_test( opponent=opponent, @@ -297,7 +299,7 @@ def test_strategy(self): attrs={"play_as": "TFTT", "shift_counter": 1, "alld_counter": 0}, ) - opponent = axelrod.MockPlayer([D, C, D, D]) + opponent = axl.MockPlayer([D, C, D, D]) actions = [ (C, D), (D, C), diff --git a/axelrod/tests/strategies/test_meta.py b/axelrod/tests/strategies/test_meta.py index 833b45fbb..3d118f1f0 100644 --- a/axelrod/tests/strategies/test_meta.py +++ b/axelrod/tests/strategies/test_meta.py @@ -1,11 +1,13 @@ """Tests for the various Meta strategies.""" -from hypothesis import given, settings -from hypothesis.strategies import integers -import axelrod +import axelrod as axl + from .test_player import TestPlayer -C, D = axelrod.Action.C, axelrod.Action.D +from hypothesis import given, settings +from hypothesis.strategies import integers + +C, D = axl.Action.C, axl.Action.D class TestMetaPlayer(TestPlayer): @@ -14,7 +16,7 @@ class TestMetaPlayer(TestPlayer): the TestPlayer class.""" name = "Meta Player" - player = axelrod.MetaPlayer + player = axl.MetaPlayer expected_classifier = { "memory_depth": float("inf"), "stochastic": True, @@ -76,15 +78,15 @@ def test_clone(self, seed): turns = 10 for op in [ - axelrod.Cooperator(), - axelrod.Defector(), - axelrod.TitForTat(), + axl.Cooperator(), + axl.Defector(), + axl.TitForTat(), ]: player1.reset() player2.reset() for p in [player1, player2]: - axelrod.seed(seed) - m = axelrod.Match((p, op), turns=turns) + axl.seed(seed) + m = axl.Match((p, op), turns=turns) m.play() self.assertEqual(len(player1.history), turns) self.assertEqual(player1.history, player2.history) @@ -93,7 +95,7 @@ def test_clone(self, seed): class TestMetaMajority(TestMetaPlayer): name = "Meta Majority" - player = axelrod.MetaMajority + player = axl.MetaMajority expected_classifier = { "memory_depth": float("inf"), # Long memory "stochastic": True, @@ -106,22 +108,22 @@ class TestMetaMajority(TestMetaPlayer): def test_strategy(self): - P1 = axelrod.MetaMajority() - P2 = axelrod.Player() + P1 = axl.MetaMajority() + P2 = axl.Player() # With more cooperators on the team than defectors, we should cooperate. - P1.team = [axelrod.Cooperator(), axelrod.Cooperator(), axelrod.Defector()] + P1.team = [axl.Cooperator(), axl.Cooperator(), axl.Defector()] self.assertEqual(P1.strategy(P2), C) # With more defectors, we should defect. - P1.team = [axelrod.Cooperator(), axelrod.Defector(), axelrod.Defector()] + P1.team = [axl.Cooperator(), axl.Defector(), axl.Defector()] self.assertEqual(P1.strategy(P2), D) class TestMetaMinority(TestMetaPlayer): name = "Meta Minority" - player = axelrod.MetaMinority + player = axl.MetaMinority expected_classifier = { "memory_depth": float("inf"), # Long memory "stochastic": True, @@ -133,28 +135,28 @@ class TestMetaMinority(TestMetaPlayer): } def test_team(self): - team = [axelrod.Cooperator] + team = [axl.Cooperator] player = self.player(team=team) self.assertEqual(len(player.team), 1) def test_strategy(self): - P1 = axelrod.MetaMinority() - P2 = axelrod.Player() + P1 = axl.MetaMinority() + P2 = axl.Player() # With more cooperators on the team, we should defect. - P1.team = [axelrod.Cooperator(), axelrod.Cooperator(), axelrod.Defector()] + P1.team = [axl.Cooperator(), axl.Cooperator(), axl.Defector()] self.assertEqual(P1.strategy(P2), D) # With defectors in the majority, we will cooperate here. - P1.team = [axelrod.Cooperator(), axelrod.Defector(), axelrod.Defector()] + P1.team = [axl.Cooperator(), axl.Defector(), axl.Defector()] self.assertEqual(P1.strategy(P2), C) class TestNiceMetaWinner(TestMetaPlayer): name = "Nice Meta Winner" - player = axelrod.NiceMetaWinner + player = axl.NiceMetaWinner expected_classifier = { "memory_depth": float("inf"), # Long memory "stochastic": True, @@ -166,8 +168,8 @@ class TestNiceMetaWinner(TestMetaPlayer): } def test_strategy(self): - P1 = axelrod.NiceMetaWinner(team=[axelrod.Cooperator, axelrod.Defector]) - P2 = axelrod.Player() + P1 = axl.NiceMetaWinner(team=[axl.Cooperator, axl.Defector]) + P2 = axl.Player() # This meta player will simply choose the strategy with the highest # current score. @@ -183,20 +185,20 @@ def test_strategy(self): P1.team[1].score = 1 self.assertEqual(P1.strategy(P2), C) - opponent = axelrod.Cooperator() - player = axelrod.NiceMetaWinner(team=[axelrod.Cooperator, axelrod.Defector]) + opponent = axl.Cooperator() + player = axl.NiceMetaWinner(team=[axl.Cooperator, axl.Defector]) for _ in range(5): player.play(opponent) self.assertEqual(player.history[-1], C) - opponent = axelrod.Defector() - player = axelrod.NiceMetaWinner(team=[axelrod.Defector]) + opponent = axl.Defector() + player = axl.NiceMetaWinner(team=[axl.Defector]) for _ in range(20): player.play(opponent) self.assertEqual(player.history[-1], D) - opponent = axelrod.Defector() - player = axelrod.MetaWinner(team=[axelrod.Cooperator, axelrod.Defector]) + opponent = axl.Defector() + player = axl.MetaWinner(team=[axl.Cooperator, axl.Defector]) for _ in range(20): player.play(opponent) self.assertEqual(player.history[-1], D) @@ -204,7 +206,7 @@ def test_strategy(self): class TestNiceMetaWinnerEnsemble(TestMetaPlayer): name = "Nice Meta Winner Ensemble" - player = axelrod.NiceMetaWinnerEnsemble + player = axl.NiceMetaWinnerEnsemble expected_classifier = { "memory_depth": float("inf"), # Long memory "stochastic": True, @@ -218,22 +220,22 @@ class TestNiceMetaWinnerEnsemble(TestMetaPlayer): def test_strategy(self): actions = [(C, C)] * 8 self.versus_test( - opponent=axelrod.Cooperator(), + opponent=axl.Cooperator(), expected_actions=actions, - init_kwargs={"team": [axelrod.Cooperator, axelrod.Defector]}, + init_kwargs={"team": [axl.Cooperator, axl.Defector]}, ) actions = [(C, D)] + [(D, D)] * 7 self.versus_test( - opponent=axelrod.Defector(), + opponent=axl.Defector(), expected_actions=actions, - init_kwargs={"team": [axelrod.Cooperator, axelrod.Defector]}, + init_kwargs={"team": [axl.Cooperator, axl.Defector]}, ) class TestMetaHunter(TestMetaPlayer): name = "Meta Hunter" - player = axelrod.MetaHunter + player = axl.MetaHunter expected_classifier = { "memory_depth": float("inf"), # Long memory "stochastic": False, @@ -248,22 +250,22 @@ def test_strategy(self): # We are not using the Cooperator Hunter here, so this should lead to # cooperation. actions = [(C, C)] * 5 - self.versus_test(opponent=axelrod.Cooperator(), expected_actions=actions) + self.versus_test(opponent=axl.Cooperator(), expected_actions=actions) # After long histories tit-for-tat should come into play. - opponent = axelrod.MockPlayer([C] * 100 + [D]) + opponent = axl.MockPlayer([C] * 100 + [D]) actions = [(C, C)] * 100 + [(C, D)] + [(D, C)] self.versus_test(opponent=opponent, expected_actions=actions) actions = [(C, C)] * 102 - self.versus_test(opponent=axelrod.Cooperator(), expected_actions=actions) + self.versus_test(opponent=axl.Cooperator(), expected_actions=actions) # All these others, however, should trigger a defection for the hunter. actions = [(C, D), (C, D), (C, D), (C, D), (D, D)] - self.versus_test(opponent=axelrod.Defector(), expected_actions=actions) + self.versus_test(opponent=axl.Defector(), expected_actions=actions) actions = [(C, C), (C, D), (C, C), (C, D), (C, C), (C, D), (D, C)] - self.versus_test(opponent=axelrod.Alternator(), expected_actions=actions) + self.versus_test(opponent=axl.Alternator(), expected_actions=actions) actions = [ (C, C), @@ -276,12 +278,12 @@ def test_strategy(self): (C, D), (D, C), ] - self.versus_test(opponent=axelrod.CyclerCCCD(), expected_actions=actions) + self.versus_test(opponent=axl.CyclerCCCD(), expected_actions=actions) class TestMetaHunterAggressive(TestMetaPlayer): name = "Meta Hunter Aggressive" - player = axelrod.MetaHunterAggressive + player = axl.MetaHunterAggressive expected_classifier = { "memory_depth": float("inf"), # Long memory "stochastic": False, @@ -296,14 +298,14 @@ def test_strategy(self): # We are using CooperatorHunter here, so this should lead to # defection actions = [(C, C)] * 4 + [(D, C)] - self.versus_test(opponent=axelrod.Cooperator(), expected_actions=actions) + self.versus_test(opponent=axl.Cooperator(), expected_actions=actions) # All these others, however, should trigger a defection for the hunter. actions = [(C, D), (C, D), (C, D), (C, D), (D, D)] - self.versus_test(opponent=axelrod.Defector(), expected_actions=actions) + self.versus_test(opponent=axl.Defector(), expected_actions=actions) actions = [(C, C), (C, D), (C, C), (C, D), (C, C), (C, D), (D, C)] - self.versus_test(opponent=axelrod.Alternator(), expected_actions=actions) + self.versus_test(opponent=axl.Alternator(), expected_actions=actions) actions = [ (C, C), @@ -316,7 +318,7 @@ def test_strategy(self): (C, D), (D, C), ] - self.versus_test(opponent=axelrod.CyclerCCCD(), expected_actions=actions) + self.versus_test(opponent=axl.CyclerCCCD(), expected_actions=actions) # To test the TFT action of the strategy after 100 turns, we need to # remove two of the hunters from its team. @@ -324,13 +326,13 @@ def test_strategy(self): # without triggering one of the hunters in the default team. As at # 16-Mar-2017, none of the strategies in the library does so. team = [ - axelrod.DefectorHunter, - axelrod.AlternatorHunter, - axelrod.RandomHunter, - axelrod.CycleHunter, - axelrod.EventualCycleHunter, + axl.DefectorHunter, + axl.AlternatorHunter, + axl.RandomHunter, + axl.CycleHunter, + axl.EventualCycleHunter, ] - opponent = axelrod.MockPlayer([C] * 100 + [D]) + opponent = axl.MockPlayer([C] * 100 + [D]) actions = [(C, C)] * 100 + [(C, D), (D, C)] self.versus_test( opponent=opponent, expected_actions=actions, init_kwargs={"team": team} @@ -339,7 +341,7 @@ def test_strategy(self): class TestMetaMajorityMemoryOne(TestMetaPlayer): name = "Meta Majority Memory One" - player = axelrod.MetaMajorityMemoryOne + player = axl.MetaMajorityMemoryOne expected_classifier = { "memory_depth": float("inf"), # Long memory "stochastic": True, @@ -352,12 +354,12 @@ class TestMetaMajorityMemoryOne(TestMetaPlayer): def test_strategy(self): actions = [(C, C), (C, D), (C, C), (C, D), (D, C)] - self.versus_test(opponent=axelrod.Alternator(), expected_actions=actions) + self.versus_test(opponent=axl.Alternator(), expected_actions=actions) class TestMetaMajorityFiniteMemory(TestMetaPlayer): name = "Meta Majority Finite Memory" - player = axelrod.MetaMajorityFiniteMemory + player = axl.MetaMajorityFiniteMemory expected_classifier = { "memory_depth": float("inf"), # Long memory "stochastic": True, @@ -370,12 +372,12 @@ class TestMetaMajorityFiniteMemory(TestMetaPlayer): def test_strategy(self): actions = [(C, C), (C, D), (D, C), (C, D), (D, C)] - self.versus_test(opponent=axelrod.Alternator(), expected_actions=actions) + self.versus_test(opponent=axl.Alternator(), expected_actions=actions) class TestMetaMajorityLongMemory(TestMetaPlayer): name = "Meta Majority Long Memory" - player = axelrod.MetaMajorityLongMemory + player = axl.MetaMajorityLongMemory expected_classifier = { "memory_depth": float("inf"), # Long memory "stochastic": True, @@ -389,18 +391,18 @@ class TestMetaMajorityLongMemory(TestMetaPlayer): def test_strategy(self): actions = [(C, C), (C, D), (D, C), (C, D), (D, C)] self.versus_test( - opponent=axelrod.Alternator(), expected_actions=actions, seed=0 + opponent=axl.Alternator(), expected_actions=actions, seed=0 ) actions = [(C, C), (C, D), (D, C), (C, D), (D, C)] self.versus_test( - opponent=axelrod.Alternator(), expected_actions=actions, seed=1 + opponent=axl.Alternator(), expected_actions=actions, seed=1 ) class TestMetaWinnerMemoryOne(TestMetaPlayer): name = "Meta Winner Memory One" - player = axelrod.MetaWinnerMemoryOne + player = axl.MetaWinnerMemoryOne expected_classifier = { "memory_depth": float("inf"), # Long memory "stochastic": True, @@ -413,12 +415,12 @@ class TestMetaWinnerMemoryOne(TestMetaPlayer): def test_strategy(self): actions = [(C, C), (C, D), (C, C), (D, D), (D, C)] - self.versus_test(opponent=axelrod.Alternator(), expected_actions=actions) + self.versus_test(opponent=axl.Alternator(), expected_actions=actions) class TestMetaWinnerFiniteMemory(TestMetaPlayer): name = "Meta Winner Finite Memory" - player = axelrod.MetaWinnerFiniteMemory + player = axl.MetaWinnerFiniteMemory expected_classifier = { "memory_depth": float("inf"), # Long memory "stochastic": True, @@ -431,12 +433,12 @@ class TestMetaWinnerFiniteMemory(TestMetaPlayer): def test_strategy(self): actions = [(C, C), (C, D), (C, C), (C, D), (D, C)] - self.versus_test(opponent=axelrod.Alternator(), expected_actions=actions) + self.versus_test(opponent=axl.Alternator(), expected_actions=actions) class TestMetaWinnerLongMemory(TestMetaPlayer): name = "Meta Winner Long Memory" - player = axelrod.MetaWinnerLongMemory + player = axl.MetaWinnerLongMemory expected_classifier = { "memory_depth": float("inf"), # Long memory "stochastic": True, @@ -449,12 +451,12 @@ class TestMetaWinnerLongMemory(TestMetaPlayer): def test_strategy(self): actions = [(C, C), (C, D), (C, C), (D, D), (D, C)] - self.versus_test(opponent=axelrod.Alternator(), expected_actions=actions) + self.versus_test(opponent=axl.Alternator(), expected_actions=actions) class TestMetaWinnerDeterministic(TestMetaPlayer): name = "Meta Winner Deterministic" - player = axelrod.MetaWinnerDeterministic + player = axl.MetaWinnerDeterministic expected_classifier = { "memory_depth": float("inf"), # Long memory "stochastic": False, @@ -467,12 +469,12 @@ class TestMetaWinnerDeterministic(TestMetaPlayer): def test_strategy(self): actions = [(C, C), (C, D), (C, C), (D, D), (D, C)] - self.versus_test(opponent=axelrod.Alternator(), expected_actions=actions) + self.versus_test(opponent=axl.Alternator(), expected_actions=actions) class TestMetaWinnerStochastic(TestMetaPlayer): name = "Meta Winner Stochastic" - player = axelrod.MetaWinnerStochastic + player = axl.MetaWinnerStochastic expected_classifier = { "memory_depth": float("inf"), # Long memory "stochastic": True, @@ -485,13 +487,13 @@ class TestMetaWinnerStochastic(TestMetaPlayer): def test_strategy(self): actions = [(C, C), (C, D), (C, C), (D, D), (D, C)] - self.versus_test(opponent=axelrod.Alternator(), expected_actions=actions) + self.versus_test(opponent=axl.Alternator(), expected_actions=actions) class TestMetaMixer(TestMetaPlayer): name = "Meta Mixer" - player = axelrod.MetaMixer + player = axl.MetaMixer expected_classifier = { "inspects_source": False, "long_run_time": True, @@ -504,22 +506,22 @@ class TestMetaMixer(TestMetaPlayer): def test_strategy(self): - team = [axelrod.TitForTat, axelrod.Cooperator, axelrod.Grudger] + team = [axl.TitForTat, axl.Cooperator, axl.Grudger] distribution = [0.2, 0.5, 0.3] - P1 = axelrod.MetaMixer(team=team, distribution=distribution) - P2 = axelrod.Cooperator() + P1 = axl.MetaMixer(team=team, distribution=distribution) + P2 = axl.Cooperator() actions = [(C, C)] * 20 self.versus_test( - opponent=axelrod.Cooperator(), + opponent=axl.Cooperator(), expected_actions=actions, init_kwargs={"team": team, "distribution": distribution}, ) - team.append(axelrod.Defector) + team.append(axl.Defector) distribution = [0.2, 0.5, 0.3, 0] # If add a defector but does not occur self.versus_test( - opponent=axelrod.Cooperator(), + opponent=axl.Cooperator(), expected_actions=actions, init_kwargs={"team": team, "distribution": distribution}, ) @@ -527,24 +529,24 @@ def test_strategy(self): distribution = [0, 0, 0, 1] # If defector is only one that is played actions = [(D, C)] * 20 self.versus_test( - opponent=axelrod.Cooperator(), + opponent=axl.Cooperator(), expected_actions=actions, init_kwargs={"team": team, "distribution": distribution}, ) def test_raise_error_in_distribution(self): - team = [axelrod.TitForTat, axelrod.Cooperator, axelrod.Grudger] + team = [axl.TitForTat, axl.Cooperator, axl.Grudger] distribution = [0.2, 0.5, 0.5] # Not a valid probability distribution - player = axelrod.MetaMixer(team=team, distribution=distribution) - opponent = axelrod.Cooperator() + player = axl.MetaMixer(team=team, distribution=distribution) + opponent = axl.Cooperator() self.assertRaises(ValueError, player.strategy, opponent) class TestNMWEDeterministic(TestMetaPlayer): name = "NMWE Deterministic" - player = axelrod.NMWEDeterministic + player = axl.NMWEDeterministic expected_classifier = { "memory_depth": float("inf"), # Long memory "stochastic": True, @@ -561,12 +563,12 @@ def classifier_test(self, expected_class_classifier=None): def test_strategy(self): actions = [(C, C), (C, D), (D, C), (D, D), (D, C)] - self.versus_test(opponent=axelrod.Alternator(), expected_actions=actions) + self.versus_test(opponent=axl.Alternator(), expected_actions=actions) class TestNMWEStochastic(TestMetaPlayer): name = "NMWE Stochastic" - player = axelrod.NMWEStochastic + player = axl.NMWEStochastic expected_classifier = { "memory_depth": float("inf"), # Long memory "stochastic": True, @@ -579,13 +581,13 @@ class TestNMWEStochastic(TestMetaPlayer): def test_strategy(self): actions = [(C, C), (C, D), (C, C), (C, D), (D, C)] - self.versus_test(opponent=axelrod.Alternator(), expected_actions=actions, + self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=20) class TestNMWEFiniteMemory(TestMetaPlayer): name = "NMWE Finite Memory" - player = axelrod.NMWEFiniteMemory + player = axl.NMWEFiniteMemory expected_classifier = { "memory_depth": float("inf"), # Long memory "stochastic": True, @@ -598,12 +600,12 @@ class TestNMWEFiniteMemory(TestMetaPlayer): def test_strategy(self): actions = [(C, C), (C, D), (D, C), (D, D), (D, C)] - self.versus_test(opponent=axelrod.Alternator(), expected_actions=actions) + self.versus_test(opponent=axl.Alternator(), expected_actions=actions) class TestNMWELongMemory(TestMetaPlayer): name = "NMWE Long Memory" - player = axelrod.NMWELongMemory + player = axl.NMWELongMemory expected_classifier = { "memory_depth": float("inf"), # Long memory "stochastic": True, @@ -616,14 +618,14 @@ class TestNMWELongMemory(TestMetaPlayer): def test_strategy(self): actions = [(C, C), (C, D), (C, C), (D, D), (D, C)] - self.versus_test(opponent=axelrod.Alternator(), + self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=10) class TestNMWEMemoryOne(TestMetaPlayer): name = "NMWE Memory One" - player = axelrod.NMWEMemoryOne + player = axl.NMWEMemoryOne expected_classifier = { "memory_depth": float("inf"), # Long memory "stochastic": True, @@ -636,13 +638,13 @@ class TestNMWEMemoryOne(TestMetaPlayer): def test_strategy(self): actions = [(C, C), (C, D), (C, C), (D, D), (D, C)] - self.versus_test(opponent=axelrod.Alternator(), expected_actions=actions) + self.versus_test(opponent=axl.Alternator(), expected_actions=actions) class TestMemoryDecay(TestPlayer): name = "Memory Decay: 0.1, 0.03, -2, 1, Tit For Tat, 15" - player = axelrod.MemoryDecay + player = axl.MemoryDecay expected_classifier = { "memory_depth": float("inf"), "long_run_time": False, @@ -655,30 +657,30 @@ class TestMemoryDecay(TestPlayer): def test_strategy(self): # Test TitForTat behavior in first 15 turns - opponent = axelrod.Cooperator() + opponent = axl.Cooperator() actions = list([(C, C)]) * 15 self.versus_test(opponent, expected_actions=actions) - opponent = axelrod.Defector() + opponent = axl.Defector() actions = [(C, D)] + list([(D, D)]) * 14 self.versus_test(opponent, expected_actions=actions) - opponent = axelrod.Alternator() + opponent = axl.Alternator() actions = [(C, C)] + [(C, D), (D, C)] * 7 self.versus_test(opponent, expected_actions=actions) opponent_actions = [C, D, D, C, D, C, C, D, C, D, D, C, C, D, D] - opponent = axelrod.MockPlayer(actions=opponent_actions) + opponent = axl.MockPlayer(actions=opponent_actions) mem_actions = [C, C, D, D, C, D, C, C, D, C, D, D, C, C, D] actions = list(zip(mem_actions, opponent_actions)) self.versus_test(opponent, expected_actions=actions) - opponent = axelrod.Random() + opponent = axl.Random() actions = [(C, D), (D, D), (D, C), (C, C), (C, D), (D, C)] self.versus_test(opponent, expected_actions=actions, seed=0) # Test net-cooperation-score (NCS) based decisions in subsequent turns - opponent = axelrod.Cooperator() + opponent = axl.Cooperator() actions = [(C, C)] * 15 + [(C, C)] self.versus_test( opponent, @@ -687,7 +689,7 @@ def test_strategy(self): init_kwargs={"memory": [D] * 5 + [C] * 10}, ) - opponent = axelrod.Cooperator() + opponent = axl.Cooperator() actions = [(C, C)] * 15 + [(C, C)] self.versus_test( opponent, @@ -697,31 +699,31 @@ def test_strategy(self): ) # Test alternative starting strategies - opponent = axelrod.Cooperator() + opponent = axl.Cooperator() actions = list([(D, C)]) * 15 self.versus_test( opponent, expected_actions=actions, - init_kwargs={"start_strategy": axelrod.Defector}, + init_kwargs={"start_strategy": axl.Defector}, ) - opponent = axelrod.Cooperator() + opponent = axl.Cooperator() actions = list([(C, C)]) * 15 self.versus_test( opponent, expected_actions=actions, - init_kwargs={"start_strategy": axelrod.Cooperator}, + init_kwargs={"start_strategy": axl.Cooperator}, ) - opponent = axelrod.Cooperator() + opponent = axl.Cooperator() actions = [(C, C)] + list([(D, C), (C, C)]) * 7 self.versus_test( opponent, expected_actions=actions, - init_kwargs={"start_strategy": axelrod.Alternator}, + init_kwargs={"start_strategy": axl.Alternator}, ) - opponent = axelrod.Defector() + opponent = axl.Defector() actions = [(C, D)] * 7 + [(D, D)] self.versus_test( opponent, @@ -729,7 +731,7 @@ def test_strategy(self): seed=4, init_kwargs={ "memory": [C] * 12, - "start_strategy": axelrod.Defector, + "start_strategy": axl.Defector, "start_strategy_duration": 0, }, ) diff --git a/axelrod/tests/strategies/test_mindcontrol.py b/axelrod/tests/strategies/test_mindcontrol.py index 56e948b6a..3088c1aca 100644 --- a/axelrod/tests/strategies/test_mindcontrol.py +++ b/axelrod/tests/strategies/test_mindcontrol.py @@ -1,16 +1,16 @@ """Tests for mind controllers and other wizards.""" -import axelrod +import axelrod as axl from .test_player import TestPlayer -C, D = axelrod.Action.C, axelrod.Action.D +C, D = axl.Action.C, axl.Action.D class TestMindController(TestPlayer): name = "Mind Controller" - player = axelrod.MindController + player = axl.MindController expected_classifier = { "memory_depth": -10, "stochastic": False, @@ -24,24 +24,24 @@ class TestMindController(TestPlayer): def test_strategy(self): """ Will always make opponent cooperate """ - p1 = axelrod.MindController() - p2 = axelrod.Cooperator() + p1 = axl.MindController() + p2 = axl.Cooperator() self.assertEqual(p1.strategy(p2), D) self.assertEqual(p2.strategy(p1), C) def test_vs_defect(self): """ Will force even defector to cooperate """ - p1 = axelrod.MindController() - p2 = axelrod.Defector() + p1 = axl.MindController() + p2 = axl.Defector() self.assertEqual(p1.strategy(p2), D) self.assertEqual(p2.strategy(p1), C) def test_vs_grudger(self): """ Will force even Grudger to forget its grudges""" - p1 = axelrod.MindController() - p2 = axelrod.Grudger() + p1 = axl.MindController() + p2 = axl.Grudger() for _ in range(4): p1.history.append(D, C) p2.history.append(C, D) @@ -52,7 +52,7 @@ def test_vs_grudger(self): class TestMindWarper(TestMindController): name = "Mind Warper" - player = axelrod.MindWarper + player = axl.MindWarper expected_classifier = { "memory_depth": -10, "stochastic": False, @@ -69,7 +69,7 @@ def test_setattr(self): def test_strategy(self): player = self.player() - opponent = axelrod.Defector() + opponent = axl.Defector() play1 = player.strategy(opponent) play2 = opponent.strategy(player) self.assertEqual(play1, D) @@ -79,7 +79,7 @@ def test_strategy(self): class TestMindBender(TestMindController): name = "Mind Bender" - player = axelrod.MindBender + player = axl.MindBender expected_classifier = { "memory_depth": -10, "stochastic": False, @@ -92,7 +92,7 @@ class TestMindBender(TestMindController): def test_strategy(self): player = self.player() - opponent = axelrod.Defector() + opponent = axl.Defector() play1 = player.strategy(opponent) play2 = opponent.strategy(player) self.assertEqual(play1, D) diff --git a/axelrod/tests/strategies/test_mindreader.py b/axelrod/tests/strategies/test_mindreader.py index 670c0556a..ffd996022 100644 --- a/axelrod/tests/strategies/test_mindreader.py +++ b/axelrod/tests/strategies/test_mindreader.py @@ -1,17 +1,17 @@ """Tests for the Mindreader strategy.""" -import axelrod +import axelrod as axl from axelrod._strategy_utils import simulate_match from .test_player import TestPlayer -C, D = axelrod.Action.C, axelrod.Action.D +C, D = axl.Action.C, axl.Action.D class TestMindReader(TestPlayer): name = "Mind Reader" - player = axelrod.MindReader + player = axl.MindReader expected_classifier = { "memory_depth": float("inf"), "stochastic": False, @@ -30,40 +30,40 @@ def test_strategy(self): """ Will defect against nice strategies """ - p1 = axelrod.MindReader() - p2 = axelrod.Cooperator() + p1 = axl.MindReader() + p2 = axl.Cooperator() self.assertEqual(p1.strategy(p2), D) def test_vs_defect(self): """ Will defect against pure defecting strategies """ - p1 = axelrod.MindReader() - p2 = axelrod.Defector() + p1 = axl.MindReader() + p2 = axl.Defector() self.assertEqual(p1.strategy(p2), D) def test_vs_grudger(self): """ Will keep nasty strategies happy if it can """ - p1 = axelrod.MindReader() - p2 = axelrod.Grudger() + p1 = axl.MindReader() + p2 = axl.Grudger() self.assertEqual(p1.strategy(p2), C) def test_vs_tit_for_tat(self): """ Will keep nasty strategies happy if it can """ - p1 = axelrod.MindReader() - p2 = axelrod.TitForTat() + p1 = axl.MindReader() + p2 = axl.TitForTat() self.assertEqual(p1.strategy(p2), C) def test_simulate_matches(self): """ Simulates a number of matches """ - p1 = axelrod.MindReader() - p2 = axelrod.Grudger() + p1 = axl.MindReader() + p2 = axl.Grudger() simulate_match(p1, p2, C, 4) self.assertEqual(p2.history, [C, C, C, C]) @@ -71,8 +71,8 @@ def test_history_is_same(self): """ Checks that the history is not altered by the player """ - p1 = axelrod.MindReader() - p2 = axelrod.Grudger() + p1 = axl.MindReader() + p2 = axl.Grudger() p1.history.append(C, C) p1.history.append(C, D) p2.history.append(C, C) @@ -83,21 +83,21 @@ def test_history_is_same(self): def test_vs_geller(self): """Ensures that a recursion error does not occur """ - p1 = axelrod.MindReader() - p2 = axelrod.Geller() + p1 = axl.MindReader() + p2 = axl.Geller() p1.strategy(p2) p2.strategy(p1) def test_init(self): """Tests for init method """ - p1 = axelrod.MindReader() + p1 = axl.MindReader() self.assertEqual(p1.history, []) class TestProtectedMindReader(TestPlayer): name = "Protected Mind Reader" - player = axelrod.ProtectedMindReader + player = axl.ProtectedMindReader expected_classifier = { "memory_depth": float("inf"), "stochastic": False, @@ -116,24 +116,24 @@ def test_strategy(self): """ Will defect against nice strategies """ - p1 = axelrod.ProtectedMindReader() - p2 = axelrod.Cooperator() + p1 = axl.ProtectedMindReader() + p2 = axl.Cooperator() self.assertEqual(p1.strategy(p2), D) def test_vs_defect(self): """ Will defect against pure defecting strategies """ - p1 = axelrod.ProtectedMindReader() - p2 = axelrod.Defector() + p1 = axl.ProtectedMindReader() + p2 = axl.Defector() self.assertEqual(p1.strategy(p2), D) def tests_protected(self): """Ensures that no other player can alter its strategy """ - p1 = axelrod.ProtectedMindReader() - p2 = axelrod.MindController() - P3 = axelrod.Cooperator() + p1 = axl.ProtectedMindReader() + p2 = axl.MindController() + P3 = axl.Cooperator() p2.strategy(p1) self.assertEqual(p1.strategy(P3), D) @@ -141,7 +141,7 @@ def tests_protected(self): class TestMirrorMindReader(TestPlayer): name = "Mirror Mind Reader" - player = axelrod.MirrorMindReader + player = axl.MirrorMindReader expected_classifier = { "memory_depth": float("inf"), "stochastic": False, @@ -157,16 +157,16 @@ def test_foil_inspection_strategy(self): self.assertEqual(player.foil_strategy_inspection(), C) def test_strategy(self): - p1 = axelrod.MirrorMindReader() - p2 = axelrod.Cooperator() + p1 = axl.MirrorMindReader() + p2 = axl.Cooperator() self.assertEqual(p1.strategy(p2), C) def test_vs_defector(self): - p1 = axelrod.MirrorMindReader() - p2 = axelrod.Defector() + p1 = axl.MirrorMindReader() + p2 = axl.Defector() self.assertEqual(p1.strategy(p2), D) def test_nice_with_itself(self): - p1 = axelrod.MirrorMindReader() - p2 = axelrod.MirrorMindReader() + p1 = axl.MirrorMindReader() + p2 = axl.MirrorMindReader() self.assertEqual(p1.strategy(p2), C) diff --git a/axelrod/tests/strategies/test_mutual.py b/axelrod/tests/strategies/test_mutual.py index 0cddf1128..8ba512912 100644 --- a/axelrod/tests/strategies/test_mutual.py +++ b/axelrod/tests/strategies/test_mutual.py @@ -1,15 +1,16 @@ """Tests for strategies Desperate, Hopeless, Willing, and Grim.""" -import axelrod + +import axelrod as axl from .test_player import TestPlayer -C, D = axelrod.Action.C, axelrod.Action.D +C, D = axl.Action.C, axl.Action.D class TestDesperate(TestPlayer): name = "Desperate" - player = axelrod.Desperate + player = axl.Desperate expected_classifier = { "memory_depth": 1, "long_run_time": False, @@ -22,33 +23,33 @@ class TestDesperate(TestPlayer): def test_strategy(self): # Our Player (Desperate) vs Cooperator SEED --> 1 - opponent = axelrod.Cooperator() + opponent = axl.Cooperator() opponent_actions = [C] * 5 actions = [(C, C), (D, C), (D, C), (D, C), (D, C)] self.versus_test(opponent, expected_actions=actions, seed=1) # Our Player (Desperate) vs Cooperator SEED --> 2 - opponent = axelrod.Cooperator() + opponent = axl.Cooperator() actions = [(D, C), (D, C), (D, C), (D, C), (D, C)] self.versus_test(opponent, expected_actions=actions, seed=2) # Our Player (Desperate) vs Defector SEED --> 1 - opponent = axelrod.Defector() + opponent = axl.Defector() actions = [(C, D), (D, D), (C, D), (D, D), (C, D)] self.versus_test(opponent, expected_actions=actions, seed=1) # Our Player (Desperate) vs Defector SEED --> 2 - opponent = axelrod.Defector() + opponent = axl.Defector() actions = [(D, D), (C, D), (D, D), (C, D), (D, D)] self.versus_test(opponent, expected_actions=actions, seed=2) # Our Player (Desperate) vs Alternator SEED --> 1 - opponent = axelrod.Alternator() + opponent = axl.Alternator() actions = [(C, C), (D, D), (C, C), (D, D), (C, C)] self.versus_test(opponent, expected_actions=actions, seed=1) # Our Player (Desperate) vs Alternator SEED --> 2 - opponent = axelrod.Alternator() + opponent = axl.Alternator() actions = [(D, C), (D, D), (C, C), (D, D), (C, C)] self.versus_test(opponent, expected_actions=actions, seed=2) @@ -56,7 +57,7 @@ def test_strategy(self): class TestHopeless(TestPlayer): name = "Hopeless" - player = axelrod.Hopeless + player = axl.Hopeless expected_classifier = { "memory_depth": 1, "long_run_time": False, @@ -69,33 +70,33 @@ class TestHopeless(TestPlayer): def test_strategy(self): # Our Player (Hopeless) vs Cooperator SEED --> 1 - opponent = axelrod.Cooperator() + opponent = axl.Cooperator() opponent_actions = [C] * 5 actions = [(C, C), (D, C), (C, C), (D, C), (C, C)] self.versus_test(opponent, expected_actions=actions, seed=1) # Our Player (Hopeless) vs Cooperator SEED --> 2 - opponent = axelrod.Cooperator() + opponent = axl.Cooperator() actions = [(D, C), (C, C), (D, C), (C, C), (D, C)] self.versus_test(opponent, expected_actions=actions, seed=2) # Our Player (Hopeless) vs Defector SEED --> 1 - opponent = axelrod.Defector() + opponent = axl.Defector() actions = [(C, D), (C, D), (C, D), (C, D), (C, D)] self.versus_test(opponent, expected_actions=actions, seed=1) # Our Player (Hopeless) vs Defector SEED --> 2 - opponent = axelrod.Defector() + opponent = axl.Defector() actions = [(D, D), (C, D), (C, D), (C, D), (C, D)] self.versus_test(opponent, expected_actions=actions, seed=2) # Our Player (Hopeless) vs Alternator SEED --> 1 - opponent = axelrod.Alternator() + opponent = axl.Alternator() actions = [(C, C), (D, D), (C, C), (D, D), (C, C)] self.versus_test(opponent, expected_actions=actions, seed=1) # Our Player (Hopeless) vs Alternator SEED --> 2 - opponent = axelrod.Alternator() + opponent = axl.Alternator() actions = [(D, C), (C, D), (C, C), (D, D), (C, C)] self.versus_test(opponent, expected_actions=actions, seed=2) @@ -103,7 +104,7 @@ def test_strategy(self): class TestWilling(TestPlayer): name = "Willing" - player = axelrod.Willing + player = axl.Willing expected_classifier = { "memory_depth": 1, "long_run_time": False, @@ -116,32 +117,32 @@ class TestWilling(TestPlayer): def test_strategy(self): # Our Player (Willing) vs Cooperator SEED --> 1 - opponent = axelrod.Cooperator() + opponent = axl.Cooperator() opponent_actions = [C] * 5 actions = [(C, C), (C, C), (C, C), (C, C), (C, C)] self.versus_test(opponent, expected_actions=actions, seed=1) # Our Player (Willing) vs Cooperator SEED --> 2 - opponent = axelrod.Cooperator() + opponent = axl.Cooperator() actions = [(D, C), (C, C), (C, C), (C, C), (C, C)] self.versus_test(opponent, expected_actions=actions, seed=2) # Our Player (Willing) vs Defector SEED --> 1 - opponent = axelrod.Defector() + opponent = axl.Defector() actions = [(C, D), (C, D), (C, D), (C, D), (C, D)] self.versus_test(opponent, expected_actions=actions, seed=1) # Our Player (Willing) vs Defector SEED --> 2 - opponent = axelrod.Defector() + opponent = axl.Defector() actions = [(D, D), (D, D), (D, D), (D, D), (D, D)] self.versus_test(opponent, expected_actions=actions, seed=2) # Our Player (Willing) vs Alternator SEED --> 1 - opponent = axelrod.Alternator() + opponent = axl.Alternator() actions = [(C, C), (C, D), (C, C), (C, D), (C, C)] self.versus_test(opponent, expected_actions=actions, seed=1) # Our Player (Willing) vs Alternator SEED --> 2 - opponent = axelrod.Alternator() + opponent = axl.Alternator() actions = [(D, C), (C, D), (C, C), (C, D), (C, C)] self.versus_test(opponent, expected_actions=actions, seed=2) diff --git a/axelrod/tests/strategies/test_negation.py b/axelrod/tests/strategies/test_negation.py index da2df5289..8c7542aaa 100644 --- a/axelrod/tests/strategies/test_negation.py +++ b/axelrod/tests/strategies/test_negation.py @@ -1,16 +1,16 @@ """Tests for the Neg Strategy""" -import axelrod +import axelrod as axl from .test_player import TestPlayer -C, D = axelrod.Action.C, axelrod.Action.D +C, D = axl.Action.C, axl.Action.D class TestNegation(TestPlayer): name = "Negation" - player = axelrod.Negation + player = axl.Negation expected_classifier = { "memory_depth": 1, "stochastic": True, @@ -25,15 +25,15 @@ def test_strategy(self): # First move is random. actions = [(C, C), (D, D), (C, C)] self.versus_test( - opponent=axelrod.Alternator(), expected_actions=actions, seed=1 + opponent=axl.Alternator(), expected_actions=actions, seed=1 ) actions = [(D, C), (D, D), (C, C)] self.versus_test( - opponent=axelrod.Alternator(), expected_actions=actions, seed=2 + opponent=axl.Alternator(), expected_actions=actions, seed=2 ) actions = [(C, C), (D, C), (D, C)] self.versus_test( - opponent=axelrod.Cooperator(), expected_actions=actions, seed=1 + opponent=axl.Cooperator(), expected_actions=actions, seed=1 ) actions = [(D, D), (C, D), (C, D)] - self.versus_test(opponent=axelrod.Defector(), expected_actions=actions, seed=2) + self.versus_test(opponent=axl.Defector(), expected_actions=actions, seed=2) diff --git a/axelrod/tests/strategies/test_oncebitten.py b/axelrod/tests/strategies/test_oncebitten.py index a3abd0a4e..c152bce10 100644 --- a/axelrod/tests/strategies/test_oncebitten.py +++ b/axelrod/tests/strategies/test_oncebitten.py @@ -2,17 +2,17 @@ import random -import axelrod +import axelrod as axl from .test_player import TestPlayer -C, D = axelrod.Action.C, axelrod.Action.D +C, D = axl.Action.C, axl.Action.D class TestOnceBitten(TestPlayer): name = "Once Bitten" - player = axelrod.OnceBitten + player = axl.OnceBitten expected_classifier = { "memory_depth": 12, "stochastic": False, @@ -27,7 +27,7 @@ def test_strategy(self): """If opponent defects at any point then the player will defect forever.""" # Become grudged if the opponent defects twice in a row - opponent = axelrod.MockPlayer([C, C, C, D]) + opponent = axl.MockPlayer([C, C, C, D]) actions = [(C, C), (C, C), (C, C), (C, D), (C, C)] self.versus_test( opponent=opponent, @@ -35,7 +35,7 @@ def test_strategy(self): attrs={"grudged": False, "grudge_memory": 0}, ) - opponent = axelrod.MockPlayer([C, C, C, D, D, D]) + opponent = axl.MockPlayer([C, C, C, D, D, D]) actions = [ (C, C), (C, C), @@ -56,7 +56,7 @@ def test_strategy(self): ) # After 10 rounds of being grudged: forgives - opponent = axelrod.MockPlayer([C, D, D, C] + [C] * 10) + opponent = axl.MockPlayer([C, D, D, C] + [C] * 10) actions = [(C, C), (C, D), (C, D), (D, C)] + [(D, C)] * 10 + [(C, C)] self.versus_test( opponent=opponent, @@ -67,7 +67,7 @@ def test_strategy(self): def test_reset(self): """Check that grudged gets reset properly""" p1 = self.player() - p2 = axelrod.Defector() + p2 = axl.Defector() p1.play(p2) p1.play(p2) p1.play(p2) @@ -79,7 +79,7 @@ def test_reset(self): class TestFoolMeOnce(TestPlayer): name = "Fool Me Once" - player = axelrod.FoolMeOnce + player = axl.FoolMeOnce expected_classifier = { "memory_depth": float("inf"), # Long memory "stochastic": False, @@ -93,16 +93,16 @@ class TestFoolMeOnce(TestPlayer): def test_strategy(self): # If opponent defects more than once, defect forever actions = [(C, C)] * 10 - self.versus_test(opponent=axelrod.Cooperator(), expected_actions=actions) + self.versus_test(opponent=axl.Cooperator(), expected_actions=actions) - opponent = axelrod.MockPlayer([D] + [C] * 9) + opponent = axl.MockPlayer([D] + [C] * 9) actions = [(C, D)] + [(C, C)] * 9 self.versus_test(opponent=opponent, expected_actions=actions) actions = [(C, D)] * 2 + [(D, D)] * 8 - self.versus_test(opponent=axelrod.Defector(), expected_actions=actions) + self.versus_test(opponent=axl.Defector(), expected_actions=actions) - opponent = axelrod.MockPlayer([D, D] + [C] * 9) + opponent = axl.MockPlayer([D, D] + [C] * 9) actions = [(C, D)] * 2 + [(D, C)] * 8 self.versus_test(opponent=opponent, expected_actions=actions) @@ -110,7 +110,7 @@ def test_strategy(self): class TestForgetfulFoolMeOnce(TestPlayer): name = "Forgetful Fool Me Once: 0.05" - player = axelrod.ForgetfulFoolMeOnce + player = axl.ForgetfulFoolMeOnce expected_classifier = { "memory_depth": float("inf"), # Long memory "stochastic": True, @@ -126,7 +126,7 @@ def test_strategy(self): # forgets count. actions = [(C, C), (C, D), (C, C), (C, D), (D, C)] self.versus_test( - opponent=axelrod.Alternator(), + opponent=axl.Alternator(), expected_actions=actions, seed=2, attrs={"D_count": 2}, @@ -135,7 +135,7 @@ def test_strategy(self): # Sometime eventually forget count: actions = [(C, D), (C, D)] + [(D, D)] * 18 + [(C, D)] self.versus_test( - opponent=axelrod.Defector(), + opponent=axl.Defector(), expected_actions=actions, seed=2, attrs={"D_count": 0}, diff --git a/axelrod/tests/strategies/test_player.py b/axelrod/tests/strategies/test_player.py index a8597157e..86160c03a 100644 --- a/axelrod/tests/strategies/test_player.py +++ b/axelrod/tests/strategies/test_player.py @@ -1,22 +1,21 @@ +import unittest import itertools import pickle import random import types -import unittest - -import axelrod import numpy as np -from axelrod import DefaultGame, Player, LimitedHistory + +import axelrod as axl from axelrod.player import simultaneous_play from axelrod.tests.property import strategy_lists from hypothesis import given, settings from hypothesis.strategies import integers, sampled_from -C, D = axelrod.Action.C, axelrod.Action.D +C, D = axl.Action.C, axl.Action.D short_run_time_short_mem = [ - s for s in axelrod.short_run_time_strategies if s().classifier["memory_depth"] <= 10 + s for s in axl.short_run_time_strategies if s().classifier["memory_depth"] <= 10 ] @@ -42,7 +41,7 @@ def defect(*args): } -class ParameterisedTestPlayer(Player): +class ParameterisedTestPlayer(axl.Player): """A simple Player class for testing init parameters""" name = "ParameterisedTestPlayer" @@ -55,7 +54,7 @@ def __init__(self, arg_test1="testing1", arg_test2="testing2"): class TestPlayerClass(unittest.TestCase): name = "Player" - player = Player + player = axl.Player classifier = {"stochastic": False} def test_play(self): @@ -88,9 +87,9 @@ def test_play(self): self.assertEqual(player2.state_distribution, {(D, C): 2}) def test_state_distribution(self): - player1 = axelrod.MockPlayer([C, C, D, D, C]) - player2 = axelrod.MockPlayer([C, D, C, D, D]) - match = axelrod.Match((player1, player2), turns=5) + player1 = axl.MockPlayer([C, C, D, D, C]) + player2 = axl.MockPlayer([C, D, C, D, D]) + match = axl.Match((player1, player2), turns=5) _ = match.play() self.assertEqual( player1.state_distribution, {(C, C): 1, (C, D): 2, (D, C): 1, (D, D): 1} @@ -100,7 +99,7 @@ def test_state_distribution(self): ) def test_noisy_play(self): - axelrod.seed(1) + axl.seed(1) noise = 0.2 player1, player2 = self.player(), self.player() player1.strategy = cooperate @@ -110,7 +109,7 @@ def test_noisy_play(self): self.assertEqual(player2.history[0], D) def test_update_history(self): - player = Player() + player = axl.Player() self.assertEqual(player.history, []) self.assertEqual(player.cooperations, 0) self.assertEqual(player.defections, 0) @@ -124,7 +123,7 @@ def test_update_history(self): self.assertEqual(player.cooperations, 1) def test_history_assignment(self): - player = Player() + player = axl.Player() with self.assertRaises(AttributeError): player.history = [] @@ -133,16 +132,16 @@ def test_strategy(self): def test_clone(self): """Tests player cloning.""" - player1 = axelrod.Random(p=0.75) # 0.5 is the default + player1 = axl.Random(p=0.75) # 0.5 is the default player2 = player1.clone() turns = 50 - for op in [axelrod.Cooperator(), axelrod.Defector(), axelrod.TitForTat()]: + for op in [axl.Cooperator(), axl.Defector(), axl.TitForTat()]: player1.reset() player2.reset() seed = random.randint(0, 10 ** 6) for p in [player1, player2]: - axelrod.seed(seed) - m = axelrod.Match((p, op), turns=turns) + axl.seed(seed) + m = axl.Match((p, op), turns=turns) m.play() self.assertEqual(len(player1.history), turns) self.assertEqual(player1.history, player2.history) @@ -150,20 +149,20 @@ def test_clone(self): def test_equality(self): """Test the equality method for some bespoke cases""" # Check repr - p1 = axelrod.Cooperator() - p2 = axelrod.Cooperator() + p1 = axl.Cooperator() + p2 = axl.Cooperator() self.assertEqual(p1, p2) p1.__repr__ = lambda: "John Nash" self.assertNotEqual(p1, p2) # Check attributes - p1 = axelrod.Cooperator() - p2 = axelrod.Cooperator() + p1 = axl.Cooperator() + p2 = axl.Cooperator() p1.test = "29" self.assertNotEqual(p1, p2) - p1 = axelrod.Cooperator() - p2 = axelrod.Cooperator() + p1 = axl.Cooperator() + p2 = axl.Cooperator() p2.test = "29" self.assertNotEqual(p1, p2) @@ -183,8 +182,8 @@ def test_equality(self): def test_equality_for_numpy_array(self): """Check numpy array attribute (a special case)""" - p1 = axelrod.Cooperator() - p2 = axelrod.Cooperator() + p1 = axl.Cooperator() + p2 = axl.Cooperator() p1.array = np.array([0, 1]) p2.array = np.array([0, 1]) @@ -196,8 +195,8 @@ def test_equality_for_numpy_array(self): def test_equality_for_generator(self): """Test equality works with generator attribute and that the generator attribute is not altered during checking of equality""" - p1 = axelrod.Cooperator() - p2 = axelrod.Cooperator() + p1 = axl.Cooperator() + p2 = axl.Cooperator() # Check that players are equal with generator p1.generator = (i for i in range(10)) @@ -223,8 +222,8 @@ def test_equality_for_cycle(self): """Test equality works with cycle attribute and that the cycle attribute is not altered during checking of equality""" # Check cycle attribute (a special case) - p1 = axelrod.Cooperator() - p2 = axelrod.Cooperator() + p1 = axl.Cooperator() + p2 = axl.Cooperator() # Check that players are equal with cycle p1.cycle = itertools.cycle(range(10)) @@ -248,7 +247,7 @@ def test_equality_for_cycle(self): def test_equality_on_init(self): """Test instances of all strategies are equal on init""" - for s in axelrod.strategies: + for s in axl.strategies: p1, p2 = s(), s() # Check three times (so testing equality doesn't change anything) self.assertEqual(p1, p2) @@ -258,8 +257,8 @@ def test_equality_on_init(self): def test_equality_with_player_as_attributes(self): """Test for a strange edge case where players have pointers to each other""" - p1 = axelrod.Cooperator() - p2 = axelrod.Cooperator() + p1 = axl.Cooperator() + p2 = axl.Cooperator() # If pointing at each other p1.player = p2 @@ -271,20 +270,20 @@ def test_equality_with_player_as_attributes(self): self.assertNotEqual(p1, p2) # If pointing at same strategy instances - p1.player = axelrod.Cooperator() - p2.player = axelrod.Cooperator() + p1.player = axl.Cooperator() + p2.player = axl.Cooperator() p2.test_attribute = "29" self.assertEqual(p1, p2) # If pointing at different strategy instances - p1.player = axelrod.Cooperator() - p2.player = axelrod.Defector() + p1.player = axl.Cooperator() + p2.player = axl.Defector() self.assertNotEqual(p1, p2) # If different strategies pointing at same strategy instances - p3 = axelrod.Defector() - p1.player = axelrod.Cooperator() - p3.player = axelrod.Cooperator() + p3 = axl.Defector() + p1.player = axl.Cooperator() + p3.player = axl.Cooperator() self.assertNotEqual(p1, p3) def test_init_params(self): @@ -315,9 +314,9 @@ def test_init_kwargs(self): # Test that init_kwargs exist and are empty self.assertEqual(self.player().init_kwargs, {}) # Test that passing a positional argument raises an error - self.assertRaises(TypeError, Player, "test") + self.assertRaises(TypeError, axl.Player, "test") # Test that passing a keyword argument raises an error - self.assertRaises(TypeError, Player, arg_test1="test") + self.assertRaises(TypeError, axl.Player, arg_test1="test") # Tests for Players with init parameters @@ -348,7 +347,7 @@ def test_init_kwargs(self): self.assertRaises(TypeError, ParameterisedTestPlayer, "other", "other", "other") -class TestOpponent(Player): +class TestOpponent(axl.Player): """A player who only exists so we have something to test against""" name = "TestOpponent" @@ -371,7 +370,7 @@ def test_initialisation(self): player = self.player() self.assertEqual(len(player.history), 0) self.assertEqual( - player.match_attributes, {"length": -1, "game": DefaultGame, "noise": 0} + player.match_attributes, {"length": -1, "game": axl.DefaultGame, "noise": 0} ) self.assertEqual(player.cooperations, 0) self.assertEqual(player.defections, 0) @@ -408,7 +407,7 @@ def equality_of_players_test(self, p1, p2, seed, opponent): a2 = opponent() self.assertEqual(p1, p2) for player, op in [(p1, a1), (p2, a2)]: - axelrod.seed(seed) + axl.seed(seed) for _ in range(10): simultaneous_play(player, op) self.assertEqual(p1, p2) @@ -427,7 +426,7 @@ def test_equality_of_clone(self, seed, opponent): self.equality_of_players_test(p1, p2, seed, opponent) @given( - opponent=sampled_from(axelrod.short_run_time_strategies), + opponent=sampled_from(axl.short_run_time_strategies), seed=integers(min_value=1, max_value=200), ) @settings(max_examples=1) @@ -439,16 +438,16 @@ def test_equality_of_pickle_clone(self, seed, opponent): def test_reset_history_and_attributes(self): """Make sure resetting works correctly.""" for opponent in [ - axelrod.Defector(), - axelrod.Random(), - axelrod.Alternator(), - axelrod.Cooperator(), + axl.Defector(), + axl.Random(), + axl.Alternator(), + axl.Cooperator(), ]: player = self.player() clone = player.clone() for seed in range(10): - axelrod.seed(seed) + axl.seed(seed) player.play(opponent) player.reset() @@ -480,16 +479,16 @@ def test_clone(self, seed): turns = 50 r = random.random() for op in [ - axelrod.Cooperator(), - axelrod.Defector(), - axelrod.TitForTat(), - axelrod.Random(p=r), + axl.Cooperator(), + axl.Defector(), + axl.TitForTat(), + axl.Random(p=r), ]: player1.reset() player2.reset() for p in [player1, player2]: - axelrod.seed(seed) - m = axelrod.Match((p, op), turns=turns) + axl.seed(seed) + m = axl.Match((p, op), turns=turns) m.play() self.assertEqual(len(player1.history), turns) self.assertEqual(player1.history, player2.history) @@ -568,11 +567,11 @@ def versus_test( init_kwargs = dict() if seed is not None: - axelrod.seed(seed) + axl.seed(seed) player = self.player(**init_kwargs) - match = axelrod.Match( + match = axl.Match( (player, opponent), turns=turns, noise=noise, @@ -630,9 +629,9 @@ def versus_test( if len(expected_actions1) != len(expected_actions2): raise ValueError("Mismatched History lengths.") if seed: - axelrod.seed(seed) + axl.seed(seed) turns = len(expected_actions1) - match = axelrod.Match((player1, player2), turns=turns, noise=noise) + match = axl.Match((player1, player2), turns=turns, noise=noise) match.play() # Test expected sequence of play. for i, (outcome1, outcome2) in enumerate( @@ -646,7 +645,7 @@ def test_versus_with_incorrect_history_lengths(self): """Test the error raised by versus_test if expected actions do not match up""" with self.assertRaises(ValueError): - p1, p2 = axelrod.Cooperator(), axelrod.Cooperator() + p1, p2 = axl.Cooperator(), axl.Cooperator() actions1 = [C, C] actions2 = [C] self.versus_test(p1, p2, actions1, actions2) @@ -670,17 +669,17 @@ def test_memory(player, opponent, memory_length, seed=0, turns=10): only the given amount of memory is used. """ # Play the match normally. - axelrod.seed(seed) - match = axelrod.Match((player, opponent), turns=turns) + axl.seed(seed) + match = axl.Match((player, opponent), turns=turns) plays = [p[0] for p in match.play()] # Play with limited history. player.reset() opponent.reset() - player._history = LimitedHistory(memory_length) - opponent._history = LimitedHistory(memory_length) - axelrod.seed(seed) - match = axelrod.Match((player, opponent), turns=turns, reset=False) + player._history = axl.LimitedHistory(memory_length) + opponent._history = axl.LimitedHistory(memory_length) + axl.seed(seed) + match = axl.Match((player, opponent), turns=turns, reset=False) limited_plays = [p[0] for p in match.play()] return plays == limited_plays @@ -696,8 +695,8 @@ def test_passes(self): The memory test function returns True in this case as the correct mem length is used """ - player = axelrod.TitFor2Tats() - opponent = axelrod.Defector() + player = axl.TitFor2Tats() + opponent = axl.Defector() self.assertTrue(test_memory(player, opponent, memory_length=2)) def test_failures(self): @@ -705,6 +704,6 @@ def test_failures(self): The memory test function returns False in this case as the incorrect mem length is used """ - player = axelrod.TitFor2Tats() - opponent = axelrod.Defector() + player = axl.TitFor2Tats() + opponent = axl.Defector() self.assertFalse(test_memory(player, opponent, memory_length=1)) diff --git a/axelrod/tests/strategies/test_prober.py b/axelrod/tests/strategies/test_prober.py index 85693309d..771e0115a 100644 --- a/axelrod/tests/strategies/test_prober.py +++ b/axelrod/tests/strategies/test_prober.py @@ -1,16 +1,16 @@ """Tests for Prober strategies.""" -import axelrod +import axelrod as axl from .test_player import TestPlayer -C, D = axelrod.Action.C, axelrod.Action.D +C, D = axl.Action.C, axl.Action.D class TestCollectiveStrategy(TestPlayer): name = "CollectiveStrategy" - player = axelrod.CollectiveStrategy + player = axl.CollectiveStrategy expected_classifier = { "memory_depth": float("inf"), "stochastic": False, @@ -24,22 +24,22 @@ class TestCollectiveStrategy(TestPlayer): def test_strategy(self): # If handshake (C, D) is used cooperate until a defection occurs and # then defect throughout - opponent = axelrod.MockPlayer([C, D] + [C] * 10) + opponent = axl.MockPlayer([C, D] + [C] * 10) actions = [(C, C), (D, D)] + [(C, C)] * 11 + [(C, D)] + [(D, C)] * 10 self.versus_test(opponent=opponent, expected_actions=actions) # If handshake is not used: defect actions = [(C, C), (D, C)] + [(D, C)] * 15 - self.versus_test(opponent=axelrod.Cooperator(), expected_actions=actions) + self.versus_test(opponent=axl.Cooperator(), expected_actions=actions) actions = [(C, D), (D, D)] + [(D, D)] * 15 - self.versus_test(opponent=axelrod.Defector(), expected_actions=actions) + self.versus_test(opponent=axl.Defector(), expected_actions=actions) class TestDetective(TestPlayer): name = "Detective" - player = axelrod.Detective + player = axl.Detective expected_classifier = { "memory_depth": float("inf"), "stochastic": False, @@ -52,30 +52,30 @@ class TestDetective(TestPlayer): def test_strategy(self): self.versus_test( - opponent=axelrod.TitForTat(), + opponent=axl.TitForTat(), expected_actions=[(C, C), (D, C), (C, D)] + [(C, C)] * 15, ) self.versus_test( - opponent=axelrod.Cooperator(), + opponent=axl.Cooperator(), expected_actions=[(C, C), (D, C), (C, C), (C, C)] + [(D, C)] * 15, ) self.versus_test( - opponent=axelrod.Defector(), + opponent=axl.Defector(), expected_actions=[(C, D), (D, D), (C, D), (C, D)] + [(D, D)] * 15, ) def test_other_initial_actions(self): self.versus_test( - opponent=axelrod.TitForTat(), + opponent=axl.TitForTat(), expected_actions=[(C, C), (C, C), (D, C)] + [(D, D)] * 15, init_kwargs={"initial_actions": [C, C]}, ) # Extreme case: no memory at all, it's simply a defector self.versus_test( - opponent=axelrod.TitForTat(), + opponent=axl.TitForTat(), expected_actions=[(D, C)] + [(D, D)] * 15, init_kwargs={"initial_actions": []}, ) @@ -84,7 +84,7 @@ def test_other_initial_actions(self): class TestProber(TestPlayer): name = "Prober" - player = axelrod.Prober + player = axl.Prober expected_classifier = { "memory_depth": float("inf"), # Long memory "stochastic": False, @@ -99,24 +99,24 @@ def test_strategy(self): # Starts by playing DCC. # Defects forever if opponent cooperated in moves 2 and 3 actions = [(D, C), (C, C), (C, C)] + [(D, C)] * 3 - self.versus_test(opponent=axelrod.Cooperator(), expected_actions=actions) + self.versus_test(opponent=axl.Cooperator(), expected_actions=actions) - opponent = axelrod.MockPlayer([D, C, C]) + opponent = axl.MockPlayer([D, C, C]) actions = [(D, D), (C, C), (C, C)] + [(D, D), (D, C), (D, C)] self.versus_test(opponent=opponent, expected_actions=actions) # Otherwise it plays like TFT actions = [(D, C), (C, D), (C, C), (C, D), (D, C)] - self.versus_test(opponent=axelrod.Alternator(), expected_actions=actions) + self.versus_test(opponent=axl.Alternator(), expected_actions=actions) actions = [(D, D), (C, D), (C, D), (D, D), (D, D)] - self.versus_test(opponent=axelrod.Defector(), expected_actions=actions) + self.versus_test(opponent=axl.Defector(), expected_actions=actions) class TestProber2(TestPlayer): name = "Prober 2" - player = axelrod.Prober2 + player = axl.Prober2 expected_classifier = { "memory_depth": float("inf"), # Long memory "stochastic": False, @@ -131,20 +131,20 @@ def test_strategy(self): # Starts by playing DCC. # Cooperates forever if opponent played D, C in moves 2 and 3 actions = [(D, C), (C, D), (C, C)] + [(C, D), (C, C), (C, D)] - self.versus_test(opponent=axelrod.Alternator(), expected_actions=actions) + self.versus_test(opponent=axl.Alternator(), expected_actions=actions) - opponent = axelrod.MockPlayer([D, D, C]) + opponent = axl.MockPlayer([D, D, C]) actions = [(D, D), (C, D), (C, C)] + [(C, D), (C, D), (C, C)] self.versus_test(opponent=opponent, expected_actions=actions) # Otherwise it plays like TFT actions = [(D, C), (C, C), (C, C), (C, C), (C, C)] - self.versus_test(opponent=axelrod.Cooperator(), expected_actions=actions) + self.versus_test(opponent=axl.Cooperator(), expected_actions=actions) actions = [(D, D), (C, D), (C, D), (D, D), (D, D)] - self.versus_test(opponent=axelrod.Defector(), expected_actions=actions) + self.versus_test(opponent=axl.Defector(), expected_actions=actions) - opponent = axelrod.MockPlayer([D, C]) + opponent = axl.MockPlayer([D, C]) actions = [(D, D), (C, C), (C, D)] + [(D, C), (C, D), (D, C)] self.versus_test(opponent=opponent, expected_actions=actions) @@ -152,7 +152,7 @@ def test_strategy(self): class TestProber3(TestPlayer): name = "Prober 3" - player = axelrod.Prober3 + player = axl.Prober3 expected_classifier = { "memory_depth": float("inf"), # Long memory "stochastic": False, @@ -167,24 +167,24 @@ def test_strategy(self): # Starts by playing DC. # Defects forever if opponent played C in move 2. actions = [(D, C), (C, C)] + [(D, C), (D, C), (D, C)] - self.versus_test(opponent=axelrod.Cooperator(), expected_actions=actions) + self.versus_test(opponent=axl.Cooperator(), expected_actions=actions) - opponent = axelrod.MockPlayer([D, C]) + opponent = axl.MockPlayer([D, C]) actions = [(D, D), (C, C)] + [(D, D), (D, C), (D, D)] self.versus_test(opponent=opponent, expected_actions=actions) # Otherwise it plays like TFT actions = [(D, C), (C, D), (D, C), (C, D), (D, C)] - self.versus_test(opponent=axelrod.Alternator(), expected_actions=actions) + self.versus_test(opponent=axl.Alternator(), expected_actions=actions) actions = [(D, D), (C, D), (D, D), (D, D), (D, D)] - self.versus_test(opponent=axelrod.Defector(), expected_actions=actions) + self.versus_test(opponent=axl.Defector(), expected_actions=actions) class TestProber4(TestPlayer): name = "Prober 4" - player = axelrod.Prober4 + player = axl.Prober4 expected_classifier = { "stochastic": False, "memory_depth": float("inf"), @@ -212,7 +212,7 @@ def test_strategy(self): attrs = {"turned_defector": True} for history in provocative_histories: - opponent = axelrod.MockPlayer(history + [C] * 5) + opponent = axl.MockPlayer(history + [C] * 5) actions = list(zip(self.initial_sequence, history)) + [(D, C)] * 5 self.versus_test(opponent=opponent, expected_actions=actions, attrs=attrs) @@ -227,7 +227,7 @@ def test_strategy(self): attrs = {"turned_defector": False} for history in unprovocative_histories: - opponent = axelrod.MockPlayer(history + [D] * 5 + [C, C]) + opponent = axl.MockPlayer(history + [D] * 5 + [C, C]) actions = list(zip(self.initial_sequence, history)) + [(C, D)] * 5 actions += [(D, C), (C, C)] self.versus_test(opponent=opponent, expected_actions=actions, attrs=attrs) @@ -236,7 +236,7 @@ def test_strategy(self): class TestHardProber(TestPlayer): name = "Hard Prober" - player = axelrod.HardProber + player = axl.HardProber expected_classifier = { "memory_depth": float("inf"), # Long memory "stochastic": False, @@ -251,24 +251,24 @@ def test_strategy(self): # Starts by playing DDCC # Defects forever if opponent played C in moves 2 and 3 actions = [(D, C), (D, C), (C, C), (C, C)] + [(D, C), (D, C), (D, C)] - self.versus_test(opponent=axelrod.Cooperator(), expected_actions=actions) + self.versus_test(opponent=axl.Cooperator(), expected_actions=actions) - opponent = axelrod.MockPlayer([D, C, C, D]) + opponent = axl.MockPlayer([D, C, C, D]) actions = [(D, D), (D, C), (C, C), (C, D)] + [(D, D), (D, C), (D, C)] self.versus_test(opponent=opponent, expected_actions=actions) # Otherwise it plays like TFT actions = [(D, C), (D, D), (C, C), (C, D)] + [(D, C), (C, D), (D, C)] - self.versus_test(opponent=axelrod.Alternator(), expected_actions=actions) + self.versus_test(opponent=axl.Alternator(), expected_actions=actions) actions = [(D, D), (D, D), (C, D), (C, D)] + [(D, D), (D, D), (D, D)] - self.versus_test(opponent=axelrod.Defector(), expected_actions=actions) + self.versus_test(opponent=axl.Defector(), expected_actions=actions) class TestNaiveProber(TestPlayer): name = "Naive Prober: 0.1" - player = axelrod.NaiveProber + player = axl.NaiveProber expected_classifier = { "memory_depth": 1, "stochastic": True, @@ -281,7 +281,7 @@ class TestNaiveProber(TestPlayer): def test_strategy(self): # Always retaliate a defection - opponent = axelrod.MockPlayer([C, D, D, D, D]) + opponent = axl.MockPlayer([C, D, D, D, D]) actions = [(C, C), (C, D), (D, D), (D, D), (D, D)] self.versus_test(opponent=opponent, expected_actions=actions) @@ -289,18 +289,18 @@ def test_random_defection(self): # Unprovoked defection with small probability actions = [(C, C), (D, C), (D, C), (C, C), (C, C)] self.versus_test( - opponent=axelrod.Cooperator(), expected_actions=actions, seed=2 + opponent=axl.Cooperator(), expected_actions=actions, seed=2 ) actions = [(C, C), (C, C), (C, C), (C, C), (D, C)] self.versus_test( - opponent=axelrod.Cooperator(), expected_actions=actions, seed=5 + opponent=axl.Cooperator(), expected_actions=actions, seed=5 ) # Always defect when p is 1 actions = [(C, C), (D, C), (D, C), (D, C), (D, C)] self.versus_test( - opponent=axelrod.Cooperator(), + opponent=axl.Cooperator(), expected_actions=actions, init_kwargs={"p": 1}, ) @@ -308,7 +308,7 @@ def test_random_defection(self): def test_reduction_to_TFT(self): actions = [(C, C), (C, D), (D, C), (C, D), (D, C)] self.versus_test( - opponent=axelrod.Alternator(), + opponent=axl.Alternator(), expected_actions=actions, init_kwargs={"p": 0}, ) @@ -317,7 +317,7 @@ def test_reduction_to_TFT(self): class TestRemorsefulProber(TestPlayer): name = "Remorseful Prober: 0.1" - player = axelrod.RemorsefulProber + player = axl.RemorsefulProber expected_classifier = { "memory_depth": 2, "stochastic": True, @@ -332,7 +332,7 @@ def test_strategy(self): # Always retaliate a defection actions = [(C, D)] + [(D, D)] * 10 self.versus_test( - opponent=axelrod.Defector(), + opponent=axl.Defector(), expected_actions=actions, attrs={"probing": False}, ) @@ -341,7 +341,7 @@ def test_random_defection(self): # Unprovoked defection with small probability actions = [(C, C), (D, C), (D, C)] self.versus_test( - opponent=axelrod.Cooperator(), + opponent=axl.Cooperator(), expected_actions=actions, seed=2, attrs={"probing": True}, @@ -349,7 +349,7 @@ def test_random_defection(self): actions = [(C, C), (C, C), (C, C), (C, C), (D, C)] self.versus_test( - opponent=axelrod.Cooperator(), + opponent=axl.Cooperator(), expected_actions=actions, seed=5, attrs={"probing": True}, @@ -358,7 +358,7 @@ def test_random_defection(self): # Always defect when p is 1 actions = [(C, C), (D, C), (D, C), (D, C), (D, C)] self.versus_test( - opponent=axelrod.Cooperator(), + opponent=axl.Cooperator(), expected_actions=actions, init_kwargs={"p": 1}, attrs={"probing": True}, @@ -366,7 +366,7 @@ def test_random_defection(self): def test_remorse(self): """After probing, if opponent retaliates, will offer a C.""" - opponent = axelrod.MockPlayer([C, C, D, C]) + opponent = axl.MockPlayer([C, C, D, C]) actions = [(C, C), (D, C), (D, D), (C, C)] self.versus_test( opponent=opponent, @@ -378,7 +378,7 @@ def test_remorse(self): def test_reduction_to_TFT(self): actions = [(C, C), (C, D), (D, C), (C, D), (D, C)] self.versus_test( - opponent=axelrod.Alternator(), + opponent=axl.Alternator(), expected_actions=actions, init_kwargs={"p": 0}, attrs={"probing": False}, diff --git a/axelrod/tests/strategies/test_punisher.py b/axelrod/tests/strategies/test_punisher.py index c94229b1d..77a8db244 100644 --- a/axelrod/tests/strategies/test_punisher.py +++ b/axelrod/tests/strategies/test_punisher.py @@ -1,16 +1,16 @@ """Tests for the Punisher strategies.""" -import axelrod +import axelrod as axl from .test_player import TestPlayer -C, D = axelrod.Action.C, axelrod.Action.D +C, D = axl.Action.C, axl.Action.D class TestPunisher(TestPlayer): name = "Punisher" - player = axelrod.Punisher + player = axl.Punisher expected_classifier = { "memory_depth": float("inf"), # Long memory "stochastic": False, @@ -23,13 +23,13 @@ class TestPunisher(TestPlayer): def test_init(self): """Tests for the __init__ method.""" - player = axelrod.Punisher() + player = axl.Punisher() self.assertEqual(player.mem_length, 1) self.assertFalse(player.grudged) self.assertEqual(player.grudge_memory, 1) def test_strategy(self): - opponent = axelrod.Alternator() + opponent = axl.Alternator() actions = [(C, C), (C, D), (D, C)] self.versus_test( opponent=opponent, @@ -37,7 +37,7 @@ def test_strategy(self): attrs={"grudged": True, "grudge_memory": 0}, ) - opponent = axelrod.MockPlayer([C, D] + [C] * 10) + opponent = axl.MockPlayer([C, D] + [C] * 10) actions = [(C, C), (C, D)] + [(D, C)] * 11 self.versus_test( opponent=opponent, @@ -46,7 +46,7 @@ def test_strategy(self): ) # Eventually the grudge is dropped - opponent = axelrod.MockPlayer([C, D] + [C] * 10) + opponent = axl.MockPlayer([C, D] + [C] * 10) actions = [(C, C), (C, D)] + [(D, C)] * 11 + [(C, D)] self.versus_test( opponent=opponent, @@ -55,7 +55,7 @@ def test_strategy(self): ) # Grudged again on opponent's D - opponent = axelrod.MockPlayer([C, D] + [C] * 11) + opponent = axl.MockPlayer([C, D] + [C] * 11) actions = [(C, C), (C, D)] + [(D, C)] * 11 + [(C, C), (C, D), (D, C)] self.versus_test( opponent=opponent, @@ -67,7 +67,7 @@ def test_strategy(self): class TestInversePunisher(TestPlayer): name = "Inverse Punisher" - player = axelrod.InversePunisher + player = axl.InversePunisher expected_classifier = { "memory_depth": float("inf"), # Long memory "stochastic": False, @@ -80,13 +80,13 @@ class TestInversePunisher(TestPlayer): def test_init(self): """Tests for the __init__ method.""" - player = axelrod.InversePunisher() + player = axl.InversePunisher() self.assertEqual(player.mem_length, 1) self.assertFalse(player.grudged) self.assertEqual(player.grudge_memory, 1) def test_strategy(self): - opponent = axelrod.Alternator() + opponent = axl.Alternator() actions = [(C, C), (C, D), (D, C)] self.versus_test( opponent=opponent, @@ -94,7 +94,7 @@ def test_strategy(self): attrs={"grudged": True, "grudge_memory": 0}, ) - opponent = axelrod.MockPlayer([C, D] + [C] * 10) + opponent = axl.MockPlayer([C, D] + [C] * 10) actions = [(C, C), (C, D)] + [(D, C)] * 11 self.versus_test( opponent=opponent, @@ -103,7 +103,7 @@ def test_strategy(self): ) # Eventually the grudge is dropped - opponent = axelrod.MockPlayer([C, D] + [C] * 10) + opponent = axl.MockPlayer([C, D] + [C] * 10) actions = [(C, C), (C, D)] + [(D, C)] * 11 + [(C, D)] self.versus_test( opponent=opponent, @@ -112,7 +112,7 @@ def test_strategy(self): ) # Grudged again on opponent's D - opponent = axelrod.MockPlayer([C, D] + [C] * 11) + opponent = axl.MockPlayer([C, D] + [C] * 11) actions = [(C, C), (C, D)] + [(D, C)] * 11 + [(C, C), (C, D), (D, C)] self.versus_test( opponent=opponent, @@ -124,7 +124,7 @@ def test_strategy(self): class TestLevelPunisher(TestPlayer): name = "Level Punisher" - player = axelrod.LevelPunisher + player = axl.LevelPunisher expected_classifier = { "memory_depth": float("inf"), # Long memory "stochastic": False, @@ -138,16 +138,16 @@ class TestLevelPunisher(TestPlayer): def test_strategy(self): # Cooperates if the turns played are less than 10. actions = [(C, C)] * 9 - self.versus_test(opponent=axelrod.Cooperator(), expected_actions=actions) + self.versus_test(opponent=axl.Cooperator(), expected_actions=actions) # After 10 rounds # Check if number of defections by opponent is greater than 20% - opponent = axelrod.MockPlayer([C] * 4 + [D] * 2 + [C] * 3 + [D]) + opponent = axl.MockPlayer([C] * 4 + [D] * 2 + [C] * 3 + [D]) actions = [(C, C)] * 4 + [(C, D)] * 2 + [(C, C)] * 3 + [(C, D), (D, C)] self.versus_test(opponent=opponent, expected_actions=actions) # Check if number of defections by opponent is less than 20% - opponent = axelrod.MockPlayer([C] * 4 + [D] + [C] * 4 + [D]) + opponent = axl.MockPlayer([C] * 4 + [D] + [C] * 4 + [D]) actions = [(C, C)] * 4 + [(C, D)] + [(C, C)] * 4 + [(C, D), (C, C)] self.versus_test(opponent=opponent, expected_actions=actions) @@ -155,7 +155,7 @@ def test_strategy(self): class TestTrickyLevelPunisher(TestPlayer): name = "Level Punisher" - player = axelrod.LevelPunisher + player = axl.LevelPunisher expected_classifier = { "memory_depth": float("inf"), # Long memory "stochastic": False, @@ -169,26 +169,26 @@ class TestTrickyLevelPunisher(TestPlayer): def test_strategy(self): # Cooperates if the turns played are less than 10. actions = [(C, C)] * 9 - self.versus_test(opponent=axelrod.Cooperator(), expected_actions=actions) + self.versus_test(opponent=axl.Cooperator(), expected_actions=actions) # After 10 rounds # Check if number of defections by opponent is greater than 20% - opponent = axelrod.MockPlayer([C] * 4 + [D] * 2 + [C] * 3 + [D]) + opponent = axl.MockPlayer([C] * 4 + [D] * 2 + [C] * 3 + [D]) actions = [(C, C)] * 4 + [(C, D)] * 2 + [(C, C)] * 3 + [(C, D), (D, C)] self.versus_test(opponent=opponent, expected_actions=actions) # Check if number of defections by opponent is greater than 10% - opponent = axelrod.MockPlayer([C] * 4 + [D] + [C] * 4 + [D]) + opponent = axl.MockPlayer([C] * 4 + [D] + [C] * 4 + [D]) actions = [(C, C)] * 4 + [(C, D)] + [(C, C)] * 4 + [(C, D), (C, C)] self.versus_test(opponent=opponent, expected_actions=actions) # After 10 rounds # Check if number of defections by opponent is greater than 5% - opponent = axelrod.MockPlayer([C] * 4 + [D] + [C] * 5) + opponent = axl.MockPlayer([C] * 4 + [D] + [C] * 5) actions = [(C, C)] * 4 + [(C, D)] + [(C, C)] * 5 self.versus_test(opponent=opponent, expected_actions=actions) # Check if number of defections by opponent is less than 5% - opponent = axelrod.MockPlayer([C] * 10) + opponent = axl.MockPlayer([C] * 10) actions = [(C, C)] * 5 self.versus_test(opponent=opponent, expected_actions=actions) diff --git a/axelrod/tests/strategies/test_qlearner.py b/axelrod/tests/strategies/test_qlearner.py index 38d4410ef..81a261109 100644 --- a/axelrod/tests/strategies/test_qlearner.py +++ b/axelrod/tests/strategies/test_qlearner.py @@ -2,18 +2,17 @@ import random -import axelrod -from axelrod import Game +import axelrod as axl from .test_player import TestPlayer -C, D = axelrod.Action.C, axelrod.Action.D +C, D = axl.Action.C, axl.Action.D class TestRiskyQLearner(TestPlayer): name = "Risky QLearner" - player = axelrod.RiskyQLearner + player = axl.RiskyQLearner expected_classifier = { "memory_depth": float("inf"), "stochastic": True, @@ -25,7 +24,7 @@ class TestRiskyQLearner(TestPlayer): } def test_payoff_matrix(self): - (R, P, S, T) = Game().RPST() + (R, P, S, T) = axl.Game().RPST() payoff_matrix = {C: {C: R, D: S}, D: {C: T, D: P}} player = self.player() self.assertEqual(player.payoff_matrix, payoff_matrix) @@ -33,7 +32,7 @@ def test_payoff_matrix(self): def test_strategy(self): actions = [(C, C), (D, C), (C, C), (C, C)] self.versus_test( - opponent=axelrod.Cooperator(), + opponent=axl.Cooperator(), expected_actions=actions, seed=5, attrs={ @@ -53,7 +52,7 @@ def test_strategy(self): class TestArrogantQLearner(TestPlayer): name = "Arrogant QLearner" - player = axelrod.ArrogantQLearner + player = axl.ArrogantQLearner expected_classifier = { "memory_depth": float("inf"), # Long memory "stochastic": True, @@ -67,7 +66,7 @@ class TestArrogantQLearner(TestPlayer): def test_strategy(self): actions = [(C, C), (D, C), (C, C), (C, C)] self.versus_test( - opponent=axelrod.Cooperator(), + opponent=axl.Cooperator(), expected_actions=actions, seed=5, attrs={ @@ -87,7 +86,7 @@ def test_strategy(self): class TestHesitantQLearner(TestPlayer): name = "Hesitant QLearner" - player = axelrod.HesitantQLearner + player = axl.HesitantQLearner expected_classifier = { "memory_depth": float("inf"), # Long memory "stochastic": True, @@ -101,7 +100,7 @@ class TestHesitantQLearner(TestPlayer): def test_strategy(self): actions = [(C, D), (D, D), (C, D), (C, D)] self.versus_test( - opponent=axelrod.Defector(), + opponent=axl.Defector(), expected_actions=actions, seed=5, attrs={ @@ -121,7 +120,7 @@ def test_strategy(self): class TestCautiousQLearner(TestPlayer): name = "Cautious QLearner" - player = axelrod.CautiousQLearner + player = axl.CautiousQLearner expected_classifier = { "memory_depth": float("inf"), # Long memory "stochastic": True, @@ -135,7 +134,7 @@ class TestCautiousQLearner(TestPlayer): def test_strategy(self): actions = [(C, D), (D, D), (C, D), (C, D)] self.versus_test( - opponent=axelrod.Defector(), + opponent=axl.Defector(), expected_actions=actions, seed=5, attrs={ diff --git a/axelrod/tests/strategies/test_rand.py b/axelrod/tests/strategies/test_rand.py index 0eceaef5f..4226a16b1 100644 --- a/axelrod/tests/strategies/test_rand.py +++ b/axelrod/tests/strategies/test_rand.py @@ -1,16 +1,16 @@ """Tests for the random strategy.""" -import axelrod +import axelrod as axl from .test_player import TestPlayer -C, D = axelrod.Action.C, axelrod.Action.D +C, D = axl.Action.C, axl.Action.D class TestRandom(TestPlayer): name = "Random: 0.5" - player = axelrod.Random + player = axl.Random expected_classifier = { "memory_depth": 0, "stochastic": True, @@ -23,24 +23,24 @@ class TestRandom(TestPlayer): def test_strategy(self): """Test that strategy is randomly picked (not affected by history).""" - opponent = axelrod.MockPlayer() + opponent = axl.MockPlayer() actions = [(C, C), (D, C), (D, C), (C, C)] self.versus_test(opponent, expected_actions=actions, seed=1) - opponent = axelrod.MockPlayer() + opponent = axl.MockPlayer() actions = [(D, C), (D, C), (C, C)] self.versus_test(opponent, expected_actions=actions, seed=2) - opponent = axelrod.MockPlayer() + opponent = axl.MockPlayer() actions = [(D, C), (D, C), (D, C)] self.versus_test(opponent, expected_actions=actions, init_kwargs={"p": 0}) - opponent = axelrod.MockPlayer() + opponent = axl.MockPlayer() actions = [(C, C), (C, C), (C, C)] self.versus_test(opponent, expected_actions=actions, init_kwargs={"p": 1}) def test_deterministic_classification(self): """Test classification when p is 0 or 1""" for p in [0, 1]: - player = axelrod.Random(p=p) + player = axl.Random(p=p) self.assertFalse(player.classifier["stochastic"]) diff --git a/axelrod/tests/strategies/test_resurrection.py b/axelrod/tests/strategies/test_resurrection.py index 65c890038..c71becde8 100644 --- a/axelrod/tests/strategies/test_resurrection.py +++ b/axelrod/tests/strategies/test_resurrection.py @@ -1,16 +1,16 @@ """Test for the Resurrection strategy.""" -import axelrod +import axelrod as axl from .test_player import TestPlayer -C, D = axelrod.Action.C, axelrod.Action.D +C, D = axl.Action.C, axl.Action.D class Resurrection(TestPlayer): name = "Resurrection" - player = axelrod.Resurrection + player = axl.Resurrection expected_classifier = { "memory_depth": 5, "stochastic": False, @@ -24,20 +24,20 @@ class Resurrection(TestPlayer): def test_strategy(self): # Check if the turns played are greater than 5 actions = [(C, C), (C, C), (C, C), (C, C), (C, C), (C, C), (C, C)] - self.versus_test(axelrod.Cooperator(), expected_actions=actions) + self.versus_test(axl.Cooperator(), expected_actions=actions) actions = [(C, D), (D, D), (D, D), (D, D), (D, D), (D, D), (D, D)] - self.versus_test(axelrod.Defector(), expected_actions=actions) + self.versus_test(axl.Defector(), expected_actions=actions) # Check for TFT behavior after 5 rounds actions = [(C, C), (C, D), (D, C), (C, D), (D, C), (C, D), (D, C)] - self.versus_test(axelrod.Alternator(), expected_actions=actions) + self.versus_test(axl.Alternator(), expected_actions=actions) class TestDoubleResurrection(TestPlayer): name = "DoubleResurrection" - player = axelrod.DoubleResurrection + player = axl.DoubleResurrection expected_classifier = { "memory_depth": 5, "stochastic": False, @@ -50,10 +50,10 @@ class TestDoubleResurrection(TestPlayer): def test_strategy(self): actions = [(C, C), (C, D), (D, C), (C, D), (D, C), (C, D)] - self.versus_test(axelrod.Alternator(), expected_actions=actions) + self.versus_test(axl.Alternator(), expected_actions=actions) actions = [(C, C), (C, C), (C, C), (C, C), (C, C), (D, C)] - self.versus_test(axelrod.Cooperator(), expected_actions=actions) + self.versus_test(axl.Cooperator(), expected_actions=actions) actions = [(C, D), (D, D), (D, D), (D, D), (D, D), (D, D), (C, D)] - self.versus_test(axelrod.Defector(), expected_actions=actions) + self.versus_test(axl.Defector(), expected_actions=actions) diff --git a/axelrod/tests/strategies/test_retaliate.py b/axelrod/tests/strategies/test_retaliate.py index c2906e1ae..251438735 100644 --- a/axelrod/tests/strategies/test_retaliate.py +++ b/axelrod/tests/strategies/test_retaliate.py @@ -1,16 +1,16 @@ """Tests for the retaliate strategy.""" -import axelrod +import axelrod as axl from .test_player import TestOpponent, TestPlayer -C, D = axelrod.Action.C, axelrod.Action.D +C, D = axl.Action.C, axl.Action.D class TestRetaliate(TestPlayer): name = "Retaliate: 0.1" - player = axelrod.Retaliate + player = axl.Retaliate expected_classifier = { "memory_depth": float("inf"), # Long memory "stochastic": False, @@ -23,11 +23,11 @@ class TestRetaliate(TestPlayer): def test_strategy(self): # If opponent has defected more than 10 percent of the time, defect. - opponent = axelrod.Cooperator() + opponent = axl.Cooperator() actions = [(C, C)] * 5 self.versus_test(opponent=opponent, expected_actions=actions) - opponent = axelrod.MockPlayer([C, C, C, D, C]) + opponent = axl.MockPlayer([C, C, C, D, C]) actions = [(C, C), (C, C), (C, C), (C, D), (D, C), (D, C)] self.versus_test(opponent=opponent, expected_actions=actions) @@ -35,7 +35,7 @@ def test_strategy(self): class TestRetaliate2(TestPlayer): name = "Retaliate 2: 0.08" - player = axelrod.Retaliate2 + player = axl.Retaliate2 expected_classifier = { "memory_depth": float("inf"), # Long memory "stochastic": False, @@ -48,7 +48,7 @@ class TestRetaliate2(TestPlayer): def test_strategy(self): # If opponent has defected more than 8 percent of the time, defect. - opponent = axelrod.MockPlayer([C] * 13 + [D]) + opponent = axl.MockPlayer([C] * 13 + [D]) actions = [(C, C)] * 13 + [(C, D), (D, C)] self.versus_test(opponent=opponent, expected_actions=actions) @@ -56,7 +56,7 @@ def test_strategy(self): class TestRetaliate3(TestPlayer): name = "Retaliate 3: 0.05" - player = axelrod.Retaliate3 + player = axl.Retaliate3 expected_classifier = { "memory_depth": float("inf"), # Long memory "stochastic": False, @@ -69,7 +69,7 @@ class TestRetaliate3(TestPlayer): def test_strategy(self): # If opponent has defected more than 5 percent of the time, defect. - opponent = axelrod.MockPlayer([C] * 19 + [D]) + opponent = axl.MockPlayer([C] * 19 + [D]) actions = [(C, C)] * 19 + [(C, D), (D, C)] self.versus_test(opponent=opponent, expected_actions=actions) @@ -77,7 +77,7 @@ def test_strategy(self): class TestLimitedRetaliate(TestPlayer): name = "Limited Retaliate: 0.1, 20" - player = axelrod.LimitedRetaliate + player = axl.LimitedRetaliate expected_classifier = { "memory_depth": float("inf"), # Long memory "stochastic": False, @@ -90,27 +90,27 @@ class TestLimitedRetaliate(TestPlayer): def test_strategy(self): # If opponent has never defected, co-operate - opponent = axelrod.Cooperator() + opponent = axl.Cooperator() actions = [(C, C)] * 5 self.versus_test( opponent=opponent, expected_actions=actions, attrs={"retaliating": False} ) # Retaliate after a (C, D) round. - opponent = axelrod.MockPlayer([C, C, C, D, C]) + opponent = axl.MockPlayer([C, C, C, D, C]) actions = [(C, C), (C, C), (C, C), (C, D), (D, C), (D, C)] self.versus_test( opponent=opponent, expected_actions=actions, attrs={"retaliating": True} ) - opponent = axelrod.Alternator() + opponent = axl.Alternator() # Count retaliations actions = [(C, C), (C, D), (D, C), (D, D), (D, C)] self.versus_test( opponent=opponent, expected_actions=actions, attrs={"retaliation_count": 3} ) - opponent = axelrod.Alternator() + opponent = axl.Alternator() # Cooperate if we hit the retaliation limit actions = [(C, C), (C, D), (D, C), (D, D), (C, C)] diff --git a/axelrod/tests/strategies/test_revised_downing.py b/axelrod/tests/strategies/test_revised_downing.py index c5637fbfb..b46f6fdcc 100644 --- a/axelrod/tests/strategies/test_revised_downing.py +++ b/axelrod/tests/strategies/test_revised_downing.py @@ -1,13 +1,13 @@ -import axelrod +import axelrod as axl from .test_player import TestPlayer -C, D = axelrod.Action.C, axelrod.Action.D +C, D = axl.Action.C, axl.Action.D class TestRevisedDowning(TestPlayer): name = "Revised Downing" - player = axelrod.RevisedDowning + player = axl.RevisedDowning expected_classifier = { "memory_depth": float("inf"), "stochastic": False, @@ -20,23 +20,23 @@ class TestRevisedDowning(TestPlayer): def test_strategy(self): actions = [(C, C), (C, C), (C, C)] - self.versus_test(axelrod.Cooperator(), expected_actions=actions) + self.versus_test(axl.Cooperator(), expected_actions=actions) actions = [(C, D), (C, D), (D, D)] - self.versus_test(axelrod.Defector(), expected_actions=actions) + self.versus_test(axl.Defector(), expected_actions=actions) - opponent = axelrod.MockPlayer(actions=[D, C, C]) + opponent = axl.MockPlayer(actions=[D, C, C]) actions = [(C, D), (C, C), (C, C), (C, D)] self.versus_test(opponent, expected_actions=actions) - opponent = axelrod.MockPlayer(actions=[D, D, C]) + opponent = axl.MockPlayer(actions=[D, D, C]) actions = [(C, D), (C, D), (D, C), (D, D)] self.versus_test(opponent, expected_actions=actions) - opponent = axelrod.MockPlayer(actions=[C, C, D, D, C, C]) + opponent = axl.MockPlayer(actions=[C, C, D, D, C, C]) actions = [(C, C), (C, C), (C, D), (C, D), (D, C), (D, C), (D, C)] self.versus_test(opponent, expected_actions=actions) - opponent = axelrod.MockPlayer(actions=[C, C, C, C, D, D]) + opponent = axl.MockPlayer(actions=[C, C, C, C, D, D]) actions = [(C, C), (C, C), (C, C), (C, C), (C, D), (C, D), (C, C)] self.versus_test(opponent, expected_actions=actions) diff --git a/axelrod/tests/strategies/test_selfsteem.py b/axelrod/tests/strategies/test_selfsteem.py index 8cb8fabd2..c0d9ec84c 100644 --- a/axelrod/tests/strategies/test_selfsteem.py +++ b/axelrod/tests/strategies/test_selfsteem.py @@ -2,17 +2,17 @@ import random -import axelrod +import axelrod as axl from .test_player import TestPlayer -C, D = axelrod.Action.C, axelrod.Action.D +C, D = axl.Action.C, axl.Action.D class TestSelfSteem(TestPlayer): name = "SelfSteem" - player = axelrod.SelfSteem + player = axl.SelfSteem expected_classifier = { "memory_depth": float("inf"), "stochastic": True, @@ -28,16 +28,16 @@ def test_strategy(self): actions = ( [(C, C), (C, C), (D, C), (D, C), (C, C), (D, C)] + [(C, C)] * 4 + [(D, C)] ) - self.versus_test(axelrod.Cooperator(), expected_actions=actions, seed=1) + self.versus_test(axl.Cooperator(), expected_actions=actions, seed=1) # Check for f < -0.95, cooperate actions = [(D, C), (C, C), (D, C), (D, C), (C, C), (D, C), (C, C), (C, C)] self.versus_test( - opponent=axelrod.Cooperator(), expected_actions=actions, seed=0 + opponent=axl.Cooperator(), expected_actions=actions, seed=0 ) actions = [(D, D)] + [(D, D)] * 5 + [(D, D), (C, D), (C, D)] - self.versus_test(opponent=axelrod.Defector(), expected_actions=actions, seed=0) + self.versus_test(opponent=axl.Defector(), expected_actions=actions, seed=0) # Check for -0.3 < f < 0.3, random actions = ( @@ -47,7 +47,7 @@ def test_strategy(self): + [(C, C)] * 7 ) self.versus_test( - opponent=axelrod.Cooperator(), expected_actions=actions, seed=6 + opponent=axl.Cooperator(), expected_actions=actions, seed=6 ) actions = ( @@ -56,7 +56,7 @@ def test_strategy(self): + [(D, D)] * 8 + [(C, D), (C, D), (D, D), (D, D), (D, D)] ) - self.versus_test(opponent=axelrod.Defector(), expected_actions=actions, seed=5) + self.versus_test(opponent=axl.Defector(), expected_actions=actions, seed=5) # Check for 0.95 > abs(f) > 0.3, follows TitForTat actions = ( @@ -64,7 +64,7 @@ def test_strategy(self): + [(C, D), (D, D), (C, D), (C, D), (D, D), (C, D)] + [(D, D)] * 5 ) - self.versus_test(opponent=axelrod.Defector(), expected_actions=actions) + self.versus_test(opponent=axl.Defector(), expected_actions=actions) actions = [ (D, C), @@ -78,4 +78,4 @@ def test_strategy(self): (C, C), (C, C), ] - self.versus_test(opponent=axelrod.Cooperator(), expected_actions=actions) + self.versus_test(opponent=axl.Cooperator(), expected_actions=actions) diff --git a/axelrod/tests/strategies/test_sequence_player.py b/axelrod/tests/strategies/test_sequence_player.py index 985103f9c..2a3c655ef 100644 --- a/axelrod/tests/strategies/test_sequence_player.py +++ b/axelrod/tests/strategies/test_sequence_player.py @@ -1,13 +1,13 @@ """Tests for the Thue-Morse strategies.""" import unittest -import axelrod +import axelrod as axl from axelrod._strategy_utils import recursive_thue_morse from axelrod.strategies.sequence_player import SequencePlayer from .test_player import TestOpponent, TestPlayer -C, D = axelrod.Action.C, axelrod.Action.D +C, D = axl.Action.C, axl.Action.D class TestThueMoreGenerator(unittest.TestCase): @@ -32,7 +32,7 @@ def cooperate_gen(): class TestThueMorse(TestPlayer): name = "ThueMorse" - player = axelrod.ThueMorse + player = axl.ThueMorse expected_classifier = { "memory_depth": float("inf"), "stochastic": False, @@ -49,16 +49,16 @@ def test_strategy(self): n = len(thue_morse_seq) actions = list(zip(thue_morse_seq, [C] * n)) - self.versus_test(axelrod.Cooperator(), expected_actions=actions) + self.versus_test(axl.Cooperator(), expected_actions=actions) actions = list(zip(thue_morse_seq, [D] * n)) - self.versus_test(axelrod.Defector(), expected_actions=actions) + self.versus_test(axl.Defector(), expected_actions=actions) class TestThueMorseInverse(TestPlayer): name = "ThueMorseInverse" - player = axelrod.ThueMorseInverse + player = axl.ThueMorseInverse expected_classifier = { "memory_depth": float("inf"), "stochastic": False, @@ -74,7 +74,7 @@ def test_strategy(self): n = len(inv_thue_morse_seq) actions = list(zip(inv_thue_morse_seq, [C] * n)) - self.versus_test(axelrod.Cooperator(), expected_actions=actions) + self.versus_test(axl.Cooperator(), expected_actions=actions) actions = list(zip(inv_thue_morse_seq, [D] * n)) - self.versus_test(axelrod.Defector(), expected_actions=actions) + self.versus_test(axl.Defector(), expected_actions=actions) diff --git a/axelrod/tests/strategies/test_shortmem.py b/axelrod/tests/strategies/test_shortmem.py index 57ef5db77..48dcd0138 100644 --- a/axelrod/tests/strategies/test_shortmem.py +++ b/axelrod/tests/strategies/test_shortmem.py @@ -1,16 +1,16 @@ """Tests for the ShortMem strategy.""" -import axelrod +import axelrod as axl from .test_player import TestPlayer -C, D = axelrod.Action.C, axelrod.Action.D +C, D = axl.Action.C, axl.Action.D class TestShortMem(TestPlayer): name = "ShortMem" - player = axelrod.ShortMem + player = axl.ShortMem expected_classifier = { "memory_depth": float('inf'), "stochastic": False, @@ -24,34 +24,34 @@ def test_strategy(self): # Starts by cooperating for the first ten moves. actions = [(C, C)] * 10 - self.versus_test(axelrod.Cooperator(), expected_actions=actions) + self.versus_test(axl.Cooperator(), expected_actions=actions) actions = [(C, D)] * 10 - self.versus_test(axelrod.Defector(), expected_actions=actions) + self.versus_test(axl.Defector(), expected_actions=actions) # Cooperate if in the last ten moves, Cooperations - Defections >= 3 actions = [(C, C)] * 11 + [(C, D)] * 4 self.versus_test( - opponent=axelrod.MockPlayer(actions=[C] * 11 + [D] * 4), + opponent=axl.MockPlayer(actions=[C] * 11 + [D] * 4), expected_actions=actions, ) # Defect if in the last ten moves, Defections - Cooperations >= 3 actions = [(C, D)] * 11 + [(D, C)] * 4 self.versus_test( - opponent=axelrod.MockPlayer(actions=[D] * 11 + [C] * 4), + opponent=axl.MockPlayer(actions=[D] * 11 + [C] * 4), expected_actions=actions, ) # If neither of the above conditions are met, apply TitForTat actions = [(C, D)] * 5 + [(C, C)] * 6 + [(C, D), (D, D), (D, D), (D, C), (C, C)] self.versus_test( - opponent=axelrod.MockPlayer(actions=[D] * 5 + [C] * 6 + [D, D, D, C, C]), + opponent=axl.MockPlayer(actions=[D] * 5 + [C] * 6 + [D, D, D, C, C]), expected_actions=actions, ) actions = [(C, C)] * 5 + [(C, D)] * 6 + [(D, C), (C, C), (C, C), (C, D), (D, D)] self.versus_test( - opponent=axelrod.MockPlayer(actions=[C] * 5 + [D] * 6 + [C, C, C, D, D]), + opponent=axl.MockPlayer(actions=[C] * 5 + [D] * 6 + [C, C, C, D, D]), expected_actions=actions, ) diff --git a/axelrod/tests/strategies/test_titfortat.py b/axelrod/tests/strategies/test_titfortat.py index d64b70ea0..b04c7f049 100644 --- a/axelrod/tests/strategies/test_titfortat.py +++ b/axelrod/tests/strategies/test_titfortat.py @@ -1,9 +1,10 @@ """Tests for the tit for tat strategies.""" import copy + import random -import axelrod +import axelrod as axl from axelrod.tests.property import strategy_lists from hypothesis import given @@ -11,7 +12,7 @@ from .test_player import TestMatch, TestPlayer -C, D = axelrod.Action.C, axelrod.Action.D +C, D = axl.Action.C, axl.Action.D class TestTitForTat(TestPlayer): @@ -22,7 +23,7 @@ class TestTitForTat(TestPlayer): """ name = "Tit For Tat" - player = axelrod.TitForTat + player = axl.TitForTat expected_classifier = { "memory_depth": 1, "stochastic": False, @@ -35,36 +36,36 @@ class TestTitForTat(TestPlayer): def test_strategy(self): # Play against opponents actions = [(C, C), (C, D), (D, C), (C, D), (D, C)] - self.versus_test(axelrod.Alternator(), expected_actions=actions) + self.versus_test(axl.Alternator(), expected_actions=actions) actions = [(C, C), (C, C), (C, C), (C, C), (C, C)] - self.versus_test(axelrod.Cooperator(), expected_actions=actions) + self.versus_test(axl.Cooperator(), expected_actions=actions) actions = [(C, D), (D, D), (D, D), (D, D), (D, D)] - self.versus_test(axelrod.Defector(), expected_actions=actions) + self.versus_test(axl.Defector(), expected_actions=actions) # This behaviour is independent of knowledge of the Match length actions = [(C, C), (C, D), (D, C), (C, D), (D, C)] self.versus_test( - axelrod.Alternator(), + axl.Alternator(), expected_actions=actions, match_attributes={"length": float("inf")}, ) # We can also test against random strategies actions = [(C, D), (D, D), (D, C), (C, C), (C, D)] - self.versus_test(axelrod.Random(), expected_actions=actions, seed=0) + self.versus_test(axl.Random(), expected_actions=actions, seed=0) actions = [(C, C), (C, D), (D, D), (D, C)] - self.versus_test(axelrod.Random(), expected_actions=actions, seed=1) + self.versus_test(axl.Random(), expected_actions=actions, seed=1) # If you would like to test against a sequence of moves you should use # a MockPlayer - opponent = axelrod.MockPlayer(actions=[C, D]) + opponent = axl.MockPlayer(actions=[C, D]) actions = [(C, C), (C, D), (D, C), (C, D)] self.versus_test(opponent, expected_actions=actions) - opponent = axelrod.MockPlayer(actions=[C, C, D, D, C, D]) + opponent = axl.MockPlayer(actions=[C, C, D, D, C, D]) actions = [(C, C), (C, C), (C, D), (D, D), (D, C), (C, D)] self.versus_test(opponent, expected_actions=actions) @@ -72,7 +73,7 @@ def test_strategy(self): class TestTitFor2Tats(TestPlayer): name = "Tit For 2 Tats" - player = axelrod.TitFor2Tats + player = axl.TitFor2Tats expected_classifier = { "memory_depth": 2, "stochastic": False, @@ -84,10 +85,10 @@ class TestTitFor2Tats(TestPlayer): def test_strategy(self): # Will punish sequence of 2 defections but will forgive one - opponent = axelrod.MockPlayer(actions=[D, D, D, C, C]) + opponent = axl.MockPlayer(actions=[D, D, D, C, C]) actions = [(C, D), (C, D), (D, D), (D, C), (C, C), (C, D)] self.versus_test(opponent, expected_actions=actions) - opponent = axelrod.MockPlayer(actions=[C, C, D, D, C, D, D, C, C, D, D]) + opponent = axl.MockPlayer(actions=[C, C, D, D, C, D, D, C, C, D, D]) actions = [ (C, C), (C, C), @@ -107,7 +108,7 @@ def test_strategy(self): class TestTwoTitsForTat(TestPlayer): name = "Two Tits For Tat" - player = axelrod.TwoTitsForTat + player = axl.TwoTitsForTat expected_classifier = { "memory_depth": 2, "stochastic": False, @@ -119,21 +120,21 @@ class TestTwoTitsForTat(TestPlayer): def test_strategy(self): # Will defect twice when last turn of opponent was defection. - opponent = axelrod.MockPlayer(actions=[D, C, C, D, C]) + opponent = axl.MockPlayer(actions=[D, C, C, D, C]) actions = [(C, D), (D, C), (D, C), (C, D), (D, C)] self.versus_test(opponent, expected_actions=actions) actions = [(C, C), (C, C)] - self.versus_test(opponent=axelrod.Cooperator(), expected_actions=actions) + self.versus_test(opponent=axl.Cooperator(), expected_actions=actions) actions = [(C, D), (D, D), (D, D)] - self.versus_test(opponent=axelrod.Defector(), expected_actions=actions) + self.versus_test(opponent=axl.Defector(), expected_actions=actions) class TestDynamicTwoTitsForTat(TestPlayer): name = "Dynamic Two Tits For Tat" - player = axelrod.DynamicTwoTitsForTat + player = axl.DynamicTwoTitsForTat expected_classifier = { "memory_depth": float("inf"), "stochastic": True, @@ -145,7 +146,7 @@ class TestDynamicTwoTitsForTat(TestPlayer): def test_strategy(self): # Test that it is stochastic - opponent = axelrod.MockPlayer(actions=[D, C, D, D, C]) + opponent = axl.MockPlayer(actions=[D, C, D, D, C]) actions = [(C, D), (D, C), (C, D), (D, D), (D, C)] self.versus_test(opponent, expected_actions=actions, seed=1) # Should respond differently with a different seed @@ -154,16 +155,16 @@ def test_strategy(self): # Will cooperate if opponent cooperates. actions = [(C, C), (C, C), (C, C), (C, C), (C, C)] - self.versus_test(axelrod.Cooperator(), expected_actions=actions) + self.versus_test(axl.Cooperator(), expected_actions=actions) # Test against defector actions = [(C, D), (D, D), (D, D), (D, D), (D, D)] - self.versus_test(axelrod.Defector(), expected_actions=actions) + self.versus_test(axl.Defector(), expected_actions=actions) class TestBully(TestPlayer): name = "Bully" - player = axelrod.Bully + player = axl.Bully expected_classifier = { "memory_depth": 1, "stochastic": False, @@ -176,19 +177,19 @@ class TestBully(TestPlayer): def test_strategy(self): # Will do opposite of what opponent does. actions = [(D, C), (D, D), (C, C), (D, D), (C, C)] - self.versus_test(axelrod.Alternator(), expected_actions=actions) + self.versus_test(axl.Alternator(), expected_actions=actions) actions = [(D, C), (D, C), (D, C), (D, C), (D, C)] - self.versus_test(axelrod.Cooperator(), expected_actions=actions) + self.versus_test(axl.Cooperator(), expected_actions=actions) actions = [(D, D), (C, D), (C, D), (C, D), (C, D)] - self.versus_test(axelrod.Defector(), expected_actions=actions) + self.versus_test(axl.Defector(), expected_actions=actions) class TestSneakyTitForTat(TestPlayer): name = "Sneaky Tit For Tat" - player = axelrod.SneakyTitForTat + player = axl.SneakyTitForTat expected_classifier = { "memory_depth": float("inf"), # Long memory "stochastic": False, @@ -199,19 +200,19 @@ class TestSneakyTitForTat(TestPlayer): } def test_strategy(self): - opponent = axelrod.MockPlayer(actions=[C, C, C, D, C, C]) + opponent = axl.MockPlayer(actions=[C, C, C, D, C, C]) actions = [(C, C), (C, C), (D, C), (D, D), (C, C), (C, C)] self.versus_test(opponent, expected_actions=actions) # Repents if punished for a defection actions = [(C, C), (C, D), (D, C), (C, D), (C, C)] - self.versus_test(axelrod.Alternator(), expected_actions=actions) + self.versus_test(axl.Alternator(), expected_actions=actions) class TestSuspiciousTitForTat(TestPlayer): name = "Suspicious Tit For Tat" - player = axelrod.SuspiciousTitForTat + player = axl.SuspiciousTitForTat expected_classifier = { "memory_depth": 1, # Four-Vector = (1.,0.,1.,0.) "stochastic": False, @@ -225,19 +226,19 @@ def test_strategy(self): # Plays like TFT after the first move, repeating the opponents last # move. actions = [(D, C), (C, D)] * 8 - self.versus_test(axelrod.TitForTat(), expected_actions=actions) + self.versus_test(axl.TitForTat(), expected_actions=actions) actions = [(D, C), (C, C), (C, C)] - self.versus_test(axelrod.Cooperator(), expected_actions=actions) + self.versus_test(axl.Cooperator(), expected_actions=actions) actions = [(D, D), (D, D), (D, D)] - self.versus_test(axelrod.Defector(), expected_actions=actions) + self.versus_test(axl.Defector(), expected_actions=actions) class TestAntiTitForTat(TestPlayer): name = "Anti Tit For Tat" - player = axelrod.AntiTitForTat + player = axl.AntiTitForTat expected_classifier = { "memory_depth": 1, # Four-Vector = (1.,0.,1.,0.) "stochastic": False, @@ -249,13 +250,13 @@ class TestAntiTitForTat(TestPlayer): def test_strategy(self): actions = [(C, C), (D, C), (D, D), (C, D)] * 4 - self.versus_test(axelrod.TitForTat(), expected_actions=actions) + self.versus_test(axl.TitForTat(), expected_actions=actions) class TestHardTitForTat(TestPlayer): name = "Hard Tit For Tat" - player = axelrod.HardTitForTat + player = axl.HardTitForTat expected_classifier = { "memory_depth": 3, "stochastic": False, @@ -266,21 +267,21 @@ class TestHardTitForTat(TestPlayer): } def test_strategy(self): - opponent = axelrod.MockPlayer(actions=[D, C, C, C, D, C]) + opponent = axl.MockPlayer(actions=[D, C, C, C, D, C]) actions = [(C, D), (D, C), (D, C), (D, C), (C, D), (D, C)] self.versus_test(opponent, expected_actions=actions) actions = [(C, C), (C, C), (C, C)] - self.versus_test(axelrod.Cooperator(), expected_actions=actions) + self.versus_test(axl.Cooperator(), expected_actions=actions) actions = [(C, D), (D, D), (D, D)] - self.versus_test(axelrod.Defector(), expected_actions=actions) + self.versus_test(axl.Defector(), expected_actions=actions) class TestHardTitFor2Tats(TestPlayer): name = "Hard Tit For 2 Tats" - player = axelrod.HardTitFor2Tats + player = axl.HardTitFor2Tats expected_classifier = { "memory_depth": 3, "stochastic": False, @@ -292,7 +293,7 @@ class TestHardTitFor2Tats(TestPlayer): def test_strategy(self): # Uses memory 3 to punish 2 consecutive defections - opponent = axelrod.MockPlayer(actions=[D, C, C, D, D, D, C]) + opponent = axl.MockPlayer(actions=[D, C, C, D, D, D, C]) actions = [(C, D), (C, C), (C, C), (C, D), (C, D), (D, D), (D, C)] self.versus_test(opponent, expected_actions=actions) @@ -300,7 +301,7 @@ def test_strategy(self): class TestOmegaTFT(TestPlayer): name = "Omega TFT: 3, 8" - player = axelrod.OmegaTFT + player = axl.OmegaTFT expected_classifier = { "memory_depth": float("inf"), @@ -315,18 +316,18 @@ def test_strategy(self): player_history = [C, D, C, D, C, C, C, C, C] opp_history = [D, C, D, C, D, C, C, C, C] actions = list(zip(player_history, opp_history)) - self.versus_test(axelrod.SuspiciousTitForTat(), expected_actions=actions) + self.versus_test(axl.SuspiciousTitForTat(), expected_actions=actions) player_history = [C, C, D, C, D, C, C, C, D, D, D, D, D, D] opp_history = [C, D] * 7 actions = list(zip(player_history, opp_history)) - self.versus_test(axelrod.Alternator(), expected_actions=actions) + self.versus_test(axl.Alternator(), expected_actions=actions) class TestGradual(TestPlayer): name = "Gradual" - player = axelrod.Gradual + player = axl.Gradual expected_classifier = { "memory_depth": float("inf"), "stochastic": False, @@ -339,7 +340,7 @@ class TestGradual(TestPlayer): def test_strategy(self): # Punishes defection with a growing number of defections and calms # the opponent with two cooperations in a row. - opponent = axelrod.MockPlayer(actions=[C]) + opponent = axl.MockPlayer(actions=[C]) actions = [(C, C)] self.versus_test( opponent, @@ -350,7 +351,7 @@ def test_strategy(self): }, ) - opponent = axelrod.MockPlayer(actions=[D]) + opponent = axl.MockPlayer(actions=[D]) actions = [(C, D)] self.versus_test( opponent, @@ -361,7 +362,7 @@ def test_strategy(self): }, ) - opponent = axelrod.MockPlayer(actions=[D, C]) + opponent = axl.MockPlayer(actions=[D, C]) actions = [(C, D), (D, C)] self.versus_test( opponent, @@ -372,7 +373,7 @@ def test_strategy(self): }, ) - opponent = axelrod.MockPlayer(actions=[D, C, C]) + opponent = axl.MockPlayer(actions=[D, C, C]) actions = [(C, D), (D, C), (C, C)] self.versus_test( opponent, @@ -383,7 +384,7 @@ def test_strategy(self): }, ) - opponent = axelrod.MockPlayer(actions=[D, C, D, C]) + opponent = axl.MockPlayer(actions=[D, C, D, C]) actions = [(C, D), (D, C), (C, D), (C, C)] self.versus_test( opponent, @@ -394,7 +395,7 @@ def test_strategy(self): }, ) - opponent = axelrod.MockPlayer(actions=[D, C, D, C, C]) + opponent = axl.MockPlayer(actions=[D, C, D, C, C]) actions = [(C, D), (D, C), (C, D), (C, C), (C, C)] self.versus_test( opponent, @@ -405,7 +406,7 @@ def test_strategy(self): }, ) - opponent = axelrod.MockPlayer(actions=[D, C, D, C, C, C]) + opponent = axl.MockPlayer(actions=[D, C, D, C, C, C]) actions = [(C, D), (D, C), (C, D), (C, C), (C, C), (C, C)] self.versus_test( opponent, @@ -416,7 +417,7 @@ def test_strategy(self): }, ) - opponent = axelrod.MockPlayer(actions=[D, C, D, C, C, C, D, C]) + opponent = axl.MockPlayer(actions=[D, C, D, C, C, C, D, C]) actions = [(C, D), (D, C), (C, D), (C, C), (C, C), (C, C), (C, D), (D, C)] self.versus_test( opponent, @@ -427,7 +428,7 @@ def test_strategy(self): }, ) - opponent = axelrod.MockPlayer(actions=[D, C, D, C, C, D, D, D]) + opponent = axl.MockPlayer(actions=[D, C, D, C, C, D, D, D]) actions = [(C, D), (D, C), (C, D), (C, C), (C, C), (C, D), (D, D), (D, D)] self.versus_test( opponent, @@ -438,7 +439,7 @@ def test_strategy(self): }, ) - opponent = axelrod.Defector() + opponent = axl.Defector() actions = [ (C, D), (D, D), # 1 defection as a response to the 1 defection by opponent @@ -509,25 +510,25 @@ def test_specific_set_of_results(self): to a memory one player that start by defecting and only cooperates if both players cooperated in the previous round. """ - mistrust_with_bug = axelrod.MemoryOnePlayer( + mistrust_with_bug = axl.MemoryOnePlayer( initial=D, four_vector=(1, 0, 0, 0), ) players = [ self.player(), - axelrod.TitForTat(), - axelrod.GoByMajority(), - axelrod.Grudger(), - axelrod.WinStayLoseShift(), - axelrod.Prober(), - axelrod.Defector(), + axl.TitForTat(), + axl.GoByMajority(), + axl.Grudger(), + axl.WinStayLoseShift(), + axl.Prober(), + axl.Defector(), mistrust_with_bug, - axelrod.Cooperator(), - axelrod.CyclerCCD(), - axelrod.CyclerDDC(), + axl.Cooperator(), + axl.CyclerCCD(), + axl.CyclerDDC(), ] - axelrod.seed(1) - tournament = axelrod.Tournament(players, turns=1000, repetitions=1) + axl.seed(1) + tournament = axl.Tournament(players, turns=1000, repetitions=1) results = tournament.play(progress_bar=False) scores = [round(average_score_per_turn * 1000, 1) for average_score_per_turn in results.payoff_matrix[0]] @@ -549,7 +550,7 @@ def test_specific_set_of_results(self): class TestOriginalGradual(TestPlayer): name = "Original Gradual" - player = axelrod.OriginalGradual + player = axl.OriginalGradual expected_classifier = { "memory_depth": float("inf"), "stochastic": False, @@ -562,7 +563,7 @@ class TestOriginalGradual(TestPlayer): def test_strategy(self): # Punishes defection with a growing number of defections and calms # the opponent with two cooperations in a row. - opponent = axelrod.MockPlayer(actions=[C]) + opponent = axl.MockPlayer(actions=[C]) actions = [(C, C)] self.versus_test( opponent, @@ -575,7 +576,7 @@ def test_strategy(self): }, ) - opponent = axelrod.MockPlayer(actions=[D]) + opponent = axl.MockPlayer(actions=[D]) actions = [(C, D)] self.versus_test( opponent, @@ -588,7 +589,7 @@ def test_strategy(self): }, ) - opponent = axelrod.MockPlayer(actions=[D, C]) + opponent = axl.MockPlayer(actions=[D, C]) actions = [(C, D), (D, C)] self.versus_test( opponent, @@ -601,7 +602,7 @@ def test_strategy(self): }, ) - opponent = axelrod.MockPlayer(actions=[D, C, C]) + opponent = axl.MockPlayer(actions=[D, C, C]) actions = [(C, D), (D, C), (C, C)] self.versus_test( opponent, @@ -614,7 +615,7 @@ def test_strategy(self): }, ) - opponent = axelrod.MockPlayer(actions=[D, C, D, C]) + opponent = axl.MockPlayer(actions=[D, C, D, C]) actions = [(C, D), (D, C), (C, D), (C, C)] self.versus_test( opponent, @@ -627,7 +628,7 @@ def test_strategy(self): }, ) - opponent = axelrod.MockPlayer(actions=[D, C, D, C, C]) + opponent = axl.MockPlayer(actions=[D, C, D, C, C]) actions = [(C, D), (D, C), (C, D), (C, C), (C, C)] self.versus_test( opponent, @@ -640,7 +641,7 @@ def test_strategy(self): }, ) - opponent = axelrod.MockPlayer(actions=[D, C, D, C, C, C]) + opponent = axl.MockPlayer(actions=[D, C, D, C, C, C]) actions = [(C, D), (D, C), (C, D), (C, C), (C, C), (C, C)] self.versus_test( opponent, @@ -653,7 +654,7 @@ def test_strategy(self): }, ) - opponent = axelrod.MockPlayer(actions=[D, C, D, C, C, C, D, C]) + opponent = axl.MockPlayer(actions=[D, C, D, C, C, C, D, C]) actions = [(C, D), (D, C), (C, D), (C, C), (C, C), (C, C), (C, D), (D, C)] self.versus_test( opponent, @@ -666,7 +667,7 @@ def test_strategy(self): }, ) - opponent = axelrod.MockPlayer(actions=[D, C, D, C, C, D, D, D]) + opponent = axl.MockPlayer(actions=[D, C, D, C, C, D, D, D]) actions = [(C, D), (D, C), (C, D), (C, C), (C, C), (C, D), (D, D), (D, D)] self.versus_test( opponent, @@ -693,23 +694,23 @@ def test_output_from_literature(self): See https://github.com/Axelrod-Python/Axelrod/issues/1294 for another discussion of this. """ - players = [axelrod.Cooperator(), - axelrod.Defector(), - axelrod.Random(), - axelrod.TitForTat(), - axelrod.Grudger(), - axelrod.CyclerDDC(), - axelrod.CyclerCCD(), - axelrod.GoByMajority(), - axelrod.SuspiciousTitForTat(), - axelrod.Prober(), + players = [axl.Cooperator(), + axl.Defector(), + axl.Random(), + axl.TitForTat(), + axl.Grudger(), + axl.CyclerDDC(), + axl.CyclerCCD(), + axl.GoByMajority(), + axl.SuspiciousTitForTat(), + axl.Prober(), self.player(), - axelrod.WinStayLoseShift(), + axl.WinStayLoseShift(), ] - axelrod.seed(1) + axl.seed(1) turns = 1000 - tournament = axelrod.Tournament(players, turns=turns, repetitions=1) + tournament = axl.Tournament(players, turns=turns, repetitions=1) results = tournament.play(progress_bar=False) scores = [round(average_score_per_turn * 1000, 1) for average_score_per_turn in results.payoff_matrix[-2]] @@ -733,7 +734,7 @@ def test_output_from_literature(self): class TestContriteTitForTat(TestPlayer): name = "Contrite Tit For Tat" - player = axelrod.ContriteTitForTat + player = axl.ContriteTitForTat expected_classifier = { "memory_depth": 3, "stochastic": False, @@ -744,7 +745,7 @@ class TestContriteTitForTat(TestPlayer): } deterministic_strategies = [ - s for s in axelrod.strategies if not s().classifier["stochastic"] + s for s in axl.strategies if not s().classifier["stochastic"] ] def test_init(self): @@ -757,16 +758,16 @@ def test_init(self): turns=integers(min_value=1, max_value=20), ) def test_is_tit_for_tat_with_no_noise(self, strategies, turns): - tft = axelrod.TitForTat() + tft = axl.TitForTat() ctft = self.player() opponent = strategies[0]() - m1 = axelrod.Match((tft, opponent), turns) - m2 = axelrod.Match((ctft, opponent), turns) + m1 = axl.Match((tft, opponent), turns) + m2 = axl.Match((ctft, opponent), turns) self.assertEqual(m1.play(), m2.play()) def test_strategy_with_noise(self): ctft = self.player() - opponent = axelrod.Defector() + opponent = axl.Defector() self.assertEqual(ctft.strategy(opponent), C) self.assertEqual(ctft._recorded_history, [C]) ctft.reset() # Clear the recorded history @@ -803,7 +804,7 @@ def test_strategy_with_noise(self): class TestAdaptiveTitForTat(TestPlayer): name = "Adaptive Tit For Tat: 0.5" - player = axelrod.AdaptiveTitForTat + player = axl.AdaptiveTitForTat expected_classifier = { "memory_depth": float("inf"), "stochastic": False, @@ -816,7 +817,7 @@ class TestAdaptiveTitForTat(TestPlayer): def test_strategy(self): actions = [(C, C), (C, D), (D, C), (C, D), (D, C)] self.versus_test( - axelrod.Alternator(), + axl.Alternator(), expected_actions=actions, attrs={"world": 0.34375, "rate": 0.5}, ) @@ -824,7 +825,7 @@ def test_strategy(self): class TestSpitefulTitForTat(TestPlayer): name = "Spiteful Tit For Tat" - player = axelrod.SpitefulTitForTat + player = axl.SpitefulTitForTat expected_classifier = { "memory_depth": float("inf"), "stochastic": False, @@ -837,19 +838,19 @@ class TestSpitefulTitForTat(TestPlayer): def test_strategy(self): # Repeats last action of opponent history until 2 consecutive # defections, then always defects - opponent = axelrod.MockPlayer(actions=[C, C, C, C]) + opponent = axl.MockPlayer(actions=[C, C, C, C]) actions = [(C, C)] * 5 self.versus_test( opponent, expected_actions=actions, attrs={"retaliating": False} ) - opponent = axelrod.MockPlayer(actions=[C, C, C, C, D, C]) + opponent = axl.MockPlayer(actions=[C, C, C, C, D, C]) actions = [(C, C)] * 4 + [(C, D), (D, C), (C, C)] self.versus_test( opponent, expected_actions=actions, attrs={"retaliating": False} ) - opponent = axelrod.MockPlayer(actions=[C, C, D, D, C]) + opponent = axl.MockPlayer(actions=[C, C, D, D, C]) actions = [(C, C), (C, C), (C, D), (D, D), (D, C)] self.versus_test( opponent, expected_actions=actions, attrs={"retaliating": True} @@ -859,7 +860,7 @@ def test_strategy(self): class TestSlowTitForTwoTats2(TestPlayer): name = "Slow Tit For Two Tats 2" - player = axelrod.SlowTitForTwoTats2 + player = axl.SlowTitForTwoTats2 expected_classifier = { "memory_depth": 2, "stochastic": False, @@ -872,7 +873,7 @@ class TestSlowTitForTwoTats2(TestPlayer): def test_strategy(self): # If opponent plays the same move twice, repeats last action of # opponent history, otherwise repeats previous move. - opponent = axelrod.MockPlayer(actions=[C, C, D, D, C, D, D, C, C, D, D]) + opponent = axl.MockPlayer(actions=[C, C, D, D, C, D, D, C, C, D, D]) actions = [ (C, C), (C, C), @@ -895,7 +896,7 @@ class TestAlexei(TestPlayer): """ name = "Alexei: (D,)" - player = axelrod.Alexei + player = axl.Alexei expected_classifier = { "memory_depth": float("inf"), "stochastic": False, @@ -907,22 +908,22 @@ class TestAlexei(TestPlayer): def test_strategy(self): actions = [(C, C), (C, C), (C, C), (C, C), (D, C)] - self.versus_test(axelrod.Cooperator(), expected_actions=actions) + self.versus_test(axl.Cooperator(), expected_actions=actions) actions = [(C, D), (D, D), (D, D), (D, D), (D, D)] - self.versus_test(axelrod.Defector(), expected_actions=actions) + self.versus_test(axl.Defector(), expected_actions=actions) actions = [(C, C), (C, D), (D, C), (C, D), (D, C), (C, D)] self.versus_test( - axelrod.Alternator(), + axl.Alternator(), expected_actions=actions, match_attributes={"length": float("inf")}, ) actions = [(C, C), (C, D), (D, C), (C, D), (D, C), (D, D)] - self.versus_test(axelrod.Alternator(), expected_actions=actions) + self.versus_test(axl.Alternator(), expected_actions=actions) - opponent = axelrod.MockPlayer(actions=[C, C, D, D, C, D]) + opponent = axl.MockPlayer(actions=[C, C, D, D, C, D]) actions = [(C, C), (C, C), (C, D), (D, D), (D, C), (D, D)] self.versus_test(opponent, expected_actions=actions) @@ -933,7 +934,7 @@ class TestEugineNier(TestPlayer): """ name = "EugineNier: (D,)" - player = axelrod.EugineNier + player = axl.EugineNier expected_classifier = { "memory_depth": float("inf"), "stochastic": False, @@ -946,12 +947,12 @@ class TestEugineNier(TestPlayer): def test_strategy(self): actions = [(C, C), (C, C), (C, C), (D, C)] self.versus_test( - axelrod.Cooperator(), expected_actions=actions, attrs={"is_defector": False} + axl.Cooperator(), expected_actions=actions, attrs={"is_defector": False} ) actions = [(C, C), (C, C), (C, C), (C, C)] self.versus_test( - axelrod.Cooperator(), + axl.Cooperator(), expected_actions=actions, attrs={"is_defector": False}, match_attributes={"length": float("inf")}, @@ -960,19 +961,19 @@ def test_strategy(self): # Plays TfT and defects in last round actions = [(C, C), (C, D), (D, C), (C, D), (D, C), (D, D)] self.versus_test( - axelrod.Alternator(), expected_actions=actions, attrs={"is_defector": False} + axl.Alternator(), expected_actions=actions, attrs={"is_defector": False} ) actions = [(C, C), (C, D), (D, C), (C, D), (D, C), (C, D)] self.versus_test( - axelrod.Alternator(), + axl.Alternator(), expected_actions=actions, attrs={"is_defector": False}, match_attributes={"length": float("inf")}, ) # Becomes defector after 5 defections - opponent = axelrod.MockPlayer(actions=[D, C, D, D, D, D, C, C]) + opponent = axl.MockPlayer(actions=[D, C, D, D, D, D, C, C]) actions = [(C, D), (D, C), (C, D), (D, D), (D, D), (D, D), (D, C), (D, C)] self.versus_test(opponent, expected_actions=actions) @@ -983,7 +984,7 @@ class TestNTitsForMTats(TestPlayer): """ name = "N Tit(s) For M Tat(s): 3, 2" - player = axelrod.NTitsForMTats + player = axl.NTitsForMTats expected_classifier = { "memory_depth": 3, "stochastic": False, @@ -1002,58 +1003,58 @@ def test_strategy(self): init_kwargs = {"N": 1, "M": 1} actions = [(C, C), (C, D), (D, C), (C, D), (D, C)] self.versus_test( - axelrod.Alternator(), expected_actions=actions, init_kwargs=init_kwargs + axl.Alternator(), expected_actions=actions, init_kwargs=init_kwargs ) actions = [(C, C), (C, C), (C, C), (C, C), (C, C)] self.versus_test( - axelrod.Cooperator(), expected_actions=actions, init_kwargs=init_kwargs + axl.Cooperator(), expected_actions=actions, init_kwargs=init_kwargs ) actions = [(C, D), (D, D), (D, D), (D, D), (D, D)] self.versus_test( - axelrod.Defector(), expected_actions=actions, init_kwargs=init_kwargs + axl.Defector(), expected_actions=actions, init_kwargs=init_kwargs ) actions = [(C, C), (C, D), (D, C), (C, D), (D, C)] self.versus_test( - axelrod.Alternator(), + axl.Alternator(), expected_actions=actions, match_attributes={"length": float("inf")}, init_kwargs=init_kwargs, ) actions = [(C, D), (D, D), (D, C), (C, C), (C, D)] self.versus_test( - axelrod.Random(), expected_actions=actions, seed=0, init_kwargs=init_kwargs + axl.Random(), expected_actions=actions, seed=0, init_kwargs=init_kwargs ) actions = [(C, C), (C, D), (D, D), (D, C)] self.versus_test( - axelrod.Random(), expected_actions=actions, seed=1, init_kwargs=init_kwargs + axl.Random(), expected_actions=actions, seed=1, init_kwargs=init_kwargs ) - opponent = axelrod.MockPlayer(actions=[C, D]) + opponent = axl.MockPlayer(actions=[C, D]) actions = [(C, C), (C, D), (D, C), (C, D)] self.versus_test(opponent, expected_actions=actions, init_kwargs=init_kwargs) - opponent = axelrod.MockPlayer(actions=[C, C, D, D, C, D]) + opponent = axl.MockPlayer(actions=[C, C, D, D, C, D]) actions = [(C, C), (C, C), (C, D), (D, D), (D, C), (C, D)] self.versus_test(opponent, expected_actions=actions, init_kwargs=init_kwargs) # TitFor2Tats test_strategy init_kwargs = {"N": 1, "M": 2} - opponent = axelrod.MockPlayer(actions=[D, D, D, C, C]) + opponent = axl.MockPlayer(actions=[D, D, D, C, C]) actions = [(C, D), (C, D), (D, D), (D, C), (C, C), (C, D)] self.versus_test(opponent, expected_actions=actions, init_kwargs=init_kwargs) # TwoTitsForTat test_strategy init_kwargs = {"N": 2, "M": 1} - opponent = axelrod.MockPlayer(actions=[D, C, C, D, C]) + opponent = axl.MockPlayer(actions=[D, C, C, D, C]) actions = [(C, D), (D, C), (D, C), (C, D), (D, C)] self.versus_test(opponent, expected_actions=actions, init_kwargs=init_kwargs) actions = [(C, C), (C, C)] self.versus_test( - opponent=axelrod.Cooperator(), + opponent=axl.Cooperator(), expected_actions=actions, init_kwargs=init_kwargs, ) actions = [(C, D), (D, D), (D, D)] self.versus_test( - opponent=axelrod.Defector(), + opponent=axl.Defector(), expected_actions=actions, init_kwargs=init_kwargs, ) @@ -1061,17 +1062,17 @@ def test_strategy(self): # Cooperator test_strategy actions = [(C, C)] + [(C, D), (C, C)] * 9 self.versus_test( - opponent=axelrod.Alternator(), + opponent=axl.Alternator(), expected_actions=actions, init_kwargs={"N": 0, "M": 1}, ) self.versus_test( - opponent=axelrod.Alternator(), + opponent=axl.Alternator(), expected_actions=actions, init_kwargs={"N": 0, "M": 5}, ) self.versus_test( - opponent=axelrod.Alternator(), + opponent=axl.Alternator(), expected_actions=actions, init_kwargs={"N": 0, "M": 0}, ) @@ -1079,19 +1080,19 @@ def test_strategy(self): # Defector test_strategy actions = [(D, C)] + [(D, D), (D, C)] * 9 self.versus_test( - opponent=axelrod.Alternator(), + opponent=axl.Alternator(), expected_actions=actions, init_kwargs={"N": 1, "M": 0}, ) self.versus_test( - opponent=axelrod.Alternator(), + opponent=axl.Alternator(), expected_actions=actions, init_kwargs={"N": 5, "M": 0}, ) # Default init args actions = [(C, C), (C, D), (C, D), (D, C), (D, C), (D, D), (C, C)] - opponent = axelrod.MockPlayer(actions=[acts[1] for acts in actions]) + opponent = axl.MockPlayer(actions=[acts[1] for acts in actions]) self.versus_test(opponent=opponent, expected_actions=actions) def test_varying_memory_depth(self): @@ -1106,7 +1107,7 @@ class TestMichaelos(TestPlayer): """ name = "Michaelos: (D,)" - player = axelrod.Michaelos + player = axl.Michaelos expected_classifier = { "memory_depth": float("inf"), "stochastic": True, @@ -1120,7 +1121,7 @@ def test_strategy(self): actions = [(C, C), (C, C), (C, C), (D, C)] self.versus_test( - axelrod.Cooperator(), + axl.Cooperator(), expected_actions=actions, attrs={"is_defector": False}, seed=2, @@ -1128,7 +1129,7 @@ def test_strategy(self): actions = [(C, C), (C, C), (C, C), (C, C)] self.versus_test( - axelrod.Cooperator(), + axl.Cooperator(), expected_actions=actions, attrs={"is_defector": False}, match_attributes={"length": float("inf")}, @@ -1137,7 +1138,7 @@ def test_strategy(self): actions = [(C, D), (D, D), (D, D), (D, D)] self.versus_test( - axelrod.Defector(), + axl.Defector(), expected_actions=actions, attrs={"is_defector": False}, seed=2, @@ -1145,7 +1146,7 @@ def test_strategy(self): actions = [(C, D), (D, D), (D, D), (D, D)] self.versus_test( - axelrod.Defector(), + axl.Defector(), expected_actions=actions, attrs={"is_defector": False}, match_attributes={"length": float("inf")}, @@ -1155,7 +1156,7 @@ def test_strategy(self): # Chance of becoming a defector is 50% after (D, C) occurs. actions = [(C, C), (C, D), (D, C), (C, D), (D, C)] self.versus_test( - axelrod.Alternator(), + axl.Alternator(), expected_actions=actions, attrs={"is_defector": False}, seed=3, @@ -1163,7 +1164,7 @@ def test_strategy(self): actions = [(C, C), (C, D), (D, C), (D, D), (D, C), (D, D), (D, C)] self.versus_test( - axelrod.Alternator(), + axl.Alternator(), expected_actions=actions, attrs={"is_defector": True}, seed=2, @@ -1171,7 +1172,7 @@ def test_strategy(self): actions = [(C, C), (C, D), (D, C), (C, D), (D, C), (D, D), (D, C)] self.versus_test( - axelrod.Alternator(), + axl.Alternator(), expected_actions=actions, attrs={"is_defector": True}, match_attributes={"length": float("inf")}, @@ -1183,7 +1184,7 @@ class TestRandomTitForTat(TestPlayer): """Tests for random tit for tat strategy.""" name = "Random Tit for Tat: 0.5" - player = axelrod.RandomTitForTat + player = axl.RandomTitForTat expected_classifier = { "memory_depth": 1, "stochastic": True, @@ -1202,29 +1203,29 @@ def test_strategy(self): """ actions = [(C, C), (C, C), (C, C)] self.versus_test( - axelrod.Cooperator(), expected_actions=actions, init_kwargs={"p": 1} + axl.Cooperator(), expected_actions=actions, init_kwargs={"p": 1} ) actions = [(C, D), (D, D), (D, D)] self.versus_test( - axelrod.Defector(), expected_actions=actions, init_kwargs={"p": 0} + axl.Defector(), expected_actions=actions, init_kwargs={"p": 0} ) actions = [(C, C), (C, C), (D, C), (C, C)] self.versus_test( - axelrod.Cooperator(), expected_actions=actions, init_kwargs={"p": 0} + axl.Cooperator(), expected_actions=actions, init_kwargs={"p": 0} ) actions = [(C, D), (D, D), (C, D), (D, D)] self.versus_test( - axelrod.Defector(), expected_actions=actions, init_kwargs={"p": 1} + axl.Defector(), expected_actions=actions, init_kwargs={"p": 1} ) actions = [(C, C), (C, C), (D, C), (C, C), (D, C), (C, C)] - self.versus_test(axelrod.Cooperator(), expected_actions=actions, seed=2) + self.versus_test(axl.Cooperator(), expected_actions=actions, seed=2) actions = [(C, D), (D, D), (C, D), (D, D), (D, D), (D, D)] - self.versus_test(axelrod.Defector(), expected_actions=actions, seed=1) + self.versus_test(axl.Defector(), expected_actions=actions, seed=1) def test_deterministic_classification(self): """ @@ -1234,5 +1235,5 @@ def test_deterministic_classification(self): """ for p in [0, 1]: - player = axelrod.RandomTitForTat(p=p) + player = axl.RandomTitForTat(p=p) self.assertFalse(player.classifier["stochastic"]) diff --git a/axelrod/tests/strategies/test_verybad.py b/axelrod/tests/strategies/test_verybad.py index de854b57e..b67545e97 100644 --- a/axelrod/tests/strategies/test_verybad.py +++ b/axelrod/tests/strategies/test_verybad.py @@ -1,16 +1,16 @@ """Tests for the VeryBad strategy.""" -import axelrod +import axelrod as axl from .test_player import TestPlayer -C, D = axelrod.Action.C, axelrod.Action.D +C, D = axl.Action.C, axl.Action.D class TestVeryBad(TestPlayer): name = "VeryBad" - player = axelrod.VeryBad + player = axl.VeryBad expected_classifier = { "memory_depth": float("inf"), "stochastic": False, @@ -24,12 +24,12 @@ def test_strategy(self): # axelrod.Defector - # cooperates for the first three, defects for the rest P(C) < .5 self.versus_test( - axelrod.Defector(), expected_actions=([(C, D)] * 3 + [(D, D)] * 7) + axl.Defector(), expected_actions=([(C, D)] * 3 + [(D, D)] * 7) ) # axelrod.Cooperator - # cooperate for all, P(C) == 1 - self.versus_test(axelrod.Cooperator(), expected_actions=[(C, C)]) + self.versus_test(axl.Cooperator(), expected_actions=[(C, C)]) expected_actions = [ (C, C), # first three cooperate @@ -43,5 +43,5 @@ def test_strategy(self): (D, C), # P(C) = .375 (D, D), # P(C) = .4 ] - mock_player = axelrod.MockPlayer(actions=[a[1] for a in expected_actions]) + mock_player = axl.MockPlayer(actions=[a[1] for a in expected_actions]) self.versus_test(mock_player, expected_actions=expected_actions) diff --git a/axelrod/tests/strategies/test_worse_and_worse.py b/axelrod/tests/strategies/test_worse_and_worse.py index 461825e2a..a402a5e15 100644 --- a/axelrod/tests/strategies/test_worse_and_worse.py +++ b/axelrod/tests/strategies/test_worse_and_worse.py @@ -1,16 +1,16 @@ """Tests for the WorseAndWorse strategies.""" -import axelrod +import axelrod as axl from .test_player import TestPlayer -C, D = axelrod.Action.C, axelrod.Action.D +C, D = axl.Action.C, axl.Action.D class TestWorseAndWorse(TestPlayer): name = "Worse and Worse" - player = axelrod.WorseAndWorse + player = axl.WorseAndWorse expected_classifier = { "memory_depth": float("inf"), "stochastic": True, @@ -25,17 +25,17 @@ def test_strategy(self): """Test that the strategy gives expected behaviour.""" # 6 Rounds Cooperate given seed actions = [(C, C)] * 6 + [(D, C)] + [(C, C)] * 3 - self.versus_test(axelrod.Cooperator(), expected_actions=actions, seed=8) + self.versus_test(axl.Cooperator(), expected_actions=actions, seed=8) # 6 Rounds Cooperate and Defect no matter oponent actions = [(C, D)] * 6 + [(D, D)] + [(C, D)] * 3 - self.versus_test(axelrod.Defector(), expected_actions=actions, seed=8) + self.versus_test(axl.Defector(), expected_actions=actions, seed=8) class TestWorseAndWorseRandom(TestPlayer): name = "Knowledgeable Worse and Worse" - player = axelrod.KnowledgeableWorseAndWorse + player = axl.KnowledgeableWorseAndWorse expected_classifier = { "memory_depth": float("inf"), "stochastic": True, @@ -50,7 +50,7 @@ def test_strategy(self): """Test that the strategy gives expected behaviour.""" actions = [(C, C)] + [(D, C)] * 4 self.versus_test( - axelrod.Cooperator(), + axl.Cooperator(), expected_actions=actions, match_attributes={"length": 5}, seed=1, @@ -59,7 +59,7 @@ def test_strategy(self): # Test that behaviour does not depend on opponent actions = [(C, D)] + [(D, D)] * 4 self.versus_test( - axelrod.Defector(), + axl.Defector(), expected_actions=actions, match_attributes={"length": 5}, seed=1, @@ -68,7 +68,7 @@ def test_strategy(self): # Test that behaviour changes when does not know length. actions = [(C, C), (C, D), (C, C), (C, D), (C, C)] self.versus_test( - axelrod.Alternator(), + axl.Alternator(), expected_actions=actions, match_attributes={"length": -1}, seed=1, @@ -78,7 +78,7 @@ def test_strategy(self): class TestWorseAndWorse2(TestPlayer): name = "Worse and Worse 2" - player = axelrod.WorseAndWorse2 + player = axl.WorseAndWorse2 expected_classifier = { "memory_depth": float("inf"), "stochastic": True, @@ -94,30 +94,30 @@ def test_strategy(self): # Test next move matches opponent actions = [(C, C)] * 19 - self.versus_test(axelrod.Cooperator(), expected_actions=actions) + self.versus_test(axl.Cooperator(), expected_actions=actions) actions = [(C, C), (C, C), (C, D), (D, C)] self.versus_test( - opponent=axelrod.MockPlayer(actions=[C, C, D, C]), expected_actions=actions + opponent=axl.MockPlayer(actions=[C, C, D, C]), expected_actions=actions ) actions = [(C, C)] * 18 + [(C, D), (D, C)] self.versus_test( - opponent=axelrod.MockPlayer(actions=[C] * 18 + [D, C]), + opponent=axl.MockPlayer(actions=[C] * 18 + [D, C]), expected_actions=actions, ) # After round 20, strategy follows stochastic behavior given a seed actions = [(C, C)] * 20 + [(C, D), (D, C), (C, C), (C, D)] self.versus_test( - opponent=axelrod.MockPlayer(actions=[C] * 20 + [D, C, C, D]), + opponent=axl.MockPlayer(actions=[C] * 20 + [D, C, C, D]), expected_actions=actions, seed=8, ) actions = [(C, C)] * 20 + [(D, D), (D, C)] + [(C, C)] * 2 + [(D, C)] self.versus_test( - opponent=axelrod.MockPlayer(actions=[C] * 20 + [D, C, C, C]), + opponent=axl.MockPlayer(actions=[C] * 20 + [D, C, C, C]), expected_actions=actions, seed=2, ) @@ -126,7 +126,7 @@ def test_strategy(self): class TestWorseAndWorse3(TestPlayer): name = "Worse and Worse 3" - player = axelrod.WorseAndWorse3 + player = axl.WorseAndWorse3 expected_classifier = { "memory_depth": float("inf"), "stochastic": True, @@ -141,17 +141,17 @@ def test_strategy(self): """Test that the strategy gives expected behaviour.""" # Test that if opponent only defects, strategy also defects actions = [(C, D)] + [(D, D)] * 4 - self.versus_test(axelrod.Defector(), expected_actions=actions) + self.versus_test(axl.Defector(), expected_actions=actions) # Test that if opponent only cooperates, strategy also cooperates actions = [(C, C)] * 5 - self.versus_test(axelrod.Cooperator(), expected_actions=actions) + self.versus_test(axl.Cooperator(), expected_actions=actions) # Test that given a non 0/1 probability of defecting, strategy follows # stochastic behaviour, given a seed actions = [(C, C), (C, D), (C, C), (D, D), (C, C), (D, C)] self.versus_test( - axelrod.MockPlayer(actions=[C, D, C, D, C]), + axl.MockPlayer(actions=[C, D, C, D, C]), expected_actions=actions, seed=8, ) diff --git a/axelrod/tests/strategies/test_zero_determinant.py b/axelrod/tests/strategies/test_zero_determinant.py index a6f479124..615ca27fd 100644 --- a/axelrod/tests/strategies/test_zero_determinant.py +++ b/axelrod/tests/strategies/test_zero_determinant.py @@ -2,13 +2,13 @@ import unittest -import axelrod +import axelrod as axl from axelrod.game import DefaultGame from axelrod.strategies.zero_determinant import LRPlayer from .test_player import TestPlayer, test_four_vector -C, D = axelrod.Action.C, axelrod.Action.D +C, D = axl.Action.C, axl.Action.D class TestLRPlayer(unittest.TestCase): @@ -20,7 +20,7 @@ def test_exception(self): class TestZDExtortion(TestPlayer): name = "ZD-Extortion: 0.2, 0.1, 1" - player = axelrod.ZDExtortion + player = axl.ZDExtortion expected_classifier = { "memory_depth": 1, "stochastic": True, @@ -38,17 +38,17 @@ def test_four_vector(self): def test_strategy(self): actions = [(C, C), (C, D), (D, C), (D, D), (D, C), (D, D)] self.versus_test( - opponent=axelrod.Alternator(), expected_actions=actions, seed=3 + opponent=axl.Alternator(), expected_actions=actions, seed=3 ) actions = [(C, D), (D, C), (D, D), (D, C), (D, D), (D, C)] - self.versus_test(opponent=axelrod.CyclerDC(), expected_actions=actions, seed=6) + self.versus_test(opponent=axl.CyclerDC(), expected_actions=actions, seed=6) class TestZDExtort2(TestPlayer): name = "ZD-Extort-2: 0.1111111111111111, 0.5" - player = axelrod.ZDExtort2 + player = axl.ZDExtort2 expected_classifier = { "memory_depth": 1, "stochastic": True, @@ -71,25 +71,25 @@ def test_receive_match_attributes(self): def test_strategy(self): actions = [(C, C), (D, D), (D, C), (D, D), (D, C), (C, D)] self.versus_test( - opponent=axelrod.Alternator(), expected_actions=actions, seed=2 + opponent=axl.Alternator(), expected_actions=actions, seed=2 ) actions = [(C, C), (C, D), (C, C), (C, D), (D, C), (C, D)] self.versus_test( - opponent=axelrod.Alternator(), expected_actions=actions, seed=31 + opponent=axl.Alternator(), expected_actions=actions, seed=31 ) actions = [(C, D), (D, C), (D, D), (D, C), (C, D), (C, C)] - self.versus_test(opponent=axelrod.CyclerDC(), expected_actions=actions, seed=2) + self.versus_test(opponent=axl.CyclerDC(), expected_actions=actions, seed=2) actions = [(C, D), (C, C), (C, D), (C, C), (C, D), (C, C)] - self.versus_test(opponent=axelrod.CyclerDC(), expected_actions=actions, seed=31) + self.versus_test(opponent=axl.CyclerDC(), expected_actions=actions, seed=31) class TestZDExtort2v2(TestPlayer): name = "ZD-Extort-2 v2: 0.125, 0.5, 1" - player = axelrod.ZDExtort2v2 + player = axl.ZDExtort2v2 expected_classifier = { "memory_depth": 1, "stochastic": True, @@ -112,16 +112,16 @@ def test_four_vector(self): def test_strategy(self): actions = [(C, C), (D, D), (D, C), (D, D), (D, C), (C, D)] self.versus_test( - opponent=axelrod.Alternator(), expected_actions=actions, seed=2 + opponent=axl.Alternator(), expected_actions=actions, seed=2 ) actions = [(C, D), (D, C), (D, D), (D, C), (D, D), (D, C)] - self.versus_test(opponent=axelrod.CyclerDC(), expected_actions=actions, seed=5) + self.versus_test(opponent=axl.CyclerDC(), expected_actions=actions, seed=5) class TestZDExtort3(TestPlayer): name = "ZD-Extort3: 0.11538461538461539, 0.3333333333333333, 1" - player = axelrod.ZDExtort3 + player = axl.ZDExtort3 expected_classifier = { "memory_depth": 1, "stochastic": True, @@ -145,18 +145,18 @@ def test_strategy(self): actions = [(C, C), (C, D), (D, C), (D, D), (D, C), (D, D)] self.versus_test( - opponent=axelrod.Alternator(), expected_actions=actions, seed=3 + opponent=axl.Alternator(), expected_actions=actions, seed=3 ) actions = [(C, D), (D, C), (D, D), (D, C), (D, D), (D, C)] - self.versus_test(opponent=axelrod.CyclerDC(), expected_actions=actions, seed=6) + self.versus_test(opponent=axl.CyclerDC(), expected_actions=actions, seed=6) class TestZDExtort4(TestPlayer): name = "ZD-Extort-4: 0.23529411764705882, 0.25, 1" - player = axelrod.ZDExtort4 + player = axl.ZDExtort4 expected_classifier = { "memory_depth": 1, "stochastic": True, @@ -174,17 +174,17 @@ def test_four_vector(self): def test_strategy(self): actions = [(C, C), (D, D), (D, C), (D, D), (D, C), (C, D)] self.versus_test( - opponent=axelrod.Alternator(), expected_actions=actions, seed=2 + opponent=axl.Alternator(), expected_actions=actions, seed=2 ) actions = [(C, D), (D, C), (D, D), (D, C), (D, D), (D, C)] - self.versus_test(opponent=axelrod.CyclerDC(), expected_actions=actions, seed=5) + self.versus_test(opponent=axl.CyclerDC(), expected_actions=actions, seed=5) class TestZDGen2(TestPlayer): name = "ZD-GEN-2: 0.125, 0.5, 3" - player = axelrod.ZDGen2 + player = axl.ZDGen2 expected_classifier = { "memory_depth": 1, "stochastic": True, @@ -203,25 +203,25 @@ def test_strategy(self): actions = [(C, C), (C, D), (D, C), (D, D), (C, C), (C, D)] self.versus_test( - opponent=axelrod.Alternator(), expected_actions=actions, seed=2 + opponent=axl.Alternator(), expected_actions=actions, seed=2 ) actions = [(C, C), (C, D), (C, C), (C, D), (C, C), (C, D)] self.versus_test( - opponent=axelrod.Alternator(), expected_actions=actions, seed=31 + opponent=axl.Alternator(), expected_actions=actions, seed=31 ) actions = [(C, D), (D, C), (D, D), (C, C), (C, D), (C, C)] - self.versus_test(opponent=axelrod.CyclerDC(), expected_actions=actions, seed=2) + self.versus_test(opponent=axl.CyclerDC(), expected_actions=actions, seed=2) actions = [(C, D), (C, C), (C, D), (C, C), (C, D), (C, C)] - self.versus_test(opponent=axelrod.CyclerDC(), expected_actions=actions, seed=31) + self.versus_test(opponent=axl.CyclerDC(), expected_actions=actions, seed=31) class TestZDGTFT2(TestPlayer): name = "ZD-GTFT-2: 0.25, 0.5" - player = axelrod.ZDGTFT2 + player = axl.ZDGTFT2 expected_classifier = { "memory_depth": 1, "stochastic": True, @@ -243,25 +243,25 @@ def test_receive_match_attributes(self): def test_strategy(self): actions = [(C, C), (C, D), (D, C), (C, D), (D, C), (C, D)] self.versus_test( - opponent=axelrod.Alternator(), expected_actions=actions, seed=2 + opponent=axl.Alternator(), expected_actions=actions, seed=2 ) actions = [(C, C), (C, D), (C, C), (C, D), (C, C), (C, D)] self.versus_test( - opponent=axelrod.Alternator(), expected_actions=actions, seed=31 + opponent=axl.Alternator(), expected_actions=actions, seed=31 ) actions = [(C, D), (D, C), (C, D), (D, C), (C, D), (C, C)] - self.versus_test(opponent=axelrod.CyclerDC(), expected_actions=actions, seed=2) + self.versus_test(opponent=axl.CyclerDC(), expected_actions=actions, seed=2) actions = [(C, D), (C, C), (C, D), (C, C), (C, D), (D, C)] - self.versus_test(opponent=axelrod.CyclerDC(), expected_actions=actions, seed=31) + self.versus_test(opponent=axl.CyclerDC(), expected_actions=actions, seed=31) class TestZDMischief(TestPlayer): name = "ZD-Mischief: 0.1, 0.0, 1" - player = axelrod.ZDMischief + player = axl.ZDMischief expected_classifier = { "memory_depth": 1, "stochastic": True, @@ -279,17 +279,17 @@ def test_four_vector(self): def test_strategy(self): actions = [(C, C), (D, D), (D, C), (D, D), (D, C), (C, D)] self.versus_test( - opponent=axelrod.Alternator(), expected_actions=actions, seed=2 + opponent=axl.Alternator(), expected_actions=actions, seed=2 ) actions = [(C, D), (D, C), (D, D), (D, C), (D, D), (D, C)] - self.versus_test(opponent=axelrod.CyclerDC(), expected_actions=actions, seed=5) + self.versus_test(opponent=axl.CyclerDC(), expected_actions=actions, seed=5) class TestZDSet2(TestPlayer): name = "ZD-SET-2: 0.25, 0.0, 2" - player = axelrod.ZDSet2 + player = axl.ZDSet2 expected_classifier = { "memory_depth": 1, "stochastic": True, @@ -312,8 +312,8 @@ def test_four_vector(self): def test_strategy(self): actions = [(C, C), (D, D), (D, C), (C, D), (C, C), (D, D)] self.versus_test( - opponent=axelrod.Alternator(), expected_actions=actions, seed=2 + opponent=axl.Alternator(), expected_actions=actions, seed=2 ) actions = [(C, D), (D, C), (D, D), (D, C), (D, D), (D, C)] - self.versus_test(opponent=axelrod.CyclerDC(), expected_actions=actions, seed=5) + self.versus_test(opponent=axl.CyclerDC(), expected_actions=actions, seed=5) diff --git a/axelrod/tests/unit/test_actions.py b/axelrod/tests/unit/test_actions.py index 2ae3ab8ac..d5ad6a338 100644 --- a/axelrod/tests/unit/test_actions.py +++ b/axelrod/tests/unit/test_actions.py @@ -1,9 +1,9 @@ import unittest -from axelrod import Action +import axelrod as axl from axelrod.action import UnknownActionError, actions_to_str, str_to_actions -C, D = Action.C, Action.D +C, D = axl.Action.C, axl.Action.D class TestAction(unittest.TestCase): @@ -34,15 +34,15 @@ def test_flip(self): self.assertEqual(D.flip(), C) def test_from_char(self): - self.assertEqual(Action.from_char("C"), C) - self.assertEqual(Action.from_char("D"), D) + self.assertEqual(axl.Action.from_char("C"), C) + self.assertEqual(axl.Action.from_char("D"), D) def test_from_char_error(self): - self.assertRaises(UnknownActionError, Action.from_char, "") - self.assertRaises(UnknownActionError, Action.from_char, "c") - self.assertRaises(UnknownActionError, Action.from_char, "d") - self.assertRaises(UnknownActionError, Action.from_char, "A") - self.assertRaises(UnknownActionError, Action.from_char, "CC") + self.assertRaises(UnknownActionError, axl.Action.from_char, "") + self.assertRaises(UnknownActionError, axl.Action.from_char, "c") + self.assertRaises(UnknownActionError, axl.Action.from_char, "d") + self.assertRaises(UnknownActionError, axl.Action.from_char, "A") + self.assertRaises(UnknownActionError, axl.Action.from_char, "CC") def test_str_to_actions(self): self.assertEqual(str_to_actions(""), ()) diff --git a/axelrod/tests/unit/test_compute_finite_state_machine_memory.py b/axelrod/tests/unit/test_compute_finite_state_machine_memory.py index f027d1be2..82e828676 100644 --- a/axelrod/tests/unit/test_compute_finite_state_machine_memory.py +++ b/axelrod/tests/unit/test_compute_finite_state_machine_memory.py @@ -1,10 +1,11 @@ """Tests for Compute FSM Memory.""" -import axelrod + import unittest +import axelrod as axl from axelrod.compute_finite_state_machine_memory import * -C, D = axelrod.Action.C, axelrod.Action.D +C, D = axl.Action.C, axl.Action.D class TestOrderedMemitTuple(unittest.TestCase): diff --git a/axelrod/tests/unit/test_deterministic_cache.py b/axelrod/tests/unit/test_deterministic_cache.py index 8728787aa..a9e0999ca 100644 --- a/axelrod/tests/unit/test_deterministic_cache.py +++ b/axelrod/tests/unit/test_deterministic_cache.py @@ -1,16 +1,16 @@ +import unittest import os import pickle -import unittest -from axelrod import Action, Defector, DeterministicCache, Random, TitForTat +import axelrod as axl -C, D = Action.C, Action.D +C, D = axl.Action.C, axl.Action.D class TestDeterministicCache(unittest.TestCase): @classmethod def setUpClass(cls): - cls.test_key = (TitForTat(), Defector()) + cls.test_key = (axl.TitForTat(), axl.Defector()) cls.test_value = [(C, D), (D, D), (D, D)] cls.test_save_file = "test_cache_save.txt" cls.test_load_file = "test_cache_load.txt" @@ -26,13 +26,13 @@ def tearDownClass(cls): os.remove(cls.test_load_file) def setUp(self): - self.cache = DeterministicCache() + self.cache = axl.DeterministicCache() def test_basic_init(self): self.assertTrue(self.cache.mutable) def test_init_from_file(self): - loaded_cache = DeterministicCache(file_name=self.test_load_file) + loaded_cache = axl.DeterministicCache(file_name=self.test_load_file) self.assertEqual(loaded_cache[self.test_key], self.test_value) def test_setitem(self): @@ -49,25 +49,25 @@ def test_setitem_invalid_key_first_two_elements_not_player(self): with self.assertRaises(ValueError): self.cache[invalid_key] = self.test_value - invalid_key = (TitForTat(), "test") + invalid_key = (axl.TitForTat(), "test") with self.assertRaises(ValueError): self.cache[invalid_key] = self.test_value - invalid_key = ("test", TitForTat()) + invalid_key = ("test", axl.TitForTat()) with self.assertRaises(ValueError): self.cache[invalid_key] = self.test_value def test_setitem_invalid_key_too_many_players(self): - invalid_key = (TitForTat(), TitForTat(), TitForTat()) + invalid_key = (axl.TitForTat(), axl.TitForTat(), axl.TitForTat()) with self.assertRaises(ValueError): self.cache[invalid_key] = self.test_value def test_setitem_invalid_key_stochastic_player(self): - invalid_key = (Random(), TitForTat()) + invalid_key = (axl.Random(), axl.TitForTat()) with self.assertRaises(ValueError): self.cache[invalid_key] = self.test_value - invalid_key = (TitForTat(), Random()) + invalid_key = (axl.TitForTat(), axl.Random()) with self.assertRaises(ValueError): self.cache[invalid_key] = self.test_value diff --git a/axelrod/tests/unit/test_ecosystem.py b/axelrod/tests/unit/test_ecosystem.py index 8b3f39718..92098ba17 100644 --- a/axelrod/tests/unit/test_ecosystem.py +++ b/axelrod/tests/unit/test_ecosystem.py @@ -2,33 +2,33 @@ import unittest -import axelrod +import axelrod as axl class TestEcosystem(unittest.TestCase): @classmethod def setUpClass(cls): - cooperators = axelrod.Tournament( + cooperators = axl.Tournament( players=[ - axelrod.Cooperator(), - axelrod.Cooperator(), - axelrod.Cooperator(), - axelrod.Cooperator(), + axl.Cooperator(), + axl.Cooperator(), + axl.Cooperator(), + axl.Cooperator(), ] ) - defector_wins = axelrod.Tournament( + defector_wins = axl.Tournament( players=[ - axelrod.Cooperator(), - axelrod.Cooperator(), - axelrod.Cooperator(), - axelrod.Defector(), + axl.Cooperator(), + axl.Cooperator(), + axl.Cooperator(), + axl.Defector(), ] ) cls.res_cooperators = cooperators.play() cls.res_defector_wins = defector_wins.play() def test_default_population_sizes(self): - eco = axelrod.Ecosystem(self.res_cooperators) + eco = axl.Ecosystem(self.res_cooperators) pops = eco.population_sizes self.assertEqual(eco.num_players, 4) self.assertEqual(len(pops), 1) @@ -37,7 +37,7 @@ def test_default_population_sizes(self): self.assertEqual(list(set(pops[0])), [0.25]) def test_non_default_population_sizes(self): - eco = axelrod.Ecosystem( + eco = axl.Ecosystem( self.res_cooperators, population=[0.7, 0.25, 0.03, 0.02] ) pops = eco.population_sizes @@ -48,7 +48,7 @@ def test_non_default_population_sizes(self): self.assertEqual(pops[0], [0.7, 0.25, 0.03, 0.02]) def test_population_normalization(self): - eco = axelrod.Ecosystem(self.res_cooperators, population=[70, 25, 3, 2]) + eco = axl.Ecosystem(self.res_cooperators, population=[70, 25, 3, 2]) pops = eco.population_sizes self.assertEqual(eco.num_players, 4) self.assertEqual(len(pops), 1) @@ -59,7 +59,7 @@ def test_population_normalization(self): def test_results_and_population_of_different_sizes(self): self.assertRaises( TypeError, - axelrod.Ecosystem, + axl.Ecosystem, self.res_cooperators, population=[0.7, 0.2, 0.03, 0.1, 0.1], ) @@ -67,18 +67,18 @@ def test_results_and_population_of_different_sizes(self): def test_negative_populations(self): self.assertRaises( TypeError, - axelrod.Ecosystem, + axl.Ecosystem, self.res_cooperators, population=[0.7, -0.2, 0.03, 0.2], ) def test_fitness_function(self): fitness = lambda p: 2 * p - eco = axelrod.Ecosystem(self.res_cooperators, fitness=fitness) + eco = axl.Ecosystem(self.res_cooperators, fitness=fitness) self.assertTrue(eco.fitness(10), 20) def test_cooperators_are_stable_over_time(self): - eco = axelrod.Ecosystem(self.res_cooperators) + eco = axl.Ecosystem(self.res_cooperators) eco.reproduce(100) pops = eco.population_sizes self.assertEqual(len(pops), 101) @@ -88,7 +88,7 @@ def test_cooperators_are_stable_over_time(self): self.assertEqual(list(set(p)), [0.25]) def test_defector_wins_with_only_cooperators(self): - eco = axelrod.Ecosystem(self.res_defector_wins) + eco = axl.Ecosystem(self.res_defector_wins) eco.reproduce(1000) pops = eco.population_sizes self.assertEqual(len(pops), 1001) diff --git a/axelrod/tests/unit/test_eigen.py b/axelrod/tests/unit/test_eigen.py index f95c57e83..36f0bf5b9 100644 --- a/axelrod/tests/unit/test_eigen.py +++ b/axelrod/tests/unit/test_eigen.py @@ -3,9 +3,11 @@ import unittest import numpy -from axelrod.eigen import _normalise, principal_eigenvector from numpy.testing import assert_array_almost_equal +from axelrod.eigen import _normalise, principal_eigenvector + + class FunctionCases(unittest.TestCase): def test_identity_matrices(self): diff --git a/axelrod/tests/unit/test_filters.py b/axelrod/tests/unit/test_filters.py index 245a1f9cb..f11d80660 100644 --- a/axelrod/tests/unit/test_filters.py +++ b/axelrod/tests/unit/test_filters.py @@ -1,6 +1,6 @@ import unittest -from axelrod import filtered_strategies +import axelrod as axl from axelrod.strategies._filters import * from hypothesis import example, given, settings @@ -156,14 +156,14 @@ class UsesLengthTestStrategy(object): uses_length_filterset = {"stochastic": True, "makes_use_of": ["length"]} self.assertEqual( - filtered_strategies(stochastic_filterset, strategies), + axl.filtered_strategies(stochastic_filterset, strategies), [StochasticTestStrategy, UsesLengthTestStrategy], ) self.assertEqual( - filtered_strategies(deterministic_filterset, strategies), + axl.filtered_strategies(deterministic_filterset, strategies), [MemoryDepth2TestStrategy], ) self.assertEqual( - filtered_strategies(uses_length_filterset, strategies), + axl.filtered_strategies(uses_length_filterset, strategies), [UsesLengthTestStrategy], ) diff --git a/axelrod/tests/unit/test_fingerprint.py b/axelrod/tests/unit/test_fingerprint.py index bab8b5055..a8ed747e2 100644 --- a/axelrod/tests/unit/test_fingerprint.py +++ b/axelrod/tests/unit/test_fingerprint.py @@ -1,17 +1,22 @@ -import os import unittest -from tempfile import mkstemp from unittest.mock import patch +import os + +from tempfile import mkstemp + import matplotlib.pyplot + import numpy as np -from hypothesis import given, settings import axelrod as axl from axelrod.fingerprint import AshlockFingerprint, Point, TransitiveFingerprint from axelrod.strategy_transformers import DualTransformer, JossAnnTransformer from axelrod.tests.property import strategy_lists +from hypothesis import given, settings + + C, D = axl.Action.C, axl.Action.D diff --git a/axelrod/tests/unit/test_game.py b/axelrod/tests/unit/test_game.py index b145472b9..c52d22b7b 100644 --- a/axelrod/tests/unit/test_game.py +++ b/axelrod/tests/unit/test_game.py @@ -1,12 +1,12 @@ import unittest -from axelrod import Action, Game +import axelrod as axl from axelrod.tests.property import games from hypothesis import given, settings from hypothesis.strategies import integers -C, D = Action.C, Action.D +C, D = axl.Action.C, axl.Action.D class TestGame(unittest.TestCase): @@ -17,29 +17,29 @@ def test_default_scores(self): (D, D): (1, 1), (C, C): (3, 3), } - self.assertEqual(Game().scores, expected_scores) + self.assertEqual(axl.Game().scores, expected_scores) def test_default_RPST(self): expected_values = (3, 1, 0, 5) - self.assertEqual(Game().RPST(), expected_values) + self.assertEqual(axl.Game().RPST(), expected_values) def test_default_score(self): - game = Game() + game = axl.Game() self.assertEqual(game.score((C, C)), (3, 3)) self.assertEqual(game.score((D, D)), (1, 1)) self.assertEqual(game.score((C, D)), (0, 5)) self.assertEqual(game.score((D, C)), (5, 0)) def test_default_equality(self): - self.assertEqual(Game(), Game()) + self.assertEqual(axl.Game(), axl.Game()) def test_not_default_equality(self): - self.assertEqual(Game(1, 2, 3, 4), Game(1, 2, 3, 4)) - self.assertNotEqual(Game(1, 2, 3, 4), Game(1, 2, 3, 5)) - self.assertNotEqual(Game(1, 2, 3, 4), Game()) + self.assertEqual(axl.Game(1, 2, 3, 4), axl.Game(1, 2, 3, 4)) + self.assertNotEqual(axl.Game(1, 2, 3, 4), axl.Game(1, 2, 3, 5)) + self.assertNotEqual(axl.Game(1, 2, 3, 4), axl.Game()) def test_wrong_class_equality(self): - self.assertNotEqual(Game(), "wrong class") + self.assertNotEqual(axl.Game(), "wrong class") @given(r=integers(), p=integers(), s=integers(), t=integers()) @settings(max_examples=5) @@ -51,21 +51,21 @@ def test_random_init(self, r, p, s, t): (D, D): (p, p), (C, C): (r, r), } - game = Game(r, s, t, p) + game = axl.Game(r, s, t, p) self.assertEqual(game.scores, expected_scores) @given(r=integers(), p=integers(), s=integers(), t=integers()) @settings(max_examples=5) def test_random_RPST(self, r, p, s, t): """Test RPST method with random scores using the hypothesis library.""" - game = Game(r, s, t, p) + game = axl.Game(r, s, t, p) self.assertEqual(game.RPST(), (r, p, s, t)) @given(r=integers(), p=integers(), s=integers(), t=integers()) @settings(max_examples=5) def test_random_score(self, r, p, s, t): """Test score method with random scores using the hypothesis library.""" - game = Game(r, s, t, p) + game = axl.Game(r, s, t, p) self.assertEqual(game.score((C, C)), (r, r)) self.assertEqual(game.score((D, D)), (p, p)) self.assertEqual(game.score((C, D)), (s, t)) diff --git a/axelrod/tests/unit/test_graph.py b/axelrod/tests/unit/test_graph.py index f1b594a5b..1e0666ee6 100644 --- a/axelrod/tests/unit/test_graph.py +++ b/axelrod/tests/unit/test_graph.py @@ -1,7 +1,8 @@ -from collections import defaultdict import unittest -from axelrod import graph +from collections import defaultdict + +import axelrod as axl class TestGraph(unittest.TestCase): @@ -18,7 +19,7 @@ def assert_in_mapping(self, g, expected_in_mapping): self.assertDictEqual(g.in_dict(node), in_dict) def test_undirected_graph_with_no_vertices(self): - g = graph.Graph() + g = axl.graph.Graph() self.assertFalse(g.directed) self.assertIsInstance(g.out_mapping, defaultdict) self.assertIsInstance(g.in_mapping, defaultdict) @@ -26,7 +27,7 @@ def test_undirected_graph_with_no_vertices(self): self.assertEqual(str(g), "") def test_directed_graph_with_no_vertices(self): - g = graph.Graph(directed=True) + g = axl.graph.Graph(directed=True) self.assertTrue(g.directed) self.assertIsInstance(g.out_mapping, defaultdict) self.assertIsInstance(g.in_mapping, defaultdict) @@ -34,7 +35,7 @@ def test_directed_graph_with_no_vertices(self): self.assertEqual(str(g), "") def test_undirected_graph_with_vertices_and_unweighted_edges(self): - g = graph.Graph(edges=[[1, 2], [2, 3]]) + g = axl.graph.Graph(edges=[[1, 2], [2, 3]]) self.assertFalse(g.directed) self.assertEqual(str(g), "") @@ -43,7 +44,7 @@ def test_undirected_graph_with_vertices_and_unweighted_edges(self): self.assert_in_mapping(g, {1: {2: None}, 2: {1: None, 3: None}, 3: {2: None}}) def test_undirected_graph_with_vertices_and_weighted_edges(self): - g = graph.Graph(edges=[[1, 2, 10], [2, 3, 5]]) + g = axl.graph.Graph(edges=[[1, 2, 10], [2, 3, 5]]) self.assertFalse(g.directed) self.assertEqual(str(g), "") @@ -52,7 +53,7 @@ def test_undirected_graph_with_vertices_and_weighted_edges(self): self.assert_in_mapping(g, {1: {2: 10}, 2: {1: 10, 3: 5}, 3: {2: 5}}) def test_directed_graph_vertices_and_weighted_edges(self): - g = graph.Graph(edges=[[1, 2, 10], [2, 3, 5]], directed=True) + g = axl.graph.Graph(edges=[[1, 2, 10], [2, 3, 5]], directed=True) self.assertTrue(g.directed) self.assertEqual(str(g), "") @@ -62,7 +63,7 @@ def test_directed_graph_vertices_and_weighted_edges(self): def test_add_loops(self): edges = [(0, 1), (0, 2), (1, 2)] - g = graph.Graph(edges) + g = axl.graph.Graph(edges) g.add_loops() self.assertEqual( list(sorted(g._edges)), @@ -87,7 +88,7 @@ def test_add_loops_with_existing_loop_and_using_strings(self): """In this case there is already a loop present; also uses strings instead of integers as the hashable.""" edges = [("a", "b"), ("b", "a"), ("c", "c")] - g = graph.Graph(edges) + g = axl.graph.Graph(edges) g.add_loops() self.assertEqual( list(sorted(g._edges)), @@ -97,40 +98,40 @@ def test_add_loops_with_existing_loop_and_using_strings(self): class TestCycle(unittest.TestCase): def test_length_1_directed(self): - g = graph.cycle(1, directed=True) + g = axl.graph.cycle(1, directed=True) self.assertEqual(g.vertices, [0]) self.assertEqual(g.edges, [(0, 0)]) self.assertEqual(g.directed, True) def test_length_1_undirected(self): - g = graph.cycle(1, directed=False) + g = axl.graph.cycle(1, directed=False) self.assertEqual(g.vertices, [0]) self.assertEqual(g.edges, [(0, 0)]) self.assertEqual(g.directed, False) def test_length_2_directed(self): - g = graph.cycle(2, directed=True) + g = axl.graph.cycle(2, directed=True) self.assertEqual(g.vertices, [0, 1]) self.assertEqual(g.edges, [(0, 1), (1, 0)]) def test_length_2_undirected(self): - g = graph.cycle(2, directed=False) + g = axl.graph.cycle(2, directed=False) self.assertEqual(g.vertices, [0, 1]) self.assertEqual(g.edges, [(0, 1), (1, 0)]) def test_length_3_directed(self): - g = graph.cycle(3, directed=True) + g = axl.graph.cycle(3, directed=True) self.assertEqual(g.vertices, [0, 1, 2]) self.assertEqual(g.edges, [(0, 1), (1, 2), (2, 0)]) def test_length_3_undirected(self): - g = graph.cycle(3, directed=False) + g = axl.graph.cycle(3, directed=False) edges = [(0, 1), (1, 0), (1, 2), (2, 1), (2, 0), (0, 2)] self.assertEqual(g.vertices, [0, 1, 2]) self.assertEqual(g.edges, edges) def test_length_4_directed(self): - g = graph.cycle(4, directed=True) + g = axl.graph.cycle(4, directed=True) self.assertEqual(g.vertices, [0, 1, 2, 3]) self.assertEqual(g.edges, [(0, 1), (1, 2), (2, 3), (3, 0)]) self.assertEqual(g.out_vertices(0), [1]) @@ -143,7 +144,7 @@ def test_length_4_directed(self): self.assertEqual(g.in_vertices(3), [2]) def test_length_4_undirected(self): - g = graph.cycle(4, directed=False) + g = axl.graph.cycle(4, directed=False) edges = [(0, 1), (1, 0), (1, 2), (2, 1), (2, 3), (3, 2), (3, 0), (0, 3)] self.assertEqual(g.vertices, [0, 1, 2, 3]) self.assertEqual(g.edges, edges) @@ -155,20 +156,20 @@ def test_length_4_undirected(self): class TestComplete(unittest.TestCase): def test_size_2(self): - g = graph.complete_graph(2, loops=False) + g = axl.graph.complete_graph(2, loops=False) self.assertEqual(g.vertices, [0, 1]) self.assertEqual(g.edges, [(0, 1), (1, 0)]) self.assertEqual(g.directed, False) def test_size_3(self): - g = graph.complete_graph(3, loops=False) + g = axl.graph.complete_graph(3, loops=False) self.assertEqual(g.vertices, [0, 1, 2]) edges = [(0, 1), (1, 0), (0, 2), (2, 0), (1, 2), (2, 1)] self.assertEqual(g.edges, edges) self.assertEqual(g.directed, False) def test_size_4(self): - g = graph.complete_graph(4, loops=False) + g = axl.graph.complete_graph(4, loops=False) self.assertEqual(g.vertices, [0, 1, 2, 3]) edges = [ (0, 1), @@ -202,20 +203,20 @@ def test_size_4(self): self.assertEqual(set(g.in_vertices(vertex)), set(neighbors)) def test_size_2_with_loops(self): - g = graph.complete_graph(2, loops=True) + g = axl.graph.complete_graph(2, loops=True) self.assertEqual(g.vertices, [0, 1]) self.assertEqual(g.edges, [(0, 1), (1, 0), (0, 0), (1, 1)]) self.assertEqual(g.directed, False) def test_size_3_with_loops(self): - g = graph.complete_graph(3, loops=True) + g = axl.graph.complete_graph(3, loops=True) self.assertEqual(g.vertices, [0, 1, 2]) edges = [(0, 1), (1, 0), (0, 2), (2, 0), (1, 2), (2, 1), (0, 0), (1, 1), (2, 2)] self.assertEqual(g.edges, edges) self.assertEqual(g.directed, False) def test_size_4_with_loops(self): - g = graph.complete_graph(4, loops=True) + g = axl.graph.complete_graph(4, loops=True) self.assertEqual(g.vertices, [0, 1, 2, 3]) edges = [ (0, 1), @@ -245,7 +246,7 @@ def test_size_4_with_loops(self): class TestAttachedComplete(unittest.TestCase): def test_size_2(self): - g = graph.attached_complete_graphs(2, loops=False) + g = axl.graph.attached_complete_graphs(2, loops=False) self.assertEqual(g.vertices, ['0:0', '0:1', '1:0', '1:1']) self.assertEqual( g.edges, @@ -254,7 +255,7 @@ def test_size_2(self): self.assertEqual(g.directed, False) def test_size_3(self): - g = graph.attached_complete_graphs(3, loops=False) + g = axl.graph.attached_complete_graphs(3, loops=False) self.assertEqual(g.vertices, ['0:0', '0:1', '0:2', '1:0', '1:1', '1:2']) self.assertEqual( g.edges, @@ -276,7 +277,7 @@ def test_size_3(self): self.assertEqual(g.directed, False) def test_size_3_with_loops(self): - g = graph.attached_complete_graphs(3, loops=True) + g = axl.graph.attached_complete_graphs(3, loops=True) self.assertEqual(g.vertices, ['0:0', '0:1', '0:2', '1:0', '1:1', '1:2']) self.assertEqual( g.edges, diff --git a/axelrod/tests/unit/test_history.py b/axelrod/tests/unit/test_history.py index 5e1dc85a4..7c517958b 100644 --- a/axelrod/tests/unit/test_history.py +++ b/axelrod/tests/unit/test_history.py @@ -1,11 +1,11 @@ -from collections import Counter import unittest -import axelrod -from axelrod import Action +from collections import Counter + +import axelrod as axl from axelrod.history import History, LimitedHistory -C, D = Action.C, Action.D +C, D = axl.Action.C, axl.Action.D class TestHistory(unittest.TestCase): @@ -66,8 +66,8 @@ def test_counts(self): self.assertEqual(h3.defections, 2) def test_flip_plays(self): - player = axelrod.Alternator() - opponent = axelrod.Cooperator() + player = axl.Alternator() + opponent = axl.Cooperator() for _ in range(5): player.play(opponent) diff --git a/axelrod/tests/unit/test_interaction_utils.py b/axelrod/tests/unit/test_interaction_utils.py index 9f8ca4c0e..2e1d2c5e1 100644 --- a/axelrod/tests/unit/test_interaction_utils.py +++ b/axelrod/tests/unit/test_interaction_utils.py @@ -1,12 +1,10 @@ -import tempfile import unittest +import tempfile from collections import Counter -import axelrod -import axelrod.interaction_utils as iu -from axelrod import Action +import axelrod as axl -C, D = Action.C, Action.D +C, D = axl.Action.C, axl.Action.D class TestMatch(unittest.TestCase): @@ -47,43 +45,43 @@ class TestMatch(unittest.TestCase): def test_compute_scores(self): for inter, score in zip(self.interactions, self.scores): - self.assertEqual(score, iu.compute_scores(inter)) + self.assertEqual(score, axl.interaction_utils.compute_scores(inter)) def test_compute_final_score(self): for inter, final_score in zip(self.interactions, self.final_scores): - self.assertEqual(final_score, iu.compute_final_score(inter)) + self.assertEqual(final_score, axl.interaction_utils.compute_final_score(inter)) def test_compute_final_score_per_turn(self): for inter, final_score_per_round in zip( self.interactions, self.final_score_per_turn ): self.assertEqual( - final_score_per_round, iu.compute_final_score_per_turn(inter) + final_score_per_round, axl.interaction_utils.compute_final_score_per_turn(inter) ) def test_compute_winner_index(self): for inter, winner in zip(self.interactions, self.winners): - self.assertEqual(winner, iu.compute_winner_index(inter)) + self.assertEqual(winner, axl.interaction_utils.compute_winner_index(inter)) def test_compute_cooperations(self): for inter, coop in zip(self.interactions, self.cooperations): - self.assertEqual(coop, iu.compute_cooperations(inter)) + self.assertEqual(coop, axl.interaction_utils.compute_cooperations(inter)) def test_compute_normalised_cooperations(self): for inter, coop in zip(self.interactions, self.normalised_cooperations): - self.assertEqual(coop, iu.compute_normalised_cooperation(inter)) + self.assertEqual(coop, axl.interaction_utils.compute_normalised_cooperation(inter)) def test_compute_state_distribution(self): for inter, dist in zip(self.interactions, self.state_distribution): - self.assertEqual(dist, iu.compute_state_distribution(inter)) + self.assertEqual(dist, axl.interaction_utils.compute_state_distribution(inter)) def test_compute_normalised_state_distribution(self): for inter, dist in zip(self.interactions, self.normalised_state_distribution): - self.assertEqual(dist, iu.compute_normalised_state_distribution(inter)) + self.assertEqual(dist, axl.interaction_utils.compute_normalised_state_distribution(inter)) def test_compute_state_to_action_distribution(self): for inter, dist in zip(self.interactions, self.state_to_action_distribution): - self.assertEqual(dist, iu.compute_state_to_action_distribution(inter)) + self.assertEqual(dist, axl.interaction_utils.compute_state_to_action_distribution(inter)) inter = [(C, D), (D, C), (C, D), (D, C), (D, D), (C, C), (C, D)] expected_dist = [ Counter( @@ -98,14 +96,14 @@ def test_compute_state_to_action_distribution(self): Counter({((C, C), D): 1, ((C, D), C): 2, ((D, C), D): 2, ((D, D), C): 1}), ] - self.assertEqual(expected_dist, iu.compute_state_to_action_distribution(inter)) + self.assertEqual(expected_dist, axl.interaction_utils.compute_state_to_action_distribution(inter)) def test_compute_normalised_state_to_action_distribution(self): for inter, dist in zip( self.interactions, self.normalised_state_to_action_distribution ): self.assertEqual( - dist, iu.compute_normalised_state_to_action_distribution(inter) + dist, axl.interaction_utils.compute_normalised_state_to_action_distribution(inter) ) inter = [(C, D), (D, C), (C, D), (D, C), (D, D), (C, C), (C, D)] expected_dist = [ @@ -121,17 +119,17 @@ def test_compute_normalised_state_to_action_distribution(self): Counter({((C, C), D): 1, ((C, D), C): 1, ((D, C), D): 1, ((D, D), C): 1}), ] self.assertEqual( - expected_dist, iu.compute_normalised_state_to_action_distribution(inter) + expected_dist, axl.interaction_utils.compute_normalised_state_to_action_distribution(inter) ) def test_compute_sparklines(self): for inter, spark in zip(self.interactions, self.sparklines): - self.assertEqual(spark, iu.compute_sparklines(inter)) + self.assertEqual(spark, axl.interaction_utils.compute_sparklines(inter)) def test_read_interactions_from_file(self): tmp_file = tempfile.NamedTemporaryFile(mode="w", delete=False) - players = [axelrod.Cooperator(), axelrod.Defector()] - tournament = axelrod.Tournament(players=players, turns=2, repetitions=3) + players = [axl.Cooperator(), axl.Defector()] + tournament = axl.Tournament(players=players, turns=2, repetitions=3) tournament.play(filename=tmp_file.name) tmp_file.close() expected_interactions = { @@ -139,10 +137,10 @@ def test_read_interactions_from_file(self): (0, 1): [[(C, D), (C, D)] for _ in range(3)], (1, 1): [[(D, D), (D, D)] for _ in range(3)], } - interactions = iu.read_interactions_from_file(tmp_file.name, progress_bar=False) + interactions = axl.interaction_utils.read_interactions_from_file(tmp_file.name, progress_bar=False) self.assertEqual(expected_interactions, interactions) def test_string_to_interactions(self): string = "CDCDDD" interactions = [(C, D), (C, D), (D, D)] - self.assertEqual(iu.string_to_interactions(string), interactions) + self.assertEqual(axl.interaction_utils.string_to_interactions(string), interactions) diff --git a/axelrod/tests/unit/test_match.py b/axelrod/tests/unit/test_match.py index 3a1192f27..71913d103 100644 --- a/axelrod/tests/unit/test_match.py +++ b/axelrod/tests/unit/test_match.py @@ -1,23 +1,23 @@ import unittest + from collections import Counter -import axelrod -from axelrod import Action +import axelrod as axl from axelrod.deterministic_cache import DeterministicCache from axelrod.tests.property import games from hypothesis import example, given from hypothesis.strategies import assume, floats, integers -C, D = Action.C, Action.D +C, D = axl.Action.C, axl.Action.D class TestMatch(unittest.TestCase): @given(turns=integers(min_value=1, max_value=200), game=games()) - @example(turns=5, game=axelrod.DefaultGame) + @example(turns=5, game=axl.DefaultGame) def test_init(self, turns, game): - p1, p2 = axelrod.Cooperator(), axelrod.Cooperator() - match = axelrod.Match((p1, p2), turns, game=game) + p1, p2 = axl.Cooperator(), axl.Cooperator() + match = axl.Match((p1, p2), turns, game=game) self.assertEqual(match.result, []) self.assertEqual(match.players, [p1, p2]) self.assertEqual(match.turns, turns) @@ -30,8 +30,8 @@ def test_init(self, turns, game): @given(prob_end=floats(min_value=0, max_value=1), game=games()) def test_init_with_prob_end(self, prob_end, game): - p1, p2 = axelrod.Cooperator(), axelrod.Cooperator() - match = axelrod.Match((p1, p2), prob_end=prob_end, game=game) + p1, p2 = axl.Cooperator(), axl.Cooperator() + match = axl.Match((p1, p2), prob_end=prob_end, game=game) self.assertEqual(match.result, []) self.assertEqual(match.players, [p1, p2]) self.assertEqual(match.turns, float("inf")) @@ -48,8 +48,8 @@ def test_init_with_prob_end(self, prob_end, game): game=games(), ) def test_init_with_prob_end_and_turns(self, turns, prob_end, game): - p1, p2 = axelrod.Cooperator(), axelrod.Cooperator() - match = axelrod.Match((p1, p2), turns=turns, prob_end=prob_end, game=game) + p1, p2 = axl.Cooperator(), axl.Cooperator() + match = axl.Match((p1, p2), turns=turns, prob_end=prob_end, game=game) self.assertEqual(match.result, []) self.assertEqual(match.players, [p1, p2]) self.assertEqual(match.turns, turns) @@ -61,17 +61,17 @@ def test_init_with_prob_end_and_turns(self, turns, prob_end, game): self.assertEqual(match._cache, {}) def test_default_init(self): - p1, p2 = axelrod.Cooperator(), axelrod.Cooperator() - match = axelrod.Match((p1, p2)) + p1, p2 = axl.Cooperator(), axl.Cooperator() + match = axl.Match((p1, p2)) self.assertEqual(match.result, []) self.assertEqual(match.players, [p1, p2]) - self.assertEqual(match.turns, axelrod.DEFAULT_TURNS) + self.assertEqual(match.turns, axl.DEFAULT_TURNS) self.assertEqual(match.prob_end, 0) self.assertEqual(match.noise, 0) self.assertEqual(match.game.RPST(), (3, 1, 0, 5)) self.assertEqual( - match.players[0].match_attributes["length"], axelrod.DEFAULT_TURNS + match.players[0].match_attributes["length"], axl.DEFAULT_TURNS ) self.assertEqual(match._cache, {}) @@ -80,11 +80,11 @@ def test_example_prob_end(self): Test that matches have diff length and also that cache has recorded the outcomes """ - p1, p2 = axelrod.Cooperator(), axelrod.Cooperator() - match = axelrod.Match((p1, p2), prob_end=0.5) + p1, p2 = axl.Cooperator(), axl.Cooperator() + match = axl.Match((p1, p2), prob_end=0.5) expected_lengths = [3, 1, 5] for seed, expected_length in zip(range(3), expected_lengths): - axelrod.seed(seed) + axl.seed(seed) self.assertEqual(match.players[0].match_attributes["length"], float("inf")) self.assertEqual(len(match.play()), expected_length) self.assertEqual(match.noise, 0) @@ -93,11 +93,11 @@ def test_example_prob_end(self): self.assertEqual(match._cache[(p1, p2)], [(C, C)] * 5) @given(turns=integers(min_value=1, max_value=200), game=games()) - @example(turns=5, game=axelrod.DefaultGame) + @example(turns=5, game=axl.DefaultGame) def test_non_default_attributes(self, turns, game): - p1, p2 = axelrod.Cooperator(), axelrod.Cooperator() + p1, p2 = axl.Cooperator(), axl.Cooperator() match_attributes = {"length": 500, "game": game, "noise": 0.5} - match = axelrod.Match( + match = axl.Match( (p1, p2), turns, game=game, match_attributes=match_attributes ) self.assertEqual(match.players[0].match_attributes["length"], 500) @@ -106,16 +106,16 @@ def test_non_default_attributes(self, turns, game): @given(turns=integers(min_value=1, max_value=200)) @example(turns=5) def test_len(self, turns): - p1, p2 = axelrod.Cooperator(), axelrod.Cooperator() - match = axelrod.Match((p1, p2), turns) + p1, p2 = axl.Cooperator(), axl.Cooperator() + match = axl.Match((p1, p2), turns) self.assertEqual(len(match), turns) def test_len_error(self): """ Length is not defined if it is infinite. """ - p1, p2 = axelrod.Cooperator(), axelrod.Cooperator() - match = axelrod.Match((p1, p2), prob_end=0.5) + p1, p2 = axl.Cooperator(), axl.Cooperator() + match = axl.Match((p1, p2), prob_end=0.5) with self.assertRaises(TypeError): len(match) @@ -124,15 +124,15 @@ def test_stochastic(self, p): assume(0 < p < 1) - p1, p2 = axelrod.Cooperator(), axelrod.Cooperator() - match = axelrod.Match((p1, p2), 5) + p1, p2 = axl.Cooperator(), axl.Cooperator() + match = axl.Match((p1, p2), 5) self.assertFalse(match._stochastic) - match = axelrod.Match((p1, p2), 5, noise=p) + match = axl.Match((p1, p2), 5, noise=p) self.assertTrue(match._stochastic) - p1 = axelrod.Random() - match = axelrod.Match((p1, p2), 5) + p1 = axl.Random() + match = axl.Match((p1, p2), 5) self.assertTrue(match._stochastic) @given(p=floats(min_value=0, max_value=1)) @@ -140,36 +140,36 @@ def test_cache_update_required(self, p): assume(0 < p < 1) - p1, p2 = axelrod.Cooperator(), axelrod.Cooperator() - match = axelrod.Match((p1, p2), 5, noise=p) + p1, p2 = axl.Cooperator(), axl.Cooperator() + match = axl.Match((p1, p2), 5, noise=p) self.assertFalse(match._cache_update_required) cache = DeterministicCache() cache.mutable = False - match = axelrod.Match((p1, p2), 5, deterministic_cache=cache) + match = axl.Match((p1, p2), 5, deterministic_cache=cache) self.assertFalse(match._cache_update_required) - match = axelrod.Match((p1, p2), 5) + match = axl.Match((p1, p2), 5) self.assertTrue(match._cache_update_required) - p1 = axelrod.Random() - match = axelrod.Match((p1, p2), 5) + p1 = axl.Random() + match = axl.Match((p1, p2), 5) self.assertFalse(match._cache_update_required) def test_play(self): cache = DeterministicCache() - players = (axelrod.Cooperator(), axelrod.Defector()) - match = axelrod.Match(players, 3, deterministic_cache=cache) + players = (axl.Cooperator(), axl.Defector()) + match = axl.Match(players, 3, deterministic_cache=cache) expected_result = [(C, D), (C, D), (C, D)] self.assertEqual(match.play(), expected_result) self.assertEqual( - cache[(axelrod.Cooperator(), axelrod.Defector())], expected_result + cache[(axl.Cooperator(), axl.Defector())], expected_result ) # a deliberately incorrect result so we can tell it came from the cache expected_result = [(C, C), (D, D), (D, C), (C, C), (C, D)] - cache[(axelrod.Cooperator(), axelrod.Defector())] = expected_result - match = axelrod.Match(players, 3, deterministic_cache=cache) + cache[(axl.Cooperator(), axl.Defector())] = expected_result + match = axl.Match(players, 3, deterministic_cache=cache) self.assertEqual(match.play(), expected_result[:3]) def test_cache_grows(self): @@ -179,8 +179,8 @@ def test_cache_grows(self): the cache. """ cache = DeterministicCache() - players = (axelrod.Cooperator(), axelrod.Defector()) - match = axelrod.Match(players, 3, deterministic_cache=cache) + players = (axl.Cooperator(), axl.Defector()) + match = axl.Match(players, 3, deterministic_cache=cache) expected_result_5_turn = [(C, D), (C, D), (C, D), (C, D), (C, D)] expected_result_3_turn = [(C, D), (C, D), (C, D)] self.assertEqual(match.play(), expected_result_3_turn) @@ -188,7 +188,7 @@ def test_cache_grows(self): self.assertEqual(match.play(), expected_result_5_turn) # The cache should now hold the 5-turn result.. self.assertEqual( - cache[(axelrod.Cooperator(), axelrod.Defector())], + cache[(axl.Cooperator(), axl.Defector())], expected_result_5_turn ) @@ -199,8 +199,8 @@ def test_cache_doesnt_shrink(self): shorter result. """ cache = DeterministicCache() - players = (axelrod.Cooperator(), axelrod.Defector()) - match = axelrod.Match(players, 5, deterministic_cache=cache) + players = (axl.Cooperator(), axl.Defector()) + match = axl.Match(players, 5, deterministic_cache=cache) expected_result_5_turn = [(C, D), (C, D), (C, D), (C, D), (C, D)] expected_result_3_turn = [(C, D), (C, D), (C, D)] self.assertEqual(match.play(), expected_result_5_turn) @@ -208,119 +208,119 @@ def test_cache_doesnt_shrink(self): self.assertEqual(match.play(), expected_result_3_turn) # The cache should still hold the 5. self.assertEqual( - cache[(axelrod.Cooperator(), axelrod.Defector())], + cache[(axl.Cooperator(), axl.Defector())], expected_result_5_turn ) def test_scores(self): - player1 = axelrod.TitForTat() - player2 = axelrod.Defector() - match = axelrod.Match((player1, player2), 3) + player1 = axl.TitForTat() + player2 = axl.Defector() + match = axl.Match((player1, player2), 3) self.assertEqual(match.scores(), []) match.play() self.assertEqual(match.scores(), [(0, 5), (1, 1), (1, 1)]) def test_final_score(self): - player1 = axelrod.TitForTat() - player2 = axelrod.Defector() + player1 = axl.TitForTat() + player2 = axl.Defector() - match = axelrod.Match((player1, player2), 3) + match = axl.Match((player1, player2), 3) self.assertEqual(match.final_score(), None) match.play() self.assertEqual(match.final_score(), (2, 7)) - match = axelrod.Match((player2, player1), 3) + match = axl.Match((player2, player1), 3) self.assertEqual(match.final_score(), None) match.play() self.assertEqual(match.final_score(), (7, 2)) def test_final_score_per_turn(self): turns = 3 - player1 = axelrod.TitForTat() - player2 = axelrod.Defector() + player1 = axl.TitForTat() + player2 = axl.Defector() - match = axelrod.Match((player1, player2), turns) + match = axl.Match((player1, player2), turns) self.assertEqual(match.final_score_per_turn(), None) match.play() self.assertEqual(match.final_score_per_turn(), (2 / turns, 7 / turns)) - match = axelrod.Match((player2, player1), turns) + match = axl.Match((player2, player1), turns) self.assertEqual(match.final_score_per_turn(), None) match.play() self.assertEqual(match.final_score_per_turn(), (7 / turns, 2 / turns)) def test_winner(self): - player1 = axelrod.TitForTat() - player2 = axelrod.Defector() + player1 = axl.TitForTat() + player2 = axl.Defector() - match = axelrod.Match((player1, player2), 3) + match = axl.Match((player1, player2), 3) self.assertEqual(match.winner(), None) match.play() self.assertEqual(match.winner(), player2) - match = axelrod.Match((player2, player1), 3) + match = axl.Match((player2, player1), 3) self.assertEqual(match.winner(), None) match.play() self.assertEqual(match.winner(), player2) - player1 = axelrod.Defector() - match = axelrod.Match((player1, player2), 3) + player1 = axl.Defector() + match = axl.Match((player1, player2), 3) self.assertEqual(match.winner(), None) match.play() self.assertEqual(match.winner(), False) def test_cooperation(self): turns = 3 - player1 = axelrod.Cooperator() - player2 = axelrod.Alternator() + player1 = axl.Cooperator() + player2 = axl.Alternator() - match = axelrod.Match((player1, player2), turns) + match = axl.Match((player1, player2), turns) self.assertEqual(match.cooperation(), None) match.play() self.assertEqual(match.cooperation(), (3, 2)) - player1 = axelrod.Alternator() - player2 = axelrod.Defector() + player1 = axl.Alternator() + player2 = axl.Defector() - match = axelrod.Match((player1, player2), turns) + match = axl.Match((player1, player2), turns) self.assertEqual(match.cooperation(), None) match.play() self.assertEqual(match.cooperation(), (2, 0)) def test_normalised_cooperation(self): turns = 3 - player1 = axelrod.Cooperator() - player2 = axelrod.Alternator() + player1 = axl.Cooperator() + player2 = axl.Alternator() - match = axelrod.Match((player1, player2), turns) + match = axl.Match((player1, player2), turns) self.assertEqual(match.normalised_cooperation(), None) match.play() self.assertEqual(match.normalised_cooperation(), (3 / turns, 2 / turns)) - player1 = axelrod.Alternator() - player2 = axelrod.Defector() + player1 = axl.Alternator() + player2 = axl.Defector() - match = axelrod.Match((player1, player2), turns) + match = axl.Match((player1, player2), turns) self.assertEqual(match.normalised_cooperation(), None) match.play() self.assertEqual(match.normalised_cooperation(), (2 / turns, 0 / turns)) def test_state_distribution(self): turns = 3 - player1 = axelrod.Cooperator() - player2 = axelrod.Alternator() + player1 = axl.Cooperator() + player2 = axl.Alternator() - match = axelrod.Match((player1, player2), turns) + match = axl.Match((player1, player2), turns) self.assertEqual(match.state_distribution(), None) match.play() expected = Counter({(C, C): 2, (C, D): 1}) self.assertEqual(match.state_distribution(), expected) - player1 = axelrod.Alternator() - player2 = axelrod.Defector() + player1 = axl.Alternator() + player2 = axl.Defector() - match = axelrod.Match((player1, player2), turns) + match = axl.Match((player1, player2), turns) self.assertEqual(match.state_distribution(), None) match.play() @@ -329,20 +329,20 @@ def test_state_distribution(self): def test_normalised_state_distribution(self): turns = 3 - player1 = axelrod.Cooperator() - player2 = axelrod.Alternator() + player1 = axl.Cooperator() + player2 = axl.Alternator() - match = axelrod.Match((player1, player2), turns) + match = axl.Match((player1, player2), turns) self.assertEqual(match.normalised_state_distribution(), None) match.play() expected = Counter({(C, C): 2 / turns, (C, D): 1 / turns}) self.assertEqual(match.normalised_state_distribution(), expected) - player1 = axelrod.Alternator() - player2 = axelrod.Defector() + player1 = axl.Alternator() + player2 = axl.Defector() - match = axelrod.Match((player1, player2), turns) + match = axl.Match((player1, player2), turns) self.assertEqual(match.normalised_state_distribution(), None) match.play() @@ -350,8 +350,8 @@ def test_normalised_state_distribution(self): self.assertEqual(match.normalised_state_distribution(), expected) def test_sparklines(self): - players = (axelrod.Cooperator(), axelrod.Alternator()) - match = axelrod.Match(players, 4) + players = (axl.Cooperator(), axl.Alternator()) + match = axl.Match(players, 4) match.play() expected_sparklines = "████\n█ █ " self.assertEqual(match.sparklines(), expected_sparklines) @@ -367,11 +367,11 @@ def test_sample_length(self): (2, 0.6, 4), (3, 0.4, 1), ]: - axelrod.seed(seed) - self.assertEqual(axelrod.match.sample_length(prob_end), expected_length) + axl.seed(seed) + self.assertEqual(axl.match.sample_length(prob_end), expected_length) def test_sample_with_0_prob(self): - self.assertEqual(axelrod.match.sample_length(0), float("inf")) + self.assertEqual(axl.match.sample_length(0), float("inf")) def test_sample_with_1_prob(self): - self.assertEqual(axelrod.match.sample_length(1), 1) + self.assertEqual(axl.match.sample_length(1), 1) diff --git a/axelrod/tests/unit/test_match_generator.py b/axelrod/tests/unit/test_match_generator.py index 8b676b793..27faa78c0 100644 --- a/axelrod/tests/unit/test_match_generator.py +++ b/axelrod/tests/unit/test_match_generator.py @@ -1,21 +1,21 @@ import unittest -import axelrod +import axelrod as axl from axelrod.match_generator import graph_is_connected from hypothesis import example, given, settings from hypothesis.strategies import floats, integers test_strategies = [ - axelrod.Cooperator, - axelrod.TitForTat, - axelrod.Defector, - axelrod.Grudger, - axelrod.GoByMajority, + axl.Cooperator, + axl.TitForTat, + axl.Defector, + axl.Grudger, + axl.GoByMajority, ] test_turns = 100 test_repetitions = 20 -test_game = axelrod.Game() +test_game = axl.Game() class TestMatchGenerator(unittest.TestCase): @@ -24,7 +24,7 @@ def setUpClass(cls): cls.players = [s() for s in test_strategies] def test_build_single_match_params(self): - rr = axelrod.MatchGenerator( + rr = axl.MatchGenerator( players=self.players, turns=test_turns, game=test_game, @@ -38,14 +38,14 @@ def test_build_single_match_params(self): self.assertIsNone(match_params["prob_end"]) # Check that can build a match - players = [axelrod.Cooperator(), axelrod.Defector()] + players = [axl.Cooperator(), axl.Defector()] match_params["players"] = players - match = axelrod.Match(**match_params) - self.assertIsInstance(match, axelrod.Match) + match = axl.Match(**match_params) + self.assertIsInstance(match, axl.Match) self.assertEqual(len(match), test_turns) def test_build_single_match_params_with_noise(self): - rr = axelrod.MatchGenerator( + rr = axl.MatchGenerator( players=self.players, turns=test_turns, game=test_game, @@ -60,14 +60,14 @@ def test_build_single_match_params_with_noise(self): self.assertIsNone(match_params["prob_end"]) # Check that can build a match - players = [axelrod.Cooperator(), axelrod.Defector()] + players = [axl.Cooperator(), axl.Defector()] match_params["players"] = players - match = axelrod.Match(**match_params) - self.assertIsInstance(match, axelrod.Match) + match = axl.Match(**match_params) + self.assertIsInstance(match, axl.Match) self.assertEqual(len(match), test_turns) def test_build_single_match_params_with_prob_end(self): - rr = axelrod.MatchGenerator( + rr = axl.MatchGenerator( players=self.players, game=test_game, repetitions=test_repetitions, @@ -81,15 +81,15 @@ def test_build_single_match_params_with_prob_end(self): self.assertEqual(match_params["prob_end"], 0.5) # Check that can build a match - players = [axelrod.Cooperator(), axelrod.Defector()] + players = [axl.Cooperator(), axl.Defector()] match_params["players"] = players - match = axelrod.Match(**match_params) - self.assertIsInstance(match, axelrod.Match) + match = axl.Match(**match_params) + self.assertIsInstance(match, axl.Match) with self.assertRaises(TypeError): len(match) def test_build_single_match_params_with_prob_end_and_noise(self): - rr = axelrod.MatchGenerator( + rr = axl.MatchGenerator( players=self.players, game=test_game, repetitions=test_repetitions, @@ -104,15 +104,15 @@ def test_build_single_match_params_with_prob_end_and_noise(self): self.assertEqual(match_params["noise"], 0.5) # Check that can build a match - players = [axelrod.Cooperator(), axelrod.Defector()] + players = [axl.Cooperator(), axl.Defector()] match_params["players"] = players - match = axelrod.Match(**match_params) - self.assertIsInstance(match, axelrod.Match) + match = axl.Match(**match_params) + self.assertIsInstance(match, axl.Match) with self.assertRaises(TypeError): len(match) def test_build_single_match_params_with_prob_end_and_turns(self): - rr = axelrod.MatchGenerator( + rr = axl.MatchGenerator( players=self.players, game=test_game, repetitions=test_repetitions, @@ -127,16 +127,16 @@ def test_build_single_match_params_with_prob_end_and_turns(self): self.assertEqual(match_params["noise"], 0) # Check that can build a match - players = [axelrod.Cooperator(), axelrod.Defector()] + players = [axl.Cooperator(), axl.Defector()] match_params["players"] = players - match = axelrod.Match(**match_params) - self.assertIsInstance(match, axelrod.Match) + match = axl.Match(**match_params) + self.assertIsInstance(match, axl.Match) self.assertIsInstance(len(match), int) self.assertGreater(len(match), 0) self.assertLessEqual(len(match), 10) def test_build_single_match_params_with_fixed_length_unknown(self): - rr = axelrod.MatchGenerator( + rr = axl.MatchGenerator( players=self.players, game=test_game, repetitions=test_repetitions, @@ -152,10 +152,10 @@ def test_build_single_match_params_with_fixed_length_unknown(self): self.assertEqual(match_params["match_attributes"], {"length": float("inf")}) # Check that can build a match - players = [axelrod.Cooperator(), axelrod.Defector()] + players = [axl.Cooperator(), axl.Defector()] match_params["players"] = players - match = axelrod.Match(**match_params) - self.assertIsInstance(match, axelrod.Match) + match = axl.Match(**match_params) + self.assertIsInstance(match, axl.Match) self.assertEqual(len(match), 5) self.assertEqual(match.match_attributes, {"length": float("inf")}) @@ -163,7 +163,7 @@ def test_build_single_match_params_with_fixed_length_unknown(self): @settings(max_examples=5) @example(repetitions=test_repetitions) def test_build_match_chunks(self, repetitions): - rr = axelrod.MatchGenerator( + rr = axl.MatchGenerator( players=self.players, turns=test_turns, game=test_game, @@ -185,7 +185,7 @@ def test_build_match_chunks(self, repetitions): @example(repetitions=test_repetitions) def test_spatial_build_match_chunks(self, repetitions): cycle = [(0, 1), (1, 2), (2, 3), (3, 4), (4, 1)] - rr = axelrod.MatchGenerator( + rr = axl.MatchGenerator( players=self.players, turns=test_turns, game=test_game, @@ -204,7 +204,7 @@ def test_spatial_build_match_chunks(self, repetitions): def test_len(self): turns = 5 repetitions = 10 - rr = axelrod.MatchGenerator( + rr = axl.MatchGenerator( players=self.players, turns=test_turns, game=test_game, @@ -215,7 +215,7 @@ def test_len(self): def test_init_with_graph_edges_not_including_all_players(self): edges = [(0, 1), (1, 2)] with self.assertRaises(ValueError): - axelrod.MatchGenerator( + axl.MatchGenerator( players=self.players, repetitions=3, game=test_game, diff --git a/axelrod/tests/unit/test_mock_player.py b/axelrod/tests/unit/test_mock_player.py index b4abd48fd..457e77711 100644 --- a/axelrod/tests/unit/test_mock_player.py +++ b/axelrod/tests/unit/test_mock_player.py @@ -1,20 +1,20 @@ import unittest -from axelrod import Action, MockPlayer, Player +import axelrod as axl -C, D = Action.C, Action.D +C, D = axl.Action.C, axl.Action.D class TestMockPlayer(unittest.TestCase): def test_strategy(self): for action in [C, D]: - m = MockPlayer(actions=[action]) - p2 = Player() + m = axl.MockPlayer(actions=[action]) + p2 = axl.Player() self.assertEqual(action, m.strategy(p2)) actions = [C, C, D, D, C, C] - m = MockPlayer(actions=actions) - p2 = Player() + m = axl.MockPlayer(actions=actions) + p2 = axl.Player() for action in actions: self.assertEqual(action, m.strategy(p2)) diff --git a/axelrod/tests/unit/test_moran.py b/axelrod/tests/unit/test_moran.py index 786204cb7..d972b288f 100644 --- a/axelrod/tests/unit/test_moran.py +++ b/axelrod/tests/unit/test_moran.py @@ -1,24 +1,23 @@ +import unittest import itertools import random -import unittest from collections import Counter - -import axelrod import matplotlib.pyplot as plt -from axelrod import ApproximateMoranProcess, MoranProcess, Pdf + +import axelrod as axl from axelrod.moran import fitness_proportionate_selection from axelrod.tests.property import strategy_lists from hypothesis import example, given, settings -C, D = axelrod.Action.C, axelrod.Action.D +C, D = axl.Action.C, axl.Action.D class TestMoranProcess(unittest.TestCase): def test_init(self): - players = axelrod.Cooperator(), axelrod.Defector() - mp = MoranProcess(players) - self.assertEqual(mp.turns, axelrod.DEFAULT_TURNS) + players = axl.Cooperator(), axl.Defector() + mp = axl.MoranProcess(players) + self.assertEqual(mp.turns, axl.DEFAULT_TURNS) self.assertIsNone(mp.prob_end) self.assertIsNone(mp.game) self.assertEqual(mp.noise, 0) @@ -28,7 +27,7 @@ def test_init(self): 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.deterministic_cache, axl.DeterministicCache()) self.assertEqual( mp.mutation_targets, {"Cooperator": [players[1]], "Defector": [players[0]]} ) @@ -39,117 +38,117 @@ def test_init(self): self.assertEqual(mp.index, {0: 0, 1: 1}) # Test non default graph cases - players = axelrod.Cooperator(), axelrod.Defector(), axelrod.TitForTat() + players = axl.Cooperator(), axl.Defector(), axl.TitForTat() edges = [(0, 1), (2, 0), (1, 2)] - graph = axelrod.graph.Graph(edges, directed=True) - mp = MoranProcess(players, interaction_graph=graph) + graph = axl.graph.Graph(edges, directed=True) + mp = axl.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) + mp = axl.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_set_players(self): """Test that set players resets all players""" - players = axelrod.Cooperator(), axelrod.Defector() - mp = MoranProcess(players) + players = axl.Cooperator(), axl.Defector() + mp = axl.MoranProcess(players) players[0].history.append(C, D) mp.set_players() self.assertEqual(players[0].cooperations, 0) 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) + players = axl.Cooperator(), axl.Defector(), axl.TitForTat() + mp = axl.MoranProcess(players, mutation_rate=0.5) + axl.seed(0) self.assertEqual(mp.mutate(0), players[0]) - axelrod.seed(1) + axl.seed(1) self.assertEqual(mp.mutate(0), players[2]) - axelrod.seed(4) + axl.seed(4) self.assertEqual(mp.mutate(0), players[1]) 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) + players = axl.Cooperator(), axl.Defector(), axl.TitForTat() + mp = axl.MoranProcess(players, mutation_rate=0.5, mode="db") + axl.seed(1) self.assertEqual(mp.death(), 0) self.assertEqual(mp.dead, 0) - axelrod.seed(5) + axl.seed(5) self.assertEqual(mp.death(), 1) self.assertEqual(mp.dead, 1) - axelrod.seed(2) + axl.seed(2) self.assertEqual(mp.death(), 2) self.assertEqual(mp.dead, 2) def test_death_in_bd(self): - players = axelrod.Cooperator(), axelrod.Defector(), axelrod.TitForTat() + players = axl.Cooperator(), axl.Defector(), axl.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) + graph = axl.graph.Graph(edges, directed=True) + mp = axl.MoranProcess(players, mode="bd", interaction_graph=graph) + axl.seed(1) self.assertEqual(mp.death(0), 0) - axelrod.seed(5) + axl.seed(5) self.assertEqual(mp.death(0), 1) - axelrod.seed(2) + axl.seed(2) self.assertEqual(mp.death(0), 0) def test_birth_in_db(self): - players = axelrod.Cooperator(), axelrod.Defector(), axelrod.TitForTat() - mp = MoranProcess(players, mode="db") - axelrod.seed(1) + players = axl.Cooperator(), axl.Defector(), axl.TitForTat() + mp = axl.MoranProcess(players, mode="db") + axl.seed(1) self.assertEqual(mp.death(), 0) self.assertEqual(mp.birth(0), 2) def test_birth_in_bd(self): - players = axelrod.Cooperator(), axelrod.Defector(), axelrod.TitForTat() - mp = MoranProcess(players, mode="bd") - axelrod.seed(1) + players = axl.Cooperator(), axl.Defector(), axl.TitForTat() + mp = axl.MoranProcess(players, mode="bd") + axl.seed(1) self.assertEqual(mp.birth(), 0) def test_fixation_check(self): - players = axelrod.Cooperator(), axelrod.Cooperator() - mp = MoranProcess(players) + players = axl.Cooperator(), axl.Cooperator() + mp = axl.MoranProcess(players) self.assertTrue(mp.fixation_check()) - players = axelrod.Cooperator(), axelrod.Defector() - mp = MoranProcess(players) + players = axl.Cooperator(), axl.Defector() + mp = axl.MoranProcess(players) self.assertFalse(mp.fixation_check()) def test_next(self): - players = axelrod.Cooperator(), axelrod.Defector() - mp = MoranProcess(players) - self.assertIsInstance(next(mp), MoranProcess) + players = axl.Cooperator(), axl.Defector() + mp = axl.MoranProcess(players) + self.assertIsInstance(next(mp), axl.MoranProcess) def test_matchup_indices(self): - players = axelrod.Cooperator(), axelrod.Defector() - mp = MoranProcess(players) + players = axl.Cooperator(), axl.Defector() + mp = axl.MoranProcess(players) self.assertEqual(mp._matchup_indices(), {(0, 1)}) - players = axelrod.Cooperator(), axelrod.Defector(), axelrod.TitForTat() + players = axl.Cooperator(), axl.Defector(), axl.TitForTat() edges = [(0, 1), (2, 0), (1, 2)] - graph = axelrod.graph.Graph(edges, directed=True) - mp = MoranProcess(players, mode="bd", interaction_graph=graph) + graph = axl.graph.Graph(edges, directed=True) + mp = axl.MoranProcess(players, mode="bd", interaction_graph=graph) self.assertEqual(mp._matchup_indices(), {(0, 1), (1, 2), (2, 0)}) def test_fps(self): self.assertEqual(fitness_proportionate_selection([0, 0, 1]), 2) - axelrod.seed(1) + axl.seed(1) self.assertEqual(fitness_proportionate_selection([1, 1, 1]), 0) self.assertEqual(fitness_proportionate_selection([1, 1, 1]), 2) def test_exit_condition(self): - p1, p2 = axelrod.Cooperator(), axelrod.Cooperator() - mp = MoranProcess((p1, p2)) + p1, p2 = axl.Cooperator(), axl.Cooperator() + mp = axl.MoranProcess((p1, p2)) mp.play() self.assertEqual(len(mp), 1) def test_two_players(self): - p1, p2 = axelrod.Cooperator(), axelrod.Defector() - axelrod.seed(17) - mp = MoranProcess((p1, p2)) + p1, p2 = axl.Cooperator(), axl.Defector() + axl.seed(17) + mp = axl.MoranProcess((p1, p2)) populations = mp.play() self.assertEqual(len(mp), 5) self.assertEqual(len(populations), 5) @@ -157,9 +156,9 @@ def test_two_players(self): self.assertEqual(mp.winning_strategy_name, str(p2)) def test_two_prob_end(self): - p1, p2 = axelrod.Random(), axelrod.TitForTat() - axelrod.seed(0) - mp = MoranProcess((p1, p2), prob_end=0.5) + p1, p2 = axl.Random(), axl.TitForTat() + axl.seed(0) + mp = axl.MoranProcess((p1, p2), prob_end=0.5) populations = mp.play() self.assertEqual(len(mp), 4) self.assertEqual(len(populations), 4) @@ -168,20 +167,20 @@ def test_two_prob_end(self): 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) + p1, p2 = axl.Cooperator(), axl.Defector() + axl.seed(0) + game = axl.Game(r=4, p=2, s=1, t=6) + mp = axl.MoranProcess((p1, p2), turns=5, game=game) populations = mp.play() self.assertEqual(mp.winning_strategy_name, str(p1)) def test_death_birth(self): """Two player death-birth should fixate after one round.""" - p1, p2 = axelrod.Cooperator(), axelrod.Defector() + p1, p2 = axl.Cooperator(), axl.Defector() seeds = range(0, 20) for seed in seeds: - axelrod.seed(seed) - mp = MoranProcess((p1, p2), mode="db") + axl.seed(seed) + mp = axl.MoranProcess((p1, p2), mode="db") mp.play() self.assertIsNotNone(mp.winning_strategy_name) # Number of populations is 2: the original and the one after the first round. @@ -194,23 +193,23 @@ def test_death_birth_outcomes(self): players = [] N = 6 for _ in range(N // 2): - players.append(axelrod.Cooperator()) - players.append(axelrod.Defector()) + players.append(axl.Cooperator()) + players.append(axl.Defector()) for seed, outcome in seeds: - axelrod.seed(seed) - mp = MoranProcess(players, mode="bd") + axl.seed(seed) + mp = axl.MoranProcess(players, mode="bd") mp.play() winner = mp.winning_strategy_name - axelrod.seed(seed) - mp = MoranProcess(players, mode="db") + axl.seed(seed) + mp = axl.MoranProcess(players, mode="db") mp.play() winner2 = mp.winning_strategy_name self.assertEqual((winner == winner2), outcome) 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)) + p1, p2 = axl.Random(p=0.5), axl.Random(p=0.25) + axl.seed(0) + mp = axl.MoranProcess((p1, p2)) populations = mp.play() self.assertEqual(len(mp), 2) self.assertEqual(len(populations), 2) @@ -218,9 +217,9 @@ def test_two_random_players(self): self.assertEqual(mp.winning_strategy_name, str(p2)) def test_two_players_with_mutation(self): - p1, p2 = axelrod.Cooperator(), axelrod.Defector() - axelrod.seed(5) - mp = MoranProcess((p1, p2), mutation_rate=0.2, stop_on_fixation=False) + p1, p2 = axl.Cooperator(), axl.Defector() + axl.seed(5) + mp = axl.MoranProcess((p1, p2), mutation_rate=0.2, stop_on_fixation=False) self.assertDictEqual(mp.mutation_targets, {str(p1): [p2], str(p2): [p1]}) # Test that mutation causes the population to alternate between # fixations @@ -238,27 +237,27 @@ def test_two_players_with_mutation(self): self.assertEqual(mp.population_distribution(), counter) def test_play_exception(self): - p1, p2 = axelrod.Cooperator(), axelrod.Defector() - mp = MoranProcess((p1, p2), mutation_rate=0.2) + p1, p2 = axl.Cooperator(), axl.Defector() + mp = axl.MoranProcess((p1, p2), mutation_rate=0.2) with self.assertRaises(ValueError): mp.play() def test_three_players(self): - players = [axelrod.Cooperator(), axelrod.Cooperator(), axelrod.Defector()] - axelrod.seed(11) - mp = MoranProcess(players) + players = [axl.Cooperator(), axl.Cooperator(), axl.Defector()] + axl.seed(11) + mp = axl.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())) + self.assertEqual(mp.winning_strategy_name, str(axl.Defector())) def test_three_players_with_mutation(self): - p1 = axelrod.Cooperator() - p2 = axelrod.Random() - p3 = axelrod.Defector() + p1 = axl.Cooperator() + p2 = axl.Random() + p3 = axl.Defector() players = [p1, p2, p3] - mp = MoranProcess(players, mutation_rate=0.2, stop_on_fixation=False) + mp = axl.MoranProcess(players, mutation_rate=0.2, stop_on_fixation=False) self.assertDictEqual( mp.mutation_targets, {str(p1): [p3, p2], str(p2): [p1, p3], str(p3): [p1, p2]}, @@ -274,34 +273,34 @@ def test_three_players_with_mutation(self): self.assertEqual(mp.population_distribution(), counter) def test_four_players(self): - players = [axelrod.Cooperator() for _ in range(3)] - players.append(axelrod.Defector()) - axelrod.seed(29) - mp = MoranProcess(players) + players = [axl.Cooperator() for _ in range(3)] + players.append(axl.Defector()) + axl.seed(29) + mp = axl.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())) + self.assertEqual(mp.winning_strategy_name, str(axl.Defector())) @given(strategies=strategy_lists(min_size=2, max_size=4)) @settings(max_examples=5) # Two specific examples relating to cloning of strategies - @example(strategies=[axelrod.BackStabber, axelrod.MindReader]) - @example(strategies=[axelrod.ThueMorse, axelrod.MindReader]) + @example(strategies=[axl.BackStabber, axl.MindReader]) + @example(strategies=[axl.ThueMorse, axl.MindReader]) def test_property_players(self, strategies): """Hypothesis test that randomly checks players""" players = [s() for s in strategies] - mp = MoranProcess(players) + mp = axl.MoranProcess(players) populations = mp.play() self.assertEqual(populations, mp.populations) self.assertIn(mp.winning_strategy_name, [str(p) for p in players]) def test_reset(self): - p1, p2 = axelrod.Cooperator(), axelrod.Defector() - axelrod.seed(45) - mp = MoranProcess((p1, p2)) + p1, p2 = axl.Cooperator(), axl.Defector() + axl.seed(45) + mp = axl.MoranProcess((p1, p2)) mp.play() self.assertEqual(len(mp), 4) self.assertEqual(len(mp.score_history), 3) @@ -315,14 +314,14 @@ def test_reset(self): def test_constant_fitness_case(self): # Scores between an Alternator and Defector will be: (1, 6) - axelrod.seed(0) + axl.seed(0) players = ( - axelrod.Alternator(), - axelrod.Alternator(), - axelrod.Defector(), - axelrod.Defector(), + axl.Alternator(), + axl.Alternator(), + axl.Defector(), + axl.Defector(), ) - mp = MoranProcess(players, turns=2) + mp = axl.MoranProcess(players, turns=2) winners = [] for _ in range(100): mp.play() @@ -332,26 +331,26 @@ def test_constant_fitness_case(self): self.assertEqual(winners["Defector"], 88) def test_cache(self): - p1, p2 = axelrod.Cooperator(), axelrod.Defector() - mp = MoranProcess((p1, p2)) + p1, p2 = axl.Cooperator(), axl.Defector() + mp = axl.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) + cache = axl.DeterministicCache() + mp = axl.MoranProcess((p1, p2), deterministic_cache=cache) self.assertEqual(cache, mp.deterministic_cache) def test_iter(self): - p1, p2 = axelrod.Cooperator(), axelrod.Defector() - mp = MoranProcess((p1, p2)) + p1, p2 = axl.Cooperator(), axl.Defector() + mp = axl.MoranProcess((p1, p2)) self.assertEqual(mp.__iter__(), mp) def test_population_plot(self): # Test that can plot on a given matplotlib axes - axelrod.seed(15) - players = [random.choice(axelrod.demo_strategies)() for _ in range(5)] - mp = axelrod.MoranProcess(players=players, turns=30) + axl.seed(15) + players = [random.choice(axl.demo_strategies)() for _ in range(5)] + mp = axl.MoranProcess(players=players, turns=30) mp.play() fig, axarr = plt.subplots(2, 2) ax = axarr[1, 0] @@ -364,26 +363,26 @@ def test_population_plot(self): self.assertEqual(ax.get_ylim(), (0, 5.25)) def test_cooperator_can_win_with_fitness_transformation(self): - axelrod.seed(689) + axl.seed(689) players = ( - axelrod.Cooperator(), - axelrod.Defector(), - axelrod.Defector(), - axelrod.Defector(), + axl.Cooperator(), + axl.Defector(), + axl.Defector(), + axl.Defector(), ) w = 0.95 fitness_transformation = lambda score: 1 - w + w * score - mp = MoranProcess( + mp = axl.MoranProcess( players, turns=10, fitness_transformation=fitness_transformation ) populations = mp.play() self.assertEqual(mp.winning_strategy_name, "Cooperator") def test_atomic_mutation_fsm(self): - axelrod.seed(12) - players = [axelrod.EvolvableFSMPlayer(num_states=2, initial_state=1, initial_action=C) + axl.seed(12) + players = [axl.EvolvableFSMPlayer(num_states=2, initial_state=1, initial_action=C) for _ in range(5)] - mp = MoranProcess(players, turns=10, mutation_method="atomic") + mp = axl.MoranProcess(players, turns=10, mutation_method="atomic") population = mp.play() self.assertEqual( mp.winning_strategy_name, @@ -392,29 +391,29 @@ def test_atomic_mutation_fsm(self): self.assertTrue(mp.fixated) def test_atomic_mutation_cycler(self): - axelrod.seed(10) + axl.seed(10) cycle_length = 5 - players = [axelrod.EvolvableCycler(cycle_length=cycle_length) + players = [axl.EvolvableCycler(cycle_length=cycle_length) for _ in range(5)] - mp = MoranProcess(players, turns=10, mutation_method="atomic") + mp = axl.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) def test_mutation_method_exceptions(self): - axelrod.seed(10) + axl.seed(10) cycle_length = 5 - players = [axelrod.EvolvableCycler(cycle_length=cycle_length) + players = [axl.EvolvableCycler(cycle_length=cycle_length) for _ in range(5)] with self.assertRaises(ValueError): - MoranProcess(players, turns=10, mutation_method="random") + axl.MoranProcess(players, turns=10, mutation_method="random") - axelrod.seed(0) - players = [axelrod.Cycler(cycle="CD" * random.randint(2, 10)) + axl.seed(0) + players = [axl.Cycler(cycle="CD" * random.randint(2, 10)) for _ in range(10)] - mp = MoranProcess(players, turns=10, mutation_method="atomic") + mp = axl.MoranProcess(players, turns=10, mutation_method="atomic") with self.assertRaises(TypeError): for _ in range(10): next(mp) @@ -427,17 +426,17 @@ def test_complete(self): seeds = range(0, 5) players = [] N = 6 - graph = axelrod.graph.complete_graph(N) + graph = axl.graph.complete_graph(N) for _ in range(N // 2): - players.append(axelrod.Cooperator()) - players.append(axelrod.Defector()) + players.append(axl.Cooperator()) + players.append(axl.Defector()) for seed in seeds: - axelrod.seed(seed) - mp = MoranProcess(players) + axl.seed(seed) + mp = axl.MoranProcess(players) mp.play() winner = mp.winning_strategy_name - axelrod.seed(seed) - mp = MoranProcess(players, interaction_graph=graph) + axl.seed(seed) + mp = axl.MoranProcess(players, interaction_graph=graph) mp.play() winner2 = mp.winning_strategy_name self.assertEqual(winner, winner2) @@ -448,18 +447,18 @@ def test_cycle(self): seeds = [(1, True), (8, False)] players = [] N = 6 - graph = axelrod.graph.cycle(N) + graph = axl.graph.cycle(N) for _ in range(N // 2): - players.append(axelrod.Cooperator()) + players.append(axl.Cooperator()) for _ in range(N // 2): - players.append(axelrod.Defector()) + players.append(axl.Defector()) for seed, outcome in seeds: - axelrod.seed(seed) - mp = MoranProcess(players) + axl.seed(seed) + mp = axl.MoranProcess(players) mp.play() winner = mp.winning_strategy_name - axelrod.seed(seed) - mp = MoranProcess(players, interaction_graph=graph) + axl.seed(seed) + mp = axl.MoranProcess(players, interaction_graph=graph) mp.play() winner2 = mp.winning_strategy_name self.assertEqual((winner == winner2), outcome) @@ -470,21 +469,21 @@ def test_asymmetry(self): seeds = [(1, True), (21, False)] players = [] N = 6 - graph1 = axelrod.graph.cycle(N) - graph2 = axelrod.graph.complete_graph(N) + graph1 = axl.graph.cycle(N) + graph2 = axl.graph.complete_graph(N) for _ in range(N // 2): - players.append(axelrod.Cooperator()) + players.append(axl.Cooperator()) for _ in range(N // 2): - players.append(axelrod.Defector()) + players.append(axl.Defector()) for seed, outcome in seeds: - axelrod.seed(seed) - mp = MoranProcess( + axl.seed(seed) + mp = axl.MoranProcess( players, interaction_graph=graph1, reproduction_graph=graph2 ) mp.play() winner = mp.winning_strategy_name - axelrod.seed(seed) - mp = MoranProcess( + axl.seed(seed) + mp = axl.MoranProcess( players, interaction_graph=graph2, reproduction_graph=graph1 ) mp.play() @@ -497,18 +496,18 @@ def test_cycle_death_birth(self): seeds = [(1, True), (5, False)] players = [] N = 6 - graph = axelrod.graph.cycle(N) + graph = axl.graph.cycle(N) for _ in range(N // 2): - players.append(axelrod.Cooperator()) + players.append(axl.Cooperator()) for _ in range(N // 2): - players.append(axelrod.Defector()) + players.append(axl.Defector()) for seed, outcome in seeds: - axelrod.seed(seed) - mp = MoranProcess(players, interaction_graph=graph, mode="bd") + axl.seed(seed) + mp = axl.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") + axl.seed(seed) + mp = axl.MoranProcess(players, interaction_graph=graph, mode="db") mp.play() winner2 = mp.winning_strategy_name self.assertEqual((winner == winner2), outcome) @@ -517,22 +516,22 @@ def test_cycle_death_birth(self): class TestApproximateMoranProcess(unittest.TestCase): """A suite of tests for the ApproximateMoranProcess""" - players = [axelrod.Cooperator(), axelrod.Defector()] + players = [axl.Cooperator(), axl.Defector()] cached_outcomes = {} counter = Counter([(0, 5)]) - pdf = Pdf(counter) + pdf = axl.Pdf(counter) cached_outcomes[("Cooperator", "Defector")] = pdf counter = Counter([(3, 3)]) - pdf = Pdf(counter) + pdf = axl.Pdf(counter) cached_outcomes[("Cooperator", "Cooperator")] = pdf counter = Counter([(1, 1)]) - pdf = Pdf(counter) + pdf = axl.Pdf(counter) cached_outcomes[("Defector", "Defector")] = pdf - amp = ApproximateMoranProcess(players, cached_outcomes) + amp = axl.ApproximateMoranProcess(players, cached_outcomes) def test_init(self): """Test the initialisation process""" diff --git a/axelrod/tests/unit/test_pickling.py b/axelrod/tests/unit/test_pickling.py index 09945dfc0..1cb14101d 100644 --- a/axelrod/tests/unit/test_pickling.py +++ b/axelrod/tests/unit/test_pickling.py @@ -1,9 +1,8 @@ +import unittest import pickle import random -import unittest import axelrod as axl -import axelrod.strategy_transformers as st C, D = axl.Action.C, axl.Action.D @@ -12,7 +11,7 @@ # First set: special cases -PointerToWrappedStrategy = st.FlipTransformer()(st.FlipTransformer()(axl.Cooperator)) +PointerToWrappedStrategy = axl.strategy_transformers.FlipTransformer()(axl.strategy_transformers.FlipTransformer()(axl.Cooperator)) class MyDefector(axl.Player): @@ -23,40 +22,40 @@ def strategy(self, opponent): return D -PointerToWrappedClassNotInStrategies = st.FlipTransformer()( - st.FlipTransformer()(MyDefector) +PointerToWrappedClassNotInStrategies = axl.strategy_transformers.FlipTransformer()( + axl.strategy_transformers.FlipTransformer()(MyDefector) ) -@st.InitialTransformer((D, C, D), name_prefix=None) -@st.DualTransformer(name_prefix=None) -@st.FlipTransformer(name_prefix=None) -@st.DualTransformer(name_prefix=None) +@axl.strategy_transformers.InitialTransformer((D, C, D), name_prefix=None) +@axl.strategy_transformers.DualTransformer(name_prefix=None) +@axl.strategy_transformers.FlipTransformer(name_prefix=None) +@axl.strategy_transformers.DualTransformer(name_prefix=None) class InterspersedDualTransformersNamePrefixAbsent(axl.Cooperator): pass -@st.IdentityTransformer((D, D, C)) -@st.DualTransformer() -@st.FlipTransformer() -@st.DualTransformer() +@axl.strategy_transformers.IdentityTransformer((D, D, C)) +@axl.strategy_transformers.DualTransformer() +@axl.strategy_transformers.FlipTransformer() +@axl.strategy_transformers.DualTransformer() class InterspersedDualTransformersNamePrefixPresent(axl.Cooperator): pass -@st.FlipTransformer() +@axl.strategy_transformers.FlipTransformer() class MyCooperator(axl.Player): def strategy(self, opponent): return C -@st.FlipTransformer() -@st.FlipTransformer() +@axl.strategy_transformers.FlipTransformer() +@axl.strategy_transformers.FlipTransformer() class DoubleFlip(axl.Cooperator): pass -@st.FlipTransformer() +@axl.strategy_transformers.FlipTransformer() class SingleFlip(axl.Cooperator): pass @@ -64,47 +63,47 @@ class SingleFlip(axl.Cooperator): # Second set: All the transformers -@st.ApologyTransformer([D], [C], name_prefix=None) +@axl.strategy_transformers.ApologyTransformer([D], [C], name_prefix=None) class Apology(axl.Cooperator): pass -@st.DeadlockBreakingTransformer(name_prefix=None) +@axl.strategy_transformers.DeadlockBreakingTransformer(name_prefix=None) class DeadlockBreaking(axl.Cooperator): pass -@st.DualTransformer(name_prefix=None) +@axl.strategy_transformers.DualTransformer(name_prefix=None) class Dual(axl.Cooperator): pass -@st.FlipTransformer(name_prefix=None) +@axl.strategy_transformers.FlipTransformer(name_prefix=None) class Flip(axl.Cooperator): pass -@st.FinalTransformer((D, D), name_prefix=None) +@axl.strategy_transformers.FinalTransformer((D, D), name_prefix=None) class Final(axl.Cooperator): pass -@st.ForgiverTransformer(0.2, name_prefix=None) +@axl.strategy_transformers.ForgiverTransformer(0.2, name_prefix=None) class Forgiver(axl.Cooperator): pass -@st.GrudgeTransformer(3, name_prefix=None) +@axl.strategy_transformers.GrudgeTransformer(3, name_prefix=None) class Grudge(axl.Cooperator): pass -@st.InitialTransformer((C, D), name_prefix=None) +@axl.strategy_transformers.InitialTransformer((C, D), name_prefix=None) class Initial(axl.Cooperator): pass -@st.JossAnnTransformer((0.2, 0.2), name_prefix=None) +@axl.strategy_transformers.JossAnnTransformer((0.2, 0.2), name_prefix=None) class JossAnn(axl.Cooperator): pass @@ -113,42 +112,42 @@ class JossAnn(axl.Cooperator): probability = [0.2, 0.3] -@st.MixedTransformer(probability, strategies, name_prefix=None) +@axl.strategy_transformers.MixedTransformer(probability, strategies, name_prefix=None) class Mixed(axl.Cooperator): pass -@st.NiceTransformer(name_prefix=None) +@axl.strategy_transformers.NiceTransformer(name_prefix=None) class Nice(axl.Cooperator): pass -@st.NoisyTransformer(0.2, name_prefix=None) +@axl.strategy_transformers.NoisyTransformer(0.2, name_prefix=None) class Noisy(axl.Cooperator): pass -@st.RetaliationTransformer(3, name_prefix=None) +@axl.strategy_transformers.RetaliationTransformer(3, name_prefix=None) class Retaliation(axl.Cooperator): pass -@st.RetaliateUntilApologyTransformer(name_prefix=None) +@axl.strategy_transformers.RetaliateUntilApologyTransformer(name_prefix=None) class RetaliateUntilApology(axl.Cooperator): pass -@st.TrackHistoryTransformer(name_prefix=None) +@axl.strategy_transformers.TrackHistoryTransformer(name_prefix=None) class TrackHistory(axl.Cooperator): pass -@st.IdentityTransformer() +@axl.strategy_transformers.IdentityTransformer() class Identity(axl.Cooperator): pass -@st.IdentityTransformer(name_prefix=None) +@axl.strategy_transformers.IdentityTransformer(name_prefix=None) class TransformedThue(axl.ThueMorse): pass @@ -161,7 +160,7 @@ def __init__(self): super().__init__(team=team) -TransformedMetaThue = st.IdentityTransformer(name_prefix=None)(MetaThue) +TransformedMetaThue = axl.strategy_transformers.IdentityTransformer(name_prefix=None)(MetaThue) transformed_no_prefix = [ @@ -184,22 +183,22 @@ def __init__(self): ] transformer_instances = [ - st.ApologyTransformer([D], [C]), - st.DeadlockBreakingTransformer(), - st.DualTransformer(), - st.FlipTransformer(), - st.FinalTransformer((D, D)), - st.ForgiverTransformer(0.2), - st.GrudgeTransformer(3), - st.InitialTransformer((C, D)), - st.JossAnnTransformer((0.2, 0.6)), - st.MixedTransformer(probability, strategies), - st.NiceTransformer(), - st.NoisyTransformer(0.2), - st.RetaliationTransformer(3), - st.RetaliateUntilApologyTransformer(), - st.TrackHistoryTransformer(), - st.IdentityTransformer(), + axl.strategy_transformers.ApologyTransformer([D], [C]), + axl.strategy_transformers.DeadlockBreakingTransformer(), + axl.strategy_transformers.DualTransformer(), + axl.strategy_transformers.FlipTransformer(), + axl.strategy_transformers.FinalTransformer((D, D)), + axl.strategy_transformers.ForgiverTransformer(0.2), + axl.strategy_transformers.GrudgeTransformer(3), + axl.strategy_transformers.InitialTransformer((C, D)), + axl.strategy_transformers.JossAnnTransformer((0.2, 0.6)), + axl.strategy_transformers.MixedTransformer(probability, strategies), + axl.strategy_transformers.NiceTransformer(), + axl.strategy_transformers.NoisyTransformer(0.2), + axl.strategy_transformers.RetaliationTransformer(3), + axl.strategy_transformers.RetaliateUntilApologyTransformer(), + axl.strategy_transformers.TrackHistoryTransformer(), + axl.strategy_transformers.IdentityTransformer(), ] @@ -237,7 +236,7 @@ def test_parameterized_player(self): self.assert_original_equals_pickled(player) def test_sequence_player(self): - inline_transformed_thue = st.IdentityTransformer(name_prefix="Transformed")(axl.ThueMorse)() + inline_transformed_thue = axl.strategy_transformers.IdentityTransformer(name_prefix="Transformed")(axl.ThueMorse)() for player in [axl.ThueMorse(), axl.ThueMorseInverse(), MetaThue(), TransformedMetaThue(), inline_transformed_thue, TransformedThue(), ]: @@ -276,9 +275,9 @@ def test_pickling_all_transformers_as_instance_called_on_a_class(self): self.assert_original_equals_pickled(player) def test_created_on_the_spot_multiple_transformers(self): - player_class = st.FlipTransformer()(axl.Cooperator) - player_class = st.DualTransformer()(player_class) - player = st.FinalTransformer((C, D))(player_class)() + player_class = axl.strategy_transformers.FlipTransformer()(axl.Cooperator) + player_class = axl.strategy_transformers.DualTransformer()(player_class) + player = axl.strategy_transformers.FinalTransformer((C, D))(player_class)() self.assert_original_equals_pickled(player) @@ -294,10 +293,10 @@ def test_dual_transformer_regression_test(self): self.assert_original_equals_pickled(player) player_class = axl.WinStayLoseShift - player_class = st.DualTransformer()(player_class) - player_class = st.InitialTransformer((C, D))(player_class) - player_class = st.DualTransformer()(player_class) - player_class = st.TrackHistoryTransformer()(player_class) + player_class = axl.strategy_transformers.DualTransformer()(player_class) + player_class = axl.strategy_transformers.InitialTransformer((C, D))(player_class) + player_class = axl.strategy_transformers.DualTransformer()(player_class) + player_class = axl.strategy_transformers.TrackHistoryTransformer()(player_class) interspersed_dual_transformers = player_class() @@ -370,7 +369,7 @@ class LocalCooperator(axl.Cooperator): self.assertRaises(AttributeError, pickle.dumps, un_transformed) - player = st.FlipTransformer()(LocalCooperator)() + player = axl.strategy_transformers.FlipTransformer()(LocalCooperator)() pickled = pickle.dumps(player) self.assertRaises(AttributeError, pickle.loads, pickled) @@ -379,17 +378,17 @@ def test_with_various_name_prefixes(self): self.assertEqual(no_prefix.__class__.__name__, "Flip") self.assert_original_equals_pickled(no_prefix) - default_prefix = st.FlipTransformer()(axl.Cooperator)() + default_prefix = axl.strategy_transformers.FlipTransformer()(axl.Cooperator)() self.assertEqual(default_prefix.__class__.__name__, "FlippedCooperator") self.assert_original_equals_pickled(default_prefix) - fliptastic = st.FlipTransformer(name_prefix="Fliptastic") + fliptastic = axl.strategy_transformers.FlipTransformer(name_prefix="Fliptastic") new_prefix = fliptastic(axl.Cooperator)() self.assertEqual(new_prefix.__class__.__name__, "FliptasticCooperator") self.assert_original_equals_pickled(new_prefix) def test_dynamic_class_no_name_prefix(self): - player = st.FlipTransformer(name_prefix=None)(axl.Cooperator)() + player = axl.strategy_transformers.FlipTransformer(name_prefix=None)(axl.Cooperator)() self.assertEqual(player.__class__.__name__, "Cooperator") self.assert_original_equals_pickled(player) diff --git a/axelrod/tests/unit/test_plot.py b/axelrod/tests/unit/test_plot.py index ddad0fef7..a8dcb6261 100644 --- a/axelrod/tests/unit/test_plot.py +++ b/axelrod/tests/unit/test_plot.py @@ -1,26 +1,29 @@ -import tempfile import unittest -import axelrod +import tempfile + import matplotlib import matplotlib.pyplot as plt + from numpy import mean +import axelrod as axl + class TestPlot(unittest.TestCase): @classmethod def setUpClass(cls): cls.filename = "test_outputs/test_results.csv" - cls.players = [axelrod.Alternator(), axelrod.TitForTat(), axelrod.Defector()] + cls.players = [axl.Alternator(), axl.TitForTat(), axl.Defector()] cls.repetitions = 3 cls.turns = 5 - cls.test_result_set = axelrod.ResultSet( + cls.test_result_set = axl.ResultSet( cls.filename, cls.players, cls.repetitions, progress_bar=False ) - cls.test_result_set = axelrod.ResultSet( + cls.test_result_set = axl.ResultSet( cls.filename, cls.players, cls.repetitions, progress_bar=False ) cls.expected_boxplot_dataset = [ @@ -57,66 +60,66 @@ def setUpClass(cls): ) def test_default_cmap(self): - cmap = axelrod.plot.default_cmap("0.0") + cmap = axl.plot.default_cmap("0.0") self.assertEqual(cmap, "YlGnBu") - cmap = axelrod.plot.default_cmap("1.3alpha") + cmap = axl.plot.default_cmap("1.3alpha") self.assertEqual(cmap, "YlGnBu") - cmap = axelrod.plot.default_cmap("1.4.99") + cmap = axl.plot.default_cmap("1.4.99") self.assertEqual(cmap, "YlGnBu") - cmap = axelrod.plot.default_cmap("1.4") + cmap = axl.plot.default_cmap("1.4") self.assertEqual(cmap, "YlGnBu") - cmap = axelrod.plot.default_cmap() + cmap = axl.plot.default_cmap() self.assertEqual(cmap, "viridis") - cmap = axelrod.plot.default_cmap("1.5") + cmap = axl.plot.default_cmap("1.5") self.assertEqual(cmap, "viridis") - cmap = axelrod.plot.default_cmap("1.5beta") + cmap = axl.plot.default_cmap("1.5beta") self.assertEqual(cmap, "viridis") - cmap = axelrod.plot.default_cmap("1.7") + cmap = axl.plot.default_cmap("1.7") self.assertEqual(cmap, "viridis") - cmap = axelrod.plot.default_cmap("2.0") + cmap = axl.plot.default_cmap("2.0") self.assertEqual(cmap, "viridis") def test_init(self): - plot = axelrod.Plot(self.test_result_set) + plot = axl.Plot(self.test_result_set) self.assertEqual(plot.result_set, self.test_result_set) def test_init_from_resulsetfromfile(self): tmp_file = tempfile.NamedTemporaryFile(mode="w", delete=False) - players = [axelrod.Cooperator(), axelrod.TitForTat(), axelrod.Defector()] - tournament = axelrod.Tournament(players=players, turns=2, repetitions=2) + players = [axl.Cooperator(), axl.TitForTat(), axl.Defector()] + tournament = axl.Tournament(players=players, turns=2, repetitions=2) tournament.play(filename=tmp_file.name, progress_bar=False) tmp_file.close() - rs = axelrod.ResultSet(tmp_file.name, players, 2, progress_bar=False) + rs = axl.ResultSet(tmp_file.name, players, 2, progress_bar=False) - plot = axelrod.Plot(rs) + plot = axl.Plot(rs) self.assertEqual(plot.result_set, rs) def test_boxplot_dataset(self): - plot = axelrod.Plot(self.test_result_set) + plot = axl.Plot(self.test_result_set) self.assertSequenceEqual(plot._boxplot_dataset, self.expected_boxplot_dataset) def test_boxplot_xticks_locations(self): - plot = axelrod.Plot(self.test_result_set) + plot = axl.Plot(self.test_result_set) self.assertEqual( plot._boxplot_xticks_locations, self.expected_boxplot_xticks_locations ) def test_boxplot_xticks_labels(self): - plot = axelrod.Plot(self.test_result_set) + plot = axl.Plot(self.test_result_set) self.assertEqual( plot._boxplot_xticks_labels, self.expected_boxplot_xticks_labels ) def test_boxplot(self): - plot = axelrod.Plot(self.test_result_set) + plot = axl.Plot(self.test_result_set) fig = plot.boxplot() self.assertIsInstance(fig, matplotlib.pyplot.Figure) plt.close(fig) @@ -125,7 +128,7 @@ def test_boxplot_with_passed_axes(self): # Test that can plot on a given matplotlib axes fig, axarr = plt.subplots(2, 2) self.assertEqual(axarr[0, 1].get_ylim(), (0, 1)) - plot = axelrod.Plot(self.test_result_set) + plot = axl.Plot(self.test_result_set) plot.boxplot(ax=axarr[0, 1]) self.assertNotEqual(axarr[0, 1].get_ylim(), (0, 1)) @@ -135,65 +138,65 @@ def test_boxplot_with_passed_axes(self): self.assertEqual(axarr[1, 0].get_title(), "dummy title") def test_boxplot_with_title(self): - plot = axelrod.Plot(self.test_result_set) + plot = axl.Plot(self.test_result_set) fig = plot.boxplot(title="title") self.assertIsInstance(fig, matplotlib.pyplot.Figure) plt.close(fig) def test_winplot_dataset(self): - plot = axelrod.Plot(self.test_result_set) + plot = axl.Plot(self.test_result_set) self.assertSequenceEqual(plot._winplot_dataset, self.expected_winplot_dataset) def test_winplot(self): - plot = axelrod.Plot(self.test_result_set) + plot = axl.Plot(self.test_result_set) fig = plot.winplot() self.assertIsInstance(fig, matplotlib.pyplot.Figure) plt.close(fig) def test_sdvplot_dataset(self): - plot = axelrod.Plot(self.test_result_set) + plot = axl.Plot(self.test_result_set) self.assertSequenceEqual(plot._sdv_plot_dataset, self.expected_sdvplot_dataset) def test_sdvplot(self): - plot = axelrod.Plot(self.test_result_set) + plot = axl.Plot(self.test_result_set) fig = plot.sdvplot() self.assertIsInstance(fig, matplotlib.pyplot.Figure) plt.close(fig) def test_lengthplot_dataset(self): - plot = axelrod.Plot(self.test_result_set) + plot = axl.Plot(self.test_result_set) self.assertSequenceEqual(plot._winplot_dataset, self.expected_winplot_dataset) def test_lengthplot(self): - plot = axelrod.Plot(self.test_result_set) + plot = axl.Plot(self.test_result_set) fig = plot.lengthplot() self.assertIsInstance(fig, matplotlib.pyplot.Figure) plt.close(fig) def test_pdplot(self): - plot = axelrod.Plot(self.test_result_set) + plot = axl.Plot(self.test_result_set) fig = plot.pdplot() self.assertIsInstance(fig, matplotlib.pyplot.Figure) plt.close(fig) def test_payoff_dataset(self): - plot = axelrod.Plot(self.test_result_set) + plot = axl.Plot(self.test_result_set) self.assertSequenceEqual(plot._payoff_dataset, self.expected_payoff_dataset) def test_payoff(self): - plot = axelrod.Plot(self.test_result_set) + plot = axl.Plot(self.test_result_set) fig = plot.payoff() self.assertIsInstance(fig, matplotlib.pyplot.Figure) plt.close(fig) def test_payoff_with_title(self): - plot = axelrod.Plot(self.test_result_set) + plot = axl.Plot(self.test_result_set) fig = plot.payoff(title="dummy title") self.assertIsInstance(fig, matplotlib.pyplot.Figure) plt.close(fig) def test_payoff_with_passed_axes(self): - plot = axelrod.Plot(self.test_result_set) + plot = axl.Plot(self.test_result_set) fig, axarr = plt.subplots(2, 2) self.assertEqual(axarr[0, 1].get_xlim(), (0, 1)) @@ -207,10 +210,10 @@ def test_payoff_with_passed_axes(self): plt.close(fig) def test_stackplot(self): - eco = axelrod.Ecosystem(self.test_result_set) + eco = axl.Ecosystem(self.test_result_set) eco.reproduce(100) - plot = axelrod.Plot(self.test_result_set) + plot = axl.Plot(self.test_result_set) fig = plot.stackplot(eco) self.assertIsInstance(fig, matplotlib.pyplot.Figure) plt.close(fig) @@ -223,9 +226,9 @@ def test_stackplot(self): def test_stackplot_with_passed_axes(self): # Test that can plot on a given matplotlib axes - eco = axelrod.Ecosystem(self.test_result_set) + eco = axl.Ecosystem(self.test_result_set) eco.reproduce(100) - plot = axelrod.Plot(self.test_result_set) + plot = axl.Plot(self.test_result_set) fig, axarr = plt.subplots(2, 2) self.assertEqual(axarr[0, 1].get_xlim(), (0, 1)) @@ -240,7 +243,7 @@ def test_stackplot_with_passed_axes(self): plt.close(fig) def test_all_plots(self): - plot = axelrod.Plot(self.test_result_set) + plot = axl.Plot(self.test_result_set) # Test that this method does not crash. self.assertIsNone( plot.save_all_plots(prefix="test_outputs/", progress_bar=False) diff --git a/axelrod/tests/unit/test_property.py b/axelrod/tests/unit/test_property.py index 97962bd9c..8449e7cce 100644 --- a/axelrod/tests/unit/test_property.py +++ b/axelrod/tests/unit/test_property.py @@ -1,6 +1,6 @@ import unittest -import axelrod +import axelrod as axl from axelrod.tests.property import ( games, matches, @@ -13,7 +13,7 @@ from hypothesis import given, settings -stochastic_strategies = [s for s in axelrod.strategies if s().classifier["stochastic"]] +stochastic_strategies = [s for s in axl.strategies if s().classifier["stochastic"]] class TestStrategyList(unittest.TestCase): @@ -21,7 +21,7 @@ def test_call(self): strategies = strategy_lists().example() self.assertIsInstance(strategies, list) for p in strategies: - self.assertIsInstance(p(), axelrod.Player) + self.assertIsInstance(p(), axl.Player) @given(strategies=strategy_lists(min_size=1, max_size=50)) @settings(max_examples=5) @@ -30,16 +30,16 @@ def test_decorator(self, strategies): self.assertGreaterEqual(len(strategies), 1) self.assertLessEqual(len(strategies), 50) for strategy in strategies: - self.assertIsInstance(strategy(), axelrod.Player) + self.assertIsInstance(strategy(), axl.Player) - @given(strategies=strategy_lists(strategies=axelrod.basic_strategies)) + @given(strategies=strategy_lists(strategies=axl.basic_strategies)) @settings(max_examples=5) def test_decorator_with_given_strategies(self, strategies): self.assertIsInstance(strategies, list) - basic_player_names = [str(s()) for s in axelrod.basic_strategies] + basic_player_names = [str(s()) for s in axl.basic_strategies] for strategy in strategies: player = strategy() - self.assertIsInstance(player, axelrod.Player) + self.assertIsInstance(player, axl.Player) self.assertIn(str(player), basic_player_names) @@ -50,13 +50,13 @@ class TestMatch(unittest.TestCase): def test_call(self): match = matches().example() - self.assertIsInstance(match, axelrod.Match) + self.assertIsInstance(match, axl.Match) @given(match=matches(min_turns=10, max_turns=50, min_noise=0, max_noise=1)) @settings(max_examples=5) def test_decorator(self, match): - self.assertIsInstance(match, axelrod.Match) + self.assertIsInstance(match, axl.Match) self.assertGreaterEqual(len(match), 10) self.assertLessEqual(len(match), 50) self.assertGreaterEqual(match.noise, 0) @@ -66,7 +66,7 @@ def test_decorator(self, match): @settings(max_examples=5) def test_decorator_with_no_noise(self, match): - self.assertIsInstance(match, axelrod.Match) + self.assertIsInstance(match, axl.Match) self.assertGreaterEqual(len(match), 10) self.assertLessEqual(len(match), 50) self.assertEqual(match.noise, 0) @@ -75,7 +75,7 @@ def test_decorator_with_no_noise(self, match): class TestTournament(unittest.TestCase): def test_call(self): tournament = tournaments().example() - self.assertIsInstance(tournament, axelrod.Tournament) + self.assertIsInstance(tournament, axl.Tournament) @given( tournament=tournaments( @@ -90,7 +90,7 @@ def test_call(self): ) @settings(max_examples=5) def test_decorator(self, tournament): - self.assertIsInstance(tournament, axelrod.Tournament) + self.assertIsInstance(tournament, axl.Tournament) self.assertLessEqual(tournament.turns, 50) self.assertGreaterEqual(tournament.turns, 2) self.assertLessEqual(tournament.noise, 1) @@ -98,11 +98,11 @@ def test_decorator(self, tournament): self.assertLessEqual(tournament.repetitions, 50) self.assertGreaterEqual(tournament.repetitions, 2) - @given(tournament=tournaments(strategies=axelrod.basic_strategies, max_size=3)) + @given(tournament=tournaments(strategies=axl.basic_strategies, max_size=3)) @settings(max_examples=5) def test_decorator_with_given_strategies(self, tournament): - self.assertIsInstance(tournament, axelrod.Tournament) - basic_player_names = [str(s()) for s in axelrod.basic_strategies] + self.assertIsInstance(tournament, axl.Tournament) + basic_player_names = [str(s()) for s in axl.basic_strategies] for p in tournament.players: self.assertIn(str(p), basic_player_names) @@ -110,7 +110,7 @@ def test_decorator_with_given_strategies(self, tournament): class TestProbEndTournament(unittest.TestCase): def test_call(self): tournament = tournaments().example() - self.assertIsInstance(tournament, axelrod.Tournament) + self.assertIsInstance(tournament, axl.Tournament) @given( tournament=prob_end_tournaments( @@ -125,7 +125,7 @@ def test_call(self): ) @settings(max_examples=5) def test_decorator(self, tournament): - self.assertIsInstance(tournament, axelrod.Tournament) + self.assertIsInstance(tournament, axl.Tournament) self.assertLessEqual(tournament.prob_end, 1) self.assertGreaterEqual(tournament.prob_end, 0) self.assertLessEqual(tournament.noise, 1) @@ -134,12 +134,12 @@ def test_decorator(self, tournament): self.assertGreaterEqual(tournament.repetitions, 2) @given( - tournament=prob_end_tournaments(strategies=axelrod.basic_strategies, max_size=3) + tournament=prob_end_tournaments(strategies=axl.basic_strategies, max_size=3) ) @settings(max_examples=5) def test_decorator_with_given_strategies(self, tournament): - self.assertIsInstance(tournament, axelrod.Tournament) - basic_player_names = [str(s()) for s in axelrod.basic_strategies] + self.assertIsInstance(tournament, axl.Tournament) + basic_player_names = [str(s()) for s in axl.basic_strategies] for p in tournament.players: self.assertIn(str(p), basic_player_names) @@ -147,7 +147,7 @@ def test_decorator_with_given_strategies(self, tournament): class TestSpatialTournament(unittest.TestCase): def test_call(self): tournament = spatial_tournaments().example() - self.assertIsInstance(tournament, axelrod.Tournament) + self.assertIsInstance(tournament, axl.Tournament) @given( tournament=spatial_tournaments( @@ -162,7 +162,7 @@ def test_call(self): ) @settings(max_examples=5) def test_decorator(self, tournament): - self.assertIsInstance(tournament, axelrod.Tournament) + self.assertIsInstance(tournament, axl.Tournament) self.assertLessEqual(tournament.turns, 50) self.assertGreaterEqual(tournament.turns, 2) self.assertLessEqual(tournament.noise, 1) @@ -171,12 +171,12 @@ def test_decorator(self, tournament): self.assertGreaterEqual(tournament.repetitions, 2) @given( - tournament=spatial_tournaments(strategies=axelrod.basic_strategies, max_size=3) + tournament=spatial_tournaments(strategies=axl.basic_strategies, max_size=3) ) @settings(max_examples=5) def test_decorator_with_given_strategies(self, tournament): - self.assertIsInstance(tournament, axelrod.Tournament) - basic_player_names = [str(s()) for s in axelrod.basic_strategies] + self.assertIsInstance(tournament, axl.Tournament) + basic_player_names = [str(s()) for s in axl.basic_strategies] for p in tournament.players: self.assertIn(str(p), basic_player_names) @@ -184,7 +184,7 @@ def test_decorator_with_given_strategies(self, tournament): class TestProbEndSpatialTournament(unittest.TestCase): def test_call(self): tournament = prob_end_spatial_tournaments().example() - self.assertIsInstance(tournament, axelrod.Tournament) + self.assertIsInstance(tournament, axl.Tournament) @given( tournament=prob_end_spatial_tournaments( @@ -199,7 +199,7 @@ def test_call(self): ) @settings(max_examples=5) def test_decorator(self, tournament): - self.assertIsInstance(tournament, axelrod.Tournament) + self.assertIsInstance(tournament, axl.Tournament) self.assertLessEqual(tournament.prob_end, 1) self.assertGreaterEqual(tournament.prob_end, 0) self.assertLessEqual(tournament.noise, 1) @@ -209,13 +209,13 @@ def test_decorator(self, tournament): @given( tournament=prob_end_spatial_tournaments( - strategies=axelrod.basic_strategies, max_size=3 + strategies=axl.basic_strategies, max_size=3 ) ) @settings(max_examples=5) def test_decorator_with_given_strategies(self, tournament): - self.assertIsInstance(tournament, axelrod.Tournament) - basic_player_names = [str(s()) for s in axelrod.basic_strategies] + self.assertIsInstance(tournament, axl.Tournament) + basic_player_names = [str(s()) for s in axl.basic_strategies] for p in tournament.players: self.assertIn(str(p), basic_player_names) @@ -223,16 +223,16 @@ def test_decorator_with_given_strategies(self, tournament): class TestGame(unittest.TestCase): def test_call(self): game = games().example() - self.assertIsInstance(game, axelrod.Game) + self.assertIsInstance(game, axl.Game) @given(game=games()) @settings(max_examples=5) def test_decorator(self, game): - self.assertIsInstance(game, axelrod.Game) + self.assertIsInstance(game, axl.Game) r, p, s, t = game.RPST() self.assertTrue((2 * r) > (t + s) and (t > r > p > s)) @given(game=games(prisoners_dilemma=False)) @settings(max_examples=5) def test_decorator_unconstrained(self, game): - self.assertIsInstance(game, axelrod.Game) + self.assertIsInstance(game, axl.Game) diff --git a/axelrod/tests/unit/test_random_.py b/axelrod/tests/unit/test_random_.py index 32df00344..fdb1d361f 100644 --- a/axelrod/tests/unit/test_random_.py +++ b/axelrod/tests/unit/test_random_.py @@ -1,22 +1,26 @@ """Tests for the random functions.""" -import random + import unittest + +import random + from collections import Counter import numpy -from axelrod import Action, Pdf, random_choice, random_flip, seed -C, D = Action.C, Action.D +import axelrod as axl + +C, D = axl.Action.C, axl.Action.D class TestRandom_(unittest.TestCase): def test_return_values(self): - self.assertEqual(random_choice(1), C) - self.assertEqual(random_choice(0), D) - seed(1) - self.assertEqual(random_choice(), C) - seed(2) - self.assertEqual(random_choice(), D) + self.assertEqual(axl.random_choice(1), C) + self.assertEqual(axl.random_choice(0), D) + axl.seed(1) + self.assertEqual(axl.random_choice(), C) + axl.seed(2) + self.assertEqual(axl.random_choice(), D) def test_set_seed(self): """Test that numpy and stdlib random seed is set by axelrod seed""" @@ -24,7 +28,7 @@ def test_set_seed(self): numpy_random_numbers = [] stdlib_random_numbers = [] for _ in range(2): - seed(0) + axl.seed(0) numpy_random_numbers.append(numpy.random.random()) stdlib_random_numbers.append(random.random()) @@ -35,19 +39,19 @@ def test_seed_not_offset_by_deterministic_call(self): """Test that when called with p = 0 or 1, the random seed is not affected.""" for p in [0, 1]: - seed(0) + axl.seed(0) r = random.random() - seed(0) - random_choice(p) + axl.seed(0) + axl.random_choice(p) self.assertEqual(r, random.random()) def test_random_flip(self): - self.assertEqual(C, random_flip(C, 0)) - self.assertEqual(C, random_flip(D, 1)) - seed(0) - self.assertEqual(C, random_flip(C, 0.2)) - seed(1) - self.assertEqual(C, random_flip(D, 0.2)) + self.assertEqual(C, axl.random_flip(C, 0)) + self.assertEqual(C, axl.random_flip(D, 1)) + axl.seed(0) + self.assertEqual(C, axl.random_flip(C, 0.2)) + axl.seed(1) + self.assertEqual(C, axl.random_flip(D, 0.2)) class TestPdf(unittest.TestCase): @@ -55,7 +59,7 @@ class TestPdf(unittest.TestCase): observations = [(C, D)] * 4 + [(C, C)] * 12 + [(D, C)] * 2 + [(D, D)] * 15 counter = Counter(observations) - pdf = Pdf(counter) + pdf = axl.Pdf(counter) def test_init(self): self.assertEqual(set(self.pdf.sample_space), set(self.counter.keys())) @@ -67,7 +71,7 @@ def test_sample(self): """Test that sample maps to correct domain""" all_samples = [] - seed(0) + axl.seed(0) for sample in range(100): all_samples.append(self.pdf.sample()) @@ -78,7 +82,7 @@ def test_seed(self): """Test that numpy seeds the sample properly""" for s in range(10): - seed(s) + axl.seed(s) sample = self.pdf.sample() - seed(s) + axl.seed(s) self.assertEqual(sample, self.pdf.sample()) diff --git a/axelrod/tests/unit/test_resultset.py b/axelrod/tests/unit/test_resultset.py index 9b7c86cea..73cf96be8 100644 --- a/axelrod/tests/unit/test_resultset.py +++ b/axelrod/tests/unit/test_resultset.py @@ -1,18 +1,17 @@ -import csv import unittest +import csv from collections import Counter - -import axelrod -import axelrod.interaction_utils as iu import pandas as pd +from dask.dataframe.core import DataFrame +from numpy import mean, nanmedian, std + +import axelrod as axl from axelrod.result_set import create_counter_dict from axelrod.tests.property import prob_end_tournaments, tournaments -from numpy import mean, nan, nanmedian, std -from dask.dataframe.core import DataFrame from hypothesis import given, settings -C, D = axelrod.Action.C, axelrod.Action.D +C, D = axl.Action.C, axl.Action.D class TestResultSet(unittest.TestCase): @@ -21,7 +20,7 @@ def setUpClass(cls): cls.filename = "test_outputs/test_results.csv" - cls.players = [axelrod.Alternator(), axelrod.TitForTat(), axelrod.Defector()] + cls.players = [axl.Alternator(), axl.TitForTat(), axl.Defector()] cls.repetitions = 3 cls.turns = 5 cls.edges = [(0, 1), (0, 2), (1, 2)] @@ -184,7 +183,7 @@ def setUpClass(cls): ] def test_init(self): - rs = axelrod.ResultSet( + rs = axl.ResultSet( self.filename, self.players, self.repetitions, progress_bar=False ) self.assertEqual(rs.players, self.players) @@ -196,9 +195,9 @@ def _clear_matrix(self, matrix): matrix[i][j] = 0 def test_ne_vectors(self): - rs_1 = axelrod.ResultSet(self.filename, self.players, self.repetitions) + rs_1 = axl.ResultSet(self.filename, self.players, self.repetitions) - rs_2 = axelrod.ResultSet(self.filename, self.players, self.repetitions) + rs_2 = axl.ResultSet(self.filename, self.players, self.repetitions) # A different vector rs_2.eigenmoses_rating = (-1, -1, -1) @@ -206,13 +205,13 @@ def test_ne_vectors(self): self.assertNotEqual(rs_1, rs_2) def test_nan_vectors(self): - rs_1 = axelrod.ResultSet(self.filename, self.players, self.repetitions) + rs_1 = axl.ResultSet(self.filename, self.players, self.repetitions) # Force a broken eigenmoses, by replacing vengeful_cooperation with # zeroes. self._clear_matrix(rs_1.vengeful_cooperation) rs_1.eigenmoses_rating = rs_1._build_eigenmoses_rating() - rs_2 = axelrod.ResultSet(self.filename, self.players, self.repetitions) + rs_2 = axl.ResultSet(self.filename, self.players, self.repetitions) # Force a broken eigenmoses, by replacing vengeful_cooperation with # zeroes. self._clear_matrix(rs_2.vengeful_cooperation) @@ -221,7 +220,7 @@ def test_nan_vectors(self): self.assertEqual(rs_1, rs_2) def test_init_multiprocessing(self): - rs = axelrod.ResultSet( + rs = axl.ResultSet( self.filename, self.players, self.repetitions, @@ -231,7 +230,7 @@ def test_init_multiprocessing(self): self.assertEqual(rs.players, self.players) self.assertEqual(rs.num_players, len(self.players)) - rs = axelrod.ResultSet( + rs = axl.ResultSet( self.filename, self.players, self.repetitions, @@ -242,7 +241,7 @@ def test_init_multiprocessing(self): self.assertEqual(rs.num_players, len(self.players)) def test_with_progress_bar(self): - rs = axelrod.ResultSet( + rs = axl.ResultSet( self.filename, self.players, self.repetitions, progress_bar=True ) self.assertTrue(rs.progress_bar) @@ -250,7 +249,7 @@ def test_with_progress_bar(self): self.assertEqual(rs.progress_bar.n, rs.progress_bar.total) def test_match_lengths(self): - rs = axelrod.ResultSet( + rs = axl.ResultSet( self.filename, self.players, self.repetitions, progress_bar=False ) self.assertIsInstance(rs.match_lengths, list) @@ -272,7 +271,7 @@ def test_match_lengths(self): self.assertEqual(length, self.turns) def test_scores(self): - rs = axelrod.ResultSet( + rs = axl.ResultSet( self.filename, self.players, self.repetitions, progress_bar=False ) self.assertIsInstance(rs.scores, list) @@ -280,7 +279,7 @@ def test_scores(self): self.assertEqual(rs.scores, self.expected_scores) def test_ranking(self): - rs = axelrod.ResultSet( + rs = axl.ResultSet( self.filename, self.players, self.repetitions, progress_bar=False ) self.assertIsInstance(rs.ranking, list) @@ -288,7 +287,7 @@ def test_ranking(self): self.assertEqual(rs.ranking, self.expected_ranking) def test_ranked_names(self): - rs = axelrod.ResultSet( + rs = axl.ResultSet( self.filename, self.players, self.repetitions, progress_bar=False ) self.assertIsInstance(rs.ranked_names, list) @@ -296,7 +295,7 @@ def test_ranked_names(self): self.assertEqual(rs.ranked_names, self.expected_ranked_names) def test_wins(self): - rs = axelrod.ResultSet( + rs = axl.ResultSet( self.filename, self.players, self.repetitions, progress_bar=False ) self.assertIsInstance(rs.wins, list) @@ -304,7 +303,7 @@ def test_wins(self): self.assertEqual(rs.wins, self.expected_wins) def test_normalised_scores(self): - rs = axelrod.ResultSet( + rs = axl.ResultSet( self.filename, self.players, self.repetitions, progress_bar=False ) self.assertIsInstance(rs.normalised_scores, list) @@ -312,7 +311,7 @@ def test_normalised_scores(self): self.assertEqual(rs.normalised_scores, self.expected_normalised_scores) def test_payoffs(self): - rs = axelrod.ResultSet( + rs = axl.ResultSet( self.filename, self.players, self.repetitions, progress_bar=False ) self.assertIsInstance(rs.payoffs, list) @@ -320,7 +319,7 @@ def test_payoffs(self): self.assertEqual(rs.payoffs, self.expected_payoffs) def test_payoff_matrix(self): - rs = axelrod.ResultSet( + rs = axl.ResultSet( self.filename, self.players, self.repetitions, progress_bar=False ) self.assertIsInstance(rs.payoff_matrix, list) @@ -328,7 +327,7 @@ def test_payoff_matrix(self): self.assertEqual(rs.payoff_matrix, self.expected_payoff_matrix) def test_score_diffs(self): - rs = axelrod.ResultSet( + rs = axl.ResultSet( self.filename, self.players, self.repetitions, progress_bar=False ) self.assertIsInstance(rs.score_diffs, list) @@ -339,7 +338,7 @@ def test_score_diffs(self): self.assertAlmostEqual(score, self.expected_score_diffs[i][j][k]) def test_payoff_diffs_means(self): - rs = axelrod.ResultSet( + rs = axl.ResultSet( self.filename, self.players, self.repetitions, progress_bar=False ) self.assertIsInstance(rs.payoff_diffs_means, list) @@ -349,7 +348,7 @@ def test_payoff_diffs_means(self): self.assertAlmostEqual(col, self.expected_payoff_diffs_means[i][j]) def test_payoff_stddevs(self): - rs = axelrod.ResultSet( + rs = axl.ResultSet( self.filename, self.players, self.repetitions, progress_bar=False ) self.assertIsInstance(rs.payoff_stddevs, list) @@ -357,7 +356,7 @@ def test_payoff_stddevs(self): self.assertEqual(rs.payoff_stddevs, self.expected_payoff_stddevs) def test_cooperation(self): - rs = axelrod.ResultSet( + rs = axl.ResultSet( self.filename, self.players, self.repetitions, progress_bar=False ) self.assertIsInstance(rs.cooperation, list) @@ -365,7 +364,7 @@ def test_cooperation(self): self.assertEqual(rs.cooperation, self.expected_cooperation) def test_initial_cooperation_count(self): - rs = axelrod.ResultSet( + rs = axl.ResultSet( self.filename, self.players, self.repetitions, progress_bar=False ) self.assertIsInstance(rs.initial_cooperation_count, list) @@ -375,7 +374,7 @@ def test_initial_cooperation_count(self): ) def test_normalised_cooperation(self): - rs = axelrod.ResultSet( + rs = axl.ResultSet( self.filename, self.players, self.repetitions, progress_bar=False ) self.assertIsInstance(rs.normalised_cooperation, list) @@ -385,7 +384,7 @@ def test_normalised_cooperation(self): self.assertAlmostEqual(col, self.expected_normalised_cooperation[i][j]) def test_initial_cooperation_rate(self): - rs = axelrod.ResultSet( + rs = axl.ResultSet( self.filename, self.players, self.repetitions, progress_bar=False ) self.assertIsInstance(rs.initial_cooperation_rate, list) @@ -395,7 +394,7 @@ def test_initial_cooperation_rate(self): ) def test_state_distribution(self): - rs = axelrod.ResultSet( + rs = axl.ResultSet( self.filename, self.players, self.repetitions, progress_bar=False ) self.assertIsInstance(rs.state_distribution, list) @@ -403,7 +402,7 @@ def test_state_distribution(self): self.assertEqual(rs.state_distribution, self.expected_state_distribution) def test_state_normalised_distribution(self): - rs = axelrod.ResultSet( + rs = axl.ResultSet( self.filename, self.players, self.repetitions, progress_bar=False ) self.assertIsInstance(rs.normalised_state_distribution, list) @@ -414,7 +413,7 @@ def test_state_normalised_distribution(self): ) def test_state_to_action_distribution(self): - rs = axelrod.ResultSet( + rs = axl.ResultSet( self.filename, self.players, self.repetitions, progress_bar=False ) self.assertIsInstance(rs.state_to_action_distribution, list) @@ -425,7 +424,7 @@ def test_state_to_action_distribution(self): ) def test_normalised_state_to_action_distribution(self): - rs = axelrod.ResultSet( + rs = axl.ResultSet( self.filename, self.players, self.repetitions, progress_bar=False ) self.assertIsInstance(rs.normalised_state_to_action_distribution, list) @@ -438,7 +437,7 @@ def test_normalised_state_to_action_distribution(self): ) def test_vengeful_cooperation(self): - rs = axelrod.ResultSet( + rs = axl.ResultSet( self.filename, self.players, self.repetitions, progress_bar=False ) self.assertIsInstance(rs.vengeful_cooperation, list) @@ -448,7 +447,7 @@ def test_vengeful_cooperation(self): self.assertAlmostEqual(col, self.expected_vengeful_cooperation[i][j]) def test_cooperating_rating(self): - rs = axelrod.ResultSet( + rs = axl.ResultSet( self.filename, self.players, self.repetitions, progress_bar=False ) self.assertIsInstance(rs.cooperating_rating, list) @@ -456,7 +455,7 @@ def test_cooperating_rating(self): self.assertEqual(rs.cooperating_rating, self.expected_cooperating_rating) def test_good_partner_matrix(self): - rs = axelrod.ResultSet( + rs = axl.ResultSet( self.filename, self.players, self.repetitions, progress_bar=False ) self.assertIsInstance(rs.good_partner_matrix, list) @@ -464,7 +463,7 @@ def test_good_partner_matrix(self): self.assertEqual(rs.good_partner_matrix, self.expected_good_partner_matrix) def test_good_partner_rating(self): - rs = axelrod.ResultSet( + rs = axl.ResultSet( self.filename, self.players, self.repetitions, progress_bar=False ) self.assertIsInstance(rs.good_partner_rating, list) @@ -472,7 +471,7 @@ def test_good_partner_rating(self): self.assertEqual(rs.good_partner_rating, self.expected_good_partner_rating) def test_eigenjesus_rating(self): - rs = axelrod.ResultSet( + rs = axl.ResultSet( self.filename, self.players, self.repetitions, progress_bar=False ) self.assertIsInstance(rs.eigenjesus_rating, list) @@ -481,7 +480,7 @@ def test_eigenjesus_rating(self): self.assertAlmostEqual(rate, self.expected_eigenjesus_rating[j]) def test_eigenmoses_rating(self): - rs = axelrod.ResultSet( + rs = axl.ResultSet( self.filename, self.players, self.repetitions, progress_bar=False ) self.assertIsInstance(rs.eigenmoses_rating, list) @@ -493,28 +492,28 @@ def test_self_interaction_for_random_strategies(self): # Based on https://github.com/Axelrod-Python/Axelrod/issues/670 # Note that the conclusion of #670 is incorrect and only includes one of # the copies of the strategy. - axelrod.seed(0) - players = [s() for s in axelrod.demo_strategies] - tournament = axelrod.Tournament(players, repetitions=2, turns=5) + axl.seed(0) + players = [s() for s in axl.demo_strategies] + tournament = axl.Tournament(players, repetitions=2, turns=5) results = tournament.play(progress_bar=False) self.assertEqual(results.payoff_diffs_means[-1][-1], 0.0) def test_equality(self): rs_sets = [ - axelrod.ResultSet( + axl.ResultSet( self.filename, self.players, self.repetitions, progress_bar=False ) for _ in range(2) ] self.assertEqual(rs_sets[0], rs_sets[1]) - players = [s() for s in axelrod.demo_strategies] - tournament = axelrod.Tournament(players, repetitions=2, turns=5) + players = [s() for s in axl.demo_strategies] + tournament = axl.Tournament(players, repetitions=2, turns=5) results = tournament.play(progress_bar=False) self.assertNotEqual(results, rs_sets[0]) def test_summarise(self): - rs = axelrod.ResultSet( + rs = axl.ResultSet( self.filename, self.players, self.repetitions, progress_bar=False ) sd = rs.summarise() @@ -565,12 +564,12 @@ def test_summarise(self): # docs/tutorial/getting_started/summarising_tournaments.rst def test_summarise_regression_test(self): players = [ - axelrod.Cooperator(), - axelrod.Defector(), - axelrod.TitForTat(), - axelrod.Grudger(), + axl.Cooperator(), + axl.Defector(), + axl.TitForTat(), + axl.Grudger(), ] - tournament = axelrod.Tournament(players, turns=10, repetitions=3) + tournament = axl.Tournament(players, turns=10, repetitions=3) results = tournament.play() summary = [ @@ -649,7 +648,7 @@ def test_summarise_regression_test(self): ) def test_write_summary(self): - rs = axelrod.ResultSet( + rs = axl.ResultSet( self.filename, self.players, self.repetitions, progress_bar=False ) rs.write_summary(filename=self.filename + ".summary") @@ -666,7 +665,7 @@ def test_write_summary(self): class TestDecorator(unittest.TestCase): def test_update_progress_bar(self): method = lambda x: None - self.assertEqual(axelrod.result_set.update_progress_bar(method)(1), None) + self.assertEqual(axl.result_set.update_progress_bar(method)(1), None) class TestResultSetSpatialStructure(TestResultSet): @@ -678,7 +677,7 @@ class TestResultSetSpatialStructure(TestResultSet): def setUpClass(cls): cls.filename = "test_outputs/test_results_spatial.csv" - cls.players = [axelrod.Alternator(), axelrod.TitForTat(), axelrod.Defector()] + cls.players = [axl.Alternator(), axl.TitForTat(), axl.Defector()] cls.turns = 5 cls.edges = [(0, 1), (0, 2)] @@ -829,7 +828,7 @@ def test_match_lengths(self): of players-nodes that are end vertices of an edge is equal to the number of turns. Otherwise it is 0. """ - rs = axelrod.ResultSet( + rs = axl.ResultSet( self.filename, self.players, self.repetitions, progress_bar=False ) self.assertIsInstance(rs.match_lengths, list) @@ -859,10 +858,10 @@ def setUpClass(cls): cls.filename = "test_outputs/test_results_spatial_two.csv" cls.players = [ - axelrod.Alternator(), - axelrod.TitForTat(), - axelrod.Defector(), - axelrod.Cooperator(), + axl.Alternator(), + axl.TitForTat(), + axl.Defector(), + axl.Cooperator(), ] cls.turns = 5 cls.edges = [(0, 1), (2, 3)] @@ -1060,10 +1059,10 @@ def setUpClass(cls): cls.filename = "test_outputs/test_results_spatial_three.csv" cls.players = [ - axelrod.Alternator(), - axelrod.TitForTat(), - axelrod.Defector(), - axelrod.Cooperator(), + axl.Alternator(), + axl.TitForTat(), + axl.Defector(), + axl.Cooperator(), ] cls.turns = 5 cls.edges = [(0, 0), (1, 1), (2, 2), (3, 3)] @@ -1197,7 +1196,7 @@ def test_equality(self): def test_summarise(self): """Overwriting for this particular case""" - rs = axelrod.ResultSet( + rs = axl.ResultSet( self.filename, self.players, self.repetitions, progress_bar=False ) sd = rs.summarise() diff --git a/axelrod/tests/unit/test_strategy_transformers.py b/axelrod/tests/unit/test_strategy_transformers.py index be9374da2..a0cda0f6b 100644 --- a/axelrod/tests/unit/test_strategy_transformers.py +++ b/axelrod/tests/unit/test_strategy_transformers.py @@ -1,29 +1,29 @@ import unittest -import axelrod +import axelrod as axl from axelrod.strategy_transformers import * from axelrod.tests.strategies.test_cooperator import TestCooperator from axelrod.tests.strategies.test_titfortat import TestTitForTat -C, D = axelrod.Action.C, axelrod.Action.D +C, D = axl.Action.C, axl.Action.D @FlipTransformer(name_prefix=None) -class CanPickle(axelrod.Cooperator): +class CanPickle(axl.Cooperator): pass @FlipTransformer() -class CanNotPickle(axelrod.Cooperator): +class CanNotPickle(axl.Cooperator): pass class TestTransformers(unittest.TestCase): def test_player_can_be_pickled(self): - player = axelrod.Cooperator() + player = axl.Cooperator() self.assertTrue(player_can_be_pickled(player)) - player = IdentityTransformer()(axelrod.Cooperator)() + player = IdentityTransformer()(axl.Cooperator)() self.assertFalse(player_can_be_pickled(player)) player = CanPickle() @@ -33,14 +33,14 @@ def test_player_can_be_pickled(self): self.assertFalse(player_can_be_pickled(player)) def test_is_strategy_static(self): - self.assertTrue(is_strategy_static(axelrod.Cooperator)) - self.assertFalse(is_strategy_static(axelrod.Alternator)) + self.assertTrue(is_strategy_static(axl.Cooperator)) + self.assertFalse(is_strategy_static(axl.Alternator)) def test_is_strategy_static_with_inherited_strategy(self): - class NewCooperator(axelrod.Cooperator): + class NewCooperator(axl.Cooperator): pass - class NewAlternator(axelrod.Alternator): + class NewAlternator(axl.Alternator): pass self.assertTrue(is_strategy_static(NewCooperator)) @@ -57,7 +57,7 @@ def test_DecoratorReBuilder(self): new_decorator = DecoratorReBuilder()(factory_args, args, kwargs, new_prefix) self.assertEqual( - decorator(axelrod.Cooperator)(), new_decorator(axelrod.Cooperator)() + decorator(axl.Cooperator)(), new_decorator(axl.Cooperator)() ) def test_StrategyReBuilder_declared_class_with_name_prefix(self): @@ -76,7 +76,7 @@ def test_StrategyReBuilder_declared_class_with_name_prefix(self): self.assertEqual(player, new_player) def test_StrategyReBuilder_dynamically_wrapped_class_with_name_prefix(self): - player = FlipTransformer()(axelrod.Cooperator)() + player = FlipTransformer()(axl.Cooperator)() self.assertEqual(player.__class__.__name__, "FlippedCooperator") decorators = [player.decorator] @@ -91,7 +91,7 @@ def test_StrategyReBuilder_dynamically_wrapped_class_with_name_prefix(self): self.assertEqual(player, new_player) def test_StrategyReBuilder_dynamically_wrapped_class_no_name_prefix(self): - player = IdentityTransformer()(axelrod.Cooperator)() + player = IdentityTransformer()(axl.Cooperator)() self.assertEqual(player.__class__.__name__, "Cooperator") decorators = [player.decorator] @@ -109,7 +109,7 @@ def test_StrategyReBuilder_many_decorators(self): decorator_1 = IdentityTransformer() decorator_2 = FlipTransformer() decorator_3 = DualTransformer() - player = decorator_3(decorator_2(decorator_1(axelrod.Cooperator)))() + player = decorator_3(decorator_2(decorator_1(axl.Cooperator)))() self.assertEqual(player.__class__.__name__, "DualFlippedCooperator") decorators = [decorator_1, decorator_2, decorator_3] @@ -126,24 +126,24 @@ def test_StrategyReBuilder_many_decorators(self): def test_all_strategies(self): # Attempt to transform each strategy to ensure that implementation # choices (like use of super) do not cause issues - for s in axelrod.strategies: - opponent = axelrod.Cooperator() + for s in axl.strategies: + opponent = axl.Cooperator() player = IdentityTransformer()(s)() player.play(opponent) def test_naming(self): """Tests that the player and class names are properly modified.""" - cls = FlipTransformer()(axelrod.Cooperator) + cls = FlipTransformer()(axl.Cooperator) p1 = cls() self.assertEqual(cls.__name__, "FlippedCooperator") self.assertEqual(p1.name, "Flipped Cooperator") - cls = ForgiverTransformer(0.5)(axelrod.Alternator) + cls = ForgiverTransformer(0.5)(axl.Alternator) p1 = cls() self.assertEqual(cls.__name__, "ForgivingAlternator") self.assertEqual(p1.name, "Forgiving Alternator") - cls = ForgiverTransformer(0.5, name_prefix="")(axelrod.Alternator) + cls = ForgiverTransformer(0.5, name_prefix="")(axl.Alternator) p1 = cls() self.assertEqual(cls.__name__, "Alternator") self.assertEqual(p1.name, "Alternator") @@ -153,20 +153,20 @@ def test_repr(self): Transformer's parameters. """ self.assertEqual( - str(ForgiverTransformer(0.5)(axelrod.Alternator)()), + str(ForgiverTransformer(0.5)(axl.Alternator)()), "Forgiving Alternator: 0.5", ) self.assertEqual( - str(InitialTransformer([D, D, C])(axelrod.Alternator)()), + str(InitialTransformer([D, D, C])(axl.Alternator)()), "Initial Alternator: [D, D, C]", ) self.assertEqual( - str(FlipTransformer()(axelrod.Random)(0.1)), "Flipped Random: 0.1" + str(FlipTransformer()(axl.Random)(0.1)), "Flipped Random: 0.1" ) self.assertEqual( str( - MixedTransformer(0.3, (axelrod.Alternator, axelrod.Bully))( - axelrod.Random + MixedTransformer(0.3, (axl.Alternator, axl.Bully))( + axl.Random )(0.1) ), "Mutated Random: 0.1: 0.3, ['Alternator', 'Bully']", @@ -174,17 +174,17 @@ def test_repr(self): def test_doc(self): """Test that the original docstring is present""" - player = axelrod.Alternator() - transformer = InitialTransformer([D, D, C])(axelrod.Alternator)() + player = axl.Alternator() + transformer = InitialTransformer([D, D, C])(axl.Alternator)() self.assertEqual(player.__doc__, transformer.__doc__) def test_cloning(self): """Tests that Player.clone preserves the application of transformations. """ - p1 = axelrod.Cooperator() - p2 = FlipTransformer()(axelrod.Cooperator)() # Defector + p1 = axl.Cooperator() + p2 = FlipTransformer()(axl.Cooperator)() # Defector p3 = p2.clone() - match = axelrod.Match((p1, p3), turns=2) + match = axl.Match((p1, p3), turns=2) results = match.play() self.assertEqual(results, [(C, D), (C, D)]) @@ -192,18 +192,18 @@ def test_generic(self): """Test that the generic wrapper does nothing.""" # This is the identity transformer transformer = StrategyTransformerFactory(generic_strategy_wrapper)() - Cooperator2 = transformer(axelrod.Cooperator) + Cooperator2 = transformer(axl.Cooperator) p1 = Cooperator2() - p2 = axelrod.Cooperator() - match = axelrod.Match((p1, p2), turns=2) + p2 = axl.Cooperator() + match = axl.Match((p1, p2), turns=2) results = match.play() self.assertEqual(results, [(C, C), (C, C)]) def test_flip_transformer(self): """Tests that FlipTransformer(Cooperator) == Defector.""" - p1 = axelrod.Cooperator() - p2 = FlipTransformer()(axelrod.Cooperator)() # Defector - match = axelrod.Match((p1, p2), turns=3) + p1 = axl.Cooperator() + p2 = FlipTransformer()(axl.Cooperator)() # Defector + match = axl.Match((p1, p2), turns=3) results = match.play() self.assertEqual(results, [(C, D), (C, D), (C, D)]) @@ -211,14 +211,14 @@ def test_dual_transformer_with_all_strategies(self): """Tests that DualTransformer produces the opposite results when faced with the same opponent history. """ - for s in axelrod.short_run_time_strategies: + for s in axl.short_run_time_strategies: self.assert_dual_wrapper_correct(s) def test_dual_jossann_regression_test(self): - player_class = JossAnnTransformer((0.2, 0.3))(axelrod.Alternator) + player_class = JossAnnTransformer((0.2, 0.3))(axl.Alternator) self.assert_dual_wrapper_correct(player_class) - player_class = JossAnnTransformer((0.5, 0.4))(axelrod.EvolvedLookerUp2_2_2) + player_class = JossAnnTransformer((0.5, 0.4))(axl.EvolvedLookerUp2_2_2) self.assert_dual_wrapper_correct(player_class) def test_dual_transformer_simple_play_regression_test(self): @@ -227,11 +227,11 @@ def test_dual_transformer_simple_play_regression_test(self): transformer or when other transformers were between multiple DualTransformers.""" multiple_dual_transformers = DualTransformer()( - FlipTransformer()(DualTransformer()(axelrod.Cooperator)) + FlipTransformer()(DualTransformer()(axl.Cooperator)) )() dual_transformer_not_first = IdentityTransformer()( - DualTransformer()(axelrod.Cooperator) + DualTransformer()(axl.Cooperator) )() for _ in range(3): @@ -246,12 +246,12 @@ def test_dual_transformer_multiple_interspersed_regression_test(self): transformer or when other transformers were between multiple DualTransformers.""" dual_not_first_transformer = IdentityTransformer()( - DualTransformer()(axelrod.EvolvedANN) + DualTransformer()(axl.EvolvedANN) ) self.assert_dual_wrapper_correct(dual_not_first_transformer) multiple_dual_transformers = DualTransformer()( - DualTransformer()(axelrod.WinStayLoseShift) + DualTransformer()(axl.WinStayLoseShift) ) self.assert_dual_wrapper_correct(multiple_dual_transformers) @@ -260,15 +260,15 @@ def assert_dual_wrapper_correct(self, player_class): p1 = player_class() p2 = DualTransformer()(player_class)() - p3 = axelrod.CyclerCCD() # Cycles 'CCD' + p3 = axl.CyclerCCD() # Cycles 'CCD' - axelrod.seed(0) + axl.seed(0) for _ in range(turns): p1.play(p3) p3.reset() - axelrod.seed(0) + axl.seed(0) for _ in range(turns): p2.play(p3) @@ -278,66 +278,66 @@ def test_jossann_transformer(self): """Tests the JossAnn transformer. """ probability = (1, 0) - p1 = JossAnnTransformer(probability)(axelrod.Defector)() + p1 = JossAnnTransformer(probability)(axl.Defector)() self.assertFalse(p1.classifier["stochastic"]) - p2 = axelrod.Cooperator() + p2 = axl.Cooperator() for _ in range(5): p1.play(p2) self.assertEqual(p1.history, [C, C, C, C, C]) probability = (0, 1) - p1 = JossAnnTransformer(probability)(axelrod.Cooperator)() + p1 = JossAnnTransformer(probability)(axl.Cooperator)() self.assertFalse(p1.classifier["stochastic"]) - p2 = axelrod.Cooperator() + p2 = axl.Cooperator() for _ in range(5): p1.play(p2) self.assertEqual(p1.history, [D, D, D, D, D]) probability = (0.3, 0.3) - p1 = JossAnnTransformer(probability)(axelrod.TitForTat)() + p1 = JossAnnTransformer(probability)(axl.TitForTat)() self.assertTrue(p1.classifier["stochastic"]) - p2 = axelrod.Cycler() - axelrod.seed(0) + p2 = axl.Cycler() + axl.seed(0) for _ in range(5): p1.play(p2) self.assertEqual(p1.history, [D, C, C, D, D]) probability = (0.6, 0.6) - p1 = JossAnnTransformer(probability)(axelrod.Cooperator)() + p1 = JossAnnTransformer(probability)(axl.Cooperator)() self.assertTrue(p1.classifier["stochastic"]) - p2 = axelrod.Cooperator() + p2 = axl.Cooperator() for _ in range(5): p1.play(p2) self.assertEqual(p1.history, [D, C, D, D, C]) probability = (0, 1) - p1 = JossAnnTransformer(probability)(axelrod.Random) + p1 = JossAnnTransformer(probability)(axl.Random) self.assertFalse(p1.classifier["stochastic"]) self.assertFalse(p1().classifier["stochastic"]) probability = (1, 0) - p1 = JossAnnTransformer(probability)(axelrod.Random) + p1 = JossAnnTransformer(probability)(axl.Random) self.assertFalse(p1.classifier["stochastic"]) self.assertFalse(p1().classifier["stochastic"]) probability = (0.5, 0.5) - p1 = JossAnnTransformer(probability)(axelrod.TitForTat) + p1 = JossAnnTransformer(probability)(axl.TitForTat) self.assertTrue(p1.classifier["stochastic"]) self.assertTrue(p1().classifier["stochastic"]) probability = (0, 0.5) - p1 = JossAnnTransformer(probability)(axelrod.TitForTat) + p1 = JossAnnTransformer(probability)(axl.TitForTat) self.assertTrue(p1.classifier["stochastic"]) self.assertTrue(p1().classifier["stochastic"]) probability = (0, 0) - p1 = JossAnnTransformer(probability)(axelrod.TitForTat) + p1 = JossAnnTransformer(probability)(axl.TitForTat) self.assertFalse(p1.classifier["stochastic"]) self.assertFalse(p1().classifier["stochastic"]) probability = (0, 0) - p1 = JossAnnTransformer(probability)(axelrod.Random) + p1 = JossAnnTransformer(probability)(axl.Random) self.assertTrue(p1.classifier["stochastic"]) self.assertTrue(p1().classifier["stochastic"]) @@ -345,115 +345,115 @@ def test_noisy_transformer(self): """Tests that the noisy transformed does flip some moves.""" random.seed(5) # Cooperator to Defector - p1 = axelrod.Cooperator() - p2 = NoisyTransformer(0.5)(axelrod.Cooperator)() + p1 = axl.Cooperator() + p2 = NoisyTransformer(0.5)(axl.Cooperator)() self.assertTrue(p2.classifier["stochastic"]) for _ in range(10): p1.play(p2) self.assertEqual(p2.history, [C, C, C, C, C, C, D, D, C, C]) - p2 = NoisyTransformer(0)(axelrod.Cooperator) + p2 = NoisyTransformer(0)(axl.Cooperator) self.assertFalse(p2.classifier["stochastic"]) self.assertFalse(p2().classifier["stochastic"]) - p2 = NoisyTransformer(1)(axelrod.Cooperator) + p2 = NoisyTransformer(1)(axl.Cooperator) self.assertFalse(p2.classifier["stochastic"]) self.assertFalse(p2().classifier["stochastic"]) - p2 = NoisyTransformer(0.3)(axelrod.Cooperator) + p2 = NoisyTransformer(0.3)(axl.Cooperator) self.assertTrue(p2.classifier["stochastic"]) self.assertTrue(p2().classifier["stochastic"]) - p2 = NoisyTransformer(0)(axelrod.Random) + p2 = NoisyTransformer(0)(axl.Random) self.assertTrue(p2.classifier["stochastic"]) self.assertTrue(p2().classifier["stochastic"]) - p2 = NoisyTransformer(1)(axelrod.Random) + p2 = NoisyTransformer(1)(axl.Random) self.assertTrue(p2.classifier["stochastic"]) self.assertTrue(p2().classifier["stochastic"]) def test_forgiving(self): """Tests that the forgiving transformer flips some defections.""" random.seed(10) - p1 = ForgiverTransformer(0.5)(axelrod.Alternator)() + p1 = ForgiverTransformer(0.5)(axl.Alternator)() self.assertTrue(p1.classifier["stochastic"]) - p2 = axelrod.Defector() + p2 = axl.Defector() for _ in range(10): p1.play(p2) self.assertEqual(p1.history, [C, D, C, C, D, C, C, D, C, D]) - p1 = ForgiverTransformer(0)(axelrod.Alternator)() + p1 = ForgiverTransformer(0)(axl.Alternator)() self.assertFalse(p1.classifier["stochastic"]) - p1 = ForgiverTransformer(1)(axelrod.Alternator)() + p1 = ForgiverTransformer(1)(axl.Alternator)() self.assertFalse(p1.classifier["stochastic"]) def test_initial_transformer(self): """Tests the InitialTransformer.""" - p1 = axelrod.Cooperator() + p1 = axl.Cooperator() self.assertEqual(p1.classifier["memory_depth"], 0) - p2 = InitialTransformer([D, D])(axelrod.Cooperator)() + p2 = InitialTransformer([D, D])(axl.Cooperator)() self.assertEqual(p2.classifier["memory_depth"], 2) for _ in range(5): p1.play(p2) self.assertEqual(p2.history, [D, D, C, C, C]) - p1 = axelrod.Cooperator() - p2 = InitialTransformer([D, D, C, D])(axelrod.Cooperator)() + p1 = axl.Cooperator() + p2 = InitialTransformer([D, D, C, D])(axl.Cooperator)() for _ in range(5): p1.play(p2) self.assertEqual(p2.history, [D, D, C, D, C]) - p3 = InitialTransformer([D, D])(axelrod.Adaptive)() + p3 = InitialTransformer([D, D])(axl.Adaptive)() self.assertEqual(p3.classifier["memory_depth"], float("inf")) def test_final_transformer(self): """Tests the FinalTransformer when tournament length is known.""" # Final play transformer - p1 = axelrod.Cooperator() - p2 = FinalTransformer([D, D, D])(axelrod.Cooperator)() + p1 = axl.Cooperator() + p2 = FinalTransformer([D, D, D])(axl.Cooperator)() self.assertEqual(p2.classifier["makes_use_of"], set(["length"])) self.assertEqual(p2.classifier["memory_depth"], 3) - self.assertEqual(axelrod.Cooperator.classifier["makes_use_of"], set([])) + self.assertEqual(axl.Cooperator.classifier["makes_use_of"], set([])) p2.match_attributes["length"] = 6 for _ in range(8): p1.play(p2) self.assertEqual(p2.history, [C, C, C, D, D, D, C, C]) - p3 = FinalTransformer([D, D])(axelrod.Adaptive)() + p3 = FinalTransformer([D, D])(axl.Adaptive)() self.assertEqual(p3.classifier["memory_depth"], float("inf")) def test_final_transformer2(self): """Tests the FinalTransformer when tournament length is not known.""" - p1 = axelrod.Cooperator() - p2 = FinalTransformer([D, D])(axelrod.Cooperator)() + p1 = axl.Cooperator() + p2 = FinalTransformer([D, D])(axl.Cooperator)() for _ in range(6): p1.play(p2) self.assertEqual(p2.history, [C, C, C, C, C, C]) def test_history_track(self): """Tests the history tracking transformer.""" - p1 = axelrod.Cooperator() - p2 = TrackHistoryTransformer()(axelrod.Random)() + p1 = axl.Cooperator() + p2 = TrackHistoryTransformer()(axl.Random)() for _ in range(6): p1.play(p2) self.assertEqual(p2.history, p2._recorded_history) def test_composition(self): """Tests that transformations can be chained or composed.""" - cls1 = InitialTransformer([D, D])(axelrod.Cooperator) + cls1 = InitialTransformer([D, D])(axl.Cooperator) cls2 = FinalTransformer([D, D])(cls1) p1 = cls2() - p2 = axelrod.Cooperator() + p2 = axl.Cooperator() p1.match_attributes["length"] = 8 for _ in range(8): p1.play(p2) self.assertEqual(p1.history, [D, D, C, C, C, C, D, D]) - cls1 = FinalTransformer([D, D])(InitialTransformer([D, D])(axelrod.Cooperator)) + cls1 = FinalTransformer([D, D])(InitialTransformer([D, D])(axl.Cooperator)) p1 = cls1() - p2 = axelrod.Cooperator() + p2 = axl.Cooperator() p1.match_attributes["length"] = 8 for _ in range(8): p1.play(p2) @@ -463,8 +463,8 @@ def test_compose_transformers(self): cls1 = compose_transformers( FinalTransformer([D, D]), InitialTransformer([D, D]) ) - p1 = cls1(axelrod.Cooperator)() - p2 = axelrod.Cooperator() + p1 = cls1(axl.Cooperator)() + p2 = axl.Cooperator() p1.match_attributes["length"] = 8 for _ in range(8): p1.play(p2) @@ -472,23 +472,23 @@ def test_compose_transformers(self): def test_retailiation(self): """Tests the RetaliateTransformer.""" - p1 = RetaliationTransformer(1)(axelrod.Cooperator)() - p2 = axelrod.Defector() + p1 = RetaliationTransformer(1)(axl.Cooperator)() + p2 = axl.Defector() for _ in range(5): p1.play(p2) self.assertEqual(p1.history, [C, D, D, D, D]) self.assertEqual(p2.history, [D, D, D, D, D]) - p1 = RetaliationTransformer(1)(axelrod.Cooperator)() - p2 = axelrod.Alternator() + p1 = RetaliationTransformer(1)(axl.Cooperator)() + p2 = axl.Alternator() for _ in range(5): p1.play(p2) self.assertEqual(p1.history, [C, C, D, C, D]) self.assertEqual(p2.history, [C, D, C, D, C]) - TwoTitsForTat = RetaliationTransformer(2)(axelrod.Cooperator) + TwoTitsForTat = RetaliationTransformer(2)(axl.Cooperator) p1 = TwoTitsForTat() - p2 = axelrod.CyclerCCD() + p2 = axl.CyclerCCD() for _ in range(9): p1.play(p2) self.assertEqual(p1.history, [C, C, C, D, D, C, D, D, C]) @@ -496,37 +496,37 @@ def test_retailiation(self): def test_retaliation_until_apology(self): """Tests the RetaliateUntilApologyTransformer.""" - TFT = RetaliateUntilApologyTransformer()(axelrod.Cooperator) + TFT = RetaliateUntilApologyTransformer()(axl.Cooperator) p1 = TFT() - p2 = axelrod.Cooperator() + p2 = axl.Cooperator() p1.play(p2) p1.play(p2) self.assertEqual(p1.history, [C, C]) p1 = TFT() - p2 = axelrod.Defector() + p2 = axl.Defector() p1.play(p2) p1.play(p2) self.assertEqual(p1.history, [C, D]) random.seed(12) p1 = TFT() - p2 = axelrod.Random() + p2 = axl.Random() for _ in range(5): p1.play(p2) self.assertEqual(p1.history, [C, C, D, D, C]) def test_apology(self): """Tests the ApologyTransformer.""" - ApologizingDefector = ApologyTransformer([D], [C])(axelrod.Defector) + ApologizingDefector = ApologyTransformer([D], [C])(axl.Defector) p1 = ApologizingDefector() - p2 = axelrod.Cooperator() + p2 = axl.Cooperator() for _ in range(5): p1.play(p2) self.assertEqual(p1.history, [D, C, D, C, D]) - ApologizingDefector = ApologyTransformer([D, D], [C, C])(axelrod.Defector) + ApologizingDefector = ApologyTransformer([D, D], [C, C])(axl.Defector) p1 = ApologizingDefector() - p2 = axelrod.Cooperator() + p2 = axl.Cooperator() for _ in range(6): p1.play(p2) self.assertEqual(p1.history, [D, D, C, D, D, C]) @@ -534,21 +534,21 @@ def test_apology(self): def test_mixed(self): """Tests the MixedTransformer.""" probability = 1 - MD = MixedTransformer(probability, axelrod.Cooperator)(axelrod.Defector) + MD = MixedTransformer(probability, axl.Cooperator)(axl.Defector) self.assertFalse(MD.classifier["stochastic"]) p1 = MD() - p2 = axelrod.Cooperator() + p2 = axl.Cooperator() for _ in range(5): p1.play(p2) self.assertEqual(p1.history, [C, C, C, C, C]) probability = 0 - MD = MixedTransformer(probability, axelrod.Cooperator)(axelrod.Defector) + MD = MixedTransformer(probability, axl.Cooperator)(axl.Defector) self.assertFalse(MD.classifier["stochastic"]) p1 = MD() - p2 = axelrod.Cooperator() + p2 = axl.Cooperator() for _ in range(5): p1.play(p2) self.assertEqual(p1.history, [D, D, D, D, D]) @@ -557,26 +557,26 @@ def test_mixed(self): # Decorate a cooperator putting all weight on other strategies that are # 'nice' probability = [0.3, 0.2, 0] - strategies = [axelrod.TitForTat, axelrod.Grudger, axelrod.Defector] - MD = MixedTransformer(probability, strategies)(axelrod.Cooperator) + strategies = [axl.TitForTat, axl.Grudger, axl.Defector] + MD = MixedTransformer(probability, strategies)(axl.Cooperator) self.assertTrue(MD.classifier["stochastic"]) p1 = MD() # Against a cooperator we see that we only cooperate - p2 = axelrod.Cooperator() + p2 = axl.Cooperator() for _ in range(5): p1.play(p2) self.assertEqual(p1.history, [C, C, C, C, C]) # Decorate a cooperator putting all weight on Defector probability = (0, 0, 1) # Note can also pass tuple - strategies = [axelrod.TitForTat, axelrod.Grudger, axelrod.Defector] - MD = MixedTransformer(probability, strategies)(axelrod.Cooperator) + strategies = [axl.TitForTat, axl.Grudger, axl.Defector] + MD = MixedTransformer(probability, strategies)(axl.Cooperator) self.assertFalse(MD.classifier["stochastic"]) p1 = MD() # Against a cooperator we see that we only defect - p2 = axelrod.Cooperator() + p2 = axl.Cooperator() for _ in range(5): p1.play(p2) self.assertEqual(p1.history, [D, D, D, D, D]) @@ -584,8 +584,8 @@ def test_mixed(self): def test_deadlock(self): """Test the DeadlockBreakingTransformer.""" # We can induce a deadlock by alterting TFT to defect first - p1 = axelrod.TitForTat() - p2 = InitialTransformer([D])(axelrod.TitForTat)() + p1 = axl.TitForTat() + p2 = InitialTransformer([D])(axl.TitForTat)() for _ in range(4): p1.play(p2) self.assertEqual(p1.history, [C, D, C, D]) @@ -593,8 +593,8 @@ def test_deadlock(self): # Now let's use the transformer to break the deadlock to achieve # Mutual cooperation - p1 = axelrod.TitForTat() - p2 = DeadlockBreakingTransformer()(InitialTransformer([D])(axelrod.TitForTat))() + p1 = axl.TitForTat() + p2 = DeadlockBreakingTransformer()(InitialTransformer([D])(axl.TitForTat))() for _ in range(4): p1.play(p2) self.assertEqual(p1.history, [C, D, C, C]) @@ -602,15 +602,15 @@ def test_deadlock(self): def test_grudging(self): """Test the GrudgeTransformer.""" - p1 = axelrod.Defector() - p2 = GrudgeTransformer(1)(axelrod.Cooperator)() + p1 = axl.Defector() + p2 = GrudgeTransformer(1)(axl.Cooperator)() for _ in range(4): p1.play(p2) self.assertEqual(p1.history, [D, D, D, D]) self.assertEqual(p2.history, [C, C, D, D]) - p1 = InitialTransformer([C])(axelrod.Defector)() - p2 = GrudgeTransformer(2)(axelrod.Cooperator)() + p1 = InitialTransformer([C])(axl.Defector)() + p2 = GrudgeTransformer(2)(axl.Cooperator)() for _ in range(8): p1.play(p2) self.assertEqual(p1.history, [C, D, D, D, D, D, D, D]) @@ -618,22 +618,22 @@ def test_grudging(self): def test_nice(self): """Tests the NiceTransformer.""" - p1 = NiceTransformer()(axelrod.Defector)() - p2 = axelrod.Defector() + p1 = NiceTransformer()(axl.Defector)() + p2 = axl.Defector() for _ in range(5): p1.play(p2) self.assertEqual(p1.history, [C, D, D, D, D]) self.assertEqual(p2.history, [D, D, D, D, D]) - p1 = NiceTransformer()(axelrod.Defector)() - p2 = axelrod.Alternator() + p1 = NiceTransformer()(axl.Defector)() + p2 = axl.Alternator() for _ in range(5): p1.play(p2) self.assertEqual(p1.history, [C, C, D, D, D]) self.assertEqual(p2.history, [C, D, C, D, C]) - p1 = NiceTransformer()(axelrod.Defector)() - p2 = axelrod.Cooperator() + p1 = NiceTransformer()(axl.Defector)() + p2 = axl.Cooperator() for _ in range(5): p1.play(p2) self.assertEqual(p1.history, [C, C, C, C, C]) @@ -647,8 +647,8 @@ def test_nilpotency(self): FlipTransformer(), TrackHistoryTransformer(), ]: - for PlayerClass in [axelrod.Cooperator, axelrod.Defector]: - for third_player in [axelrod.Cooperator(), axelrod.Defector()]: + for PlayerClass in [axl.Cooperator, axl.Defector]: + for third_player in [axl.Cooperator(), axl.Defector()]: player = PlayerClass() transformed = transformer(transformer(PlayerClass))() clone = third_player.clone() @@ -675,8 +675,8 @@ def test_idempotency(self): TrackHistoryTransformer(), ApologyTransformer([D], [C]), ]: - for PlayerClass in [axelrod.Cooperator, axelrod.Defector]: - for third_player in [axelrod.Cooperator(), axelrod.Defector()]: + for PlayerClass in [axl.Cooperator, axl.Defector]: + for third_player in [axl.Cooperator(), axl.Defector()]: clone = third_player.clone() player = transformer(PlayerClass)() transformed = transformer(transformer(PlayerClass))() @@ -690,22 +690,22 @@ def test_implementation(self): FlipTransformer is applied to Alternator and CyclerCD. In other words, the implementation matters, not just the outcomes.""" # Difference between Alternator and CyclerCD - p1 = axelrod.Cycler(cycle="CD") - p2 = FlipTransformer()(axelrod.Cycler)(cycle="CD") + p1 = axl.Cycler(cycle="CD") + p2 = FlipTransformer()(axl.Cycler)(cycle="CD") for _ in range(5): p1.play(p2) self.assertEqual(p1.history, [C, D, C, D, C]) self.assertEqual(p2.history, [D, C, D, C, D]) - p1 = axelrod.Alternator() - p2 = FlipTransformer()(axelrod.Alternator)() + p1 = axl.Alternator() + p2 = FlipTransformer()(axl.Alternator)() for _ in range(5): p1.play(p2) self.assertEqual(p1.history, [C, D, C, D, C]) self.assertEqual(p2.history, [D, D, D, D, D]) -TFT = RetaliateUntilApologyTransformer()(axelrod.Cooperator) +TFT = RetaliateUntilApologyTransformer()(axl.Cooperator) class TestRUAisTFT(TestTitForTat): @@ -724,7 +724,7 @@ class TestRUAisTFT(TestTitForTat): # Test that FlipTransformer(Defector) == Cooperator -Cooperator2 = FlipTransformer()(axelrod.Defector) +Cooperator2 = FlipTransformer()(axl.Defector) class TestFlipDefector(TestCooperator): diff --git a/axelrod/tests/unit/test_strategy_utils.py b/axelrod/tests/unit/test_strategy_utils.py index 73dfaf421..fef7e2af6 100644 --- a/axelrod/tests/unit/test_strategy_utils.py +++ b/axelrod/tests/unit/test_strategy_utils.py @@ -2,8 +2,7 @@ import unittest -import axelrod -from axelrod import Action, Game, Player +import axelrod as axl from axelrod._strategy_utils import ( detect_cycle, inspect_strategy, @@ -16,7 +15,7 @@ from hypothesis import given, settings from hypothesis.strategies import integers, lists, sampled_from -C, D = Action.C, Action.D +C, D = axl.Action.C, axl.Action.D class TestDetectCycle(unittest.TestCase): @@ -66,8 +65,8 @@ def test_cycle_greater_than_max_size_returns_none(self): class TestInspectStrategy(unittest.TestCase): def test_strategies_without_countermeasures_return_their_strategy(self): - tft = axelrod.TitForTat() - inspector = axelrod.Alternator() + tft = axl.TitForTat() + inspector = axl.Alternator() tft.play(inspector) self.assertEqual(tft.history, [C]) @@ -78,8 +77,8 @@ def test_strategies_without_countermeasures_return_their_strategy(self): self.assertEqual(tft.strategy(inspector), D) def test_strategies_with_countermeasures_return_their_countermeasures(self): - d_geller = axelrod.GellerDefector() - inspector = axelrod.Cooperator() + d_geller = axl.GellerDefector() + inspector = axl.Cooperator() d_geller.play(inspector) self.assertEqual(inspect_strategy(inspector=inspector, opponent=d_geller), D) @@ -88,16 +87,16 @@ def test_strategies_with_countermeasures_return_their_countermeasures(self): class TestSimulateMatch(unittest.TestCase): def test_tft_reacts_to_cooperation(self): - tft = axelrod.TitForTat() - inspector = axelrod.Alternator() + tft = axl.TitForTat() + inspector = axl.Alternator() simulate_match(inspector, tft, C, 5) self.assertEqual(inspector.history, [C, C, C, C, C]) self.assertEqual(tft.history, [C, C, C, C, C]) def test_tft_reacts_to_defection(self): - tft = axelrod.TitForTat() - inspector = axelrod.Alternator() + tft = axl.TitForTat() + inspector = axl.Alternator() simulate_match(inspector, tft, D, 5) self.assertEqual(inspector.history, [D, D, D, D, D]) @@ -106,18 +105,18 @@ def test_tft_reacts_to_defection(self): class TestLookAhead(unittest.TestCase): def setUp(self): - self.inspector = Player() - self.game = Game() + self.inspector = axl.Player() + self.game = axl.Game() def test_cooperator(self): - tft = axelrod.Cooperator() + tft = axl.Cooperator() # It always makes sense to defect here. self.assertEqual(look_ahead(self.inspector, tft, self.game, 1), D) self.assertEqual(look_ahead(self.inspector, tft, self.game, 2), D) self.assertEqual(look_ahead(self.inspector, tft, self.game, 5), D) def test_tit_for_tat(self): - tft = axelrod.TitForTat() + tft = axl.TitForTat() # Cooperation should be chosen if we look ahead further than one move. self.assertEqual(look_ahead(self.inspector, tft, self.game, 1), D) self.assertEqual(look_ahead(self.inspector, tft, self.game, 2), C) diff --git a/axelrod/tests/unit/test_tournament.py b/axelrod/tests/unit/test_tournament.py index 27f994f09..b4d651032 100644 --- a/axelrod/tests/unit/test_tournament.py +++ b/axelrod/tests/unit/test_tournament.py @@ -1,4 +1,6 @@ """Tests for the main tournament class.""" +import unittest +from unittest.mock import MagicMock, patch import csv import filecmp @@ -6,14 +8,13 @@ import logging import os import pickle -import unittest import warnings from multiprocessing import Queue, cpu_count -from unittest.mock import MagicMock, patch - -import axelrod import numpy as np import pandas as pd +from tqdm import tqdm + +import axelrod as axl from axelrod.tests.property import ( prob_end_tournaments, spatial_tournaments, @@ -21,19 +22,18 @@ tournaments, ) from axelrod.tournament import _close_objects -from tqdm import tqdm from hypothesis import example, given, settings from hypothesis.strategies import floats, integers -C, D = axelrod.Action.C, axelrod.Action.D +C, D = axl.Action.C, axl.Action.D test_strategies = [ - axelrod.Cooperator, - axelrod.TitForTat, - axelrod.Defector, - axelrod.Grudger, - axelrod.GoByMajority, + axl.Cooperator, + axl.TitForTat, + axl.Defector, + axl.Grudger, + axl.GoByMajority, ] test_repetitions = 5 test_turns = 100 @@ -43,7 +43,7 @@ test_edges = [(0, 1), (1, 2), (3, 4)] deterministic_strategies = [ - s for s in axelrod.short_run_time_strategies if not s().classifier["stochastic"] + s for s in axl.short_run_time_strategies if not s().classifier["stochastic"] ] @@ -66,7 +66,7 @@ def reset_record(cls): class TestTournament(unittest.TestCase): @classmethod def setUpClass(cls): - cls.game = axelrod.Game() + cls.game = axl.Game() cls.players = [s() for s in test_strategies] cls.test_name = "test" cls.test_repetitions = test_repetitions @@ -91,7 +91,7 @@ def setUpClass(cls): cls.filename = "test_outputs/test_tournament.csv" def setUp(self): - self.test_tournament = axelrod.Tournament( + self.test_tournament = axl.Tournament( name=self.test_name, players=self.players, game=self.game, @@ -100,7 +100,7 @@ def setUp(self): ) def test_init(self): - tournament = axelrod.Tournament( + tournament = axl.Tournament( name=self.test_name, players=self.players, game=self.game, @@ -109,7 +109,7 @@ def test_init(self): ) self.assertEqual(len(tournament.players), len(test_strategies)) self.assertIsInstance( - tournament.players[0].match_attributes["game"], axelrod.Game + tournament.players[0].match_attributes["game"], axl.Game ) self.assertEqual(tournament.game.score((C, C)), (3, 3)) self.assertEqual(tournament.turns, self.test_turns) @@ -117,11 +117,11 @@ def test_init(self): self.assertEqual(tournament.name, "test") self.assertIsInstance(tournament._logger, logging.Logger) self.assertEqual(tournament.noise, 0.2) - anonymous_tournament = axelrod.Tournament(players=self.players) + anonymous_tournament = axl.Tournament(players=self.players) self.assertEqual(anonymous_tournament.name, "axelrod") def test_init_with_match_attributes(self): - tournament = axelrod.Tournament( + tournament = axl.Tournament( players=self.players, match_attributes={"length": float("inf")} ) mg = tournament.match_generator @@ -129,7 +129,7 @@ def test_init_with_match_attributes(self): self.assertEqual(match_params["match_attributes"], {"length": float("inf")}) def test_warning(self): - tournament = axelrod.Tournament( + tournament = axl.Tournament( name=self.test_name, players=self.players, game=self.game, @@ -244,7 +244,7 @@ def test_get_progress_bar(self): self.assertEqual(pbar.total, self.test_tournament.match_generator.size) new_edges = [(0, 1), (1, 2), (2, 3), (3, 4)] - new_tournament = axelrod.Tournament(players=self.players, edges=new_edges) + new_tournament = axl.Tournament(players=self.players, edges=new_edges) new_tournament.use_progress_bar = True pbar = new_tournament._get_progress_bar() self.assertEqual(pbar.desc, "Playing matches") @@ -253,22 +253,22 @@ def test_get_progress_bar(self): def test_serial_play(self): # Test that we get an instance of ResultSet - tournament = axelrod.Tournament( + tournament = axl.Tournament( name=self.test_name, players=self.players, game=self.game, - turns=axelrod.DEFAULT_TURNS, + turns=axl.DEFAULT_TURNS, repetitions=self.test_repetitions, ) results = tournament.play(progress_bar=False) - self.assertIsInstance(results, axelrod.ResultSet) + self.assertIsInstance(results, axl.ResultSet) # Test that _run_serial_repetitions is called with empty matches list - tournament = axelrod.Tournament( + tournament = axl.Tournament( name=self.test_name, players=self.players, game=self.game, - turns=axelrod.DEFAULT_TURNS, + turns=axl.DEFAULT_TURNS, repetitions=self.test_repetitions, ) results = tournament.play(progress_bar=False) @@ -276,8 +276,8 @@ def test_serial_play(self): def test_serial_play_with_different_game(self): # Test that a non default game is passed to the result set - game = axelrod.Game(p=-1, r=-1, s=-1, t=-1) - tournament = axelrod.Tournament( + game = axl.Game(p=-1, r=-1, s=-1, t=-1) + tournament = axl.Tournament( name=self.test_name, players=self.players, game=game, turns=1, repetitions=1 ) results = tournament.play(progress_bar=False) @@ -286,18 +286,18 @@ def test_serial_play_with_different_game(self): @patch("tqdm.tqdm", RecordedTQDM) def test_no_progress_bar_play(self): """Test that progress bar is not created for progress_bar=False""" - tournament = axelrod.Tournament( + tournament = axl.Tournament( name=self.test_name, players=self.players, game=self.game, - turns=axelrod.DEFAULT_TURNS, + turns=axl.DEFAULT_TURNS, repetitions=self.test_repetitions, ) # Test with build results RecordedTQDM.reset_record() results = tournament.play(progress_bar=False) - self.assertIsInstance(results, axelrod.ResultSet) + self.assertIsInstance(results, axl.ResultSet) # Check that no progress bar was created. self.assertEqual(RecordedTQDM.record, []) @@ -318,17 +318,17 @@ def assert_play_pbar_correct_total_and_finished(self, pbar, total): @patch("tqdm.tqdm", RecordedTQDM) def test_progress_bar_play(self): """Test that progress bar is created by default and with True argument""" - tournament = axelrod.Tournament( + tournament = axl.Tournament( name=self.test_name, players=self.players, game=self.game, - turns=axelrod.DEFAULT_TURNS, + turns=axl.DEFAULT_TURNS, repetitions=self.test_repetitions, ) RecordedTQDM.reset_record() results = tournament.play() - self.assertIsInstance(results, axelrod.ResultSet) + self.assertIsInstance(results, axl.ResultSet) # Check that progress bar was created, updated and closed. self.assertEqual(len(RecordedTQDM.record), 2) play_pbar = RecordedTQDM.record[0] @@ -338,7 +338,7 @@ def test_progress_bar_play(self): RecordedTQDM.reset_record() results = tournament.play(progress_bar=True) - self.assertIsInstance(results, axelrod.ResultSet) + self.assertIsInstance(results, axl.ResultSet) self.assertEqual(len(RecordedTQDM.record), 2) play_pbar = RecordedTQDM.record[0] self.assert_play_pbar_correct_total_and_finished(play_pbar, total=15) @@ -357,11 +357,11 @@ def test_progress_bar_play(self): def test_progress_bar_play_parallel(self): """Test that tournament plays when asking for progress bar for parallel tournament and that progress bar is created.""" - tournament = axelrod.Tournament( + tournament = axl.Tournament( name=self.test_name, players=self.players, game=self.game, - turns=axelrod.DEFAULT_TURNS, + turns=axl.DEFAULT_TURNS, repetitions=self.test_repetitions, ) @@ -369,12 +369,12 @@ def test_progress_bar_play_parallel(self): RecordedTQDM.reset_record() results = tournament.play(progress_bar=False, processes=2) self.assertEqual(RecordedTQDM.record, []) - self.assertIsInstance(results, axelrod.ResultSet) + self.assertIsInstance(results, axl.ResultSet) # progress_bar = True RecordedTQDM.reset_record() results = tournament.play(progress_bar=True, processes=2) - self.assertIsInstance(results, axelrod.ResultSet) + self.assertIsInstance(results, axl.ResultSet) self.assertEqual(len(RecordedTQDM.record), 2) play_pbar = RecordedTQDM.record[0] @@ -383,7 +383,7 @@ def test_progress_bar_play_parallel(self): # progress_bar is default RecordedTQDM.reset_record() results = tournament.play(processes=2) - self.assertIsInstance(results, axelrod.ResultSet) + self.assertIsInstance(results, axl.ResultSet) self.assertEqual(len(RecordedTQDM.record), 2) play_pbar = RecordedTQDM.record[0] @@ -401,7 +401,7 @@ def test_progress_bar_play_parallel(self): ) @settings(max_examples=50) @example( - tournament=axelrod.Tournament( + tournament=axl.Tournament( players=[s() for s in test_strategies], turns=test_turns, repetitions=test_repetitions, @@ -412,48 +412,48 @@ def test_progress_bar_play_parallel(self): # As explained there: https://github.com/Axelrod-Python/Axelrod/issues/465, # these two examples were identified by hypothesis. @example( - tournament=axelrod.Tournament( - players=[axelrod.BackStabber(), axelrod.MindReader()], + tournament=axl.Tournament( + players=[axl.BackStabber(), axl.MindReader()], turns=2, repetitions=1, ) ) @example( - tournament=axelrod.Tournament( - players=[axelrod.BackStabber(), axelrod.ThueMorse()], turns=2, repetitions=1 + tournament=axl.Tournament( + players=[axl.BackStabber(), axl.ThueMorse()], turns=2, repetitions=1 ) ) def test_property_serial_play(self, tournament): """Test serial play using hypothesis""" # Test that we get an instance of ResultSet results = tournament.play(progress_bar=False) - self.assertIsInstance(results, axelrod.ResultSet) + self.assertIsInstance(results, axl.ResultSet) self.assertEqual(results.num_players, len(tournament.players)) self.assertEqual(results.players, [str(p) for p in tournament.players]) def test_parallel_play(self): # Test that we get an instance of ResultSet - tournament = axelrod.Tournament( + tournament = axl.Tournament( name=self.test_name, players=self.players, game=self.game, - turns=axelrod.DEFAULT_TURNS, + turns=axl.DEFAULT_TURNS, repetitions=self.test_repetitions, ) results = tournament.play(processes=2, progress_bar=False) - self.assertIsInstance(results, axelrod.ResultSet) + self.assertIsInstance(results, axl.ResultSet) self.assertEqual(tournament.num_interactions, 75) # The following relates to #516 players = [ - axelrod.Cooperator(), - axelrod.Defector(), - axelrod.BackStabber(), - axelrod.PSOGambler2_2_2(), - axelrod.ThueMorse(), - axelrod.DoubleCrosser(), + axl.Cooperator(), + axl.Defector(), + axl.BackStabber(), + axl.PSOGambler2_2_2(), + axl.ThueMorse(), + axl.DoubleCrosser(), ] - tournament = axelrod.Tournament( + tournament = axl.Tournament( name=self.test_name, players=players, game=self.game, @@ -464,26 +464,26 @@ def test_parallel_play(self): self.assertEqual(len(scores), len(players)) def test_parallel_play_with_writing_to_file(self): - tournament = axelrod.Tournament( + tournament = axl.Tournament( name=self.test_name, players=self.players, game=self.game, - turns=axelrod.DEFAULT_TURNS, + turns=axl.DEFAULT_TURNS, repetitions=self.test_repetitions, ) results = tournament.play( processes=2, progress_bar=False, filename=self.filename ) - self.assertIsInstance(results, axelrod.ResultSet) + self.assertIsInstance(results, axl.ResultSet) self.assertEqual(tournament.num_interactions, 75) def test_run_serial(self): - tournament = axelrod.Tournament( + tournament = axl.Tournament( name=self.test_name, players=self.players, game=self.game, - turns=axelrod.DEFAULT_TURNS, + turns=axl.DEFAULT_TURNS, repetitions=self.test_repetitions, ) tournament._write_interactions_to_file = MagicMock( @@ -500,11 +500,11 @@ class PickleableMock(MagicMock): def __reduce__(self): return MagicMock, () - tournament = axelrod.Tournament( + tournament = axl.Tournament( name=self.test_name, players=self.players, game=self.game, - turns=axelrod.DEFAULT_TURNS, + turns=axl.DEFAULT_TURNS, repetitions=self.test_repetitions, ) tournament._write_interactions_to_file = PickleableMock( @@ -527,20 +527,20 @@ def __reduce__(self): def test_n_workers(self): max_processes = cpu_count() - tournament = axelrod.Tournament( + tournament = axl.Tournament( name=self.test_name, players=self.players, game=self.game, - turns=axelrod.DEFAULT_TURNS, + turns=axl.DEFAULT_TURNS, repetitions=self.test_repetitions, ) self.assertEqual(tournament._n_workers(processes=1), max_processes) - tournament = axelrod.Tournament( + tournament = axl.Tournament( name=self.test_name, players=self.players, game=self.game, - turns=axelrod.DEFAULT_TURNS, + turns=axl.DEFAULT_TURNS, repetitions=self.test_repetitions, ) self.assertEqual( @@ -552,11 +552,11 @@ def test_2_workers(self): # This is a separate test with a skip condition because we # cannot guarantee that the tests will always run on a machine # with more than one processor - tournament = axelrod.Tournament( + tournament = axl.Tournament( name=self.test_name, players=self.players, game=self.game, - turns=axelrod.DEFAULT_TURNS, + turns=axl.DEFAULT_TURNS, repetitions=self.test_repetitions, ) self.assertEqual(tournament._n_workers(processes=2), 2) @@ -565,11 +565,11 @@ def test_start_workers(self): workers = 2 work_queue = Queue() done_queue = Queue() - tournament = axelrod.Tournament( + tournament = axl.Tournament( name=self.test_name, players=self.players, game=self.game, - turns=axelrod.DEFAULT_TURNS, + turns=axl.DEFAULT_TURNS, repetitions=self.test_repetitions, ) chunks = tournament.match_generator.build_match_chunks() @@ -585,11 +585,11 @@ def test_start_workers(self): self.assertEqual(stops, workers) def test_worker(self): - tournament = axelrod.Tournament( + tournament = axl.Tournament( name=self.test_name, players=self.players, game=self.game, - turns=axelrod.DEFAULT_TURNS, + turns=axl.DEFAULT_TURNS, repetitions=self.test_repetitions, ) @@ -612,22 +612,22 @@ def test_worker(self): self.assertEqual(queue_stop, "STOP") def test_build_result_set(self): - tournament = axelrod.Tournament( + tournament = axl.Tournament( name=self.test_name, players=self.players, game=self.game, - turns=axelrod.DEFAULT_TURNS, + turns=axl.DEFAULT_TURNS, repetitions=self.test_repetitions, ) results = tournament.play(progress_bar=False) - self.assertIsInstance(results, axelrod.ResultSet) + self.assertIsInstance(results, axl.ResultSet) def test_no_build_result_set(self): - tournament = axelrod.Tournament( + tournament = axl.Tournament( name=self.test_name, players=self.players, game=self.game, - turns=axelrod.DEFAULT_TURNS, + turns=axl.DEFAULT_TURNS, repetitions=self.test_repetitions, ) @@ -646,9 +646,9 @@ def test_no_build_result_set(self): @given(turns=integers(min_value=1, max_value=200)) @settings(max_examples=5) @example(turns=3) - @example(turns=axelrod.DEFAULT_TURNS) + @example(turns=axl.DEFAULT_TURNS) def test_play_matches(self, turns): - tournament = axelrod.Tournament( + tournament = axl.Tournament( name=self.test_name, players=self.players, game=self.game, @@ -692,17 +692,17 @@ def test_match_cache_is_used(self): Create two Random players that are classified as deterministic. As they are deterministic the cache will be used. """ - FakeRandom = axelrod.Random + FakeRandom = axl.Random FakeRandom.classifier["stochastic"] = False p1 = FakeRandom() p2 = FakeRandom() - tournament = axelrod.Tournament((p1, p2), turns=5, repetitions=2) + tournament = axl.Tournament((p1, p2), turns=5, repetitions=2) results = tournament.play(progress_bar=False) for player_scores in results.scores: self.assertEqual(player_scores[0], player_scores[1]) def test_write_interactions(self): - tournament = axelrod.Tournament( + tournament = axl.Tournament( name=self.test_name, players=self.players, game=self.game, @@ -724,7 +724,7 @@ def test_write_interactions(self): self.assertEqual(len(calls), 15) def test_write_to_csv_with_results(self): - tournament = axelrod.Tournament( + tournament = axl.Tournament( name=self.test_name, players=self.players, game=self.game, @@ -737,7 +737,7 @@ def test_write_to_csv_with_results(self): self.assertTrue(df.equals(expected_df)) def test_write_to_csv_without_results(self): - tournament = axelrod.Tournament( + tournament = axl.Tournament( name=self.test_name, players=self.players, game=self.game, @@ -755,14 +755,14 @@ def test_write_to_csv_without_results(self): class TestProbEndTournament(unittest.TestCase): @classmethod def setUpClass(cls): - cls.game = axelrod.Game() + cls.game = axl.Game() cls.players = [s() for s in test_strategies] cls.test_name = "test" cls.test_repetitions = test_repetitions cls.test_prob_end = test_prob_end def test_init(self): - tournament = axelrod.Tournament( + tournament = axl.Tournament( name=self.test_name, players=self.players, game=self.game, @@ -777,7 +777,7 @@ def test_init(self): self.assertEqual(tournament.name, "test") self.assertIsInstance(tournament._logger, logging.Logger) self.assertEqual(tournament.noise, 0.2) - anonymous_tournament = axelrod.Tournament(players=self.players) + anonymous_tournament = axl.Tournament(players=self.players) self.assertEqual(anonymous_tournament.name, "axelrod") @given( @@ -792,7 +792,7 @@ def test_init(self): ) @settings(max_examples=5) @example( - tournament=axelrod.Tournament( + tournament=axl.Tournament( players=[s() for s in test_strategies], prob_end=0.2, repetitions=test_repetitions, @@ -803,15 +803,15 @@ def test_init(self): # As explained there: https://github.com/Axelrod-Python/Axelrod/issues/465, # these two examples were identified by hypothesis. @example( - tournament=axelrod.Tournament( - players=[axelrod.BackStabber(), axelrod.MindReader()], + tournament=axl.Tournament( + players=[axl.BackStabber(), axl.MindReader()], prob_end=0.2, repetitions=1, ) ) @example( - tournament=axelrod.Tournament( - players=[axelrod.ThueMorse(), axelrod.MindReader()], + tournament=axl.Tournament( + players=[axl.ThueMorse(), axl.MindReader()], prob_end=0.2, repetitions=1, ) @@ -820,7 +820,7 @@ def test_property_serial_play(self, tournament): """Test serial play using hypothesis""" # Test that we get an instance of ResultSet results = tournament.play(progress_bar=False) - self.assertIsInstance(results, axelrod.ResultSet) + self.assertIsInstance(results, axl.ResultSet) self.assertEqual(results.num_players, len(tournament.players)) self.assertEqual(results.players, [str(p) for p in tournament.players]) @@ -828,7 +828,7 @@ def test_property_serial_play(self, tournament): class TestSpatialTournament(unittest.TestCase): @classmethod def setUpClass(cls): - cls.game = axelrod.Game() + cls.game = axl.Game() cls.players = [s() for s in test_strategies] cls.test_name = "test" cls.test_repetitions = test_repetitions @@ -836,7 +836,7 @@ def setUpClass(cls): cls.test_edges = test_edges def test_init(self): - tournament = axelrod.Tournament( + tournament = axl.Tournament( name=self.test_name, players=self.players, game=self.game, @@ -853,7 +853,7 @@ def test_init(self): self.assertIsInstance(tournament._logger, logging.Logger) self.assertEqual(tournament.noise, 0.2) self.assertEqual(tournament.match_generator.noise, 0.2) - anonymous_tournament = axelrod.Tournament(players=self.players) + anonymous_tournament = axl.Tournament(players=self.players) self.assertEqual(anonymous_tournament.name, "axelrod") @given( @@ -880,17 +880,17 @@ def test_complete_tournament(self, strategies, turns, repetitions, noise, seed): edges.append((i, j)) # create a round robin tournament - tournament = axelrod.Tournament( + tournament = axl.Tournament( players, repetitions=repetitions, turns=turns, noise=noise ) # create a complete spatial tournament - spatial_tournament = axelrod.Tournament( + spatial_tournament = axl.Tournament( players, repetitions=repetitions, turns=turns, noise=noise, edges=edges ) - axelrod.seed(seed) + axl.seed(seed) results = tournament.play(progress_bar=False) - axelrod.seed(seed) + axl.seed(seed) spatial_results = spatial_tournament.play(progress_bar=False) self.assertEqual(results.ranked_names, spatial_results.ranked_names) @@ -917,27 +917,27 @@ def test_particular_tournament(self): """A test for a tournament that has caused failures during some bug fixing""" players = [ - axelrod.Cooperator(), - axelrod.Defector(), - axelrod.TitForTat(), - axelrod.Grudger(), + axl.Cooperator(), + axl.Defector(), + axl.TitForTat(), + axl.Grudger(), ] edges = [(0, 2), (0, 3), (1, 2), (1, 3)] - tournament = axelrod.Tournament(players, edges=edges) + tournament = axl.Tournament(players, edges=edges) results = tournament.play(progress_bar=False) expected_ranked_names = ["Cooperator", "Tit For Tat", "Grudger", "Defector"] self.assertEqual(results.ranked_names, expected_ranked_names) # Check that this tournament runs with noise - tournament = axelrod.Tournament(players, edges=edges, noise=0.5) + tournament = axl.Tournament(players, edges=edges, noise=0.5) results = tournament.play(progress_bar=False) - self.assertIsInstance(results, axelrod.ResultSet) + self.assertIsInstance(results, axl.ResultSet) class TestProbEndingSpatialTournament(unittest.TestCase): @classmethod def setUpClass(cls): - cls.game = axelrod.Game() + cls.game = axl.Game() cls.players = [s() for s in test_strategies] cls.test_name = "test" cls.test_repetitions = test_repetitions @@ -945,7 +945,7 @@ def setUpClass(cls): cls.test_edges = test_edges def test_init(self): - tournament = axelrod.Tournament( + tournament = axl.Tournament( name=self.test_name, players=self.players, game=self.game, @@ -981,18 +981,18 @@ def test_complete_tournament(self, strategies, prob_end, seed, reps): players = [s() for s in strategies] # create a prob end round robin tournament - tournament = axelrod.Tournament(players, prob_end=prob_end, repetitions=reps) - axelrod.seed(seed) + tournament = axl.Tournament(players, prob_end=prob_end, repetitions=reps) + axl.seed(seed) results = tournament.play(progress_bar=False) # create a complete spatial tournament # edges edges = [(i, j) for i in range(len(players)) for j in range(i, len(players))] - spatial_tournament = axelrod.Tournament( + spatial_tournament = axl.Tournament( players, prob_end=prob_end, repetitions=reps, edges=edges ) - axelrod.seed(seed) + axl.seed(seed) spatial_results = spatial_tournament.play(progress_bar=False) self.assertEqual(results.match_lengths, spatial_results.match_lengths) self.assertEqual(results.ranked_names, spatial_results.ranked_names) @@ -1002,7 +1002,7 @@ def test_complete_tournament(self, strategies, prob_end, seed, reps): @given( tournament=spatial_tournaments( - strategies=axelrod.basic_strategies, + strategies=axl.basic_strategies, max_turns=1, max_noise=0, max_repetitions=3, @@ -1015,15 +1015,15 @@ def test_one_turn_tournament(self, tournament, seed): Tests that gives same result as the corresponding spatial round robin spatial tournament """ - prob_end_tour = axelrod.Tournament( + prob_end_tour = axl.Tournament( tournament.players, prob_end=1, edges=tournament.edges, repetitions=tournament.repetitions, ) - axelrod.seed(seed) + axl.seed(seed) prob_end_results = prob_end_tour.play(progress_bar=False) - axelrod.seed(seed) + axl.seed(seed) one_turn_results = tournament.play(progress_bar=False) self.assertEqual(prob_end_results.scores, one_turn_results.scores) self.assertEqual(prob_end_results.wins, one_turn_results.wins) diff --git a/axelrod/tests/unit/test_version.py b/axelrod/tests/unit/test_version.py index ef10a9d2d..2f2021bcd 100644 --- a/axelrod/tests/unit/test_version.py +++ b/axelrod/tests/unit/test_version.py @@ -1,9 +1,10 @@ """Tests the version number.""" + import unittest -from axelrod import __version__ +import axelrod as axl class TestVersion(unittest.TestCase): def test_version(self): - self.assertIsInstance(__version__, str) + self.assertIsInstance(axl.__version__, str)