alpaqa pi-pico
Nonconvex constrained optimization
Loading...
Searching...
No Matches
alm-helpers.tpp
Go to the documentation of this file.
1#pragma once
2
4#if ALPAQA_WITH_OCP
6#endif
7
8#include <algorithm>
9#include <stdexcept>
10
11namespace alpaqa::detail {
12
13template <Config Conf>
14struct ALMHelpers {
16
18 real_t Δ, bool first_iter, rvec e,
22 if (norm_e <= params.dual_tolerance) {
23 // Don't update the penalty factors if the constraint violation is
24 // already below the required tolerance.
25 return;
26 }
27 if (params.single_penalty_factor) {
28 if constexpr (requires { Σ(0); }) {
29 if (first_iter || norm_e > θ * old_norm_e) {
30 real_t new_Σ = std::fmin(params.max_penalty, Δ * Σ(0));
31 Σ.setConstant(new_Σ);
32 }
33 } else {
34 throw std::logic_error("This configuration does not support "
35 "single-penalty parameter mode");
36 }
37 } else {
38 auto new_Σ = (e.cwiseAbs() * (Δ / norm_e))
39 .cwiseMax(1)
40 .cwiseProduct(Σ)
41 .cwiseMin(params.max_penalty);
42 if (first_iter) {
43 // Update the penalty factors regardless of previous error
44 // (because we don't have the previous error yet).
45 // TODO: we could in theory evaluate it before the first
46 // iteration, the inner solver computes it anyway, but this may
47 // add unnecessary complexity.
48 Σ = new_Σ;
49 } else {
50 // Decide which constraints' penalty factors to increase.
51 auto incr = e.cwiseAbs().array() > θ * old_e.cwiseAbs().array();
52 Σ = incr.select(new_Σ, Σ);
53 }
54 }
55 }
56
58 const ALMParams<config_t> &params, crvec x0,
59 rvec Σ) {
60 real_t f0 = p.eval_f(x0);
61 vec g0(p.get_m());
62 p.eval_g(x0, g0);
63 // TODO: reuse evaluations of f ang g in PANOC?
65 std::max(real_t(1), std::abs(f0)) /
66 std::max(real_t(1), real_t(0.5) * g0.squaredNorm());
67 σ = std::clamp(σ, params.min_penalty, params.max_penalty);
68 Σ.setConstant(σ);
69 }
70
71#if ALPAQA_WITH_OCP
72 static void initialize_penalty(
74 const ALMParams<config_t> &params, [[maybe_unused]] crvec x0, rvec Σ) {
75 real_t σ = 1;
76 σ = std::clamp(σ, params.min_penalty, params.max_penalty);
77 Σ.setConstant(σ);
78 }
79#endif
80};
81
82// clang-format off
87// clang-format on
88
89} // namespace alpaqa::detail
Nonlinear optimal control problem with finite horizon .
The main polymorphic minimization problem interface.
length_t get_m() const
[Required] Number of constraints.
real_t eval_f(crvec x) const
[Required] Function that evaluates the cost,
void eval_g(crvec x, rvec gx) const
[Required] Function that evaluates the constraints,
#define USING_ALPAQA_CONFIG(Conf)
Definition config.hpp:77
#define ALPAQA_IF_QUADF(...)
Definition config.hpp:223
#define ALPAQA_IF_LONGD(...)
Definition config.hpp:235
#define ALPAQA_IF_FLOAT(...)
Definition config.hpp:229
#define ALPAQA_EXPORT_EXTERN_TEMPLATE(...)
Definition export.hpp:21
real_t initial_penalty_factor
Initial penalty parameter factor.
Definition alm.hpp:37
real_t min_penalty
Minimum penalty factor (used during initialization).
Definition alm.hpp:49
real_t dual_tolerance
Dual tolerance (constraint violation and complementarity).
Definition alm.hpp:27
real_t max_penalty
Maximum penalty factor.
Definition alm.hpp:47
real_t rel_penalty_increase_threshold
Error tolerance for penalty increase.
Definition alm.hpp:43
bool single_penalty_factor
Use one penalty factor for all m constraints.
Definition alm.hpp:62
Parameters for the Augmented Lagrangian solver.
Definition alm.hpp:21
typename Conf::real_t real_t
Definition config.hpp:86
constexpr const auto inf
Definition config.hpp:112
typename Conf::rvec rvec
Definition config.hpp:91
typename Conf::crvec crvec
Definition config.hpp:92
typename Conf::vec vec
Definition config.hpp:88
Double-precision double configuration.
Definition config.hpp:176
Single-precision float configuration.
Definition config.hpp:172
long double configuration.
Definition config.hpp:181
static void initialize_penalty(const TypeErasedControlProblem< config_t > &p, const ALMParams< config_t > &params, crvec x0, rvec Σ)
static void update_penalty_weights(const ALMParams< config_t > &params, real_t Δ, bool first_iter, rvec e, rvec old_e, real_t norm_e, real_t old_norm_e, rvec Σ)
static void initialize_penalty(const TypeErasedProblem< config_t > &p, const ALMParams< config_t > &params, crvec x0, rvec Σ)