Skip to content

Commit bda417f

Browse files
marcharperdrvinceknight
authored andcommitted
More style fixes for test_dbs.py
1 parent d63c668 commit bda417f

File tree

1 file changed

+96
-113
lines changed

1 file changed

+96
-113
lines changed

axelrod/tests/strategies/test_dbs.py

Lines changed: 96 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
"""Tests DBS strategy."""
22

3-
import axelrod
43
import unittest
5-
from .test_player import TestPlayer
4+
import axelrod
65
from axelrod.strategies import dbs
6+
from .test_player import TestPlayer
77

88
C, D = axelrod.Actions.C, axelrod.Actions.D
99

1010

1111
class TestNode(unittest.TestCase):
12-
"""
13-
Test for the base class
14-
"""
12+
"""Test for the base Node class."""
1513
node = dbs.Node()
1614

1715
def test_get_siblings(self):
@@ -25,21 +23,18 @@ def test_is_stochastic(self):
2523

2624
class TestTreeSearch(unittest.TestCase):
2725
"""
28-
A set of tests for the tree-search functions.
29-
We test the answers of both minimax_tree_search and move_gen
30-
functions, against a set of classic policies (the answer being the
31-
best move to play for the next turn, considering an income
32-
position (C, C), (C, D), (D, C) or (D, D))
33-
For each policy, we test the answer for all income position
26+
A set of tests for the tree-search functions. We test the answers of both
27+
minimax_tree_search and move_gen functions, against a set of classic
28+
policies (the answer being the best move to play for the next turn,
29+
considering an incoming position (C, C), (C, D), (D, C) or (D, D)).
30+
For each policy, we test the answer for all incoming position.
3431
"""
3532
def setUp(self):
36-
"""
37-
Initialization for tests.
38-
"""
33+
"""Initialization for tests."""
3934
# For each test, we check the answer against each possible
40-
# inputs, that are in self.input_pos
35+
# inputs, that are in self.input_pos.
4136
self.input_pos = [(C, C), (C, D), (D, C), (D, D)]
42-
# We define the policies against which we are going to test
37+
# We define the policies against which we are going to test.
4338
self.cooperator_policy = dbs.create_policy(1, 1, 1, 1)
4439
self.defector_policy = dbs.create_policy(0, 0, 0, 0)
4540
self.titForTat_policy = dbs.create_policy(1, 1, 0, 0)
@@ -50,159 +45,149 @@ def setUp(self):
5045
def test_minimaxTreeSearch_cooperator(self):
5146
"""
5247
Tests the minimax_tree_search function when playing against a
53-
Cooperator player.
54-
Output == 0 means Cooperate, 1 means Defect.
48+
Cooperator player. Output == 0 means Cooperate, 1 means Defect.
5549
The best (hence expected) answer to Cooperator is to defect
5650
whatever the input position is.
5751
"""
5852
expected_output = [1, 1, 1, 1]
5953
for inp, out in zip(self.input_pos, expected_output):
6054
begin_node = dbs.DeterministicNode(inp[0], inp[1], depth=0)
61-
values = dbs.minimax_tree_search(begin_node,
62-
self.cooperator_policy, max_depth=5)
63-
self.assertEqual(values.index(max(values)),out)
55+
values = dbs.minimax_tree_search(
56+
begin_node, self.cooperator_policy, max_depth=5)
57+
self.assertEqual(values.index(max(values)), out)
6458

6559
def test_move_gen_cooperator(self):
6660
"""
67-
Tests the move_gen function when playing against a
68-
Cooperator player.
61+
Tests the move_gen function when playing against a Cooperator player.
6962
"""
7063
expected_output = [D, D, D, D]
7164
for inp, out in zip(self.input_pos, expected_output):
72-
out_move = dbs.move_gen(inp, self.cooperator_policy,
73-
depth_search_tree=5)
65+
out_move = dbs.move_gen(
66+
inp, self.cooperator_policy, depth_search_tree=5)
7467
self.assertEqual(out_move, out)
7568

7669
def test_minimaxTreeSearch_defector(self):
7770
"""
7871
Tests the minimax_tree_search function when playing against a
79-
Defector player.
80-
The best answer to Defector is to always defect
72+
Defector player. The best answer to Defector is to always defect
8173
"""
8274
expected_output = [1, 1, 1, 1]
8375
for inp, out in zip(self.input_pos, expected_output):
8476
begin_node = dbs.DeterministicNode(inp[0], inp[1], depth=0)
85-
values = dbs.minimax_tree_search(begin_node,
86-
self.defector_policy, max_depth=5)
77+
values = dbs.minimax_tree_search(
78+
begin_node, self.defector_policy, max_depth=5)
8779
self.assertEqual(values.index(max(values)),out)
8880

8981
def test_move_gen_defector(self):
9082
"""
91-
Tests the move_gen function when playing against a
92-
Defector player.
83+
Tests the move_gen function when playing against a Defector player.
9384
"""
9485
expected_output = [D, D, D, D]
9586
for inp, out in zip(self.input_pos, expected_output):
96-
out_move = dbs.move_gen(inp, self.defector_policy,
97-
depth_search_tree=5)
87+
out_move = dbs.move_gen(
88+
inp, self.defector_policy, depth_search_tree=5)
9889
self.assertEqual(out_move, out)
9990

10091
def test_minimaxTreeSearch_titForTat(self):
10192
"""
10293
Tests the minimax_tree_search function when playing against a
103-
TitForTat player.
104-
The best (hence expected) answer to TitFOrTat is to cooperate
105-
whatever the input position is.
94+
TitForTat player. The best (hence expected) answer to TitFOrTat is to
95+
cooperate whatever the input position is.
10696
"""
10797
expected_output = [0, 0, 0, 0]
10898
for inp, out in zip(self.input_pos, expected_output):
10999
begin_node = dbs.DeterministicNode(inp[0], inp[1], depth=0)
110-
values = dbs.minimax_tree_search(begin_node,
111-
self.titForTat_policy, max_depth=5)
100+
values = dbs.minimax_tree_search(
101+
begin_node, self.titForTat_policy, max_depth=5)
112102
self.assertEqual(values.index(max(values)),out)
113103

114104
def test_last_node_titForTat(self):
115105
"""
116-
Test that against TitForTat, for the last move, i.e. if tree
117-
depth is 1, the algorithms defects for all input
106+
Test that against TitForTat, for the last move, i.e. if tree depth is 1,
107+
the algorithms defects for all input.
118108
"""
119109
expected_output = [1, 1, 1, 1]
120110
for inp, out in zip(self.input_pos, expected_output):
121111
begin_node = dbs.DeterministicNode(inp[0], inp[1], depth=0)
122-
values = dbs.minimax_tree_search(begin_node,
123-
self.titForTat_policy, max_depth=1)
112+
values = dbs.minimax_tree_search(
113+
begin_node, self.titForTat_policy, max_depth=1)
124114
self.assertEqual(values.index(max(values)),out)
125115

126116
def test_move_gen_titForTat(self):
127117
"""
128-
Tests the move_gen function when playing against a
129-
TitForTat player.
118+
Tests the move_gen function when playing against a TitForTat player.
130119
"""
131120
expected_output = [C, C, C, C]
132121
for inp, out in zip(self.input_pos, expected_output):
133-
out_move = dbs.move_gen(inp, self.titForTat_policy,
134-
depth_search_tree=5)
122+
out_move = dbs.move_gen(
123+
inp, self.titForTat_policy, depth_search_tree=5)
135124
self.assertEqual(out_move, out)
136125

137126
def test_minimaxTreeSearch_alternator(self):
138127
"""
139128
Tests the minimax_tree_search function when playing against an
140-
Alternator player.
141-
The best answer to Alternator is to always defect
129+
Alternator player. The best answer to Alternator is to always defect.
142130
"""
143131
expected_output = [1, 1, 1, 1]
144132
for inp, out in zip(self.input_pos, expected_output):
145133
begin_node = dbs.DeterministicNode(inp[0], inp[1], depth=0)
146-
values = dbs.minimax_tree_search(begin_node,
147-
self.alternator_policy, max_depth=5)
134+
values = dbs.minimax_tree_search(
135+
begin_node, self.alternator_policy, max_depth=5)
148136
self.assertEqual(values.index(max(values)),out)
149137

150138
def test_move_gen_alternator(self):
151139
"""
152-
Tests the move_gen function when playing against an
153-
Alternator player.
140+
Tests the move_gen function when playing against an Alternator player.
154141
"""
155142
expected_output = [D, D, D, D]
156143
for inp, out in zip(self.input_pos, expected_output):
157-
out_move = dbs.move_gen(inp, self.random_policy, depth_search_tree=5)
144+
out_move = dbs.move_gen(inp, self.random_policy,
145+
depth_search_tree=5)
158146
self.assertEqual(out_move, out)
159147

160148
def test_minimaxTreeSearch_random(self):
161149
"""
162-
Tests the minimax_tree_search function when playing against a
163-
Random player.
164-
The best answer to Random is to always defect
150+
Tests the minimax_tree_search function when playing against a Random
151+
player. The best answer to Random is to always defect.
165152
"""
166153
expected_output = [1, 1, 1, 1]
167154
for inp, out in zip(self.input_pos, expected_output):
168155
begin_node = dbs.DeterministicNode(inp[0], inp[1], depth=0)
169-
values = dbs.minimax_tree_search(begin_node,
170-
self.random_policy, max_depth=5)
156+
values = dbs.minimax_tree_search(
157+
begin_node, self.random_policy, max_depth=5)
171158
self.assertEqual(values.index(max(values)),out)
172159

173160
def test_move_gen_random(self):
174161
"""
175-
Tests the move_gen function when playing against a
176-
Random player.
162+
Tests the move_gen function when playing against a Random player.
177163
"""
178164
expected_output = [D, D, D, D]
179165
for inp, out in zip(self.input_pos, expected_output):
180-
out_move = dbs.move_gen(inp, self.random_policy, depth_search_tree=5)
166+
out_move = dbs.move_gen(inp, self.random_policy,
167+
depth_search_tree=5)
181168
self.assertEqual(out_move, out)
182169

183170
def test_minimaxTreeSearch_grudger(self):
184171
"""
185172
Tests the minimax_tree_search function when playing against a
186-
Grudger player.
187-
The best answer to Grudger is to cooperate if both cooperated
188-
at last round, else it's to defect
173+
Grudger player. The best answer to Grudger is to cooperate if both
174+
cooperated at last round, else it's to defect.
189175
"""
190176
expected_output = [0, 1, 1, 1]
191177
for inp, out in zip(self.input_pos, expected_output):
192178
begin_node = dbs.DeterministicNode(inp[0], inp[1], depth=0)
193-
values = dbs.minimax_tree_search(begin_node,
194-
self.grudger_policy, max_depth=5)
179+
values = dbs.minimax_tree_search(
180+
begin_node, self.grudger_policy, max_depth=5)
195181
self.assertEqual(values.index(max(values)),out)
196182

197183
def test_move_gen_grudger(self):
198184
"""
199-
Tests the move_gen function when playing against a
200-
Grudger player.
185+
Tests the move_gen function when playing against a Grudger player.
201186
"""
202187
expected_output = [C, D, D, D]
203188
for inp, out in zip(self.input_pos, expected_output):
204-
out_move = dbs.move_gen(inp,
205-
self.grudger_policy, depth_search_tree=5)
189+
out_move = dbs.move_gen(
190+
inp, self.grudger_policy, depth_search_tree=5)
206191
self.assertEqual(out_move, out)
207192

208193

@@ -222,68 +207,66 @@ class TestDBS(TestPlayer):
222207

223208
def test_strategy(self):
224209
default_init_kwargs = {
225-
'discount_factor':.75, 'promotion_threshold':3,
226-
'violation_threshold':4, 'reject_threshold':4,
227-
'tree_depth':5
228-
}
210+
'discount_factor': .75, 'promotion_threshold': 3,
211+
'violation_threshold': 4, 'reject_threshold': 4,
212+
'tree_depth': 5
213+
}
229214

230-
# test that DBS always cooperate against Cooperator
215+
# Test that DBS always cooperate against Cooperator.
231216
actions = [(C, C)] * 7
232217
self.versus_test(
233-
opponent=axelrod.Cooperator(),
234-
expected_actions=actions,
235-
init_kwargs = default_init_kwargs
236-
)
218+
opponent=axelrod.Cooperator(),
219+
expected_actions=actions,
220+
init_kwargs = default_init_kwargs
221+
)
237222

238-
# test if it correctly learns Alternator strategy
223+
# Test if it correctly learns Alternator strategy.
239224
actions = [(C, C), (C, D)] * 3 + [(D, C), (C, D)] * 3
240225
self.versus_test(
241-
opponent=axelrod.Alternator(),
242-
expected_actions=actions,
243-
init_kwargs = default_init_kwargs
244-
)
226+
opponent=axelrod.Alternator(),
227+
expected_actions=actions,
228+
init_kwargs = default_init_kwargs
229+
)
245230

246-
# check that algorithms take into account a change in
247-
# opponent's strategy
231+
# Check that algorithms take into account a change in opponent's
232+
# strategy.
248233
mock_actions = [C, C, C, D, D, D, D, D, D, D]
249234
exp_actions = [(C, C)] * 3 + [(C, D)] * 4 + [(D, D)] * 3
250235
self.versus_test(
251-
opponent=axelrod.MockPlayer(actions=mock_actions),
252-
expected_actions=exp_actions,
253-
init_kwargs=default_init_kwargs
254-
)
236+
opponent=axelrod.MockPlayer(actions=mock_actions),
237+
expected_actions=exp_actions,
238+
init_kwargs=default_init_kwargs
239+
)
255240

256-
# check that adaptation is faster if diminishing promotion_threshold
241+
# Check that adaptation is faster if diminishing promotion_threshold.
257242
init_kwargs_2 = {
258-
'discount_factor':.75, 'promotion_threshold':2,
259-
'violation_threshold':4, 'reject_threshold':4,
260-
'tree_depth':5
261-
}
243+
'discount_factor': .75, 'promotion_threshold': 2,
244+
'violation_threshold': 4, 'reject_threshold': 4,
245+
'tree_depth': 5
246+
}
262247
mock_actions = [C, C, C, D, D, D, D, D, D, D]
263248
exp_actions = [(C, C)] * 3 + [(C, D)] * 3 + [(D, D)] * 4
264249
self.versus_test(
265-
opponent=axelrod.MockPlayer(actions=mock_actions),
266-
expected_actions=exp_actions,
267-
init_kwargs = init_kwargs_2
268-
)
250+
opponent=axelrod.MockPlayer(actions=mock_actions),
251+
expected_actions=exp_actions,
252+
init_kwargs = init_kwargs_2
253+
)
269254

270-
# check that ShouldDemote mecanism works.
271-
# We play against Alternator during 12 turns to make the
255+
# Check that ShouldDemote mechanism works.
256+
# We play against Alternator for 12 turns to make the
272257
# algorithm learn Alternator's strategy, then at turn 13 we
273-
# change opponent to Defector, hence trigging ShouldDemote
274-
# mecanism
275-
# For this test we use violation_threshold=3
258+
# change opponent to Defector, hence triggering ShouldDemote
259+
# mechanism. For this test we use violation_threshold=3
276260
init_kwargs_3 = {
277-
'discount_factor':.75, 'promotion_threshold':3,
278-
'violation_threshold':3, 'reject_threshold':3,
279-
'tree_depth':5
280-
}
261+
'discount_factor': .75, 'promotion_threshold': 3,
262+
'violation_threshold': 3, 'reject_threshold': 3,
263+
'tree_depth': 5
264+
}
281265
exp_actions = [(C, C), (C, D)] * 3 + [(D, C), (C, D)] * 3
282266
exp_actions += [(D, D), (C, D)] * 3 + [(D, D)]
283267
mock_actions = [C, D, C, D, C, D, C, D, C, D, C, D, D, D, D, D, D, D, D]
284268
self.versus_test(
285-
opponent=axelrod.MockPlayer(actions=mock_actions),
286-
expected_actions=exp_actions,
287-
init_kwargs = init_kwargs_3
288-
)
289-
269+
opponent=axelrod.MockPlayer(actions=mock_actions),
270+
expected_actions=exp_actions,
271+
init_kwargs=init_kwargs_3
272+
)

0 commit comments

Comments
 (0)