def test_memory_depth_is_len_cycle_minus_one(self): len_ten = "DCDCDDCDCD" len_five = "DCDDC" depth_nine = axl.Cycler(cycle=len_ten) depth_four = axl.Cycler(cycle=len_five) self.assertEqual(axl.Classifiers["memory_depth"](depth_nine), 9) self.assertEqual(axl.Classifiers["memory_depth"](depth_four), 4)
def test_jossann_transformer(self): """Tests the JossAnn transformer. """ probability = (1, 0) p1 = JossAnnTransformer(probability)(axl.Defector)() self.assertFalse(axl.Classifiers["stochastic"](p1)) 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)(axl.Cooperator)() self.assertFalse(axl.Classifiers["stochastic"](p1)) 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)(axl.TitForTat)() self.assertTrue(axl.Classifiers["stochastic"](p1)) 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)(axl.Cooperator)() self.assertTrue(axl.Classifiers["stochastic"](p1)) 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)(axl.Random) self.assertFalse(axl.Classifiers["stochastic"](p1())) probability = (1, 0) p1 = JossAnnTransformer(probability)(axl.Random) self.assertFalse(axl.Classifiers["stochastic"](p1())) probability = (0.5, 0.5) p1 = JossAnnTransformer(probability)(axl.TitForTat) self.assertTrue(axl.Classifiers["stochastic"](p1())) probability = (0, 0.5) p1 = JossAnnTransformer(probability)(axl.TitForTat) self.assertTrue(axl.Classifiers["stochastic"](p1())) probability = (0, 0) p1 = JossAnnTransformer(probability)(axl.TitForTat) self.assertFalse(axl.Classifiers["stochastic"](p1())) probability = (0, 0) p1 = JossAnnTransformer(probability)(axl.Random) self.assertTrue(axl.Classifiers["stochastic"](p1()))
def test_jossann_transformer(self): """Tests the JossAnn transformer. """ probability = (1, 0) p1 = JossAnnTransformer(probability)(axelrod.Defector)() p2 = axelrod.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)() p2 = axelrod.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)() p2 = axelrod.Cycler() axelrod.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)() p2 = axelrod.Cooperator() for _ in range(5): p1.play(p2) self.assertEqual(p1.history, [D, C, D, D, C])
def test_strategy_PavlovD(self): """Tests that PavolvD is identified by DDCDDC.""" opponent = axl.Cycler(cycle="DDC") 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"} )
def player(self): """ Create and return a Cycler player with the sequence that has been generated with this run. Returns ------- Cycler(sequence) """ return axl.Cycler(self.get_sequence_str())
def test_implementation(self): """A test that demonstrates the difference in outcomes if FlipTransformer is applied to Alternator and CyclerCD. In other words, the implementation matters, not just the outcomes.""" p1 = axl.Cycler(cycle="CD") p2 = FlipTransformer()(axl.Cycler)(cycle="CD") self.versus_test(p1, p2, [C, D, C, D, C], [D, C, D, C, D]) p1 = axl.Alternator() p2 = FlipTransformer()(axl.Alternator)() self.versus_test(p1, p2, [C, D, C, D, C], [D, D, D, D, D])
def get_fitness_of_individual(sequence, opponent, seed, index, turns): """ Returns the score of a sequence against a given opponent and the index of the sequence in the population. """ if seed is not np.NaN: axl.seed(seed) opponent = opponent() player = axl.Cycler(get_sequence_str(sequence)) match = axl.Match([opponent, player], turns=turns) match.play() return index, match.final_score_per_turn()[-1]
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=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=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=axl.SuspiciousTitForTat(), expected_actions=actions)
def test_dual_majority_transformer(self): """Tests that DualTransformer produces the opposite results when faced with the same opponent history. """ p1 = axelrod.GoByMajority() p2 = DualTransformer()(axelrod.GoByMajority)() p3 = axelrod.Cycler('CDD') # Cycles 'CDD' for _ in range(10): p1.play(p3) p3.reset() for _ in range(10): p2.play(p3) self.assertEqual(p1.history, [flip_action(x) for x in p2.history])
def test_mutation_method_exceptions(self): axelrod.seed(10) cycle_length = 5 players = [axelrod.EvolvableCycler(cycle_length=cycle_length) for _ in range(5)] with self.assertRaises(ValueError): MoranProcess(players, turns=10, mutation_method="random") axelrod.seed(0) players = [axelrod.Cycler(cycle="CD" * random.randint(2, 10)) for _ in range(10)] mp = MoranProcess(players, turns=10, mutation_method="atomic") with self.assertRaises(TypeError): for _ in range(10): next(mp)
def test_implementation(self): """A test that demonstrates the difference in outcomes if 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") 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)() 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])
def test_strategy(self): # Cooperator Test opponent = axelrod.Cooperator() actions = [(C, C), (C, C), (C, C), (C, C)] self.versus_test(opponent, expected_actions=actions) # Defector Test opponent = axelrod.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]) actions = [(C, D), (D, C), (C, D), (D, C), (D, D), (D, C), (D, D), (D, C), (C, D), (D, C)] self.versus_test(opponent, expected_actions=actions, attrs={'is_retaliating': True, 'retaliation_length': 4, 'retaliation_remaining': 3}) opponent = axelrod.Cycler('DDCDD') actions = [(C, D), (D, D), (D, C), (D, D), (D, D), (D, D), (D, D), (D, C), (D, D), (D, D), (D, D), (D, D), (D, C), (D, D), (D, D), (D, D), (D, D), (D, C), (D, D), (D, D)] self.versus_test(opponent, expected_actions=actions, attrs={'current_score': 34, 'opponent_score': 19, 'last_fresh_start': 0, 'retaliation_length': 6, 'retaliation_remaining': 2}) # When the length is given this strategy will not give a fresh start opponent = axelrod.Cycler('DDCDD') actions = [(C, D), (D, D), (D, C), (D, D), (D, D), (D, D), (D, D), (D, C), (D, D), (D, D), (D, D), (D, D), (D, C), (D, D), (D, D), (D, D), (D, D), (D, C), (D, D), (D, D), (D, D), (D, D), (D, C), (D, D), (D, D), (D, D), (D, D), (D, C), (C, D), (C, D)] self.versus_test(opponent, expected_actions=actions, match_attributes={'length': 50}) # When the length is not given this strategy will give a fresh start. opponent = axelrod.Cycler('DDCDD') actions = [(C, D), (D, D), (D, C), (D, D), (D, D), (D, D), (D, D), (D, C), (D, D), (D, D), (D, D), (D, D), (D, C), (D, D), (D, D), (D, D), (D, D), (D, C), (D, D), (D, D), (D, D), (D, D), (D, C), (D, D), (D, D), (D, D), (D, D), (D, C), (C, D), (C, D)] self.versus_test(opponent, expected_actions=actions, match_attributes={'length': float('inf')}) # 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') actions = [(C, D), (D, D), (D, C), (D, D), (D, D), (D, D), (D, D), (D, C), (D, D), (D, D), (D, C), (C, D), (D, C), (D, C), (D, C), (D, D), (D, D), (D, C), (D, D), (D, D), (D, D), (D, D), (D, C), (C, D), (D, D)] self.versus_test(opponent, expected_actions=actions, attrs={'last_fresh_start': 0}) # Check the fresh start condition opponent = axelrod.TitForTat() actions = [(C, C), (C, C), (C, C), (C, C)] 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') actions = [(C, C), (C, C), (C, C), (C, C), (C, D), (D, C), (C, C), (C, C), (C, C), (C, D), (D, C), (D, C), (C, C), (C, C), (C, D), (D, C), (D, C), (D, C), (C, C), (C, D), (D, C), (D, C), (D, C), (C, C), (C, D), (D, C), (C, C), (C, C), (C, C), (C, D), (D, C), (D, C), (C, C), (C, C), (C, D)] self.versus_test(opponent, expected_actions=actions, match_attributes={'length': 35}, attrs={'current_score': 108, 'opponent_score': 78, 'last_fresh_start': 24, 'retaliation_length': 2, 'retaliation_remaining': 0})
def test_parameterized_player(self): player = axl.Cycler("DDCCDD") self.assert_orignal_equals_pickled(player)
def test_stochastic1(self): probability = (0.3, 0.3) p1 = JossAnnTransformer(probability)(axl.TitForTat)() self.assertTrue(axl.Classifiers["stochastic"](p1)) p2 = axl.Cycler() self.versus_test(p1, p2, [D, C, C, D, D], [C, C, D, C, C], seed=18)
def test_strategy(self): # Cooperator Test opponent = axelrod.Cooperator() actions = [(C, C), (C, C), (C, C), (C, C)] self.versus_test(opponent, expected_actions=actions) # Defector Test opponent = axelrod.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]) actions = [ (C, D), (D, C), (C, D), (D, C), (D, D), (D, C), (D, D), (D, C), (C, D), (D, C), ] self.versus_test( opponent, expected_actions=actions, attrs={ "is_retaliating": True, "retaliation_length": 4, "retaliation_remaining": 3, }, ) opponent = axelrod.Cycler("DDCDD") actions = [ (C, D), (D, D), (D, C), (D, D), (D, D), (D, D), (D, D), (D, C), (D, D), (D, D), (D, D), (D, D), (D, C), (D, D), (D, D), (D, D), (D, D), (D, C), (D, D), (D, D), ] self.versus_test( opponent, expected_actions=actions, attrs={ "current_score": 34, "opponent_score": 19, "last_fresh_start": 0, "retaliation_length": 6, "retaliation_remaining": 2, }, ) # When the length is given this strategy will not give a fresh start opponent = axelrod.Cycler("DDCDD") actions = [ (C, D), (D, D), (D, C), (D, D), (D, D), (D, D), (D, D), (D, C), (D, D), (D, D), (D, D), (D, D), (D, C), (D, D), (D, D), (D, D), (D, D), (D, C), (D, D), (D, D), (D, D), (D, D), (D, C), (D, D), (D, D), (D, D), (D, D), (D, C), (C, D), (C, D), ] self.versus_test(opponent, expected_actions=actions, match_attributes={"length": 50}) # When the length is not given this strategy will give a fresh start. opponent = axelrod.Cycler("DDCDD") actions = [ (C, D), (D, D), (D, C), (D, D), (D, D), (D, D), (D, D), (D, C), (D, D), (D, D), (D, D), (D, D), (D, C), (D, D), (D, D), (D, D), (D, D), (D, C), (D, D), (D, D), (D, D), (D, D), (D, C), (D, D), (D, D), (D, D), (D, D), (D, C), (C, D), (C, D), ] self.versus_test( opponent, expected_actions=actions, match_attributes={"length": float("inf")}, ) # 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") actions = [ (C, D), (D, D), (D, C), (D, D), (D, D), (D, D), (D, D), (D, C), (D, D), (D, D), (D, C), (C, D), (D, C), (D, C), (D, C), (D, D), (D, D), (D, C), (D, D), (D, D), (D, D), (D, D), (D, C), (C, D), (D, D), ] self.versus_test(opponent, expected_actions=actions, attrs={"last_fresh_start": 0}) # Check the fresh start condition opponent = axelrod.TitForTat() actions = [(C, C), (C, C), (C, C), (C, C)] 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") actions = [ (C, C), (C, C), (C, C), (C, C), (C, D), (D, C), (C, C), (C, C), (C, C), (C, D), (D, C), (D, C), (C, C), (C, C), (C, D), (D, C), (D, C), (D, C), (C, C), (C, D), (D, C), (D, C), (D, C), (C, C), (C, D), (D, C), (C, C), (C, C), (C, C), (C, D), (D, C), (D, C), (C, C), (C, C), (C, D), ] self.versus_test( opponent, expected_actions=actions, match_attributes={"length": 35}, attrs={ "current_score": 108, "opponent_score": 78, "last_fresh_start": 24, "retaliation_length": 2, "retaliation_remaining": 0, }, )
import axelrod as axl strats = [axl.TitForTat, axl.WinStayLoseShift, axl.AntiTitForTat, axl.Cooperator, axl.Defector, axl.Cycler('CD'), axl.GoByMajority(75)] for s in strats: probe = axl.TitForTat af = axl.AshlockFingerprint(s, probe) data = af.fingerprint(turns=500, repetitions=200, step=0.01, processes=0) p = af.plot() p.savefig('/scratch/c1304586/img/{}-Numerical.pdf'.format(s.name))