1
1
"""Tests for the Grumpy strategy."""
2
2
3
- import axelrod
3
+ import axelrod as axl
4
4
from .test_player import TestPlayer
5
5
6
- C , D = axelrod .Actions .C , axelrod .Actions .D
6
+ C , D = axl .Actions .C , axl .Actions .D
7
7
8
8
9
9
class TestGrumpy (TestPlayer ):
10
10
11
11
name = "Grumpy: Nice, 10, -10"
12
- player = axelrod .Grumpy
12
+ player = axl .Grumpy
13
13
expected_classifier = {
14
14
'memory_depth' : float ('inf' ), # Long memory
15
15
'stochastic' : False ,
@@ -20,33 +20,65 @@ class TestGrumpy(TestPlayer):
20
20
'manipulates_state' : False
21
21
}
22
22
23
- def test_strategy (self ):
24
- # Starts by cooperating.
25
- self .first_play_test (C )
26
- # Starts by defecting if grumpy initially.
27
- self .responses_test (D , init_kwargs = {"starting_state" : "Grumpy" })
28
- # Tests that grumpy will play C until threshold is hit at which point it
29
- # will become grumpy. Player will then not become nice until lower nice
30
- # threshold is hit.
31
-
32
- self .responses_test ([C ], [C , D , D , D ], [C , C , C , C ],
33
- attrs = {"state" : "Nice" },
34
- init_kwargs = {"grumpy_threshold" : 3 ,
35
- "nice_threshold" : 0 })
36
- self .responses_test ([D ], [C , C , D , D , D ], [D , D , D , D , D ],
37
- init_kwargs = {"grumpy_threshold" : 3 ,
38
- "nice_threshold" : 0 })
39
- self .responses_test ([D ], [C , C , D , D , D , D , D , D ],
40
- [D , D , D , D , D , C , C , C ],
41
- init_kwargs = {"grumpy_threshold" : 3 ,
42
- "nice_threshold" : 0 })
43
- self .responses_test ([C ], [C , C , D , D , D , D , D , D , D , D , D ],
44
- [D , D , D , D , D , C , C , C , C , C , C ],
45
- init_kwargs = {"grumpy_threshold" : 3 ,
46
- "nice_threshold" : 0 })
23
+ def test_default_strategy (self ):
24
+
25
+ opponent = axl .Cooperator ()
26
+ actions = [(C , C )] * 30
27
+ self .versus_test (opponent , expected_actions = actions )
28
+
29
+ opponent = axl .Alternator ()
30
+ actions = [(C , C ), (C , D )] * 30
31
+ self .versus_test (opponent , expected_actions = actions )
32
+
33
+ opponent = axl .Defector ()
34
+ actions = [(C , D )] * 11 + [(D , D )] * 20
35
+ self .versus_test (opponent , expected_actions = actions )
36
+
37
+ opponent_actions = [D ] * 11 + [C ] * 22 + [D ] * 11
38
+ opponent = axl .MockPlayer (actions = opponent_actions )
39
+ actions = ([(C , D )] * 11 + [(D , C )] * 22 + [(C , D )] * 11 ) * 3
40
+ self .versus_test (opponent , expected_actions = actions )
41
+
42
+ def test_starting_state (self ):
43
+ opponent_actions = [D ] * 11 + [C ] * 22 + [D ] * 11
44
+ opponent = axl .MockPlayer (actions = opponent_actions )
45
+
46
+ actions = ([(C , D )] * 11 + [(D , C )] * 22 + [(C , D )] * 11 ) * 3
47
+ init_kwargs = {'starting_state' : 'Nice' }
48
+ self .versus_test (opponent , expected_actions = actions ,
49
+ init_kwargs = init_kwargs )
50
+
51
+ opponent = axl .MockPlayer (actions = opponent_actions )
52
+ grumpy_starting = [(D , D )] * 11 + [(D , C )] * 22 + [(C , D )] * 11
53
+ actions = grumpy_starting + actions
54
+ init_kwargs = {'starting_state' : 'Grumpy' }
55
+ self .versus_test (opponent , expected_actions = actions ,
56
+ init_kwargs = init_kwargs )
57
+
58
+ def test_thresholds (self ):
59
+ init_kwargs = {'grumpy_threshold' : 3 , 'nice_threshold' : - 2 }
60
+ opponent_actions = [D ] * 4 + [C ] * 7 + [D ] * 3
61
+ opponent = axl .MockPlayer (actions = opponent_actions )
62
+ actions = ([(C , D )] * 4 + [(D , C )] * 7 + [(C , D )] * 3 ) * 3
63
+ self .versus_test (opponent , expected_actions = actions ,
64
+ init_kwargs = init_kwargs )
65
+
66
+ init_kwargs = {'grumpy_threshold' : 0 , 'nice_threshold' : - 2 }
67
+ opponent_actions = [D ] * 1 + [C ] * 4 + [D ] * 3
68
+ opponent = axl .MockPlayer (actions = opponent_actions )
69
+ actions = ([(C , D )] * 1 + [(D , C )] * 4 + [(C , D )] * 3 ) * 3
70
+ self .versus_test (opponent , expected_actions = actions ,
71
+ init_kwargs = init_kwargs )
72
+
73
+ init_kwargs = {'grumpy_threshold' : 3 , 'nice_threshold' : 0 }
74
+ opponent_actions = [D ] * 4 + [C ] * 5 + [D ] * 1
75
+ opponent = axl .MockPlayer (actions = opponent_actions )
76
+ actions = ([(C , D )] * 4 + [(D , C )] * 5 + [(C , D )] * 1 ) * 3
77
+ self .versus_test (opponent , expected_actions = actions ,
78
+ init_kwargs = init_kwargs )
47
79
48
80
def test_reset_state_with_non_default_init (self ):
49
- P1 = axelrod .Grumpy (starting_state = 'Grumpy' )
50
- P1 .state = 'Nice'
51
- P1 .reset ()
52
- self .assertEqual (P1 .state , 'Grumpy' )
81
+ player = axl .Grumpy (starting_state = 'Grumpy' )
82
+ player .state = 'Nice'
83
+ player .reset ()
84
+ self .assertEqual (player .state , 'Grumpy' )
0 commit comments