For both implicit and adaptive MPC controllers, you can adjust the prediction and control horizons while the controller operates. Doing so can be useful for:
Efficiently evaluating different horizon choices at run time during prototyping
Adjusting horizons without redeployment after plant dynamics change significantly, such as in a batch process
To adjust the horizons at run time from the command line, at each control interval,
specify the following properties of the
PredictionHorizon — Run-time prediction horizon signal, specified
as a positive integer
ControlHorizon — Run-time control horizon signal, specified as a
positive integer or a vector of positive integers
In Simulink®, to adjust the horizons for an MPC Controller or Adaptive MPC Controller block, select the Adjust prediction horizon and control horizon at run time parameter. Doing so adds the following input ports to the block:
p — Run-time prediction horizon signal, specified as a scalar
m — Run-time control horizon signal, specified as a scalar or
You must specify the maximum prediction horizon using the Maximum prediction
horizon parameter. Doing so ensures that the optimal sequence output ports of
the block (
y.seq) have constant sizes with
pmax+1 rows, where
pmax is the maximum prediction horizon.
Run-time horizon tuning supports code generation in both MATLAB® and Simulink. Generating code for a controller that supports run-time horizon changes allows you to tune your horizon values on your deployed controller hardware.
After tuning the horizon values, to improve the computational efficiency of your final deployed controller, you can generate code for a constant-horizon controller using the tuned values.
Deploying your controller with run-time horizon tuning enabled significantly increases the computational load and memory footprint of your MPC application. If you plan to use run-time horizon tuning only for prototyping to find the proper horizon values, after tuning, ensure that this feature is disabled. You can then generate code with a constant-horizon controller using the tuned values.
If your controller uses manipulated variable blocking and you generate code for your controller, the size of the control horizon vector must remain constant at run-time. In this case, you can still tune the values within the control horizon vector.
To generate code for a controller that uses run-time horizon tuning, your deployed
hardware target must support dynamic memory allocation. For example, if your embedded
system does not support the
malloc C function, then the generated code
will not run.
To generate code in MATLAB, set the
'UseVariableHorizon' name-value pair of
true. At each control interval,
you can then specify the horizons before calling
[configData,stateData,onlineData] = getCodeGenerationData(mpcobj,'UseVariableHorizon',true); ... onlineData.Horizons.p = 10; onlineData.Horizons.m = 3; [u,stateData] = mpcmoveCodeGeneration(configData,stateData,onlineData);
If your application uses controller parameters that vary over the prediction horizon, adjusting the prediction horizon at run time affects the behavior of these time-varying parameters.
If you define time-varying constraints in your controller object, the profile of the constraints across the prediction horizon does not change at run time. If your run-time prediction horizon value Pr is:
Greater than the length of the constraint profile specified in your controller, then the controller uses the final value of the constraint profile for the remainder of the prediction horizon
Less than the length of the constraint profile specified in your controller, then the controller truncates the constraint profile after Pr steps
For more information on adjusting constraints, see Update Constraints at Run Time.
To vary tuning weights at run-time, you specify weight arrays where the number of rows must not exceed the prediction horizons length. If you adjust the prediction horizon at run time, it is best practice to also adjust the number of rows in your tuning weight arrays to be consistent with the new horizon value.
If you do not adjust the sizes of your tuning weight arrays, the number of rows is extended or truncated in a manner similar to the time-varying constraint profile length.
For more information on tuning weights, see Tune Weights at Run Time.
For a linear time-varying MPC controller, you vary the plant model and nominal conditions across the prediction horizon. If you adjust the prediction horizon at run time, it is best practice to also adjust the lengths of your time-varying model and nominal condition arrays to be consistent with the new horizon value.
If you do not adjust the lengths of the model and nominal condition arrays, the arrays are extended or truncated in a manner similar to the time-varying constraint profile.
For more information on linear time-varying MPC, see Time-Varying MPC.