Main Content

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. For more information about when to use fast restart, see How Fast Restart Improves Iterative Simulations.

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. For more information, see Get Started with Fast Restart.

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 spe_engine_throttle_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 2023-Aug-19, 16:08:43

 Iter F-count        f(x)      Step-size  optimality
    0      9       32.048            1
    1     18        12.24       0.6495           18
    2     27       3.5944       0.3919         8.65
    3     36      1.11908        0.188         3.11
    4     45     0.648736       0.1968         1.25
    5     54     0.287517        1.093         1.12
    6     63     0.150691       0.3233        0.414
    7     72    0.0909367       0.5668       0.0794
    8     81    0.0705989       0.5692        0.154
    9     90    0.0705989        9.235        0.154
   10     99     0.070166        2.256        0.252
   11    108    0.0515291       0.3859       0.0676
   12    117    0.0481688       0.5612        0.214
   13    126    0.0378954       0.2593       0.0254
   14    135    0.0366916       0.3957        0.195
   15    144     0.028778      0.06182       0.0671
   16    153    0.0244849        2.036        0.087
   17    162    0.0210853        2.309       0.0435
   18    171    0.0177636        4.618        0.114
   19    180    0.0137155        4.973        0.114
   20    189    0.0107631        3.471       0.0684
   21    198   0.00861032        2.924       0.0603
   22    207   0.00771667        1.815       0.0461
Local minimum possible.

lsqnonlin stopped because the final change in the sum of squares relative to 
its initial value is less than the 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 Estimator and the Response Optimizer. 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 app.

Close the model.


See Also


Related Examples

More About