Sleipnir C++ API
Loading...
Searching...
No Matches
slp::OCP< Scalar > Class Template Reference

#include <sleipnir/optimization/ocp.hpp>

Inheritance diagram for slp::OCP< Scalar >:
slp::Problem< Scalar >

Public Member Functions

 OCP (int num_states, int num_inputs, std::chrono::duration< Scalar > dt, int num_steps, function_ref< VariableMatrix< Scalar >(const VariableMatrix< Scalar > &x, const VariableMatrix< Scalar > &u)> dynamics, DynamicsType dynamics_type=DynamicsType::EXPLICIT_ODE, TimestepMethod timestep_method=TimestepMethod::FIXED, TranscriptionMethod transcription_method=TranscriptionMethod::DIRECT_TRANSCRIPTION)
 
 OCP (int num_states, int num_inputs, std::chrono::duration< Scalar > dt, int num_steps, function_ref< VariableMatrix< Scalar >(const Variable< Scalar > &t, const VariableMatrix< Scalar > &x, const VariableMatrix< Scalar > &u, const Variable< Scalar > &dt)> dynamics, DynamicsType dynamics_type=DynamicsType::EXPLICIT_ODE, TimestepMethod timestep_method=TimestepMethod::FIXED, TranscriptionMethod transcription_method=TranscriptionMethod::DIRECT_TRANSCRIPTION)
 
template<typename T >
requires ScalarLike<T> || MatrixLike<T>
void constrain_initial_state (const T &initial_state)
 
template<typename T >
requires ScalarLike<T> || MatrixLike<T>
void constrain_final_state (const T &final_state)
 
void for_each_step (const function_ref< void(const VariableMatrix< Scalar > &x, const VariableMatrix< Scalar > &u)> callback)
 
void for_each_step (const function_ref< void(const Variable< Scalar > &t, const VariableMatrix< Scalar > &x, const VariableMatrix< Scalar > &u, const Variable< Scalar > &dt)> callback)
 
template<typename T >
requires ScalarLike<T> || MatrixLike<T>
void set_lower_input_bound (const T &lower_bound)
 
template<typename T >
requires ScalarLike<T> || MatrixLike<T>
void set_upper_input_bound (const T &upper_bound)
 
void set_min_timestep (std::chrono::duration< Scalar > min_timestep)
 
void set_max_timestep (std::chrono::duration< Scalar > max_timestep)
 
VariableMatrix< Scalar > & X ()
 
VariableMatrix< Scalar > & U ()
 
VariableMatrix< Scalar > & dt ()
 
VariableMatrix< Scalar > initial_state ()
 
VariableMatrix< Scalar > final_state ()
 
- Public Member Functions inherited from slp::Problem< Scalar >
 Problem () noexcept=default
 
Variable< Scalar > decision_variable ()
 
VariableMatrix< Scalar > decision_variable (int rows, int cols=1)
 
VariableMatrix< Scalar > symmetric_decision_variable (int rows)
 
void minimize (const Variable< Scalar > &cost)
 
void minimize (Variable< Scalar > &&cost)
 
void maximize (const Variable< Scalar > &objective)
 
void maximize (Variable< Scalar > &&objective)
 
void subject_to (const EqualityConstraints< Scalar > &constraint)
 
void subject_to (EqualityConstraints< Scalar > &&constraint)
 
void subject_to (const InequalityConstraints< Scalar > &constraint)
 
void subject_to (InequalityConstraints< Scalar > &&constraint)
 
ExpressionType cost_function_type () const
 
ExpressionType equality_constraint_type () const
 
ExpressionType inequality_constraint_type () const
 
ExitStatus solve (const Options &options=Options{}, bool spy=false)
 
template<typename F >
requires requires(F callback, const IterationInfo<Scalar>& info) { { callback(info) } -> std::same_as<void>; }
void add_callback (F &&callback)
 
template<typename F >
requires requires(F callback, const IterationInfo<Scalar>& info) { { callback(info) } -> std::same_as<bool>; }
void add_callback (F &&callback)
 
void clear_callbacks ()
 
template<typename F >
requires requires(F callback, const IterationInfo<Scalar>& info) { { callback(info) } -> std::same_as<bool>; }
void add_persistent_callback (F &&callback)
 

Detailed Description

template<typename Scalar>
class slp::OCP< Scalar >

This class allows the user to pose and solve a constrained optimal control problem (OCP) in a variety of ways.

The system is transcripted by one of three methods (direct transcription, direct collocation, or single-shooting) and additional constraints can be added.

In direct transcription, each state is a decision variable constrained to the integrated dynamics of the previous state. In direct collocation, the trajectory is modeled as a series of cubic polynomials where the centerpoint slope is constrained. In single-shooting, states depend explicitly as a function of all previous states and all previous inputs.

Explicit ODEs are integrated using RK4.

For explicit ODEs, the function must be in the form dx/dt = f(t, x, u). For discrete state transition functions, the function must be in the form xₖ₊₁ = f(t, xₖ, uₖ).

Direct collocation requires an explicit ODE. Direct transcription and single-shooting can use either an ODE or state transition function.

https://underactuated.mit.edu/trajopt.html goes into more detail on each transcription method.

Template Parameters
ScalarScalar type.

Constructor & Destructor Documentation

◆ OCP() [1/2]

template<typename Scalar >
slp::OCP< Scalar >::OCP ( int  num_states,
int  num_inputs,
std::chrono::duration< Scalar >  dt,
int  num_steps,
function_ref< VariableMatrix< Scalar >(const VariableMatrix< Scalar > &x, const VariableMatrix< Scalar > &u)>  dynamics,
DynamicsType  dynamics_type = DynamicsType::EXPLICIT_ODE,
TimestepMethod  timestep_method = TimestepMethod::FIXED,
TranscriptionMethod  transcription_method = TranscriptionMethod::DIRECT_TRANSCRIPTION 
)
inline

Build an optimization problem using a system evolution function (explicit ODE or discrete state transition function).

Parameters
num_statesThe number of system states.
num_inputsThe number of system inputs.
dtThe timestep for fixed-step integration.
num_stepsThe number of control points.
dynamicsFunction representing an explicit or implicit ODE, or a discrete state transition function.
  • Explicit: dx/dt = f(x, u, *)
  • Implicit: f([x dx/dt]', u, *) = 0
  • State transition: xₖ₊₁ = f(xₖ, uₖ)
dynamics_typeThe type of system evolution function.
timestep_methodThe timestep method.
transcription_methodThe transcription method.

◆ OCP() [2/2]

template<typename Scalar >
slp::OCP< Scalar >::OCP ( int  num_states,
int  num_inputs,
std::chrono::duration< Scalar >  dt,
int  num_steps,
function_ref< VariableMatrix< Scalar >(const Variable< Scalar > &t, const VariableMatrix< Scalar > &x, const VariableMatrix< Scalar > &u, const Variable< Scalar > &dt)>  dynamics,
DynamicsType  dynamics_type = DynamicsType::EXPLICIT_ODE,
TimestepMethod  timestep_method = TimestepMethod::FIXED,
TranscriptionMethod  transcription_method = TranscriptionMethod::DIRECT_TRANSCRIPTION 
)
inline

Build an optimization problem using a system evolution function (explicit ODE or discrete state transition function).

Parameters
num_statesThe number of system states.
num_inputsThe number of system inputs.
dtThe timestep for fixed-step integration.
num_stepsThe number of control points.
dynamicsFunction representing an explicit or implicit ODE, or a discrete state transition function.
  • Explicit: dx/dt = f(t, x, u, *)
  • Implicit: f(t, [x dx/dt]', u, *) = 0
  • State transition: xₖ₊₁ = f(t, xₖ, uₖ, dt)
dynamics_typeThe type of system evolution function.
timestep_methodThe timestep method.
transcription_methodThe transcription method.

Member Function Documentation

◆ constrain_final_state()

template<typename Scalar >
template<typename T >
requires ScalarLike<T> || MatrixLike<T>
void slp::OCP< Scalar >::constrain_final_state ( const T &  final_state)
inline

Utility function to constrain the final state.

Parameters
final_statethe final state to constrain to.

◆ constrain_initial_state()

template<typename Scalar >
template<typename T >
requires ScalarLike<T> || MatrixLike<T>
void slp::OCP< Scalar >::constrain_initial_state ( const T &  initial_state)
inline

Utility function to constrain the initial state.

Parameters
initial_statethe initial state to constrain to.

◆ dt()

template<typename Scalar >
VariableMatrix< Scalar > & slp::OCP< Scalar >::dt ( )
inline

Get the timestep variables. After the problem is solved, this will contain the timesteps corresponding to the optimized trajectory.

Shaped 1x(num_steps+1), although the last timestep is unused in the trajectory.

Returns
The timestep variable matrix.

◆ final_state()

template<typename Scalar >
VariableMatrix< Scalar > slp::OCP< Scalar >::final_state ( )
inline

Convenience function to get the final state in the trajectory.

Returns
The final state of the trajectory.

◆ for_each_step() [1/2]

template<typename Scalar >
void slp::OCP< Scalar >::for_each_step ( const function_ref< void(const Variable< Scalar > &t, const VariableMatrix< Scalar > &x, const VariableMatrix< Scalar > &u, const Variable< Scalar > &dt)>  callback)
inline

Set the constraint evaluation function. This function is called num_steps+1 times, with the corresponding state and input VariableMatrices.

Parameters
callbackThe callback f(t, x, u, dt) where t is time, x is the state vector, u is the input vector, and dt is the timestep duration.

◆ for_each_step() [2/2]

template<typename Scalar >
void slp::OCP< Scalar >::for_each_step ( const function_ref< void(const VariableMatrix< Scalar > &x, const VariableMatrix< Scalar > &u)>  callback)
inline

Set the constraint evaluation function. This function is called num_steps+1 times, with the corresponding state and input VariableMatrices.

Parameters
callbackThe callback f(x, u) where x is the state and u is the input vector.

◆ initial_state()

template<typename Scalar >
VariableMatrix< Scalar > slp::OCP< Scalar >::initial_state ( )
inline

Convenience function to get the initial state in the trajectory.

Returns
The initial state of the trajectory.

◆ set_lower_input_bound()

template<typename Scalar >
template<typename T >
requires ScalarLike<T> || MatrixLike<T>
void slp::OCP< Scalar >::set_lower_input_bound ( const T &  lower_bound)
inline

Convenience function to set a lower bound on the input.

Parameters
lower_boundThe lower bound that inputs must always be above. Must be shaped (num_inputs)x1.

◆ set_max_timestep()

template<typename Scalar >
void slp::OCP< Scalar >::set_max_timestep ( std::chrono::duration< Scalar >  max_timestep)
inline

Convenience function to set an upper bound on the timestep.

Parameters
max_timestepThe maximum timestep.

◆ set_min_timestep()

template<typename Scalar >
void slp::OCP< Scalar >::set_min_timestep ( std::chrono::duration< Scalar >  min_timestep)
inline

Convenience function to set a lower bound on the timestep.

Parameters
min_timestepThe minimum timestep.

◆ set_upper_input_bound()

template<typename Scalar >
template<typename T >
requires ScalarLike<T> || MatrixLike<T>
void slp::OCP< Scalar >::set_upper_input_bound ( const T &  upper_bound)
inline

Convenience function to set an upper bound on the input.

Parameters
upper_boundThe upper bound that inputs must always be below. Must be shaped (num_inputs)x1.

◆ U()

template<typename Scalar >
VariableMatrix< Scalar > & slp::OCP< Scalar >::U ( )
inline

Get the input variables. After the problem is solved, this will contain the inputs corresponding to the optimized trajectory.

Shaped (num_inputs)x(num_steps+1), although the last input step is unused in the trajectory.

Returns
The input variable matrix.

◆ X()

template<typename Scalar >
VariableMatrix< Scalar > & slp::OCP< Scalar >::X ( )
inline

Get the state variables. After the problem is solved, this will contain the optimized trajectory.

Shaped (num_states)x(num_steps+1).

Returns
The state variable matrix.

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