Skip to content

Commit 0c054b1

Browse files
Add ability to pass match attributes (#29)
* Add a test for match_attributes. * Can pass match attributes to objective function
1 parent a1ac0a7 commit 0c054b1

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

src/axelrod_dojo/utils.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def close(self):
3030
## Objective functions for optimization
3131

3232
def prepare_objective(name="score", turns=200, noise=0., repetitions=None,
33-
nmoran=None):
33+
nmoran=None, match_attributes=None):
3434
name = name.lower()
3535
if name not in ["score", "score_diff", "moran"]:
3636
raise ValueError("Score must be one of score, score_diff, or moran")
@@ -40,23 +40,27 @@ def prepare_objective(name="score", turns=200, noise=0., repetitions=None,
4040
if nmoran is None:
4141
nmoran = 4
4242
objective = partial(objective_moran_win, turns=turns, noise=noise,
43-
repetitions=repetitions, N=nmoran)
43+
repetitions=repetitions, N=nmoran,
44+
match_attributes=match_attributes)
4445
elif name == "score":
4546
if repetitions is None:
4647
repetitions = 20
4748
objective = partial(objective_score, turns=turns, noise=noise,
48-
repetitions=repetitions)
49+
repetitions=repetitions,
50+
match_attributes=match_attributes)
4951
elif name == "score_diff":
5052
if repetitions is None:
5153
repetitions = 20
5254
objective = partial(objective_score_diff, turns=turns, noise=noise,
53-
repetitions=repetitions)
55+
repetitions=repetitions,
56+
match_attributes=match_attributes)
5457
return objective
5558

5659

57-
def objective_score(me, other, turns, noise, repetitions):
60+
def objective_score(me, other, turns, noise, repetitions, match_attributes=None):
5861
"""Objective function to maximize total score over matches."""
59-
match = axl.Match((me, other), turns=turns, noise=noise)
62+
match = axl.Match((me, other), turns=turns, noise=noise,
63+
match_attributes=match_attributes)
6064
if not match._stochastic:
6165
repetitions = 1
6266
scores_for_this_opponent = []
@@ -67,9 +71,11 @@ def objective_score(me, other, turns, noise, repetitions):
6771
return scores_for_this_opponent
6872

6973

70-
def objective_score_diff(me, other, turns, noise, repetitions):
74+
def objective_score_diff(me, other, turns, noise, repetitions,
75+
match_attributes=None):
7176
"""Objective function to maximize total score difference over matches."""
72-
match = axl.Match((me, other), turns=turns, noise=noise)
77+
match = axl.Match((me, other), turns=turns, noise=noise,
78+
match_attributes=match_attributes)
7379
if not match._stochastic:
7480
repetitions = 1
7581
scores_for_this_opponent = []
@@ -82,7 +88,8 @@ def objective_score_diff(me, other, turns, noise, repetitions):
8288
return scores_for_this_opponent
8389

8490

85-
def objective_moran_win(me, other, turns, noise, repetitions, N=5):
91+
def objective_moran_win(me, other, turns, noise, repetitions, N=5,
92+
match_attributes=None):
8693
"""Objective function to maximize Moran fixations over N=4 matches"""
8794
population = []
8895
for _ in range(N):

tests/unit/test_utils.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@ def test_incorrect_objective_name(self):
3030
utils.prepare_objective(name=name)
3131

3232
def test_score(self):
33-
objective = utils.prepare_objective(name="score",
34-
turns=200,
35-
noise=0,
36-
repetitions=5)
33+
objective = utils.prepare_objective(
34+
name="score",
35+
turns=200,
36+
noise=0,
37+
match_attributes={"length": float("inf")},
38+
repetitions=5)
3739
self.assertIsInstance(objective, functools.partial)
3840
self.assertIn("objective_score ", str(objective))
3941

0 commit comments

Comments
 (0)