|
| 1 | +"""Test for the Looker Up strategy.""" |
| 2 | + |
| 3 | +import axelrod |
| 4 | +from .test_player import TestPlayer, TestHeadsUp |
| 5 | + |
| 6 | +C, D = axelrod.Actions.C, axelrod.Actions.D |
| 7 | + |
| 8 | + |
| 9 | +class TestLookerUp(TestPlayer): |
| 10 | + |
| 11 | + name = "LookerUp" |
| 12 | + player = axelrod.LookerUp |
| 13 | + |
| 14 | + expected_classifier = { |
| 15 | + 'memory_depth': 1, # Default TFT table |
| 16 | + 'stochastic': False, |
| 17 | + 'inspects_source': False, |
| 18 | + 'manipulates_source': False, |
| 19 | + 'manipulates_state': False |
| 20 | + } |
| 21 | + |
| 22 | + def test_init(self): |
| 23 | + # Test empty table |
| 24 | + player = self.player(dict()) |
| 25 | + opponent = axelrod.Cooperator() |
| 26 | + self.assertEqual(player.strategy(opponent), C) |
| 27 | + # Test default table |
| 28 | + player = self.player() |
| 29 | + expected_lookup_table = { |
| 30 | + ('', 'C', 'D') : D, |
| 31 | + ('', 'D', 'D') : D, |
| 32 | + ('', 'C', 'C') : C, |
| 33 | + ('', 'D', 'C') : C, |
| 34 | + } |
| 35 | + self.assertEqual(player.lookup_table, expected_lookup_table) |
| 36 | + # Test malformed tables |
| 37 | + table = {(C, C): C, ('DD', 'DD'): C} |
| 38 | + with self.assertRaises(ValueError): |
| 39 | + player = self.player(table) |
| 40 | + table = {(C, C): C, (C, D): 'CD'} |
| 41 | + with self.assertRaises(ValueError): |
| 42 | + player = self.player(table) |
| 43 | + |
| 44 | + def test_strategy(self): |
| 45 | + self.markov_test([C, D, C, D]) # TFT |
| 46 | + self.responses_test([C] * 4, [C, C, C, C], [C]) |
| 47 | + self.responses_test([C] * 5, [C, C, C, C, D], [D]) |
| 48 | + |
| 49 | + def test_defector_table(self): |
| 50 | + """ |
| 51 | + Testing a lookup table that always defects if there is enough history. |
| 52 | + In order for the testing framework to be able to construct new player |
| 53 | + objects for the test, self.player needs to be callable with no |
| 54 | + arguments, thus we use a lambda expression which will call the |
| 55 | + constructor with the lookup table we want. |
| 56 | + """ |
| 57 | + defector_table = { |
| 58 | + ('', C, D) : D, |
| 59 | + ('', D, D) : D, |
| 60 | + ('', C, C) : D, |
| 61 | + ('', D, C) : D, |
| 62 | + } |
| 63 | + self.player = lambda : axelrod.LookerUp(defector_table) |
| 64 | + self.responses_test([C, C], [C, C], [D]) |
| 65 | + self.responses_test([C, D], [D, C], [D]) |
| 66 | + self.responses_test([D, D], [D, D], [D]) |
| 67 | + |
| 68 | + def test_starting_move(self): |
| 69 | + """A lookup table that always repeats the opponent's first move.""" |
| 70 | + |
| 71 | + first_move_table = { |
| 72 | + # If oppponent starts by cooperating: |
| 73 | + (C, C, D) : C, |
| 74 | + (C, D, D) : C, |
| 75 | + (C, C, C) : C, |
| 76 | + (C, D, C) : C, |
| 77 | + # If opponent starts by defecting: |
| 78 | + (D, C, D) : D, |
| 79 | + (D, D, D) : D, |
| 80 | + (D, C, C) : D, |
| 81 | + (D, D, C) : D, |
| 82 | + } |
| 83 | + |
| 84 | + self.player = lambda : axelrod.LookerUp(first_move_table) |
| 85 | + |
| 86 | + # if the opponent started by cooperating, we should always cooperate |
| 87 | + self.responses_test([C, C, C], [C, C, C], [C]) |
| 88 | + self.responses_test([D, D, D], [C, C, C], [C]) |
| 89 | + self.responses_test([C, C, C], [C, D, C], [C]) |
| 90 | + self.responses_test([C, C, D], [C, D, C], [C]) |
| 91 | + |
| 92 | + # if the opponent started by defecting, we should always defect |
| 93 | + self.responses_test([C, C, C], [D, C, C], [D]) |
| 94 | + self.responses_test([D, D, D], [D, C, C], [D]) |
| 95 | + self.responses_test([C, C, C], [D, D, C], [D]) |
| 96 | + self.responses_test([C, C, D], [D, D, C], [D]) |
| 97 | + |
| 98 | + |
| 99 | +class TestEvolvedLookerUp(TestPlayer): |
| 100 | + |
| 101 | + name = "EvolvedLookerUp" |
| 102 | + player = axelrod.EvolvedLookerUp |
| 103 | + |
| 104 | + expected_classifier = { |
| 105 | + 'memory_depth': float('inf'), |
| 106 | + 'stochastic': False, |
| 107 | + 'inspects_source': False, |
| 108 | + 'manipulates_source': False, |
| 109 | + 'manipulates_state': False |
| 110 | + } |
| 111 | + |
| 112 | + def test_init(self): |
| 113 | + # Check for a few known keys |
| 114 | + known_pairs = {('DD', 'CC', 'CD'): 'D', ('DC', 'CD', 'CD'): 'D', |
| 115 | + ('DD', 'CD', 'CD'): 'C', ('DC', 'DC', 'DC'): 'C', |
| 116 | + ('DD', 'DD', 'CC'): 'D', ('CD', 'CC', 'DC'): 'C'} |
| 117 | + player = self.player() |
| 118 | + for k, v in known_pairs.items(): |
| 119 | + self.assertEqual(player.lookup_table[k], v) |
| 120 | + |
| 121 | + def test_strategy(self): |
| 122 | + """Starts by cooperating.""" |
| 123 | + self.first_play_test(C) |
| 124 | + |
| 125 | + |
| 126 | +# Some heads up tests for EvolvedLookerUp |
| 127 | +class EvolvedLookerUpvsDefector(TestHeadsUp): |
| 128 | + def test_vs(self): |
| 129 | + outcomes = zip([C, C, D], [D, D, D]) |
| 130 | + self.versus_test(axelrod.EvolvedLookerUp, axelrod.Defector, outcomes) |
| 131 | + |
| 132 | + |
| 133 | +class EvolvedLookerUpvsCooperator(TestHeadsUp): |
| 134 | + def test_vs(self): |
| 135 | + outcomes = zip([C] * 10, [C] * 10) |
| 136 | + self.versus_test(axelrod.EvolvedLookerUp, axelrod.Cooperator, outcomes) |
| 137 | + |
| 138 | + |
| 139 | +class EvolvedLookerUpvsTFT(TestHeadsUp): |
| 140 | + def test_vs(self): |
| 141 | + outcomes = zip([C] * 10, [C] * 10) |
| 142 | + self.versus_test(axelrod.EvolvedLookerUp, axelrod.TitForTat, outcomes) |
| 143 | + |
| 144 | + |
| 145 | +class EvolvedLookerUpvsAlternator(TestHeadsUp): |
| 146 | + def test_vs(self): |
| 147 | + outcomes = zip([C, C, D, D, D, D], [C, D, C, D, C, D]) |
| 148 | + self.versus_test(axelrod.EvolvedLookerUp, axelrod.Alternator, outcomes) |
0 commit comments