Sleipnir C++ API
Loading...
Searching...
No Matches
sleipnir::OCPSolver Class Reference

#include <sleipnir/control/OCPSolver.hpp>

Inheritance diagram for sleipnir::OCPSolver:
sleipnir::OptimizationProblem

Public Member Functions

 OCPSolver (int numStates, int numInputs, std::chrono::duration< double > dt, int numSteps, function_ref< VariableMatrix(const VariableMatrix &x, const VariableMatrix &u)> dynamics, DynamicsType dynamicsType=DynamicsType::kExplicitODE, TimestepMethod timestepMethod=TimestepMethod::kFixed, TranscriptionMethod method=TranscriptionMethod::kDirectTranscription)
 
 OCPSolver (int numStates, int numInputs, std::chrono::duration< double > dt, int numSteps, function_ref< VariableMatrix(const Variable &t, const VariableMatrix &x, const VariableMatrix &u, const Variable &dt)> dynamics, DynamicsType dynamicsType=DynamicsType::kExplicitODE, TimestepMethod timestepMethod=TimestepMethod::kFixed, TranscriptionMethod method=TranscriptionMethod::kDirectTranscription)
 
template<typename T >
requires ScalarLike<T> || MatrixLike<T>
void ConstrainInitialState (const T &initialState)
 
template<typename T >
requires ScalarLike<T> || MatrixLike<T>
void ConstrainFinalState (const T &finalState)
 
void ForEachStep (const function_ref< void(const VariableMatrix &x, const VariableMatrix &u)> callback)
 
void ForEachStep (const function_ref< void(const Variable &t, const VariableMatrix &x, const VariableMatrix &u, const Variable &dt)> callback)
 
template<typename T >
requires ScalarLike<T> || MatrixLike<T>
void SetLowerInputBound (const T &lowerBound)
 
template<typename T >
requires ScalarLike<T> || MatrixLike<T>
void SetUpperInputBound (const T &upperBound)
 
void SetMinTimestep (std::chrono::duration< double > minTimestep)
 
void SetMaxTimestep (std::chrono::duration< double > maxTimestep)
 
VariableMatrixX ()
 
VariableMatrixU ()
 
VariableMatrixDT ()
 
VariableMatrix InitialState ()
 
VariableMatrix FinalState ()
 
- Public Member Functions inherited from sleipnir::OptimizationProblem
 OptimizationProblem () noexcept=default
 
Variable DecisionVariable ()
 
VariableMatrix DecisionVariable (int rows, int cols=1)
 
VariableMatrix SymmetricDecisionVariable (int rows)
 
void Minimize (const Variable &cost)
 
void Minimize (Variable &&cost)
 
void Maximize (const Variable &objective)
 
void Maximize (Variable &&objective)
 
void SubjectTo (const EqualityConstraints &constraint)
 
void SubjectTo (EqualityConstraints &&constraint)
 
void SubjectTo (const InequalityConstraints &constraint)
 
void SubjectTo (InequalityConstraints &&constraint)
 
SolverStatus Solve (const SolverConfig &config=SolverConfig{})
 
template<typename F >
requires requires(F callback, const SolverIterationInfo& info) { { callback(info) } -> std::same_as<void>; }
void Callback (F &&callback)
 
template<typename F >
requires requires(F callback, const SolverIterationInfo& info) { { callback(info) } -> std::same_as<bool>; }
void Callback (F &&callback)
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ OCPSolver() [1/2]

sleipnir::OCPSolver::OCPSolver ( int  numStates,
int  numInputs,
std::chrono::duration< double dt,
int  numSteps,
function_ref< VariableMatrix(const VariableMatrix &x, const VariableMatrix &u)>  dynamics,
DynamicsType  dynamicsType = DynamicsType::kExplicitODE,
TimestepMethod  timestepMethod = TimestepMethod::kFixed,
TranscriptionMethod  method = TranscriptionMethod::kDirectTranscription 
)
inline

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

Parameters
numStatesThe number of system states.
numInputsThe number of system inputs.
dtThe timestep for fixed-step integration.
numStepsThe 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ₖ)
dynamicsTypeThe type of system evolution function.
timestepMethodThe timestep method.
methodThe transcription method.

◆ OCPSolver() [2/2]

sleipnir::OCPSolver::OCPSolver ( int  numStates,
int  numInputs,
std::chrono::duration< double dt,
int  numSteps,
function_ref< VariableMatrix(const Variable &t, const VariableMatrix &x, const VariableMatrix &u, const Variable &dt)>  dynamics,
DynamicsType  dynamicsType = DynamicsType::kExplicitODE,
TimestepMethod  timestepMethod = TimestepMethod::kFixed,
TranscriptionMethod  method = TranscriptionMethod::kDirectTranscription 
)
inline

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

Parameters
numStatesThe number of system states.
numInputsThe number of system inputs.
dtThe timestep for fixed-step integration.
numStepsThe 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)
dynamicsTypeThe type of system evolution function.
timestepMethodThe timestep method.
methodThe transcription method.

Member Function Documentation

◆ ConstrainFinalState()

template<typename T >
requires ScalarLike<T> || MatrixLike<T>
void sleipnir::OCPSolver::ConstrainFinalState ( const T &  finalState)
inline

Utility function to constrain the final state.

Parameters
finalStatethe final state to constrain to.

◆ ConstrainInitialState()

template<typename T >
requires ScalarLike<T> || MatrixLike<T>
void sleipnir::OCPSolver::ConstrainInitialState ( const T &  initialState)
inline

Utility function to constrain the initial state.

Parameters
initialStatethe initial state to constrain to.

◆ DT()

VariableMatrix & sleipnir::OCPSolver::DT ( )
inline

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

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

Returns
The timestep variable matrix.

◆ FinalState()

VariableMatrix sleipnir::OCPSolver::FinalState ( )
inline

Convenience function to get the final state in the trajectory.

Returns
The final state of the trajectory.

◆ ForEachStep() [1/2]

void sleipnir::OCPSolver::ForEachStep ( const function_ref< void(const Variable &t, const VariableMatrix &x, const VariableMatrix &u, const Variable &dt)>  callback)
inline

Set the constraint evaluation function. This function is called numSteps+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.

◆ ForEachStep() [2/2]

void sleipnir::OCPSolver::ForEachStep ( const function_ref< void(const VariableMatrix &x, const VariableMatrix &u)>  callback)
inline

Set the constraint evaluation function. This function is called numSteps+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.

◆ InitialState()

VariableMatrix sleipnir::OCPSolver::InitialState ( )
inline

Convenience function to get the initial state in the trajectory.

Returns
The initial state of the trajectory.

◆ SetLowerInputBound()

template<typename T >
requires ScalarLike<T> || MatrixLike<T>
void sleipnir::OCPSolver::SetLowerInputBound ( const T &  lowerBound)
inline

Convenience function to set a lower bound on the input.

Parameters
lowerBoundThe lower bound that inputs must always be above. Must be shaped (numInputs)x1.

◆ SetMaxTimestep()

void sleipnir::OCPSolver::SetMaxTimestep ( std::chrono::duration< double maxTimestep)
inline

Convenience function to set an upper bound on the timestep.

Parameters
maxTimestepThe maximum timestep.

◆ SetMinTimestep()

void sleipnir::OCPSolver::SetMinTimestep ( std::chrono::duration< double minTimestep)
inline

Convenience function to set a lower bound on the timestep.

Parameters
minTimestepThe minimum timestep.

◆ SetUpperInputBound()

template<typename T >
requires ScalarLike<T> || MatrixLike<T>
void sleipnir::OCPSolver::SetUpperInputBound ( const T &  upperBound)
inline

Convenience function to set an upper bound on the input.

Parameters
upperBoundThe upper bound that inputs must always be below. Must be shaped (numInputs)x1.

◆ U()

VariableMatrix & sleipnir::OCPSolver::U ( )
inline

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

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

Returns
The input variable matrix.

◆ X()

VariableMatrix & sleipnir::OCPSolver::X ( )
inline

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

Shaped (numStates)x(numSteps+1).

Returns
The state variable matrix.

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