Model¶

class do_mpc.model.Model(model_type=None, symvar_type='SX')[source]

The do-mpc model class. This class holds the full model description and is at the core of do_mpc.simulator.Simulator, do_mpc.controller.MPC and do_mpc.estimator.Estimator. The Model class is created with setting the model_type (continuous or discrete). A continous model consists of an underlying ordinary differential equation (ODE) or differential algebraic equation (DAE):

$\begin{split}\dot{x}(t) &= f(x(t),u(t),z(t),p(t),p_{\text{tv}}(t)) + w(t),\\ 0 &= g(x(t),u(t),z(t),p(t),p_{\text{tv}}(t))\\ y &= h(x(t),u(t),z(t),p(t),p_{\text{tv}}(t)) + v(t)\end{split}$

whereas a discrete model consists of a difference equation:

$\begin{split}x_{k+1} &= f(x_k,u_k,z_k,p_k,p_{\text{tv},k}) + w_k,\\ 0 &= g(x_k,u_k,z_k,p_k,p_{\text{tv},k})\\ y_k &= h(x_k,u_k,z_k,p_k,p_{\text{tv},k}) + v_k\end{split}$

The do-mpc model can be initiated with either SX or MX variable type. We refer to the CasADi documentation on the difference of these two types.

Note

SX vs. MX in a nutshell: In general use SX variables (default). If your model consists of scalar operations SX variables will be beneficial. Your implementation will most likely only benefit from MX variables if you use large(r)-scale matrix-vector multiplications.

Note

The option symvar_type will be inherited to all derived classes (e.g. do_mpc.simulator.Simulator, do_mpc.controller.MPC and do_mpc.estimator.Estimator). All symbolic variables in these classes will be chosen respectively.

Configuration and setup:

Configuring and setting up the Model involves the following steps:

1. Use set_variable() to introduce new variables to the model.
2. Optionally introduce “auxiliary” expressions as functions of the previously defined variables with set_expression(). The expressions can be used for monitoring or be reused as constraints, the cost function etc.
3. Optionally introduce measurement equations with set_meas(). The syntax is identical to set_expression(). By default state-feedback is assumed.
4. Define the right-hand-side of the discrete or continuous model as a function of the previously defined variables with set_rhs(). This method must be called once for each introduced state.
5. Call setup() to finalize the Model. No further changes are possible afterwards.

Note

All introduced model variables are accessible as Attributes of the Model. Use these attributes to query to variables, e.g. to form the cost function in a seperate file for the MPC configuration.

Parameters: model_type (str) – Set if the model is discrete or continuous. symvar_type (str) – Set if the model is configured with CasADi SX or MX variables. assertion – model_type must be string assertion – model_type must be either discrete or continuous
__getitem__(ind)[source]

The Model class supports the __getitem__ method, which can be used to retrieve the model variables (see attribute list).

# Query the states like this:
x = model.x
# or like this:
x = model['x']


This also allows to retrieve multiple variables simultaneously:

x, u, z = model['x','u','z']


Attributes

 Model.aux Auxiliary expressions. Model.p Static parameters. Model.tvp Time-varying parameters. Model.u Inputs. Model.v Measurement noise. Model.w Process noise. Model.x Dynamic states. Model.y Measurements. Model.z Algebraic states.

Methods

 Model.set_alg Introduce new algebraic equation to model. Model.set_expression Introduce new expression to the model class. Model.set_meas Introduce new measurable output to the model class. Model.set_rhs Formulate the right hand side (rhs) of the ODE: Model.set_variable Introduce new variables to the model class. Model.setup Setup method must be called to finalize the modelling process.