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

Commit c60bfc7

Browse files
author
Tanguy Damart
committed
Fix StoppingCriteria
1 parent 8d6cfb8 commit c60bfc7

File tree

6 files changed

+18
-18
lines changed

6 files changed

+18
-18
lines changed

bluepyopt/deapext/CMA_MO.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,9 @@ def set_fitness_parents(self, fitnesses):
223223
for f, ind in zip(fitnesses, self.parents):
224224
ind.fitness.values = f
225225

226-
def check_termination(self, ngen):
226+
def check_termination(self, gen):
227227
stopping_params = {
228-
"ngen": ngen,
228+
"gen": gen,
229229
"population": self.population,
230230
}
231231

bluepyopt/deapext/CMA_SO.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,9 @@ def set_fitness(self, fitnesses):
166166
for f, ind in zip(fitnesses, self.population):
167167
ind.fitness.values = f
168168

169-
def check_termination(self, ngen):
169+
def check_termination(self, gen):
170170
stopping_params = {
171-
"ngen": ngen,
171+
"gen": gen,
172172
"population": self.population,
173173
"centroid": self.centroid,
174174
"pc": self.pc,

bluepyopt/deapext/algorithms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def eaAlphaMuPlusLambdaCheckpoint(
154154
population = parents + offspring
155155

156156
invalid_count = _evaluate_invalid_fitness(toolbox, offspring)
157-
utils.history_and_hof(halloffame, history, population)
157+
utils.update_history_and_hof(halloffame, history, population)
158158
utils.record_stats(stats, logbook, gen, population, invalid_count)
159159

160160
# Select the next generation parents

bluepyopt/deapext/optimisationsCMA.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -174,16 +174,15 @@ def __init__(self,
174174
# Overwrite the bounds with -1. and 1.
175175
self.lbounds = numpy.full(self.problem_size, -1.)
176176
self.ubounds = numpy.full(self.problem_size, 1.)
177+
178+
self.setup_deap()
177179

178180
# In case initial guesses were provided, rescale them to the norm space
179181
if self.centroids is not None:
180182
self.centroids = [self.toolbox.Individual(_ind_convert_space(ind,
181183
self.to_norm))
182184
for ind in centroids]
183185

184-
self.setup_deap()
185-
186-
187186
def setup_deap(self):
188187
"""Set up optimisation"""
189188

@@ -269,7 +268,7 @@ def run(self,
269268
numpy.random.set_state(cp["np_rndstate"])
270269
CMA_es = cp["CMA_es"]
271270
CMA_es.map_function = self.map_function
272-
271+
273272
else:
274273
history = deap.tools.History()
275274
logbook = deap.tools.Logbook()
@@ -292,7 +291,7 @@ def run(self,
292291
CMA_es.set_fitness_parents(fitness)
293292

294293
gen = 1
295-
294+
296295
# Run until a termination criteria is met
297296
while CMA_es.active:
298297
logger.info("Generation {}".format(gen))
@@ -339,7 +338,7 @@ def run(self,
339338
CMA_es.map_function = temp_mf
340339

341340
gen += 1
342-
341+
343342
return pop, self.hof, logbook, history
344343

345344

bluepyopt/deapext/stoppingCriteria.py

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

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

29+
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+
2932

3033
class MaxNGen(bluepyopt.stoppingCriteria.StoppingCriteria):
3134
"""Max ngen stopping criteria class"""
@@ -39,7 +42,6 @@ def __init__(self, max_ngen):
3942
def check(self, kwargs):
4043
"""Check if the maximum number of iteration is reached"""
4144
gen = kwargs.get("gen")
42-
4345
if gen > self.max_ngen:
4446
self.criteria_met = True
4547

@@ -61,7 +63,7 @@ def __init__(self, lambda_, problem_size):
6163

6264
def check(self, kwargs):
6365
"""Check if the population stopped improving"""
64-
ngen = kwargs.get("ngen")
66+
ngen = kwargs.get("gen")
6567
population = kwargs.get("population")
6668
fitness = [ind.fitness.reduce for ind in population]
6769
fitness.sort()
@@ -117,7 +119,7 @@ def __init__(self, lambda_, problem_size):
117119
def check(self, kwargs):
118120
"""Check if in 1/3rd of the last problem_size iterations the best and
119121
k'th best solutions are equal"""
120-
ngen = kwargs.get("ngen")
122+
ngen = kwargs.get("gen")
121123
population = kwargs.get("population")
122124

123125
fitness = [ind.fitness.reduce for ind in population]
@@ -201,7 +203,7 @@ def __init__(self, problem_size):
201203

202204
def check(self, kwargs):
203205
"""Check if the coordinate axis std is too low"""
204-
ngen = kwargs.get("ngen")
206+
ngen = kwargs.get("gen")
205207
centroid = kwargs.get("centroid")
206208
sigma = kwargs.get("sigma")
207209
diagD = kwargs.get("diagD")

bluepyopt/tests/test_CMA/test_optimisationsCMA.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import nose.tools as nt
44

5-
import bluepyopt.optimisations
5+
import bluepyopt
66
import bluepyopt.ephys.examples as examples
77

88
from nose.plugins.attrib import attr
@@ -33,8 +33,7 @@ def test_optimisationsCMA_run():
3333
optimisation = bluepyopt.deapext.optimisationsCMA.DEAPOptimisationCMA(
3434
evaluator=evaluator,
3535
centroids=[x])
36-
37-
pop, hof, log, hist = optimisation.run(max_ngen=1)
36+
pop, hof, log, hist = optimisation.run(max_ngen=2)
3837
raised = False
3938
except:
4039
raised = True

0 commit comments

Comments
 (0)