Dip  0.92.4
UtilMacros.h
Go to the documentation of this file.
1 //===========================================================================//
2 // This file is part of the Decomp Solver Framework. //
3 // //
4 // Decomp is distributed under the Common Public License as part of the //
5 // COIN-OR repository (http://www.coin-or.org). //
6 // //
7 // Author: Matthew Galati, Lehigh University //
8 // //
9 // Copyright (C) 2002-2007, Lehigh University, Matthew Galati, and Ted Ralphs//
10 // All Rights Reserved. //
11 //===========================================================================//
12 
13 #ifndef UTIL_MACROS_INCLUDED
14 #define UTIL_MACROS_INCLUDED
15 
16 // =========================================================================
17 #include "DecompConfig.h"
18 #include "DecompPortable.h"
19 class DecompApp;
20 // =========================================================================
21 const string UtilSpaces = " \t\r\n";
22 const double UtilEpsilon = 1.0e-6;
23 // =========================================================================
24 
25 // =========================================================================
26 // Memory Macros
27 // =========================================================================
28 #define UTIL_DELPTR(x) if(x) {delete x; x = 0;}
29 #define UTIL_DELARR(x) if(x) {delete [] x; x = 0;}
30 
31 // =========================================================================
32 // Debug Macros
33 // =========================================================================
34 #define UTIL_DEBUG(param, level, x) if(param > level) {x fflush(stdout);}
35 #define UTIL_DEBUG0(x) {x fflush(stdout);}
36 
37 // ------------------------------------------------------------------------- //
38 template <class T> inline void
39 UtilPrintVector(const vector<T> & v,
40  ostream* os = &cout)
41 {
42  typename vector<T>::const_iterator it;
43  (*os) << "\n";
44 
45  for (it = v.begin(); it != v.end(); it++) {
46  (*os) << *it << " ";
47  }
48 }
49 
50 // ------------------------------------------------------------------------- //
51 template <class T> inline void
52 UtilPrintList(const list<T> & v,
53  ostream* os = &cout)
54 {
55  typename list<T>::const_iterator it;
56  (*os) << "\n";
57 
58  for (it = v.begin(); it != v.end(); it++) {
59  (*os) << *it << " ";
60  }
61 }
62 
63 
64 // =========================================================================
65 // COIN Macros
66 // TODO: anything that depends on COIN should probably not be in util lib
67 // =========================================================================
69  const double* dense,
70  const double etol);
71 void UtilPackedVectorFromDense(const int len,
72  const double* dense,
73  const double etol,
74  CoinPackedVector& v);
75 
76 //TODO: now depends on DecompApp!? ugh... then belongs in DecompUtil,
77 // not Util...
79  ostream* os = &cout,
80  DecompApp* app = 0);
81 
82 // =========================================================================
83 // Graph Macros
84 // =========================================================================
85 
86 /* -------------------------------------------------------------------------
87  --- Assumption: a complete undirected graph,
88  --- (i,j) = (j,i), i!=j (setup for i>j)
89 
90  --- Loop thru edges: i: 1 -> n-1, j: 0 -> i-1
91  --- Number of edges: m = (n^2 - n)/2
92 
93  --- Get the edge index from (i,j):
94  --- INDEX_U(i,j) = i > j ? (i * (i-1) / 2) + j : (j * (j-1) / 2) + i
95 
96  --- Get (i,j) from the edge index:
97  --- index = (i * (i-1) / 2) + j
98  --- index = (j * (j-1) / 2) + i
99  --- ax^2 + bx + c = 0 -> x = (-b +- sqrt(b^2 - 4ac)) / 2a
100  --- j = index - (j * (j-1))/2
101  --- i = -1/2 + 1/2 sqrt(1 + 8 * index)
102 
103  --- Example: n = 5 (i>j)
104 
105  --- 0 1 2 3 = j
106  --- 0
107  --- 1 0 (1,0)
108  --- 2 1 (2,0) 2 (2,1)
109  --- 3 3 (3,0) 4 (3,1) 5 (3,2)
110  --- 4 6 (4,0) 7 (4,1) 8 (4,2) 9 (4,3)
111 
112  --- For the directed version (see EWCP):
113 
114  --- Loop thru edges:
115  --- i: 1 -> n-1, j: 0 -> i-1 (i>j)
116  --- j: 1 -> n-1, i: 0 -> j-1 (i<j)
117 
118  --- Number of edges: m = (n^2 - n)/2
119 
120  --- 0 1 2 3 4 = j
121  --- 0 10 (0,1) 11 (0,2) 13 (0,3) 16 (0,4)
122  --- 1 12 (1,2) 14 (1,3) 17 (1,4)
123  --- 2 15 (2,3) 18 (2,4)
124  --- 3 19 (3,4)
125  -------------------------------------------------------------------------*/
126 
127 /*-----------------------------------------------------------------------*/
128 inline int UtilNumEdgesU(const int n)
129 {
130  return ((n * n) - n) / 2;
131 }
132 
133 /*-----------------------------------------------------------------------*/
134 inline int UtilIndexU(const int i, const int j)
135 {
136  return i > j ? (i * (i - 1) / 2) + j : (j * (j - 1) / 2) + i;
137 }
138 
139 /*-----------------------------------------------------------------------*/
140 pair<int, int> UtilBothEndsU(const int index);
141 
142 /*-----------------------------------------------------------------------*/
143 inline void UtilPrintEdge(const int index,
144  ostream* os = &cout)
145 {
146  pair<int, int> uv = UtilBothEndsU(index);
147  (*os) << "(" << setw(2) << uv.first << "," << setw(2) << uv.second << ") ";
148 }
149 
150 // =========================================================================
151 // Fill-In Macros
152 // =========================================================================
153 
154 #include "CoinHelperFunctions.hpp"
155 
156 /*-----------------------------------------------------------------------*/
157 template <class T> inline void
158 UtilFillN(T* to, const int size, const T value)
159 {
160  CoinFillN(to, size, value); //dep on COIN
161 }
162 
163 /*-----------------------------------------------------------------------*/
164 template <class T> inline void
165 UtilFillN(vector<T> & v, const int size, const T value)
166 {
167  std::fill_n(back_inserter(v), size, value);
168 }
169 
170 /*-----------------------------------------------------------------------*/
171 inline void UtilIotaN(int* first,
172  const int size,
173  const int init)
174 {
175  int val = init + size;
176  int ii;
177 
178  for (ii = size; ii-- != 0; ) {
179  first[ii] = --val;
180  }
181 }
182 
183 /*-----------------------------------------------------------------------*/
184 //TODO: something faster?
185 inline void UtilIotaN(vector<int> & first,
186  const int size,
187  const int init)
188 {
189  first.reserve(size);
190  int i, val = init + size;
191 
192  for (i = init; i < val; i++) {
193  first.push_back(i);
194  }
195 }
196 
197 // =========================================================================
198 // Random Numbers
199 // =========================================================================
200 
201 /*-----------------------------------------------------------------------*/
202 inline double UtilURand(const double a, const double b)
203 {
204  double rand01 = static_cast<double>(rand()) / static_cast<double>(RAND_MAX);
205  return a + (rand01 * (b - a));
206 }
207 
208 /*-----------------------------------------------------------------------*/
209 inline int UtilURand(const int a, const int b)
210 {
211  double rand01 = static_cast<double>(rand()) / static_cast<double>(RAND_MAX);
212  return a + static_cast<int>(rand01 * (b - a));
213 }
214 
215 // =========================================================================
216 // Statistics
217 // =========================================================================
218 //?? make templates?
219 /*-----------------------------------------------------------------------*/
220 inline double UtilAve(const vector<double> & x)
221 {
222  return std::accumulate(x.begin(), x.end(), 0.0) / x.size();
223 }
224 
225 /*-----------------------------------------------------------------------*/
226 inline double UtilAve(const vector<int> & x)
227 {
228  return std::accumulate(x.begin(), x.end(), 0.0) / x.size();
229 }
230 
231 /*-----------------------------------------------------------------------*/
232 inline double UtilAve(const double* x,
233  const int len)
234 {
235  return std::accumulate(x, x + len, 0.0) / len;
236 }
237 
238 // =========================================================================
239 // Other Macros
240 // =========================================================================
241 
242 // ------------------------------------------------------------------------- //
243 inline double UtilFracPart(const double x)
244 {
245  double floor_x = floor(x);
246  double floor_xplus = floor(x + 0.5);
247 
248  if (fabs(floor_xplus - x) < (UtilEpsilon * (fabs(floor_xplus) + 1.0))) {
249  return 0.0;
250  }
251 
252  return x - floor_x;
253 }
254 
255 // ------------------------------------------------------------------------- //
256 int UtilScaleDblToIntArr(const int arrLen,
257  const double* arrDbl,
258  int* arrInt,
259  const double oneDbl,
260  int* oneInt,
261  const double epstol = UtilEpsilon);
262 
263 // ------------------------------------------------------------------------- //
264 int UtilScaleDblToIntArr(const int arrLen,
265  const double* arrDbl,
266  int* arrInt,
267  const double epstol = UtilEpsilon);
268 
269 
270 
271 // ------------------------------------------------------------------------- //
272 inline bool UtilIsZero(const double x,
273  const double etol = 1.0e-8)
274 {
275  return fabs(x) < etol;
276 }
277 
278 // ------------------------------------------------------------------------- //
279 inline string UtilIntToStr(const int i)
280 {
281  stringstream ss;
282  ss << i;
283  return ss.str();
284 }
285 
286 // ------------------------------------------------------------------------- //
287 template <class T>
288 void UtilDeleteVectorPtr(vector<T*> & vectorPtr,
289  typename vector<T*>::iterator first,
290  typename vector<T*>::iterator last)
291 {
292  typename vector<T*>::iterator it;
293 
294  for (it = first; it != last; it++) {
295  delete *it;
296  }
297 
298  vectorPtr.erase(first, last);
299 }
300 
301 // ------------------------------------------------------------------------- //
302 template <class T> void UtilDeleteVectorPtr(vector<T*> & vectorPtr)
303 {
304  UtilDeleteVectorPtr(vectorPtr, vectorPtr.begin(), vectorPtr.end());
305 }
306 
307 // ------------------------------------------------------------------------- //
308 template <class T> void UtilDeleteListPtr(list<T*> & listPtr,
309  typename list<T*>::iterator first,
310  typename list<T*>::iterator last)
311 {
312  typename list<T*>::iterator it;
313 
314  for (it = first; it != last; it++) {
315  delete *it;
316  }
317 
318  listPtr.erase(first, last);
319 }
320 
321 // ------------------------------------------------------------------------- //
322 template <class T> void UtilDeleteListPtr(list<T*> & listPtr)
323 {
324  UtilDeleteListPtr(listPtr, listPtr.begin(), listPtr.end());
325 }
326 
327 // ------------------------------------------------------------------------- //
328 inline bool UtilIsIntegral(const double x,
329  const double etol = 1.0e-10)
330 {
331  return UtilIsZero(x - floor(x), etol) || UtilIsZero(ceil(x) - x, etol);
332 }
333 
334 // ------------------------------------------------------------------------- //
335 template <class T> inline void UtilNegateArr(const int arrLen,
336  T* arr)
337 {
338  transform(arr, arr + arrLen, arr, negate<T>());
339 }
340 
341 // ------------------------------------------------------------------------- //
342 template <class T>
343 struct AddOffset : public unary_function<T, T> {
344  T m_n;
345  T operator() (const T& k) {
346  return k + m_n;
347  }
348  AddOffset(T n) : m_n (n) {};
349 };
350 
351 // ------------------------------------------------------------------------- //
352 template <class T> inline void UtilAddOffsetArr(const int arrLen,
353  T offset,
354  T* arr)
355 {
356  transform(arr, arr + arrLen, arr, AddOffset<T>(offset));
357 }
358 
359 // ------------------------------------------------------------------------- //
360 struct Perturb { //: public unary_function
361  double m_randLB;
362  double m_randUB;
363  double operator() (const double& k) {
364  return k + UtilURand(m_randLB, m_randUB);
365  }
366  Perturb(double randLB, double randUB) :
367  m_randLB(randLB), m_randUB(randUB) {};
368 };
369 
370 // ------------------------------------------------------------------------- //
371 inline void UtilPerturbCost(const int seed,
372  const int arrLen,
373  const double randLB,
374  const double randUB,
375  double* arr)
376 {
377  srand(seed);
378  transform(arr, arr + arrLen, arr, Perturb(randLB, randUB));
379 }
380 
381 // ------------------------------------------------------------------------- //
382 inline void UtilFlipRowLtoG(const int len,
383  double* els,
384  char& sense,
385  double& rhs)
386 {
387  if (sense == 'L') {
388  return;
389  }
390 
391  if (sense == 'G') {
392  //C++ row to negate? TODO
393  for (int i = 0; i < len; i++) {
394  els[i] = -els[i];
395  }
396 
397  sense = 'L';
398  rhs = -rhs;
399  }
400 
401  assert(0);
402 }
403 
404 // ------------------------------------------------------------------------- //
405 inline void UtilBoundToSense(const double lb,
406  const double ub,
407  const double inf,
408  char& sense,
409  double& rhs,
410  double& range)
411 {
412  range = 0.0;
413 
414  if (lb > -inf) {
415  if (ub < inf) {
416  rhs = ub;
417 
418  if (UtilIsZero(ub - lb)) {
419  sense = 'E';
420  } else {
421  sense = 'R';
422  range = ub - lb;
423  }
424  } else {
425  sense = 'G';
426  rhs = lb;
427  }
428  } else {
429  if (ub < inf) {
430  sense = 'L';
431  rhs = ub;
432  } else {
433  sense = 'N';
434  rhs = 0.0;
435  }
436  }
437 }
438 
439 // ------------------------------------------------------------------------- //
440 inline void UtilSenseToBound(const char sense,
441  const double rhs,
442  const double range,
443  const double inf,
444  double& lb,
445  double& ub)
446 {
447  switch (sense) {
448  case 'E':
449  lb = rhs;
450  ub = rhs;
451  break;
452  case 'L':
453  lb = -inf;
454  ub = rhs;
455  break;
456  case 'G':
457  lb = rhs;
458  ub = inf;
459  break;
460  case 'R':
461  lb = rhs - range;
462  ub = rhs;
463  break;
464  case 'N':
465  lb = -inf;
466  ub = inf;
467  break;
468  }
469 }
470 
471 // --------------------------------------------------------------------- //
472 template<class S, class T> class UtilIsGreaterThan {
473 public:
474  bool operator()( const pair<S, T> & x,
475  const pair<S, T> & y) {
476  return x.second > y.second;
477  }
478 };
479 
480 // --------------------------------------------------------------------- //
481 template<class S, class T> class UtilIsLessThan {
482 public:
483  bool operator()( const pair<S, T> & x,
484  const pair<S, T> & y) {
485  return x.second < y.second;
486  }
487 };
488 
489 #if 0
490 // ------------------------------------------------------------------------- //
491 class UtilIsLessThan {
492 public:
493  bool operator()( const pair<int, double> & x,
494  const pair<int, double> & y) {
495  return x.second < y.second;
496  }
497  bool operator()( const pair< pair<int, int>, double> & x,
498  const pair< pair<int, int>, double> & y) {
499  return x.second < y.second;
500  }
501 };
502 
503 // ------------------------------------------------------------------------- //
504 class UtilIsGreaterThan {
505 public:
506  bool operator()( const pair<int, double> & x,
507  const pair<int, double> & y) {
508  return x.second > y.second;
509  }
510 };
511 #endif
512 
513 // ------------------------------------------------------------------------- //
514 inline string UtilDirSlash()
515 {
516  string slash;
517 #if defined(_MSC_VER)
518  slash = "\\";
519 #else
520  slash = "/";
521 #endif
522  return slash;
523 }
524 
525 // ------------------------------------------------------------------------- //
526 inline void UtilOpenFile(ifstream& fs,
527  const char* fileName) throw(CoinError)
528 {
529  fs.open(fileName);
530 
531  if (!fs) {
532  string errMessage = "Error: Filename = ";
533  errMessage += fileName;
534  errMessage += " failed to open.";
535  CoinAssertHint(fs, errMessage.c_str());
536  }
537 }
538 
539 // =========================================================================
540 // String Macros
541 // =========================================================================
542 
543 // ------------------------------------------------------------------------- //
544 //trims white space (as defined by UtilSpaces) in-place
545 inline string& UtilStrTrim(string& s,
546  const string& t = UtilSpaces)
547 {
548  if (s.size() == 0) {
549  return s;
550  }
551 
552  string::size_type pos = s.find_last_not_of(t);
553 
554  if (pos != string::npos) {
555  s.erase(pos + 1);
556  pos = s.find_first_not_of(' ');
557 
558  if (pos != string::npos) {
559  s.erase(0, pos);
560  }
561  } else {
562  s.erase(s.begin(), s.end());
563  }
564 
565  return s;
566 }
567 
568 // ------------------------------------------------------------------------- //
569 // returns a lower case version of the string in-place
570 inline string& UtilStrToLower(string& s)
571 {
572  // Purify did not like this version:
573  // transform (s.begin(), s.end(), s.begin(), myToLower());
574  if (s.size() == 0) {
575  return s;
576  }
577 
578  int i;
579 
580  for (i = 0; s[i] != '\0'; i++) {
581  s[i] = static_cast<char>(tolower(s[i]));
582  }
583 
584  return s;
585 }
586 
587 
588 // ------------------------------------------------------------------------- //
589 // returns an upper case version of the string in-place
590 inline string& UtilStrToUpper(string& s)
591 {
592  // Purify did not like this version:
593  // transform (s.begin(), s.end(), s.begin(), myToUpper());
594  int i;
595 
596  if (s.size() == 0) {
597  return s;
598  }
599 
600  for (i = 0; s[i] != '\0'; i++) {
601  s[i] = static_cast<char>(toupper(s[i]));
602  }
603 
604  return s;
605 }
606 
607 
608 #endif
int UtilIndexU(const int i, const int j)
Definition: UtilMacros.h:134
CoinPackedVector * UtilPackedVectorFromDense(const int len, const double *dense, const double etol)
void UtilSenseToBound(const char sense, const double rhs, const double range, const double inf, double &lb, double &ub)
Definition: UtilMacros.h:440
void UtilDeleteVectorPtr(vector< T * > &vectorPtr, typename vector< T * >::iterator first, typename vector< T * >::iterator last)
Definition: UtilMacros.h:288
void UtilNegateArr(const int arrLen, T *arr)
Definition: UtilMacros.h:335
bool UtilIsZero(const double x, const double etol=1.0e-8)
Definition: UtilMacros.h:272
void UtilPrintList(const list< T > &v, ostream *os=&cout)
Definition: UtilMacros.h:52
double m_randLB
Definition: UtilMacros.h:361
int UtilNumEdgesU(const int n)
Definition: UtilMacros.h:128
void UtilBoundToSense(const double lb, const double ub, const double inf, char &sense, double &rhs, double &range)
Definition: UtilMacros.h:405
pair< int, int > UtilBothEndsU(const int index)
void UtilPerturbCost(const int seed, const int arrLen, const double randLB, const double randUB, double *arr)
Definition: UtilMacros.h:371
double UtilAve(const vector< double > &x)
Definition: UtilMacros.h:220
#define CoinAssertHint(expression, hint)
Definition: CoinError.hpp:184
string & UtilStrToUpper(string &s)
Definition: UtilMacros.h:590
void UtilPrintEdge(const int index, ostream *os=&cout)
Definition: UtilMacros.h:143
string UtilDirSlash()
Definition: UtilMacros.h:514
void UtilIotaN(int *first, const int size, const int init)
Definition: UtilMacros.h:171
void UtilDeleteListPtr(list< T * > &listPtr, typename list< T * >::iterator first, typename list< T * >::iterator last)
Definition: UtilMacros.h:308
void UtilOpenFile(ifstream &fs, const char *fileName)
Definition: UtilMacros.h:526
void UtilPrintVector(const vector< T > &v, ostream *os=&cout)
Definition: UtilMacros.h:39
#define srand(x)
Definition: mcknap.h:40
double UtilFracPart(const double x)
Definition: UtilMacros.h:243
void UtilPrintPackedVector(const CoinPackedVector &v, ostream *os=&cout, DecompApp *app=0)
void UtilAddOffsetArr(const int arrLen, T offset, T *arr)
Definition: UtilMacros.h:352
void UtilFillN(T *to, const int size, const T value)
Definition: UtilMacros.h:158
void CoinFillN(T *to, const CoinBigIndex size, const T value)
This helper function fills an array with a given value.
Error Class thrown by an exception.
Definition: CoinError.hpp:42
T operator()(const T &k)
Definition: UtilMacros.h:345
Sparse Vector.
string UtilIntToStr(const int i)
Definition: UtilMacros.h:279
Perturb(double randLB, double randUB)
Definition: UtilMacros.h:366
bool operator()(const pair< S, T > &x, const pair< S, T > &y)
Definition: UtilMacros.h:483
double m_randUB
Definition: UtilMacros.h:362
bool UtilIsIntegral(const double x, const double etol=1.0e-10)
Definition: UtilMacros.h:328
bool operator()(const pair< S, T > &x, const pair< S, T > &y)
Definition: UtilMacros.h:474
const double UtilEpsilon
Definition: UtilMacros.h:22
double UtilURand(const double a, const double b)
Definition: UtilMacros.h:202
AddOffset(T n)
Definition: UtilMacros.h:348
string & UtilStrTrim(string &s, const string &t=UtilSpaces)
Definition: UtilMacros.h:545
void UtilFlipRowLtoG(const int len, double *els, char &sense, double &rhs)
Definition: UtilMacros.h:382
int UtilScaleDblToIntArr(const int arrLen, const double *arrDbl, int *arrInt, const double oneDbl, int *oneInt, const double epstol=UtilEpsilon)
string & UtilStrToLower(string &s)
Definition: UtilMacros.h:570
The main application class.
Definition: DecompApp.h:48
const string UtilSpaces
Definition: UtilMacros.h:21
double operator()(const double &k)
Definition: UtilMacros.h:363