EllAlgo 1.6.14
Loading...
Searching...
No Matches
ell_base.hpp
Go to the documentation of this file.
1
6#pragma once
7
8#include <cstddef>
9#include <utility>
10#include <valarray>
11
12#include "ell_config.hpp"
13#include "ell_core.hpp"
14
49template <typename Arr, bool Stable> class EllBase {
50 public:
51 using Vec = std::valarray<double>;
52 using ArrayType = Arr;
53
54 protected:
55 size_t _n;
58 Vec _g; // scratch buffer for the cut gradient / center displacement
59
61 auto operator=(const EllBase& E) -> EllBase& = delete;
62
63 public:
70 EllBase(const Vec& val, Arr x)
71 : _n{static_cast<std::size_t>(x.size())}, _xc{std::move(x)}, _mgr(val, _n), _g(0.0, _n) {}
72
79 EllBase(const double alpha, Arr x)
80 : _n{static_cast<std::size_t>(x.size())}, _xc{std::move(x)}, _mgr(alpha, _n), _g(0.0, _n) {}
81
87 EllBase(EllBase&& E) noexcept = default;
88
95 auto operator=(EllBase&& E) noexcept -> EllBase& = default;
96
100 ~EllBase() = default;
101
107 explicit EllBase(const EllBase& E) = default;
108
114 auto xc() const -> Arr { return this->_xc; }
115
121 void set_xc(const Arr& xc) { this->_xc = xc; }
122
128 constexpr auto tsq() const -> double { return this->_mgr.tsq(); }
129
136
164 template <typename T> auto update_bias_cut(const std::pair<Arr, T>& cut) -> CutStatus {
165 return this->_update_core(cut, [this](Vec& grad, const T& beta) {
166 if constexpr (Stable) {
167 return this->_mgr.update_stable_bias_cut(grad, beta);
168 } else {
169 return this->_mgr.update_bias_cut(grad, beta);
170 }
171 });
172 }
173
201 template <typename T> auto update_central_cut(const std::pair<Arr, T>& cut) -> CutStatus {
202 return this->_update_core(cut, [this](Vec& grad, const T& beta) {
203 if constexpr (Stable) {
204 return this->_mgr.update_stable_central_cut(grad, beta);
205 } else {
206 return this->_mgr.update_central_cut(grad, beta);
207 }
208 });
209 }
210
240 template <typename T> auto update_q(const std::pair<Arr, T>& cut) -> CutStatus {
241 return this->_update_core(cut, [this](Vec& grad, const T& beta) {
242 if constexpr (Stable) {
243 return this->_mgr.update_stable_q(grad, beta);
244 } else {
245 return this->_mgr.update_q(grad, beta);
246 }
247 });
248 }
249
250 protected:
260 template <typename T, typename Fn>
261 auto _update_core(const std::pair<Arr, T>& cut, Fn&& cut_strategy) -> CutStatus {
262 const auto& grad = cut.first;
263 const auto& beta = cut.second;
264 auto& g = this->_g;
265 for (size_t i = 0; i != this->_n; ++i) {
266 g[i] = grad[i];
267 }
268
269 auto result = cut_strategy(g, beta);
270
271 if (result == CutStatus::Success) {
272 for (size_t i = 0; i != this->_n; ++i) {
273 this->_xc[i] -= g[i];
274 }
275 }
276
277 return result;
278 }
279}; // } EllBase
1D or 2D array backed by std::vector<double> for small optimization problems
Definition arr.hpp:23
Ellipsoid Search Space (shared base)
Definition ell_base.hpp:49
constexpr auto tsq() const -> double
Get the squared radius of the ellipsoid.
Definition ell_base.hpp:128
EllBase(EllBase &&E) noexcept=default
Construct a new EllBase object (move constructor)
Vec _g
Definition ell_base.hpp:58
EllCore _mgr
Definition ell_base.hpp:57
EllBase(const EllBase &E)=default
Construct a new EllBase object (explicit copy)
auto operator=(EllBase &&E) noexcept -> EllBase &=default
Move assignment operator.
void set_xc(const Arr &xc)
Set the center of the ellipsoid.
Definition ell_base.hpp:121
~EllBase()=default
Destroy the EllBase object.
Arr _xc
Definition ell_base.hpp:56
size_t _n
Definition ell_base.hpp:55
auto _update_core(const std::pair< Arr, T > &cut, Fn &&cut_strategy) -> CutStatus
Update ellipsoid core function using the cut(s).
Definition ell_base.hpp:261
auto update_q(const std::pair< Arr, T > &cut) -> CutStatus
Update ellipsoid using a cut with a specific Q matrix.
Definition ell_base.hpp:240
std::valarray< double > Vec
Definition ell_base.hpp:51
EllBase(const Vec &val, Arr x)
Construct a new EllBase object from a vector and an array.
Definition ell_base.hpp:70
auto xc() const -> Arr
Get the center of the ellipsoid.
Definition ell_base.hpp:114
void set_use_parallel_cut(bool value)
Set whether to use parallel cut.
Definition ell_base.hpp:135
auto update_central_cut(const std::pair< Arr, T > &cut) -> CutStatus
Update ellipsoid using a central cut.
Definition ell_base.hpp:201
auto update_bias_cut(const std::pair< Arr, T > &cut) -> CutStatus
Update ellipsoid using a deep cut.
Definition ell_base.hpp:164
auto operator=(const EllBase &E) -> EllBase &=delete
Deleted copy assignment operator (non-copyable).
EllBase(const double alpha, Arr x)
Construct a new EllBase object from an alpha value and an array.
Definition ell_base.hpp:79
Ellipsoid Search Space Core.
Definition ell_core.hpp:44
auto update_q(Vec &grad, const T &beta) -> CutStatus
Update ellipsoid core function using the cut(s)
Definition ell_core.hpp:207
auto update_central_cut(Vec &grad, const T &beta) -> CutStatus
Update ellipsoid core function using the central cut(s)
Definition ell_core.hpp:190
auto update_stable_q(Vec &grad, const T &beta) -> CutStatus
Update ellipsoid core function using the cut(s)
Definition ell_core.hpp:258
auto update_stable_bias_cut(Vec &grad, const T &beta) -> CutStatus
Update ellipsoid core function using the deep cut(s)
Definition ell_core.hpp:224
auto update_bias_cut(Vec &grad, const T &beta) -> CutStatus
Update ellipsoid core function using the deep cut(s)
Definition ell_core.hpp:173
void set_use_parallel_cut(bool value)
Definition ell_core.hpp:160
auto update_stable_central_cut(Vec &grad, const T &beta) -> CutStatus
Update ellipsoid core function using the central cut(s)
Definition ell_core.hpp:241
constexpr auto tsq() const -> double
Get the squared ellipsoid radius τ²
Definition ell_core.hpp:152
auto invalid_value() -> T
Return an invalid/sentinel value for type T.
Definition cutting_plane.hpp:27
Configuration types and constants for the ellipsoid algorithm.
CutStatus
Status of cutting plane operations.
Definition ell_config.hpp:47
@ Success
Cut was successful and ellipsoid was updated.
Ellipsoid search space core with matrix update.