Skip to content

Commit dc17f33

Browse files
authored
Merge pull request #996 from eric-s-s/884-grumpy
884-grumpy
2 parents bd437b3 + 42ea53e commit dc17f33

File tree

2 files changed

+67
-34
lines changed

2 files changed

+67
-34
lines changed

axelrod/strategies/grumpy.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ def __init__(self, starting_state: str ='Nice', grumpy_threshold: int =10,
2525
"""
2626
Parameters
2727
----------
28-
starting_state, str: 'Nice' or 'Grumpy'
29-
grumpy_threshold, int
28+
starting_state: str
29+
'Nice' or 'Grumpy'
30+
grumpy_threshold: int
3031
The threshold of opponent defections - cooperations to become
3132
grumpy
32-
nice_threshold, int
33+
nice_threshold: int
3334
The threshold of opponent defections - cooperations to become
3435
nice
3536
"""
Lines changed: 63 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
"""Tests for the Grumpy strategy."""
22

3-
import axelrod
3+
import axelrod as axl
44
from .test_player import TestPlayer
55

6-
C, D = axelrod.Actions.C, axelrod.Actions.D
6+
C, D = axl.Actions.C, axl.Actions.D
77

88

99
class TestGrumpy(TestPlayer):
1010

1111
name = "Grumpy: Nice, 10, -10"
12-
player = axelrod.Grumpy
12+
player = axl.Grumpy
1313
expected_classifier = {
1414
'memory_depth': float('inf'), # Long memory
1515
'stochastic': False,
@@ -20,33 +20,65 @@ class TestGrumpy(TestPlayer):
2020
'manipulates_state': False
2121
}
2222

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)
4779

4880
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

Comments
 (0)