idol
A C++ Framework for Optimization
Loading...
Searching...
No Matches
Point.h
1//
2// Created by henri on 24.10.24.
3//
4
5#ifndef IDOL_POINT_H
6#define IDOL_POINT_H
7
8#include "SparseVector.h"
9#include "types.h"
10#include "idol/mixed-integer/modeling/variables/Var.h"
11#include "idol/general/numericals.h"
12#include <optional>
13
14namespace idol {
15 template<class T> class Point;
16 using PrimalPoint = Point<Var>;
17 using DualPoint = Point<Ctr>;
18}
19
20template<class T>
21class idol::Point : public SparseVector<T, double> {
22 SolutionStatus m_status = Loaded;
23 SolutionReason m_reason = NotSpecified;
24 std::optional<double> m_objective_value;
25public:
26 Point() = default;
27 explicit Point(const SparseVector<T, double>& t_vector) : SparseVector<T, double>(t_vector) {}
28 explicit Point(SparseVector<T, double>&& t_vector) : SparseVector<T, double>(std::move(t_vector)) {}
29
30 [[nodiscard]] SolutionStatus status() const { return m_status; }
31 void set_status(SolutionStatus t_status) { m_status = t_status; }
32
33 [[nodiscard]] SolutionReason reason() const { return m_reason; }
34
35 void set_reason(SolutionReason t_reason) { m_reason = t_reason; }
36
37 [[nodiscard]] double objective_value() const;
38
39 void set_objective_value(double t_objective_value) { m_objective_value = t_objective_value; }
40
41 [[nodiscard]] bool has_objective_value() const { return m_objective_value.has_value(); }
42
43 void reset_objective_value() { m_objective_value.reset(); }
44
45 Point& operator*=(double t_factor) override { return static_cast<Point&>(SparseVector<T, double>::operator*=(t_factor)); }
46 Point& operator+=(const Point& t_other) { return static_cast<Point&>(SparseVector<T, double>::operator+=(t_other)); }
47 Point& operator+=(Point&& t_other) { return static_cast<Point&>(SparseVector<T, double>::operator+=(std::move(t_other))); }
48 Point& operator-=(const Point& t_other) { return static_cast<Point&>(SparseVector<T, double>::operator-=(t_other)); }
49 Point& operator-=(Point&& t_other) { return static_cast<Point&>(SparseVector<T, double>::operator-=(std::move(t_other))); }
50};
51
52template<class T>
54
55 if (m_objective_value.has_value()) {
56 return m_objective_value.value();
57 }
58
59 if (m_status == Unbounded) {
60 return -Inf;
61 }
62
63 return Inf;
64}
65
66namespace idol {
67 template<class T>
68 static std::ostream &operator<<(std::ostream &t_os, const Point<T> &t_point) {
69 t_os << "Status: " << t_point.status() << '\n';
70 t_os << "Reason: " << t_point.reason() << '\n';
71 t_os << "Objective value: " << t_point.objective_value() << '\n';
72 t_os << "Values: " << '\n';
73 t_os << static_cast<const SparseVector<T, double> &>(t_point);
74 return t_os;
75 }
76
77 template<class T> static Point<T> operator+(Point<T>&& t_x, Point<T>&& t_y) { return Point(std::move(t_x)) += t_y; }
78 template<class T> static Point<T> operator+(Point<T>&& t_x, const Point<T>& t_y) { return Point(std::move(t_x)) += t_y; }
79 template<class T> static Point<T> operator+(const Point<T>& t_x, Point<T>&& t_y) { return Point(std::move(t_y)) += t_x; }
80 template<class T> static Point<T> operator+(const Point<T>& t_x, const Point<T>& t_y) { return Point(t_x) += t_y; }
81
82 template<class T> static Point<T> operator*(double t_factor, const Point<T>& t_y) { return Point(t_y) *= t_factor; }
83}
84
85#endif //IDOL_POINT_H