63 m_decisionVariables.emplace_back();
64 return m_decisionVariables.back();
75 m_decisionVariables.reserve(m_decisionVariables.size() + rows * cols);
79 for (
int row = 0; row < rows; ++row) {
80 for (
int col = 0; col < cols; ++col) {
81 m_decisionVariables.emplace_back();
82 vars(row, col) = m_decisionVariables.back();
107 m_decisionVariables.reserve(m_decisionVariables.size() +
108 (rows * rows + rows) / 2);
112 for (
int row = 0; row < rows; ++row) {
113 for (
int col = 0; col <= row; ++col) {
114 m_decisionVariables.emplace_back();
115 vars(row, col) = m_decisionVariables.back();
116 vars(col, row) = m_decisionVariables.back();
134 status.costFunctionType = m_f.value().
Type();
147 m_f = std::move(cost);
148 status.costFunctionType = m_f.value().Type();
163 status.costFunctionType = m_f.value().
Type();
177 m_f = -std::move(objective);
178 status.costFunctionType = m_f.value().Type();
190 status.equalityConstraintType =
191 std::max(status.equalityConstraintType, c.Type());
194 m_equalityConstraints.reserve(m_equalityConstraints.size() +
197 std::back_inserter(m_equalityConstraints));
208 for (
const auto& c : constraint.constraints) {
209 status.equalityConstraintType =
210 std::max(status.equalityConstraintType, c.Type());
213 m_equalityConstraints.reserve(m_equalityConstraints.size() +
214 constraint.constraints.size());
215 std::copy(constraint.constraints.begin(), constraint.constraints.end(),
216 std::back_inserter(m_equalityConstraints));
228 status.inequalityConstraintType =
229 std::max(status.inequalityConstraintType, c.Type());
232 m_inequalityConstraints.reserve(m_inequalityConstraints.size() +
235 std::back_inserter(m_inequalityConstraints));
246 for (
const auto& c : constraint.constraints) {
247 status.inequalityConstraintType =
248 std::max(status.inequalityConstraintType, c.Type());
251 m_inequalityConstraints.reserve(m_inequalityConstraints.size() +
252 constraint.constraints.size());
253 std::copy(constraint.constraints.begin(), constraint.constraints.end(),
254 std::back_inserter(m_inequalityConstraints));
265 Eigen::VectorXd x{m_decisionVariables.size()};
266 for (
size_t i = 0; i < m_decisionVariables.size(); ++i) {
267 x(i) = m_decisionVariables[i].Value();
273 if (!m_f.has_value()) {
277 if (config.diagnostics) {
278 constexpr std::array kExprTypeToName{
"empty",
"constant",
"linear",
279 "quadratic",
"nonlinear"};
283 "The cost function is {}.",
284 kExprTypeToName[
static_cast<int>(status.costFunctionType)]);
286 "The equality constraints are {}.",
287 kExprTypeToName[
static_cast<int>(status.equalityConstraintType)]);
289 "The inequality constraints are {}.",
290 kExprTypeToName[
static_cast<int>(status.inequalityConstraintType)]);
295 m_decisionVariables.size());
297 m_equalityConstraints.size());
299 m_inequalityConstraints.size());
303 if (status.costFunctionType <= ExpressionType::kConstant &&
304 status.equalityConstraintType <= ExpressionType::kConstant &&
305 status.inequalityConstraintType <= ExpressionType::kConstant) {
310 if (m_equalityConstraints.empty() && m_inequalityConstraints.empty()) {
311 Newton(m_decisionVariables, m_f.value(), m_callback, config, x, &status);
312 }
else if (m_inequalityConstraints.empty()) {
313 SQP(m_decisionVariables, m_equalityConstraints, m_f.value(), m_callback,
316 Eigen::VectorXd s = Eigen::VectorXd::Ones(m_inequalityConstraints.size());
318 m_inequalityConstraints, m_f.value(), m_callback, config,
319 false, x, s, &status);
322 if (config.diagnostics) {
327 VariableMatrix{m_decisionVariables}.SetValue(x);
339 template <
typename F>
340 requires requires(F callback,
const SolverIterationInfo& info) {
341 { callback(info) } -> std::same_as<void>;
344 m_callback = [=, callback = std::forward<F>(callback)](
359 template <
typename F>
361 { callback(info) } -> std::same_as<bool>;
364 m_callback = std::forward<F>(callback);
373 std::optional<Variable> m_f;
#define SLEIPNIR_DLLEXPORT
Definition SymbolExports.hpp:34
Definition OptimizationProblem.hpp:51
void SubjectTo(InequalityConstraints &&constraint)
Definition OptimizationProblem.hpp:244
void Callback(F &&callback)
Definition OptimizationProblem.hpp:363
OptimizationProblem() noexcept=default
void SubjectTo(EqualityConstraints &&constraint)
Definition OptimizationProblem.hpp:206
void Callback(F &&callback)
Definition OptimizationProblem.hpp:343
VariableMatrix DecisionVariable(int rows, int cols=1)
Definition OptimizationProblem.hpp:74
SolverStatus Solve(const SolverConfig &config=SolverConfig{})
Definition OptimizationProblem.hpp:263
void SubjectTo(const EqualityConstraints &constraint)
Definition OptimizationProblem.hpp:187
void Minimize(const Variable &cost)
Definition OptimizationProblem.hpp:132
void Minimize(Variable &&cost)
Definition OptimizationProblem.hpp:146
void SubjectTo(const InequalityConstraints &constraint)
Definition OptimizationProblem.hpp:225
void Maximize(Variable &&objective)
Definition OptimizationProblem.hpp:175
void Maximize(const Variable &objective)
Definition OptimizationProblem.hpp:160
VariableMatrix SymmetricDecisionVariable(int rows)
Definition OptimizationProblem.hpp:99
Definition VariableMatrix.hpp:28
Definition Variable.hpp:33
ExpressionType Type() const
Definition Variable.hpp:219
Definition small_vector.hpp:3616
Definition Expression.hpp:18
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)
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)
SLEIPNIR_DLLEXPORT constexpr std::string_view ToMessage(const SolverExitCondition &exitCondition)
Definition SolverExitCondition.hpp:47
void println(std::format_string< T... > fmt, T &&... args)
Definition Print.hpp:38
SLEIPNIR_DLLEXPORT void Newton(std::span< Variable > decisionVariables, Variable &f, function_ref< bool(const SolverIterationInfo &info)> callback, const SolverConfig &config, Eigen::VectorXd &x, SolverStatus *status)
Definition Variable.hpp:547
small_vector< Variable > constraints
A vector of scalar equality constraints.
Definition Variable.hpp:549
Definition Variable.hpp:609
small_vector< Variable > constraints
A vector of scalar inequality constraints.
Definition Variable.hpp:611
Definition SolverConfig.hpp:15
Definition SolverIterationInfo.hpp:13
Definition SolverStatus.hpp:15
SolverExitCondition exitCondition
The solver's exit condition.
Definition SolverStatus.hpp:26