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