Main Content

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: 6.7739
          funccount: 18
    constrviolation: 0
               ineq: [1×0 double]
           rngstate: [1×1 struct]
            message: 'Optimization stopped by a plot function or output function.'

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 =

    0.8299    0.6861


fval =

    0.0296


exitflag =

     0


output = 

  struct with fields:

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

x =

    1.0369    1.0758


fval =

    0.0014


exitflag =

     0


output = 

  struct with fields:

        elapsedtime: 177.8927
          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

See Also

Related Topics