WPILibC++ 2027.0.0-alpha-2
Loading...
Searching...
No Matches
slp::Problem Class Reference

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

#include </home/runner/work/allwpilib/allwpilib/wpimath/src/main/native/thirdparty/sleipnir/include/sleipnir/optimization/problem.hpp>

Inheritance diagram for slp::Problem:
slp::OCP

Public Member Functions

 Problem () noexcept=default
 Construct the optimization problem.
 
Variable decision_variable ()
 Create a decision variable in the optimization problem.
 
VariableMatrix decision_variable (int rows, int cols=1)
 Create a matrix of decision variables in the optimization problem.
 
VariableMatrix symmetric_decision_variable (int rows)
 Create a symmetric matrix of decision variables in the optimization problem.
 
void minimize (const Variable &cost)
 Tells the solver to minimize the output of the given cost function.
 
void minimize (Variable &&cost)
 Tells the solver to minimize the output of the given cost function.
 
void maximize (const Variable &objective)
 Tells the solver to maximize the output of the given objective function.
 
void maximize (Variable &&objective)
 Tells the solver to maximize the output of the given objective function.
 
void subject_to (const EqualityConstraints &constraint)
 Tells the solver to solve the problem while satisfying the given equality constraint.
 
void subject_to (EqualityConstraints &&constraint)
 Tells the solver to solve the problem while satisfying the given equality constraint.
 
void subject_to (const InequalityConstraints &constraint)
 Tells the solver to solve the problem while satisfying the given inequality constraint.
 
void subject_to (InequalityConstraints &&constraint)
 Tells the solver to solve the problem while satisfying the given inequality constraint.
 
ExpressionType cost_function_type () const
 Returns the cost function's type.
 
ExpressionType equality_constraint_type () const
 Returns the type of the highest order equality constraint.
 
ExpressionType inequality_constraint_type () const
 Returns the type of the highest order inequality constraint.
 
ExitStatus solve (const Options &options=Options{}, bool spy=false)
 Solve the optimization problem.
 
template<typename F >
requires requires(F callback, const IterationInfo& info) { { callback(info) } -> std::same_as<void>; }
void add_callback (F &&callback)
 Adds a callback to be called at the beginning of each solver iteration.
 
template<typename F >
requires requires(F callback, const IterationInfo& info) { { callback(info) } -> std::same_as<bool>; }
void add_callback (F &&callback)
 Adds a callback to be called at the beginning of each solver iteration.
 
void clear_callbacks ()
 Clears the registered callbacks.
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ Problem()

slp::Problem::Problem ( )
defaultnoexcept

Construct the optimization problem.

Member Function Documentation

◆ add_callback() [1/2]

template<typename F >
requires requires(F callback, const IterationInfo& info) { { callback(info) } -> std::same_as<void>; }
void slp::Problem::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 F >
requires requires(F callback, const IterationInfo& info) { { callback(info) } -> std::same_as<bool>; }
void slp::Problem::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.

◆ clear_callbacks()

void slp::Problem::clear_callbacks ( )
inline

Clears the registered callbacks.

◆ cost_function_type()

ExpressionType slp::Problem::cost_function_type ( ) const
inline

Returns the cost function's type.

Returns
The cost function's type.

◆ decision_variable() [1/2]

Variable slp::Problem::decision_variable ( )
inlinenodiscard

Create a decision variable in the optimization problem.

Returns
A decision variable in the optimization problem.

◆ decision_variable() [2/2]

VariableMatrix slp::Problem::decision_variable ( int rows,
int cols = 1 )
inlinenodiscard

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()

ExpressionType slp::Problem::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()

ExpressionType slp::Problem::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]

void slp::Problem::maximize ( const Variable & 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]

void slp::Problem::maximize ( Variable && 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]

void slp::Problem::minimize ( const Variable & 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]

void slp::Problem::minimize ( Variable && 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()

ExitStatus slp::Problem::solve ( const Options & options = Options{},
bool spy = false )

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]

void slp::Problem::subject_to ( const EqualityConstraints & constraint)
inline

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

Parameters
constraintThe constraint to satisfy.

◆ subject_to() [2/4]

void slp::Problem::subject_to ( const InequalityConstraints & constraint)
inline

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

Parameters
constraintThe constraint to satisfy.

◆ subject_to() [3/4]

void slp::Problem::subject_to ( EqualityConstraints && constraint)
inline

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

Parameters
constraintThe constraint to satisfy.

◆ subject_to() [4/4]

void slp::Problem::subject_to ( InequalityConstraints && constraint)
inline

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

Parameters
constraintThe constraint to satisfy.

◆ symmetric_decision_variable()

VariableMatrix slp::Problem::symmetric_decision_variable ( int rows)
inlinenodiscard

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: