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

Commit ecc92f1

Browse files
Merge pull request #460 from BlueBrain/save-frequency
possibility of using a minimum amount of time before saving optimisation
2 parents a15387c + 93555bd commit ecc92f1

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

bluepyopt/deapext/algorithms.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import logging
2727
import shutil
2828
import os
29+
import time
2930

3031
import deap.algorithms
3132
import deap.tools
@@ -92,6 +93,7 @@ def eaAlphaMuPlusLambdaCheckpoint(
9293
stats=None,
9394
halloffame=None,
9495
cp_frequency=1,
96+
cp_period=None,
9597
cp_filename=None,
9698
continue_cp=False,
9799
terminator=None,
@@ -108,6 +110,8 @@ def eaAlphaMuPlusLambdaCheckpoint(
108110
stats(deap.tools.Statistics): generation of statistics
109111
halloffame(deap.tools.HallOfFame): hall of fame
110112
cp_frequency(int): generations between checkpoints
113+
cp_period(float): minimum time (in s) between checkpoint.
114+
None to save checkpoint independently of the time between them
111115
cp_filename(string): path to checkpoint filename
112116
continue_cp(bool): whether to continue
113117
terminator (multiprocessing.Event): exit loop when is set.
@@ -159,6 +163,7 @@ def eaAlphaMuPlusLambdaCheckpoint(
159163
# Begin the generational process
160164
gen = start_gen + 1
161165
stopping_params = {"gen": gen}
166+
time_last_save = time.time()
162167
while utils.run_next_gen(
163168
not (_check_stopping_criteria(stopping_criteria, stopping_params)),
164169
terminator):
@@ -176,7 +181,8 @@ def eaAlphaMuPlusLambdaCheckpoint(
176181
logger.info(logbook.stream)
177182

178183
if (cp_filename and cp_frequency and
179-
gen % cp_frequency == 0):
184+
gen % cp_frequency == 0 and
185+
(cp_period is None or time.time() - time_last_save > cp_period)):
180186
cp = dict(population=population,
181187
generation=gen,
182188
parents=parents,
@@ -190,6 +196,8 @@ def eaAlphaMuPlusLambdaCheckpoint(
190196
shutil.copy(cp_filename_tmp, cp_filename)
191197
logger.debug('Wrote checkpoint to %s', cp_filename)
192198

199+
time_last_save = time.time()
200+
193201
gen += 1
194202
stopping_params["gen"] = gen
195203

bluepyopt/deapext/optimisations.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ def run(self,
256256
continue_cp=False,
257257
cp_filename=None,
258258
cp_frequency=1,
259+
cp_period=None,
259260
parent_population=None,
260261
terminator=None):
261262
"""Run optimisation"""
@@ -315,6 +316,7 @@ def run(self,
315316
stats=stats,
316317
halloffame=self.hof,
317318
cp_frequency=cp_frequency,
319+
cp_period=None,
318320
continue_cp=continue_cp,
319321
cp_filename=cp_filename,
320322
terminator=terminator,

bluepyopt/deapext/optimisationsCMA.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import functools
2727
import shutil
2828
import os
29+
import time
2930

3031
import deap.tools
3132

@@ -243,6 +244,7 @@ def run(
243244
self,
244245
max_ngen=0,
245246
cp_frequency=1,
247+
cp_period=None,
246248
continue_cp=False,
247249
cp_filename=None,
248250
terminator=None,
@@ -252,6 +254,8 @@ def run(
252254
Args:
253255
max_ngen(int): Total number of generation to run
254256
cp_frequency(int): generations between checkpoints
257+
cp_period(float): minimum time (in s) between checkpoint.
258+
None to save checkpoint independently of the time between them
255259
continue_cp(bool): whether to continue
256260
cp_filename(string): path to checkpoint filename
257261
terminator (multiprocessing.Event): exit loop when is set.
@@ -309,6 +313,7 @@ def run(
309313
if hasattr(self.evaluator, "param_names"):
310314
param_names = self.evaluator.param_names
311315

316+
time_last_save = time.time()
312317
# Run until a termination criteria is met
313318
while utils.run_next_gen(CMA_es.active, terminator):
314319
logger.info("Generation {}".format(gen))
@@ -343,8 +348,12 @@ def run(
343348
# termination conditions were reached
344349
CMA_es.update_strategy()
345350
CMA_es.check_termination(gen)
346-
347-
if cp_filename and cp_frequency and gen % cp_frequency == 0:
351+
if (
352+
cp_filename and
353+
cp_frequency and
354+
gen % cp_frequency == 0 and
355+
(cp_period is None or time.time() - time_last_save > cp_period)
356+
):
348357

349358
# Map function shouldn't be pickled
350359
temp_mf = CMA_es.map_function
@@ -368,6 +377,8 @@ def run(
368377

369378
CMA_es.map_function = temp_mf
370379

380+
time_last_save = time.time()
381+
371382
gen += 1
372383

373384
return pop, self.hof, logbook, history

0 commit comments

Comments
 (0)