def test_flip_transformer(self): """Tests that FlipTransformer(Cooperator) == Defector.""" p1 = axelrod.Cooperator() p2 = FlipTransformer()(axelrod.Cooperator)() # Defector self.assertEqual(simulate_play(p1, p2), (C, D)) self.assertEqual(simulate_play(p1, p2), (C, D)) self.assertEqual(simulate_play(p1, p2), (C, D))
def test_responses(test_class, P1, P2, history_1, history_2, responses, random_seed=None): """ Test responses to arbitrary histories. Used for the the following tests in TestPlayer: first_play_test, markov_test, and responses_test. Works for arbitrary players as well. Input response_lists is a list of lists, each of which consists of a list for the history of player 1, a list for the history of player 2, and a list for the subsequent moves by player one to test. """ if random_seed: random.seed(random_seed) # Force the histories, In case either history is impossible or if some # internal state needs to be set, actually submit to moves to the strategy # method. Still need to append history manually. for h1, h2 in zip(history_1, history_2): simulate_play(P1, P2, h1, h2) # Run the tests for response in responses: s1, s2 = simulate_play(P1, P2) test_class.assertEqual(s1, response)
def test_responses(test_class, player1, player2, responses, history1=None, history2=None, seed=None, attrs=None): """ Test responses to arbitrary histories. Used for the following tests in TestPlayer: first_play_test, second_play_test, and responses_test. Works for arbitrary players as well. Input response_lists is a list of lists, each of which consists of a list for the history of player 1, a list for the history of player 2, and a list for the subsequent moves by player one to test. """ if seed is not None: axelrod.seed(seed) # Force the histories, In case either history is impossible or if some # internal state needs to be set, actually submit to moves to the strategy # method. Still need to append history manually. if history1 and history2: for h1, h2 in zip(history1, history2): s1, s2 = simulate_play(player1, player2, h1, h2) # Run the tests for response in responses: s1, s2 = simulate_play(player1, player2) test_class.assertEqual(s1, response) if attrs: for attr, value in attrs.items(): test_class.assertEqual(getattr(player1, attr), value)
def test_responses(test_class, P1, P2, history_1, history_2, responses, random_seed=None, attrs=None): """ Test responses to arbitrary histories. Used for the following tests in TestPlayer: first_play_test, markov_test, and responses_test. Works for arbitrary players as well. Input response_lists is a list of lists, each of which consists of a list for the history of player 1, a list for the history of player 2, and a list for the subsequent moves by player one to test. """ if random_seed: axelrod.seed(random_seed) # Force the histories, In case either history is impossible or if some # internal state needs to be set, actually submit to moves to the strategy # method. Still need to append history manually. for h1, h2 in zip(history_1, history_2): simulate_play(P1, P2, h1, h2) # Run the tests for response in responses: s1, s2 = simulate_play(P1, P2) test_class.assertEqual(s1, response) if attrs: for attr, value in attrs.items(): test_class.assertEqual(getattr(P1, attr), value)
def test_cloning(self): """Tests that Player.clone preserves the application of transformations. """ p1 = axelrod.Cooperator() p2 = FlipTransformer()(axelrod.Cooperator)() # Defector p3 = p2.clone() self.assertEqual(simulate_play(p1, p3), (C, D)) self.assertEqual(simulate_play(p1, p3), (C, D))
def test_vs_update(self): """Test that the q and v values update.""" random.seed(5) p1 = axelrod.CautiousQLearner() p2 = axelrod.Cooperator() simulate_play(p1, p2) self.assertEqual(p1.Vs, {"": 0.1, "0.0": 0}) simulate_play(p1, p2) self.assertEqual(p1.Vs, {"": 0.1, "0.0": 0.30000000000000004, "C1.0": 0})
def test_vs_update(self): """Test that the q and v values update.""" random.seed(5) p1 = axelrod.RiskyQLearner() p2 = axelrod.Cooperator() simulate_play(p1, p2) self.assertEqual(p1.Vs, {'': 0.9, '0.0': 0}) simulate_play(p1, p2) self.assertEqual(p1.Vs,{'': 0.9, '0.0': 2.7, 'C1.0': 0})
def test_prev_state_updates(self): """Test that the q and v values update.""" random.seed(5) p1 = axelrod.RiskyQLearner() p2 = axelrod.Cooperator() simulate_play(p1, p2) self.assertEqual(p1.prev_state, '0.0') simulate_play(p1, p2) self.assertEqual(p1.prev_state, 'C1.0')
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) p1 = Cooperator2() p2 = axelrod.Cooperator() self.assertEqual(simulate_play(p1, p2), (C, C)) self.assertEqual(simulate_play(p1, p2), (C, C))
def test_qs_update(self): """Test that the q and v values update.""" random.seed(5) p1 = axelrod.CautiousQLearner() p2 = axelrod.Cooperator() simulate_play(p1, p2) self.assertEqual(p1.Qs, {'': {C: 0, D: 0.1}, '0.0': {C: 0, D: 0}}) simulate_play(p1, p2) self.assertEqual(p1.Qs,{'': {C: 0, D: 0.1}, '0.0': {C: 0.30000000000000004, D: 0}, 'C1.0': {C: 0, D: 0.0}})
def test_vs_update(self): """Test that the q and v values update.""" random.seed(5) p1 = axelrod.RiskyQLearner() p2 = axelrod.Cooperator() simulate_play(p1, p2) self.assertEqual(p1.Vs, {'': 0.9, '0.0': 0}) simulate_play(p1, p2) self.assertEqual(p1.Vs, {'': 0.9, '0.0': 2.7, 'C1.0': 0})
def test_qs_update(self): """Test that the q and v values update.""" random.seed(5) p1 = axelrod.RiskyQLearner() p2 = axelrod.Cooperator() simulate_play(p1, p2) self.assertEqual(p1.Qs, {"": {C: 0, D: 0.9}, "0.0": {C: 0, D: 0}}) simulate_play(p1, p2) self.assertEqual(p1.Qs, {"": {C: 0, D: 0.9}, "0.0": {C: 2.7, D: 0}, "C1.0": {C: 0, D: 0}})
def test_vs_update(self): """ Test that the q and v values update """ random.seed(5) p1 = axelrod.ArrogantQLearner() p2 = axelrod.Cooperator() simulate_play(p1, p2) self.assertEqual(p1.Vs, {"": 0.9, "0.0": 0}) simulate_play(p1, p2) self.assertEqual(p1.Vs, {"": 0.9, "0.0": 2.7, "C1.0": 0})
def test_qs_update(self): """ Test that the q and v values update """ random.seed(5) p1 = axelrod.ArrogantQLearner() p2 = axelrod.Cooperator() play_1, play_2 = simulate_play(p1, p2) self.assertEqual(p1.Qs, {'': {C: 0, D: 0.9}, '0.0': {C: 0, D: 0}}) simulate_play(p1, p2) self.assertEqual(p1.Qs,{'': {C: 0, D: 0.9}, '0.0': {C: 2.7, D: 0}, 'C1.0': {C: 0, D: 0}})
def test_prev_state_updates(self): """ Test that the q and v values update """ random.seed(5) p1 = axelrod.HesitantQLearner() p2 = axelrod.Cooperator() simulate_play(p1, p2) self.assertEqual(p1.prev_state, "0.0") simulate_play(p1, p2) self.assertEqual(p1.prev_state, "C1.0")
def test_vs_update(self): """ Test that the q and v values update """ random.seed(5) p1 = axelrod.HesitantQLearner() p2 = axelrod.Cooperator() simulate_play(p1, p2) self.assertEqual(p1.Vs, {'': 0.1, '0.0': 0}) simulate_play(p1, p2) self.assertEqual(p1.Vs,{'': 0.1, '0.0': 0.30000000000000004, 'C1.0': 0})
def test_state_distribution(self): player1, player2 = self.player(), self.player() player1.strategy = randomize player2.strategy = randomize history_1 = [C, C, D, D, C] history_2 = [C, D, C, D, D] for h1, h2 in zip(history_1, history_2): simulate_play(player1, player2, h1, h2) self.assertEqual(player1.state_distribution, {(C, C): 1, (C, D): 2, (D, C): 1, (D, D): 1}) self.assertEqual(player2.state_distribution, {(C, C): 1, (C, D): 1, (D, C): 2, (D, D): 1})
def test_vs_update(self): """ Test that the q and v values update """ random.seed(5) p1 = axelrod.HesitantQLearner() p2 = axelrod.Cooperator() simulate_play(p1, p2) self.assertEqual(p1.Vs, {'': 0.1, '0.0': 0}) simulate_play(p1, p2) self.assertEqual(p1.Vs, { '': 0.1, '0.0': 0.30000000000000004, 'C1.0': 0 })
def test_various(self): p1 = TestOpponent() p2 = TestOpponent() 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)
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)
def test_various(self): p1 = TestOpponent() p2 = TestOpponent() 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) # TestOpponent always returns C for h1 in [C, D]: for h2 in [C, D]: self.assertEqual(simulate_play(p1, p2, h1, h2), (C, C)) self.assertEqual(p1.cooperations, 3) self.assertEqual(p2.cooperations, 3) self.assertEqual(p1.defections, 2) self.assertEqual(p2.defections, 2)
def test_various2(self): p1 = axelrod.Cooperator() p2 = axelrod.Defector() self.assertEqual(simulate_play(p1, p2), (C, D)) self.assertEqual(p1.cooperations, 1) self.assertEqual(p2.cooperations, 0) self.assertEqual(p1.defections, 0) self.assertEqual(p2.defections, 1) self.assertEqual(simulate_play(p1, p2), (C, D)) self.assertEqual(p1.cooperations, 2) self.assertEqual(p2.cooperations, 0) self.assertEqual(p1.defections, 0) self.assertEqual(p2.defections, 2) self.assertEqual(p1.history, [C] * 2) self.assertEqual(p2.history, [D] * 2)
def test_state_distribution(self): p1, p2 = self.player(), self.player() history_1 = [C, C, D, D, C] history_2 = [C, D, C, D, D] p1.strategy = randomize p2.strategy = randomize simulate_play(p1, p2, history_1, history_2) self.assertEqual(p1.state_distribution, { (C, C): 1, (C, D): 2, (D, C): 1, (D, D): 1 }) self.assertEqual(p2.state_distribution, { (C, C): 1, (C, D): 1, (D, C): 2, (D, D): 1 })
def test_qs_update(self): """Test that the q and v values update.""" random.seed(5) p1 = axelrod.ArrogantQLearner() p2 = axelrod.Cooperator() play_1, play_2 = simulate_play(p1, p2) self.assertEqual(p1.Qs, {'': {C: 0, D: 0.9}, '0.0': {C: 0, D: 0}}) simulate_play(p1, p2) self.assertEqual(p1.Qs, { '': { C: 0, D: 0.9 }, '0.0': { C: 2.7, D: 0 }, 'C1.0': { C: 0, D: 0 } })
def test_qs_update(self): """Test that the q and v values update.""" random.seed(5) p1 = axelrod.CautiousQLearner() p2 = axelrod.Cooperator() simulate_play(p1, p2) self.assertEqual(p1.Qs, {'': {C: 0, D: 0.1}, '0.0': {C: 0, D: 0}}) simulate_play(p1, p2) self.assertEqual( p1.Qs, { '': { C: 0, D: 0.1 }, '0.0': { C: 0.30000000000000004, D: 0 }, 'C1.0': { C: 0, D: 0.0 } })