From 948006be75947f11d3928e653091109ae5478d99 Mon Sep 17 00:00:00 2001 From: Ben Elliston Date: Tue, 19 Aug 2014 21:36:19 +1000 Subject: [PATCH] Add an optional max_processes parameter to setMultiProcessing() to control the number of worker processes in the pool. Defaults to None, which leads to one worker for every CPU available (as the Python multiprocessing module does). --- pyevolve/GPopulation.py | 9 +++++---- pyevolve/GSimpleGA.py | 5 +++-- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/pyevolve/GPopulation.py b/pyevolve/GPopulation.py index 0746dd4..a8b7f19 100644 --- a/pyevolve/GPopulation.py +++ b/pyevolve/GPopulation.py @@ -152,13 +152,13 @@ def __init__(self, genome): self.allSlots = [self.scaleMethod] self.internalParams = {} - self.multiProcessing = (False, False) + self.multiProcessing = (False, False, None) # Statistics self.statted = False self.stats = Statistics() - def setMultiProcessing(self, flag=True, full_copy=False): + def setMultiProcessing(self, flag=True, full_copy=False, max_processes=None): """ Sets the flag to enable/disable the use of python multiprocessing module. Use this option when you have more than one core on your CPU and when your evaluation function is very slow. @@ -168,6 +168,7 @@ def setMultiProcessing(self, flag=True, full_copy=False): :param flag: True (default) or False :param full_copy: True or False (default) + :param max_processes: None (default) or an integer value .. warning:: Use this option only when your evaluation function is slow, se you will get a good tradeoff between the process communication speed and the @@ -177,7 +178,7 @@ def setMultiProcessing(self, flag=True, full_copy=False): The `setMultiProcessing` method. """ - self.multiProcessing = (flag, full_copy) + self.multiProcessing = (flag, full_copy, max_processes) def setMinimax(self, minimax): """ Sets the population minimax @@ -385,7 +386,7 @@ def evaluate(self, **args): # We have multiprocessing if self.multiProcessing[0] and MULTI_PROCESSING: logging.debug("Evaluating the population using the multiprocessing method") - proc_pool = Pool() + proc_pool = Pool(processes=self.multiProcessing[2]) # Multiprocessing full_copy parameter if self.multiProcessing[1]: diff --git a/pyevolve/GSimpleGA.py b/pyevolve/GSimpleGA.py index b750a86..8f39aa7 100644 --- a/pyevolve/GSimpleGA.py +++ b/pyevolve/GSimpleGA.py @@ -383,7 +383,7 @@ def __repr__(self): ret += "\n" return ret - def setMultiProcessing(self, flag=True, full_copy=False): + def setMultiProcessing(self, flag=True, full_copy=False, max_processes=None): """ Sets the flag to enable/disable the use of python multiprocessing module. Use this option when you have more than one core on your CPU and when your evaluation function is very slow. @@ -403,6 +403,7 @@ def setMultiProcessing(self, flag=True, full_copy=False): :param flag: True (default) or False :param full_copy: True or False (default) + :param max_processes: None (default) or an integer value .. warning:: Use this option only when your evaluation function is slow, so you'll get a good tradeoff between the process communication speed and the @@ -424,7 +425,7 @@ def setMultiProcessing(self, flag=True, full_copy=False): if type(full_copy) != BooleanType: Util.raiseException("Multiprocessing 'full_copy' option must be True or False", TypeError) - self.internalPop.setMultiProcessing(flag, full_copy) + self.internalPop.setMultiProcessing(flag, full_copy, max_processes) def setMigrationAdapter(self, migration_adapter=None): """ Sets the Migration Adapter