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