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

- File List
- File Members

# /home/coin/svn-release/Smi-0.93.1/Smi/src/SmiScnModel.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 SmiScnModel_HPP
    00005 #define SmiScnModel_HPP
    00006
    00007 // Include files
    00008 #include "CoinPragma.hpp"
    00009 #include "SmiDiscreteDistribution.hpp"
    00010 #include "SmiScenarioTree.hpp"
    00011 #include "SmiScnData.hpp"
    00012 #include "OsiSolverInterface.hpp"
    00013 #include "CoinPackedVector.hpp"
    00014 #include "SmiMessage.hpp"
    00015
    00016 // STL declarations
    00017 #include <vector>
    00018
    00019 // forward declaration of SmiScnNode
    00020 class SmiScnNode;
    00021
    00022
    00023 //#############################################################################
    00024
    00047 class SmiScnModel
    00048 {
    00049     friend void SmiScnModelDiscreteUnitTest();
    00050     friend void DecompUnitTest();
    00051 public:
    00052
    00065     int readSmps(const char *name,
    00066         SmiCoreCombineRule *r=NULL);
    00067
    00081     int writeSmps(const char *name, bool winFileExtensions = false, bool strictFormat = true);
    00082
    00083     SmiCoreData * getCore() {return core_;}
    00084
    00095
    00096
    00097
    00099     void processDiscreteDistributionIntoScenarios(SmiDiscreteDistribution *s, bool test=false);
    00100
    00101     void setModelProb(double p) {totalProb_=p; }
    00102
    00103     int addNodeToSubmodel(SmiScnNode * smiScnNode);
    00104
    00122     SmiScenarioIndex generateScenario(SmiCoreData *core,
    00123         CoinPackedMatrix *matrix,
    00124         CoinPackedVector *dclo, CoinPackedVector *dcup,
    00125         CoinPackedVector *dobj,
    00126         CoinPackedVector *drlo, CoinPackedVector *drup,
    00127         SmiStageIndex branch, SmiScenarioIndex anc, double prob,
    00128         SmiCoreCombineRule *r = SmiCoreCombineReplace::Instance());
    00129
    00145     SmiScenarioIndex generateScenario(SmiCoreData *core,
    00146         CoinPackedMatrix *matrix,
    00147         CoinPackedVector *dclo, CoinPackedVector *dcup,
    00148         CoinPackedVector *dobj,
    00149         CoinPackedVector *drlo, CoinPackedVector *drup,
    00150         std::vector<int>labels, double prob,
    00151         SmiCoreCombineRule *r = SmiCoreCombineReplace::Instance());
    00152
    00164     OsiSolverInterface * loadOsiSolverData();
    00165     OsiSolverInterface * loadOsiSolverDataForSubproblem(int stage, int scenStart);
    00166
    00167     std::vector< std::pair<double,double> > solveWS(OsiSolverInterface *osiSolver, double objSense); //Returns value of Wait-And-See solution, with objSense of 1 (= minimization) for default
    00168     std::pair<double,double*> solveEV(OsiSolverInterface *osiSolver, double objSense);
    00169     double solveEEV(OsiSolverInterface *osiSolver, double objSense);
    00170
    00171     double getWSValue(OsiSolverInterface *osiSolver, double objSense);
    00172     double getEVValue(OsiSolverInterface* osiSolver, double objSense);
    00173     double getEEVValue(OsiSolverInterface* osiSolver, double objSense);
    00174
    00175     //Let SmiScnModel own core data
    00176     void setCore(SmiCoreData * val) { core_ = val; }
    00177
    00178     // get scenario problem data
    00179     SmiScenarioIndex getNumScenarios(){ return smiTree_.getNumScenarios();}
    00180     double getScenarioProb(SmiScenarioIndex ns);
    00181     SmiScnNode * getLeafNode(SmiScenarioIndex i){ return smiTree_.getLeaf(i)->getDataPtr(); }
    00182     SmiScnNode * getRootNode(){ return smiTree_.getRoot()->getDataPtr(); }
    00183
    00184     int* getIntegerInd(){ return core_->getIntegerIndices();}
    00185     int getIntegerLen(){return core_->getIntegerLength(); }
    00186     int* getBinaryInd(){return core_->getBinaryIndices();}
    00187     int getBinaryLen(){return core_->getBinaryLength();}
    00188
    00189     inline std::vector<int> getIntIndices() {
    00190         return intIndices;
    00191     }
    00192     inline void addIntIndice(int indice) {
    00193         intIndices.push_back(indice);
    00194     }
    00195
    00196     // getXXX by scenario
    00197     double getObjectiveValue(SmiScenarioIndex ns);
    00198     double *getColSolution(SmiScenarioIndex ns, int *length);
    00199     double *getRowSolution(SmiScenarioIndex ns, int *length);
    00200     double getColSolution(SmiScenarioIndex ns, int stage, int colIndex);
    00201     double getRowSolution(SmiScenarioIndex ns, int stage, int colIndex);
    00202
    00203     // OsiSolverInterface
    00204     void setOsiSolverHandle(OsiSolverInterface &osi)
    00205     {
    00206         osiStoch_ = osi.clone(false);
    00207     }
    00208     void setOsiSolverHandle(OsiSolverInterface *osi)
    00209     {
    00210         osiStoch_ = osi->clone(false);
    00211     }
    00212     OsiSolverInterface * getOsiSolverInterface();
    00213     // If user wants to delete SmiScnModel but keep OsiSolverInterface
    00214     inline void releaseSolver() {osiStoch_=NULL;}
    00215     inline void releaseCore() { core_=NULL; }
    00216
    00217
    00218     // constructor: Lesson from Effective C++: Initialize values in the same order as declared in .hpp file.
    00219     SmiScnModel():
    00220     handler_(NULL),messages_(NULL),osiStoch_(NULL), nrow_(0), ncol_(0), nels_(0),nels_max(0),
    00221         drlo_(NULL), drup_(NULL), dobj_(NULL), dclo_(NULL), dcup_(NULL), matrix_(NULL),
    00222         dels_(NULL),indx_(NULL),rstrt_(NULL),minrow_(0),
    00223         solve_synch_(false),totalProb_(0),core_(NULL),smiTree_(),integerInd(NULL),integerLen(0),binaryInd(NULL),binaryLen(0),intIndices(),maxNelsPerScenInStage(NULL)
    00224     { }
    00225
    00226     // destructor
    00227     ~SmiScnModel();
    00228     //Christian: this method adds data of current node to big det. eq. problem
    00229     void addNode(SmiScnNode *node, bool notDetEq = false);
    00230     void deleteNode(SmiScnNode *tnode);
    00231
    00232     void addNode(SmiNodeData *node);
    00233     inline SmiScenarioTree<SmiScnNode *> * getSmiTree() { return &smiTree_; }
    00234 private:
    00235     CoinMessageHandler *handler_;
    00236     SmiMessage *messages_;
    00237
    00238     // internal clone of user declared OSI
    00239     OsiSolverInterface * osiStoch_;
    00240     // model statistics useful for predefining size of structures
    00241     int nrow_;
    00242     int ncol_;
    00243     int nels_;
    00244     int nels_max;
    00245     // data pointers used in AddNode and loadOsiSolverData aka generateDetEq
    00246     double *drlo_;
    00247     double *drup_;
    00248     double *dobj_;
    00249     double *dclo_;
    00250     double *dcup_;
    00251     CoinPackedMatrix *matrix_;
    00252     double *dels_;
    00253     int    *indx_;
    00254     int    *rstrt_;
    00255     // number of scenarios
    00256     //  int scen_;
    00257     // not sure if this is used
    00258     int minrow_;
    00259     // not sure if this is used
    00260     bool solve_synch_;
    00261     // total probability of added scenarios; used to normalize probability to one
    00262     double totalProb_;
    00263     //core model --- used for discrete distributions
    00264
    00265 private:
    00266     SmiCoreData * core_;
    00267
    00268     // scenario tree
    00269     SmiScenarioTree<SmiScnNode *> smiTree_;
    00270
    00271     int* integerInd;
    00272     int integerLen;
    00273     int* binaryInd;
    00274     int binaryLen;
    00275
    00276     std::vector<int> intIndices;
    00277     int* maxNelsPerScenInStage;
    00278 };
    00279
    00280 class SmiScnNode
    00281 {
    00282     friend class SmiScnModel;
    00283     friend void DecompUnitTest();
    00284 public:
    00285     int getCoreColIndex(int i);
    00286     int getCoreRowIndex(int i);
    00287     inline void setScenarioIndex(SmiScenarioIndex i){ scen_=i;}
    00288     inline SmiScenarioIndex getScenarioIndex() {return scen_;}
    00289     inline int  getColStart() {return coffset_;}
    00290     inline int  getRowStart() {return roffset_;}
    00291     inline int getNumCols(){ return node_->getCore()->getNumCols(node_->getStage());}
    00292     inline int getNumRows(){ return node_->getCore()->getNumRows(node_->getStage());}
    00293     //inline int getNumElements() { return node_->getCore()->getNumElements(node_>getStage());}
    00294     inline double getModelProb(){return mdl_prob_;}
    00295     inline SmiStageIndex getStage() { return node_->getStage(); }
    00296     inline SmiScnNode * getParent(){ return parent_;}
    00297     inline void setProb(double p){prob_=p;}
    00298     inline double addProb(double prob){ return prob_+=prob;}
    00299     inline double getProb() { return prob_; }
    00300     inline void setParent(SmiScnNode * g) {parent_=g;}
    00301
    00302     inline bool isVirtualNode() { return !include_; }
    00303     // So can delete root node
    00304     inline void zapNode() {node_=NULL;}
    00305
    00306
    00307     inline SmiScnNode(SmiNodeData *node)
    00308     {
    00309         node_=node;
    00310         node->addPtr();
    00311         prob_=0;
    00312         condProb_ = 0;
    00313         parent_=NULL;
    00314         scen_=-1;
    00315         include_=true;
    00316     }
    00317     ~SmiScnNode()
    00318     {
    00319         if (node_)
    00320         {
    00321             //Only delete non-core nodes. Core nodes get deleted via SmiCoreData..
    00322             if (!node_->isCoreNode()){
    00323                 delete node_;
    00324                 node_= NULL;
    00325             }
    00326         }
    00327
    00328     }
    00329
    00330 private:
    00331     inline void setRowOffset(int r) {roffset_ = r;}
    00332     inline void setColOffset(int c) {coffset_ = c;}
    00333
    00334     inline double getCondProb(){return condProb_;}
    00335     inline void setCondProb(double p){condProb_=p;}
    00336     inline void setModelProb(double p){mdl_prob_=p;}
    00337     //Necessary to get access to the data node stored within the SmiScnNode
    00338 public:
    00339     inline SmiNodeData *getNode() {return node_;}
    00340 private:
    00341     inline void setIncludeOff() { include_=false; }
    00342     inline void setIncludeOn()  { include_=true; }
    00343     inline bool getInclude() { return include_; }
    00344
    00345
    00346
    00347 private:
    00348     SmiNodeData *node_;
    00349     SmiScnNode *parent_;
    00350     double prob_;
    00351     double mdl_prob_;
    00352     double condProb_;
    00353     int coffset_;
    00354     int roffset_;
    00355     SmiScenarioIndex scen_;
    00356     bool include_;
    00357 };
    00358
    00359 // function object for addnode loop
    00360 class SmiScnModelAddNode{
    00361 public:
    00362     void operator() (SmiScnNode *node)
    00363     {
    00364         s_->addNode(node);
    00365     }
    00366
    00367     SmiScnModelAddNode(SmiScnModel *s) { s_ = s;}
    00368 private:
    00369     SmiScnModel *s_;
    00370
    00371
    00372 };
    00373
    00374 // function object for deleteNode loop
    00375 class SmiScnModelDeleteNode{
    00376 public:
    00377     void operator() (SmiScnNode *node)
    00378     {
    00379         s_->deleteNode(node);
    00380     }
    00381
    00382     SmiScnModelDeleteNode(SmiScnModel *s) { s_ = s;}
    00383 private:
    00384     SmiScnModel *s_;
    00385
    00386
    00387 };
    00388
    00389 #endif //#define SmiScnModel_HPP