MAiNGO
babUtils.h
Go to the documentation of this file.
1 /**********************************************************************************
2  * Copyright (c) 2019 Process Systems Engineering (AVT.SVT), RWTH Aachen University
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License 2.0 which is available at
6  * http://www.eclipse.org/legal/epl-2.0.
7  *
8  * SPDX-License-Identifier: EPL-2.0
9  *
10  * @file babUtils.h
11  *
12  * @brief File containing definitions needed in several places of the
13  * Branch-and-Bound elements.
14  *
15  **********************************************************************************/
16 
17 #pragma once
18 
19 #include <cmath>
20 #include <functional>
21 #include <iomanip>
22 #include <iostream>
23 #include <limits>
24 #include <queue>
25 #include <string>
26 #include <type_traits>
27 
28 
29 namespace babBase {
30 
31 
39 template <class T>
40 typename std::enable_if<!std::numeric_limits<T>::is_integer, bool>::type
41 almost_equal(T x, T y, int ulp = 2)
42 {
43  // the machine epsilon has to be scaled to the magnitude of the values used
44  // and multiplied by the desired precision in ULPs (units in the last place)
45  return std::abs(x - y) <= std::numeric_limits<T>::epsilon() * std::abs(x + y) * ulp
46  // unless the result is subnormal
47  || std::abs(x - y) < std::numeric_limits<T>::min();
48 }
49 
50 
59 inline bool
60 larger_or_equal_within_rel_and_abs_tolerance(const double LBD, const double UBD, const double epsilonR, const double epsilonA)
61 {
62  bool absDone = (LBD >= (UBD - epsilonA)); // Done means that absolute criterion is met
63  bool relDone = (LBD >= (UBD - fabs(UBD) * epsilonR)); // Done means that relative criterion is met
64  bool done = (absDone || relDone); // If either criterion is met we are done
65 
66  return done;
67 }
68 
69 
74 struct BabLog {
75  std::queue<double> time;
76  std::queue<double> LBD;
77  std::queue<double> UBD;
78  std::queue<double> iters;
79  std::queue<double> nodeid;
80  std::queue<double> curLB;
81  std::queue<double> nodesLeft;
82  std::queue<double> absGap;
83  std::queue<double> relGap;
84  std::string solutionStatus;
85  std::string logFileName;
86  std::string csvIterationsName;
87  std::string csvGeneralName;
93  void clear()
94  {
95  time = std::queue<double>();
96  LBD = std::queue<double>();
97  UBD = std::queue<double>();
98  iters = std::queue<double>();
99  nodeid = std::queue<double>();
100  curLB = std::queue<double>();
101  nodesLeft = std::queue<double>();
102  absGap = std::queue<double>();
103  relGap = std::queue<double>();
104  solutionStatus = "";
105  reachedMinNodeSize = false;
106  if (logFileName.empty()) {
107  logFileName = "bab.log";
108  }
109  if (csvIterationsName.empty()) {
110  csvIterationsName = "bab_Report_Iterations.csv";
111  }
112  if (csvGeneralName.empty()) {
113  csvGeneralName = "bab_Report_General.csv";
114  }
115  }
116 };
117 
118 
119 namespace enums {
120 
121 
137 };
138 
143 enum NS {
147 };
148 
153 enum BV {
157 };
158 
159 
160 } // end namespace enums
161 
162 
169 template <class T>
170 class OutVar {
171 
172  public:
173  typedef T type;
175  explicit OutVar(T& ref) noexcept:
176  _ptr(std::addressof(ref)) {}
177  OutVar(T&&) = delete;
178  OutVar(const OutVar&) noexcept = default;
179  OutVar& operator=(const OutVar& x) noexcept = default;
181  operator T&() const noexcept { return *_ptr; }
182  T& get() const noexcept { return *_ptr; }
184  private:
185  T* _ptr;
186 };
187 
193 template <typename T>
194 OutVar<T>
195 out_par(T& arr)
196 {
197  return OutVar<T>(arr);
198 }
199 
200 
201 } // end namespace babBase
std::queue< double > iters
Definition: babUtils.h:78
Helper class that can be used to enforce the caller to explicitly state that the variable he passed m...
Definition: babUtils.h:170
std::queue< double > UBD
Definition: babUtils.h:77
NS
Enum for selecting the Node Selection heuristic.
Definition: babUtils.h:143
void clear()
Clears all logging information.
Definition: babUtils.h:93
Definition: babUtils.h:127
BAB_RETCODE
Enum for representing the return codes returned by the B&B solver.
Definition: babUtils.h:126
std::queue< double > curLB
Definition: babUtils.h:80
T * _ptr
Definition: babUtils.h:185
std::string csvIterationsName
Definition: babUtils.h:86
Definition: babUtils.h:133
Definition: babUtils.h:146
BV
Enum for selecting the Branching Variable selection heuristic.
Definition: babUtils.h:153
std::queue< double > absGap
Definition: babUtils.h:82
namespace holding all essentials of the babBase submodule
Definition: babBrancher.h:40
Definition: babUtils.h:154
Definition: babUtils.h:128
Definition: babUtils.h:130
Definition: babUtils.h:129
std::queue< double > relGap
Definition: babUtils.h:83
std::queue< double > nodeid
Definition: babUtils.h:79
Definition: babUtils.h:156
std::string csvGeneralName
Definition: babUtils.h:87
T type
Definition: babUtils.h:173
std::enable_if<!std::numeric_limits< T >::is_integer, bool >::type almost_equal(T x, T y, int ulp=2)
compares if two floating numbers are very close to each other from:https://en.cppreference....
Definition: babUtils.h:41
std::string solutionStatus
Definition: babUtils.h:84
std::string logFileName
Definition: babUtils.h:85
T & get() const noexcept
Definition: babUtils.h:182
Definition: babUtils.h:134
std::queue< double > nodesLeft
Definition: babUtils.h:81
Definition: babUtils.h:145
OutVar(T &ref) noexcept
Definition: babUtils.h:175
Definition: babUtils.h:136
OutVar & operator=(const OutVar &x) noexcept=default
Struct storing logging information during B&B prodcedure.
Definition: babUtils.h:74
Definition: babUtils.h:135
bool reachedMinNodeSize
Definition: babUtils.h:88
bool larger_or_equal_within_rel_and_abs_tolerance(const double LBD, const double UBD, const double epsilonR, const double epsilonA)
Function for checking if LBD is larger than UBD, or smaller by not more than the specified tolerance.
Definition: babUtils.h:60
Definition: babUtils.h:132
OutVar< T > out_par(T &arr)
Function for casting to OutVar<type T>
Definition: babUtils.h:195
Definition: babUtils.h:144
Definition: babUtils.h:155
std::queue< double > time
Definition: babUtils.h:75
std::queue< double > LBD
Definition: babUtils.h:76
Definition: babUtils.h:131