3636logger = logging .getLogger ('__main__' )
3737
3838
39- class ReduceFitness (deap .base .Fitness ):
40- """Fitness that compares by weighted"""
41-
42- def __init__ (self , values = (), obj_size = None , reduce_fcn = numpy .sum ):
43- self .weights = [- 1.0 ] * obj_size if obj_size is not None else [- 1 ]
44- self .reduce_fcn = reduce_fcn
45- super (ReduceFitness , self ).__init__ (values )
46-
47- @property
48- def reduce (self ):
49- return self .reduce_fcn (self .values )
50-
51- @property
52- def reduce_weight (self ):
53- """Weighted reduce of wvalues"""
54- return self .reduce_fcn (self .wvalues )
55-
56- def __le__ (self , other ):
57- return self .reduce_weight <= other .reduce_weight
58-
59- def __lt__ (self , other ):
60- return self .reduce_weight < other .reduce_weight
61-
62- def __deepcopy__ (self , _ ):
63- """Override deepcopy"""
64-
65- cls = self .__class__
66- result = cls .__new__ (cls )
67- result .__dict__ .update (self .__dict__ )
68- return result
69-
70-
71- class ListIndividual (list ):
72- """Individual consisting of list with weighted fitness field"""
73-
74- def __init__ (self , * args , ** kwargs ):
75- """Constructor"""
76- self .fitness = ReduceFitness (obj_size = kwargs ['obj_size' ],
77- reduce_fcn = kwargs ['reduce_fcn' ])
78- del kwargs ['obj_size' ]
79- del kwargs ['reduce_fcn' ]
80- super (ListIndividual , self ).__init__ (* args , ** kwargs )
81-
82- # Used by CMA multi objective
83- self ._ps = "p" , 0
84-
85-
8639def _ind_convert_space (ind , convert_fcn ):
8740 return [f (x ) for f , x in zip (convert_fcn , ind )]
8841
8942
9043class DEAPOptimisationCMA (bluepyopt .optimisations .Optimisation ):
91- """CMA based optimisation class"""
44+
45+ """Optimisation class for CMA-based evolution strategies"""
9246
9347 def __init__ (self ,
9448 evaluator = None ,
@@ -105,10 +59,12 @@ def __init__(self,
10559
10660 Args:
10761 evaluator (Evaluator): Evaluator object
62+ seed (float): Random number generator seed
63+ offspring_size (int): Number of offspring individuals in each
64+ generation
10865 centroids (list): list of initial guesses used as the starting
10966 points of the CMA-ES
11067 sigma (float): initial standard deviation of the distribution
111- seed (float): Random number generator seed
11268 map_function (function): Function used to map (parallelize) the
11369 evaluation function calls
11470 hof (hof): Hall of Fame object
@@ -124,11 +80,12 @@ def __init__(self,
12480 self .use_scoop = use_scoop
12581 self .seed = seed
12682 self .map_function = map_function
83+
12784 self .hof = hof
12885 if self .hof is None :
12986 self .hof = deap .tools .HallOfFame (10 )
130- self .fitness_reduce = fitness_reduce
13187
88+ self .fitness_reduce = fitness_reduce
13289 self .offspring_size = offspring_size
13390 self .centroids = centroids
13491 self .sigma = sigma
@@ -162,6 +119,7 @@ def __init__(self,
162119 self .lbounds = numpy .asarray (self .lbounds )
163120 bounds_radius = (self .ubounds - self .lbounds ) / 2.
164121 bounds_mean = (self .ubounds + self .lbounds ) / 2.
122+
165123 self .to_norm = []
166124 self .to_space = []
167125 for r , m in zip (bounds_radius , bounds_mean ):
@@ -200,17 +158,17 @@ def setup_deap(self):
200158 # Register the individual format
201159 self .toolbox .register (
202160 "Individual" ,
203- functools .partial (ListIndividual ,
161+ functools .partial (utils . WSListIndividual ,
204162 obj_size = self .ind_size ,
205163 reduce_fcn = self .fitness_reduce )
206164 )
207165
208166 # A Random Indiviual is create by ListIndividual and parameters are
209167 # initially picked by 'uniform'
210168 self .toolbox .register (
211- "RandomIndividual " ,
169+ "RandomInd " ,
212170 deap .tools .initIterate ,
213- functools .partial (ListIndividual ,
171+ functools .partial (WSListIndividual ,
214172 obj_size = self .ind_size ,
215173 reduce_fcn = self .fitness_reduce ),
216174 self .toolbox .uniformparams )
@@ -220,7 +178,7 @@ def setup_deap(self):
220178 "population" ,
221179 deap .tools .initRepeat ,
222180 list ,
223- self .toolbox .RandomIndividual )
181+ self .toolbox .RandomInd )
224182
225183 # Register the evaluation function for the individuals
226184 self .toolbox .register ("evaluate" , self .evaluator .evaluate_with_lists )
@@ -251,13 +209,14 @@ def run(self,
251209 Args:
252210 max_ngen(int): Total number of generation to run
253211 cp_frequency(int): generations between checkpoints
254- cp_filename(string): path to checkpoint filename
255212 continue_cp(bool): whether to continue
213+ cp_filename(string): path to checkpoint filename
256214 """
257215
258216 stats = self .get_stats ()
259217
260218 if continue_cp :
219+
261220 # A file name has been given, then load the data from the file
262221 cp = pickle .load (open (cp_filename , "br" ))
263222 gen = cp ["generation" ]
@@ -270,17 +229,18 @@ def run(self,
270229 CMA_es .map_function = self .map_function
271230
272231 else :
232+
273233 history = deap .tools .History ()
274234 logbook = deap .tools .Logbook ()
275235 logbook .header = ["gen" , "nevals" ] + stats .fields
276236
277- # Instantiate the CMA strategies centered on the centroids
237+ # Instantiate the CMA strategy centered on the centroids
278238 CMA_es = self .cma_creator (centroids = self .centroids ,
279239 offspring_size = self .offspring_size ,
280240 sigma = self .sigma ,
281241 max_ngen = max_ngen ,
282242 IndCreator = self .toolbox .Individual ,
283- RandIndCreator = self .toolbox .RandomIndividual ,
243+ RandIndCreator = self .toolbox .RandomInd ,
284244 map_function = self .map_function ,
285245 use_scoop = self .use_scoop )
286246
@@ -299,8 +259,9 @@ def run(self,
299259 # Generate the new populations
300260 n_out = CMA_es .generate_new_pop (lbounds = self .lbounds ,
301261 ubounds = self .ubounds )
302- logger .info ("Number of individuals outside of bounds: {} ({:.2f}%)"
303- "" .format (n_out , 100. * n_out / len (CMA_es .population )))
262+ logger .debug ("Number of individuals outside of bounds: {} "
263+ "({:.2f}%)" .format (n_out , 100. *
264+ n_out / len (CMA_es .population )))
304265
305266 # Get all the individuals in the original space for evaluation
306267 to_evaluate = CMA_es .get_population (self .to_space )
@@ -323,8 +284,11 @@ def run(self,
323284 CMA_es .check_termination (gen )
324285
325286 if cp_filename and cp_frequency and gen % cp_frequency == 0 :
287+
288+ # Map function shouldn't be pickled
326289 temp_mf = CMA_es .map_function
327290 CMA_es .map_function = None
291+
328292 cp = dict (population = pop ,
329293 generation = gen ,
330294 halloffame = self .hof ,
@@ -335,6 +299,7 @@ def run(self,
335299 CMA_es = CMA_es )
336300 pickle .dump (cp , open (cp_filename , "wb" ))
337301 logger .debug ('Wrote checkpoint to %s' , cp_filename )
302+
338303 CMA_es .map_function = temp_mf
339304
340305 gen += 1
0 commit comments