simple_casadi_mpc
GitHub
Loading...
Searching...
No Matches
simple_casadi_mpc::MPC Class Reference

Runtime MPC solver. Builds a CasADi NLP from a Problem and solves it on demand. More...

#include <simple_casadi_mpc.hpp>

Inheritance diagram for simple_casadi_mpc::MPC:
simple_casadi_mpc::CompiledMPC simple_casadi_mpc::JITMPC

Public Member Functions

template<class T >
 MPC (std::shared_ptr< T > prob, std::string solver_name="ipopt", casadi::Dict config=default_ipopt_config())
 Build the NLP from prob and create the underlying nlpsol.
 
virtual Eigen::VectorXd solve (Eigen::VectorXd x0, casadi::DMDict new_param_list=casadi::DMDict())
 Solve the NLP at the current state and return the first optimal control.
 
casadi::MXDict casadi_prob () const
 Symbolic NLP description {x, f, g, p} constructed from the Problem.
 
const std::string & solver_name () const
 Backend name passed to CasADi nlpsol.
 
casadi::Dict solver_config () const
 Effective config forwarded to nlpsol (after consuming simple-casadi-mpc keys).
 
std::vector< casadi_int > equality_flags () const
 Per-constraint flags marking equality (true) vs inequality (false).
 

Static Public Member Functions

static casadi::Dict default_ipopt_config ()
 Reasonable defaults for the IPOPT backend (silent, warm-start enabled).
 
static casadi::Dict default_qpoases_config ()
 Reasonable defaults for SQP method with the qpOASES inner QP solver.
 
static casadi::Dict default_fatrop_config ()
 Reasonable defaults for the FATROP backend (auto structure detection).
 
static bool equality_required (const std::string &solver_name, const casadi::Dict &config)
 Whether the chosen backend needs an equality flag vector in the config.
 

Protected Member Functions

void build_with_map (size_t nx, size_t nu, casadi_int N, bool mapsum_stage_cost=true, bool expand_inner_functions=true)
 
virtual void build_solver ()
 

Protected Attributes

std::shared_ptr< Problemprob_
 
std::string solver_name_
 
casadi::Dict config_
 
casadi::MXDict casadi_prob_
 
casadi::Function solver_
 
std::vector< casadi::MX > Xs = {}
 
std::vector< casadi::MX > Us = {}
 
casadi::DM lbw_
 
casadi::DM ubw_
 
casadi::DM lbg_
 
casadi::DM ubg_
 
std::vector< casadi::DM > param_vec_ = {}
 
std::vector< bool > equality_ = {}
 
casadi::DM w0_
 
casadi::DM lam_x0_
 
casadi::DM lam_g0_
 

Detailed Description

Runtime MPC solver. Builds a CasADi NLP from a Problem and solves it on demand.

This is the simplest variant: the NLP is constructed once at construction time and solved at runtime using the chosen CasADi nlpsol backend. For faster iteration time after a startup cost, use JITMPC; for build-time AOT compilation use CompiledMPC.

Constructor & Destructor Documentation

◆ MPC()

template<class T >
simple_casadi_mpc::MPC::MPC ( std::shared_ptr< T > prob,
std::string solver_name = "ipopt",
casadi::Dict config = default_ipopt_config() )
inline

Build the NLP from prob and create the underlying nlpsol.

Parameters
probthe problem to solve.
solver_nameCasADi nlpsol backend name (e.g. "ipopt", "fatrop", "sqpmethod").
confignlpsol options. Two simple-casadi-mpc-specific keys are also recognised and consumed before being forwarded to CasADi:
  • mapsum_stage_cost (bool, default true): build the stage-cost sum via MapSum so AD stays loop-shaped.
  • expand_inner_functions (bool, default true): SX-expand per-stage F/L/G before mapping for faster JIT compilation.

Member Function Documentation

◆ equality_required()

static bool simple_casadi_mpc::MPC::equality_required ( const std::string & solver_name,
const casadi::Dict & config )
inlinestatic

Whether the chosen backend needs an equality flag vector in the config.

FATROP with structure_detection == "auto" requires it, so the constructor inserts it automatically when this returns true.

◆ solve()

virtual Eigen::VectorXd simple_casadi_mpc::MPC::solve ( Eigen::VectorXd x0,
casadi::DMDict new_param_list = casadi::DMDict() )
inlinevirtual

Solve the NLP at the current state and return the first optimal control.

Warm-starts from the previous solve (x, lam_x, lam_g are cached internally), so calling this repeatedly during closed-loop simulation benefits from incremental convergence.

Parameters
x0current measured state, shape (nx,).
new_param_listupdates to parameters declared via Problem::parameter. Keys are parameter names; values are casadi::DM of matching shape.
Returns
optimal control to apply now, u_0, shape (nu,).

Reimplemented in simple_casadi_mpc::JITMPC, and simple_casadi_mpc::CompiledMPC.


The documentation for this class was generated from the following file: