Skip to content

Commit

Permalink
Add ability to pass match attributes (#29)
Browse files Browse the repository at this point in the history
* Add a test for match_attributes.

* Can pass match attributes to objective function
  • Loading branch information
drvinceknight authored Aug 5, 2017
1 parent a1ac0a7 commit 0c054b1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
25 changes: 16 additions & 9 deletions src/axelrod_dojo/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def close(self):
## Objective functions for optimization

def prepare_objective(name="score", turns=200, noise=0., repetitions=None,
nmoran=None):
nmoran=None, match_attributes=None):
name = name.lower()
if name not in ["score", "score_diff", "moran"]:
raise ValueError("Score must be one of score, score_diff, or moran")
Expand All @@ -40,23 +40,27 @@ def prepare_objective(name="score", turns=200, noise=0., repetitions=None,
if nmoran is None:
nmoran = 4
objective = partial(objective_moran_win, turns=turns, noise=noise,
repetitions=repetitions, N=nmoran)
repetitions=repetitions, N=nmoran,
match_attributes=match_attributes)
elif name == "score":
if repetitions is None:
repetitions = 20
objective = partial(objective_score, turns=turns, noise=noise,
repetitions=repetitions)
repetitions=repetitions,
match_attributes=match_attributes)
elif name == "score_diff":
if repetitions is None:
repetitions = 20
objective = partial(objective_score_diff, turns=turns, noise=noise,
repetitions=repetitions)
repetitions=repetitions,
match_attributes=match_attributes)
return objective


def objective_score(me, other, turns, noise, repetitions):
def objective_score(me, other, turns, noise, repetitions, match_attributes=None):
"""Objective function to maximize total score over matches."""
match = axl.Match((me, other), turns=turns, noise=noise)
match = axl.Match((me, other), turns=turns, noise=noise,
match_attributes=match_attributes)
if not match._stochastic:
repetitions = 1
scores_for_this_opponent = []
Expand All @@ -67,9 +71,11 @@ def objective_score(me, other, turns, noise, repetitions):
return scores_for_this_opponent


def objective_score_diff(me, other, turns, noise, repetitions):
def objective_score_diff(me, other, turns, noise, repetitions,
match_attributes=None):
"""Objective function to maximize total score difference over matches."""
match = axl.Match((me, other), turns=turns, noise=noise)
match = axl.Match((me, other), turns=turns, noise=noise,
match_attributes=match_attributes)
if not match._stochastic:
repetitions = 1
scores_for_this_opponent = []
Expand All @@ -82,7 +88,8 @@ def objective_score_diff(me, other, turns, noise, repetitions):
return scores_for_this_opponent


def objective_moran_win(me, other, turns, noise, repetitions, N=5):
def objective_moran_win(me, other, turns, noise, repetitions, N=5,
match_attributes=None):
"""Objective function to maximize Moran fixations over N=4 matches"""
population = []
for _ in range(N):
Expand Down
10 changes: 6 additions & 4 deletions tests/unit/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ def test_incorrect_objective_name(self):
utils.prepare_objective(name=name)

def test_score(self):
objective = utils.prepare_objective(name="score",
turns=200,
noise=0,
repetitions=5)
objective = utils.prepare_objective(
name="score",
turns=200,
noise=0,
match_attributes={"length": float("inf")},
repetitions=5)
self.assertIsInstance(objective, functools.partial)
self.assertIn("objective_score ", str(objective))

Expand Down

0 comments on commit 0c054b1

Please sign in to comment.