Dip  0.92.4
UtilMacros.h
Go to the documentation of this file.
1 //===========================================================================//
2 // This file is part of the DIP Solver Framework. //
3 // //
4 // DIP is distributed under the Eclipse Public License as part of the //
5 // COIN-OR repository (http://www.coin-or.org). //
6 // //
7 // Authors: Matthew Galati, SAS Institute Inc. (matthew.galati@sas.com) //
8 // Ted Ralphs, Lehigh University (ted@lehigh.edu) //
9 // Jiadong Wang, Lehigh University (jiw408@lehigh.edu) //
10 // //
11 // Copyright (C) 2002-2019, Lehigh University, Matthew Galati, Ted Ralphs //
12 // All Rights Reserved. //
13 //===========================================================================//
14 
15 #ifndef UTIL_MACROS_INCLUDED
16 #define UTIL_MACROS_INCLUDED
17 
18 // =========================================================================
19 #include <cstdio>
20 #include <cassert>
21 #include <vector>
22 #include <list>
23 #include <iostream>
24 #include <fstream>
25 #include <iomanip>
26 #include <numeric>
27 #include <sstream>
28 #include <algorithm>
29 #include <functional>
30 #include <string>
31 #include <map>
32 #include <climits>
33 #include <cmath>
34 #include <cstring>
35 #include <ctime>
36 #include <memory>
37 
38 // =========================================================================
39 const std::string UtilSpaces = " \t\r\n";
40 const double UtilEpsilon = 1.0e-6;
41 const double UtilTooBig = 1.0e20;
42 const double UtilSmallerThanTooBig = 1.0e19;
43 #ifndef INT_MAX
44 #define INT_MAX (static_cast<int>((~(static_cast<unsigned int>(0))) >> 1))
45 #endif
46 
47 #ifndef round
48 #define round(x) floor(x+0.5)
49 #endif
50 
51 // =========================================================================
52 
53 // =========================================================================
54 // Util Error Codes
55 // =========================================================================
56 enum UtilStatus {
59 };
60 
61 // =========================================================================
62 // Memory Macros
63 // =========================================================================
64 #define UTIL_DELPTR(x) if(x) {delete x; x = 0;}
65 #define UTIL_DELARR(x) if(x) {delete [] x; x = 0;}
66 
67 // =========================================================================
68 // Debug Macros
69 // =========================================================================
70 #ifdef NDEBUG
71 //use with LogDebugLevel
72 #define UTIL_DEBUG(param, level, x)
73 //#define UTIL_DEBUG(param, level, x) if(param >= level) {x fflush(stdout);}
74 #else
75 #define UTIL_DEBUG(param, level, x) if(param >= level) {x fflush(stdout);}
76 #endif
77 
78 //use with LogLevel
79 #define UTIL_MSG(param, level, x) if(param >= level) {x fflush(stdout);}
80 
81 
82 // ------------------------------------------------------------------------- //
83 #ifndef NDEBUG
84 #define UtilAssert(expression,errorMsg,os) assert(expresssion)
85 #else
86 inline void UtilAssert(bool expression,
87  std::string errorMsg,
88  std::ostream* os)
89 {
90  //---
91  //--- this is a forced assertion (even when -NDEBUG)
92  //---
93  if (!expression) {
94  (*os) << "ERROR:" << errorMsg << std::endl;
95  abort();
96  }
97 }
98 #endif
99 
100 // ------------------------------------------------------------------------- //
101 inline void UtilPrintParameter(std::ostream* os,
102  const std::string& section,
103  const std::string& name,
104  const int value)
105 {
106  (*os) << std::left << std::setw(15) << section
107  << std::left << std::setw(25) << name
108  << std::setw(10) << value << std::endl;
109 }
110 
111 // ------------------------------------------------------------------------- //
112 inline void UtilPrintParameter(std::ostream* os,
113  const std::string& section,
114  const std::string& name,
115  const double value)
116 {
117  (*os) << std::left << std::setw(15) << section
118  << std::left << std::setw(25) << name
119  << std::setw(10) << value << std::endl;
120 }
121 
122 
123 inline void UtilPrintParameter(std::ostream* os,
124  const std::string& section,
125  const std::string& name,
126  const std::string& value)
127 {
128  (*os) << std::left << std::setw(15) << section
129  << std::left << std::setw(25) << name
130  << std::setw(10) << value << std::endl;
131 }
132 
133 
134 // ------------------------------------------------------------------------- //
135 template <class T> inline void
136 UtilPrintVector(const std::vector<T>& v,
137  std::ostream* os = &std::cout)
138 {
139  typename std::vector<T>::const_iterator it;
140 
141  for (it = v.begin(); it != v.end(); it++) {
142  (*os) << *it << " ";
143  }
144 
145  (*os) << "\n";
146 }
147 
148 // ------------------------------------------------------------------------- //
149 template <class T> inline void
150 UtilPrintVector(const std::vector<T>& v,
151  const std::vector<std::string>& label,
152  std::ostream* os = &std::cout)
153 {
154  typename std::vector<T>::const_iterator it;
155 
156  for (it = v.begin(); it != v.end(); it++) {
157  (*os) << std::setw(5) << *it << " -> "
158  << std::setw(25) << label[*it] << std::endl;
159  }
160 }
161 
162 // ------------------------------------------------------------------------- //
163 template <class T> inline void
164 UtilPrintList(const std::list<T>& v,
165  std::ostream* os = &std::cout)
166 {
167  typename std::list<T>::const_iterator it;
168  (*os) << "\n";
169 
170  for (it = v.begin(); it != v.end(); it++) {
171  (*os) << *it << " ";
172  }
173 }
174 
175 // =========================================================================
176 // Graph Macros
177 // =========================================================================
178 
179 //TODO: should this be in a UtilGraph.h or something?
180 /* -------------------------------------------------------------------------
181  --- Assumption: a complete undirected graph,
182  --- (i,j) = (j,i), i!=j (setup for i>j)
183 
184  --- Loop thru edges: i: 1 -> n-1, j: 0 -> i-1
185  --- Number of edges: m = (n^2 - n)/2
186 
187  --- Get the edge index from (i,j):
188  --- INDEX_U(i,j) = i > j ? (i * (i-1) / 2) + j : (j * (j-1) / 2) + i
189 
190  --- Get (i,j) from the edge index:
191  --- index = (i * (i-1) / 2) + j
192  --- index = (j * (j-1) / 2) + i
193  --- ax^2 + bx + c = 0 -> x = (-b +- sqrt(b^2 - 4ac)) / 2a
194  --- j = index - (j * (j-1))/2
195  --- i = -1/2 + 1/2 sqrt(1 + 8 * index)
196 
197  --- Example: n = 5 (i>j)
198 
199  --- 0 1 2 3 = j
200  --- 0
201  --- 1 0 (1,0)
202  --- 2 1 (2,0) 2 (2,1)
203  --- 3 3 (3,0) 4 (3,1) 5 (3,2)
204  --- 4 6 (4,0) 7 (4,1) 8 (4,2) 9 (4,3)
205 
206  --- For the directed version (see EWCP):
207 
208  --- Loop thru edges:
209  --- i: 1 -> n-1, j: 0 -> i-1 (i>j)
210  --- j: 1 -> n-1, i: 0 -> j-1 (i<j)
211 
212  --- Number of edges: m = (n^2 - n)/2
213 
214  --- 0 1 2 3 4 = j
215  --- 0 10 (0,1) 11 (0,2) 13 (0,3) 16 (0,4)
216  --- 1 12 (1,2) 14 (1,3) 17 (1,4)
217  --- 2 15 (2,3) 18 (2,4)
218  --- 3 19 (3,4)
219  -------------------------------------------------------------------------*/
220 
221 /*-----------------------------------------------------------------------*/
222 inline int UtilNumEdgesU(const int n)
223 {
224  return ((n * n) - n) / 2;
225 }
226 
227 /*-----------------------------------------------------------------------*/
228 inline int UtilIndexU(const int i, const int j)
229 {
230  return i > j ? (i * (i - 1) / 2) + j : (j * (j - 1) / 2) + i;
231 }
232 
233 /*-----------------------------------------------------------------------*/
234 std::pair<int, int> UtilBothEndsU(const int index);
235 
236 /*-----------------------------------------------------------------------*/
237 inline void UtilPrintEdge(const int index,
238  std::ostream* os = &std::cout)
239 {
240  std::pair<int, int> uv = UtilBothEndsU(index);
241  (*os) << "(" << std::setw(2) << uv.first << "," << std::setw(
242  2) << uv.second << ") ";
243 }
244 
245 /*-----------------------------------------------------------------------*/
246 inline std::string UtilEdgeToStr(const int index)
247 {
248  std::stringstream ss;
249  std::pair<int, int> uv = UtilBothEndsU(index);
250  ss << "(" << std::setw(2) << uv.first << "," << std::setw(
251  2) << uv.second << ") ";
252  return ss.str();
253 }
254 
255 // =========================================================================
256 // Fill-In Macros
257 // =========================================================================
258 
259 // =========================================================================
260 template <class T> inline void
261 UtilFillN(T* to, const int size, const T value)
262 {
263  int i;
264 
265  for (i = 0; i < size; i++) {
266  to[i] = value;
267  }
268 }
269 
270 // =========================================================================
271 template <class T> inline void
272 UtilFillN(std::vector<T>& v, const int size, const T value)
273 {
274  std::fill_n(back_inserter(v), size, value);
275 }
276 
277 /*-----------------------------------------------------------------------*/
278 inline void UtilIotaN(int* first,
279  const int size,
280  const int init)
281 {
282  int val = init + size;
283  int ii;
284 
285  for (ii = size; ii-- != 0; ) {
286  first[ii] = --val;
287  }
288 }
289 
290 /*-----------------------------------------------------------------------*/
291 inline void UtilIotaN(std::vector<int>& first,
292  const int size,
293  const int init)
294 {
295  first.reserve(size);
296  int i, val = init + size;
297 
298  for (i = init; i < val; i++) {
299  first.push_back(i);
300  }
301 }
302 
303 // =========================================================================
304 // Random Numbers
305 // =========================================================================
306 
307 /*-----------------------------------------------------------------------*/
308 inline double UtilURand(const double a, const double b)
309 {
310  double rand01 = static_cast<double>(rand()) / static_cast<double>(RAND_MAX);
311  return a + (rand01 * (b - a));
312 }
313 
314 /*-----------------------------------------------------------------------*/
315 inline int UtilURand(const int a, const int b)
316 {
317  double rand01 = static_cast<double>(rand()) / static_cast<double>(RAND_MAX);
318  return a + static_cast<int>(rand01 * (b - a));
319 }
320 
321 /*-----------------------------------------------------------------------*/
322 inline double UtilNormRand(const double mean,
323  const double sigma)
324 {
325  //http://mathworld.wolfram.com/Box-MullerTransformation.html
326  double rand01a = static_cast<double>(rand()) / static_cast<double>(RAND_MAX);
327  double rand01b = static_cast<double>(rand()) / static_cast<double>(RAND_MAX);
328  const double pi = 3.14159265358979323846;
329  double z1 = sqrt(-2.0 * log(rand01a)) * cos(2.0 * pi * rand01b);
330  return z1 * sigma + mean;
331 }
332 
333 // =========================================================================
334 // Statistics
335 // =========================================================================
336 
337 // ------------------------------------------------------------------------- //
338 inline double UtilAve(const std::vector<double>& x)
339 {
340  return std::accumulate(x.begin(), x.end(), 0.0) /
341  static_cast<double>(x.size());
342 }
343 
344 // ------------------------------------------------------------------------- //
345 inline double UtilAve(const std::vector<int>& x)
346 {
347  return std::accumulate(x.begin(), x.end(), 0.0) /
348  static_cast<double>(x.size());
349 }
350 
351 // ------------------------------------------------------------------------- //
352 inline double UtilAve(const double* x,
353  const int len)
354 {
355  return std::accumulate(x, x + len, 0.0) / static_cast<double>(len);
356 }
357 
358 // =========================================================================
359 // String Macros
360 // =========================================================================
361 
362 // ------------------------------------------------------------------------- //
363 inline void UtilStringTokenize(std::string const& input,
364  std::string const& delimiters,
365  std::vector<std::string>& tokens)
366 {
367  std::string::size_type last_pos = 0;
368  std::string::size_type pos = 0;
369 
370  while (true) {
371  pos = input.find_first_of(delimiters, last_pos);
372 
373  if ( pos == std::string::npos ) {
374  tokens.push_back(input.substr(last_pos));
375  break;
376  } else {
377  tokens.push_back(input.substr(last_pos, pos - last_pos));
378  last_pos = pos + 1;
379  }
380  }
381 }
382 
383 // ------------------------------------------------------------------------- //
384 inline std::string UtilStringRandom(int iLength)
385 {
386  std::string strReturn;
387  srand( (unsigned int)time(NULL) );
388 
389  for ( int i = 0 ; i < iLength ; ++i ) {
390  int iNumber;
391  iNumber = rand() % 122;
392 
393  if ( 48 > iNumber ) {
394  iNumber += 48;
395  }
396 
397  if ( ( 57 < iNumber ) && ( 65 > iNumber ) ) {
398  iNumber += 7;
399  }
400 
401  if ( ( 90 < iNumber ) && ( 97 > iNumber ) ) {
402  iNumber += 6;
403  }
404 
405  strReturn += (char)iNumber;
406  }
407 
408  srand(1);
409  return strReturn;
410 }
411 
412 // ------------------------------------------------------------------------- //
413 //trims white space (as defined by UtilSpaces) in-place
414 inline std::string& UtilStrTrim(std::string& s,
415  const std::string& t = UtilSpaces)
416 {
417  if (s.size() == 0) {
418  return s;
419  }
420 
421  std::string::size_type pos = s.find_last_not_of(t);
422 
423  if (pos != std::string::npos) {
424  s.erase(pos + 1);
425  pos = s.find_first_not_of(t);
426 
427  if (pos != std::string::npos) {
428  s.erase(0, pos);
429  }
430  } else {
431  s.erase(s.begin(), s.end());
432  }
433 
434  return s;
435 }
436 
437 // ------------------------------------------------------------------------- //
438 // returns a lower case version of the std::string in-place
439 inline std::string& UtilStrToLower(std::string& s)
440 {
441  // Purify did not like this version:
442  // transform (s.begin(), s.end(), s.begin(), myToLower());
443  if (s.size() == 0) {
444  return s;
445  }
446 
447  // This is a fix for MSVC++ The old version is below
448  std::transform(s.begin(), s.end(), s.begin(), std::ptr_fun<int, int>(tolower));
449  //int i;
450  //for (i = 0; s[i] != '\0'; i++)
451  // s[i] = static_cast<char>(tolower(s[i]));
452  return s;
453 }
454 
455 
456 // ------------------------------------------------------------------------- //
457 // returns an upper case version of the std::string in-place
458 inline std::string& UtilStrToUpper(std::string& s)
459 {
460  // Purify did not like this version:
461  // transform (s.begin(), s.end(), s.begin(), myToUpper());
462  int i;
463 
464  if (s.size() == 0) {
465  return s;
466  }
467 
468  for (i = 0; s[i] != '\0'; i++) {
469  s[i] = static_cast<char>(toupper(s[i]));
470  }
471 
472  return s;
473 }
474 
475 // =========================================================================
476 // Other Macros
477 // =========================================================================
478 
479 // ------------------------------------------------------------------------- //
480 template <class T> inline int UtilGetSize(const std::vector<T>& vec)
481 {
482  return static_cast<int>(vec.size());
483 }
484 
485 // ------------------------------------------------------------------------- //
486 inline bool UtilIsInSet(const int value,
487  const int* set,
488  const int setSize)
489 {
490  int i;
491  bool inSet = false;
492 
493  for (i = 0; i < setSize; i++) {
494  if (set[i] == value) {
495  inSet = true;
496  break;
497  }
498  }
499 
500  return inSet;
501 }
502 
503 // ------------------------------------------------------------------------- //
504 inline int UtilNumNonzeros(const double* x,
505  const int len,
506  const double etol = 1.0e-8)
507 {
508  int i;
509  int nzs = 0;
510 
511  for (i = 0; i < len; i++) {
512  if (fabs(x[i]) > etol) {
513  nzs++;
514  }
515  }
516 
517  return nzs;
518 }
519 
520 // ------------------------------------------------------------------------- //
521 inline double UtilFracPart(const double x)
522 {
523  double floor_x = floor(x);
524  double floor_xplus = floor(x + 0.5);
525 
526  if (fabs(floor_xplus - x) < (UtilEpsilon * (fabs(floor_xplus) + 1.0))) {
527  return 0.0;
528  }
529 
530  return x - floor_x;
531 }
532 
533 // ------------------------------------------------------------------------- //
534 int UtilScaleDblToIntArr(const int arrLen,
535  const double* arrDbl,
536  int* arrInt,
537  const double oneDbl,
538  int* oneInt,
539  const double epstol = UtilEpsilon);
540 
541 // ------------------------------------------------------------------------- //
542 int UtilScaleDblToIntArr(const int arrLen,
543  const double* arrDbl,
544  int* arrInt,
545  const double epstol = UtilEpsilon);
546 
547 // ------------------------------------------------------------------------- //
548 inline bool UtilIsZero(const double x,
549  const double etol = 1.0e-8)
550 {
551  return fabs(x) < etol;
552 }
553 
554 // ------------------------------------------------------------------------- //
555 inline std::string UtilIntToStr(const int i)
556 {
557  std::stringstream ss;
558  ss << i;
559  return ss.str();
560 }
561 
562 // ------------------------------------------------------------------------- //
563 inline std::string UtilDblToStr(const double x,
564  const int precision = -1,
565  const double tooBig = UtilSmallerThanTooBig)
566 {
567  std::stringstream ss;
568 
569  if (fabs(x) > tooBig) {
570  if (x < 0) {
571  ss << "-INF";
572  } else {
573  ss << " INF";
574  }
575  } else {
576  if (precision >= 0) {
577  ss << std::setiosflags(std::ios::fixed | std::ios::showpoint);
578  ss << std::setprecision(precision);
579  }
580 
581  ss << x;
582  }
583 
584  return ss.str();
585 }
586 
587 // ------------------------------------------------------------------------- //
588 inline void UtilPrintMemUsage(std::ostream* os = &std::cout,
589  int logLevel = 0,
590  int logLimit = 2)
591 {
592  // This doesn't build in gcc 4.5 (at least on MinGW)
593 #if 0
594 #if not defined(_MSC_VER)
595 
596  if (logLevel >= logLimit) {
597  struct mallinfo memInfo = mallinfo();
598  double memUsage = static_cast<double>(memInfo.uordblks +
599  memInfo.hblkhd) / 1024.0;
600  memUsage /= 1024.0;
601  (*os) << "memUsage = " << UtilDblToStr(memUsage, 2) << " MB\n";
602  }
603 
604 #endif
605 #endif
606 }
607 
608 // ------------------------------------------------------------------------- //
609 template <class T>
610 void UtilDeleteVectorPtr(std::vector<T*>& vectorPtr,
611  typename std::vector<T*>::iterator first,
612  typename std::vector<T*>::iterator last)
613 {
614  typename std::vector<T*>::iterator it;
615 
616  for (it = first; it != last; it++) {
617  delete *it;
618  }
619 
620  vectorPtr.erase(first, last);
621 }
622 
623 // ------------------------------------------------------------------------- //
624 template <class T> void UtilDeleteVectorPtr(std::vector<T*>& vectorPtr)
625 {
626  UtilDeleteVectorPtr(vectorPtr, vectorPtr.begin(), vectorPtr.end());
627 }
628 
629 // ------------------------------------------------------------------------- //
630 template <class T> void UtilDeleteListPtr(std::list<T*>& listPtr,
631  typename std::list<T*>::iterator first,
632  typename std::list<T*>::iterator last)
633 {
634  typename std::list<T*>::iterator it;
635 
636  for (it = first; it != last; it++) {
637  delete *it;
638  }
639 
640  listPtr.erase(first, last);
641 }
642 
643 // ------------------------------------------------------------------------- //
644 template <class T> void UtilDeleteListPtr(std::list<T*>& listPtr)
645 {
646  UtilDeleteListPtr(listPtr, listPtr.begin(), listPtr.end());
647 }
648 
649 // ------------------------------------------------------------------------- //
650 template <class S, class T>
651 void UtilDeleteMapPtr(std::map<S, T*>& mapPtr,
652  typename std::map<S, T*>::iterator first,
653  typename std::map<S, T*>::iterator last)
654 {
655  typename std::map<S, T*>::iterator it;
656 
657  for (it = first; it != last; it++) {
658  delete (*it).second;
659  }
660 
661  mapPtr.erase(first, last);
662 }
663 
664 // ------------------------------------------------------------------------- //
665 template <class S, class T> void UtilDeleteMapPtr(std::map<S, T*>& mapPtr)
666 {
667  UtilDeleteMapPtr(mapPtr, mapPtr.begin(), mapPtr.end());
668 }
669 
670 // ------------------------------------------------------------------------- //
671 template <class S, class T>
672 void UtilDeleteMapVecPtr(std::map<S, std::vector<T*> >& mapPtr,
673  typename std::map<S, std::vector<T*> >::iterator first,
674  typename std::map<S, std::vector<T*> >::iterator last)
675 {
676  typename std::map<S, std::vector<T*> >::iterator it;
677 
678  for (it = first; it != last; it++) {
679  UtilDeleteVectorPtr((*it).second);
680  }
681 
682  mapPtr.erase(first, last);
683 }
684 
685 // ------------------------------------------------------------------------- //
686 template <class S, class T>
687 void UtilDeleteMapVecPtr(std::map<S, std::vector<T*> >& mapPtr)
688 {
689  UtilDeleteMapVecPtr(mapPtr, mapPtr.begin(), mapPtr.end());
690 }
691 
692 // ------------------------------------------------------------------------- //
693 inline bool UtilIsIntegral(const double x,
694  const double etol = 1.0e-10)
695 {
696  return UtilIsZero(x - floor(x), etol) || UtilIsZero(ceil(x) - x, etol);
697 }
698 
699 // ------------------------------------------------------------------------- //
700 inline bool UtilIsIntegral(const double* x,
701  const int len,
702  const double etol = 1.0e-10)
703 {
704  int i;
705 
706  for (i = 0; i < len; i++) {
707  if (!UtilIsIntegral(x[i], etol)) {
708  return false;
709  }
710  }
711 
712  return true;
713 }
714 
715 // ------------------------------------------------------------------------- //
716 template <class T> inline void UtilNegateArr(const int arrLen,
717  T* arr)
718 {
719  transform(arr, arr + arrLen, arr, std::negate<T>());
720 }
721 
722 // ------------------------------------------------------------------------- //
723 template <class T>
724 struct AddOffset : public std::unary_function<T, T> {
725  T m_n;
726  T operator() (const T& k) {
727  return k + m_n;
728  }
729  AddOffset(T n) : m_n (n) {};
730 };
731 
732 // ------------------------------------------------------------------------- //
733 template <class T> inline void UtilAddOffsetArr(const int arrLen,
734  T offset,
735  T* arr)
736 {
737  transform(arr, arr + arrLen, arr, AddOffset<T>(offset));
738 }
739 
740 // ------------------------------------------------------------------------- //
741 struct Perturb { //: public unary_function
742  double m_randLB;
743  double m_randUB;
744  double operator() (const double& k) {
745  return k + UtilURand(m_randLB, m_randUB);
746  }
747  Perturb(double randLB, double randUB) :
748  m_randLB(randLB), m_randUB(randUB) {};
749 };
750 
751 // ------------------------------------------------------------------------- //
752 inline void UtilPerturbCost(const int seed,
753  const int arrLen,
754  const double randLB,
755  const double randUB,
756  double* arr)
757 {
758  srand(seed);
759  std::transform(arr, arr + arrLen, arr, Perturb(randLB, randUB));
760 }
761 
762 // ------------------------------------------------------------------------- //
763 inline void UtilFlipRowLtoG(const int len,
764  double* els,
765  char& sense,
766  double& rhs)
767 {
768  if (sense == 'L') {
769  return;
770  }
771 
772  if (sense == 'G') {
773  for (int i = 0; i < len; i++) {
774  els[i] = -els[i];
775  }
776 
777  sense = 'L';
778  rhs = -rhs;
779  }
780 
781  assert(0);
782 }
783 
784 // ------------------------------------------------------------------------- //
785 inline void UtilBoundToSense(const double lb,
786  const double ub,
787  const double inf,
788  char& sense,
789  double& rhs,
790  double& range)
791 {
792  range = 0.0;
793 
794  if (lb > -inf) {
795  if (ub < inf) {
796  rhs = ub;
797 
798  if (UtilIsZero(ub - lb)) {
799  sense = 'E';
800  } else {
801  sense = 'R';
802  range = ub - lb;
803  }
804  } else {
805  sense = 'G';
806  rhs = lb;
807  }
808  } else {
809  if (ub < inf) {
810  sense = 'L';
811  rhs = ub;
812  } else {
813  sense = 'N';
814  rhs = 0.0;
815  }
816  }
817 }
818 
819 // ------------------------------------------------------------------------- //
820 inline void UtilSenseToBound(const char sense,
821  const double rhs,
822  const double range,
823  const double inf,
824  double& lb,
825  double& ub)
826 {
827  switch (sense) {
828  case 'E':
829  lb = rhs;
830  ub = rhs;
831  break;
832  case 'L':
833  lb = -inf;
834  ub = rhs;
835  break;
836  case 'G':
837  lb = rhs;
838  ub = inf;
839  break;
840  case 'R':
841  lb = rhs - range;
842  ub = rhs;
843  break;
844  case 'N':
845  lb = -inf;
846  ub = inf;
847  break;
848  }
849 }
850 
851 // --------------------------------------------------------------------- //
852 template<class S, class T> class UtilIsGreaterThan {
853 public:
854  bool operator()( const std::pair<S, T>& x,
855  const std::pair<S, T>& y) {
856  return x.second > y.second;
857  }
858 };
859 
860 // --------------------------------------------------------------------- //
861 template<class S, class T> class UtilIsLessThan {
862 public:
863  bool operator()( const std::pair<S, T>& x,
864  const std::pair<S, T>& y) {
865  return x.second < y.second;
866  }
867 };
868 
869 // ------------------------------------------------------------------------- //
870 inline std::string UtilDirSlash()
871 {
872  std::string slash;
873 #if defined(_MSC_VER)
874  slash = "\\";
875 #else
876  slash = "/";
877 #endif
878  return slash;
879 }
880 
881 // ------------------------------------------------------------------------- //
882 inline int UtilOpenFile(std::ofstream& os,
883  const char* fileName)
884 {
885  int status = UtilStatusOk;
886  os.open(fileName);
887 
888  if (!os) {
889  std::string errMessage = "Error: Filename = ";
890  errMessage += fileName;
891  errMessage += " failed to open.";
892  std::cerr << errMessage.c_str() << std::endl;
893  fflush(stdout);
894  status = UtilStatusFileIO;
895  assert(os);
896  }
897 
898  return status;
899 }
900 
901 // ------------------------------------------------------------------------- //
902 inline int UtilOpenFile(std::ifstream& is,
903  const char* fileName)
904 {
905  int status = UtilStatusOk;
906  is.open(fileName);
907 
908  if (!is) {
909  std::string errMessage = "Error: Filename = ";
910  errMessage += fileName;
911  errMessage += " failed to open.";
912  std::cerr << errMessage.c_str() << std::endl;
913  fflush(stdout);
914  status = UtilStatusFileIO;
915  assert(is);
916  }
917 
918  return status;
919 }
920 
921 // ------------------------------------------------------------------------- //
922 inline int UtilOpenFile(std::ofstream& os,
923  const std::string& fileName)
924 {
925  return UtilOpenFile(os, fileName.c_str());
926 }
927 
928 // ------------------------------------------------------------------------- //
929 inline int UtilOpenFile(std::ifstream& is,
930  const std::string& fileName)
931 {
932  return UtilOpenFile(is, fileName.c_str());
933 }
934 
935 
936 
937 #endif
int UtilIndexU(const int i, const int j)
Definition: UtilMacros.h:134
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
std::string UtilStringRandom(int iLength)
Definition: UtilMacros.h:384
int UtilNumNonzeros(const double *x, const int len, const double etol=1.0e-8)
Definition: UtilMacros.h:504
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
std::string UtilDblToStr(const double x, const int precision=-1, const double tooBig=UtilSmallerThanTooBig)
Definition: UtilMacros.h:563
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
const double UtilTooBig
Definition: UtilMacros.h:41
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 UtilAssert(expression, errorMsg, os)
Definition: UtilMacros.h:84
void UtilStringTokenize(std::string const &input, std::string const &delimiters, std::vector< std::string > &tokens)
Definition: UtilMacros.h:363
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
bool operator()(const std::pair< S, T > &x, const std::pair< S, T > &y)
Definition: UtilMacros.h:863
const double UtilSmallerThanTooBig
Definition: UtilMacros.h:42
void UtilPrintVector(const vector< T > &v, ostream *os=&cout)
Definition: UtilMacros.h:39
#define srand(x)
Definition: mcknap.h:40
std::string UtilEdgeToStr(const int index)
Definition: UtilMacros.h:246
bool UtilIsInSet(const int value, const int *set, const int setSize)
Definition: UtilMacros.h:486
double UtilFracPart(const double x)
Definition: UtilMacros.h:243
void UtilAddOffsetArr(const int arrLen, T offset, T *arr)
Definition: UtilMacros.h:352
void UtilPrintMemUsage(std::ostream *os=&std::cout, int logLevel=0, int logLimit=2)
Definition: UtilMacros.h:588
void UtilFillN(T *to, const int size, const T value)
Definition: UtilMacros.h:158
T operator()(const T &k)
Definition: UtilMacros.h:345
string UtilIntToStr(const int i)
Definition: UtilMacros.h:279
UtilStatus
Definition: UtilMacros.h:56
Perturb(double randLB, double randUB)
Definition: UtilMacros.h:747
int UtilGetSize(const std::vector< T > &vec)
Definition: UtilMacros.h:480
double m_randUB
Definition: UtilMacros.h:362
double UtilNormRand(const double mean, const double sigma)
Definition: UtilMacros.h:322
bool UtilIsIntegral(const double x, const double etol=1.0e-10)
Definition: UtilMacros.h:328
bool operator()(const std::pair< S, T > &x, const std::pair< S, T > &y)
Definition: UtilMacros.h:854
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:729
void UtilPrintParameter(std::ostream *os, const std::string &section, const std::string &name, const int value)
Definition: UtilMacros.h:101
string & UtilStrTrim(string &s, const string &t=UtilSpaces)
Definition: UtilMacros.h:545
void UtilDeleteMapPtr(std::map< S, T * > &mapPtr, typename std::map< S, T * >::iterator first, typename std::map< S, T * >::iterator last)
Definition: UtilMacros.h:651
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)
void UtilDeleteMapVecPtr(std::map< S, std::vector< T * > > &mapPtr, typename std::map< S, std::vector< T * > >::iterator first, typename std::map< S, std::vector< T * > >::iterator last)
Definition: UtilMacros.h:672
string & UtilStrToLower(string &s)
Definition: UtilMacros.h:570
const string UtilSpaces
Definition: UtilMacros.h:21
double operator()(const double &k)
Definition: UtilMacros.h:363