Skip to content

Commit

Permalink
Move processes argument from Tournament.__init__ to tournament.play
Browse files Browse the repository at this point in the history
  • Loading branch information
marcharper committed May 11, 2016
1 parent 6cab8c8 commit 4b5feac
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 41 deletions.
5 changes: 2 additions & 3 deletions axelrod/tests/integration/test_tournament.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ def test_parallel_play(self):
players=self.players,
game=self.game,
turns=20,
repetitions=self.test_repetitions,
processes=2)
scores = tournament.play(progress_bar=False).scores
repetitions=self.test_repetitions)
scores = tournament.play(processes=2, progress_bar=False).scores
actual_outcome = sorted(zip(self.player_names, scores))
self.assertEqual(actual_outcome, self.expected_outcome)
40 changes: 15 additions & 25 deletions axelrod/tests/unit/test_tournament.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ def test_init(self):
players=self.players,
game=self.game,
turns=self.test_turns,
processes=4,
noise=0.2)
self.assertEqual(len(tournament.players), len(test_strategies))
self.assertIsInstance(
Expand All @@ -72,7 +71,6 @@ def test_init(self):
self.assertEqual(tournament.turns, self.test_turns)
self.assertEqual(tournament.repetitions, 10)
self.assertEqual(tournament.name, 'test')
self.assertEqual(tournament._processes, 4)
self.assertTrue(tournament._with_morality)
self.assertIsInstance(tournament._logger, logging.Logger)
self.assertEqual(tournament.noise, 0.2)
Expand Down Expand Up @@ -182,10 +180,9 @@ def test_progress_bar_play_parallel(self):
players=self.players,
game=self.game,
turns=200,
repetitions=self.test_repetitions,
processes=2)
repetitions=self.test_repetitions)

results = tournament.play()
results = tournament.play(processes=2)
self.assertIsInstance(results, axelrod.ResultSet)

results = tournament.play(progress_bar=True)
Expand Down Expand Up @@ -231,9 +228,8 @@ def test_parallel_play(self):
players=self.players,
game=self.game,
turns=200,
repetitions=self.test_repetitions,
processes=2)
results = tournament.play(progress_bar=False)
repetitions=self.test_repetitions)
results = tournament.play(processes=2, progress_bar=False)
self.assertIsInstance(results, axelrod.ResultSet)

# The following relates to #516
Expand All @@ -245,9 +241,8 @@ def test_parallel_play(self):
players=players,
game=self.game,
turns=20,
repetitions=self.test_repetitions,
processes=2)
scores = tournament.play(progress_bar=False).scores
repetitions=self.test_repetitions)
scores = tournament.play(processes=2, progress_bar=False).scores
self.assertEqual(len(scores), len(players))

def test_run_serial(self):
Expand All @@ -256,8 +251,7 @@ def test_run_serial(self):
players=self.players,
game=self.game,
turns=200,
repetitions=self.test_repetitions,
processes=2)
repetitions=self.test_repetitions)
tournament._write_interactions = MagicMock(
name='_write_interactions')
self.assertTrue(tournament._run_serial())
Expand All @@ -272,8 +266,7 @@ def test_run_parallel(self):
players=self.players,
game=self.game,
turns=200,
repetitions=self.test_repetitions,
processes=2)
repetitions=self.test_repetitions)
tournament._write_interactions = MagicMock(
name='_write_interactions')
self.assertTrue(tournament._run_parallel())
Expand All @@ -290,18 +283,17 @@ def test_n_workers(self):
players=self.players,
game=self.game,
turns=200,
repetitions=self.test_repetitions,
processes=1)
self.assertEqual(tournament._n_workers(), max_processes)
repetitions=self.test_repetitions)
self.assertEqual(tournament._n_workers(processes=1), max_processes)

tournament = axelrod.Tournament(
name=self.test_name,
players=self.players,
game=self.game,
turns=200,
repetitions=self.test_repetitions,
processes=max_processes + 2)
self.assertEqual(tournament._n_workers(), max_processes)
repetitions=self.test_repetitions)
self.assertEqual(tournament._n_workers(processes=max_processes+2),
max_processes)

@unittest.skipIf(
cpu_count() < 2,
Expand All @@ -315,9 +307,8 @@ def test_2_workers(self):
players=self.players,
game=self.game,
turns=200,
repetitions=self.test_repetitions,
processes=2)
self.assertEqual(tournament._n_workers(), 2)
repetitions=self.test_repetitions,)
self.assertEqual(tournament._n_workers(processes=2), 2)

def test_start_workers(self):
workers = 2
Expand Down Expand Up @@ -523,7 +514,6 @@ def test_init(self):
self.assertEqual(tournament.turns, float("inf"))
self.assertEqual(tournament.repetitions, 10)
self.assertEqual(tournament.name, 'test')
self.assertEqual(tournament._processes, None)
self.assertTrue(tournament._with_morality)
self.assertIsInstance(tournament._logger, logging.Logger)
self.assertEqual(tournament.noise, 0.2)
Expand Down
23 changes: 10 additions & 13 deletions axelrod/tournament.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Tournament(object):

def __init__(self, players, match_generator=RoundRobinMatches,
name='axelrod', game=None, turns=200, repetitions=10,
processes=None, noise=0, with_morality=True):
noise=0, with_morality=True):
"""
Parameters
----------
Expand Down Expand Up @@ -53,7 +53,6 @@ def __init__(self, players, match_generator=RoundRobinMatches,
self.match_generator = match_generator(players, turns, self.game,
self.repetitions)
self._with_morality = with_morality
self._processes = processes
self._logger = logging.getLogger(__name__)

def setup_output_file(self, filename=None):
Expand All @@ -68,7 +67,7 @@ def setup_output_file(self, filename=None):
# Save filename for loading ResultSet later
self.filename = filename

def play(self, build_results=True, filename=None, progress_bar=True):
def play(self, build_results=True, filename=None, processes=None, progress_bar=True):
"""
Plays the tournament and passes the results to the ResultSet class
Expand All @@ -92,10 +91,10 @@ def play(self, build_results=True, filename=None, progress_bar=True):
if not build_results and not filename:
warnings.warn("Tournament results will not be accessible since build_results=False and no filename was supplied.")

if self._processes is None:
if processes is None:
self._run_serial(progress_bar=progress_bar)
else:
self._run_parallel(progress_bar=progress_bar)
self._run_parallel(processes=processes, progress_bar=progress_bar)

# Make sure that python has finished writing to disk
self.outputfile.flush()
Expand Down Expand Up @@ -151,7 +150,7 @@ def _write_interactions(self, results):
row.append(history2)
self.writer.writerow(row)

def _run_parallel(self, progress_bar=False):
def _run_parallel(self, processes=2, progress_bar=False):
"""
Run all matches in parallel
Expand All @@ -166,7 +165,7 @@ def _run_parallel(self, progress_bar=False):
# target functions which can be pickled and instance methods cannot.
work_queue = Queue()
done_queue = Queue()
workers = self._n_workers()
workers = self._n_workers(processes=processes)

chunks = self.match_generator.build_match_chunks()
for chunk in chunks:
Expand All @@ -177,16 +176,16 @@ def _run_parallel(self, progress_bar=False):

return True

def _n_workers(self):
def _n_workers(self, processes=2):
"""
Determines the number of parallel processes to use.
Returns
-------
integer
"""
if (2 <= self._processes <= cpu_count()):
n_workers = self._processes
if (2 <= processes <= cpu_count()):
n_workers = processes
else:
n_workers = cpu_count()
return n_workers
Expand Down Expand Up @@ -297,7 +296,6 @@ class ProbEndTournament(Tournament):

def __init__(self, players, match_generator=ProbEndRoundRobinMatches,
name='axelrod', game=None, prob_end=.5, repetitions=10,
processes=None,
noise=0,
with_morality=True):
"""
Expand All @@ -324,8 +322,7 @@ def __init__(self, players, match_generator=ProbEndRoundRobinMatches,
"""
super(ProbEndTournament, self).__init__(
players, name=name, game=game, turns=float("inf"),
repetitions=repetitions, processes=processes,
noise=noise, with_morality=with_morality)
repetitions=repetitions, noise=noise, with_morality=with_morality)

self.prob_end = prob_end
self.match_generator = ProbEndRoundRobinMatches(
Expand Down

0 comments on commit 4b5feac

Please sign in to comment.