idol
A C++ Framework for Optimization
Loading...
Searching...
No Matches
numericals.h
1//
2// Created by henri on 01/09/22.
3//
4
5#ifndef OPTIMIZE_NUMERICALS_H
6#define OPTIMIZE_NUMERICALS_H
7
8#include <limits>
9#include <cmath>
10#include <iostream>
11#include <iomanip>
12#include <numeric>
13#include <idol/mixed-integer/modeling/Types.h>
14
15namespace idol {
16
17 static constexpr double Inf = 1e20;
18
26 namespace Tolerance {
27
37 static double Sparsity = 1e-8;
38
39 static unsigned int Digits = 8;
40
51 static double MIPRelativeGap = 1e-4;
52
63 static double MIPAbsoluteGap = 1e-5;
64
73 static double Integer = 10e-5;
74
83 static double Feasibility = 10e-6;
84
92 static double Optimality = 10e-6;
93 };
94
95 static bool is_pos_inf(double t_value) {
96 return t_value >= Inf;
97 }
98
99 static bool is_neg_inf(double t_value) {
100 return t_value <= -Inf;
101 }
102
103 static bool is_inf(double t_value) {
104 return is_pos_inf(t_value) || is_neg_inf(t_value);
105 }
106
107 static bool equals(double t_a, double t_b, double t_tolerance) {
108 return std::abs(t_a - t_b) <= t_tolerance;
109 }
110
111 static bool is(double t_a, CtrType t_type, double t_b, double t_tolerance) {
112
113 switch (t_type) {
114 case LessOrEqual:
115 return t_a <= t_b + t_tolerance;
116 case GreaterOrEqual:
117 return t_a >= t_b - t_tolerance;
118 case Equal:
119 return equals(t_a, t_b, t_tolerance);
120 default:;
121 }
122
123 throw Exception("Enum out of bounds.");
124 }
125
126 static double relative_gap(double t_lb, double t_ub) {
127
128 if (is_neg_inf(t_lb) || is_pos_inf(t_ub)) {
129 return Inf;
130 }
131
132 return std::abs(t_lb - t_ub) / (1e-10 + std::abs(t_ub));
133 }
134
135 static double absolute_gap(double t_lb, double t_ub) {
136 if (is_pos_inf(t_ub) || is_neg_inf(t_lb)) {
137 return Inf;
138 }
139 return std::abs(t_ub - t_lb);
140 }
141
142 static bool is_zero(double t_value, double t_tolerance) {
143 return std::abs(t_value) <= t_tolerance;
144 }
145
146 template<class T>
147 decltype(std::declval<T>().is_zero(.1)) is_zero(const T& t_expr, double t_tolerance) {
148 return t_expr.is_zero(t_tolerance);
149 }
150
151 static bool is_integer(double t_value, double t_tolerance) {
152 return std::abs(t_value - std::round(t_value)) <= t_tolerance;
153 }
154
155 static double round(double t_value, unsigned int t_n_digits = 0) {
156 const double multiplier = std::pow(10, t_n_digits);
157 return std::round(t_value * multiplier) / multiplier;
158 }
159
160}
161
162#endif //OPTIMIZE_NUMERICALS_H
static double Feasibility
Definition numericals.h:83
static double MIPAbsoluteGap
Definition numericals.h:63
static double MIPRelativeGap
Definition numericals.h:51
static double Integer
Definition numericals.h:73
static double Optimality
Definition numericals.h:92
static double Sparsity
Definition numericals.h:37