62 m_decisionVariables.emplace_back();
63 return m_decisionVariables.back();
74 m_decisionVariables.reserve(m_decisionVariables.size() + rows * cols);
78 for (
int row = 0; row < rows; ++row) {
79 for (
int col = 0; col < cols; ++col) {
80 m_decisionVariables.emplace_back();
81 vars(row, col) = m_decisionVariables.back();
106 m_decisionVariables.reserve(m_decisionVariables.size() +
107 (rows * rows + rows) / 2);
111 for (
int row = 0; row < rows; ++row) {
112 for (
int col = 0; col <= row; ++col) {
113 m_decisionVariables.emplace_back();
114 vars(row, col) = m_decisionVariables.back();
115 vars(col, row) = m_decisionVariables.back();
133 status.costFunctionType = m_f.value().
Type();
146 m_f = std::move(cost);
147 status.costFunctionType = m_f.value().Type();
162 status.costFunctionType = m_f.value().
Type();
176 m_f = -std::move(objective);
177 status.costFunctionType = m_f.value().Type();
189 status.equalityConstraintType =
190 std::max(status.equalityConstraintType, c.Type());
193 m_equalityConstraints.reserve(m_equalityConstraints.size() +
196 std::back_inserter(m_equalityConstraints));
207 for (
const auto& c : constraint.constraints) {
208 status.equalityConstraintType =
209 std::max(status.equalityConstraintType, c.Type());
212 m_equalityConstraints.reserve(m_equalityConstraints.size() +
213 constraint.constraints.size());
214 std::copy(constraint.constraints.begin(), constraint.constraints.end(),
215 std::back_inserter(m_equalityConstraints));
227 status.inequalityConstraintType =
228 std::max(status.inequalityConstraintType, c.Type());
231 m_inequalityConstraints.reserve(m_inequalityConstraints.size() +
234 std::back_inserter(m_inequalityConstraints));
245 for (
const auto& c : constraint.constraints) {
246 status.inequalityConstraintType =
247 std::max(status.inequalityConstraintType, c.Type());
250 m_inequalityConstraints.reserve(m_inequalityConstraints.size() +
251 constraint.constraints.size());
252 std::copy(constraint.constraints.begin(), constraint.constraints.end(),
253 std::back_inserter(m_inequalityConstraints));
264 Eigen::VectorXd x{m_decisionVariables.size()};
265 for (
size_t i = 0; i < m_decisionVariables.size(); ++i) {
266 x(i) = m_decisionVariables[i].Value();
269 status.exitCondition = SolverExitCondition::kSuccess;
272 if (!m_f.has_value()) {
276 if (config.diagnostics) {
277 constexpr std::array kExprTypeToName{
"empty",
"constant",
"linear",
278 "quadratic",
"nonlinear"};
282 "The cost function is {}.",
283 kExprTypeToName[
static_cast<int>(status.costFunctionType)]);
285 "The equality constraints are {}.",
286 kExprTypeToName[
static_cast<int>(status.equalityConstraintType)]);
288 "The inequality constraints are {}.",
289 kExprTypeToName[
static_cast<int>(status.inequalityConstraintType)]);
294 m_decisionVariables.size());
296 m_equalityConstraints.size());
298 m_inequalityConstraints.size());
302 if (status.costFunctionType <= ExpressionType::kConstant &&
303 status.equalityConstraintType <= ExpressionType::kConstant &&
304 status.inequalityConstraintType <= ExpressionType::kConstant) {
309 if (m_inequalityConstraints.empty()) {
310 SQP(m_decisionVariables, m_equalityConstraints, m_f.value(), m_callback,
313 Eigen::VectorXd s = Eigen::VectorXd::Ones(m_inequalityConstraints.size());
315 m_inequalityConstraints, m_f.value(), m_callback, config,
316 false, x, s, &status);
319 if (config.diagnostics) {
324 VariableMatrix{m_decisionVariables}.SetValue(x);
336 template <
typename F>
337 requires requires(F callback,
const SolverIterationInfo& info) {
338 { callback(info) } -> std::same_as<void>;
341 m_callback = [=, callback = std::forward<F>(callback)](
356 template <
typename F>
358 { callback(info) } -> std::same_as<bool>;
361 m_callback = std::forward<F>(callback);
370 std::optional<Variable> m_f;
This file defines the SmallVector class.
#define SLEIPNIR_DLLEXPORT
Definition SymbolExports.hpp:34
This class allows the user to pose a constrained nonlinear optimization problem in natural mathematic...
Definition OptimizationProblem.hpp:50
void SubjectTo(InequalityConstraints &&constraint)
Tells the solver to solve the problem while satisfying the given inequality constraint.
Definition OptimizationProblem.hpp:243
void Callback(F &&callback)
Sets a callback to be called at each solver iteration.
Definition OptimizationProblem.hpp:360
OptimizationProblem() noexcept=default
Construct the optimization problem.
void SubjectTo(EqualityConstraints &&constraint)
Tells the solver to solve the problem while satisfying the given equality constraint.
Definition OptimizationProblem.hpp:205
void Callback(F &&callback)
Sets a callback to be called at each solver iteration.
Definition OptimizationProblem.hpp:340
VariableMatrix DecisionVariable(int rows, int cols=1)
Create a matrix of decision variables in the optimization problem.
Definition OptimizationProblem.hpp:73
SolverStatus Solve(const SolverConfig &config=SolverConfig{})
Solve the optimization problem.
Definition OptimizationProblem.hpp:262
void SubjectTo(const EqualityConstraints &constraint)
Tells the solver to solve the problem while satisfying the given equality constraint.
Definition OptimizationProblem.hpp:186
void Minimize(const Variable &cost)
Tells the solver to minimize the output of the given cost function.
Definition OptimizationProblem.hpp:131
void Minimize(Variable &&cost)
Tells the solver to minimize the output of the given cost function.
Definition OptimizationProblem.hpp:145
void SubjectTo(const InequalityConstraints &constraint)
Tells the solver to solve the problem while satisfying the given inequality constraint.
Definition OptimizationProblem.hpp:224
void Maximize(Variable &&objective)
Tells the solver to maximize the output of the given objective function.
Definition OptimizationProblem.hpp:174
void Maximize(const Variable &objective)
Tells the solver to maximize the output of the given objective function.
Definition OptimizationProblem.hpp:159
VariableMatrix SymmetricDecisionVariable(int rows)
Create a symmetric matrix of decision variables in the optimization problem.
Definition OptimizationProblem.hpp:98
An autodiff variable pointing to an expression node.
Definition Variable.hpp:31
ExpressionType Type() const
Returns the type of this expression (constant, linear, quadratic, or nonlinear).
Definition Variable.hpp:208
A matrix of autodiff variables.
Definition VariableMatrix.hpp:28
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition SmallVector.h:1212
Definition Hessian.hpp:18
void println(fmt::format_string< T... > fmt, T &&... args)
Wrapper around fmt::println() that squelches write failure exceptions.
Definition Print.hpp:43
SLEIPNIR_DLLEXPORT void SQP(std::span< Variable > decisionVariables, std::span< Variable > equalityConstraints, Variable &f, function_ref< bool(const SolverIterationInfo &info)> callback, const SolverConfig &config, Eigen::VectorXd &x, SolverStatus *status)
Finds the optimal solution to a nonlinear program using Sequential Quadratic Programming (SQP).
SLEIPNIR_DLLEXPORT void InteriorPoint(std::span< Variable > decisionVariables, std::span< Variable > equalityConstraints, std::span< Variable > inequalityConstraints, Variable &f, function_ref< bool(const SolverIterationInfo &info)> callback, const SolverConfig &config, bool feasibilityRestoration, Eigen::VectorXd &x, Eigen::VectorXd &s, SolverStatus *status)
Finds the optimal solution to a nonlinear program using the interior-point method.
SLEIPNIR_DLLEXPORT constexpr std::string_view ToMessage(const SolverExitCondition &exitCondition)
Returns user-readable message corresponding to the exit condition.
Definition SolverExitCondition.hpp:47
A vector of equality constraints of the form cₑ(x) = 0.
Definition Variable.hpp:535
wpi::SmallVector< Variable > constraints
A vector of scalar equality constraints.
Definition Variable.hpp:537
A vector of inequality constraints of the form cᵢ(x) ≥ 0.
Definition Variable.hpp:597
wpi::SmallVector< Variable > constraints
A vector of scalar inequality constraints.
Definition Variable.hpp:599
Solver configuration.
Definition SolverConfig.hpp:15
Solver iteration information exposed to a user callback.
Definition SolverIterationInfo.hpp:13
Return value of OptimizationProblem::Solve() containing the cost function and constraint types and so...
Definition SolverStatus.hpp:15