Skip to content

Commit

Permalink
store complete state of global optimiser as backup
Browse files Browse the repository at this point in the history
  • Loading branch information
ppietzonka committed Dec 19, 2020
1 parent ac7dcf2 commit 3e63640
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
6 changes: 4 additions & 2 deletions pyross/inference.pyx
Expand Up @@ -1727,7 +1727,7 @@ cdef class SIR_type:
verbose=False, verbose_likelihood=False, ftol=1e-5, global_max_iter=100,
local_max_iter=100, local_initial_step=None, global_atol=1., enable_global=True,
enable_local=True, cma_processes=0, cma_population=16, cma_random_seed=None,
objective='likelihood', alternative_guess=None, tmp_file=None):
objective='likelihood', alternative_guess=None, use_mode_as_guess=False, tmp_file=None, load_backup_file=None):
"""
Compute the maximum a-posteriori (MAP) estimate for the initial conditions and all desired parameters, including control parameters,
for a SIR type model with partially observed classes. The unobserved classes are treated as latent variables.
Expand Down Expand Up @@ -1792,6 +1792,8 @@ cdef class SIR_type:
Array in the same format as 'flat_params' in the result dictionary of a previous optimisation run.
tmp_file: optional, string
If specified, name of a file to store the temporary best estimate of the global optimiser (as backup or for inspection) as numpy array file
load_backup_file: optional, string
If specified, name of a file to restore the the state of the global optimiser
Returns
-------
Expand Down Expand Up @@ -1920,7 +1922,7 @@ cdef class SIR_type:
cma_processes=cma_processes,
cma_population=cma_population, cma_stds=cma_stds,
verbose=verbose, cma_random_seed=cma_random_seed,
args_dict=minimize_args, tmp_file=tmp_file)
args_dict=minimize_args, tmp_file=tmp_file, load_backup_file=load_backup_file)
estimates = res[0]

# Get the parameters (in their original structure) from the flattened parameter vector.
Expand Down
27 changes: 21 additions & 6 deletions pyross/utils_python.py
Expand Up @@ -7,6 +7,7 @@
import cma
import sys
import traceback
import pickle
from scipy.stats import truncnorm, lognorm
from itertools import product

Expand All @@ -20,7 +21,7 @@ def minimization(objective_fct, guess, bounds, global_max_iter=100,
local_max_iter=100, ftol=1e-2, global_atol=1,
enable_global=True, enable_local=True, local_initial_step=None,
cma_processes=0, cma_population=16, cma_stds=None,
cma_random_seed=None, verbose=True, tmp_file=None, args_dict={}):
cma_random_seed=None, verbose=True, tmp_file=None, load_backup_file=None, args_dict={}):
""" Compute the global minimum of the objective function.
This function computes the global minimum of `objective_fct` using a combination of a global minimisation step
Expand Down Expand Up @@ -63,7 +64,9 @@ def minimization(objective_fct, guess, bounds, global_max_iter=100,
verbose: bool
Enable output.
tmp_file: optional, string
If specified, name of a file to store the temporary best estimate of the global optimiser (as backup or for inspection) as numpy array file
If specified, name of a file to store the state of the optimiser as backup
load_backup_file: optional, string
If specified, name of a file to restore the the state of the optimiser
args_dict: dict
Key-word arguments that are passed to the minimisation function.
Expand All @@ -79,7 +82,10 @@ def minimization(objective_fct, guess, bounds, global_max_iter=100,
# Step 1: Global optimisation
if enable_global:
if verbose:
print('Starting global minimisation...')
if load_backup_file is None:
print('Starting global minimisation ...')
else:
print('Continuing global minimisation from backup ...')

if not pathos_mp and cma_processes != 1:
print('Warning: Optional dependecy for multiprocessing support `pathos` not installed.')
Expand All @@ -100,8 +106,16 @@ def minimization(objective_fct, guess, bounds, global_max_iter=100,
if cma_random_seed is None:
cma_random_seed = np.random.randint(2**32-2)
options['seed'] = cma_random_seed

global_opt = cma.CMAEvolutionStrategy(guess, 1.0, options)
if load_backup_file is None:
global_opt = cma.CMAEvolutionStrategy(guess, 1.0, options)
else:
try:
s = open(load_backup_file, 'rb').read()
global_opt = pickle.loads(s)
except:
print('Backup file not found or invalid, starting new optimisation')
global_opt = cma.CMAEvolutionStrategy(guess, 1.0, options)

iteration = 0
while not global_opt.stop() and iteration < global_max_iter:
positions = global_opt.ask()
Expand All @@ -123,7 +137,8 @@ def minimization(objective_fct, guess, bounds, global_max_iter=100,
values = _take_global_optimisation_step(positions, objective_fct, cma_processes, **args_dict)
global_opt.tell(positions, values)
if tmp_file is not None:
np.save(tmp_file, global_opt.best.x)
s = global_opt.pickle_dumps()
open(tmp_file, 'wb').write(s)
if verbose:
global_opt.disp()
iteration += 1
Expand Down

0 comments on commit 3e63640

Please sign in to comment.