EllAlgo 1.6.13
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
60 auto operator=(const EllBase& E) -> EllBase& = delete;
61
62 public:
69 EllBase(const Vec& val, Arr x)
70 : _n{static_cast<std::size_t>(x.size())}, _xc{std::move(x)}, _mgr(val, _n) {}
71
78 EllBase(const double alpha, Arr x)
79 : _n{static_cast<std::size_t>(x.size())}, _xc{std::move(x)}, _mgr(alpha, _n) {}
80
86 EllBase(EllBase&& E) noexcept = default;
87
94 auto operator=(EllBase&& E) noexcept -> EllBase& = default;
95
99 ~EllBase() = default;
100
106 explicit EllBase(const EllBase& E) = default;
107
113 auto xc() const -> Arr { return this->_xc; }
114
120 void set_xc(const Arr& xc) { this->_xc = xc; }
121
127 constexpr auto tsq() const -> double { return this->_mgr.tsq(); }
128
135
163 template <typename T> auto update_bias_cut(const std::pair<Arr, T>& cut) -> CutStatus {
164 return this->_update_core(cut, [this](Vec& grad, const T& beta) {
165 if constexpr (Stable) {
166 return this->_mgr.update_stable_bias_cut(grad, beta);
167 } else {
168 return this->_mgr.update_bias_cut(grad, beta);
169 }
170 });
171 }
172
200 template <typename T> auto update_central_cut(const std::pair<Arr, T>& cut) -> CutStatus {
201 return this->_update_core(cut, [this](Vec& grad, const T& beta) {
202 if constexpr (Stable) {
203 return this->_mgr.update_stable_central_cut(grad, beta);
204 } else {
205 return this->_mgr.update_central_cut(grad, beta);
206 }
207 });
208 }
209
239 template <typename T> auto update_q(const std::pair<Arr, T>& cut) -> CutStatus {
240 return this->_update_core(cut, [this](Vec& grad, const T& beta) {
241 if constexpr (Stable) {
242 return this->_mgr.update_stable_q(grad, beta);
243 } else {
244 return this->_mgr.update_q(grad, beta);
245 }
246 });
247 }
248
249 protected:
259 template <typename T, typename Fn>
260 auto _update_core(const std::pair<Arr, T>& cut, Fn&& cut_strategy) -> CutStatus {
261 const auto& grad = cut.first;
262 const auto& beta = cut.second;
263 std::valarray<double> g(this->_n);
264 for (size_t i = 0; i != this->_n; ++i) {
265 g[i] = grad[i];
266 }
267
268 auto result = cut_strategy(g, beta);
269
270 if (result == CutStatus::Success) {
271 for (size_t i = 0; i != this->_n; ++i) {
272 this->_xc[i] -= g[i];
273 }
274 }
275
276 return result;
277 }
278}; // } 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:127
EllBase(EllBase &&E) noexcept=default
Construct a new EllBase object (move constructor)
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:120
~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:260
auto update_q(const std::pair< Arr, T > &cut) -> CutStatus
Update ellipsoid using a cut with a specific Q matrix.
Definition ell_base.hpp:239
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:69
auto xc() const -> Arr
Get the center of the ellipsoid.
Definition ell_base.hpp:113
void set_use_parallel_cut(bool value)
Set whether to use parallel cut.
Definition ell_base.hpp:134
auto update_central_cut(const std::pair< Arr, T > &cut) -> CutStatus
Update ellipsoid using a central cut.
Definition ell_base.hpp:200
auto update_bias_cut(const std::pair< Arr, T > &cut) -> CutStatus
Update ellipsoid using a deep cut.
Definition ell_base.hpp:163
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:78
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.