Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members

AgentProblem.h

Go to the documentation of this file.
00001 /*Copyright (C) 2006/2007, Professorship (p.p.) for Land Use Economics in the Tropics and
00002 Subtropics, Institute for Agricultural Economics and Social Sciences in the Tropics
00003 and Subtropics, University of Hohenheim, Stuttgart
00004 Thomas Berger, Lutz Göhring
00005 All rights reserved
00006 */
00007 #ifndef LP_OLD
00008 #ifndef AGENT_PROBLEM_H_
00009 #define AGENT_PROBLEM_H_
00010 
00011 #include <typeinfo>
00012 
00013 using namespace std;
00014 
00015 #include "CoinPackedMatrix.hpp"
00016 #include "tableau.h"
00017 
00018 
00020 
00029 class AgentProblem{
00030  
00031 public:
00032 
00033 //---------------------------------------------------------------------------
00034    enum keepCachedFlag {
00036       KEEPCACHED_NONE    = 0,
00038       KEEPCACHED_COLUMN  = 1,
00040       KEEPCACHED_ROW     = 2,
00042       KEEPCACHED_MATRIX  = 4,
00044       KEEPCACHED_SOS     = 5,
00046       KEEPCACHED_RESULTS = 8,
00048       KEEPCACHED_PROBLEM = KEEPCACHED_COLUMN | KEEPCACHED_ROW | KEEPCACHED_MATRIX | KEEPCACHED_SOS,
00050       KEEPCACHED_ALL     = KEEPCACHED_PROBLEM | KEEPCACHED_RESULTS,
00052       FREECACHED_COLUMN  = KEEPCACHED_PROBLEM & !KEEPCACHED_COLUMN & !KEEPCACHED_SOS,
00054       FREECACHED_ROW     = KEEPCACHED_PROBLEM & !KEEPCACHED_ROW,
00056       FREECACHED_MATRIX  = KEEPCACHED_PROBLEM & !KEEPCACHED_MATRIX,
00058       FREECACHED_RESULTS = KEEPCACHED_ALL & !KEEPCACHED_RESULTS
00059    };
00060     //---------------------------------------------------------------------------
00071 
00072         int getNumCols()const;
00074    int getNumRows()const;
00076    int getNumElements() const;
00078    const double * getColLower()const;
00080    const double * getColUpper() const;
00090    const char * getRowSense()const;
00103    const double * getRightHandSide()const;
00112    const double * getRowRange()const;
00114    const double * getRowLower()const;
00116    const double * getRowUpper()const;
00118    const double * getObjCoefficients()const;
00122    const double * getObj2Coefficients()const;
00124    double getObjSense()const;
00126         double getMatCoeff(int i, int j);
00128    int getNumberOfSpeciallyOrderedSets()const;
00130    SpeciallyOrderedSet* getSpeciallyOrderedSet(int index)const;
00132         int getNumberOfIntegerSets()const;
00134    SpeciallyOrderedSet* getIntegerSet(int index)const;
00136    bool isContinuous(int colIndex)const;
00138    bool isBinary(int colIndex)const {;};
00143    bool isInteger(int colIndex)const;
00145    bool isIntegerNonBinary(int colIndex)const {;};
00147    bool isFreeBinary(int colIndex) const {;};
00149    const CoinPackedMatrix * getMatrixByRow() const;
00151    const CoinPackedMatrix * getMatrixByCol() const;
00153    double getInfinity() const;  
00155    double getTolerance() const;
00157          int getNumberOfDecimals() const;
00159         bool getTestFlag()const;
00161         int getNumberOfRowsToDrop()const;
00163         int* getRowsToDrop()const;
00165         int getNumberOfColsToDrop()const;
00167         int* getColsToDrop()const;
00169         int getActivityType(int index) const;
00171         const CoinPackedMatrix * getActivityType()const;
00173 
00175 
00176    CoinPackedMatrix getInfoBlock()const;
00178     //-------------------------------------------------------------------------
00180 
00181 
00182    virtual void setObjCoeff(int elementIndex, double elementValue);
00184    virtual void setObj2Coeff(int elementIndex, double elementValue );
00187    virtual void setColLower(int elementIndex, double elementValue );
00190    virtual void setColUpper(int elementIndex, double elementValue );
00193    virtual void setRowLower(int elementIndex, double elementValue )const;
00196    virtual void setRowUpper(int elementIndex, double elementValue )const;
00198    virtual void setRowType(int index, char sense, double rightHandSide,
00199                               double range)const;
00201         virtual void setRightHandSide(int index,double elementValue)const;            
00204    virtual void setObjSense(double s);
00206    virtual void setNumberOfSpeciallyOrderedSets(int numberofsos_);
00208    virtual void setNumberOfIntegerSets(int numberofintegersets_);
00210    virtual void setMatCoeff(int i, int j, double coeff);
00212    virtual int setActivityType(int index, double value); 
00214 
00215  //-------------------------------------------------------------------------
00219    virtual void setContinuous(int index);
00220 
00222    virtual void setInteger(int index);
00223 
00224    virtual void setColName(char **colname){;};
00225 
00227     
00228     //-------------------------------------------------------------------------
00231     virtual void setInfinity(double infinity);
00233     
00237     virtual void setTolerance(double eps_);
00239     
00243     virtual void setNumberOfDecimals(int numberofdecimals_);
00245     virtual void setTestFlag(bool test);
00247  //-------------------------------------------------------------------------
00250 
00251     virtual void setSpeciallyOrderedSets(int numberOfSos, SpeciallyOrderedSet* speciallyOrdSets);
00253     virtual void setIntegerSets(int numberOfIntegerSets, SpeciallyOrderedSet* integerSets);
00255 
00257 
00258     virtual void setInfoBlock(const CoinPackedMatrix infoBlock_);
00260     
00261     //-------------------------------------------------------------------------
00262  
00277     virtual void loadProblem(const CoinPackedMatrix& MatrixDouble,
00278                              const double* collb, const double* colub,
00279                              const double* obj,
00280                              const char* rowsen, const double* rowrhs,
00281                              const double* rowrng);
00283 
00285 
00286     
00294     virtual void findEmptyRowsAndCols();
00296   //------------------------------------------------------------------------------------------------------------
00298 
00299 
00300 
00304     int read(FILE* datStream,int& istransposed);
00306 
00310   int write(FILE* file, int istransposed)const;
00312   virtual void output(string name);
00314   //-------------------------------------------------------------------------------------------------------------
00316 
00317 
00318   AgentProblem();
00320   AgentProblem(const AgentProblem& problem);
00321   //Assignment operator
00322   AgentProblem& operator=(const AgentProblem& rhs);
00324   ~AgentProblem();
00326   //-------------------------------------------------------------------------------------------------------------
00327   
00328   protected:
00333     inline void
00334     convertBoundToSense(const double lower, const double upper,
00335                         char& sense, double& right, double& range) const;
00338     inline void
00339     convertSenseToBound(const char sense, const double right,
00340                         const double range,
00341                         double& lower, double& upper) const;
00343   
00344   //---------------------------------------------------------------------------------------------------------------
00345    private:
00348 
00349    double  *obj;
00351    double  *obj2;
00353    double  *collower;
00355    double  *colupper;
00357    char    *rowsense;
00359    double  *rowrhs;
00363    double  *rowrange;
00365    double  *rowlower;
00367    double  *rowupper;
00369    int * isint;
00371 
00376    CoinPackedMatrix *matrixByRow;
00378    CoinPackedMatrix *matrixByCol;
00380    double objsense;
00382    int numberofsos;
00384    SpeciallyOrderedSet** speciallyOrderedSets;
00386    int numberofintegersets;
00388    SpeciallyOrderedSet** integerSets;
00390    CoinPackedMatrix infoBlock;
00392         CoinPackedMatrix* activityType;       
00394    double infinity;
00396    double eps;
00398    int numberofdecimals;
00400    bool test;
00402         int numberofrowstodrop;
00404         int* rowsToDrop;
00406         int numberofcolstodrop;
00408         int* colsToDrop;
00409         
00410         //{@name Private methods
00412 
00417         void round();
00419         void round(double* vector,int vectorlength);
00421         void round(CoinPackedMatrix *MatrixDouble);
00422         //truncates respectively rounds a double x to precision eps or if absolute value is smaller than eps sets it to zero
00423         
00427         double round(double value);
00429         
00436         double truncate(double precision,double& x);
00438         double round(int numberofdecimals_, double& x);
00440         int allocateVectors(const int numcols, const int numrows);
00441 
00443   template <class Type> int allocateVector(Type*& pointertovector, long int sizeofvector);
00449 
00450   template <class ClassType> void copy(ClassType*& finalObjectPtr_, const ClassType* originalObject);
00452   void freeCachedColRim();
00454   void freeCachedRowRim();
00456   void freeCachedResults();
00458   void freeCachedMatrix();
00460   void freeCachedSpeciallyOrdSets();
00462   void freeCachedData( int keepCached = KEEPCACHED_NONE );
00464   void gutsOfConstructor();
00466   void gutsOfDestructor();
00468   void freeAllMemory();
00470 };
00471 
00472 template <class Type>
00473 int AgentProblem::allocateVector(Type*& pointertovector, long int sizeofvector){
00474         if (sizeofvector > 0)
00475                 if(pointertovector== NULL){
00476                         try{
00477                                 pointertovector = new Type[sizeofvector];
00478                                 for (int i = 0; i<sizeofvector;i++) pointertovector[i] = NULL;
00479                         }
00480                         catch(exception& exceptionObject){
00481                                 cerr << "Exception: "<< exceptionObject.what()<<endl;
00482                                 cerr << "Error in "<< typeid(*this).name()<<"::allocateVector(): cannot allocate memory for Type"<< typeid(*pointertovector).name()<<"["<<sizeofvector<<"]" << endl;
00483                                 exit(0);
00484                         }
00485                 }
00486                 else {
00487                         delete[] pointertovector;
00488                         pointertovector = NULL;
00489                         try{
00490                                 pointertovector = new Type[sizeofvector];
00491                                 for (int i = 0; i<sizeofvector;i++) pointertovector[i] = NULL;
00492                         }
00493                         catch(exception& exceptionObject){
00494                                 cerr << "Exception: "<< exceptionObject.what()<<endl;
00495                                 cerr << "Error in "<< typeid(*this).name()<<"::allocateVector(): cannot allocate memory for Type"<< typeid(*pointertovector).name()<<"["<<sizeofvector<<"]" << endl;
00496                                 exit(0);
00497                         }
00498                 }
00499         else {
00500                 if (test) cerr  << "Warning in "<< typeid(*this).name()<<"::allocateVector(): nothing to allocate:"
00501                                                 << "length of array of "<<typeid(*pointertovector).name()<<" is: "<<sizeofvector<<"!" << endl;
00502                 return 0;//failure/warning
00503         }
00504         return 1;//success                              
00505 }
00506 
00507  //Allocates memory for class member finalObjectPtr_ if necessary and copies data from the object originalObjectPtr_ points to
00508 template <class ClassType> void AgentProblem::copy(ClassType*& finalObjectPtr_, const ClassType* originalObject){
00509         if (originalObject != NULL){
00510                 if (finalObjectPtr_ == NULL)
00511                         try {
00512                                 finalObjectPtr_ = new ClassType(*originalObject);
00513                         }
00514                         catch (exception& exceptionObject){
00515                                 cerr << "Exception: "<< exceptionObject.what() << endl;
00516                                 cerr << "Error in "<< typeid(*this).name()<<"::copy(): cannot allocate memory for "<< typeid(ClassType).name()  << endl;
00517                         }
00518                 else {
00519                         delete finalObjectPtr_;
00520                         finalObjectPtr_ = NULL;
00521                         try {
00522                                 finalObjectPtr_ = new ClassType(*originalObject);
00523                         }
00524                         catch (exception& exceptionObject){
00525                                 cerr << "Exception: "<< exceptionObject.what() << endl;
00526                                 cerr << "Error in "<< typeid(*this).name()<<"::copy(): cannot allocate memory for "<< typeid(ClassType).name() << endl;
00527                         }
00528                 }
00529         }
00530         else if (test) cerr << "Warning in "<< typeid(*this).name() << "::copy(): "<<typeid(originalObject).name() <<" == NULL!" << endl;
00531 }
00532 //#############################################################################
00533 //The two following functions are taken from OsiSolverInterface
00536 inline void
00537 AgentProblem::convertBoundToSense(const double lower, const double upper,
00538                                         char& sense, double& right,
00539                                         double& range) const
00540 {
00541   double inf = getInfinity();
00542   range = 0.0;
00543   if (lower > -inf) {
00544     if (upper < inf) {
00545       right = upper;
00546       if (upper==lower) {
00547         sense = 'E';
00548       } else {
00549         sense = 'R';
00550         range = upper - lower;
00551       }
00552     } else {
00553       sense = 'G';
00554       right = lower;
00555     }
00556   } else {
00557     if (upper < inf) {
00558       sense = 'L';
00559       right = upper;
00560     } else {
00561       sense = 'N';
00562       right = 0.0;
00563     }
00564   }
00565 }
00566 
00567 //-----------------------------------------------------------------------------
00570 inline void
00571 AgentProblem::convertSenseToBound(const char sense, const double right,
00572                                         const double range,
00573                                         double& lower, double& upper) const
00574 {
00575   double inf=getInfinity();
00576   switch (sense) {
00577   case 'E':
00578     lower = upper = right;
00579     break;
00580   case 'L':
00581     lower = -inf;
00582     upper = right;
00583     break;
00584   case 'G':
00585     lower = right;
00586     upper = inf;
00587     break;
00588   case 'R':
00589     lower = right - range;
00590     upper = right;
00591     break;
00592   case 'N':
00593     lower = -inf;
00594     upper = inf;
00595     break;
00596   }
00597 }
00598 
00599  
00600 #endif /*AGENT_PROBLEM_H_*/
00601 #endif /* LP_OLD*/
00602 
00603 

Generated on Thu Aug 28 12:38:44 2008 for MPMAS by  doxygen 1.3.9.1