def test_probend_matches(): for strategy in all_strategies: for opponent in (Alternator, Cooperator, Defector): players = (Player(strategy), opponent()) match = Match(players, prob_end=0.5, noise=0.5) assert all(action in (C, D) for interaction in match.play() for action in interaction)
def test_matches(): for strategy in all_strategies: for opponent in (Alternator, Cooperator, Defector): players = (Player(strategy), opponent()) match = Match(players, turns=200) assert all(action in (C, D) for interaction in match.play() for action in interaction)
def test_reset(self): player = self.player() opponent = axl.Cooperator() match = Match((player, opponent), turns=100) match.play() self.assertFalse(player.countCC == 0) player.reset() self.assertTrue(player.countCC == 0)
def test_matches_with_different_game(): for strategy in all_strategies: for opponent in (Alternator, Cooperator, Defector): game = Game(r=4, s=0, p=2, t=6) players = (Player(strategy), opponent()) match = Match(players, turns=200, game=game) assert all(action in (C, D) for interaction in match.play() for action in interaction)
def test_warning_for_self_interaction(recwarn): """ Test that a warning is given for a self interaction. """ player = Player("k42r") opponent = player match = Match((player, opponent)) interactions = match.play() assert len(recwarn) == 1
def test_no_warning_for_normal_interaction(recwarn): """ Test that a warning is not given for a normal interaction """ for players in [(Player("k42r"), Alternator()), (Player("k42r"), Player("k41r"))]: match = Match(players) interactions = match.play() assert len(recwarn) == 0
def test_deterministic_strategies(): """ Test that the strategies classified as deterministic indeed act deterministically. """ for strategy in all_strategies: player = Player(strategy) if player.classifier["stochastic"] is False: for opponent in basic_strategies: match = Match((player, opponent())) interactions = match.play() for _ in range(2): # Repeat 3 matches assert interactions == match.play(), player
def matches( draw, strategies=short_run_time_strategies, min_turns=1, max_turns=200, min_noise=0, max_noise=1, ): """ A hypothesis decorator to return a random match. Parameters ---------- strategies : list The strategies from which to sample the two the players min_turns : integer The minimum number of turns max_turns : integer The maximum number of turns min_noise : float The minimum noise max_noise : float The maximum noise Returns ------- match : a random match """ strategies = draw(strategy_lists(min_size=2, max_size=2)) 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) return match
def test_champion_v_alternator(): """ Specific regression test for a bug. See: https://github.com/Axelrod-Python/axelrod-fortran/issues/62 """ player = Player("k61r") opponent = Alternator() seed = 3 match = Match((player, opponent), seed=seed) interactions = match.play() assert interactions[25:30] == [(C, D), (C, C), (C, D), (D, C), (C, D)] match = Match((player, opponent), seed=seed) assert interactions == match.play()
def test_match_reproducibility(): for _ in range(100): rng = RandomGenerator() seed = rng.random_seed_int() strategies = rng.choice(all_strategies, size=2) players1 = [Player(strategy) for strategy in strategies] match1 = Match(players1, turns=200, noise=0.1, seed=seed) results1 = match1.play() players2 = [Player(strategy) for strategy in strategies] match2 = Match(players2, turns=200, noise=0.1, seed=seed) results2 = match2.play() assert (results1 == results2)
def test_strategy(self): player = self.player() # Test against cyclers for opponent in [ axl.CyclerCCD(), axl.CyclerCCCD(), axl.CyclerCCCCCD(), axl.Alternator(), ]: player.reset() match = Match((player, opponent), turns=50) match.play() self.assertEqual(player.history[-1], D) # Test against non-cyclers and cooperators for opponent in [ axl.Random(), axl.AntiCycler(), axl.DoubleCrosser(), axl.Cooperator(), ]: player.reset() match = Match((player, opponent), turns=50, seed=43) match.play() self.assertEqual(player.history[-1], C)
def test_implemented_strategies(): """ Test that the deterministic strategies that are implemented in Axelrod give the same outcomes. """ for strategy, dictionary in characteristics.items(): axelrod_class = dictionary["axelrod-python_class"] stochastic = Player(strategy).classifier["stochastic"] if axelrod_class is not None and not stochastic: for opponent_strategy in basic_strategies: player = Player(strategy) opponent = opponent_strategy() match = Match((player, opponent)) interactions = match.play() axl_player = axelrod_class() opponent = opponent_strategy() axl_match = Match((axl_player, opponent)) axl_interactions = axl_match.play() assert interactions == axl_interactions