Dip 0.95.0
Loading...
Searching...
No Matches
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
28extern "C" {
29#if defined (COIN_HAS_METIS)
30#include "hmetis.h"
31#endif
32}
33//===========================================================================//
34class DecompAlgo;
35
36//===========================================================================//
47//===========================================================================//
48class DecompApp {
49
50private:
54 std::string m_classTag;
55
56protected:
60 std::ostream* m_osLog;
61
67
68public:
69
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
145
146public:
156
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
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
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
266public:
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
374public:
375
377 virtual void initializeApp();
378
382
384 const int* rowsPart);
385
387 const int nRowsPart,
388 const int* rowsPart);
389
391 const int nRowsPart,
392 const int* rowsPart);
393
394
396
399
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
430
434
435public:
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
DecompSolverStatus
Definition Decomp.h:208
@ DecompSolStatNoSolution
Definition Decomp.h:213
std::list< DecompVar * > DecompVarList
Definition Decomp.h:92
std::list< DecompCut * > DecompCutList
Definition Decomp.h:93
#define UTIL_DELPTR(x)
Definition UtilMacros.h:64
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
#define UTIL_DELARR(x)
Definition UtilMacros.h:65
Base class for DECOMP algorithms.
Definition DecompAlgo.h:62
The main application class.
Definition DecompApp.h:48
std::map< int, DecompConstraintSet * > m_modelR
Definition DecompApp.h:124
DecompAlgo * m_decompAlgo
Pointer to the base algorithmic object.
Definition DecompApp.h:106
void setModelObjective(const double *objective, const int length)
Set the model objective function.
Definition DecompApp.h:189
void setInfinity()
Set the value of infinity.
void findActiveColumns(const std::vector< int > &rowsPart, std::set< int > &activeColsSet)
Find the active columns for some block.
void readProblem()
Read Problem.
void setModelCore(DecompConstraintSet *model, const std::string modelName)
Set the model core constraint matrix.
Definition DecompApp.h:205
void createModelPartSparse(DecompConstraintSet *model, const int nRowsPart, const int *rowsPart)
DecompConstraintSet * m_modelC
The model constraint systems used for different algos.
Definition DecompApp.h:123
virtual void solveRelaxedWhich(std::vector< int > &blocksToSolve, std::map< int, std::vector< double > > &userDualsByBlock)
Definition DecompApp.h:339
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
int NumBlocks
Number of Blocks defalut value 0 set by BlockNumInput parameter.
Definition DecompApp.h:74
virtual int generateInitVars(DecompVarList &initVars)
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
DecompParam m_param
Parameters.
Definition DecompApp.h:79
void setModelRelaxNest(DecompConstraintSet *model, const std::string modelName, const int blockId=0)
Set the model relaxed (nested) constraint matrix (for a particular block).
virtual int generateCuts(const double *x, DecompCutList &newCuts)
double m_bestKnownUB
Definition DecompApp.h:66
void setBestKnownLB(const double bestKnownLB)
Definition DecompApp.h:176
double m_infinity
The value of infinity.
Definition DecompApp.h:144
virtual ~DecompApp()
Destructor.
Definition DecompApp.h:472
void createModels()
Create model parts.
virtual DecompSolverStatus solveRelaxed(const int whichBlock, const double *redCostX, const double target, DecompVarList &varList)
Definition DecompApp.h:344
int m_threadIndex
serves as an index to track different DecompApp object during Concurrent process, where when m_thread...
Definition DecompApp.h:140
const double getBestKnownUB() const
Definition DecompApp.h:172
void readBlockFile()
Read block file.
const CoinPackedMatrix * getMatrix()
Get constraint matrix for analysis.
Definition DecompApp.h:429
const double * m_objective
Model data: objective function.
Definition DecompApp.h:85
std::map< int, std::vector< int > > m_blocks
Definition of blocks (by rows)
Definition DecompApp.h:128
virtual void initDualVector(std::vector< double > &dualVector)
Initialize the dual vector for PhaseII of PC.
Definition DecompApp.h:282
std::map< int, std::vector< DecompModel > > m_modelRelaxNest
Model data: the relaxed (nested) model(s) (A')
Definition DecompApp.h:100
virtual void printOriginalColumn(const int index, std::ostream *os=&std::cout) const
virtual void printOriginalSolution(const int n_cols, const std::vector< std::string > &colNames, const double *solution, std::ostream *os=&std::cout) const
int createModel()
virtual int APPheuristics(const double *xhat, const double *origCost, std::vector< DecompSolution * > &xhatIPFeas)
Definition DecompApp.h:316
void setModelRelax(DecompConstraintSet *model, const std::string modelName="", const int blockId=0)
Set the model relaxed constraint matrix (for a particular block).
void createModelPart(DecompConstraintSet *model, const int nRowsPart, const int *rowsPart)
virtual DecompSolverStatus solveRelaxedNest(const int whichBlock, const double *redCostX, const double target, DecompVarList &varList)
Definition DecompApp.h:350
const std::string getInstanceName()
Get Intance name.
Definition DecompApp.h:414
CoinMpsIO m_mpsIO
MPS object for reading instances.
Definition DecompApp.h:113
std::map< int, DecompModel > m_modelRelax
Model data: the relaxed model(s) (A')
Definition DecompApp.h:95
virtual void initializeApp()
Initialize applications.
CoinLpIO m_lpIO
LP object for reading instances.
Definition DecompApp.h:116
DecompApp(UtilParameters &utilParam)
Constructor for base DecompApp class.
Definition DecompApp.h:442
const CoinPackedMatrix * m_matrix
Original constraint matrix for the instance.
Definition DecompApp.h:120
DecompAlgo * getDecompAlgo() const
Get a pointer to the base algorithm class.
Definition DecompApp.h:256
DecompModel m_modelCore
Model data: the core model (A'')
Definition DecompApp.h:90
void readInitSolutionFile(DecompVarList &initVars)
void singlyBorderStructureDetection()
Automatically detect singly bordered structure.
double m_bestKnownLB
The best known LB/UB for this application (if known, for debugging).
Definition DecompApp.h:65
void startupLog()
Print startup message to log.
void preprocess()
Preprocess (standard ): on the TODO list.
std::ostream * m_osLog
Log file.
Definition DecompApp.h:60
void setBestKnownUB(const double bestKnownUB)
Definition DecompApp.h:179
UtilParameters * m_utilParam
Definition DecompApp.h:80
DecompConstraintSet * createModelPart(const int nRowsPart, const int *rowsPart)
const double getBestKnownLB() const
Definition DecompApp.h:169
void prepareModel(double infinity, bool modelIsCore=false)
const bool hasPrepRun() const
void setModel(DecompConstraintSet *model)
Definition DecompModel.h:46
void setModelName(const std::string modelName)
Definition DecompModel.h:49
void getSettings(UtilParameters &param)
std::string Instance
void dumpSettings(const std::string &sec, std::ostream *os=&std::cout)