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
41 };
42
43 static bool is_pos_inf(double t_value) {
44 return t_value >= Inf;
45 }
46
47 static bool is_neg_inf(double t_value) {
48 return t_value <= -Inf;
49 }
50
51 static bool is_inf(double t_value) {
52 return is_pos_inf(t_value) || is_neg_inf(t_value);
53 }
54
55 static bool equals(double t_a, double t_b, double t_tolerance) {
56 return std::abs(t_a - t_b) <= t_tolerance;
57 }
58
59 static bool is(double t_a, CtrType t_type, double t_b, double t_tolerance) {
60
61 switch (t_type) {
62 case LessOrEqual:
63 return t_a <= t_b + t_tolerance;
64 case GreaterOrEqual:
65 return t_a >= t_b - t_tolerance;
66 case Equal:
67 return equals(t_a, t_b, t_tolerance);
68 default:;
69 }
70
71 throw Exception("Enum out of bounds.");
72 }
73
74 static double relative_gap(double t_lb, double t_ub) {
75
76 if (is_neg_inf(t_lb) || is_pos_inf(t_ub)) {
77 return Inf;
78 }
79
80 return std::abs(t_lb - t_ub) / (1e-10 + std::abs(t_ub));
81 }
82
83 static double absolute_gap(double t_lb, double t_ub) {
84 if (is_pos_inf(t_ub) || is_neg_inf(t_lb)) {
85 return Inf;
86 }
87 return std::abs(t_ub - t_lb);
88 }
89
90 static bool is_zero(double t_value, double t_tolerance) {
91 return std::abs(t_value) <= t_tolerance;
92 }
93
94 template<class T>
95 decltype(std::declval<T>().is_zero(.1)) is_zero(const T& t_expr, double t_tolerance) {
96 return t_expr.is_zero(t_tolerance);
97 }
98
99 static bool is_integer(double t_value, double t_tolerance) {
100 return std::abs(t_value - std::round(t_value)) <= t_tolerance;
101 }
102
103 static double round(double t_value, unsigned int t_n_digits = 0) {
104 const double multiplier = std::pow(10, t_n_digits);
105 return std::round(t_value * multiplier) / multiplier;
106 }
107
108}
109
110#endif //OPTIMIZE_NUMERICALS_H
static double Sparsity
Definition numericals.h:37