Dip  0.92.4
DecompApp.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 
16 #ifndef DECOMP_APP_INCLUDED
17 #define DECOMP_APP_INCLUDED
18 
19 //===========================================================================//
20 #include "UtilParameters.h"
21 #include "DecompParam.h"
22 #include "DecompModel.h"
23 #include "DecompSolution.h"
24 #include "DecompConstraintSet.h"
25 #include "CoinMpsIO.hpp"
26 #include "CoinLpIO.hpp"
27 
28 extern "C" {
29 #if defined (COIN_HAS_METIS)
30 #include "hmetis.h"
31 #endif
32 }
33 //===========================================================================//
34 class DecompAlgo;
35 
36 //===========================================================================//
47 //===========================================================================//
48 class DecompApp {
49 
50 private:
54  std::string m_classTag;
55 
56 protected:
60  std::ostream* m_osLog;
61 
65  double m_bestKnownLB;
66  double m_bestKnownUB;
67 
68 public:
69 
74  int NumBlocks;
75 
81 
85  const double* m_objective;
86 
91 
95  std::map<int, DecompModel> m_modelRelax;
96 
100  std::map<int, std::vector<DecompModel> > m_modelRelaxNest;
101 
107  /*
108  * The following definitiions was from MILPBlock
109  *
110  */
111 
114 
117 
124  std::map<int, DecompConstraintSet*> m_modelR;
125 
128  std::map<int, std::vector<int> > m_blocks;
129 
130 
141 
144  double m_infinity;
145 
146 public:
155  void preprocess();
156 
160  void startupLog();
161 
162  //TODO:
163  //base layer needs to do some kind of check to make sure this actually
164  //got done - but also nice to have user version... so base createModel
165  //and user uesrCreateModel() which is pure?, in base userCreateModel
166  //gets called and checked that it returns good information
167  int createModel();
168 
169  inline const double getBestKnownLB() const {
170  return m_bestKnownLB;
171  }
172  inline const double getBestKnownUB() const {
173  return m_bestKnownUB;
174  }
175 
176  inline void setBestKnownLB(const double bestKnownLB) {
177  m_bestKnownLB = bestKnownLB;
178  }
179  inline void setBestKnownUB(const double bestKnownUB) {
180  m_bestKnownUB = bestKnownUB;
181  }
182 
183 
189  inline void setModelObjective(const double* objective, const
190  int length) {
191  assert(objective);
192  double* obj = new double[length];
193  memcpy(obj, objective, length * sizeof(double));
194  m_objective = obj;
195  }
196 
202  //TODO: having these implementations in the header makes
203  // it harder to view this as an interface class - it is unclear
204  // what the user must do vs can do
205  inline void setModelCore(DecompConstraintSet* model,
206  const std::string modelName) {
207  assert(model);
208 
209  if (!model->hasPrepRun()) {
210  model->prepareModel(m_infinity, true);
211  }
212 
213  m_modelCore.setModel(model);
214  m_modelCore.setModelName(modelName);
215  }
217  //TODO: change to setModelCore( ... )
218  // to long set of args of basic types - more like how Osi
219  // does loadProblem, etc... or juse use Osi as shell and accept
220  // an OsiSolverInterface?? or an OsiModel... is there one?
221  // maybe use CoinModel?? much cleaner?
222  //at least set up an alternative to do it that way
223  //still, this is still a function the user must call versus
224  // a method they MUST override -which would fit more into the
225  // framework concept - for e.g.,
226  //virtual CoinModel * createModelCore() - the use must create a CoinModel
227  // or maybe a DecompModel which is derived from a CoinModel with
228  // whatever extra stuff might be needed - but I would try to avoid that
229  //is it best to have it returned as return of function - forcing the
230  //user to do it? or as arguments? and copy or assign pointers like
231  //in SAS load problem design
232 
233 
234 
242  const std::string modelName = "",
243  const int blockId = 0);
244 
250  const std::string modelName,
251  const int blockId = 0);
252 
256  inline DecompAlgo* getDecompAlgo() const {
257  return m_decompAlgo;
258  }
259 
260 
266 public:
282  virtual void initDualVector(std::vector<double>& dualVector) {}
283 
284 
285  //TODO: change name - no other one is using APP, why here?
308  //TODO: what is doxy tag for function return
309  //TOOD: don't need numCols and tolZero should not be user overidable
310  virtual bool APPisUserFeasible(const double* x,
311  const int numCols,
312  const double tolZero) {
313  return true;
314  };
315 
316  virtual int APPheuristics(const double* xhat,
317  const double* origCost,
318  std::vector<DecompSolution*>& xhatIPFeas) {
319  return 0;
320  }
321 
330  virtual const double* getDualForGenerateVars(const double* dual) {
331  return 0;
332  }
333 
334  virtual int generateInitVars(DecompVarList& initVars);
335 
336  virtual int generateCuts(const double* x,
337  DecompCutList& newCuts);
338 
339  virtual void solveRelaxedWhich(std::vector<int>& blocksToSolve,
340  std::map< int,
341  std::vector<double> >& userDualsByBlock) {
342  };
343 
344  virtual DecompSolverStatus solveRelaxed(const int whichBlock,
345  const double* redCostX,
346  const double target,
347  DecompVarList& varList) {
349  }
350  virtual DecompSolverStatus solveRelaxedNest(const int whichBlock,
351  const double* redCostX,
352  const double target,
353  DecompVarList& varList) {
355  }
356 
357 
358  virtual void printOriginalColumn(const int index,
359  std::ostream* os = &std::cout) const;
360 
361  //TODO: change api so colNames comes from modelCore if exists
362  // rather than - to simplify API
363  virtual void printOriginalSolution(const int n_cols,
364  const std::vector<std::string>& colNames,
365  const double* solution,
366  std::ostream* os = &std::cout) const;
367 
374 public:
375 
377  virtual void initializeApp();
378 
381  void createModels();
382 
383  DecompConstraintSet* createModelPart(const int nRowsPart,
384  const int* rowsPart);
385 
387  const int nRowsPart,
388  const int* rowsPart);
389 
391  const int nRowsPart,
392  const int* rowsPart);
393 
394 
395  void readInitSolutionFile(DecompVarList& initVars);
396 
398  void readBlockFile();
399 
401  void readProblem();
402 
403 
407 
409  void findActiveColumns(const std::vector<int>& rowsPart,
410  std::set<int>& activeColsSet);
411 
414  const std::string getInstanceName() {
415  return m_param.Instance;
416  }
417  /*
418  void HMETIS_PartKway(int nvtxs, int nhedges, int *vwgts, int *eptr,
419  int *eind, int *hewgts, int nparts, int ubfactor,
420  int * options, int * part, int *edgecut);
421 
422  void HMETIS_PartRecursive(int nvtxs, int nhedges, int *vwgts, int *eptr,
423  int *eind, int *hewgts, int nparts, int ubfactor,
424  int * options, int * part, int *edgecut);
425  */
426 
429  const CoinPackedMatrix* getMatrix() { return m_matrix; }
430 
433  void setInfinity();
434 
435 public:
442  DecompApp(UtilParameters& utilParam) :
443  m_classTag ("D-APP"),
444  m_osLog (&std::cout ),
445  m_bestKnownLB(-1e75 ),
446  m_bestKnownUB( 1e75 ),
447  NumBlocks ( 0 ),
448  m_utilParam (&utilParam),
449  m_objective ( NULL ),
450  m_modelCore (utilParam),
451  m_matrix ( NULL ),
452  m_modelC ( NULL ),
453  m_threadIndex( 0 )
454  {
455  //---
456  //--- get application parameters
457  //---
458  m_param.getSettings(utilParam);
459 
460  if (m_param.LogLevel >= 1) {
462  }
463 
464  startupLog();
465 
466  setInfinity();
467  };
468 
472  virtual ~DecompApp() {
476  };
477 };
478 
479 #endif
std::list< DecompCut * > DecompCutList
Definition: Decomp.h:93
const CoinPackedMatrix * getMatrix()
Get constraint matrix for analysis.
Definition: DecompApp.h:429
const CoinPackedMatrix * m_matrix
Original constraint matrix for the instance.
Definition: DecompApp.h:120
void setModelObjective(const double *objective, const int length)
Set the model objective function.
Definition: DecompApp.h:189
std::map< int, DecompConstraintSet * > m_modelR
Definition: DecompApp.h:124
virtual void solveRelaxedWhich(std::vector< int > &blocksToSolve, std::map< int, std::vector< double > > &userDualsByBlock)
Initialize the dual vector for PhaseII of PC.
Definition: DecompApp.h:339
void setModelName(const std::string modelName)
Definition: DecompModel.h:49
virtual void initDualVector(std::vector< double > &dualVector)
Initialize the dual vector for PhaseII of PC.
Definition: DecompApp.h:282
DecompAlgo * m_decompAlgo
Pointer to the base algorithmic object.
Definition: DecompApp.h:106
const double getBestKnownUB() const
Preprocess (standard ): on the TODO list.
Definition: DecompApp.h:172
Class to read and write Lp files.
Definition: CoinLpIO.hpp:105
std::string m_classTag
Store the name of the class (for logging/debugging) - &quot;who am I?&quot;.
Definition: DecompApp.h:54
void setBestKnownLB(const double bestKnownLB)
Preprocess (standard ): on the TODO list.
Definition: DecompApp.h:176
std::map< int, DecompModel > m_modelRelax
Model data: the relaxed model(s) (A&#39;)
Definition: DecompApp.h:95
void dumpSettings(const std::string &sec, std::ostream *os=&std::cout)
Definition: DecompParam.h:473
virtual void printOriginalColumn(const int index, std::ostream *os=&std::cout) const
Initialize the dual vector for PhaseII of PC.
DecompApp(UtilParameters &utilParam)
Constructor for base DecompApp class.
Definition: DecompApp.h:442
void singlyBorderStructureDetection()
Automatically detect singly bordered structure.
void setModelRelaxNest(DecompConstraintSet *model, const std::string modelName, const int blockId=0)
Set the model relaxed (nested) constraint matrix (for a particular block).
#define UTIL_DELARR(x)
Definition: UtilMacros.h:29
Sparse Matrix Base Class.
void setModelCore(DecompConstraintSet *model, const std::string modelName)
Set the model core constraint matrix.
Definition: DecompApp.h:205
double m_infinity
The value of infinity.
Definition: DecompApp.h:144
int m_threadIndex
serves as an index to track different DecompApp object during Concurrent process, where when m_thread...
Definition: DecompApp.h:140
void setBestKnownUB(const double bestKnownUB)
Preprocess (standard ): on the TODO list.
Definition: DecompApp.h:179
void readBlockFile()
Read block file.
int LogLevel
0: print nothing 1: print the node objective history
Definition: DecompParam.h:38
void preprocess()
Preprocess (standard ): on the TODO list.
const double * m_objective
Model data: objective function.
Definition: DecompApp.h:85
DecompModel m_modelCore
Model data: the core model (A&#39;&#39;)
Definition: DecompApp.h:90
std::map< int, std::vector< int > > m_blocks
Definition of blocks (by rows)
Definition: DecompApp.h:128
virtual int generateCuts(const double *x, DecompCutList &newCuts)
Initialize the dual vector for PhaseII of PC.
virtual bool APPisUserFeasible(const double *x, const int numCols, const double tolZero)
Method to determine if the solution (x) is feasible to the original model.
Definition: DecompApp.h:310
virtual ~DecompApp()
Destructor.
Definition: DecompApp.h:472
std::ostream * m_osLog
Log file.
Definition: DecompApp.h:60
void getSettings(UtilParameters &param)
Definition: DecompParam.h:453
void createModelPartSparse(DecompConstraintSet *model, const int nRowsPart, const int *rowsPart)
void startupLog()
Print startup message to log.
const std::string getInstanceName()
Get Intance name.
Definition: DecompApp.h:414
CoinLpIO m_lpIO
LP object for reading instances.
Definition: DecompApp.h:116
virtual void initializeApp()
Initialize applications.
DecompAlgo * getDecompAlgo() const
Get a pointer to the base algorithm class.
Definition: DecompApp.h:256
void setInfinity()
Set the value of infinity.
virtual const double * getDualForGenerateVars(const double *dual)
This function allows the user to return their own dual vector to be used in the generation of new var...
Definition: DecompApp.h:330
#define UTIL_DELPTR(x)
Definition: UtilMacros.h:28
virtual int generateInitVars(DecompVarList &initVars)
Initialize the dual vector for PhaseII of PC.
UtilParameters * m_utilParam
Definition: DecompApp.h:80
std::map< int, std::vector< DecompModel > > m_modelRelaxNest
Model data: the relaxed (nested) model(s) (A&#39;)
Definition: DecompApp.h:100
void findActiveColumns(const std::vector< int > &rowsPart, std::set< int > &activeColsSet)
Find the active columns for some block.
double m_bestKnownLB
The best known LB/UB for this application (if known, for debugging).
Definition: DecompApp.h:65
void setModel(DecompConstraintSet *model)
Definition: DecompModel.h:46
DecompParam m_param
Parameters.
Definition: DecompApp.h:79
void prepareModel(double infinity, bool modelIsCore=false)
double m_bestKnownUB
Definition: DecompApp.h:66
virtual void printOriginalSolution(const int n_cols, const std::vector< std::string > &colNames, const double *solution, std::ostream *os=&std::cout) const
Initialize the dual vector for PhaseII of PC.
int createModel()
Preprocess (standard ): on the TODO list.
virtual int APPheuristics(const double *xhat, const double *origCost, std::vector< DecompSolution * > &xhatIPFeas)
Initialize the dual vector for PhaseII of PC.
Definition: DecompApp.h:316
virtual DecompSolverStatus solveRelaxedNest(const int whichBlock, const double *redCostX, const double target, DecompVarList &varList)
Initialize the dual vector for PhaseII of PC.
Definition: DecompApp.h:350
Base class for DECOMP algorithms.
Definition: DecompAlgo.h:62
virtual DecompSolverStatus solveRelaxed(const int whichBlock, const double *redCostX, const double target, DecompVarList &varList)
Initialize the dual vector for PhaseII of PC.
Definition: DecompApp.h:344
void readInitSolutionFile(DecompVarList &initVars)
std::string Instance
0: print nothing 1: print the node objective history
Definition: DecompParam.h:230
CoinMpsIO m_mpsIO
MPS object for reading instances.
Definition: DecompApp.h:113
DecompSolverStatus
Definition: Decomp.h:208
void createModels()
Create model parts.
const double getBestKnownLB() const
Preprocess (standard ): on the TODO list.
Definition: DecompApp.h:169
const bool hasPrepRun() const
void setModelRelax(DecompConstraintSet *model, const std::string modelName="", const int blockId=0)
Set the model relaxed constraint matrix (for a particular block).
std::list< DecompVar * > DecompVarList
Definition: Decomp.h:91
void readProblem()
Read Problem.
DecompConstraintSet * createModelPart(const int nRowsPart, const int *rowsPart)
void UtilDeleteMapPtr(std::map< S, T * > &mapPtr, typename std::map< S, T * >::iterator first, typename std::map< S, T * >::iterator last)
Definition: UtilMacros.h:651
DecompConstraintSet * m_modelC
The model constraint systems used for different algos.
Definition: DecompApp.h:123
MPS IO Interface.
Definition: CoinMpsIO.hpp:401
The main application class.
Definition: DecompApp.h:48
int NumBlocks
Number of Blocks defalut value 0 set by BlockNumInput parameter.
Definition: DecompApp.h:74