MATLAB Examples

Improving Optimization Performance Using Fast Restart (Code)

This example shows how to use the Fast Restart feature of Simulink® to speed up optimization of a model. You use Fast Restart to estimate the parameters of an engine throttle model.


How Fast Restart Speeds up the Optimization

Simulation of Simulink models requires that the model be compiled before it is simulated. In this context compilation of a model means analyzing and formatting the model so that it can be simulated. The idea of Fast Restart is to perform the model compilation once and reuse the compiled information for subsequent simulations, see "How Fast Restart Improves Iterative Simulations" in the Simulink documentation for a description of Fast Restart.

During optimization the model is repeatedly simulated (often tens or hundreds of times) Fast Restart means that the model is only compiled once for these simulation in comparison to non-fast restart where the model is recompiled each time.

Models where compilation is a significant portion of overall simulation time benefit the most from Fast Restart. Further once a model is compiled not all model parameters can be changed, specifically only tunable parameters can be changed, see "Factors Affecting Fast Restart" in the Simulink documentation for more information.

Open Model

Load the engine throttle model. The goal is to tune the parameters of the model to match measured data. For details on the problem setup see the "Estimate Model Parameter Values (GUI)" example.


Define the Estimation Problem Data

This examples focuses on the command line interface for using Fast Restart during estimation. For a detailed description of the estimation command line interface see "Estimate Model Parameter Values (Code)".

Specify the model parameter values to estimate and any parameter bounds.

p = sdo.getParameterFromModel('spe_engine_throttle',{'J','c','input_delay','k'});
p(1).Minimum = 0;
p(2).Minimum = 0;
p(3).Minimum = 0;
p(3).Maximum = 0.1;
p(4).Minimum = 0;

Define the estimation experiment. The measured experiment data is loaded from the sdoFastRestart_ExperimentData MATLAB file. The MATLAB file contains a Input_SignalData and Output_SignalData variable specifying the experiment input and output signal data.

load spe_engine_throttle_ExperimentData

Exp = sdo.Experiment('spe_engine_throttle');
Input = Simulink.SimulationData.Signal;
Input.Values    = Input_SignalData;
Input.BlockPath = 'spe_engine_throttle/Input';
Input.PortType  = 'inport';
Input.PortIndex = 1;
Input.Name      = 'spe_engine_throttle/Input:1';
Exp.InputData = Input;
Output = Simulink.SimulationData.Signal;
Output.Values    = Output_SignalData;
Output.BlockPath = 'spe_engine_throttle/Throttle';
Output.PortType  = 'outport';
Output.PortIndex = 1;
Output.Name      = 'spe_engine_throttle/Throttle:1';
Exp.OutputData = Output;

Create a model simulator from the experiment

Simulator = createSimulator(Exp);

Configure the Simulator for Fast Restart

The simulator controls whether the model is simulated using fast restart or not. The fastRestart command is used to configure the simulator to use Fast Restart.

The spe_engine_throttle model uses a variable-step solver, and may not output values at the times in the measured experiment data. To output values at the times of the measured data, use the set_param command to specify the model logging output times as a workspace variable. In the estimation objective function, the variable is then used to specify output times to be the same as the measured experiment data. The model OutputTimes is set before configuring the simulator for fast restart, as once the model is configured for fast restart, the model logging configuration can not change.

Simulator = fastRestart(Simulator,'on');

The simulator can now be used during estimation, and the model will be simulated using fast restart.

Run the Estimation

Create an estimation objective function to evaluate how closely the simulation output, generated using the estimated parameter values, matches the measured data. Use an anonymous function with one argument that calls the spe_engine_throttle_Objective function. The spe_engine_throttle_Objective function includes the Simulator argument that has been configured to use fast restart.

optimfcn = @(P) spe_engine_throttle_Objective(P,Simulator,Exp);

Set the optimization options, and run the optimization.

Options = sdo.OptimizeOptions;
Options.Method = 'lsqnonlin';
[pOpt,Info] = sdo.optimize(optimfcn,p,Options);
 Optimization started 25-Jul-2017 17:41:09

                               Step-size  First-order 
 Iter F-count        f(x)                 optimality
    0      9       32.048            1                                         
    1     18        12.24       0.6495           18
    2     27      3.59416       0.3919         8.65
    3     36      1.11975       0.1879         3.11
    4     45     0.649095       0.1966         1.25
    5     54     0.286711        1.261         1.14
    6     63     0.143419       0.3011        0.411
    7     72    0.0820165       0.5474       0.0941
    8     81    0.0626914       0.2693        0.138
    9     90    0.0626914        8.455        0.138
   10     99    0.0623762        1.734        0.242
   11    108     0.048215       0.2462       0.0745
   12    117    0.0477283       0.4335       0.0321
Local minimum possible.

lsqnonlin stopped because the final change in the sum of squares relative to 
its initial value is less than the selected value of the function tolerance.

Restore the Model

Restore the simulator fast restart settings. This clears the logging and other settings used for the optimization problem.

Simulator = fastRestart(Simulator,'off');

Related Examples

You can also generate code to configure you model for fast restart in the Parameter Estimation and Response Optimization tools. Configure the model for fast restart as described in "Improving Optimization Performance using Fast Restart (GUI)". Then use the Generate MATLAB Code feature of the tool.

Close the model.