Dip-All  0.91.0
DecompVar.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 //this is a lambda_s variable
17 //assumption throughout is lambda is {0,1}
18 
19 #ifndef DECOMP_VAR_INCLUDED
20 #define DECOMP_VAR_INCLUDED
21 
22 #include "Decomp.h"
23 #include "UtilHash.h"
24 #include "UtilMacrosDecomp.h"
25 
26 class DecompApp;
27 class DecompAlgoModel;
28 
29 // --------------------------------------------------------------------- //
30 class DecompVar {
31 public:
32  //THINK: or user overriden way to store it (like a tree)
33  //and a function which says expandVarToCol - just as in cuts
34  CoinPackedVector m_s;//this is the var in terms of x-space
35 
36 private:
37  //TODO: lb, ub, "type"?
39  double m_origCost;
40  double m_redCost; //(c - uA'')s - alpha
41  int m_effCnt; //effectiveness counter
42  std::string m_strHash;
43  int m_blockId;
45  double m_norm;
46 
47 public:
48  inline DecompVarType getVarType() const {
49  return m_varType;
50  }
51  inline double getOriginalCost() const {
52  return m_origCost;
53  }
54  inline double getReducedCost() const {
55  return m_redCost;
56  }
57  inline int getEffectiveness() const {
58  return m_effCnt;
59  }
60  inline double getLowerBound() const {
61  return 0.0; //TODO
62  }
63  inline double getUpperBound() const {
64  return DecompInf; //TODO
65  }
66  inline std::string getStrHash() const {
67  return m_strHash;
68  }
69  inline int getBlockId() const {
70  return m_blockId;
71  }
72  inline int getColMasterIndex() const {
73  return m_colMasterIndex;
74  }
75  inline double getNorm() const {
76  return m_norm;
77  }
78 
79  inline void setVarType(const DecompVarType varType) {
80  m_varType = varType;
81  }
82  inline void setColMasterIndex(const int colIndex) {
83  m_colMasterIndex = colIndex;
84  }
85  inline void setBlockId (const int blockId) {
86  m_blockId = blockId;
87  }
88  inline void setReducedCost (const double redCost) {
89  m_redCost = redCost;
90  }
91  inline void setOriginalCost(const double origCost) {
92  m_origCost = origCost;
93  }
94 
95  inline void resetEffectiveness() {
96  m_effCnt = 0;
97  }
98 
101  inline void increaseEffCnt() {
102  m_effCnt = m_effCnt <= 0 ? 1 : m_effCnt + 1;
103  }
104 
107  inline void decreaseEffCnt() {
108  m_effCnt = m_effCnt >= 0 ? -1 : m_effCnt - 1;
109  }
110 
111  inline double calcNorm() {
112  return m_norm = m_s.twoNorm();
113  }
114 
115  inline void sortVar() {
116  m_s.sortIncrIndex();
117  }
118 
119  bool isEquivalent(const DecompVar& dvar) {
120  return m_s.isEquivalent(dvar.m_s);
121  }
122 
123  bool isDuplicate(const DecompVarList& vars) {
124  DecompVarList::const_iterator vi;
125 
126  for (vi = vars.begin(); vi != vars.end(); vi++) {
127  if ((*vi)->getStrHash() == this->getStrHash()) {
128  return true;
129  }
130  }
131 
132  return false;
133  }
134 
135  bool doesSatisfyBounds(int denseLen,
136  double* denseArr,
137  const DecompAlgoModel& model,
138  const double* lbs,
139  const double* ubs);
140 
141  void fillDenseArr(int len,
142  double* arr);
143 
144 public:
145  virtual void print(std::ostream* os = &std::cout,
146  DecompApp* app = 0) const;
147  virtual void print(std::ostream* os,
148  const std::vector<std::string>& colNames,
149  const double* value = NULL) const;
150 
151 public:
153  DecompVar(const DecompVar& source) :
154  m_s (source.m_s),
155  m_varType (source.m_varType),
156  m_origCost(source.m_origCost),
157  m_effCnt (source.m_effCnt),
158  m_strHash (source.m_strHash),
159  m_blockId (source.m_blockId),
161  m_norm (source.m_norm) {
162  }
163 
165  if (this != &rhs) {
166  m_s = rhs.m_s;
167  m_varType = rhs.m_varType;
168  m_origCost = rhs.m_origCost;
169  m_redCost = rhs.m_redCost;
170  m_effCnt = rhs.m_effCnt;
171  m_strHash = rhs.m_strHash;
172  m_blockId = rhs.m_blockId;
174  }
175 
176  return *this;
177  }
178 
180  m_s (),
182  m_origCost(0.0),
183  m_redCost (0.0),
184  m_effCnt (0),
185  m_strHash (),
186  m_blockId (0),
187  m_colMasterIndex(-1),
188  m_norm (0.0) {
189  }
190 
191  DecompVar(const std::vector<int>& ind,
192  const double els,
193  const double redCost,
194  const double origCost,
195  const DecompVarType varType) :
196  m_s (),
197  m_varType (varType),
198  m_origCost(origCost),
199  m_redCost (redCost),
200  m_effCnt (0),
201  m_strHash (),
202  m_blockId (0),
203  m_colMasterIndex(-1),
204  m_norm (0.0) {
205  //m_varType = varType;
206  if (ind.size() > 0) {
207  m_s.setConstant(static_cast<int>(ind.size()),
208  &ind[0], els, DECOMP_TEST_DUPINDEX);
209  m_strHash = UtilCreateStringHash(static_cast<int>(ind.size()),
210  &ind[0], els);
211  m_norm = calcNorm();
212  sortVar();
213  }
214  }
215 
216  DecompVar(const std::vector<int>& ind,
217  const std::vector<double>& els,
218  const double redCost,
219  const double origCost) :
220  m_s (),
222  m_origCost(origCost),
223  m_redCost (redCost),
224  m_effCnt (0),
225  m_strHash (),
226  m_blockId (0),
227  m_colMasterIndex(-1),
228  m_norm (0.0) {
229  //m_varType = DecompVar_Point;
230  if (ind.size() > 0) {
231  m_s.setVector(static_cast<int>(ind.size()),
232  &ind[0], &els[0], DECOMP_TEST_DUPINDEX);
233  m_strHash = UtilCreateStringHash(static_cast<int>(ind.size()),
234  &ind[0], &els[0]);
235  m_norm = calcNorm();
236  sortVar();
237  }
238  }
239 
240  DecompVar(const std::vector<int>& ind,
241  const std::vector<double>& els,
242  const double redCost,
243  const double origCost,
244  const DecompVarType varType) :
245  m_s (),
246  m_varType (varType),
247  m_origCost(origCost),
248  m_redCost (redCost),
249  m_effCnt (0),
250  m_strHash (),
251  m_blockId (0),
252  m_colMasterIndex(-1),
253  m_norm (0.0) {
254  //m_varType = varType;
255  if (ind.size() > 0) {
256  m_s.setVector(static_cast<int>(ind.size()),
257  &ind[0], &els[0], DECOMP_TEST_DUPINDEX);
258  m_strHash = UtilCreateStringHash(static_cast<int>(ind.size()),
259  &ind[0], &els[0]);
260  m_norm = calcNorm();
261  sortVar();
262  }
263  }
264 
265  DecompVar(const int len,
266  const int* ind,
267  const double* els,
268  const double origCost) :
269  m_s (),
271  m_origCost(origCost),
272  m_redCost (0.0),
273  m_effCnt (0),
274  m_strHash (),
275  m_blockId (0),
276  m_colMasterIndex(-1),
277  m_norm (0.0) {
278  if (len > 0) {
279  m_s.setVector(len, ind, els, DECOMP_TEST_DUPINDEX);
280  m_strHash = UtilCreateStringHash(len, ind, els);
281  m_norm = calcNorm();
282  sortVar();
283  }
284  }
285 
286  DecompVar(const int len,
287  const int* ind,
288  const double* els,
289  const double origCost,
290  const DecompVarType varType) :
291  m_s (),
292  m_varType (varType),
293  m_origCost(origCost),
294  m_redCost (0.0),
295  m_effCnt (0),
296  m_strHash (),
297  m_blockId (0),
298  m_colMasterIndex(-1),
299  m_norm (0.0) {
300  if (len > 0) {
301  m_s.setVector(len, ind, els, DECOMP_TEST_DUPINDEX);
302  m_strHash = UtilCreateStringHash(len, ind, els);
303  m_norm = calcNorm();
304  sortVar();
305  }
306  }
307 
308  DecompVar(const int len,
309  const int* ind,
310  const double els,
311  const double origCost) :
312  m_s (),
314  m_origCost(origCost),
315  m_redCost (0.0),
316  m_effCnt (0),
317  m_strHash (),
318  m_blockId (0),
319  m_colMasterIndex(-1),
320  m_norm (0.0) {
321  if (len > 0) {
322  m_s.setConstant(len, ind, els, DECOMP_TEST_DUPINDEX);
323  m_strHash = UtilCreateStringHash(len, ind, els);
324  m_norm = calcNorm();
325  sortVar();
326  }
327  }
328 
329  DecompVar(const int len,
330  const int* ind,
331  const double els,
332  const double origCost,
333  const DecompVarType varType) :
334  m_s (),
335  m_varType (varType),
336  m_origCost(origCost),
337  m_redCost (0.0),
338  m_effCnt (0),
339  m_strHash (),
340  m_blockId (0),
341  m_colMasterIndex(-1),
342  m_norm (0.0) {
343  if (len > 0) {
344  m_s.setConstant(len, ind, els, DECOMP_TEST_DUPINDEX);
345  m_strHash = UtilCreateStringHash(len, ind, els);
346  m_norm = calcNorm();
347  sortVar();
348  }
349  }
350 
351  DecompVar(const int len,
352  const int* ind,
353  const double* els,
354  const double redCost,
355  const double origCost) :
356  m_s (),
358  m_origCost(origCost),
359  m_redCost (redCost),
360  m_effCnt (0),
361  m_strHash (),
362  m_blockId (0),
363  m_colMasterIndex(-1),
364  m_norm (0.0) {
365  if (len > 0) {
366  m_s.setVector(len, ind, els, DECOMP_TEST_DUPINDEX);
367  m_strHash = UtilCreateStringHash(len, ind, els);
368  m_norm = calcNorm();
369  sortVar();
370  }
371  }
372 
373  DecompVar(const int len,
374  const int* ind,
375  const double* els,
376  const double redCost,
377  const double origCost,
378  const DecompVarType varType) :
379  m_s (),
380  m_varType (varType),
381  m_origCost(origCost),
382  m_redCost (redCost),
383  m_effCnt (0),
384  m_strHash (),
385  m_blockId (0),
386  m_colMasterIndex(-1),
387  m_norm (0.0) {
388  if (len > 0) {
389  m_s.setVector(len, ind, els, DECOMP_TEST_DUPINDEX);
390  m_strHash = UtilCreateStringHash(len, ind, els);
391  m_norm = calcNorm();
392  sortVar();
393  }
394  }
395 
396  DecompVar(const int denseLen,
397  const double* denseArray,
398  const double redCost,
399  const double origCost,
400  const DecompVarType varType) :
402  m_varType (varType),
403  m_origCost(origCost),
404  m_redCost (redCost),
405  m_effCnt (0),
406  m_strHash (),
407  m_blockId (0),
408  m_colMasterIndex(-1),
409  m_norm (0.0) {
410  UtilPackedVectorFromDense(denseLen, denseArray, DecompEpsilon, m_s);
411 
412  if (m_s.getNumElements() > 0) {
413  m_strHash = UtilCreateStringHash(denseLen, denseArray);
414  m_norm = calcNorm();
415  sortVar();
416  }
417  }
418 
419  virtual ~DecompVar() {};
420 };
421 
422 #endif
#define DECOMP_TEST_DUPINDEX
Definition: Decomp.h:347
CoinPackedVector * UtilPackedVectorFromDense(const int len, const double *dense, const double etol)
void setConstant(int size, const int *inds, double elems, bool testForDuplicateIndex=COIN_DEFAULT_VALUE_FOR_DUPLICATE)
Elements set to have the same scalar value.
DecompVar(const std::vector< int > &ind, const std::vector< double > &els, const double redCost, const double origCost, const DecompVarType varType)
Definition: DecompVar.h:240
bool isEquivalent(const CoinPackedVectorBase &rhs, const FloatEqual &eq) const
equivalent - If shallow packed vector A &amp; B are equivalent, then they are still equivalent no matter ...
void resetEffectiveness()
Definition: DecompVar.h:95
DecompVar(const int len, const int *ind, const double els, const double origCost)
Definition: DecompVar.h:308
double getReducedCost() const
Definition: DecompVar.h:54
DecompVarType
Definition: Decomp.h:231
int getEffectiveness() const
Definition: DecompVar.h:57
double m_redCost
Definition: DecompVar.h:40
DecompVar(const int len, const int *ind, const double *els, const double redCost, const double origCost, const DecompVarType varType)
Definition: DecompVar.h:373
void setVarType(const DecompVarType varType)
Definition: DecompVar.h:79
double getOriginalCost() const
Definition: DecompVar.h:51
void setVector(int size, const int *inds, const double *elems, bool testForDuplicateIndex=COIN_DEFAULT_VALUE_FOR_DUPLICATE)
Set vector size, indices, and elements.
DecompVar(const std::vector< int > &ind, const std::vector< double > &els, const double redCost, const double origCost)
Definition: DecompVar.h:216
int m_effCnt
Definition: DecompVar.h:41
int getBlockId() const
Definition: DecompVar.h:69
void sortIncrIndex()
Sort the packed storage vector.
CoinPackedVector m_s
Definition: DecompVar.h:34
DecompVar(const int len, const int *ind, const double *els, const double origCost, const DecompVarType varType)
Definition: DecompVar.h:286
void setColMasterIndex(const int colIndex)
Definition: DecompVar.h:82
virtual int getNumElements() const
Get the size.
virtual ~DecompVar()
Definition: DecompVar.h:419
bool doesSatisfyBounds(int denseLen, double *denseArr, const DecompAlgoModel &model, const double *lbs, const double *ubs)
DecompVar(const int len, const int *ind, const double els, const double origCost, const DecompVarType varType)
Definition: DecompVar.h:329
void setReducedCost(const double redCost)
Definition: DecompVar.h:88
void setBlockId(const int blockId)
Definition: DecompVar.h:85
int getColMasterIndex() const
Definition: DecompVar.h:72
const double DecompEpsilon
Definition: Decomp.h:62
void increaseEffCnt()
Increase the effectiveness count by 1 (or to 1 if it was negative).
Definition: DecompVar.h:101
double getUpperBound() const
Definition: DecompVar.h:63
DecompVar & operator=(const DecompVar &rhs)
Definition: DecompVar.h:164
void fillDenseArr(int len, double *arr)
bool isDuplicate(const DecompVarList &vars)
Definition: DecompVar.h:123
double m_norm
Definition: DecompVar.h:45
DecompVar(const int len, const int *ind, const double *els, const double redCost, const double origCost)
Definition: DecompVar.h:351
int m_colMasterIndex
Definition: DecompVar.h:44
double twoNorm() const
Return the 2-norm of the vector.
std::string getStrHash() const
Definition: DecompVar.h:66
DecompVar(const DecompVar &source)
Definition: DecompVar.h:153
DecompVar(const int denseLen, const double *denseArray, const double redCost, const double origCost, const DecompVarType varType)
Definition: DecompVar.h:396
bool isEquivalent(const DecompVar &dvar)
Definition: DecompVar.h:119
int m_blockId
Definition: DecompVar.h:43
Sparse Vector.
void decreaseEffCnt()
Decrease the effectiveness count by 1 (or to -1 if it was positive).
Definition: DecompVar.h:107
double getLowerBound() const
Definition: DecompVar.h:60
DecompVar(const std::vector< int > &ind, const double els, const double redCost, const double origCost, const DecompVarType varType)
Definition: DecompVar.h:191
DecompVarType getVarType() const
Definition: DecompVar.h:48
DecompVarType m_varType
Definition: DecompVar.h:38
virtual void print(std::ostream *os=&std::cout, DecompApp *app=0) const
double calcNorm()
Definition: DecompVar.h:111
DecompVar(const int len, const int *ind, const double *els, const double origCost)
Definition: DecompVar.h:265
std::list< DecompVar * > DecompVarList
Definition: Decomp.h:53
double m_origCost
Definition: DecompVar.h:39
std::string m_strHash
Definition: DecompVar.h:42
void sortVar()
Definition: DecompVar.h:115
double getNorm() const
Definition: DecompVar.h:75
void setOriginalCost(const double origCost)
Definition: DecompVar.h:91
The main application class.
Definition: DecompApp.h:50
string UtilCreateStringHash(const int len, const double *els, const int precision=6)