Dip-All  0.91.0
DecompApp.h
Go to the documentation of this file.
1 
2 //===========================================================================//
3 // This file is part of the DIP Solver Framework. //
4 // //
5 // DIP is distributed under the Eclipse Public License as part of the //
6 // COIN-OR repository (http://www.coin-or.org). //
7 // //
8 // Author: Matthew Galati, SAS Institute Inc. (matthew.galati@sas.com) //
9 // //
10 // Conceptual Design: Matthew Galati, SAS Institute Inc. //
11 // Ted Ralphs, Lehigh University //
12 // //
13 // Copyright (C) 2002-2015, Lehigh University, Matthew Galati, Ted Ralphs //
14 // All Rights Reserved. //
15 //===========================================================================//
16 
17 
18 #ifndef DECOMP_APP_INCLUDED
19 #define DECOMP_APP_INCLUDED
20 
21 //===========================================================================//
22 #include "UtilParameters.h"
23 #include "DecompParam.h"
24 #include "DecompModel.h"
25 #include "DecompSolution.h"
26 #include "DecompConstraintSet.h"
27 #include "CoinMpsIO.hpp"
28 #include "CoinLpIO.hpp"
29 
30 extern "C" {
31 #if defined (COIN_HAS_METIS)
32 #include "hmetis.h"
33 #endif
34 }
35 //===========================================================================//
36 class DecompAlgo;
37 
38 //===========================================================================//
49 //===========================================================================//
50 class DecompApp {
51 
52 private:
56  std::string m_classTag;
57 
58 protected:
62  std::ostream* m_osLog;
63 
67  double m_bestKnownLB;
68  double m_bestKnownUB;
69 
70 public:
71 
76  int NumBlocks;
77 
82 
86  const double* m_objective;
87 
88 
93 
97  std::map<int, DecompAppModel> m_modelRelax;
98 
102  std::map<int, std::vector<DecompAppModel> > m_modelRelaxNest;
103 
109 
110 
111  /*
112  * The following definitiions was from MILPBlock
113  *
114  */
115 
118 
121 
125 
126 
130  std::map<int, DecompConstraintSet*> m_modelR;
131 
132 
135  std::map<int, std::vector<int> > m_blocks;
136 
137 
148 
149 public:
158  void preprocess();
159 
163  void startupLog();
164 
165  //TODO:
166  //base layer needs to do some kind of check to make sure this actually
167  //got done - but also nice to have user version... so base createModel
168  //and user uesrCreateModel() which is pure?, in base userCreateModel
169  //gets called and checked that it returns good information
170  int createModel();
171 
172  inline const double getBestKnownLB() const {
173  return m_bestKnownLB;
174  }
175  inline const double getBestKnownUB() const {
176  return m_bestKnownUB;
177  }
178 
179  inline void setBestKnownLB(const double bestKnownLB) {
180  m_bestKnownLB = bestKnownLB;
181  }
182  inline void setBestKnownUB(const double bestKnownUB) {
183  m_bestKnownUB = bestKnownUB;
184  }
185 
186 
192  inline void setModelObjective(const double* objective, const
193  int length) {
194  assert(objective);
195  double* obj = new double[length];
196  memcpy(obj, objective, length * sizeof(double));
197  m_objective = obj;
198  }
199 
205  //TODO: having these implementations in the header makes
206  // it harder to view this as an interface class - it is unclear
207  // what the user must do vs can do
208  inline void setModelCore(DecompConstraintSet* model,
209  const std::string modelName) {
210  assert(model);
211 
212  if (!model->hasPrepRun()) {
213  model->prepareModel(true);
214  }
215 
216  m_modelCore.setModel(model);
217  m_modelCore.setModelName(modelName);
218  }
220  //TODO: change to setModelCore( ... )
221  // to long set of args of basic types - more like how Osi
222  // does loadProblem, etc... or juse use Osi as shell and accept
223  // an OsiSolverInterface?? or an OsiModel... is there one?
224  // maybe use CoinModel?? much cleaner?
225  //at least set up an alternative to do it that way
226  //still, this is still a function the user must call versus
227  // a method they MUST override -which would fit more into the
228  // framework concept - for e.g.,
229  //virtual CoinModel * createModelCore() - the use must create a CoinModel
230  // or maybe a DecompModel which is derived from a CoinModel with
231  // whatever extra stuff might be needed - but I would try to avoid that
232  //is it best to have it returned as return of function - forcing the
233  //user to do it? or as arguments? and copy or assign pointers like
234  //in SAS load problem design
235 
236 
237 
244  inline void setModelRelax(DecompConstraintSet* model,
245  const std::string modelName = "",
246  const int blockId = 0) {
247  if (model && !model->hasPrepRun()) {
248  model->prepareModel();
249  }
250 
251  //---
252  //--- make sure this block has not been set yet
253  //---
254  std::map<int, DecompAppModel>::iterator mit = m_modelRelax.find(blockId);
255 
256  if (mit != m_modelRelax.end()) {
257  std::cerr << "Block " << blockId << " relaxation has already been set. "
258  << "Only one relaxation definition can be used at one time."
259  << std::endl;
260  throw UtilException("Multiple relaxation definitions",
261  "setModelRelax", "DecompApp");
262  }
263 
264  DecompAppModel appModel(model, modelName, blockId);
265  m_modelRelax.insert(std::make_pair(blockId, appModel));
266  }
267 
273  const std::string modelName,
274  const int blockId = 0) {
275  assert(model);
276 
277  if (!model->hasPrepRun()) {
278  model->prepareModel();
279  }
280 
281  DecompAppModel appModel(model, modelName, blockId);
282  m_modelRelaxNest[blockId].push_back(appModel);
283  }
284 
288  inline DecompAlgo* getDecompAlgo() const {
289  return m_decompAlgo;
290  }
291 
292 
298 public:
314  virtual void initDualVector(std::vector<double>& dualVector) {}
315 
316 
317  //TODO: change name - no other one is using APP, why here?
340  //TODO: what is doxy tag for function return
341  //TOOD: don't need numCols and tolZero should not be user overidable
342  virtual bool APPisUserFeasible(const double* x,
343  const int numCols,
344  const double tolZero) {
345  return true;
346  };
347 
348  virtual int APPheuristics(const double* xhat,
349  const double* origCost,
350  std::vector<DecompSolution*>& xhatIPFeas) {
351  return 0;
352  }
353 
362  virtual const double* getDualForGenerateVars(const double* dual) {
363  return 0;
364  }
365 
366  virtual int generateInitVars(DecompVarList& initVars);
367 
368  virtual int generateCuts(const double* x,
369  DecompCutList& newCuts);
370 
371  virtual void solveRelaxedWhich(std::vector<int>& blocksToSolve,
372  std::map< int,
373  std::vector<double> >& userDualsByBlock) {
374  };
375 
376  virtual DecompSolverStatus solveRelaxed(const int whichBlock,
377  const double* redCostX,
378  const double target,
379  DecompVarList& varList) {
381  }
382  virtual DecompSolverStatus solveRelaxedNest(const int whichBlock,
383  const double* redCostX,
384  const double target,
385  DecompVarList& varList) {
387  }
388 
389 
390  virtual void printOriginalColumn(const int index,
391  std::ostream* os = &std::cout) const;
392 
393  //TODO: change api so colNames comes from modelCore if exists
394  // rather than - to simplify API
395  virtual void printOriginalSolution(const int n_cols,
396  const std::vector<std::string>& colNames,
397  const double* solution,
398  std::ostream* os = &std::cout) const;
399 
406 public:
407 
409  virtual void initializeApp(UtilParameters& utilParam);
410 
413  void createModels();
414 
415  DecompConstraintSet* createModelPart(const int nRowsPart,
416  const int* rowsPart);
417 
419  const int nRowsPart,
420  const int* rowsPart);
421 
423  const int nRowsPart,
424  const int* rowsPart);
425 
426 
427  void readInitSolutionFile(DecompVarList& initVars);
428 
430  void readBlockFile();
431 
433  const CoinPackedMatrix* readProblem(UtilParameters& utilParam);
434 
435 
439 
441  void findActiveColumns(const std::vector<int>& rowsPart,
442  std::set<int>& activeColsSet);
443 
446  const std::string getInstanceName() {
447  return m_param.Instance;
448  }
449  /*
450  void HMETIS_PartKway(int nvtxs, int nhedges, int *vwgts, int *eptr,
451  int *eind, int *hewgts, int nparts, int ubfactor,
452  int * options, int * part, int *edgecut);
453 
454  void HMETIS_PartRecursive(int nvtxs, int nhedges, int *vwgts, int *eptr,
455  int *eind, int *hewgts, int nparts, int ubfactor,
456  int * options, int * part, int *edgecut);
457  */
458 
459 
460 
461 
462 public:
470  DecompApp(UtilParameters& utilParam) :
471  m_classTag ("D-APP"),
472  m_osLog (&std::cout ),
473  m_bestKnownLB(-1e75 ),
474  m_bestKnownUB( 1e75 ),
475  NumBlocks ( 0 ),
476  m_objective ( NULL ),
477  m_matrix ( NULL ),
478  m_modelC ( NULL ),
479  m_threadIndex( 0 ) {
480  m_param.getSettings(utilParam);
481  startupLog();
482  };
483 
484 
486  m_classTag ("D-APP"),
487  m_osLog (&std::cout ),
488  m_bestKnownLB(-1e75 ),
489  m_bestKnownUB( 1e75 ),
490  NumBlocks ( 0 ),
491  m_objective ( NULL ),
492  m_matrix ( NULL ),
493  m_modelC ( NULL ),
494  m_threadIndex( 0 ) {
495  //---
496  //--- comment these functions, which were used in
497  //--- MILPBlock, otherwise, conflict occurs in building
498  //--- individual examples
499  // m_param.getSettings(utilParam);
500  // initializeApp(utilParam);
501  // startupLog();
502  };
503 
507  virtual ~DecompApp() {
511  };
512 };
513 
514 #endif
std::list< DecompCut * > DecompCutList
Definition: Decomp.h:55
const CoinPackedMatrix * m_matrix
Original constraint matrix for the instance.
Definition: DecompApp.h:124
void setModelObjective(const double *objective, const int length)
Set the model objective function.
Definition: DecompApp.h:192
std::map< int, DecompConstraintSet * > m_modelR
Definition: DecompApp.h:130
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:371
virtual void initDualVector(std::vector< double > &dualVector)
Initialize the dual vector for PhaseII of PC.
Definition: DecompApp.h:314
DecompAlgo * m_decompAlgo
Pointer to the base algorithmic object.
Definition: DecompApp.h:108
const double getBestKnownUB() const
Preprocess (standard ): on the TODO list.
Definition: DecompApp.h:175
void prepareModel(bool modelIsCore=false)
Class to read and write Lp files.
Definition: CoinLpIO.hpp:104
std::string m_classTag
Store the name of the class (for logging/debugging) - &quot;who am I?&quot;.
Definition: DecompApp.h:56
void setBestKnownLB(const double bestKnownLB)
Preprocess (standard ): on the TODO list.
Definition: DecompApp.h:179
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:470
std::map< int, DecompAppModel > m_modelRelax
Model data: the relaxed model(s) (A&#39;)
Definition: DecompApp.h:97
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).
Definition: DecompApp.h:272
DecompAppModel m_modelCore
Model data: the core model (A&#39;&#39;)
Definition: DecompApp.h:92
#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:208
int m_threadIndex
serves as an index to track different DecompApp object during Concurrent process, where when m_thread...
Definition: DecompApp.h:147
void setBestKnownUB(const double bestKnownUB)
Preprocess (standard ): on the TODO list.
Definition: DecompApp.h:182
void readBlockFile()
Read block file.
void setModel(DecompConstraintSet *model)
Definition: DecompModel.h:49
void preprocess()
Preprocess (standard ): on the TODO list.
const double * m_objective
Model data: objective function.
Definition: DecompApp.h:86
std::map< int, std::vector< int > > m_blocks
Definition of blocks (by rows)
Definition: DecompApp.h:135
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:342
virtual ~DecompApp()
Destructor.
Definition: DecompApp.h:507
std::ostream * m_osLog
Log file.
Definition: DecompApp.h:62
void getSettings(UtilParameters &param)
Definition: DecompParam.h:447
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:446
CoinLpIO m_lpIO
LP object for reading instances.
Definition: DecompApp.h:120
DecompAlgo * getDecompAlgo() const
Get a pointer to the base algorithm class.
Definition: DecompApp.h:288
virtual void initializeApp(UtilParameters &utilParam)
Initialize applications.
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:362
std::map< int, std::vector< DecompAppModel > > m_modelRelaxNest
Model data: the relaxed (nested) model(s) (A&#39;)
Definition: DecompApp.h:102
void setModelName(const std::string modelName)
Definition: DecompModel.h:52
const CoinPackedMatrix * readProblem(UtilParameters &utilParam)
Read Problem.
#define UTIL_DELPTR(x)
Definition: UtilMacros.h:28
virtual int generateInitVars(DecompVarList &initVars)
Initialize the dual vector for PhaseII of PC.
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:67
DecompParam m_param
Parameters.
Definition: DecompApp.h:81
#define UtilException(msg, methodN, classN)
double m_bestKnownUB
Definition: DecompApp.h:68
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:348
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:382
Base class for DECOMP algorithms.
Definition: DecompAlgo.h:63
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:376
void readInitSolutionFile(DecompVarList &initVars)
std::string Instance
0: print nothing 1: print the node objective history
Definition: DecompParam.h:232
CoinMpsIO m_mpsIO
MPS object for reading instances.
Definition: DecompApp.h:117
DecompSolverStatus
Definition: Decomp.h:170
void createModels()
Create model parts.
const double getBestKnownLB() const
Preprocess (standard ): on the TODO list.
Definition: DecompApp.h:172
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).
Definition: DecompApp.h:244
std::list< DecompVar * > DecompVarList
Definition: Decomp.h:53
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:652
DecompConstraintSet * m_modelC
The model constraint systems used for different algos.
Definition: DecompApp.h:129
MPS IO Interface.
Definition: CoinMpsIO.hpp:329
The main application class.
Definition: DecompApp.h:50
int NumBlocks
Number of Blocks defalut value 0 set by BlockNumInput parameter.
Definition: DecompApp.h:76