11"""Multi Objective CMA-es class"""
22
33"""
4- Copyright (c) 2016, EPFL/Blue Brain Project
4+ Copyright (c) 2016-2020 , EPFL/Blue Brain Project
55
66 This file is part of BluePyOpt <https://github.com/BlueBrain/BluePyOpt>
77
3434from . import utils
3535from . import hype
3636
37- logger = logging .getLogger (' __main__' )
37+ logger = logging .getLogger (" __main__" )
3838
3939
4040def get_hyped (pop ):
4141 # Cap the obj at 250
4242 points = numpy .array ([ind .fitness .values for ind in pop ])
43- points [points > 250. ] = 250.
43+ points [points > 250.0 ] = 250.0
4444 lbounds = numpy .min (points , axis = 0 )
4545 ubounds = numpy .max (points , axis = 0 )
4646
@@ -55,28 +55,29 @@ def get_hyped(pop):
5555
5656 # Rescale the objective space
5757 points = (points - lbounds ) / numpy .max (ubounds .flatten ())
58- ubounds = numpy .max (points , axis = 0 ) + 2.
58+ ubounds = numpy .max (points , axis = 0 ) + 2.0
5959
60- hv = hype .hypeIndicatorSampled (points = points ,
61- bounds = ubounds ,
62- k = 5 ,
63- nrOfSamples = 200000 )
60+ hv = hype .hypeIndicatorSampled (
61+ points = points , bounds = ubounds , k = 5 , nrOfSamples = 200000
62+ )
6463 return hv
6564
6665
6766class CMA_MO (cma .StrategyMultiObjective ):
6867 """Multiple objective covariance matrix adaption"""
6968
70- def __init__ (self ,
71- centroids ,
72- offspring_size ,
73- sigma ,
74- max_ngen ,
75- IndCreator ,
76- RandIndCreator ,
77- weight_hv = 0.5 ,
78- map_function = None ,
79- use_scoop = False ):
69+ def __init__ (
70+ self ,
71+ centroids ,
72+ offspring_size ,
73+ sigma ,
74+ max_ngen ,
75+ IndCreator ,
76+ RandIndCreator ,
77+ weight_hv = 0.5 ,
78+ map_function = None ,
79+ use_scoop = False ,
80+ ):
8081 """Constructor
8182
8283 Args:
@@ -85,8 +86,8 @@ def __init__(self,
8586 sigma (float): initial standard deviation of the distribution
8687 max_ngen (int): total number of generation to run
8788 IndCreator (fcn): function returning an individual of the pop
88- weight_hv (float): between 0 and 1. Weight given to the
89- hypervolume contribution when computing the score of an
89+ weight_hv (float): between 0 and 1. Weight given to the
90+ hypervolume contribution when computing the score of an
9091 individual in MO-CMA. The weight of the fitness contribution
9192 is computed as 1 - weight_hv.
9293 """
@@ -101,18 +102,19 @@ def __init__(self,
101102 else :
102103 if len (centroids ) != lambda_ :
103104 from itertools import cycle
105+
104106 generator = cycle (centroids )
105107 starters = [next (generator ) for i in range (lambda_ )]
106108 else :
107109 starters = centroids
108110
109- cma .StrategyMultiObjective .__init__ (self , starters , sigma ,
110- mu = int (lambda_ * 0.5 ),
111- lambda_ = lambda_ )
111+ cma .StrategyMultiObjective .__init__ (
112+ self , starters , sigma , mu = int (lambda_ * 0.5 ), lambda_ = lambda_
113+ )
112114
113115 self .population = []
114116 self .problem_size = len (starters [0 ])
115-
117+
116118 self .weight_hv = weight_hv
117119
118120 self .map_function = map_function
@@ -126,16 +128,19 @@ def __init__(self,
126128 if self .use_scoop :
127129 if self .map_function :
128130 raise Exception (
129- 'Impossible to use scoop is providing self defined map '
130- 'function: %s' % self .map_function )
131+ "Impossible to use scoop is providing self defined map "
132+ "function: %s" % self .map_function
133+ )
131134 from scoop import futures
135+
132136 self .map_function = futures .map
133137
134138 # Set termination conditions
135139 self .active = True
136140 if max_ngen <= 0 :
137141 max_ngen = 100 + 50 * (self .problem_size + 3 ) ** 2 / numpy .sqrt (
138- self .lambda_ )
142+ self .lambda_
143+ )
139144
140145 self .stopping_conditions = [MaxNGen (max_ngen )]
141146
@@ -146,29 +151,30 @@ def _select(self, candidates):
146151 absolute fitness and hyper-volume contribution.
147152 """
148153
149- if self .weight_hv == 0. :
154+ if self .weight_hv == 0.0 :
150155 fit = [numpy .sum (ind .fitness .values ) for ind in candidates ]
151156 idx_fit = list (numpy .argsort (fit ))
152157 idx_scores = idx_fit [:]
153158
154- elif self .weight_hv == 1. :
159+ elif self .weight_hv == 1.0 :
155160 hv = get_hyped (candidates )
156- idx_hv = list (numpy .argsort (hv ))[::- 1 ]
157- idx_scores = idx_hv [:]
161+ idx_hv = list (numpy .argsort (hv ))[::- 1 ]
162+ idx_scores = idx_hv [:]
158163
159164 else :
160165 hv = get_hyped (candidates )
161- idx_hv = list (numpy .argsort (hv ))[::- 1 ]
166+ idx_hv = list (numpy .argsort (hv ))[::- 1 ]
162167 fit = [numpy .sum (ind .fitness .values ) for ind in candidates ]
163168 idx_fit = list (numpy .argsort (fit ))
164169 scores = []
165170 for i in range (len (candidates )):
166- score = (self .weight_hv * idx_hv .index (i )) + \
167- ((1. - self .weight_hv ) * idx_fit .index (i ))
171+ score = (self .weight_hv * idx_hv .index (i )) + (
172+ (1.0 - self .weight_hv ) * idx_fit .index (i )
173+ )
168174 scores .append (score )
169175 idx_scores = list (numpy .argsort (scores ))
170176
171- chosen = [candidates [i ] for i in idx_scores [:self .mu ]]
177+ chosen = [candidates [i ] for i in idx_scores [: self .mu ]]
172178 not_chosen = [candidates [i ] for i in idx_scores [self .mu :]]
173179 return chosen , not_chosen
174180
@@ -213,6 +219,8 @@ def check_termination(self, gen):
213219 [c .check (stopping_params ) for c in self .stopping_conditions ]
214220 for c in self .stopping_conditions :
215221 if c .criteria_met :
216- logger .info ('CMA stopped because of termination criteria: ' +
217- ' ' .join (c .name ))
222+ logger .info (
223+ "CMA stopped because of termination criteria: " +
224+ "" + " " .join (c .name )
225+ )
218226 self .active = False
0 commit comments