2
2
"""
3
3
from __future__ import absolute_import , division , print_function #, unicode_literals
4
4
import sys
5
+ import warnings
5
6
import numpy as np
6
7
from multiprocessing import Pool as ProcessingPool
7
8
# from pathos.multiprocessing import ProcessingPool
@@ -207,8 +208,10 @@ class EvalParallel2(object):
207
208
208
209
Examples:
209
210
210
- >>> import cma
211
211
>>> from cma.optimization_tools import EvalParallel2
212
+ >>> for n_jobs in [None, -1, 0, 1, 2, 4]:
213
+ ... with EvalParallel2(cma.fitness_functions.elli, n_jobs) as eval_all:
214
+ ... res = eval_all([[1,2], [3,4]])
212
215
>>> # class usage, don't forget to call terminate
213
216
>>> ep = EvalParallel2(cma.fitness_functions.elli, 4)
214
217
>>> ep([[1,2], [3,4], [4, 5]]) # doctest:+ELLIPSIS
@@ -244,11 +247,11 @@ class EvalParallel2(object):
244
247
"""
245
248
def __init__ (self , fitness_function = None , number_of_processes = None ):
246
249
self .fitness_function = fitness_function
247
- self .processes = number_of_processes
248
- if self .processes is not None and self .processes <= 0 :
249
- self .pool = None
250
- else :
250
+ self .processes = number_of_processes # for the record
251
+ if self .processes is None or self .processes > 0 :
251
252
self .pool = ProcessingPool (self .processes )
253
+ else :
254
+ self .pool = None
252
255
253
256
def __call__ (self , solutions , fitness_function = None , args = (), timeout = None ):
254
257
"""evaluate a list/sequence of solution-"vectors", return a list
@@ -269,7 +272,7 @@ def __call__(self, solutions, fitness_function=None, args=(), timeout=None):
269
272
warning_str = ("`fitness_function` must be a function, not a"
270
273
" `lambda` or an instancemethod, in order to work with"
271
274
" `multiprocessing` under Python 2" )
272
- if sys .version [0 ] == '2' : # not necessary anymore?
275
+ if sys .version [0 ] == '2' :
273
276
if isinstance (fitness_function , type (self .__init__ )):
274
277
warnings .warn (warning_str )
275
278
jobs = [self .pool .apply_async (fitness_function , (x ,) + args )
0 commit comments