WPILibC++ 2025.1.1
Loading...
Searching...
No Matches
sleipnir::OptimizationProblem 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/OptimizationProblem.hpp>

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

Public Member Functions

 OptimizationProblem () noexcept=default
 Construct the optimization problem.
 
Variable DecisionVariable ()
 Create a decision variable in the optimization problem.
 
VariableMatrix DecisionVariable (int rows, int cols=1)
 Create a matrix of decision variables in the optimization problem.
 
VariableMatrix SymmetricDecisionVariable (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 SubjectTo (const EqualityConstraints &constraint)
 Tells the solver to solve the problem while satisfying the given equality constraint.
 
void SubjectTo (EqualityConstraints &&constraint)
 Tells the solver to solve the problem while satisfying the given equality constraint.
 
void SubjectTo (const InequalityConstraints &constraint)
 Tells the solver to solve the problem while satisfying the given inequality constraint.
 
void SubjectTo (InequalityConstraints &&constraint)
 Tells the solver to solve the problem while satisfying the given inequality constraint.
 
SolverStatus Solve (const SolverConfig &config=SolverConfig{})
 Solve the optimization problem.
 
template<typename F >
requires requires(F callback, const SolverIterationInfo& info) { { callback(info) } -> std::same_as<void>; }
void Callback (F &&callback)
 Sets a callback to be called at each solver iteration.
 
template<typename F >
requires requires(F callback, const SolverIterationInfo& info) { { callback(info) } -> std::same_as<bool>; }
void Callback (F &&callback)
 Sets a callback to be called at each solver iteration.
 

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

◆ OptimizationProblem()

sleipnir::OptimizationProblem::OptimizationProblem ( )
defaultnoexcept

Construct the optimization problem.

Member Function Documentation

◆ Callback() [1/2]

template<typename F >
requires requires(F callback, const SolverIterationInfo& info) { { callback(info) } -> std::same_as<void>; }
void sleipnir::OptimizationProblem::Callback ( F && callback)
inline

Sets a callback to be called at each solver iteration.

The callback for this overload should return void.

Parameters
callbackThe callback.

◆ Callback() [2/2]

template<typename F >
requires requires(F callback, const SolverIterationInfo& info) { { callback(info) } -> std::same_as<bool>; }
void sleipnir::OptimizationProblem::Callback ( F && callback)
inline

Sets a callback to be called at 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.

◆ DecisionVariable() [1/2]

Variable sleipnir::OptimizationProblem::DecisionVariable ( )
inlinenodiscard

Create a decision variable in the optimization problem.

◆ DecisionVariable() [2/2]

VariableMatrix sleipnir::OptimizationProblem::DecisionVariable ( 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.

◆ Maximize() [1/2]

void sleipnir::OptimizationProblem::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 sleipnir::OptimizationProblem::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 sleipnir::OptimizationProblem::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 sleipnir::OptimizationProblem::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()

SolverStatus sleipnir::OptimizationProblem::Solve ( const SolverConfig & config = SolverConfig{})
inline

Solve the optimization problem.

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

Parameters
configConfiguration options for the solver.

◆ SubjectTo() [1/4]

void sleipnir::OptimizationProblem::SubjectTo ( const EqualityConstraints & constraint)
inline

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

Parameters
constraintThe constraint to satisfy.

◆ SubjectTo() [2/4]

void sleipnir::OptimizationProblem::SubjectTo ( const InequalityConstraints & constraint)
inline

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

Parameters
constraintThe constraint to satisfy.

◆ SubjectTo() [3/4]

void sleipnir::OptimizationProblem::SubjectTo ( EqualityConstraints && constraint)
inline

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

Parameters
constraintThe constraint to satisfy.

◆ SubjectTo() [4/4]

void sleipnir::OptimizationProblem::SubjectTo ( InequalityConstraints && constraint)
inline

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

Parameters
constraintThe constraint to satisfy.

◆ SymmetricDecisionVariable()

VariableMatrix sleipnir::OptimizationProblem::SymmetricDecisionVariable ( 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.

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