- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

# /home/coin/svn-release/Smi-0.93.1/Smi/src/SmiScnData.hpp

Go to the documentation of this file.

    00001 // Copyright (C) 2003, International Business Machines
    00002 // Corporation and others.  All Rights Reserved.
    00003
    00004 #ifndef SmiScnData_HPP
    00005 #define SmiScnData_HPP
    00006
    00007 #include "CoinPragma.hpp"
    00008 #include "OsiSolverInterface.hpp"
    00009 #include "CoinPackedVector.hpp"
    00010 #include "CoinMpsIO.hpp"
    00011 #include "SmiCoreCombineRule.hpp"
    00012 //#include "SmiLinearData.hpp"
    00013
    00014 #include <map>
    00015 #include <vector>
    00016
    00017
    00018 typedef int SmiCoreIndex;
    00019 typedef int SmiScenarioIndex;
    00020 typedef int SmiStageIndex;
    00021
    00022 class SmiCoreData;
    00023
    00024 class SmiNodeData
    00025         //: public SmiLinearData
    00026 {
    00027 public:
    00028         typedef std::map<int,CoinPackedVector *> SmiRowMap;
    00029         typedef std::map<int,double *> SmiDenseRowMap;
    00030         void setCoreNode();
    00031         /*
    00032         CoinPackedVector * getRow(int i) {
    00033                 SmiRowMap::iterator r=rowMap.find(i);
    00034                 if (r!=rowMap.end()) return r->second;
    00035                 else return NULL;}
    00036         */
    00037         double * getDenseRow(int i);
    00038
    00039         inline SmiCoreData * getCore() { return core_;}
    00040         inline int getStage() { return stg_;}
    00041
    00042         inline void setCoreCombineRule(SmiCoreCombineRule *r){combineRule_=r;}
    00043         inline SmiCoreCombineRule * getCoreCombineRule() { return combineRule_;}
    00044
    00045         void copyRowLower(double * drlo);
    00046         void copyRowUpper(double * drup);
    00047         void copyColLower(double * dclo);
    00048         void copyColUpper(double * dcup);
    00049         void copyObjective(double * dobj);
    00050
    00051         const int getNumMatrixElements(){
    00052                 if (this->has_matrix_)
    00053                         return this->strt_[getMatEnd()]-this->strt_[getMatStart()];
    00054                 else
    00055                         return 0;
    00056         }
    00057          const int getRowLength(int irow){
    00058                  if (this->has_matrix_)
    00059                          return getLength(this->getMatStart()+irow-this->rowbeg_);
    00060                  else
    00061                         return 0;
    00062          }
    00063          const int getRowLowerLength()   {return getLength(this->getRloStart());}
    00064          const int getRowUpperLength()   {return getLength(this->getRupStart());}
    00065          const int getColLowerLength()   {return getLength(this->getCloStart());}
    00066          const int getColUpperLength()   {return getLength(this->getCupStart());}
    00067          const int getObjectiveLength()  {return getLength(this->getObjStart());}
    00068      const bool isCoreNode() { return isCoreNode_; }
    00069
    00070          const int *getRowIndices(int irow){
    00071                  if (this->has_matrix_)
    00072                          return getIndices(this->getMatStart()+irow-this->rowbeg_);
    00073                  else
    00074                          return NULL;
    00075          }
    00076          int *getMutableRowIndices(int irow){
    00077                  // using const_cast to stop warnings about cast-away constness
    00078                  return const_cast<int *>(getRowIndices(irow));
    00079          }
    00080          const int *getRowLowerIndices()   {return getIndices(this->getRloStart());}
    00081          const int *getRowUpperIndices()   {return getIndices(this->getRupStart());}
    00082          const int *getColLowerIndices()   {return getIndices(this->getCloStart());}
    00083          const int *getColUpperIndices()   {return getIndices(this->getCupStart());}
    00084          const int *getObjectiveIndices()  {return getIndices(this->getObjStart());}
    00085
    00086          const double *getRowElements(int irow){
    00087                  if (this->has_matrix_)
    00088                          return getElements(this->getMatStart()+irow-this->rowbeg_);
    00089                  else
    00090                          return NULL;
    00091          }
    00092
    00093          const int *getRowStarts(int irow){
    00094                 if (this->has_matrix_)
    00095                         return &strt_[this->getMatStart()+irow-this->rowbeg_];
    00096                 else
    00097                         return NULL;
    00098                  }
    00099          double *getMutableRowElements(int irow){
    00100                  // using const_cast to stop warnings about cast-away constness
    00101                  return const_cast<double *>(getRowElements(irow));
    00102          }
    00103          const double *getRowLowerElements()   {return getElements(this->getRloStart());}
    00104          const double *getRowUpperElements()   {return getElements(this->getRupStart());}
    00105          const double *getColLowerElements()   {return getElements(this->getCloStart());}
    00106          const double *getColUpperElements()   {return getElements(this->getCupStart());}
    00107          const double *getObjectiveElements()  {return getElements(this->getObjStart());}
    00108
    00109
    00110         CoinPackedVector * combineWithCoreRow(CoinPackedVector *cr, CoinPackedVector *nr);
    00111         int combineWithDenseCoreRow(double *dr,CoinPackedVector *cpv,double *dels,int *indx);
    00112         int combineWithDenseCoreRow(double *dr,const int nels,const int *inds, const double *dels, double *dest_dels,int *dest_indx);
    00113
    00114         SmiNodeData(SmiStageIndex stg, SmiCoreData *core,
    00115                                  const CoinPackedMatrix *const matrix,
    00116                                  CoinPackedVector *dclo,
    00117                                  CoinPackedVector *dcup,
    00118                                  CoinPackedVector *dobj,
    00119                                  CoinPackedVector *drlo,
    00120                                  CoinPackedVector *drup);
    00121
    00122         int addPtr() { return ++ptr_count; }
    00123
    00124         ~SmiNodeData();
    00125
    00126 protected:
    00127         void combineWithCoreDoubleArray(double *d_out, const CoinPackedVector &cpv, int o);
    00128         void combineWithCoreDoubleArray(double *d_out, const int len, const int * inds, const double *dels, int o);
    00129         //Memory assignment/deletion
    00130         void assignMemory();
    00131         void deleteMemory();
    00132
    00133         //Covers for starts of arrays
    00134          const int getMatStart() { return mat_strt_;}
    00135          const int getMatEnd()   { return mat_strt_+nrow_;}
    00136          const int getRloStart() { return rlo_strt_;}
    00137          const int getRupStart() { return rup_strt_;}
    00138          const int getCloStart() { return clo_strt_;}
    00139          const int getCupStart() { return cup_strt_;}
    00140          const int getObjStart() { return obj_strt_;}
    00141
    00142         //Access to stored memory
    00143          //Return number of Elements in row/bounds/ranges/objective istart
    00144          const int getLength  (int istart){
    00145                 return this->strt_[istart+1] - this->strt_[istart];
    00146         }
    00147          //Return index array that starts at row/bounds/ranges/objective istart
    00148          const int * getIndices (int istart){
    00149                 return this->inds_+this->strt_[istart];
    00150         }
    00151          //Return double array that starts at row/bounds/ranges/objective istart
    00152          const double *getElements(int istart){
    00153                 return this->dels_+this->strt_[istart];
    00154         }
    00155
    00156
    00157 private:
    00158
    00159         SmiStageIndex stg_; //Stage to which the node belongs
    00160         SmiRowMap rowMap; //not used
    00161         SmiDenseRowMap dRowMap; //Map of <int,double*>, stores row entries in dense format
    00162
    00163         SmiCoreData *core_; //Link to core data
    00164         bool isCoreNode_; //Is this node a core node? If so, it should, at least in my opinion, not turn up in the Tree
    00165         SmiCoreCombineRule *combineRule_;
    00166
    00167         int numarrays_; //= number of arrays used = 5 (clo,cup,rlo,rup,obj)
    00168         int nels_; //number of elements in the matrix and the other arrays
    00169         int nrow_; //number of rows (of corresponding stage in core model)
    00170         int ncol_; //number of columns (of corresponding stage in core model)
    00171         int rowbeg_; //index, where rows of corresponding stage begins in the core model
    00172         int colbeg_; //index, where columns begin wrt to the columns of the corresponding stage in the core model
    00173         int nstrt_; //number of strt indices = numarray + nrow+1 (see assignMemory())
    00174         bool has_matrix_; //If this node has a matrix entry
    00175         int mat_strt_; //start arrays for corresponding entries
    00176         int clo_strt_;
    00177         int cup_strt_;
    00178         int obj_strt_;
    00179         int rlo_strt_;
    00180         int rup_strt_;
    00181         double * dels_; //concrete elements
    00182         int    * inds_; //indices for the elements
    00183         int    * strt_; //start array, where new row begins
    00184
    00185         int ptr_count; //Propably for memory management?!
    00186 };
    00187
    00188
    00189 class SmiCoreData
    00190 {
    00191 public:
    00192         inline int getNumCols(){ return ncol_;} //Gesamtzahl Variablen f�r das CoreModell
    00193         inline int getNumRows(){ return nrow_;} //Gesamtzahl Restriktionen f�r das CoreModell
    00194         inline int getNumElements(){ return nz_;} // Gesamtzahl Elemente f�r das CoreModell
    00195         inline int getNumStages(){ return nstag_;} //Anzahl an Stufen
    00196         inline int getNumCols(SmiStageIndex t){ return nColInStage_[t];} //Anzahl Variablen in Stufe t
    00197         inline int getNumRows(SmiStageIndex t){ return nRowInStage_[t];} //Number of Restrictions in stage t
    00198         inline int getColStart(SmiStageIndex t){ return stageColPtr_[t];} //Index where Variables of stage t starts
    00199         inline int getRowStart(SmiStageIndex t){ return stageRowPtr_[t];} //Index where Rows of stage t starts
    00200         inline int getColStage(int i){ return colStage_[i];} //Stage to Variable i
    00201         inline int getRowStage(int i){ return rowStage_[i];} //Stage to Row i
    00202         //Christian: TODO: In my Opinion these methods do not belong here, as it takes care of converting indices from/to det. eq. to core model formulation
    00203         inline int getRowInternalIndex(int i){ return rowEx2In_[i];} //Get Internal Index (in case of det. eq.) from external index (core model)
    00204         inline int getColInternalIndex(int i){ return colEx2In_[i];}
    00205         inline int getRowExternalIndex(int i){ return rowIn2Ex_[i];} //Get external index (core model) from internal index (det. eq. model)
    00206         inline int getColExternalIndex(int i){ return colIn2Ex_[i];}
    00207         /*
    00208         inline CoinPackedVector * getMatrixRow(SmiStageIndex t, int i){ return nodes_[t]->getRow(i);}
    00209         */
    00210         inline const double * getDenseRowLower(SmiStageIndex t){return cdrlo_[t];}
    00211         inline const double * getDenseRowUpper(SmiStageIndex t){return cdrup_[t];}
    00212         inline const double * getDenseColLower(SmiStageIndex t){return cdclo_[t];}
    00213         inline const double * getDenseColUpper(SmiStageIndex t){return cdcup_[t];}
    00214         inline const double * getDenseObjCoefficients(SmiStageIndex t){return cdobj_[t];}
    00215
    00216     inline std::vector<int> getIntCols(int stage) { return intColsStagewise[stage]; };
    00217         inline int* getIntegerIndices() { return integerIndices_; } //indices of integer variables
    00218         inline int getIntegerLength() { return integerLength_; }
    00219     inline int* getBinaryIndices() { return binaryIndices_; } //indices of binary variables
    00220     inline int getBinaryLength() { return binaryLength_; }
    00221     inline double getInfinity() { return infinity_; }
    00222
    00223         void copyRowLower(double * drlo,SmiStageIndex t );
    00224         void copyRowUpper(double * drup,SmiStageIndex t);
    00225         void copyColLower(double * dclo,SmiStageIndex t);
    00226         void copyColUpper(double * dcup,SmiStageIndex t);
    00227         void copyObjective(double * dobj,SmiStageIndex t);
    00228
    00229         inline SmiNodeData * getNode(SmiStageIndex t){return nodes_[t];}
    00230
    00231         inline void setColumnNames(char** namesStrict, char** namesFree) { this->colNamesStrict = namesStrict; this->colNamesFree = namesFree; }
    00232         inline char** getColumnNames(bool strictFormat = true) { if (strictFormat) return this->colNamesStrict; else return this->colNamesFree; }
    00233
    00234     SmiCoreData(CoinMpsIO *cMps, int nstag, int *cstag, int *rstag,int *integerIndices = 0, int integerLength = 0, int *binaryIndices = 0, int binaryLength = 0);
    00235     SmiCoreData(OsiSolverInterface *osi, int nstag, int *cstag, int *rstag,int *integerIndices = 0, int integerLength = 0, int *binaryIndices = 0, int binaryLength = 0);
    00236
    00237         ~SmiCoreData();
    00238
    00239 private:
    00240
    00241         //Christian: Creates SmiCoreData for whole tree from deterministic core model and information about stages. Splits
    00242         // rows and columns into nstag stages, given by cstag and rstag. Creates a core node for every stage.
    00243     void gutsOfConstructor(int nrow,int ncol,int nstag, int *cstag,int *rstag, CoinPackedMatrix *matrix, CoinPackedVector *dclo, CoinPackedVector *dcup, CoinPackedVector *dobj, CoinPackedVector *drlo, CoinPackedVector *drup, int* integerIndices = 0,int integerLength = 0,int* binaryIndices = 0,int binaryLength = 0);
    00244
    00245     OsiSolverInterface* generateCoreProblem(OsiSolverInterface* osi);
    00246
    00247
    00248 private:
    00249         int nrow_;
    00250         int ncol_;
    00251     int nz_; // We can count the total number of elements, but I do not if we need this at one point
    00252         SmiStageIndex nstag_; //Total number of stages in the problem, apart from the first stage. A two-stage problem has nstag_ = 1 and so on. If the problem is deterministic, nstag_ = 0.
    00253         int *nColInStage_; //Number of Columns in Stage
    00254         int *nRowInStage_; //Number of Rows in Stage
    00255         int *stageColPtr_; //Start Index of Columns in Stage, with respect to ncol_
    00256         int *stageRowPtr_; //Start Index of Rows in Stage, with respect to nrow_
    00257         int *colStage_; //To which stage belongs the column?
    00258         int *rowStage_; //To which stage belongs the row?
    00259         int *colEx2In_; //Not clear yet
    00260         int *rowEx2In_; //Not clear yet
    00261         int *colIn2Ex_; // Not clear yet
    00262         int *rowIn2Ex_; // Not clear yet
    00263     int *integerIndices_; //indices of integer variables
    00264     int integerLength_; //number of integer variables
    00265     int *binaryIndices_; //indices of binary variables
    00266     int binaryLength_; //number of binary variables
    00267     double infinity_; //Infinity of solver
    00268         double **cdrlo_;
    00269         double **cdrup_;
    00270         double **cdobj_;
    00271         double **cdclo_;
    00272         double **cdcup_;
    00273         std::vector<SmiNodeData*> nodes_; //Nodes, that contain stage dependent constraints (with Bounds,Ranges,Objective,Matrix), so called CoreNodes
    00274         std::vector<double *> pDenseRow_; //dense probability vector
    00275         std::vector< std::vector<int> > intColsStagewise; // For each stage separately, it contains the position of every integer column
    00276         char **colNamesStrict;
    00277         char **colNamesFree;
    00278 };
    00279
    00280 #endif //#define SmiScnData_HPP