Documentation

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.

open_system('spe_engine_throttle')

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.

set_param('spe_engine_throttle','OutputOption','SpecifiedOutputTimes','OutputTimes','OutputTimesValues');
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 14-Aug-2017 19:27:08

                               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.5944       0.3919         8.65
    3     36      1.11908        0.188         3.11
    4     45     0.648846        0.197         1.25
    5     54     0.286999        1.174         1.12
    6     63     0.145554       0.3484        0.417
    7     72    0.0876181       0.5157       0.0797
    8     81    0.0675225       0.4867       0.0587
    9     90    0.0546981        3.473        0.119
   10     99    0.0546981        7.271        0.119
   11    108    0.0530811        1.818       0.0693
   12    117    0.0530811        1.898       0.0693
   13    126    0.0522303       0.4745       0.0322
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');
set_param('spe_engine_throttle','OutputOption','RefineOutputTimes','OutputTimes','[]');

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.

bdclose('spe_engine_throttle')
Was this topic helpful?