def test_play(self): """ Test that play method looks for attribute strategy (which does not exist) """ P1, P2 = axelrod.Player(), axelrod.Player() self.assertEquals(P1.play(P2), None) self.assertEquals(P2.play(P1), None)
def test_calculate_cooperation(self): p1, p2 = axelrod.Player(), axelrod.Player() p1.history = [C, C, D, D] rr = axelrod.RoundRobin(players=[p1, p2], game=self.game, turns=20) result = rr._calculate_cooperation(p1) expected = 2 self.assertEqual(result, expected)
def test_calculate_score_for_all_defect(self): """Test that scores are calculated correctly.""" P1 = axelrod.Player() P1.history = ['D', 'D', 'D'] P2 = axelrod.Player() P2.history = ['D', 'D', 'D'] round_robin = axelrod.RoundRobin(players=[P1, P2], game=self.game, turns=200) self.assertEqual(round_robin.calculate_scores(P1, P2), (3, 3))
def test_calculate_scores(self): p1, p2 = axelrod.Player(), axelrod.Player() p1.history = [C, C, D, D] p2.history = [C, D, C, D] rr = axelrod.RoundRobin(players=[p1, p2], game=self.game, turns=20) result = rr._calculate_scores(p1, p2) expected = (9, 9) self.assertEqual(result, expected)
def test_calculate_score_for_all_cooperate(self): """Test that scores are calculated correctly.""" P1 = axelrod.Player() P1.history = [C, C, C] P2 = axelrod.Player() P2.history = [C, C, C] round_robin = axelrod.RoundRobin(players=[P1, P2], game=self.game, turns=200) self.assertEqual(round_robin._calculate_scores(P1, P2), (9, 9))
def test_calculate_score_for_all_cooperate(self): """ Test that scores are calculated correctly """ P1 = axelrod.Player() P1.history = ['C', 'C', 'C'] P2 = axelrod.Player() P2.history = ['C', 'C', 'C'] tournament = axelrod.Axelrod(P1, P2) self.assertEqual(tournament.calculate_scores(P1, P2), (6, 6))
def test_calculate_score_for_all_defect(self): """ Test that scores are calculated correctly """ P1 = axelrod.Player() P1.history = ['D', 'D', 'D'] P2 = axelrod.Player() P2.history = ['D', 'D', 'D'] tournament = axelrod.Axelrod(P1, P2) self.assertEqual(tournament.calculate_scores(P1, P2), (12, 12))
def test_strategy(self): for action in [C, D]: m = MockPlayer(actions=[action]) p2 = axelrod.Player() self.assertEqual(action, m.strategy(p2)) actions = [C, C, D, D, C, C] m = MockPlayer(actions=actions) p2 = axelrod.Player() for action in actions: self.assertEqual(action, m.strategy(p2))
def test_cache_update_required(self): p1, p2 = axelrod.Player(), axelrod.Player() rr = axelrod.RoundRobin(players=[p1, p2], game=self.game, turns=20, noise=0.2) self.assertFalse(rr._cache_update_required(p1, p2)) rr = axelrod.RoundRobin(players=[p1, p2], game=self.game, turns=20) self.assertTrue(rr._cache_update_required(p1, p2)) p1 = axelrod.Random() rr = axelrod.RoundRobin(players=[p1, p2], game=self.game, turns=20) self.assertFalse(rr._cache_update_required(p1, p2))
def test_stochastic_interaction(self): p1, p2 = axelrod.Player(), axelrod.Player() rr = axelrod.RoundRobin(players=[p1, p2], game=self.game, turns=20, noise=0.2) self.assertTrue(rr._stochastic_interaction(p1, p2)) rr = axelrod.RoundRobin(players=[p1, p2], game=self.game, turns=20) self.assertFalse(rr._stochastic_interaction(p1, p2)) p1 = axelrod.Random() rr = axelrod.RoundRobin(players=[p1, p2], game=self.game, turns=20) self.assertTrue(rr._stochastic_interaction(p1, p2))
def test_init(self): p1, p2 = axelrod.Player(), axelrod.Player() rr = axelrod.RoundRobin(players=[p1, p2], game=self.game, turns=20, noise=0.2) self.assertEqual(rr.players, [p1, p2]) self.assertEqual(rr.nplayers, 2) self.assertEqual(rr.game.score((C, C)), (3, 3)) self.assertEqual(rr.turns, 20) self.assertEqual(rr.deterministic_cache, {}) self.assertEqual(rr.cache_mutable, True) self.assertEqual(rr._noise, 0.2)
def test_strategy(self): P1 = axl.NiceMetaWinner(team=[axl.Cooperator, axl.Defector]) P2 = axl.Player() # This meta player will simply choose the strategy with the highest # current score. P1.team[0].score = 0 P1.team[1].score = 1 self.assertEqual(P1.strategy(P2), C) P1.team[0].score = 1 P1.team[1].score = 0 self.assertEqual(P1.strategy(P2), C) # If there is a tie, choose to cooperate if possible. P1.team[0].score = 1 P1.team[1].score = 1 self.assertEqual(P1.strategy(P2), C) opponent = axl.Cooperator() player = axl.NiceMetaWinner(team=[axl.Cooperator, axl.Defector]) match = axl.Match((player, opponent), turns=5) match.play() self.assertEqual(player.history[-1], C) opponent = axl.Defector() player = axl.NiceMetaWinner(team=[axl.Defector]) match = axl.Match((player, opponent), turns=20) match.play() self.assertEqual(player.history[-1], D) opponent = axl.Defector() player = axl.MetaWinner(team=[axl.Cooperator, axl.Defector]) match = axl.Match((player, opponent), turns=20) match.play() self.assertEqual(player.history[-1], D)
def test_strategy(self): # Testing the expected value is difficult here so these just ensure that # future changes that break these tests will be examined carefully. P1 = self.player() P2 = axelrod.Player() self.assertEqual(P1.strategy(P2), 'C') P1.history = ['C'] P2.history = ['C'] random.seed(2) self.assertEqual(P1.strategy(P2), 'C') self.assertEqual(P1.strategy(P2), 'C') self.assertEqual(P1.strategy(P2), 'C') self.assertEqual(P1.strategy(P2), 'C') P1.history = ['C'] P2.history = ['D'] self.assertEqual(P1.strategy(P2), 'D') self.assertEqual(P1.strategy(P2), 'D') self.assertEqual(P1.strategy(P2), 'D') self.assertEqual(P1.strategy(P2), 'C') P1.history = ['D'] P2.history = ['C'] self.assertEqual(P1.strategy(P2), 'C') self.assertEqual(P1.strategy(P2), 'C') self.assertEqual(P1.strategy(P2), 'C') self.assertEqual(P1.strategy(P2), 'C') P1.history = ['D'] P2.history = ['D'] self.assertEqual(P1.strategy(P2), 'D') self.assertEqual(P1.strategy(P2), 'D') self.assertEqual(P1.strategy(P2), 'D') self.assertEqual(P1.strategy(P2), 'D')
def test_effect_of_strategy(self): """If opponent has defected more than 10 percent of the time, defect.""" P1 = axelrod.Retaliate() P2 = axelrod.Player() self.responses_test([C] * 4, [C] * 4, [C]) self.responses_test([C, C, C, C, D], [C, C, C, D, C], [D]) self.responses_test([C] * 6, [C] * 5 + [D], [D])
def test_strategy(self): P1 = axelrod.MathConstantHunter() P2 = axelrod.Player() P1.history = ['C'] * 8 P2.history = ['C', 'C', 'C', 'D', 'C', 'C', 'C', 'D'] self.assertEqual(P1.strategy(P2), 'D')
def test_initial_strategy(self): """ Starts by cooperating """ P1 = axelrod.Alternator() P2 = axelrod.Player() self.assertEqual(P1.strategy(P2), 'C')
def test_initial_strategy(self): """ Starts by cooperating """ P1 = axelrod.ForgivingTitForTat() P2 = axelrod.Player() self.assertEqual(P1.strategy(P2), 'C')
def test_when_less_than_pi(self): """tests that if the ratio of Cs to Ds is less than pi then strategy co-operates""" P1 = axelrod.Pi() P2 = axelrod.Player() P1.history = ['C', 'C', 'C', 'C'] P2.history = ['C', 'C', 'D', 'D'] self.assertEqual(P1.strategy(P2), 'C')
def test_when_no_defection(self): """tests that if the opposing player does not defect initially then strategy defects""" P1 = axelrod.e() P2 = axelrod.Player() P1.history = ['C'] P2.history = ['C'] self.assertEqual(P1.strategy(P2), 'D')
def test_strategy(self): P1 = axelrod.NiceMetaWinner(team=[axelrod.Cooperator, axelrod.Defector]) P2 = axelrod.Player() # This meta player will simply choose the strategy with the highest # current score. P1.team[0].score = 0 P1.team[1].score = 1 self.assertEqual(P1.strategy(P2), C) P1.team[0].score = 1 P1.team[1].score = 0 self.assertEqual(P1.strategy(P2), C) # If there is a tie, choose to cooperate if possible. P1.team[0].score = 1 P1.team[1].score = 1 self.assertEqual(P1.strategy(P2), C) opponent = axelrod.Cooperator() player = axelrod.NiceMetaWinner(team=[axelrod.Cooperator, axelrod.Defector]) for _ in range(5): player.play(opponent) self.assertEqual(player.history[-1], C) opponent = axelrod.Defector() player = axelrod.NiceMetaWinner(team=[axelrod.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]) for _ in range(20): player.play(opponent) self.assertEqual(player.history[-1], D)
def test_when_greater_than_pi(self): """tests that if the ratio of Cs to Ds is greater than pi then strategy defects""" P1 = axelrod.Pi() P2 = axelrod.Player() P1.history = ['C', 'C', 'C', 'C'] P2.history = ['C', 'C', 'C', 'D'] self.assertEqual(P1.strategy(P2), 'D')
def test_strategy(self): self.first_play_test(C) P1 = axelrod.Calculator() P1.history = [C] * 20 P2 = axelrod.Player() P2.history = [C, D] * 10 # Defects on cycle detection self.assertEqual(D, P1.strategy(P2)) # Test non-cycle response history = [C, C, D, C, C, D, C, C, C, D, C, C, C, C, D, C, C, C, C, C] P2.history = history self.assertEqual(C, P1.strategy(P2)) # Test post 20 rounds responses self.responses_test([D], [C] * 21, [C] * 21) history = [ C, C, D, C, C, D, C, C, C, D, C, C, C, C, D, C, C, C, C, C, D ] self.responses_test([D], [C] * 21, history) history = [ C, C, D, C, C, D, C, C, C, D, C, C, C, C, D, C, C, C, C, C, D, C ] self.responses_test([C], [C] * 22, history)
def test_initial_strategy(self): """ Starts by cooperating """ P1 = axelrod.OppositeGrudger() P2 = axelrod.Player() self.assertEqual(P1.strategy(P2), 'D')
def test_strategy(self): """ Starts by cooperating """ P1 = axelrod.TwoTitsForTat() P2 = axelrod.Player() self.assertEqual(P1.strategy(P2), 'C')
def test_initial_nice_strategy(self): """ Starts by cooperating """ P1 = axelrod.Grumpy() P2 = axelrod.Player() self.assertEqual(P1.strategy(P2), 'C')
def test_initial_grumpy_strategy(self): """ Starts by defecting if grumpy """ P1 = axelrod.Grumpy(starting_state='Grumpy') P2 = axelrod.Player() self.assertEqual(P1.strategy(P2), D)
def test_strategy(self): """Test if it is trying to trick opponent.""" P1 = axelrod.TrickyDefector() P2 = axelrod.Player() self.assertEqual(P1.strategy(P2), 'D') P1.history = ['D', 'D', 'D', 'D'] P2.history = ['C', 'D', 'D', 'D']
def test_when_less_than_golder_ratio(self): """tests that if the ratio of Cs to Ds is less than the golden ratio then strategy co-operates""" P1 = axelrod.Golden() P2 = axelrod.Player() P1.history = ['C', 'C', 'C', 'C'] P2.history = ['D', 'D', 'D', 'D'] self.assertEqual(P1.strategy(P2), 'C')
def test_strategy(self): """If opponent has defected more than 10 percent of the time, defect.""" P1 = axelrod.ForgivingTitForTat() P2 = axelrod.Player() P1.history = ['C', 'C', 'C', 'C'] P2.history = ['C', 'C', 'C', 'C'] self.assertEqual(P1.strategy(P2), 'C') P1.history = [ 'C', 'C', 'C', 'C', 'D', ] P2.history = [ 'C', 'C', 'C', 'D', 'C', ] self.assertEqual(P1.strategy(P2), 'C') P1.history = ['C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C'] P2.history = ['C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'D'] self.assertEqual(P1.strategy(P2), 'C') P1.history = ['C', 'C', 'C', 'C'] P2.history = ['C', 'C', 'C', 'D'] self.assertEqual(P1.strategy(P2), 'D')
def test_various(self): p1 = axelrod.Player() p2 = axelrod.Player() self.assertEqual(simulate_play(p1, p2), (C, C)) self.assertEqual(p1.cooperations, 1) self.assertEqual(p2.cooperations, 1) self.assertEqual(p1.defections, 0) self.assertEqual(p2.defections, 0) for h1 in [C, D]: for h2 in [C, D]: self.assertEqual(simulate_play(p1, p2, h1, h2), (h1, h2)) self.assertEqual(p1.cooperations, 3) self.assertEqual(p2.cooperations, 3) self.assertEqual(p1.defections, 2) self.assertEqual(p2.defections, 2)