work with checkpoint files
checkpoint for restarting
a checkpoint file contains data about the optimization process. to obtain a checkpoint
file, use the checkpointfile
option.
one basic use of a checkpoint file is to resume an optimization when it stops prematurely. the cause of the premature stopping can be events such as a power failure or a crash, or when you press the stop button in a plot function window.
whatever the reason for the premature stopping, the restart procedure is simply to call
surrogateopt
with the checkpoint file name.
for example, suppose that you run an optimization with the 'check1'
checkpoint file, and then click the stop button soon after the
optimization starts.
options = optimoptions('surrogateopt','checkpointfile','check1.mat'); lb = [-6,-8]; ub = -lb; fun = @(x)100*(x(2) - x(1)^2)^2 (1 - x(1))^2; [x,fval,exitflag,output] = surrogateopt(fun,lb,ub,options)
optimization stopped by a plot function or output function. x = 0 0 fval = 1 exitflag = -1 output = struct with fields: elapsedtime: 15.3330 funccount: 30 constrviolation: 0 ineq: [1×0 double] rngstate: [1×1 struct] message: 'optimization stopped by a plot function or output function.'
note
checkpointing takes time. this overhead is especially noticeable for functions that otherwise take little time to evaluate.
to resume the optimization, call surrogateopt
with the
'check1.mat'
argument.
[x,fval,exitflag,output] = surrogateopt('check1.mat')
surrogateopt stopped because it exceeded the function evaluation limit set by 'options.maxfunctionevaluations'. x = 1.0186 1.0377 fval = 3.4902e-04 exitflag = 0 output = struct with fields: elapsedtime: 181.5824 funccount: 200 constrviolation: 0 ineq: [1×0 double] rngstate: [1×1 struct] message: 'surrogateopt stopped because it exceeded the function evaluation limit set by ↵'options.maxfunctionevaluations'.'
change options to extend or monitor optimization
you can extend an optimization, whether it stops due to an unforeseen event or not, by changing the stopping criteria in the options. you can also monitor the optimization by displaying information at each iteration.
note
surrogateopt
allows you to change only a limited set of options.
for reliability, update the original options structure instead of creating new
options.
for a list of the options you can change when restarting, see .
for example, suppose that you want to extend the previous optimization to run for a
total of 400 function evaluations. additionally, you want to monitor the optimization using
the 'surrogateoptplot'
plot function.
opts = optimoptions(options,'maxfunctionevaluations',400,... 'plotfcn','surrogateoptplot'); [x,fval,exitflag,output] = surrogateopt('check1.mat',opts)
surrogateopt stopped because it exceeded the function evaluation limit set by 'options.maxfunctionevaluations'. x = 1.0186 1.0377 fval = 3.4902e-04 exitflag = 0 output = struct with fields: elapsedtime: 959.7619 funccount: 400 constrviolation: 0 ineq: [1×0 double] rngstate: [1×1 struct] message: 'surrogateopt stopped because it exceeded the function evaluation limit set by ↵'options.maxfunctionevaluations'.'
the new plot function plots from the beginning of the optimization, even though you
started the plot function only after the solver stopped at function evaluation number 200.
the 'surrogateoptplot'
plot function also shows the evaluation numbers
where the optimization stopped and where it restarted from the checkpoint file.
code for robust surrogate optimization
to restart a surrogate optimization from a checkpoint file only if the file exists, use the following code logic. in this way, you can write scripts to keep an optimization going, even after a crash or other unexpected event.
% assume that myfun, lb, and ub exist if isfile('saveddata.mat') [x,fval,exitflag,output] = surrogateopt('saveddata.mat'); else options = optimoptions("surrogateopt","checkpointfile",'saveddata.mat'); [x,fval,exitflag,output] = surrogateopt(myfun,lb,ub,options); end