# MPC¶

class do_mpc.controller.MPC(model)[source]

Model predictive controller.

For general information on model predictive control, please read our background article.

The MPC controller extends the do_mpc.optimizer.Optimizer base class (which is also used for the do_mpc.estimator.MHE estimator).

Use this class to configure and run the MPC controller based on a previously configured do_mpc.model.Model instance.

Configuration and setup:

Configuring and setting up the MPC controller involves the following steps:

1. Use set_param() to configure the MPC instance.
2. Set the objective of the control problem with set_objective() and set_rterm()
3. Set upper and lower bounds with bounds (optional).
4. Set further (non-linear) constraints with set_nl_cons() (optional).
5. Use the low-level API (get_p_template() and set_p_fun()) or high level API (set_uncertainty_values()) to create scenarios for robust MPC (optional).
6. Use get_tvp_template() and set_tvp_fun() to create a method to obtain new time-varying parameters at each iteration.
7. To finalize the class configuration there are two routes. The default approach is to call setup(). For deep customization use the combination of prepare_nlp() and create_nlp(). See graph below for an illustration of the process.

Route to setting up the MPC class.

Warning

Before running the controller, make sure to supply a valid initial guess for all optimized variables (states, algebraic states and inputs). Simply set the initial values of x0, z0 and u0 and then call set_initial_guess().

To take full control over the initial guess, modify the values of opt_x_num.

During runtime call make_step() with the current state $$x$$ to obtain the optimal control input $$u$$.

Attributes

 MPC.bounds Query and set bounds of the optimization variables. MPC.nlp_cons Query and modify (symbolically) the NLP constraints. MPC.nlp_cons_lb Query and modify the lower bounds of the nlp_cons. MPC.nlp_cons_ub Query and modify the upper bounds of the nlp_cons. MPC.nlp_obj Query and modify (symbolically) the NLP objective function. MPC.opt_p Full structure of (symbolic) MPC parameters. MPC.opt_p_num Full MPC parameter vector. MPC.opt_x Full structure of (symbolic) MPC optimization variables. MPC.opt_x_num Full MPC solution and initial guess. MPC.scaling Query and set scaling of the optimization variables. MPC.t0 Current time marker of the class. MPC.terminal_bounds Query and set the terminal bounds for the states. MPC.u0 Initial input and current iterate. MPC.x0 Initial state and current iterate. MPC.z0 Initial algebraic state and current iterate.

Methods

 MPC.create_nlp Create the optimization problem. MPC.get_p_template Obtain output template for set_p_fun(). MPC.get_tvp_template Obtain output template for set_tvp_fun(). MPC.make_step Main method of the class during runtime. MPC.prepare_nlp Prepare the optimization problem. MPC.reset_history Reset the history of the optimizer. MPC.set_initial_guess Initial guess for optimization variables. MPC.set_nl_cons Introduce new constraint to the class. MPC.set_objective Sets the objective of the optimal control problem (OCP). MPC.set_p_fun Set function which returns parameters. MPC.set_param Set the parameters of the MPC class. MPC.set_rterm Set the penality factor for the inputs. MPC.set_tvp_fun Set function which returns time-varying parameters. MPC.set_uncertainty_values Define scenarios for the uncertain parameters. MPC.setup Setup the MPC class. MPC.solve Solves the optmization problem.