Troubleshooting your model and Safe Mode
In order to obtain the optimal solution to your control problem, DynaProg uses your system and cost function(s) with n-dimensional arrays. Some operations may produce unintended results. In order to avoid these issues, you can either follow this troubleshooting guide or enable Safe Mode. Troubleshooting your model function
In the backward phase of the control optimization algorithm, DynaProg uses your system and cost function to evaluate updated state values, stage costs and unfeasibilities. When it does so, it passes state and control variables passes as an input in the form of n-dimensional arrays. In these arrays, each dimension is assigned to one specific state or control variable.
Consider an optimization problem with two state variables and two control variables, which are discretized on computational grids having
,
,
and
elements respectively. The system and cost function is defined as: function [x_next, stage_cost, unfeas] = sysfun(x, u, ~)
x_next{1} = x{1} + a .* dt;
x_next{2} = x{1} + (x{1} - x{2}) .* dt;
In the backward phase, DynaProg passes as inputs:
- x{1} as an array of size
, - x{2} as an array of size
, - u{1} as an array of size
, - u{1} as an array of size
.
As MATLAB evaluates each line of the system and cost function, it will expand the size of the resulting array accordingly. Therefore:
- a will be an array of size
, as it is only dependent on the control variables, - x_next{1} will be an array of size
, as it is only dependent on
,
and
, - x_next{2} will be an array of size
, as it is only dependent on all state and control variables, - stageCost will be an array of size
, as it is only dependent on
.
For this reason, you must ensure that all operations that you use in your system and cost function are consistent in their output when operating on n-dimensional arrays.
Example of such operations are:
- Element-wise sum (+), subtraction (-), product (.*), division (./) and power (.^).
- Indexing with an nd-array with at least two non-singleton dimensions.
- Logical indexing if the indexed array and the index array have the same size.
- Functions that operate elementwise and preserve the input size.
Example of operations that are inconsistent are:
- Matrix operations such as matrix multiplication (*), "division" (\ and /) and power (^).
- Indexing with an nd-array with at only one one non-singleton dimensions (i.e. a vector).
- Logical indexing if the indexed array and the index array do not have the same size.
To see some examples of troublesome operations, and how to fix them, see this example. Safe Mode
Safe Mode is an alternative way of dealing with the system and cost function which is meant to automatically handle potentially troublesome operations, at the cost of increased optimization time.
By default, Safe Mode is disabled. To enable Safe Mode, specify it in the constructor after the mandatory positional arguments:
prob = DynaProg(StateGrid, StateInitial, StateFinal, ControlGrid, Nstages, SysName, 'SafeMode', true);
or set the SafeMode property to true on an existing problem structure.
Safe Mode forces DynaProg to always pass the state variables, control variables, exogenous inputs and intermediate variables as full n-dimensional arrays having size
where
and
are the number of grid points that discretize the state variable
and the control variable
and
and
are the number of state and control variables. This effectively avoids all errors related to array indexing, but it means that many repetitive, unnecessary computations are performed. Note, however, that Safe Mode may not prevent all sorts of errors that may arise when you run the model function.