Camargue
util.hpp
Go to the documentation of this file.
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
8 #ifndef CMR_UTIL_H
9 #define CMR_UTIL_H
10 
11 #include <algorithm>
12 #include <array>
13 #include <iostream>
14 #include <memory>
15 #include <string>
16 #include <utility>
17 #include <vector>
18 
19 #include <cmath>
20 
22 namespace CMR {
23 
25 struct OutPrefs {
26  std::string probname;
27 
33 
35  bool save_tour = true;
36  bool dump_xy = false;
37  bool save_tour_edges = false;
38 
44  bool gif_tour = false;
45 
47 
48  bool verbose = false;
49 
51  bool prog_bar = false;
52 
54  bool detailed_stats = false;
55 };
56 
57 
59 namespace Epsilon {
60 
61 
62 constexpr double Zero = 0.000001;
63 constexpr double MinCut = 0.0001;
64 
66 constexpr double CutViol = 0.001;
67 
68 constexpr double DualDust = 0.001;
69 
71 constexpr double TotalDelta = 0.01;
72 
73 constexpr double PHratio = 0.1;
74 
75 }
76 
78 namespace util {
79 
81 inline bool var_integral(double d)
82 { return fabs(d) < Epsilon::Zero || fabs(d) > 1 - Epsilon::Zero; }
83 
84 double zeit (void);
85 double real_zeit (void);
86 
88 template<typename T, typename ...Args>
89 std::unique_ptr<T> make_unique( Args&& ...args )
90 {
91  return std::unique_ptr<T>( new T( std::forward<Args>(args)... ) );
92 }
93 
98 template<typename T, typename ...Args>
99 void ptr_reset(std::unique_ptr<T> &target, Args&& ...args)
100 {
101  target.reset(new T(std::forward<Args>(args)...));
102 }
103 
108 template<typename ElemType, typename PredType>
109 void erase_remove(std::vector<ElemType> &vec, PredType pred)
110 {
111  vec.erase(std::remove_if(vec.begin(), vec.end(), pred), vec.end());
112 }
113 
120 template <typename T>
123  void operator()(T* ptr) const {
124  if (ptr) free(ptr);
125  ptr = nullptr;
126  }
127 };
128 
129 
130 
136 template<typename numtype>
137 using c_array_ptr = std::unique_ptr<numtype, C_resource_deleter<numtype>>;
138 
140 template<class T>
141 class SquareUT {
142 public:
143  SquareUT() = default;
144 
146  SquareUT(SquareUT &&M) noexcept : data(std::move(M.data)) {}
147 
149  SquareUT &operator=(SquareUT &&M) noexcept
150  { data = std::move(M.data); return *this; }
151 
152 
154  SquareUT(size_t size) : data(size)
155  {
156  for (auto i = 0; i < size; ++i)
157  data[i] = std::vector<T>(size - i);
158  }
159 
161  SquareUT(size_t size, T val) : data(size)
162  {
163  for (auto i = 0; i < size; ++i)
164  data[i] = std::vector<T>(size - i, val);
165  }
166 
168  T &operator()(size_t row, size_t column)
169  {
170  return data[row][column - row];
171  }
172 
174  void fill(const T &val)
175  {
176  for (std::vector<T> &vec : data)
177  std::fill(vec.begin(), vec.end(), val);
178  }
179 
180 private:
182  std::vector<std::vector<T>> data;
183 };
184 
185 }
186 
198 struct Segment {
200  Segment() = default;
201 
203  Segment(int lo, int hi) : start(lo), end(hi) {}
204 
209  int size() const { return end - start + 1; }
210 
214  bool contains(int vx) const { return start <= vx && vx <= end; }
215 
220  bool subset_of(const Segment &seg) const
221  { return seg.contains(start) && seg.contains(end); }
222 
224  bool operator>(const Segment &rhs) const
225  {
226  return std::make_tuple(size(), start, end) >
227  std::make_tuple(rhs.size(), rhs.start, rhs.end);
228  }
229 
231  bool operator==(const Segment &rhs) const
232  { return start == rhs.start && end == rhs.end; }
233 
234  int start;
235  int end;
236 };
237 
239 struct EndPts {
240  EndPts() : end{{-1, -1}} {}
241  EndPts(int e0, int e1) : end{{e0, e1}}
242  { if (end[0] > end[1]) std::swap(end[0], end[1]); }
243 
244  std::array<int, 2> end;
245 };
246 
247 inline bool operator==(EndPts e1, EndPts e2)
248 {
249  return e1.end == e2.end;
250 }
251 
252 inline std::ostream &operator<<(std::ostream &os, const EndPts &e)
253 {
254  os << "(" << e.end[0] << ", " << e.end[1] << ")";
255  return os;
256 }
257 
258 }
259 
260 
261 
262 
263 #endif
Class template for a square upper triangular matrix.
Definition: util.hpp:141
constexpr double Zero
Numbers less than this treated as zero.
Definition: util.hpp:62
double zeit(void)
CPU time function.
Definition: util.cpp:14
Segment(int lo, int hi)
Construct a Segment with specified start and end point.
Definition: util.hpp:203
constexpr double TotalDelta
A round of cuts is a failure if the pivot deltas sum to less than this.
Definition: util.hpp:71
Class template for deleting resources allocated by C functions.
Definition: util.hpp:121
std::vector< std::vector< T > > data
The entries of the matrix, stored as a ragged vector of vectors.
Definition: util.hpp:182
bool detailed_stats
Detailed timer/profiling of the code.
Definition: util.hpp:54
Definition: cliq.hpp:118
bool gif_tour
Record each augmenting tour as specified by save_tour/save_tour_edges.
Definition: util.hpp:44
void ptr_reset(std::unique_ptr< T > &target, Args &&...args)
Adaptation of make_unique to reset a unique_ptr to an object.
Definition: util.hpp:99
int end
The end index of the Segment.
Definition: util.hpp:235
void fill(const T &val)
Fill all entries of this matrix with val.
Definition: util.hpp:174
std::unique_ptr< numtype, C_resource_deleter< numtype >> c_array_ptr
Alias declaration for unique_ptr to C array.
Definition: util.hpp:137
SquareUT(size_t size, T val)
Construct a matrix of dimension size with val as all entries.
Definition: util.hpp:161
void operator()(T *ptr) const
Call operator for deleting the managed pointer.
Definition: util.hpp:123
std::unique_ptr< T > make_unique(Args &&...args)
As per Herb Sutter, port of C++14&#39;s make_unique faculty.
Definition: util.hpp:89
bool prog_bar
A jittering progress bar for pivot values.
Definition: util.hpp:51
T & operator()(size_t row, size_t column)
Access entry by matrix subscripting.
Definition: util.hpp:168
bool save_tour_edges
Save save_tour edges to probname_tour.x
Definition: util.hpp:37
SquareUT(SquareUT &&M) noexcept
Move constructor.
Definition: util.hpp:146
bool dump_xy
If possible, dump x-y coords to probname.xy
Definition: util.hpp:36
std::string probname
The instance name.
Definition: util.hpp:26
Preferences related to tour file output and verbosity.
Definition: util.hpp:25
bool operator>(const Segment &rhs) const
Compare segments by size and then start point.
Definition: util.hpp:224
SquareUT & operator=(SquareUT &&M) noexcept
Move assignment.
Definition: util.hpp:149
bool operator==(const Segment &rhs) const
Equality operator.
Definition: util.hpp:231
bool contains(int vx) const
Does the Segment contain a certain vertex.
Definition: util.hpp:214
void erase_remove(std::vector< ElemType > &vec, PredType pred)
Standardization of erase-remove idiom.
Definition: util.hpp:109
The namespace for this project.
Definition: abc_nodesel.hpp:20
constexpr double CutViol
Cuts are not considered violated unless by at least this much.
Definition: util.hpp:66
bool verbose
Verbose output from classes and subroutines.
Definition: util.hpp:48
bool save_tour
Save the latest, best tour to probname.sol.
Definition: util.hpp:35
SquareUT(size_t size)
Construct a matrix of dimension size.
Definition: util.hpp:154
Simple base class for storing edge of a graph as a sorted pair of nodes.
Definition: util.hpp:239
Simple utility struct for storing an interval of nodes.
Definition: util.hpp:198
constexpr double PHratio
A small value of the Padberg-Hong metric.
Definition: util.hpp:73
constexpr double MinCut
Tolerance for min cut computations.
Definition: util.hpp:63
bool var_integral(double d)
Is a zero-one variable considered integral.
Definition: util.hpp:81
int size() const
Size of the Segment.
Definition: util.hpp:209
double real_zeit(void)
Wall clock time function.
Definition: util.cpp:24
constexpr double DualDust
Small dual values.
Definition: util.hpp:68
bool subset_of(const Segment &seg) const
Is one Segment a subset of the other.
Definition: util.hpp:220
int start
The start index of the Segment.
Definition: util.hpp:234