-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from aarongarrett/feat/2to3
feat: run 2to3 to update code for py3
- Loading branch information
Showing
14 changed files
with
246 additions
and
261 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,15 +2,15 @@ | |
=============================================== | ||
:mod:`evaluators` -- Fitness evaluation methods | ||
=============================================== | ||
Evaluator functions are problem-specific. This module provides pre-defined | ||
Evaluator functions are problem-specific. This module provides pre-defined | ||
evaluators for evolutionary computations. | ||
All evaluator functions have the following arguments: | ||
- *candidates* -- the candidate solutions | ||
- *args* -- a dictionary of keyword arguments | ||
.. Copyright 2012 Aaron Garrett | ||
.. Permission is hereby granted, free of charge, to any person obtaining a copy | ||
|
@@ -29,43 +29,40 @@ | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. | ||
THE SOFTWARE. | ||
.. module:: evaluators | ||
.. moduleauthor:: Aaron Garrett <[email protected]> | ||
.. moduleauthor:: Jelle Feringa <[email protected]> | ||
""" | ||
import functools | ||
try: | ||
import cPickle as pickle | ||
except ImportError: | ||
import pickle | ||
import pickle | ||
|
||
|
||
def evaluator(evaluate): | ||
"""Return an inspyred evaluator function based on the given function. | ||
This function generator takes a function that evaluates only one | ||
candidate. The generator handles the iteration over each candidate | ||
candidate. The generator handles the iteration over each candidate | ||
to be evaluated. | ||
The given function ``evaluate`` must have the following signature:: | ||
fitness = evaluate(candidate, args) | ||
This function is most commonly used as a function decorator with | ||
the following usage:: | ||
@evaluator | ||
def evaluate(candidate, args): | ||
# Implementation of evaluation | ||
pass | ||
The generated function also contains an attribute named | ||
``single_evaluation`` which holds the original evaluation function. | ||
In this way, the original single-candidate function can be | ||
retrieved if necessary. | ||
""" | ||
@functools.wraps(evaluate) | ||
def inspyred_evaluator(candidates, args): | ||
|
@@ -75,59 +72,59 @@ def inspyred_evaluator(candidates, args): | |
return fitness | ||
inspyred_evaluator.single_evaluation = evaluate | ||
return inspyred_evaluator | ||
|
||
|
||
def parallel_evaluation_pp(candidates, args): | ||
"""Evaluate the candidates in parallel using Parallel Python. | ||
This function allows parallel evaluation of candidate solutions. | ||
It uses the `Parallel Python <http://www.parallelpython.com>`_ (pp) | ||
library to accomplish the parallelization. This library must already | ||
be installed in order to use this function. The function assigns the | ||
evaluation of each candidate to its own job, all of which are then | ||
library to accomplish the parallelization. This library must already | ||
be installed in order to use this function. The function assigns the | ||
evaluation of each candidate to its own job, all of which are then | ||
distributed to the available processing units. | ||
.. note:: | ||
All arguments to the evaluation function must be pickleable. | ||
Those that are not will not be sent through the ``args`` variable | ||
and will be unavailable to your function. | ||
.. Arguments: | ||
candidates -- the candidate solutions | ||
args -- a dictionary of keyword arguments | ||
Required keyword arguments in args: | ||
- *pp_evaluator* -- actual evaluation function to be used (This function | ||
should have the same signature as any other inspyred evaluation function.) | ||
Optional keyword arguments in args: | ||
- *pp_dependencies* -- tuple of functional dependencies of the serial | ||
- *pp_dependencies* -- tuple of functional dependencies of the serial | ||
evaluator (default ()) | ||
- *pp_modules* -- tuple of modules that must be imported for the | ||
- *pp_modules* -- tuple of modules that must be imported for the | ||
functional dependencies (default ()) | ||
- *pp_servers* -- tuple of servers (on a cluster) that will be used | ||
- *pp_servers* -- tuple of servers (on a cluster) that will be used | ||
for parallel processing (default ("*",)) | ||
- *pp_secret* -- string representing the secret key needed to authenticate | ||
on a worker node (default "inspyred") | ||
- *pp_nprocs* -- integer representing the number of worker processes to | ||
start on the local machine (default "autodetect", which sets it to the | ||
number of processors in the system) | ||
For more information about these arguments, please consult the | ||
documentation for `Parallel Python <http://www.parallelpython.com>`_. | ||
""" | ||
import pp | ||
logger = args['_ec'].logger | ||
|
||
try: | ||
evaluator = args['pp_evaluator'] | ||
except KeyError: | ||
logger.error('parallel_evaluation_pp requires \'pp_evaluator\' be defined in the keyword arguments list') | ||
raise | ||
raise | ||
secret_key = args.setdefault('pp_secret', 'inspyred') | ||
try: | ||
job_server = args['_pp_job_server'] | ||
|
@@ -138,7 +135,7 @@ def parallel_evaluation_pp(candidates, args): | |
args['_pp_job_server'] = job_server | ||
pp_depends = args.setdefault('pp_dependencies', ()) | ||
pp_modules = args.setdefault('pp_modules', ()) | ||
|
||
pickled_args = {} | ||
for key in args: | ||
try: | ||
|
@@ -147,10 +144,10 @@ def parallel_evaluation_pp(candidates, args): | |
except (TypeError, pickle.PickleError, pickle.PicklingError): | ||
logger.debug('unable to pickle args parameter {0} in parallel_evaluation_pp'.format(key)) | ||
pass | ||
|
||
func_template = pp.Template(job_server, evaluator, pp_depends, pp_modules) | ||
jobs = [func_template.submit([c], pickled_args) for c in candidates] | ||
|
||
fitness = [] | ||
for i, job in enumerate(jobs): | ||
r = job() | ||
|
@@ -166,46 +163,46 @@ def parallel_evaluation_mp(candidates, args): | |
"""Evaluate the candidates in parallel using ``multiprocessing``. | ||
This function allows parallel evaluation of candidate solutions. | ||
It uses the standard multiprocessing library to accomplish the | ||
It uses the standard multiprocessing library to accomplish the | ||
parallelization. The function assigns the evaluation of each | ||
candidate to its own job, all of which are then distributed to the | ||
available processing units. | ||
.. note:: | ||
All arguments to the evaluation function must be pickleable. | ||
Those that are not will not be sent through the ``args`` variable | ||
and will be unavailable to your function. | ||
.. Arguments: | ||
candidates -- the candidate solutions | ||
args -- a dictionary of keyword arguments | ||
Required keyword arguments in args: | ||
- *mp_evaluator* -- actual evaluation function to be used (This function | ||
should have the same signature as any other inspyred evaluation function.) | ||
Optional keyword arguments in args: | ||
- *mp_nprocs* -- number of processors that will be used (default machine | ||
- *mp_nprocs* -- number of processors that will be used (default machine | ||
cpu count) | ||
""" | ||
import time | ||
import multiprocessing | ||
logger = args['_ec'].logger | ||
|
||
try: | ||
evaluator = args['mp_evaluator'] | ||
except KeyError: | ||
logger.error('parallel_evaluation_mp requires \'mp_evaluator\' be defined in the keyword arguments list') | ||
raise | ||
raise | ||
try: | ||
nprocs = args['mp_nprocs'] | ||
except KeyError: | ||
nprocs = multiprocessing.cpu_count() | ||
|
||
pickled_args = {} | ||
for key in args: | ||
try: | ||
|
@@ -228,4 +225,4 @@ def parallel_evaluation_mp(candidates, args): | |
else: | ||
end = time.time() | ||
logger.debug('completed parallel_evaluation_mp in {0} seconds'.format(end - start)) | ||
|
Oops, something went wrong.