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