alpaqa 0.0.1
Nonconvex constrained optimization
polymorphic-panoc-direction.hpp
Go to the documentation of this file.
1#pragma once
4
5#include <memory>
6
7#include <pybind11/cast.h>
8#include <pybind11/pytypes.h>
9namespace py = pybind11;
10
11namespace alpaqa {
12
14 : public std::enable_shared_from_this<PolymorphicPANOCDirectionBase> {
15 public:
16 virtual ~PolymorphicPANOCDirectionBase() = default;
17 virtual void initialize(crvec x₀, crvec x̂₀, crvec p₀, crvec grad₀) = 0;
18 virtual bool update(crvec xₖ, crvec xₖ₊₁, crvec pₖ, crvec pₖ₊₁,
19 crvec grad_new, const Box &C, real_t γ_new) = 0;
20 virtual bool apply(crvec xₖ, crvec x̂ₖ, crvec pₖ, real_t γ, rvec qₖ) = 0;
21 virtual void changed_γ(real_t γₖ, real_t old_γₖ) = 0;
22 virtual void reset() = 0;
23 virtual std::string get_name() const = 0;
24 vec apply_ret(crvec xₖ, crvec x̂ₖ, crvec pₖ, real_t γ) {
25 vec qₖ(pₖ.size());
26 apply(xₖ, x̂ₖ, pₖ, γ, qₖ);
27 return qₖ;
28 }
29 virtual py::object get_params() const = 0;
30};
31
34 public:
35 void initialize(crvec x₀, crvec x̂₀, crvec p₀, crvec grad₀) override {
36 PYBIND11_OVERRIDE_PURE(void, PolymorphicPANOCDirectionBase, initialize,
37 x₀, x̂₀, p₀, grad₀);
38 }
39 bool update(crvec xₖ, crvec xₖ₊₁, crvec pₖ, crvec pₖ₊₁, crvec grad_new,
40 const Box &C, real_t γ_new) override {
41 PYBIND11_OVERRIDE_PURE(bool, PolymorphicPANOCDirectionBase, update, xₖ,
42 xₖ₊₁, pₖ, pₖ₊₁, grad_new, C, γ_new);
43 }
44 bool apply(crvec xₖ, crvec x̂ₖ, crvec pₖ, real_t γ, rvec qₖ) override {
45 PYBIND11_OVERRIDE_PURE(bool, PolymorphicPANOCDirectionBase, apply, xₖ,
46 x̂ₖ, pₖ, γ, qₖ);
47 }
48 void changed_γ(real_t γₖ, real_t old_γₖ) override {
49 PYBIND11_OVERRIDE_PURE(void, PolymorphicPANOCDirectionBase, changed_γ,
50 γₖ, old_γₖ);
51 }
52 void reset() override {
53 PYBIND11_OVERRIDE_PURE(void, PolymorphicPANOCDirectionBase, reset, );
54 }
55 std::string get_name() const override {
56 PYBIND11_OVERRIDE_PURE(std::string, PolymorphicPANOCDirectionBase,
57 get_name, );
58 }
59 py::object get_params() const override {
60 PYBIND11_OVERRIDE_PURE(py::object, PolymorphicPANOCDirectionBase,
61 get_params, );
62 }
63};
64
65template <>
67 using DirectionPtr = std::shared_ptr<PolymorphicPANOCDirectionBase>;
69
70 PANOCDirection(const DirectionPtr &direction) : direction(direction) {}
72 : direction(std::move(direction)) {}
73
74 void initialize(crvec x₀, crvec x̂₀, crvec p₀, crvec grad₀) {
75 direction->initialize(x₀, x̂₀, p₀, grad₀);
76 }
77 bool update(crvec xₖ, crvec xₖ₊₁, crvec pₖ, crvec pₖ₊₁, crvec grad_new,
78 const Box &C, real_t γ_new) {
79 return direction->update(xₖ, xₖ₊₁, pₖ, pₖ₊₁, grad_new, C, γ_new);
80 }
81 bool apply(crvec xₖ, crvec x̂ₖ, crvec pₖ, real_t γ, rvec qₖ) {
82 return direction->apply(xₖ, x̂ₖ, pₖ, γ, qₖ);
83 }
84 void changed_γ(real_t γₖ, real_t old_γₖ) {
85 direction->changed_γ(γₖ, old_γₖ);
86 }
87 void reset() { direction->reset(); }
88 std::string get_name() const { return direction->get_name(); }
89};
90
91template <class DirectionProviderT>
93
94 public:
95 using DirectionProvider = DirectionProviderT;
96
98 : direction_provider(std::forward<DirectionProvider>(direction)) {}
100 : direction_provider(direction) {}
101
102 void initialize(crvec x₀, crvec x̂₀, crvec p₀, crvec grad₀) override {
103 direction_provider.initialize(x₀, x̂₀, p₀, grad₀);
104 }
105 bool update(crvec xₖ, crvec xₖ₊₁, crvec pₖ, crvec pₖ₊₁, crvec grad_new,
106 const Box &C, real_t γ_new) override {
107 return direction_provider.update(xₖ, xₖ₊₁, pₖ, pₖ₊₁, grad_new, C,
108 γ_new);
109 }
110 bool apply(crvec xₖ, crvec x̂ₖ, crvec pₖ, real_t γ, rvec qₖ) override {
111 return direction_provider.apply(xₖ, x̂ₖ, pₖ, γ, qₖ);
112 }
113 void changed_γ(real_t γₖ, real_t old_γₖ) override {
114 direction_provider.changed_γ(γₖ, old_γₖ);
115 }
116 void reset() override { direction_provider.reset(); }
117 std::string get_name() const override {
118 return direction_provider.get_name();
119 }
120 py::object get_params() const override {
121 return py::cast(direction_provider.get_params());
122 }
123
124 private:
126};
127
129
130} // namespace alpaqa
virtual void changed_γ(real_t γₖ, real_t old_γₖ)=0
virtual ~PolymorphicPANOCDirectionBase()=default
virtual void initialize(crvec x₀, crvec x̂₀, crvec p₀, crvec grad₀)=0
virtual bool apply(crvec xₖ, crvec x̂ₖ, crvec pₖ, real_t γ, rvec qₖ)=0
vec apply_ret(crvec xₖ, crvec x̂ₖ, crvec pₖ, real_t γ)
virtual std::string get_name() const =0
virtual py::object get_params() const =0
virtual bool update(crvec xₖ, crvec xₖ₊₁, crvec pₖ, crvec pₖ₊₁, crvec grad_new, const Box &C, real_t γ_new)=0
bool update(crvec xₖ, crvec xₖ₊₁, crvec pₖ, crvec pₖ₊₁, crvec grad_new, const Box &C, real_t γ_new) override
bool apply(crvec xₖ, crvec x̂ₖ, crvec pₖ, real_t γ, rvec qₖ) override
void initialize(crvec x₀, crvec x̂₀, crvec p₀, crvec grad₀) override
void changed_γ(real_t γₖ, real_t old_γₖ) override
bool update(crvec xₖ, crvec xₖ₊₁, crvec pₖ, crvec pₖ₊₁, crvec grad_new, const Box &C, real_t γ_new) override
PolymorphicPANOCDirection(DirectionProvider &&direction)
bool apply(crvec xₖ, crvec x̂ₖ, crvec pₖ, real_t γ, rvec qₖ) override
void initialize(crvec x₀, crvec x̂₀, crvec p₀, crvec grad₀) override
void changed_γ(real_t γₖ, real_t old_γₖ) override
PANOCDirection< DirectionProvider > direction_provider
PolymorphicPANOCDirection(const DirectionProvider &direction)
Eigen::Ref< const vec > crvec
Default type for immutable references to vectors.
Definition: vec.hpp:18
realvec vec
Default type for vectors.
Definition: vec.hpp:14
double real_t
Default floating point type.
Definition: vec.hpp:8
Eigen::Ref< vec > rvec
Default type for mutable references to vectors.
Definition: vec.hpp:16
bool update(crvec xₖ, crvec xₖ₊₁, crvec pₖ, crvec pₖ₊₁, crvec grad_new, const Box &C, real_t γ_new)
void initialize(crvec x₀, crvec x̂₀, crvec p₀, crvec grad₀)
std::shared_ptr< PolymorphicPANOCDirectionBase > DirectionPtr
bool apply(crvec xₖ, crvec x̂ₖ, crvec pₖ, real_t γ, rvec qₖ)