Skip to content
This repository was archived by the owner on Feb 26, 2025. It is now read-only.

Commit 62a98f1

Browse files
author
damart
committed
Syntax
1 parent c60bfc7 commit 62a98f1

File tree

6 files changed

+51
-50
lines changed

6 files changed

+51
-50
lines changed

bluepyopt/deapext/CMA_SO.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
2020
"""
2121

22+
# pylint: disable=R0912, R0914
23+
2224
import logging
2325
import numpy
2426
from math import sqrt, log
@@ -110,32 +112,29 @@ def update(self, population):
110112
c_diff = self.centroid - old_centroid
111113

112114
# Cumulation : update evolution path
113-
self.ps = (1 - self.cs) * self.ps \
114-
+ sqrt(self.cs * (2 - self.cs) * self.mueff) / self.sigma \
115-
* numpy.dot(self.B, (1. / self.diagD) *
116-
numpy.dot(self.B.T, c_diff))
115+
self.ps = (1 - self.cs) * self.ps + sqrt(self.cs * (2 - self.cs) *
116+
self.mueff) / self.sigma * numpy.dot(self.B, (1. /
117+
self.diagD) * numpy.dot(self.B.T, c_diff)) # noqa
117118

118-
hsig = float((numpy.linalg.norm(self.ps) /
119-
sqrt(1. - (1. - self.cs) ** (
120-
2. * (self.update_count + 1.))) / self.chiN <
121-
(1.4 + 2. / (self.dim + 1.))))
119+
hsig = float((numpy.linalg.norm(self.ps) / sqrt(1. - (1. - self.cs)
120+
** (2. * (self.update_count + 1.))) / self.chiN <
121+
(1.4 + 2. / (self.dim + 1.)))) # noqa
122122

123123
self.update_count += 1
124124

125-
self.pc = (1 - self.cc) * self.pc + hsig \
126-
* sqrt(self.cc * (2 - self.cc) * self.mueff) / self.sigma \
127-
* c_diff
125+
self.pc = (1 - self.cc) * self.pc + hsig * sqrt(self.cc * (2 - self.cc)
126+
* self.mueff) / self.sigma * c_diff # noqa
128127

129128
# Update covariance matrix
130129
artmp = population[0:self.mu] - old_centroid
131130
self.C = (1 - self.ccov1 - self.ccovmu + (1 - hsig) *
132131
self.ccov1 * self.cc * (2 - self.cc)) * self.C \
133132
+ self.ccov1 * numpy.outer(self.pc, self.pc) \
134133
+ self.ccovmu * numpy.dot((self.weights * artmp.T), artmp) \
135-
/ self.sigma ** 2
134+
/ self.sigma ** 2 # noqa
136135

137136
self.sigma *= numpy.exp((numpy.linalg.norm(self.ps) / self.chiN - 1.) *
138-
self.cs / self.damps)
137+
self.cs / self.damps) # noqa
139138

140139
self.diagD, self.B = numpy.linalg.eigh(self.C)
141140
indx = numpy.argsort(self.diagD)

bluepyopt/deapext/algorithms.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,11 @@ def eaAlphaMuPlusLambdaCheckpoint(
141141

142142
invalid_count = _evaluate_invalid_fitness(toolbox, population)
143143
utils.update_history_and_hof(halloffame, history, population)
144-
utils.record_stats(stats, logbook, start_gen, population, invalid_count)
144+
utils.record_stats(stats,
145+
logbook,
146+
start_gen,
147+
population,
148+
invalid_count)
145149

146150
stopping_criteria = [MaxNGen(ngen)]
147151

bluepyopt/deapext/optimisations.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,8 @@ def setup_deap(self):
171171
UPPER.append(parameter.upper_bound)
172172

173173
# Register the 'uniform' function
174-
self.toolbox.register("uniformparams", utils.uniform, LOWER, UPPER, IND_SIZE)
174+
self.toolbox.register("uniformparams", utils.uniform, LOWER, UPPER,
175+
IND_SIZE)
175176

176177
# Register the individual format
177178
# An indiviual is create by WSListIndividual and parameters

bluepyopt/deapext/optimisationsCMA.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ def _ind_convert_space(ind, convert_fcn):
8888

8989

9090
class DEAPOptimisationCMA(bluepyopt.optimisations.Optimisation):
91-
9291
"""CMA based optimisation class"""
9392

9493
def __init__(self,
@@ -140,8 +139,8 @@ def __init__(self,
140139
elif self.selector_name == 'multi_objective':
141140
self.cma_creator = CMA_MO
142141
else:
143-
raise Exception("The selector_name has to be 'single_objective' or "
144-
"'multi_objective'. Not "
142+
raise Exception("The selector_name has to be 'single_objective' "
143+
"or 'multi_objective'. Not "
145144
"{}".format(self.selector_name))
146145

147146
# Number of objective values
@@ -167,21 +166,22 @@ def __init__(self,
167166
self.to_space = []
168167
for r, m in zip(bounds_radius, bounds_mean):
169168
self.to_norm.append(
170-
functools.partial(lambda param, bm, br: (param - bm) / br, bm=m, br=r))
169+
functools.partial(lambda param, bm, br: (param - bm) / br,
170+
bm=m, br=r))
171171
self.to_space.append(
172-
functools.partial(lambda param, bm, br: (param * br) + bm, bm=m, br=r))
172+
functools.partial(lambda param, bm, br: (param * br) + bm,
173+
bm=m, br=r))
173174

174175
# Overwrite the bounds with -1. and 1.
175176
self.lbounds = numpy.full(self.problem_size, -1.)
176177
self.ubounds = numpy.full(self.problem_size, 1.)
177-
178+
178179
self.setup_deap()
179180

180181
# In case initial guesses were provided, rescale them to the norm space
181182
if self.centroids is not None:
182-
self.centroids = [self.toolbox.Individual(_ind_convert_space(ind,
183-
self.to_norm))
184-
for ind in centroids]
183+
self.centroids = [self.toolbox.Individual(
184+
_ind_convert_space(ind, self.to_norm)) for ind in centroids]
185185

186186
def setup_deap(self):
187187
"""Set up optimisation"""
@@ -191,8 +191,8 @@ def setup_deap(self):
191191
numpy.random.seed(self.seed)
192192

193193
# Register the 'uniform' function
194-
self.toolbox.register("uniformparams",
195-
utils.uniform,
194+
self.toolbox.register("uniformparams",
195+
utils.uniform,
196196
self.lbounds,
197197
self.ubounds,
198198
self.ind_size)
@@ -224,7 +224,7 @@ def setup_deap(self):
224224

225225
# Register the evaluation function for the individuals
226226
self.toolbox.register("evaluate", self.evaluator.evaluate_with_lists)
227-
227+
228228
import copyreg
229229
import types
230230
copyreg.pickle(types.MethodType, utils.reduce_method)
@@ -268,7 +268,7 @@ def run(self,
268268
numpy.random.set_state(cp["np_rndstate"])
269269
CMA_es = cp["CMA_es"]
270270
CMA_es.map_function = self.map_function
271-
271+
272272
else:
273273
history = deap.tools.History()
274274
logbook = deap.tools.Logbook()
@@ -291,7 +291,7 @@ def run(self,
291291
CMA_es.set_fitness_parents(fitness)
292292

293293
gen = 1
294-
294+
295295
# Run until a termination criteria is met
296296
while CMA_es.active:
297297
logger.info("Generation {}".format(gen))
@@ -338,9 +338,8 @@ def run(self,
338338
CMA_es.map_function = temp_mf
339339

340340
gen += 1
341-
342-
return pop, self.hof, logbook, history
343341

342+
return pop, self.hof, logbook, history
344343

345344
def get_stats(self):
346345
"""Get the stats that will be saved during optimisation"""

bluepyopt/deapext/stoppingCriteria.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@
2626

2727
logger = logging.getLogger('__main__')
2828

29+
2930
def isclose(a, b, rel_tol=1e-09, abs_tol=0.0):
30-
return abs(a-b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol)
31+
return abs(a - b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol)
3132

3233

3334
class MaxNGen(bluepyopt.stoppingCriteria.StoppingCriteria):
@@ -73,12 +74,13 @@ def check(self, kwargs):
7374
self.stagnation_iter = int(numpy.ceil(
7475
0.2 * ngen + 120 + 30. * self.problem_size / self.lambda_))
7576

76-
if len(self.best) > self.stagnation_iter and \
77-
len(self.median) > self.stagnation_iter and \
78-
numpy.median(self.best[-20:]) >= numpy.median(
79-
self.best[-self.stagnation_iter:-self.stagnation_iter + 20]) and \
80-
numpy.median(self.median[-20:]) >= numpy.median(
81-
self.median[-self.stagnation_iter:-self.stagnation_iter + 20]):
77+
cbest = len(self.best) > self.stagnation_iter
78+
cmed = len(self.median) > self.stagnation_iter
79+
cbest2 = numpy.median(self.best[-20:]) >= numpy.median(
80+
self.best[-self.stagnation_iter:-self.stagnation_iter + 20])
81+
cmed2 = numpy.median(self.median[-20:]) >= numpy.median(
82+
self.median[-self.stagnation_iter:-self.stagnation_iter + 20])
83+
if cbest and cmed and cbest2 and cmed2:
8284
self.criteria_met = True
8385

8486

bluepyopt/tests/test_CMA/test_optimisationsCMA.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,22 @@ def test_optimisationsCMA_normspace():
1616
optimisation = bluepyopt.deapext.optimisationsCMA.DEAPOptimisationCMA(
1717
evaluator=evaluator)
1818

19-
x = [n*0.1 for n in range(len(evaluator.params))]
20-
y = [f2(f1(_)) for _,f1,f2 in zip(x, optimisation.to_norm,
21-
optimisation.to_space)]
19+
x = [n * 0.1 for n in range(len(evaluator.params))]
20+
y = [f2(f1(_)) for _, f1, f2 in zip(x, optimisation.to_norm,
21+
optimisation.to_space)]
2222
for a, b in zip(x, y):
2323
nt.assert_almost_equal(a, b)
2424

25+
2526
@attr('unit')
2627
def test_optimisationsCMA_run():
2728
"deapext.optimisationsCMA: Testing optimisationsCMA run from centroid"
2829

2930
evaluator = examples.simplecell.cell_evaluator
3031
x = [n * 0.1 for n in range(len(evaluator.params))]
3132

32-
try:
33-
optimisation = bluepyopt.deapext.optimisationsCMA.DEAPOptimisationCMA(
34-
evaluator=evaluator,
35-
centroids=[x])
36-
pop, hof, log, hist = optimisation.run(max_ngen=2)
37-
raised = False
38-
except:
39-
raised = True
40-
33+
optimiser = bluepyopt.deapext.optimisationsCMA.DEAPOptimisationCMA
34+
optimisation = optimiser(evaluator=evaluator, centroids=[x])
35+
pop, hof, log, hist = optimisation.run(max_ngen=2)
36+
raised = False
4137
nt.assert_equal(raised, False)

0 commit comments

Comments
 (0)