simple_casadi_mpc
GitHub
Loading...
Searching...
No Matches
simple_casadi_mpc::Problem Class Referenceabstract

Base class describing an MPC optimal control problem. More...

#include <simple_casadi_mpc.hpp>

Public Types

enum class  DynamicsType { ContinuesForwardEuler , ContinuesModifiedEuler , ContinuesRK4 , Discretized }
 Discretization scheme used to advance the dynamics. More...
 
enum class  ConstraintType { Equality , Inequality }
 Constraint kind passed to add_constraint and add_soft_constraint. More...
 

Public Member Functions

 Problem (DynamicsType dyn_type, size_t _nx, size_t _nu, size_t _horizon, double _dt)
 Construct a problem with a fixed prediction horizon and uniform time step.
 
 Problem (DynamicsType dyn_type, size_t _nx, size_t _nu, size_t _horizon, std::vector< double > _dts)
 Construct a problem with a per-stage (variable) time step.
 
virtual casadi::MX dynamics (casadi::MX x, casadi::MX u)=0
 Symbolic dynamics, must be overridden by the user.
 
Eigen::VectorXd dynamics_eval (Eigen::VectorXd x, Eigen::VectorXd u)
 Evaluate dynamics at numeric (x, u). Useful for plant simulation.
 
Eigen::VectorXd simulate (Eigen::VectorXd x0, Eigen::MatrixXd u)
 Advance the plant one step using the user-provided discrete dynamics.
 
Eigen::VectorXd simulate (Eigen::VectorXd x0, Eigen::MatrixXd u, double dt)
 Advance the plant dt seconds using the configured continuous integrator.
 
void set_input_bound (Eigen::VectorXd lb, Eigen::VectorXd ub, int start=-1, int end=-1)
 Set per-stage input bounds \(u_{\text{lb},k} \le u_k \le u_{\text{ub},k}\).
 
void set_input_lower_bound (Eigen::VectorXd lb, int start=-1, int end=-1)
 Set only the lower bound on the input. See set_input_bound for start/end.
 
void set_input_upper_bound (Eigen::VectorXd ub, int start=-1, int end=-1)
 Set only the upper bound on the input. See set_input_bound for start/end.
 
void set_state_bound (Eigen::VectorXd lb, Eigen::VectorXd ub, int start=-1, int end=-1)
 Set per-stage state bounds \(x_{\text{lb},k} \le x_k \le x_{\text{ub},k}\). Range semantics match set_input_bound.
 
void set_state_lower_bound (Eigen::VectorXd lb, int start=-1, int end=-1)
 Set only the lower bound on the state. See set_input_bound for start/end.
 
void set_state_upper_bound (Eigen::VectorXd ub, int start=-1, int end=-1)
 Set only the upper bound on the state. See set_input_bound for start/end.
 
void add_constraint (ConstraintType type, std::function< casadi::MX(casadi::MX, casadi::MX)> constrinat)
 Add a hard path constraint applied at every stage.
 
void add_constraint_at (ConstraintType type, std::function< casadi::MX(casadi::MX, casadi::MX)> constraint, int start, int end=-1)
 Add a hard path constraint applied only at the specified stage range.
 
void add_soft_constraint (ConstraintType type, std::function< casadi::MX(casadi::MX, casadi::MX)> constraint, double w1=1e3, double w2=0.0)
 Add a soft path constraint with per-stage non-negative slack.
 
void add_soft_constraint_at (ConstraintType type, std::function< casadi::MX(casadi::MX, casadi::MX)> constraint, double w1, double w2, int start, int end=-1)
 Add a soft path constraint applied only at the specified stage range.
 
virtual casadi::MX stage_cost (casadi::MX x, casadi::MX u, size_t k)
 Stage cost \(L(x_k, u_k, k;\,p)\) at step \(k\). Default returns 0.
 
virtual casadi::MX terminal_cost (casadi::MX x)
 Terminal cost \(\Phi(x_N;\,p)\) evaluated at \(x_N\). Default returns 0.
 
DynamicsType dynamics_type () const
 Discretization scheme.
 
size_t nx () const
 State dimension.
 
size_t nu () const
 Control dimension.
 
size_t horizon () const
 Number of stages N.
 
double dt () const
 Step size at stage 0 (or the uniform step for problems built with the single-dt constructor). Use dt(size_t) for per-stage values.
 
double dt (size_t k) const
 Per-stage time step \(\Delta t_k\).
 
const std::vector< double > & dts () const
 Read-only view of all per-stage time steps, length horizon().
 
bool has_uniform_dt () const
 True iff every stage has the same dt.
 
casadi::MX parameter (std::string name, size_t rows, size_t cols)
 Declare a runtime-tunable symbolic parameter that can be passed to MPC::solve.
 
casadi::MX reference_trajectory (std::string name="x_ref")
 Convenience wrapper of parameter with shape \((n_x, N)\).
 

Friends

class MPC
 
class JITMPC
 
class CompiledMPC
 

Detailed Description

Base class describing an MPC optimal control problem.

Derive from this class and override dynamics, stage_cost and optionally terminal_cost. Constraints, bounds and time-varying parameters are configured via the public methods on this class.

The optimization horizon is fixed at construction time. The dynamics may be either continuous (auto-discretized via Forward Euler / Modified Euler / RK4) or already discretized.

Member Enumeration Documentation

◆ ConstraintType

Constraint kind passed to add_constraint and add_soft_constraint.

Enumerator
Equality 

g(x, u) = 0.

Inequality 

g(x, u) <= 0.

◆ DynamicsType

Discretization scheme used to advance the dynamics.

Enumerator
ContinuesForwardEuler 

Continuous-time, integrated by forward Euler.

ContinuesModifiedEuler 

Continuous-time, integrated by Heun's method.

ContinuesRK4 

Continuous-time, integrated by classical RK4.

Discretized 

User supplies x_{k+1} directly.

Constructor & Destructor Documentation

◆ Problem() [1/2]

simple_casadi_mpc::Problem::Problem ( DynamicsType dyn_type,
size_t _nx,
size_t _nu,
size_t _horizon,
double _dt )
inline

Construct a problem with a fixed prediction horizon and uniform time step.

Parameters
dyn_typediscretization scheme used by dynamics.
_nxstate dimension.
_nucontrol dimension.
_horizonnumber of stages in the optimization (N).
_dtuniform time step in seconds (used by continuous dynamics).

◆ Problem() [2/2]

simple_casadi_mpc::Problem::Problem ( DynamicsType dyn_type,
size_t _nx,
size_t _nu,
size_t _horizon,
std::vector< double > _dts )
inline

Construct a problem with a per-stage (variable) time step.

Each entry of _dts is the integration step \(\Delta t_k\) used between stage \(k\) and \(k+1\). Use this overload when the prediction horizon spans non-uniform time intervals (e.g. coarse-to-fine schedules).

Parameters
dyn_typediscretization scheme used by dynamics.
_nxstate dimension.
_nucontrol dimension.
_horizonnumber of stages in the optimization (N).
_dtsper-stage time steps, must have size == _horizon.

Member Function Documentation

◆ add_constraint()

void simple_casadi_mpc::Problem::add_constraint ( ConstraintType type,
std::function< casadi::MX(casadi::MX, casadi::MX)> constrinat )
inline

Add a hard path constraint applied at every stage.

Parameters
typeConstraintType::Equality ( \(g(x, u) = 0\)) or ConstraintType::Inequality ( \(g(x, u) \le 0\)).
constrinatcallable returning the constraint vector at one stage.

◆ add_constraint_at()

void simple_casadi_mpc::Problem::add_constraint_at ( ConstraintType type,
std::function< casadi::MX(casadi::MX, casadi::MX)> constraint,
int start,
int end = -1 )
inline

Add a hard path constraint applied only at the specified stage range.

Range semantics match set_input_bound:

  • start == -1, end == -1 → all stages \([0, N)\) (same as add_constraint).
  • start != -1, end == -1 → only stage start.
  • start != -1, end != -1 → stages \([\text{start}, \text{end})\).

Internally the constraint is still attached to the per-stage g(x, u) vector (so the symbolic structure remains uniform across stages); the path-constraint bound is set to \([-\infty, +\infty]\) on inactive stages so it does not influence the solution.

◆ add_soft_constraint()

void simple_casadi_mpc::Problem::add_soft_constraint ( ConstraintType type,
std::function< casadi::MX(casadi::MX, casadi::MX)> constraint,
double w1 = 1e3,
double w2 = 0.0 )
inline

Add a soft path constraint with per-stage non-negative slack.

Introduces slack \(s \ge 0\) and adds the penalty

\[ w_1 \mathbf{1}^\top s + \tfrac{1}{2}\, w_2\, s^\top s \]

to the cost. The original constraint is relaxed as:

  • Inequality: \(g(x, u) \le 0 \;\to\; g - s \le 0\).
  • Equality: \(h(x, u) = 0 \;\to\; |h| \le s\), i.e. \(h - s \le 0\) and \(-h - s \le 0\).

    \(w_2 = 0\) (default) gives a pure L1 (exact) penalty; \(w_2 > 0\) adds an L2 (smooth) term. Hard add_constraint and soft add_soft_constraint may be mixed on the same problem.

Parameters
typeconstraint kind, see ConstraintType.
constraintcallable returning the constraint vector at one stage.
w1L1 penalty weight \(w_1\) (default 1e3).
w2L2 penalty weight \(w_2\) (default 0.0).

◆ add_soft_constraint_at()

void simple_casadi_mpc::Problem::add_soft_constraint_at ( ConstraintType type,
std::function< casadi::MX(casadi::MX, casadi::MX)> constraint,
double w1,
double w2,
int start,
int end = -1 )
inline

Add a soft path constraint applied only at the specified stage range.

Range semantics match add_constraint_at. On inactive stages the associated slack is forced to 0 (so the penalty contributes nothing) and the relaxed constraint bound is set to \([-\infty, +\infty]\).

◆ dynamics()

virtual casadi::MX simple_casadi_mpc::Problem::dynamics ( casadi::MX x,
casadi::MX u )
pure virtual

Symbolic dynamics, must be overridden by the user.

For a continuous DynamicsType, return \(\frac{dx}{dt} = f(x, u)\) given the current \((x, u)\); the discretization is applied automatically by the chosen DynamicsType. For DynamicsType::Discretized, return the next state \(x_{k+1}\) directly.

Parameters
xstate \(x_k \in \mathbb{R}^{n_x}\).
uinput \(u_k \in \mathbb{R}^{n_u}\).
Returns
either \(\frac{dx}{dt}\) (continuous) or \(x_{k+1}\) (discrete).

◆ parameter()

casadi::MX simple_casadi_mpc::Problem::parameter ( std::string name,
size_t rows,
size_t cols )
inline

Declare a runtime-tunable symbolic parameter that can be passed to MPC::solve.

Use this to inject reference trajectories, weights, obstacle positions, etc. without rebuilding the solver. Update its numeric value via solve(x0, {{name, dm}}).

Parameters
nameunique identifier (used as the key in the DMDict passed to solve).
rowsnumber of rows of the parameter matrix.
colsnumber of cols. Use cols == horizon for per-stage values (column k is consumed at stage k); otherwise the parameter is broadcast.
Returns
symbolic MX you can use inside dynamics, stage_cost, etc.

◆ reference_trajectory()

casadi::MX simple_casadi_mpc::Problem::reference_trajectory ( std::string name = "x_ref")
inline

Convenience wrapper of parameter with shape \((n_x, N)\).

Each column is the reference state at the corresponding stage, suitable for trajectory tracking inside stage_cost.

◆ set_input_bound()

void simple_casadi_mpc::Problem::set_input_bound ( Eigen::VectorXd lb,
Eigen::VectorXd ub,
int start = -1,
int end = -1 )
inline

Set per-stage input bounds \(u_{\text{lb},k} \le u_k \le u_{\text{ub},k}\).

Parameters
lblower bound, shape (nu,).
ubupper bound, shape (nu,).
startfirst stage (inclusive), or -1 for all stages.
endone-past-last stage, or -1 (with start == -1 applies to all stages; with start != -1, end == -1 applies to the single stage start).

◆ simulate() [1/2]

Eigen::VectorXd simple_casadi_mpc::Problem::simulate ( Eigen::VectorXd x0,
Eigen::MatrixXd u )
inline

Advance the plant one step using the user-provided discrete dynamics.

Precondition
dynamics_type() == DynamicsType::Discretized.

◆ simulate() [2/2]

Eigen::VectorXd simple_casadi_mpc::Problem::simulate ( Eigen::VectorXd x0,
Eigen::MatrixXd u,
double dt )
inline

Advance the plant dt seconds using the configured continuous integrator.

Precondition
dynamics_type() != DynamicsType::Discretized.
Parameters
x0current state, shape (nx,).
ucontrol held over the step.
dtsimulation step (independent of the MPC discretization step).

◆ stage_cost()

virtual casadi::MX simple_casadi_mpc::Problem::stage_cost ( casadi::MX x,
casadi::MX u,
size_t k )
inlinevirtual

Stage cost \(L(x_k, u_k, k;\,p)\) at step \(k\). Default returns 0.

Parameters
xstate at stage k, shape (nx, 1).
uinput at stage k, shape (nu, 1).
kstage index in \([0, N)\).

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