Skip to content

Commit e4cf23b

Browse files
authored
Merge pull request #1017 from Axelrod-Python/884-qlearners
Refactor the Q learner tests.
2 parents 20b4a8c + 01cb9cd commit e4cf23b

File tree

1 file changed

+62
-193
lines changed

1 file changed

+62
-193
lines changed

axelrod/tests/strategies/test_qlearner.py

Lines changed: 62 additions & 193 deletions
Original file line numberDiff line numberDiff line change
@@ -26,49 +26,25 @@ class TestRiskyQLearner(TestPlayer):
2626
def test_payoff_matrix(self):
2727
(R, P, S, T) = Game().RPST()
2828
payoff_matrix = {C: {C: R, D: S}, D: {C: T, D: P}}
29-
p1 = self.player()
30-
self.assertEqual(p1.payoff_matrix, payoff_matrix)
31-
32-
def test_qs_update(self):
33-
"""Test that the q and v values update."""
34-
random.seed(5)
35-
p1 = axelrod.RiskyQLearner()
36-
p2 = axelrod.Cooperator()
37-
simulate_play(p1, p2)
38-
self.assertEqual(p1.Qs, {'': {C: 0, D: 0.9}, '0.0': {C: 0, D: 0}})
39-
simulate_play(p1, p2)
40-
self.assertEqual(p1.Qs,
41-
{'': {C: 0, D: 0.9}, '0.0': {C: 2.7, D: 0},
42-
'C1.0': {C: 0, D: 0}})
43-
44-
def test_vs_update(self):
45-
"""Test that the q and v values update."""
46-
random.seed(5)
47-
p1 = axelrod.RiskyQLearner()
48-
p2 = axelrod.Cooperator()
49-
simulate_play(p1, p2)
50-
self.assertEqual(p1.Vs, {'': 0.9, '0.0': 0})
51-
simulate_play(p1, p2)
52-
self.assertEqual(p1.Vs,{'': 0.9, '0.0': 2.7, 'C1.0': 0})
53-
54-
def test_prev_state_updates(self):
55-
"""Test that the q and v values update."""
56-
random.seed(5)
57-
p1 = axelrod.RiskyQLearner()
58-
p2 = axelrod.Cooperator()
59-
simulate_play(p1, p2)
60-
self.assertEqual(p1.prev_state, '0.0')
61-
simulate_play(p1, p2)
62-
self.assertEqual(p1.prev_state, 'C1.0')
29+
player = self.player()
30+
self.assertEqual(player.payoff_matrix, payoff_matrix)
6331

6432
def test_strategy(self):
65-
"""Tests that it chooses the best strategy."""
66-
random.seed(5)
67-
p1 = axelrod.RiskyQLearner()
68-
p1.state = 'CCDC'
69-
p1.Qs = {'': {C: 0, D: 0}, 'CCDC': {C: 2, D: 6}}
70-
p2 = axelrod.Cooperator()
71-
test_responses(self, p1, p2, [C, D, C, C, D, C, C])
33+
actions = [(C, C), (D, C), (C, C), (C, C)]
34+
self.versus_test(opponent=axelrod.Cooperator(),
35+
expected_actions=actions,
36+
seed=5,
37+
attrs={"Qs": {'': {C: 0, D: 0.9},
38+
'0.0': {C: 2.7, D: 0},
39+
'C1.0': {C: 0, D: 4.5},
40+
'CC2.0': {C: 2.7, D: 0},
41+
'CCC3.0': {C: 0, D: 0}},
42+
"Vs": {'': 0.9,
43+
'0.0': 2.7,
44+
'C1.0': 4.5,
45+
'CC2.0': 2.7,
46+
'CCC3.0': 0},
47+
"prev_state": 'CCC3.0'})
7248

7349

7450
class TestArrogantQLearner(TestPlayer):
@@ -85,57 +61,22 @@ class TestArrogantQLearner(TestPlayer):
8561
'manipulates_state': False
8662
}
8763

88-
def test_qs_update(self):
89-
"""Test that the q and v values update."""
90-
random.seed(5)
91-
p1 = axelrod.ArrogantQLearner()
92-
p2 = axelrod.Cooperator()
93-
play_1, play_2 = simulate_play(p1, p2)
94-
self.assertEqual(p1.Qs, {'': {C: 0, D: 0.9}, '0.0': {C: 0, D: 0}})
95-
simulate_play(p1, p2)
96-
self.assertEqual(p1.Qs,{'': {C: 0, D: 0.9}, '0.0': {C: 2.7, D: 0},
97-
'C1.0': {C: 0, D: 0}})
98-
99-
def test_vs_update(self):
100-
"""Test that the q and v values update."""
101-
random.seed(5)
102-
p1 = axelrod.ArrogantQLearner()
103-
p2 = axelrod.Cooperator()
104-
simulate_play(p1, p2)
105-
self.assertEqual(p1.Vs, {'': 0.9, '0.0': 0})
106-
simulate_play(p1, p2)
107-
self.assertEqual(p1.Vs,{'': 0.9, '0.0': 2.7, 'C1.0': 0})
108-
109-
def test_prev_state_updates(self):
110-
"""Test that the q and v values update."""
111-
random.seed(5)
112-
p1 = axelrod.ArrogantQLearner()
113-
p2 = axelrod.Cooperator()
114-
simulate_play(p1, p2)
115-
self.assertEqual(p1.prev_state, '0.0')
116-
simulate_play(p1, p2)
117-
self.assertEqual(p1.prev_state, 'C1.0')
118-
11964
def test_strategy(self):
120-
"""Tests that it chooses the best strategy."""
121-
random.seed(9)
122-
p1 = axelrod.ArrogantQLearner()
123-
p1.state = 'CCDC'
124-
p1.Qs = {'': {C: 0, D: 0}, 'CCDC': {C: 2, D: 6}}
125-
p2 = axelrod.Cooperator()
126-
test_responses(self, p1, p2, [C, C, C, C, C, C, C])
127-
128-
def test_reset_method(self):
129-
"""Tests the reset method."""
130-
P1 = axelrod.ArrogantQLearner()
131-
P1.Qs = {'': {C: 0, D: -0.9}, '0.0': {C: 0, D: 0}}
132-
P1.Vs = {'': 0, '0.0': 0}
133-
P1.history = [C, D, D, D]
134-
P1.prev_state = C
135-
P1.reset()
136-
self.assertEqual(P1.prev_state, '')
137-
self.assertEqual(P1.Vs, {'':0})
138-
self.assertEqual(P1.Qs, {'':{C:0, D:0}})
65+
actions = [(C, C), (D, C), (C, C), (C, C)]
66+
self.versus_test(opponent=axelrod.Cooperator(),
67+
expected_actions=actions,
68+
seed=5,
69+
attrs={"Qs": {'': {C: 0, D: 0.9},
70+
'0.0': {C: 2.7, D: 0},
71+
'C1.0': {C: 0, D: 4.5},
72+
'CC2.0': {C: 2.7, D: 0},
73+
'CCC3.0': {C: 0, D: 0}},
74+
"Vs": {'': 0.9,
75+
'0.0': 2.7,
76+
'C1.0': 4.5,
77+
'CC2.0': 2.7,
78+
'CCC3.0': 0},
79+
"prev_state": 'CCC3.0'})
13980

14081

14182
class TestHesitantQLearner(TestPlayer):
@@ -152,58 +93,22 @@ class TestHesitantQLearner(TestPlayer):
15293
'manipulates_state': False
15394
}
15495

155-
def test_qs_update(self):
156-
"""Test that the q and v values update."""
157-
random.seed(5)
158-
p1 = axelrod.HesitantQLearner()
159-
p2 = axelrod.Cooperator()
160-
simulate_play(p1, p2)
161-
self.assertEqual(p1.Qs, {'': {C: 0, D: 0.1}, '0.0': {C: 0, D: 0}})
162-
simulate_play(p1, p2)
163-
self.assertEqual(p1.Qs,{'': {C: 0, D: 0.1},
164-
'0.0': {C: 0.30000000000000004, D: 0},
165-
'C1.0': {C: 0, D: 0}})
166-
167-
def test_vs_update(self):
168-
"""Test that the q and v values update."""
169-
random.seed(5)
170-
p1 = axelrod.HesitantQLearner()
171-
p2 = axelrod.Cooperator()
172-
simulate_play(p1, p2)
173-
self.assertEqual(p1.Vs, {'': 0.1, '0.0': 0})
174-
simulate_play(p1, p2)
175-
self.assertEqual(p1.Vs,{'': 0.1, '0.0': 0.30000000000000004, 'C1.0': 0})
176-
177-
def test_prev_state_updates(self):
178-
"""Test that the q and v values update."""
179-
random.seed(5)
180-
p1 = axelrod.HesitantQLearner()
181-
p2 = axelrod.Cooperator()
182-
simulate_play(p1, p2)
183-
self.assertEqual(p1.prev_state, '0.0')
184-
simulate_play(p1, p2)
185-
self.assertEqual(p1.prev_state, 'C1.0')
186-
18796
def test_strategy(self):
188-
"""Tests that it chooses the best strategy."""
189-
random.seed(9)
190-
p1 = axelrod.HesitantQLearner()
191-
p1.state = 'CCDC'
192-
p1.Qs = {'': {C: 0, D: 0}, 'CCDC': {C: 2, D: 6}}
193-
p2 = axelrod.Cooperator()
194-
test_responses(self, p1, p2, [C, C, C, C, C, C, C])
195-
196-
def test_reset_method(self):
197-
"""Tests the reset method."""
198-
P1 = axelrod.HesitantQLearner()
199-
P1.Qs = {'': {C: 0, D: -0.9}, '0.0': {C: 0, D: 0}}
200-
P1.Vs = {'': 0, '0.0': 0}
201-
P1.history = [C, D, D, D]
202-
P1.prev_state = C
203-
P1.reset()
204-
self.assertEqual(P1.prev_state, '')
205-
self.assertEqual(P1.Vs, {'': 0})
206-
self.assertEqual(P1.Qs, {'': {C: 0, D: 0}})
97+
actions = [(C, D), (D, D), (C, D), (C, D)]
98+
self.versus_test(opponent=axelrod.Defector(),
99+
expected_actions=actions,
100+
seed=5,
101+
attrs={"Qs": {'': {C: 0, D: 0.1},
102+
'0.0': {C: 0, D: 0},
103+
'D0.0': {C: 0, D: 0.1},
104+
'DD0.0': {C: 0, D: 0},
105+
'DDD0.0': {C: 0, D: 0}},
106+
"Vs": {'': 0.1,
107+
'0.0': 0.0,
108+
'D0.0': 0.1,
109+
'DD0.0': 0.0,
110+
'DDD0.0': 0},
111+
"prev_state": 'DDD0.0'})
207112

208113

209114
class TestCautiousQLearner(TestPlayer):
@@ -220,55 +125,19 @@ class TestCautiousQLearner(TestPlayer):
220125
'manipulates_state': False
221126
}
222127

223-
def test_qs_update(self):
224-
"""Test that the q and v values update."""
225-
random.seed(5)
226-
p1 = axelrod.CautiousQLearner()
227-
p2 = axelrod.Cooperator()
228-
simulate_play(p1, p2)
229-
self.assertEqual(p1.Qs, {'': {C: 0, D: 0.1}, '0.0': {C: 0, D: 0}})
230-
simulate_play(p1, p2)
231-
self.assertEqual(p1.Qs,{'': {C: 0, D: 0.1},
232-
'0.0': {C: 0.30000000000000004, D: 0},
233-
'C1.0': {C: 0, D: 0.0}})
234-
235-
def test_vs_update(self):
236-
"""Test that the q and v values update."""
237-
random.seed(5)
238-
p1 = axelrod.CautiousQLearner()
239-
p2 = axelrod.Cooperator()
240-
simulate_play(p1, p2)
241-
self.assertEqual(p1.Vs, {'': 0.1, '0.0': 0})
242-
simulate_play(p1, p2)
243-
self.assertEqual(p1.Vs,{'': 0.1, '0.0': 0.30000000000000004, 'C1.0': 0})
244-
245-
def test_prev_state_updates(self):
246-
"""Test that the q and v values update."""
247-
random.seed(5)
248-
p1 = axelrod.CautiousQLearner()
249-
p2 = axelrod.Cooperator()
250-
simulate_play(p1, p2)
251-
self.assertEqual(p1.prev_state, '0.0')
252-
simulate_play(p1, p2)
253-
self.assertEqual(p1.prev_state, 'C1.0')
254-
255128
def test_strategy(self):
256-
"""Tests that it chooses the best strategy."""
257-
random.seed(9)
258-
p1 = axelrod.CautiousQLearner()
259-
p1.state = 'CCDC'
260-
p1.Qs = {'': {C: 0, D: 0}, 'CCDC': {C: 2, D: 6}}
261-
p2 = axelrod.Cooperator()
262-
test_responses(self, p1, p2, [C, C, C, C, C, C, C])
263-
264-
def test_reset_method(self):
265-
"""Tests the reset method."""
266-
P1 = axelrod.CautiousQLearner()
267-
P1.Qs = {'': {C: 0, D: -0.9}, '0.0': {C: 0, D: 0}}
268-
P1.Vs = {'': 0, '0.0': 0}
269-
P1.history = [C, D, D, D]
270-
P1.prev_state = C
271-
P1.reset()
272-
self.assertEqual(P1.prev_state, '')
273-
self.assertEqual(P1.Vs, {'': 0})
274-
self.assertEqual(P1.Qs, {'': {C: 0, D: 0}})
129+
actions = [(C, D), (D, D), (C, D), (C, D)]
130+
self.versus_test(opponent=axelrod.Defector(),
131+
expected_actions=actions,
132+
seed=5,
133+
attrs={"Qs": {'': {C: 0, D: 0.1},
134+
'0.0': {C: 0, D: 0},
135+
'D0.0': {C: 0, D: 0.1},
136+
'DD0.0': {C: 0, D: 0},
137+
'DDD0.0': {C: 0, D: 0}},
138+
"Vs": {'': 0.1,
139+
'0.0': 0.0,
140+
'D0.0': 0.1,
141+
'DD0.0': 0.0,
142+
'DDD0.0': 0},
143+
"prev_state": 'DDD0.0'})

0 commit comments

Comments
 (0)