Skip to content

Commit ee8e3f1

Browse files
committed
Pair the class and kwargs.
Note that in practice a user won't need to create the `PlayerInfo` tuples. These are constructed by the `Population.__init__` when it deconstructs the player instances passed as the `opponents` variable (a list).
1 parent 5e51cf3 commit ee8e3f1

File tree

3 files changed

+29
-28
lines changed

3 files changed

+29
-28
lines changed

src/axelrod_dojo/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
from .version import __version__
22
from .archetypes.fsm import FSMParams
3-
from .utils import prepare_objective, Population, load_params, Params, score_for
3+
from .utils import (prepare_objective,
4+
Population,
5+
load_params,
6+
Params,
7+
score_for,
8+
PlayerInfo)

src/axelrod_dojo/utils.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from collections import namedtuple
12
from functools import partial
23
from itertools import repeat
34
from multiprocessing import Pool, cpu_count
@@ -137,22 +138,20 @@ def params(self):
137138
def crossover(self, other):
138139
pass
139140

141+
PlayerInfo = namedtuple('PlayerInfo', ['strategy', 'init_kwargs'])
140142

141-
def score_params(params, objective, opponents,
142-
opponent_init_kwargs=None,
143+
def score_params(params, objective,
144+
opponents_information,
143145
weights=None):
144146
"""
145147
Return the overall mean score of a Params instance.
146148
"""
147149
scores_for_all_opponents = []
148150
player = params.player()
149151

150-
if opponent_init_kwargs is None:
151-
opponent_init_kwargs = [{} for _ in opponents]
152-
153-
for opponent_class, init_kwargs in zip(opponents, opponent_init_kwargs):
152+
for strategy, init_kwargs in opponents_information:
154153
player.reset()
155-
opponent = opponent_class(**init_kwargs)
154+
opponent = strategy(**init_kwargs)
156155
scores_for_this_opponent = objective(player, opponent)
157156
mean_vs_opponent = mean(scores_for_this_opponent)
158157
scores_for_all_opponents.append(mean_vs_opponent)
@@ -179,11 +178,11 @@ def __init__(self, params_class, params_args, size, objective, output_filename,
179178
else:
180179
self.bottleneck = bottleneck
181180
if opponents is None:
182-
self.opponents = axl.short_run_time_strategies
183-
self.opponent_init_kwargs = None
181+
self.opponents_information = [
182+
PlayerInfo(s, {}) for s in axl.short_run_time_strategies]
184183
else:
185-
self.opponents = [p.__class__ for p in opponents]
186-
self.opponent_init_kwargs = [p.init_kwargs for p in opponents]
184+
self.opponents_information = [
185+
PlayerInfo(p.__class__, p.init_kwargs) for p in opponents]
187186
self.generation = 0
188187
self.params_args = params_args
189188
self.population = [params_class(*params_args) for _ in range(self.size)]
@@ -193,8 +192,7 @@ def score_all(self):
193192
starmap_params = zip(
194193
self.population,
195194
repeat(self.objective),
196-
repeat(self.opponents),
197-
repeat(self.opponent_init_kwargs),
195+
repeat(self.opponents_information),
198196
repeat(self.weights))
199197
results = self.pool.starmap(score_params, starmap_params)
200198
return results

tests/unit/test_utils.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -180,66 +180,64 @@ class DummyParams(utils.Params):
180180
def player(self):
181181
return axl.Cooperator()
182182

183-
184183
class TestScoreParams(unittest.TestCase):
185184
def test_score(self):
186185
axl.seed(0)
187-
opponents = axl.demo_strategies
186+
opponents_information = [utils.PlayerInfo(s, {})
187+
for s in axl.demo_strategies]
188188
objective = utils.prepare_objective()
189189
params = DummyParams()
190190
score = utils.score_params(params,
191191
objective=objective,
192-
opponents=opponents)
192+
opponents_information=opponents_information)
193193
expected_score = 2.0949
194194
self.assertEqual(score, expected_score)
195195

196196
def test_with_init_kwargs(self):
197197
axl.seed(0)
198-
opponents = [axl.Random]
199-
opponent_init_kwargs = [{"p": 0}] # Creating a defector
198+
opponents_information = [utils.PlayerInfo(axl.Random, {"p": 0})]
200199
objective = utils.prepare_objective()
201200
params = DummyParams()
202201
score = utils.score_params(params,
203202
objective=objective,
204-
opponent_init_kwargs=opponent_init_kwargs,
205-
opponents=opponents)
203+
opponents_information=opponents_information)
206204
expected_score = 0
207205
self.assertEqual(score, expected_score)
208206

209-
opponent_init_kwargs = [{"p": 1}] # Creating a cooperator
207+
opponents_information = [utils.PlayerInfo(axl.Random, {"p": 1})]
210208
objective = utils.prepare_objective()
211209
params = DummyParams()
212210
score = utils.score_params(params,
213211
objective=objective,
214-
opponent_init_kwargs=opponent_init_kwargs,
215-
opponents=opponents)
212+
opponents_information=opponents_information)
216213
expected_score = 3.0
217214
self.assertEqual(score, expected_score)
218215

219216
def test_score_with_weights(self):
220217
axl.seed(0)
221-
opponents = axl.demo_strategies
218+
opponents_information = [utils.PlayerInfo(s, {})
219+
for s in axl.demo_strategies]
222220
objective = utils.prepare_objective()
223221
params = DummyParams()
224222
score = utils.score_params(params,
225223
objective=objective,
226-
opponents=opponents,
224+
opponents_information=opponents_information,
227225
# All weight on Coop
228226
weights=[1, 0, 0, 0, 0])
229227
expected_score = 3
230228
self.assertEqual(score, expected_score)
231229

232230
score = utils.score_params(params,
233231
objective=objective,
234-
opponents=opponents,
232+
opponents_information=opponents_information,
235233
# Shared weight between Coop and Def
236234
weights=[2, 2, 0, 0, 0])
237235
expected_score = 1.5
238236
self.assertEqual(score, expected_score)
239237

240238
score = utils.score_params(params,
241239
objective=objective,
242-
opponents=opponents,
240+
opponents_information=opponents_information,
243241
# Shared weight between Coop and Def
244242
weights=[2, -.5, 0, 0, 0])
245243
expected_score = 4.0

0 commit comments

Comments
 (0)