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');
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:

       rngstate: [1×1 struct]
      funccount: 27
    elapsedtime: 56.0377
        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' argument.

[x,fval,exitflag,output] = surrogateopt('check1')
Surrogateopt stopped because it exceeded the function evaluation limit set by 
'options.MaxFunctionEvaluations'.

x =

    1.0009    1.0010


fval =

   4.8378e-05


exitflag =

     0


output = 

  struct with fields:

       rngstate: [1×1 struct]
      funccount: 200
    elapsedtime: 487.7058
        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 opts.

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',opts)
Surrogateopt stopped because it exceeded the function evaluation limit set by 
'options.MaxFunctionEvaluations'.

x =

    1.0009    1.0010


fval =

   4.8378e-05


exitflag =

     0


output = 

  struct with fields:

       rngstate: [1×1 struct]
      funccount: 400
    elapsedtime: 1.1340e+03
        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');
else
    options = optimoptions("surrogateopt","CheckpointFile",'saveddata');
    [x,fval,exitflag,output] = surrogateopt(myfun,lb,ub,options);
end

See Also

Related Topics