DecompModel.h

Go to the documentation of this file.
00001 //===========================================================================//
00002 // This file is part of the DIP Solver Framework.                            //
00003 //                                                                           //
00004 // DIP is distributed under the Eclipse Public License as part of the        //
00005 // COIN-OR repository (http://www.coin-or.org).                              //
00006 //                                                                           //
00007 // Author: Matthew Galati, SAS Institute Inc. (matthew.galati@sas.com)       //
00008 //                                                                           //
00009 // Conceptual Design: Matthew Galati, SAS Institute Inc.                     //
00010 //                    Ted Ralphs, Lehigh University                          //
00011 //                                                                           //
00012 // Copyright (C) 2002-2015, Lehigh University, Matthew Galati, and Ted Ralphs//
00013 // All Rights Reserved.                                                      //
00014 //===========================================================================//
00015 
00016 #ifndef DECOMP_MODEL_INCLUDED
00017 #define DECOMP_MODEL_INCLUDED
00018 
00019 //===========================================================================//
00020 #include "UtilMacrosDecomp.h"
00021 #include "DecompParam.h"
00022 #include "DecompConstraintSet.h"
00023 #include "DecompSolverResult.h"
00024 #ifdef __DECOMP_IP_SYMPHONY__
00025 #include "OsiSolverInterface.hpp"
00026 #include "OsiSymSolverInterface.hpp"
00027 #endif
00028 //===========================================================================//
00029 //naming convention - usually would do DecompModelXx, DecompModelYy
00030 //===========================================================================//
00031 class DecompAppModel {
00032 protected:
00033    DecompConstraintSet* m_model;
00034    std::string           m_modelName;
00035    int                   m_blockId;
00036 
00037 public:
00038    DecompConstraintSet* getModel()     const {
00039       return m_model;
00040    }
00041    const std::string&      getModelName() const {
00042       return m_modelName;
00043    }
00044    const int             getBlockId()   const {
00045       return m_blockId;
00046    }
00047 
00048 public:
00049    void setModel    (DecompConstraintSet* model) {
00050       m_model = model;
00051    }
00052    void setModelName(const std::string modelName) {
00053       m_modelName = modelName;
00054    }
00055    void setBlockId  (const int blockId) {
00056       m_blockId = blockId;
00057    }
00058 
00059 public:
00060    DecompAppModel(const DecompAppModel& appModel) {
00061       m_model     = appModel.m_model;
00062       m_modelName = appModel.m_modelName;
00063       m_blockId   = appModel.m_blockId;
00064    }
00065    DecompAppModel& operator=(const DecompAppModel& rhs) {
00066       m_model     = rhs.m_model;
00067       m_modelName = rhs.m_modelName;
00068       m_blockId   = rhs.m_blockId;
00069       return *this;
00070    }
00071    DecompAppModel() :
00072       m_model    (NULL),
00073       m_modelName(""),
00074       m_blockId  (0) {};
00075    DecompAppModel(DecompConstraintSet* model,
00076                   std::string                modelName,
00077                   int                   blockId) :
00078       m_model    (model),
00079       m_modelName(modelName),
00080       m_blockId  (blockId) {};
00081    virtual ~DecompAppModel() {}
00082 };
00083 
00084 //===========================================================================//
00085 class DecompAlgoModel : public DecompAppModel {
00086 private:
00087    OsiSolverInterface*   m_osi;
00088 #ifdef __DECOMP_IP_SYMPHONY__
00089    OsiSymSolverInterface* osi_Sym;
00090 #endif
00091    int                   m_numCols;
00092    int*                  m_colIndices;
00093    int                   m_counter;
00094    int                   m_ws_tag;
00095    CoinWarmStart*        m_ws;
00096 public:
00097 
00098    inline void setCounter(const int num) {
00099       m_counter = num;
00100    }
00101 
00102    inline int getCounter() {
00103       return m_counter;
00104    }
00105 
00106    void setOsi(OsiSolverInterface* osi) {
00107       m_osi = osi;
00108 
00109       if (!m_colIndices) {
00110          //---
00111          //--- For use with (re-)setting the objective coefficients
00112          //---  setup an array of indices 0,...,n-1. This object assumes
00113          //---  that the number of columns stayed constant throughout.
00114          //---
00115          const int numCols = m_osi->getNumCols();
00116          m_numCols         = numCols;
00117          m_colIndices      = new int[numCols];
00118 
00119          if (!m_colIndices) {
00120             UtilExceptionMemory("setOsi", "DecompAlgoModel");
00121          }
00122 
00123          UtilIotaN(m_colIndices, numCols, 0);
00124       }
00125    }
00126 
00127    void setOsiObjCoeff(const double* objCoeff) {
00128       assert(m_osi);
00129       assert(m_colIndices);
00130       assert(m_numCols == m_osi->getNumCols());
00131 
00132       if (getModel()->isSparse()) {
00133          const DecompConstraintSet* model = getModel();
00134          const std::map<int, int>& origToSparse = model->getMapOrigToSparse();
00135          std::map<int, int>::const_iterator mcit;
00136 
00137          for (mcit = origToSparse.begin();
00138                mcit != origToSparse.end(); mcit++) {
00139             m_osi->setObjCoeff(mcit->second,           //sparse-index
00140                                objCoeff[mcit->first]); //original-index
00141          }
00142       } else
00143          m_osi->setObjCoeffSet(m_colIndices,
00144                                m_colIndices + m_numCols, objCoeff);
00145    }
00146 
00147 
00148    void setActiveColBounds(const double* colLB,
00149                            const double* colUB) {
00150       DecompConstraintSet* model         = getModel();
00151       std::vector<int>&          activeColumns = model->activeColumns;
00152 
00153       //---
00154       //--- if no active columns are set,  assume they are all active
00155       //---   for e.g., in the case of one block (or sparse)
00156       //---
00157       if (model->isSparse()) {
00158          const std::map<int, int>& origToSparse = model->getMapOrigToSparse();
00159          std::map<int, int>::const_iterator mcit;
00160 
00161          for (mcit  = origToSparse.begin();
00162                mcit != origToSparse.end(); mcit++) {
00163             m_osi->setColLower(mcit->second,        //sparse-index
00164                                colLB[mcit->first]); //original-index
00165             m_osi->setColUpper(mcit->second,        //sparse-index
00166                                colUB[mcit->first]); //original-index
00167             //printf("setColBounds orig:%d sparse:%d lb:%g ub:%g\n",
00168             //   mcit->first, mcit->second,
00169             //   colLB[mcit->first],
00170             //   colUB[mcit->first]);
00171          }
00172       } else {
00173          if (activeColumns.size()) {
00174             std::vector<int>::iterator vi;
00175 
00176             for (vi = activeColumns.begin(); vi != activeColumns.end(); vi++) {
00177                //printf("setColBounds i:%d to LB:%g UB:%g\n",
00178                //     *vi, colLB[*vi], colUB[*vi]);
00179                m_osi->setColBounds(*vi, colLB[*vi], colUB[*vi]);
00180             }
00181          } else {
00182             m_osi->setColLower(colLB);
00183             m_osi->setColUpper(colUB);
00184          }
00185       }
00186    }
00187 
00188 public:
00189    OsiSolverInterface*   getOsi() const {
00190       return m_osi;
00191    }
00192 
00193 public:
00194    void solveOsiAsIp(DecompSolverResult* result,
00195                      DecompParam&         param,
00196                      bool                 doExact,
00197                      bool                 doCutoff,
00198                      bool                 isRoot,
00199                      double               cutoff,
00200                      double               timeLimit);
00201 
00202    bool isPointFeasible(const double* x,
00203                         const bool     isXSparse  = false,
00204                         const int      logLevel   = 0,
00205                         const double   feasVarTol = 1.0e-5,
00206                         const double   feasConTol = 1.0e-4);
00207 
00208 public:
00209    DecompAlgoModel(const DecompAppModel& appModel) :
00210       DecompAppModel(appModel),
00211       m_osi         (NULL),
00212 #ifdef __DECOMP_IP_SYMPHONY__
00213       osi_Sym     (NULL),
00214 #endif
00215       m_numCols     (0   ),
00216       m_colIndices  (NULL),
00217       m_counter    ( 0 ),
00218       m_ws_tag     ( 0 ),
00219       m_ws          (NULL)
00220    {};
00221 
00222    DecompAlgoModel& operator=(const DecompAppModel& rhs) {
00223       DecompAppModel::operator=(rhs);
00224       return *this;
00225    }
00226 
00227    DecompAlgoModel() :
00228       DecompAppModel(),
00229       m_osi         (NULL),
00230 #ifdef __DECOMP_IP_SYMPHONY__
00231       osi_Sym       (NULL),
00232 #endif
00233       m_numCols     (0   ),
00234       m_colIndices  (NULL),
00235       m_counter     (0),
00236       m_ws_tag      (0),
00237       m_ws          (NULL)
00238    {};
00239    DecompAlgoModel(DecompConstraintSet* model,
00240                    std::string                modelName,
00241                    int                   blockId) :
00242       DecompAppModel(model, modelName, blockId),
00243       m_osi         (NULL),
00244 #ifdef __DECOMP_IP_SYMPHONY__
00245       osi_Sym       (NULL),
00246 #endif
00247       m_numCols     (0   ),
00248       m_colIndices  (NULL),
00249       m_counter     (0),
00250       m_ws_tag      (0),
00251       m_ws          (NULL)
00252    {};
00253    ~DecompAlgoModel() {
00254       if (m_osi) {
00255          delete    m_osi;
00256       }
00257 
00258       if (m_colIndices) {
00259          delete [] m_colIndices;
00260       }
00261    }
00262 };
00263 
00264 #endif

Generated on 12 Mar 2015 for Dip-All by  doxygen 1.6.1