|
| 1 | +import axelrod as axl |
| 2 | +from axelrod import Action |
| 3 | +from axelrod.tests.strategies.test_player import TestPlayer |
| 4 | +from axelrod.strategies.momentum import Momentum |
| 5 | + |
| 6 | +C, D = Action.C, Action.D |
| 7 | + |
| 8 | +class TestMomentum(TestPlayer): |
| 9 | + name = "Momentum" |
| 10 | + player = Momentum |
| 11 | + expected_classifier = { |
| 12 | + "memory_depth": float("inf"), |
| 13 | + "stochastic": False, |
| 14 | + "long_run_time": False, |
| 15 | + "inspects_source": False, |
| 16 | + "manipulates_source": False, |
| 17 | + "manipulates_state": False, |
| 18 | + } |
| 19 | + |
| 20 | + def test_initialisation(self): |
| 21 | + player = self.player(alpha=0.9, threshold=0.8) |
| 22 | + self.assertEqual(player.alpha, 0.9) |
| 23 | + self.assertEqual(player.threshold, 0.8) |
| 24 | + self.assertEqual(player.momentum, 1.0) |
| 25 | + |
| 26 | + def test_repr(self): |
| 27 | + player = self.player(alpha=0.9, threshold=0.8) |
| 28 | + self.assertEqual(repr(player), "Momentum: 0.9, 0.8") |
| 29 | + |
| 30 | + def test_strategy(self): |
| 31 | + actions = [(C, C)] |
| 32 | + self.versus_test( |
| 33 | + axl.MockPlayer(actions=[C]), |
| 34 | + expected_actions=actions, |
| 35 | + init_kwargs={"alpha": 0.5, |
| 36 | + "threshold": 0.5 |
| 37 | + }, |
| 38 | + attrs={"momentum": 1.0} |
| 39 | + ) |
| 40 | + |
| 41 | + actions = [(C, D), (C, D), (D, D)] |
| 42 | + self.versus_test( |
| 43 | + axl.MockPlayer(actions=[D]), |
| 44 | + expected_actions=actions, |
| 45 | + init_kwargs={"alpha": 0.5, |
| 46 | + "threshold": 0.5 |
| 47 | + }, |
| 48 | + attrs={"momentum": 0.25} |
| 49 | + ) |
| 50 | + |
| 51 | + def test_vs_alternator(self): |
| 52 | + actions = [(C, C), (C, D), (C, C), (C, D), (D, C)] |
| 53 | + self.versus_test(axl.Alternator(), |
| 54 | + expected_actions=actions, |
| 55 | + init_kwargs={"alpha": 0.5, |
| 56 | + "threshold": 0.5 |
| 57 | + }, |
| 58 | + ) |
| 59 | + |
| 60 | + def test_vs_cooperator(self): |
| 61 | + actions = [(C, C), (C, C), (C, C), (C, C), (C, C)] |
| 62 | + self.versus_test(axl.Cooperator(), |
| 63 | + expected_actions=actions, |
| 64 | + init_kwargs={"alpha": 0.5, |
| 65 | + "threshold": 0.5 |
| 66 | + }, |
| 67 | + ) |
| 68 | + |
| 69 | + def test_vs_defector(self): |
| 70 | + actions = [(C, D), (C, D), (D, D), (D, D), (D, D)] |
| 71 | + self.versus_test(axl.Defector(), |
| 72 | + expected_actions=actions, |
| 73 | + init_kwargs={"alpha": 0.5, |
| 74 | + "threshold": 0.5 |
| 75 | + }, |
| 76 | + ) |
| 77 | + |
| 78 | + def test_vs_random(self): |
| 79 | + # We can also test against random strategies |
| 80 | + actions = [(C, D), (C, C), (C, C), (C, D), (D, D)] |
| 81 | + self.versus_test(axl.Random(), |
| 82 | + expected_actions=actions, |
| 83 | + seed=17, |
| 84 | + init_kwargs={"alpha": 0.5, |
| 85 | + "threshold": 0.5 |
| 86 | + }, |
| 87 | + ) |
| 88 | + |
| 89 | + def test_vs_random2(self): |
| 90 | + actions = [(C, C), (C, C), (C, C), (C, C)] |
| 91 | + self.versus_test(axl.Random(), |
| 92 | + expected_actions=actions, |
| 93 | + seed=3, |
| 94 | + init_kwargs={"alpha": 0.5, |
| 95 | + "threshold": 0.5 |
| 96 | + }, |
| 97 | + ) |
| 98 | + |
| 99 | + |
0 commit comments