Dip  0.92.4
DecompAlgo.h
Go to the documentation of this file.
1 //===========================================================================//
2 // This file is part of the Decomp Solver Framework. //
3 // //
4 // Decomp is distributed under the Common Public License as part of the //
5 // COIN-OR repository (http://www.coin-or.org). //
6 // //
7 // Author: Matthew Galati, Lehigh University //
8 // //
9 // Copyright (C) 2002-2007, Lehigh University, Matthew Galati, and Ted Ralphs//
10 // All Rights Reserved. //
11 //===========================================================================//
12 
13 #ifndef DECOMP_ALGO_INCLUDED
14 #define DECOMP_ALGO_INCLUDED
15 
16 #define EXPLICIT_BOUNDS
17 
18 #include "DecompConstraintSet.h"
19 #include "DecompSolution.h"
20 #include "DecompMemPool.h"
21 #include "DecompVarPool.h"
22 #include "DecompCutPool.h"
23 #include "DecompStats.h"
24 //class DecompStats;
25 class DecompApp;
26 class ClpModel;
27 
28 
29 #include "OsiSolverInterface.hpp"
30 #include "DecompConstants.h"
31 
32 //TODO: bundle????
33 //TODO:
34 // logfile for app, algo separate? as well as possible to combine?
35 // might be better just to point this log file to the app log file
36 
37 
38 
39 // --------------------------------------------------------------------- //
40 class DecompAlgo {
41 private:
42  DecompAlgo(const DecompAlgo&);
44 
45 private:
46  static const char* m_classTag;
47  static const char* versionTag;
48  decompAlgoType m_algo; //"who am i"
49 
50 protected:
53 
65 
67 
68  ostream* m_osLog;
69 
70  //THINK: naming outer, inner?? - will work across all?
71 
72  //solver interface for subproblem (P')
73  map<int, OsiSolverInterface*> m_subprobSI;
74 
75  //solver interface for master (Q")
77 
78 #ifdef __DECOMP_LP_CLP__
79  //THINK: so we don't freecached when getModelPtr - do once...
80  //makes it so that we are stuck with CLP - also need to support CPX
81  ClpModel* m_masterCLP;
82 #endif
83 
84 
85  //m_vars and m_cuts have no destructor, but they are list of ptrs
86  //the memory they point to needs to be deleted.
87 
88  //member of app or member of algo?
89  DecompVarList m_vars; //list of vars added to master
94 
95  double m_tlb;
96  double m_tub;
97  double m_bestUpperBound;//known opt
98 
99  double* m_xhat;
100 
101  //think about do we want a solution pool?
102  //for now, just keep the best and toss the rest
103  vector<DecompSolution*> m_xhatIPFeas;
105 
106  int m_numOrigCols; //DECOMP
107 
108  vector< vector<double> > m_optPoint;
109 
110  //stabilization techniques
111  double* m_piEstimate;
112  vector<bool> isStab;
113 
114 public:
115  //these are just pointers into app
118 
120 
121 public:
122  //helper functions
124 
125  //TODO: functions in alphabetical order??
126  void startupLog();
127  void initSetup(int whichModel = 1);
128 
129  //void solve(DecompApp * app,
130  // int whichModel = 1);
131  void tighten(int whichModel);
132  void solve(int whichModel = 1);
133 
134  inline double getTrueLowerBound() {
135  return m_tlb;
136  }
137  inline double getTrueUpperBound() {
138  return m_tub;
139  }
140 
141  //TODO: should not call these LB UB
142  void setTrueLowerBound(const double mostNegReducedCost);
143  void setTrueUpperBound(const double ub) {
144  m_tub = ub;
145  };
146  double calcConstant(const int m,
147  const double* u);
148 
149  //helper functions - DecompDebug.cpp
150  bool isDualRayInfProof(const double* dualRay,
151  const CoinPackedMatrix* rowMatrix,
152  const double* colLB,
153  const double* colUB,
154  const double* rowRhs,
155  ostream* os = 0);
157  ostream* os);
159  const string baseName,
160  const int nodeIndex,
161  const int cutPass,
162  const int pricePass,
163  const bool printMps = true,
164  const bool printLp = true);
166  const string fileName,
167  const bool printMps = true,
168  const bool printLp = true);
169 
170  void printVars(ostream* os = &cout);
171  void printCuts(ostream* os = &cout);
172 
173  void solveBruteForce();
174  void createFullMps(const string filename);
175  vector<double*> getDualRays(int maxNumRays);
176 
177  inline void setApp(DecompApp* app) {
178  m_app = app;
179  }
180 
181 
182  inline void setBestUpperBound(const double bestUpperBound) {
183  m_bestUpperBound = bestUpperBound;
184  }
185 
186  decompStat solveRelaxed(const int whichModel,
187  const double* redCostX,
188  const double* origCost,
189  const double alpha,
190  const int n_origCols,
191  const bool checkRC,
192  const bool checkDup,
194  list<DecompVar*> & vars);
195 
196 public:
197  //pure virtual methods
198 
199 
200 
201 
202  virtual void createMasterProblem(DecompVarList& initVars) = 0;
203 
204  //possible base is enough here too... think at least for PC and C
205  //seem to be ok
206 
207  //just base is enough for PC and C here
208  virtual decompStat solutionUpdate(const decompPhase phase,
209  const int maxInnerIter,
210  const int maxOuterIter);
211 
212  //just base is enough?
213  virtual decompPhase phaseUpdate(const decompPhase phase,
214  const decompStat stat);
215 
216 public:
217  //virtual methods
218  //all this mess, just so that we don't use m_masterSI for RC??
219  //doesn't make alot of sense to me... THINK
220 
221  //just feed u vector in rowPrice in OSI???
222 
223  virtual void setMasterBounds(const double* lbs,
224  const double* ubs);
225 
227  return m_masterSI;
228  }
229 
230  virtual const double* getRowPrice() const {
231  return m_masterSI->getRowPrice();
232  }
233 
234  inline const double* getX() {
235  return m_xhat;
236  }
237 
238  inline DecompApp* getApp() {
239  return m_app;
240  }
241 
242  inline const DecompSolution* getXhatIPBest() {
243  return m_xhatIPBest;
244  }
245 
246 #if 1
247  virtual const double* getRightHandSide() const {
248  return &m_modelCore->rowRhs[0];//THINK
249  //return m_masterSI->getRightHandSide();
250  }
251  virtual const char* getRowSense() const {
252  return &m_modelCore->rowSense[0];//THINK - should be SI?
253  //return m_masterSI->getRowSense();
254  }
255 #endif
256 
257  int heuristics(const double* xhat,
258  vector<DecompSolution*> & xhatIPFeas);
259 
260  virtual int generateVars(const decompStat stat,
261  DecompVarList& newVars,
262  double& mostNegReducedCost);
263  virtual int generateCuts(DecompCutList& newCuts);
264 
265  //different name? set x vector? and maybe base just memcpy
266  //while PC does recompose then copies in?
267  virtual void recomposeSolution(const double* solution,
268  double* rsolution) {
269  printf("\nbase recomposeSolution does nothing.");
270  };
271 
272  virtual int generateInitVars(DecompVarList& initVars);
273  virtual bool isDone() {
274  return false;
275  };
276 
277 
278  //will never get called in C, yet PC specific, do nothing in base
279  //and move to PC?
280  void addVarsToPool(DecompVarList& newVars);
281  void addVarsFromPool();
282 
283 
284  bool isIPFeasible(const double* x,
285  const double feasTol = 1.0e-4,
286  const double intTol = 1.0e-4);
287  bool isLPFeasible(const double* x,
288  const double feasTol = 1.0e-4);
289 
290 
291  //different for C and PC, pure virtual for now
292  virtual void addCutsToPool(const double* x,
293  DecompCutList& newCuts,
294  int& n_newCuts);
295  virtual int addCutsFromPool();
296 
297  //DecompBranch.cpp
298  int chooseBranchVar(int& branchedOnIndex,
299  double& branchedOnValue);
300  virtual int branch(int branchedOnIndex,
301  double branchedOnValue);
302  decompStat processNode(const int nodeIndex = 0);
303  //process node? or just process?
304 
305 
306  /*helper functions*/
307  inline void appendVars(DecompVar* var) {
308  m_vars.push_back(var);
309  }
310  inline void appendVars(DecompVarList& varList) {
311  copy(varList.begin(), varList.end(), back_inserter(m_vars));
312 #if 0
313 
314  if (m_vars.empty()) {
315  m_vars = varList;
316  } else {
317  m_vars.insert(m_vars.end(), varList.begin(), varList.end());
318  }
319 
320 #endif
321  }
322 
323 
324 
325 public:
327  DecompApp* app) :
328  m_algo(algo),
329  m_auxMemPool(),
330  m_app(app),
331  m_whichModel(-1),
332  m_whichCoreModel(-1),
333  m_nodeIndex(0),
336  m_cutCallsRound(0),
337  m_cutCallsTotal(0),
338  m_varsThisRound(0),
339  m_cutsThisRound(0),
340  m_varsThisCall(0),
341  m_cutsThisCall(0),
342  m_isTightenAlgo(0),
343  m_osLog(&cout),//TODO
344  m_masterSI(0),
345  m_vars(),
346  m_initVars(),
347  m_cuts(),
348  m_varpool(),
349  m_cutpool(),
350  m_tlb(-DecompInf),
351  m_tub( DecompInf),
352  m_bestUpperBound(DecompInf),
353  m_xhat(0),
354  m_xhatIPBest(0),
355  m_piEstimate(0) {
356  (*m_osLog).setf(ios::fixed);
357  };
358 
359  virtual ~DecompAlgo() {
360  map<int, OsiSolverInterface*>::iterator it;
361 
362  for (it = m_subprobSI.begin();
363  it != m_subprobSI.end(); it++) {
364  if (it->second) {
365  UTIL_DELPTR(it->second);
366  }
367  }
368 
372  //THINK - if was class, the std::list<T*> would be cleaner?
373  //let T's destructor do the work? the way cutpool manages that?
374  //should m_cuts and m_vars be pools anyway? vector to waiting row
375  //or list of waiting row pts?
378  //THINK - m_initVars always (?) gets pushed into m_vars -
379  //so if we free here we'll double free?
380  //UtilDeleteListPtr(m_initVars);
381  //?? who deletes m_var, m_cuts????
382  };
383 };
384 
385 #endif
386 
int m_isTightenAlgo
Definition: DecompAlgo.h:66
std::list< DecompCut * > DecompCutList
Definition: Decomp.h:93
virtual void createMasterProblem(DecompVarList &initVars)
Create the master problem (all algorithms must define this function).
void startupLog()
virtual void addVarsFromPool()
void UtilDeleteVectorPtr(vector< T * > &vectorPtr, typename vector< T * >::iterator first, typename vector< T * >::iterator last)
Definition: UtilMacros.h:288
vector< DecompSolution * > m_xhatIPFeas
Definition: DecompAlgo.h:103
vector< bool > isStab
Definition: DecompAlgo.h:112
DecompConstraintSet * m_modelCore
Definition: DecompAlgo.h:116
DecompAlgo & operator=(const DecompAlgo &)
DecompStats m_stats
Storage of statistics for run and node.
Definition: DecompAlgo.h:113
int m_nodeIndex
Definition: DecompAlgo.h:54
int heuristics(const double *xhat, vector< DecompSolution * > &xhatIPFeas)
virtual DecompStatus processNode(const AlpsDecompTreeNode *node, const double globalLB, const double globalUB)
The main DECOMP process loop for a node.
map< int, OsiSolverInterface * > m_subprobSI
Definition: DecompAlgo.h:73
void solveBruteForce()
void printCurrentProblem(const OsiSolverInterface *si, const std::string baseName, const int nodeIndex, const int cutPass, const int pricePass, const int blockId=-1, const bool printMps=true, const bool printLp=true)
Initial setup of algorithm structures and solver interfaces.
int chooseBranchVar(int &branchedOnIndex, double &branchedOnValue)
void setBestUpperBound(const double bestUpperBound)
Definition: DecompAlgo.h:182
virtual void recomposeSolution(const double *solution, double *rsolution)
Definition: DecompAlgo.h:267
DecompMemPool m_auxMemPool
Definition: DecompAlgo.h:51
int m_priceCallsRound
Definition: DecompAlgo.h:57
virtual int generateVars(DecompVarList &newVars, double &mostNegReducedCost)
DecompStatus solveRelaxed(const double *redCostX, const double *origCost, const double alpha, const int n_origCols, const bool isNested, DecompSubModel &subModel, DecompSolverResult *solveResult, std::list< DecompVar * > &vars, double timeLimit)
DecompConstraintSet * m_modelRelax
Definition: DecompAlgo.h:117
void solve(int whichModel=1)
DecompCutList m_cuts
Containers for cuts (current and pool).
Definition: DecompAlgo.h:175
#define UTIL_DELARR(x)
Definition: UtilMacros.h:29
DecompSubModel m_modelCore
Store the name of the class (for logging/debugging) - &quot;who am I?&quot;.
Definition: DecompAlgo.h:161
Sparse Matrix Base Class.
DecompVarList m_vars
Containers for variables (current and pool).
Definition: DecompAlgo.h:169
DecompAlgo(const decompAlgoType algo, DecompApp *app)
Definition: DecompAlgo.h:326
int m_cutCallsRound
Definition: DecompAlgo.h:59
virtual const double * getRowPrice() const
Definition: DecompAlgo.h:230
std::vector< double * > getDualRays(int maxNumRays)
const double * getX()
Definition: DecompAlgo.h:234
int m_cutsThisRound
Definition: DecompAlgo.h:62
decompPhase
std::vector< DecompSolution * > m_xhatIPFeas
Store the name of the class (for logging/debugging) - &quot;who am I?&quot;.
Definition: DecompAlgo.h:189
double getTrueUpperBound()
Definition: DecompAlgo.h:137
virtual void addCutsToPool(const double *x, DecompCutList &newCuts, int &m_cutsThisCall)
double calcConstant(const int m, const double *u)
void appendVars(DecompVarList &varList)
Definition: DecompAlgo.h:310
void setTrueUpperBound(const double ub)
Definition: DecompAlgo.h:143
virtual bool isDone()
Definition: DecompAlgo.h:273
DecompVarList m_initVars
Definition: DecompAlgo.h:90
OsiSolverInterface * initSolverInterface()
decompAlgoType
void tighten(int whichModel)
void UtilDeleteListPtr(list< T * > &listPtr, typename list< T * >::iterator first, typename list< T * >::iterator last)
Definition: UtilMacros.h:308
int m_whichCoreModel
Definition: DecompAlgo.h:56
DecompApp * getApp()
Definition: DecompAlgo.h:238
DecompAlgo(const DecompAlgoType algo, DecompApp *app, UtilParameters &utilParam, bool doSetup=true)
Default constructors.
Definition: DecompAlgo.h:979
DecompVarPool m_varpool
Store the name of the class (for logging/debugging) - &quot;who am I?&quot;.
Definition: DecompAlgo.h:170
decompStat
void printVars(std::ostream *os)
Initial setup of algorithm structures and solver interfaces.
int m_priceCallsTotal
Definition: DecompAlgo.h:58
void printBasisInfo(OsiSolverInterface *si, std::ostream *os)
Initial setup of algorithm structures and solver interfaces.
int m_cutCallsTotal
Definition: DecompAlgo.h:60
static const char * versionTag
Definition: DecompAlgo.h:47
virtual DecompStatus solutionUpdate(const DecompPhase phase, const bool resolve=true, const int maxInnerIter=COIN_INT_MAX, const int maxOuterIter=COIN_INT_MAX)
Update of the solution vectors (primal and/or dual).
virtual void addVarsToPool(DecompVarList &newVars)
double * m_xhat
Storage for current solution (in x-space).
Definition: DecompAlgo.h:181
double getTrueLowerBound()
Definition: DecompAlgo.h:134
const DecompSolution * getXhatIPBest()
Definition: DecompAlgo.h:242
virtual const char * getRowSense() const
Definition: DecompAlgo.h:251
double m_tub
Definition: DecompAlgo.h:96
int m_varsThisRound
Definition: DecompAlgo.h:61
Abstract Base Class for describing an interface to a solver.
int m_varsThisCall
Definition: DecompAlgo.h:63
virtual void setMasterBounds(const double *lbs, const double *ubs)
void printCuts(std::ostream *os)
Initial setup of algorithm structures and solver interfaces.
DecompAlgoType m_algo
Type of algorithm for this instance.
Definition: DecompAlgo.h:86
decompAlgoType m_algo
Definition: DecompAlgo.h:48
bool isLPFeasible(const double *x, const bool isXSparse=false, const double feasVarTol=1.0e-6, const double feasConTol=1.0e-5)
int m_numOrigCols
Definition: DecompAlgo.h:106
DecompApp * m_app
Pointer to current active DECOMP application.
Definition: DecompAlgo.h:108
virtual int addCutsFromPool()
#define UTIL_DELPTR(x)
Definition: UtilMacros.h:28
void appendVars(DecompVar *var)
Definition: DecompAlgo.h:307
void setApp(DecompApp *app)
Definition: DecompAlgo.h:177
virtual ~DecompAlgo()
Definition: DecompAlgo.h:359
bool isIPFeasible(const double *x, const bool isXSparse=false, const double feasVarTol=1.0e-6, const double feasConTol=1.0e-5, const double intTol=1.0e-5)
OsiSolverInterface * m_masterSI
Solver interface(s) for subproblems (P&#39;).
Definition: DecompAlgo.h:147
double m_bestUpperBound
Definition: DecompAlgo.h:97
DecompSolution * m_xhatIPBest
Store the name of the class (for logging/debugging) - &quot;who am I?&quot;.
Definition: DecompAlgo.h:190
virtual int generateCuts(double *xhat, DecompCutList &newCuts)
virtual const double * getRowPrice() const =0
Get pointer to array[getNumRows()] of dual variable values.
double * m_piEstimate
Definition: DecompAlgo.h:111
ostream * m_osLog
Definition: DecompAlgo.h:68
virtual const double * getRightHandSide() const
Definition: DecompAlgo.h:247
static const char * m_classTag
Definition: DecompAlgo.h:46
OsiSolverInterface * getMasterSolverInterface()
Definition: DecompAlgo.h:226
DecompCutPool m_cutpool
Store the name of the class (for logging/debugging) - &quot;who am I?&quot;.
Definition: DecompAlgo.h:176
Base class for DECOMP algorithms.
Definition: DecompAlgo.h:62
double m_tlb
Definition: DecompAlgo.h:95
bool isDualRayInfProof(const double *dualRay, const CoinPackedMatrix *rowMatrix, const double *colLB, const double *colUB, const double *rowRhs, std::ostream *os)
Initial setup of algorithm structures and solver interfaces.
virtual void phaseUpdate(DecompPhase &phase, DecompStatus &status)
Update of the phase for process loop.
std::ostream * m_osLog
Stream for log file (default to stdout).
Definition: DecompAlgo.h:124
void setTrueLowerBound(const double mostNegReducedCost)
int m_whichModel
Definition: DecompAlgo.h:55
virtual int generateInitVars(DecompVarList &initVars)
Generate initial variables for master problem (PC/DC/RC).
std::list< DecompVar * > DecompVarList
Definition: Decomp.h:91
The main application class.
Definition: DecompApp.h:48
int m_cutsThisCall
Definition: DecompAlgo.h:64
void createFullMps(const std::string fileName)
Initial setup of algorithm structures and solver interfaces.
virtual int branch(int branchedOnIndex, double branchedOnValue)
void initSetup()
Initial setup of algorithm structures and solver interfaces.
vector< vector< double > > m_optPoint
Definition: DecompAlgo.h:108