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.
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 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.
Configure the simulator for optimization and enable fast restart using the setup
function.
Simulator = setup(Simulator,FastRestart='on');
Before R2024a: Enable and disable fast restart using the fastRestart
function.
The spe_engine_throttle
model uses a variable-step solver, and may not output values at the times in the measured experiment data. The setup
function configures the model to output values at the times of the measured data.
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 2024-Sep-05, 19:00:27 First-order Iter F-count f(x) Step-size 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.649065 0.1965 1.25 5 54 0.288093 1.275 1.16 6 63 0.149839 0.2086 0.412 7 72 0.0892561 0.5565 0.119 8 81 0.0679834 0.4024 0.166 9 90 0.0679834 9.811 0.166 10 99 0.0679834 2.264 0.166 11 108 0.067686 0.1798 0.138 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 and model settings modified by the setup
function, which includes disabling fast restart.
Simulator = restore(Simulator);
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.
See Also
sdo.SimulationTest
| setup
| restore
Related Examples
- Improving Optimization Performance Using Fast Restart (GUI)
- Use Fast Restart Mode During Response Optimization
- Use Fast Restart Mode During Sensitivity Analysis