Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions pyevolve/GPopulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,13 @@ def __init__(self, genome):
self.allSlots = [self.scaleMethod]

self.internalParams = {}
self.multiProcessing = (False, False)
self.multiProcessing = (False, False, False)

# Statistics
self.statted = False
self.stats = Statistics()

def setMultiProcessing(self, flag=True, full_copy=False):
def setMultiProcessing(self, flag=True, full_copy=False, limit_cores=False):
""" 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.
Expand All @@ -167,6 +167,7 @@ def setMultiProcessing(self, flag=True, full_copy=False):

:param flag: True (default) or False
:param full_copy: True or False (default)
:param limit_cores: Number of cores to use or just all use them all (default)

.. 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
Expand All @@ -176,7 +177,7 @@ def setMultiProcessing(self, flag=True, full_copy=False):
The `setMultiProcessing` method.

"""
self.multiProcessing = (flag, full_copy)
self.multiProcessing = (flag, full_copy, limit_cores)

def setMinimax(self, minimax):
""" Sets the population minimax
Expand Down Expand Up @@ -362,7 +363,15 @@ 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()

# Check for the numer of cores to use
if self.multiProcessing[2] is not False and (self.multiProcessing[2] <= CPU_COUNT and self.multiProcessing[2] > 0):
proc_pool = Pool(processes=self.multiProcessing[2])
logging.debug("You have limited the number of cores to be used to %d out of %d CPU cores available" % (self.multiProcessing[2], CPU_COUNT))
else:
proc_pool = Pool(processes=CPU_COUNT)
logging.debug("Warning: Too many cores requested! You asked for %d out of %d CPU cores available, so defaulting back to using all of them." % (self.multiprocessing[2], CPU_COUNT))


# Multiprocessing full_copy parameter
if self.multiProcessing[1]:
Expand Down
11 changes: 8 additions & 3 deletions pyevolve/GSimpleGA.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
import logging

from time import time
from types import BooleanType
from types import BooleanType, IntType
from sys import platform as sys_platform
from sys import stdout as sys_stdout

Expand Down Expand Up @@ -381,7 +381,7 @@ def __repr__(self):
ret+="\n"
return ret

def setMultiProcessing(self, flag=True, full_copy=False):
def setMultiProcessing(self, flag=True, full_copy=False, limit_cores=False):
""" 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.
Expand All @@ -401,6 +401,7 @@ def setMultiProcessing(self, flag=True, full_copy=False):

:param flag: True (default) or False
:param full_copy: True or False (default)
:param limit_cores: Number of cores to use or just use them all (default)

.. 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
Expand All @@ -422,7 +423,11 @@ 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)
if type(limit_cores) != BooleanType:
if type(limit_cores) != IntType:
Util.raiseException("Multiprocessing 'limit_cores' option must be either False or an Integer number of processes to spawn", TypeError)

self.internalPop.setMultiProcessing(flag, full_copy, limit_cores)

def setMigrationAdapter(self, migration_adapter=None):
""" Sets the Migration Adapter
Expand Down