Dip-All  0.91.0
DecompModel.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, and Ted Ralphs//
13 // All Rights Reserved. //
14 //===========================================================================//
15 
16 #ifndef DECOMP_MODEL_INCLUDED
17 #define DECOMP_MODEL_INCLUDED
18 
19 //===========================================================================//
20 #include "UtilMacrosDecomp.h"
21 #include "DecompParam.h"
22 #include "DecompConstraintSet.h"
23 #include "DecompSolverResult.h"
24 #ifdef __DECOMP_IP_SYMPHONY__
25 #include "OsiSolverInterface.hpp"
26 #include "OsiSymSolverInterface.hpp"
27 #endif
28 //===========================================================================//
29 //naming convention - usually would do DecompModelXx, DecompModelYy
30 //===========================================================================//
32 protected:
34  std::string m_modelName;
35  int m_blockId;
36 
37 public:
39  return m_model;
40  }
41  const std::string& getModelName() const {
42  return m_modelName;
43  }
44  const int getBlockId() const {
45  return m_blockId;
46  }
47 
48 public:
49  void setModel (DecompConstraintSet* model) {
50  m_model = model;
51  }
52  void setModelName(const std::string modelName) {
53  m_modelName = modelName;
54  }
55  void setBlockId (const int blockId) {
56  m_blockId = blockId;
57  }
58 
59 public:
60  DecompAppModel(const DecompAppModel& appModel) {
61  m_model = appModel.m_model;
62  m_modelName = appModel.m_modelName;
63  m_blockId = appModel.m_blockId;
64  }
66  m_model = rhs.m_model;
68  m_blockId = rhs.m_blockId;
69  return *this;
70  }
72  m_model (NULL),
73  m_modelName(""),
74  m_blockId (0) {};
76  std::string modelName,
77  int blockId) :
78  m_model (model),
79  m_modelName(modelName),
80  m_blockId (blockId) {};
81  virtual ~DecompAppModel() {}
82 };
83 
84 //===========================================================================//
86 private:
88 #ifdef __DECOMP_IP_SYMPHONY__
89  OsiSymSolverInterface* osi_Sym;
90 #endif
91  int m_numCols;
93  int m_counter;
94  int m_ws_tag;
96 public:
97 
98  inline void setCounter(const int num) {
99  m_counter = num;
100  }
101 
102  inline int getCounter() {
103  return m_counter;
104  }
105 
107  m_osi = osi;
108 
109  if (!m_colIndices) {
110  //---
111  //--- For use with (re-)setting the objective coefficients
112  //--- setup an array of indices 0,...,n-1. This object assumes
113  //--- that the number of columns stayed constant throughout.
114  //---
115  const int numCols = m_osi->getNumCols();
116  m_numCols = numCols;
117  m_colIndices = new int[numCols];
118 
119  if (!m_colIndices) {
120  UtilExceptionMemory("setOsi", "DecompAlgoModel");
121  }
122 
123  UtilIotaN(m_colIndices, numCols, 0);
124  }
125  }
126 
127  void setOsiObjCoeff(const double* objCoeff) {
128  assert(m_osi);
129  assert(m_colIndices);
130  assert(m_numCols == m_osi->getNumCols());
131 
132  if (getModel()->isSparse()) {
133  const DecompConstraintSet* model = getModel();
134  const std::map<int, int>& origToSparse = model->getMapOrigToSparse();
135  std::map<int, int>::const_iterator mcit;
136 
137  for (mcit = origToSparse.begin();
138  mcit != origToSparse.end(); mcit++) {
139  m_osi->setObjCoeff(mcit->second, //sparse-index
140  objCoeff[mcit->first]); //original-index
141  }
142  } else
144  m_colIndices + m_numCols, objCoeff);
145  }
146 
147 
148  void setActiveColBounds(const double* colLB,
149  const double* colUB) {
150  DecompConstraintSet* model = getModel();
151  std::vector<int>& activeColumns = model->activeColumns;
152 
153  //---
154  //--- if no active columns are set, assume they are all active
155  //--- for e.g., in the case of one block (or sparse)
156  //---
157  if (model->isSparse()) {
158  const std::map<int, int>& origToSparse = model->getMapOrigToSparse();
159  std::map<int, int>::const_iterator mcit;
160 
161  for (mcit = origToSparse.begin();
162  mcit != origToSparse.end(); mcit++) {
163  m_osi->setColLower(mcit->second, //sparse-index
164  colLB[mcit->first]); //original-index
165  m_osi->setColUpper(mcit->second, //sparse-index
166  colUB[mcit->first]); //original-index
167  //printf("setColBounds orig:%d sparse:%d lb:%g ub:%g\n",
168  // mcit->first, mcit->second,
169  // colLB[mcit->first],
170  // colUB[mcit->first]);
171  }
172  } else {
173  if (activeColumns.size()) {
174  std::vector<int>::iterator vi;
175 
176  for (vi = activeColumns.begin(); vi != activeColumns.end(); vi++) {
177  //printf("setColBounds i:%d to LB:%g UB:%g\n",
178  // *vi, colLB[*vi], colUB[*vi]);
179  m_osi->setColBounds(*vi, colLB[*vi], colUB[*vi]);
180  }
181  } else {
182  m_osi->setColLower(colLB);
183  m_osi->setColUpper(colUB);
184  }
185  }
186  }
187 
188 public:
190  return m_osi;
191  }
192 
193 public:
194  void solveOsiAsIp(DecompSolverResult* result,
195  DecompParam& param,
196  bool doExact,
197  bool doCutoff,
198  bool isRoot,
199  double cutoff,
200  double timeLimit);
201 
202  bool isPointFeasible(const double* x,
203  const bool isXSparse = false,
204  const int logLevel = 0,
205  const double feasVarTol = 1.0e-5,
206  const double feasConTol = 1.0e-4);
207 
208 public:
209  DecompAlgoModel(const DecompAppModel& appModel) :
210  DecompAppModel(appModel),
211  m_osi (NULL),
212 #ifdef __DECOMP_IP_SYMPHONY__
213  osi_Sym (NULL),
214 #endif
215  m_numCols (0 ),
216  m_colIndices (NULL),
217  m_counter ( 0 ),
218  m_ws_tag ( 0 ),
219  m_ws (NULL)
220  {};
221 
224  return *this;
225  }
226 
228  DecompAppModel(),
229  m_osi (NULL),
230 #ifdef __DECOMP_IP_SYMPHONY__
231  osi_Sym (NULL),
232 #endif
233  m_numCols (0 ),
234  m_colIndices (NULL),
235  m_counter (0),
236  m_ws_tag (0),
237  m_ws (NULL)
238  {};
240  std::string modelName,
241  int blockId) :
242  DecompAppModel(model, modelName, blockId),
243  m_osi (NULL),
244 #ifdef __DECOMP_IP_SYMPHONY__
245  osi_Sym (NULL),
246 #endif
247  m_numCols (0 ),
248  m_colIndices (NULL),
249  m_counter (0),
250  m_ws_tag (0),
251  m_ws (NULL)
252  {};
254  if (m_osi) {
255  delete m_osi;
256  }
257 
258  if (m_colIndices) {
259  delete [] m_colIndices;
260  }
261  }
262 };
263 
264 #endif
virtual void setColLower(int elementIndex, double elementValue)=0
Set a single column lower bound.
void setOsi(OsiSolverInterface *osi)
Definition: DecompModel.h:106
DecompConstraintSet * getModel() const
Definition: DecompModel.h:38
DecompAlgoModel(const DecompAppModel &appModel)
Definition: DecompModel.h:209
virtual void setObjCoeff(int elementIndex, double elementValue)=0
Set an objective function coefficient.
const int getBlockId() const
Definition: DecompModel.h:44
virtual void setColBounds(int elementIndex, double lower, double upper)
Set a single column lower and upper bound.
virtual int getNumCols() const =0
Get the number of columns.
std::vector< int > activeColumns
int * m_colIndices
Definition: DecompModel.h:92
const bool isSparse() const
void setOsiObjCoeff(const double *objCoeff)
Definition: DecompModel.h:127
virtual void setColUpper(int elementIndex, double elementValue)=0
Set a single column upper bound.
void setModel(DecompConstraintSet *model)
Definition: DecompModel.h:49
DecompAppModel & operator=(const DecompAppModel &rhs)
Definition: DecompModel.h:65
const std::string & getModelName() const
Definition: DecompModel.h:41
void UtilIotaN(int *first, const int size, const int init)
Definition: UtilMacros.h:171
DecompAppModel(DecompConstraintSet *model, std::string modelName, int blockId)
Definition: DecompModel.h:75
virtual void setObjCoeffSet(const int *indexFirst, const int *indexLast, const double *coeffList)
Set a set of objective function coefficients.
void setCounter(const int num)
Definition: DecompModel.h:98
bool isPointFeasible(const double *x, const bool isXSparse=false, const int logLevel=0, const double feasVarTol=1.0e-5, const double feasConTol=1.0e-4)
void solveOsiAsIp(DecompSolverResult *result, DecompParam &param, bool doExact, bool doCutoff, bool isRoot, double cutoff, double timeLimit)
DecompAlgoModel & operator=(const DecompAppModel &rhs)
Definition: DecompModel.h:222
std::string m_modelName
Definition: DecompModel.h:34
#define UtilExceptionMemory(methodN, classN)
OsiSolverInterface * getOsi() const
Definition: DecompModel.h:189
const std::map< int, int > & getMapOrigToSparse() const
void setActiveColBounds(const double *colLB, const double *colUB)
Definition: DecompModel.h:148
Abstract Base Class for describing an interface to a solver.
DecompAlgoModel(DecompConstraintSet *model, std::string modelName, int blockId)
Definition: DecompModel.h:239
void setModelName(const std::string modelName)
Definition: DecompModel.h:52
CoinWarmStart * m_ws
Definition: DecompModel.h:95
void setBlockId(const int blockId)
Definition: DecompModel.h:55
Abstract base class for warm start information.
DecompConstraintSet * m_model
Definition: DecompModel.h:33
virtual ~DecompAppModel()
Definition: DecompModel.h:81
DecompAppModel(const DecompAppModel &appModel)
Definition: DecompModel.h:60
Storage of solver result.
OsiSolverInterface * m_osi
Definition: DecompModel.h:87