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

#include <sleipnir/optimization/problem.hpp>

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

Public Member Functions

 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::Problem< Scalar >

This class allows the user to pose a constrained nonlinear optimization problem in natural mathematical notation and solve it.

This class supports problems of the form:

      minₓ f(x)
subject to cₑ(x) = 0
           cᵢ(x) ≥ 0

where f(x) is the scalar cost function, x is the vector of decision variables (variables the solver can tweak to minimize the cost function), cᵢ(x) are the inequality constraints, and cₑ(x) are the equality constraints. Constraints are equations or inequalities of the decision variables that constrain what values the solver is allowed to use when searching for an optimal solution.

The nice thing about this class is users don't have to put their system in the form shown above manually; they can write it in natural mathematical form and it'll be converted for them.

Template Parameters
ScalarScalar type.

Constructor & Destructor Documentation

◆ Problem()

template<typename Scalar >
slp::Problem< Scalar >::Problem ( )
defaultnoexcept

Construct the optimization problem.

Member Function Documentation

◆ add_callback() [1/2]

template<typename Scalar >
template<typename F >
requires requires(F callback, const IterationInfo<Scalar>& info) { { callback(info) } -> std::same_as<void>; }
void slp::Problem< Scalar >::add_callback ( F &&  callback)
inline

Adds a callback to be called at the beginning of each solver iteration.

The callback for this overload should return void.

Parameters
callbackThe callback.

◆ add_callback() [2/2]

template<typename Scalar >
template<typename F >
requires requires(F callback, const IterationInfo<Scalar>& info) { { callback(info) } -> std::same_as<bool>; }
void slp::Problem< Scalar >::add_callback ( F &&  callback)
inline

Adds a callback to be called at the beginning of each solver iteration.

The callback for this overload should return bool.

Parameters
callbackThe callback. Returning true from the callback causes the solver to exit early with the solution it has so far.

◆ add_persistent_callback()

template<typename Scalar >
template<typename F >
requires requires(F callback, const IterationInfo<Scalar>& info) { { callback(info) } -> std::same_as<bool>; }
void slp::Problem< Scalar >::add_persistent_callback ( F &&  callback)
inline

Adds a callback to be called at the beginning of each solver iteration.

Language bindings should call this in the Problem constructor to register callbacks that shouldn't be removed by clear_callbacks(). Persistent callbacks run after non-persistent callbacks.

Parameters
callbackThe callback. Returning true from the callback causes the solver to exit early with the solution it has so far.

◆ clear_callbacks()

template<typename Scalar >
void slp::Problem< Scalar >::clear_callbacks ( )
inline

Clears the registered callbacks.

◆ cost_function_type()

template<typename Scalar >
ExpressionType slp::Problem< Scalar >::cost_function_type ( ) const
inline

Returns the cost function's type.

Returns
The cost function's type.

◆ decision_variable() [1/2]

template<typename Scalar >
Variable< Scalar > slp::Problem< Scalar >::decision_variable ( )
inline

Create a decision variable in the optimization problem.

Returns
A decision variable in the optimization problem.

◆ decision_variable() [2/2]

template<typename Scalar >
VariableMatrix< Scalar > slp::Problem< Scalar >::decision_variable ( int  rows,
int  cols = 1 
)
inline

Create a matrix of decision variables in the optimization problem.

Parameters
rowsNumber of matrix rows.
colsNumber of matrix columns.
Returns
A matrix of decision variables in the optimization problem.

◆ equality_constraint_type()

template<typename Scalar >
ExpressionType slp::Problem< Scalar >::equality_constraint_type ( ) const
inline

Returns the type of the highest order equality constraint.

Returns
The type of the highest order equality constraint.

◆ inequality_constraint_type()

template<typename Scalar >
ExpressionType slp::Problem< Scalar >::inequality_constraint_type ( ) const
inline

Returns the type of the highest order inequality constraint.

Returns
The type of the highest order inequality constraint.

◆ maximize() [1/2]

template<typename Scalar >
void slp::Problem< Scalar >::maximize ( const Variable< Scalar > &  objective)
inline

Tells the solver to maximize the output of the given objective function.

Note that this is optional. If only constraints are specified, the solver will find the closest solution to the initial conditions that's in the feasible set.

Parameters
objectiveThe objective function to maximize.

◆ maximize() [2/2]

template<typename Scalar >
void slp::Problem< Scalar >::maximize ( Variable< Scalar > &&  objective)
inline

Tells the solver to maximize the output of the given objective function.

Note that this is optional. If only constraints are specified, the solver will find the closest solution to the initial conditions that's in the feasible set.

Parameters
objectiveThe objective function to maximize.

◆ minimize() [1/2]

template<typename Scalar >
void slp::Problem< Scalar >::minimize ( const Variable< Scalar > &  cost)
inline

Tells the solver to minimize the output of the given cost function.

Note that this is optional. If only constraints are specified, the solver will find the closest solution to the initial conditions that's in the feasible set.

Parameters
costThe cost function to minimize.

◆ minimize() [2/2]

template<typename Scalar >
void slp::Problem< Scalar >::minimize ( Variable< Scalar > &&  cost)
inline

Tells the solver to minimize the output of the given cost function.

Note that this is optional. If only constraints are specified, the solver will find the closest solution to the initial conditions that's in the feasible set.

Parameters
costThe cost function to minimize.

◆ solve()

template<typename Scalar >
ExitStatus slp::Problem< Scalar >::solve ( const Options options = Options{},
bool  spy = false 
)
inline

Solve the optimization problem. The solution will be stored in the original variables used to construct the problem.

Parameters
optionsSolver options.
spyEnables writing sparsity patterns of H, Aₑ, and Aᵢ to files named H.spy, A_e.spy, and A_i.spy respectively during solve. Use tools/spy.py to plot them.
Returns
The solver status.

◆ subject_to() [1/4]

template<typename Scalar >
void slp::Problem< Scalar >::subject_to ( const EqualityConstraints< Scalar > &  constraint)
inline

Tells the solver to solve the problem while satisfying the given equality constraint.

Parameters
constraintThe constraint to satisfy.

◆ subject_to() [2/4]

template<typename Scalar >
void slp::Problem< Scalar >::subject_to ( const InequalityConstraints< Scalar > &  constraint)
inline

Tells the solver to solve the problem while satisfying the given inequality constraint.

Parameters
constraintThe constraint to satisfy.

◆ subject_to() [3/4]

template<typename Scalar >
void slp::Problem< Scalar >::subject_to ( EqualityConstraints< Scalar > &&  constraint)
inline

Tells the solver to solve the problem while satisfying the given equality constraint.

Parameters
constraintThe constraint to satisfy.

◆ subject_to() [4/4]

template<typename Scalar >
void slp::Problem< Scalar >::subject_to ( InequalityConstraints< Scalar > &&  constraint)
inline

Tells the solver to solve the problem while satisfying the given inequality constraint.

Parameters
constraintThe constraint to satisfy.

◆ symmetric_decision_variable()

template<typename Scalar >
VariableMatrix< Scalar > slp::Problem< Scalar >::symmetric_decision_variable ( int  rows)
inline

Create a symmetric matrix of decision variables in the optimization problem.

Variable instances are reused across the diagonal, which helps reduce problem dimensionality.

Parameters
rowsNumber of matrix rows.
Returns
A symmetric matrix of decision varaibles in the optimization problem.

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