Dip-All  0.91.0
DecompSolution.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 // Author: Matthew Galati, SAS Institute Inc. (matthew.galati@sas.com) //
8 // //
9 // Conceptual Design: Matthew Galati, SAS Institute Inc. //
10 // Ted Ralphs, Lehigh University //
11 // //
12 // Copyright (C) 2002-2015, Lehigh University, Matthew Galati, Ted Ralphs //
13 // All Rights Reserved. //
14 //===========================================================================//
15 
16 #ifndef DECOMP_SOLUTION_INCLUDED
17 #define DECOMP_SOLUTION_INCLUDED
18 
19 //TODO: make this public AlpsDecompSolution?
21 protected:
23  int m_size;
24 
26  double* m_values;
27 
29  double m_quality;
30 
31 public:
35  inline const int getSize() const {
36  return m_size;
37  }
38 
40  inline const double* getValues() const {
41  return m_values;
42  }
43 
45  inline const double getQuality() const {
46  return m_quality;
47  }
48 
49 public:
51  virtual void print(int precision = 4,
52  std::ostream& os = std::cout) const {
53  int i;
54  os << std::setprecision(precision);
55  os << std::setiosflags(std::ios::fixed | std::ios::showpoint)
56  << std::setw(14);
57  os << "-------------------------" << std::endl;
58  os << "Quality = " << getQuality() << std::endl;
59  os << "Solution = " << std::endl;
60 
61  for (i = 0; i < m_size; i++) {
62  if (!UtilIsZero(m_values[i])) {
63  os << std::setw(15) << i << " " << m_values[i] << std::endl;
64  }
65  }
66 
67  os << "-------------------------" << std::endl;
68  os << std::resetiosflags(std::ios::fixed | std::ios::showpoint |
69  std::ios::scientific);
70  }
71 
73  virtual void print(const std::vector<std::string>& colNames,
74  int precision = 2,
75  std::ostream& os = std::cout) const {
76  int i;
77  os << std::setprecision(precision);
78  os << std::setiosflags(std::ios::fixed | std::ios::showpoint);
79 
80  //os << "-------------------------" << std::endl;
81  //os << "obj= " << getQuality() << std::endl;
82  for (i = 0; i < m_size; i++) {
83  if (!UtilIsZero(m_values[i])) {
84  os << std::setw(25) << colNames[i] << " " << m_values[i] << std::endl;
85  }
86  }
87 
88  //os << "-------------------------" << std::endl;
89  os << std::resetiosflags(std::ios::fixed | std::ios::showpoint |
90  std::ios::scientific);
91  }
92 
93 public:
95  DecompSolution(const DecompSolution& source) :
96  m_size(source.m_size),
97  m_values(0),
98  m_quality(source.m_quality) {
99  m_values = new double[m_size];
100  CoinAssertHint(m_values, "Error: Out of Memory");
101  memcpy(m_values, source.m_values, m_size * sizeof(double));
102  }
104  if (this != &rhs) {
105  m_size = rhs.m_size;
106  m_quality = rhs.m_quality;
107  m_values = new double[m_size];
108  CoinAssertHint(m_values, "Error: Out of Memory");
109  memcpy(m_values, rhs.m_values, m_size * sizeof(double));
110  }
111 
112  return *this;
113  }
114 
115 public:
120  m_size(0),
121  m_values(0),
122  m_quality(1e75) {
123  }
124 
126  DecompSolution(const int size,
127  const double* values,
128  const double quality) :
129  m_size(size),
130  m_values(0),
131  m_quality(quality) {
132  CoinAssert(m_size > 0);
133  m_values = new double[m_size];
134  CoinAssertHint(m_values, "Error: Out of Memory");
135  memcpy(m_values, values, m_size * sizeof(double));
136  }
137 
138  DecompSolution(const int size,
139  const double* values,
140  const double* cost) :
141  m_size(size),
142  m_values(0),
143  m_quality(0.0) {
144  CoinAssert(m_size > 0);
145  m_values = new double[m_size];
146  CoinAssertHint(m_values, "Error: Out of Memory");
147  memcpy(m_values, values, m_size * sizeof(double));
148 
149  //---
150  //--- calculate quality
151  //---
152  for (int i = 0; i < size; i++) {
153  m_quality += cost[i] * values[i];
154  }
155  }
156 
157  virtual ~DecompSolution() {
159  };
160 };
161 
162 #endif
DecompSolution(const int size, const double *values, const double *cost)
Default constructor.
const double * getValues() const
Get solution values.
double * m_values
Solution values.
virtual void print(int precision=4, std::ostream &os=std::cout) const
Print solution.
bool UtilIsZero(const double x, const double etol=1.0e-8)
Definition: UtilMacros.h:272
virtual ~DecompSolution()
Default constructor.
int m_size
Length of solution (number of columns).
const int getSize() const
Get length of solution.
double m_quality
Quality of solution (bound wrt to objective).
#define UTIL_DELARR(x)
Definition: UtilMacros.h:29
DecompSolution(const int size, const double *values, const double quality)
Constructor.
#define CoinAssertHint(expression, hint)
Definition: CoinError.hpp:180
DecompSolution & operator=(const DecompSolution &rhs)
const double getQuality() const
Get quality of solution.
virtual void print(const std::vector< std::string > &colNames, int precision=2, std::ostream &os=std::cout) const
Print solution in MIPLIB2010 solution checker format.
DecompSolution(const DecompSolution &source)
#define CoinAssert(expression)
Definition: CoinError.hpp:179
DecompSolution()
Default constructor.