00001
00002
00003 #ifndef ClpModel_H
00004 #define ClpModel_H
00005
00006 #include "ClpConfig.h"
00007
00008 #include <iostream>
00009 #include <cassert>
00010 #include <cmath>
00011 #include <vector>
00012 #include <string>
00013
00014
00015
00016 #include "ClpPackedMatrix.hpp"
00017 #include "CoinMessageHandler.hpp"
00018 #include "CoinHelperFunctions.hpp"
00019 #include "ClpParameters.hpp"
00020 #include "ClpObjective.hpp"
00021 class ClpEventHandler;
00022
00023
00024 #ifndef COIN_DBL_MAX
00025 #define COIN_DBL_MAX DBL_MAX
00026 #endif
00027
00037 class CoinBuild;
00038 class CoinModel;
00039 class ClpModel {
00040
00041 public:
00042
00048
00049 ClpModel (bool emptyMessages=false );
00050
00055 ClpModel(const ClpModel & rhs, int scalingMode=-1);
00057 ClpModel & operator=(const ClpModel & rhs);
00062 ClpModel (const ClpModel * wholeModel,
00063 int numberRows, const int * whichRows,
00064 int numberColumns, const int * whichColumns,
00065 bool dropNames=true, bool dropIntegers=true);
00067 ~ClpModel ( );
00069
00083 void loadProblem ( const ClpMatrixBase& matrix,
00084 const double* collb, const double* colub,
00085 const double* obj,
00086 const double* rowlb, const double* rowub,
00087 const double * rowObjective=NULL);
00088 void loadProblem ( const CoinPackedMatrix& matrix,
00089 const double* collb, const double* colub,
00090 const double* obj,
00091 const double* rowlb, const double* rowub,
00092 const double * rowObjective=NULL);
00093
00096 void loadProblem ( const int numcols, const int numrows,
00097 const CoinBigIndex* start, const int* index,
00098 const double* value,
00099 const double* collb, const double* colub,
00100 const double* obj,
00101 const double* rowlb, const double* rowub,
00102 const double * rowObjective=NULL);
00108 int loadProblem ( CoinModel & modelObject,bool tryPlusMinusOne=false);
00110 void loadProblem ( const int numcols, const int numrows,
00111 const CoinBigIndex* start, const int* index,
00112 const double* value,const int * length,
00113 const double* collb, const double* colub,
00114 const double* obj,
00115 const double* rowlb, const double* rowub,
00116 const double * rowObjective=NULL);
00118 void loadQuadraticObjective(const int numberColumns,
00119 const CoinBigIndex * start,
00120 const int * column, const double * element);
00121 void loadQuadraticObjective ( const CoinPackedMatrix& matrix);
00123 void deleteQuadraticObjective();
00125 void setRowObjective(const double * rowObjective);
00127 int readMps(const char *filename,
00128 bool keepNames=false,
00129 bool ignoreErrors = false);
00131 int readGMPL(const char *filename,const char * dataName,
00132 bool keepNames=false);
00134 void copyInIntegerInformation(const char * information);
00136 void deleteIntegerInformation();
00138 void setContinuous(int index);
00140 void setInteger(int index);
00142 bool isInteger(int index) const;
00144 void resize (int newNumberRows, int newNumberColumns);
00146 void deleteRows(int number, const int * which);
00148 void addRow(int numberInRow, const int * columns,
00149 const double * elements, double rowLower=-COIN_DBL_MAX,
00150 double rowUpper=COIN_DBL_MAX);
00152 void addRows(int number, const double * rowLower,
00153 const double * rowUpper,
00154 const CoinBigIndex * rowStarts, const int * columns,
00155 const double * elements);
00157 void addRows(int number, const double * rowLower,
00158 const double * rowUpper,
00159 const CoinBigIndex * rowStarts, const int * rowLengths,
00160 const int * columns,
00161 const double * elements);
00162 #ifndef CLP_NO_VECTOR
00163 void addRows(int number, const double * rowLower,
00164 const double * rowUpper,
00165 const CoinPackedVectorBase * const * rows);
00166 #endif
00167
00172 int addRows(const CoinBuild & buildObject,bool tryPlusMinusOne=false,
00173 bool checkDuplicates=true);
00182 int addRows(CoinModel & modelObject,bool tryPlusMinusOne=false,
00183 bool checkDuplicates=true);
00184
00186 void deleteColumns(int number, const int * which);
00188 void addColumn(int numberInColumn,
00189 const int * rows,
00190 const double * elements,
00191 double columnLower=0.0,
00192 double columnUpper=COIN_DBL_MAX,
00193 double objective=0.0);
00195 void addColumns(int number, const double * columnLower,
00196 const double * columnUpper,
00197 const double * objective,
00198 const CoinBigIndex * columnStarts, const int * rows,
00199 const double * elements);
00200 void addColumns(int number, const double * columnLower,
00201 const double * columnUpper,
00202 const double * objective,
00203 const CoinBigIndex * columnStarts, const int * columnLengths,
00204 const int * rows,
00205 const double * elements);
00206 #ifndef CLP_NO_VECTOR
00207 void addColumns(int number, const double * columnLower,
00208 const double * columnUpper,
00209 const double * objective,
00210 const CoinPackedVectorBase * const * columns);
00211 #endif
00212
00217 int addColumns(const CoinBuild & buildObject,bool tryPlusMinusOne=false,
00218 bool checkDuplicates=true);
00226 int addColumns(CoinModel & modelObject,bool tryPlusMinusOne=false,
00227 bool checkDuplicates=true);
00229 inline void modifyCoefficient(int row, int column, double newElement,
00230 bool keepZero=false)
00231 {matrix_->modifyCoefficient(row,column,newElement,keepZero);}
00233 void chgRowLower(const double * rowLower);
00235 void chgRowUpper(const double * rowUpper);
00237 void chgColumnLower(const double * columnLower);
00239 void chgColumnUpper(const double * columnUpper);
00241 void chgObjCoefficients(const double * objIn);
00245 void borrowModel(ClpModel & otherModel);
00248 void returnModel(ClpModel & otherModel);
00249
00251 void createEmptyMatrix();
00259 int cleanMatrix(double threshold=1.0e-20);
00261 void copy(const ClpMatrixBase * from, ClpMatrixBase * & to);
00262 #ifndef CLP_NO_STD
00264 void dropNames();
00266 void copyNames(std::vector<std::string> & rowNames,
00267 std::vector<std::string> & columnNames);
00269 void copyRowNames(const std::vector<std::string> & rowNames,int first, int last);
00271 void copyColumnNames(const std::vector<std::string> & columnNames, int first, int last);
00273 void copyRowNames(const char * const * rowNames,int first, int last);
00275 void copyColumnNames(const char * const * columnNames, int first, int last);
00277 void setRowName(int rowIndex, std::string & name) ;
00279 void setColumnName(int colIndex, std::string & name) ;
00280 #endif
00281
00288 int findNetwork(char * rotate, double fractionNeeded=0.75);
00291 CoinModel * createCoinModel() const;
00292
00305 int writeMps(const char *filename,
00306 int formatType=0,int numberAcross=2,
00307 double objSense=0.0) const ;
00309
00311
00312 inline int numberRows() const {
00313 return numberRows_;
00314 }
00315 inline int getNumRows() const {
00316 return numberRows_;
00317 }
00319 inline int getNumCols() const {
00320 return numberColumns_;
00321 }
00322 inline int numberColumns() const {
00323 return numberColumns_;
00324 }
00326 inline double primalTolerance() const {
00327 return dblParam_[ClpPrimalTolerance];
00328 }
00329 void setPrimalTolerance( double value) ;
00331 inline double dualTolerance() const { return dblParam_[ClpDualTolerance]; }
00332 void setDualTolerance( double value) ;
00334 inline double primalObjectiveLimit() const { return dblParam_[ClpPrimalObjectiveLimit];}
00335 void setPrimalObjectiveLimit(double value);
00337 inline double dualObjectiveLimit() const { return dblParam_[ClpDualObjectiveLimit];}
00338 void setDualObjectiveLimit(double value);
00340 inline double objectiveOffset() const { return dblParam_[ClpObjOffset];}
00341 void setObjectiveOffset(double value);
00343 inline double presolveTolerance() const
00344 { return dblParam_[ClpPresolveTolerance];}
00345 #ifndef CLP_NO_STD
00346 inline std::string problemName() const { return strParam_[ClpProbName]; }
00347 #endif
00349 inline int numberIterations() const { return numberIterations_; }
00350 inline int getIterationCount() const { return numberIterations_; }
00351 inline void setNumberIterations(int numberIterations)
00352 { numberIterations_ = numberIterations;}
00354 inline int solveType() const
00355 { return solveType_;}
00356 inline void setSolveType(int type)
00357 { solveType_=type;}
00359 inline int maximumIterations() const { return intParam_[ClpMaxNumIteration]; }
00360 void setMaximumIterations(int value);
00362 inline double maximumSeconds() const { return dblParam_[ClpMaxSeconds]; }
00363 void setMaximumSeconds(double value);
00365 bool hitMaximumIterations() const;
00375 inline int status() const { return problemStatus_; }
00376 inline int problemStatus() const { return problemStatus_; }
00378 inline void setProblemStatus(int problemStatus)
00379 { problemStatus_ = problemStatus;}
00394 inline int secondaryStatus() const { return secondaryStatus_; }
00395 inline void setSecondaryStatus(int status)
00396 { secondaryStatus_ = status;}
00398 inline bool isAbandoned() const { return problemStatus_==4; }
00400 inline bool isProvenOptimal() const { return problemStatus_==0; }
00402 inline bool isProvenPrimalInfeasible() const { return problemStatus_==1; }
00404 inline bool isProvenDualInfeasible() const { return problemStatus_==2; }
00406 bool isPrimalObjectiveLimitReached() const ;
00408 bool isDualObjectiveLimitReached() const ;
00410 inline bool isIterationLimitReached() const { return problemStatus_==3; }
00412 inline double optimizationDirection() const {
00413 return optimizationDirection_;
00414 }
00415 inline double getObjSense() const { return optimizationDirection_; }
00416 void setOptimizationDirection(double value);
00418 inline double * primalRowSolution() const { return rowActivity_; }
00419 inline const double * getRowActivity() const { return rowActivity_; }
00421 inline double * primalColumnSolution() const { return columnActivity_; }
00422 inline const double * getColSolution() const { return columnActivity_; }
00423 inline void setColSolution(const double * input)
00424 { memcpy(columnActivity_,input,numberColumns_*sizeof(double));}
00426 inline double * dualRowSolution() const { return dual_; }
00427 inline const double * getRowPrice() const { return dual_; }
00429 inline double * dualColumnSolution() const { return reducedCost_; }
00430 inline const double * getReducedCost() const { return reducedCost_; }
00432 inline double* rowLower() const { return rowLower_; }
00433 inline const double* getRowLower() const { return rowLower_; }
00435 inline double* rowUpper() const { return rowUpper_; }
00436 inline const double* getRowUpper() const { return rowUpper_; }
00437
00441 void setObjectiveCoefficient( int elementIndex, double elementValue );
00443 inline void setObjCoeff( int elementIndex, double elementValue )
00444 { setObjectiveCoefficient( elementIndex, elementValue);}
00445
00448 void setColumnLower( int elementIndex, double elementValue );
00449
00452 void setColumnUpper( int elementIndex, double elementValue );
00453
00455 void setColumnBounds( int elementIndex,
00456 double lower, double upper );
00457
00466 void setColumnSetBounds(const int* indexFirst,
00467 const int* indexLast,
00468 const double* boundList);
00469
00472 inline void setColLower( int elementIndex, double elementValue )
00473 { setColumnLower(elementIndex, elementValue);}
00476 inline void setColUpper( int elementIndex, double elementValue )
00477 { setColumnUpper(elementIndex, elementValue);}
00478
00480 inline void setColBounds( int elementIndex,
00481 double lower, double upper )
00482 { setColumnBounds(elementIndex, lower, upper);}
00483
00490 inline void setColSetBounds(const int* indexFirst,
00491 const int* indexLast,
00492 const double* boundList)
00493 { setColumnSetBounds(indexFirst, indexLast, boundList);}
00494
00497 void setRowLower( int elementIndex, double elementValue );
00498
00501 void setRowUpper( int elementIndex, double elementValue ) ;
00502
00504 void setRowBounds( int elementIndex,
00505 double lower, double upper ) ;
00506
00513 void setRowSetBounds(const int* indexFirst,
00514 const int* indexLast,
00515 const double* boundList);
00516
00518
00519 inline const double * rowScale() const {return rowScale_;}
00520 inline const double * columnScale() const {return columnScale_;}
00521 inline double * mutableRowScale() const {return rowScale_;}
00522 inline double * mutableColumnScale() const {return columnScale_;}
00523 void setRowScale(double * scale) ;
00524 void setColumnScale(double * scale);
00526 inline double objectiveScale() const
00527 { return objectiveScale_;}
00528 inline void setObjectiveScale(double value)
00529 { objectiveScale_ = value;}
00531 inline double rhsScale() const
00532 { return rhsScale_;}
00533 inline void setRhsScale(double value)
00534 { rhsScale_ = value;}
00536 void scaling(int mode=1);
00539 void unscale();
00541 inline int scalingFlag() const {return scalingFlag_;}
00543 inline double * objective() const
00544 {
00545 if (objective_) {
00546 double offset;
00547 return objective_->gradient(NULL,NULL,offset,false);
00548 } else {
00549 return NULL;
00550 }
00551 }
00552 inline double * objective(const double * solution, double & offset,bool refresh=true) const
00553 {
00554 offset=0.0;
00555 if (objective_) {
00556 return objective_->gradient(NULL,solution,offset,refresh);
00557 } else {
00558 return NULL;
00559 }
00560 }
00561 inline const double * getObjCoefficients() const
00562 {
00563 if (objective_) {
00564 double offset;
00565 return objective_->gradient(NULL,NULL,offset,false);
00566 } else {
00567 return NULL;
00568 }
00569 }
00571 inline double * rowObjective() const { return rowObjective_; }
00572 inline const double * getRowObjCoefficients() const {
00573 return rowObjective_;
00574 }
00576 inline double * columnLower() const { return columnLower_; }
00577 inline const double * getColLower() const { return columnLower_; }
00579 inline double * columnUpper() const { return columnUpper_; }
00580 inline const double * getColUpper() const { return columnUpper_; }
00582 inline CoinPackedMatrix * matrix() const {
00583 if ( matrix_ == NULL ) return NULL;
00584 else return matrix_->getPackedMatrix();
00585 }
00587 inline int getNumElements() const
00588 { return matrix_->getNumElements();}
00591 inline double getSmallElementValue() const
00592 { return smallElement_;}
00593 inline void setSmallElementValue(double value)
00594 { smallElement_=value;}
00596 inline ClpMatrixBase * rowCopy() const { return rowCopy_; }
00598 inline ClpMatrixBase * clpMatrix() const { return matrix_; }
00600 inline ClpPackedMatrix * clpScaledMatrix() const { return scaledMatrix_; }
00602 inline void setClpScaledMatrix(ClpPackedMatrix * scaledMatrix)
00603 { delete scaledMatrix_; scaledMatrix_=scaledMatrix; }
00609 void replaceMatrix(ClpMatrixBase * matrix,bool deleteCurrent=false);
00615 inline void replaceMatrix(CoinPackedMatrix * matrix,
00616 bool deleteCurrent=false)
00617 { replaceMatrix(new ClpPackedMatrix(matrix),deleteCurrent);}
00619 inline double objectiveValue() const {
00620 return objectiveValue_*optimizationDirection_ - dblParam_[ClpObjOffset];
00621 }
00622 inline void setObjectiveValue(double value) {
00623 objectiveValue_ = (value+ dblParam_[ClpObjOffset])/optimizationDirection_;
00624 }
00625 inline double getObjValue() const {
00626 return objectiveValue_*optimizationDirection_ - dblParam_[ClpObjOffset];
00627 }
00629 inline char * integerInformation() const { return integerType_; }
00632 double * infeasibilityRay() const;
00633 double * unboundedRay() const;
00635 inline bool statusExists() const
00636 { return (status_!=NULL);}
00638 inline unsigned char * statusArray() const
00639 { return status_;}
00642 unsigned char * statusCopy() const;
00644 void copyinStatus(const unsigned char * statusArray);
00645
00647 inline void setUserPointer (void * pointer)
00648 { userPointer_=pointer;}
00649 inline void * getUserPointer () const
00650 { return userPointer_;}
00652 inline int whatsChanged() const
00653 { return whatsChanged_;}
00654 inline void setWhatsChanged(int value)
00655 { whatsChanged_ = value;}
00657 inline int numberThreads() const
00658 { return numberThreads_;}
00659 inline void setNumberThreads(int value)
00660 { numberThreads_ = value;}
00662
00664
00665 void passInMessageHandler(CoinMessageHandler * handler);
00667 CoinMessageHandler * pushMessageHandler(CoinMessageHandler * handler,
00668 bool & oldDefault);
00670 void popMessageHandler(CoinMessageHandler * oldHandler,bool oldDefault);
00672 void newLanguage(CoinMessages::Language language);
00673 inline void setLanguage(CoinMessages::Language language) { newLanguage(language); }
00675 inline CoinMessageHandler * messageHandler() const { return handler_; }
00677 inline CoinMessages messages() const { return messages_; }
00679 inline CoinMessages * messagesPointer() { return & messages_; }
00681 inline CoinMessages coinMessages() const { return coinMessages_; }
00683 inline CoinMessages * coinMessagesPointer() { return & coinMessages_; }
00692 inline void setLogLevel(int value) { handler_->setLogLevel(value); }
00693 inline int logLevel() const { return handler_->logLevel(); }
00695 inline bool defaultHandler() const
00696 { return defaultHandler_;}
00698 void passInEventHandler(const ClpEventHandler * eventHandler);
00700 inline ClpEventHandler * eventHandler() const
00701 { return eventHandler_;}
00703 inline CoinThreadRandom * randomNumberGenerator()
00704 { return &randomNumberGenerator_;}
00706 inline CoinThreadRandom & mutableRandomNumberGenerator()
00707 { return randomNumberGenerator_;}
00709 inline void setRandomSeed(int value)
00710 { randomNumberGenerator_.setSeed(value);}
00712 inline int lengthNames() const { return lengthNames_; }
00713 #ifndef CLP_NO_STD
00715 inline void setLengthNames(int value) { lengthNames_=value; }
00717 inline const std::vector<std::string> * rowNames() const {
00718 return &rowNames_;
00719 }
00720 inline const std::string& rowName(int iRow) const {
00721 return rowNames_[iRow];
00722 }
00724 std::string getRowName(int iRow) const;
00726 inline const std::vector<std::string> * columnNames() const {
00727 return &columnNames_;
00728 }
00729 inline const std::string& columnName(int iColumn) const {
00730 return columnNames_[iColumn];
00731 }
00733 std::string getColumnName(int iColumn) const;
00734 #endif
00736 inline ClpObjective * objectiveAsObject() const
00737 { return objective_;}
00738 void setObjective(ClpObjective * objective);
00739 inline void setObjectivePointer(ClpObjective * objective)
00740 { objective_ = objective;}
00743 int emptyProblem(int * infeasNumber=NULL, double * infeasSum=NULL,bool printMessage=true);
00744
00746
00755 void times(double scalar,
00756 const double * x, double * y) const;
00760 void transposeTimes(double scalar,
00761 const double * x, double * y) const ;
00763
00764
00765
00783
00784 bool setIntParam(ClpIntParam key, int value) ;
00786 bool setDblParam(ClpDblParam key, double value) ;
00787 #ifndef CLP_NO_STD
00789 bool setStrParam(ClpStrParam key, const std::string & value);
00790 #endif
00791
00792 inline bool getIntParam(ClpIntParam key, int& value) const {
00793 if (key<ClpLastIntParam) {
00794 value = intParam_[key];
00795 return true;
00796 } else {
00797 return false;
00798 }
00799 }
00800
00801 inline bool getDblParam(ClpDblParam key, double& value) const {
00802 if (key<ClpLastDblParam) {
00803 value = dblParam_[key];
00804 return true;
00805 } else {
00806 return false;
00807 }
00808 }
00809 #ifndef CLP_NO_STD
00810
00811 inline bool getStrParam(ClpStrParam key, std::string& value) const {
00812 if (key<ClpLastStrParam) {
00813 value = strParam_[key];
00814 return true;
00815 } else {
00816 return false;
00817 }
00818 }
00819 #endif
00821 void generateCpp( FILE * fp);
00822
00852 #define COIN_CBC_USING_CLP 0x01000000
00853 inline unsigned int specialOptions() const
00854 { return specialOptions_;}
00855 void setSpecialOptions(unsigned int value);
00856 inline bool inCbcBranchAndBound() const
00857 { return (specialOptions_&COIN_CBC_USING_CLP)!=0;}
00859
00862 protected:
00864 void gutsOfDelete(int type);
00868 void gutsOfCopy(const ClpModel & rhs, int trueCopy=1);
00870 void getRowBound(int iRow, double& lower, double& upper) const;
00872 void gutsOfLoadModel ( int numberRows, int numberColumns,
00873 const double* collb, const double* colub,
00874 const double* obj,
00875 const double* rowlb, const double* rowub,
00876 const double * rowObjective=NULL);
00878 void gutsOfScaling();
00880 inline double rawObjectiveValue() const {
00881 return objectiveValue_;
00882 }
00884 inline bool permanentArrays() const
00885 { return (specialOptions_&65536)!=0;}
00887 void startPermanentArrays();
00889 void stopPermanentArrays();
00891 const char * const * rowNamesAsChar() const;
00893 const char * const * columnNamesAsChar() const;
00895 void deleteNamesAsChar(const char * const * names,int number) const;
00897 void onStopped();
00899
00900
00902 protected:
00903
00906
00907 double optimizationDirection_;
00909 double dblParam_[ClpLastDblParam];
00911 double objectiveValue_;
00913 double smallElement_;
00915 double objectiveScale_;
00917 double rhsScale_;
00919 int numberRows_;
00921 int numberColumns_;
00923 double * rowActivity_;
00925 double * columnActivity_;
00927 double * dual_;
00929 double * reducedCost_;
00931 double* rowLower_;
00933 double* rowUpper_;
00935 ClpObjective * objective_;
00937 double * rowObjective_;
00939 double * columnLower_;
00941 double * columnUpper_;
00943 ClpMatrixBase * matrix_;
00945 ClpMatrixBase * rowCopy_;
00947 ClpPackedMatrix * scaledMatrix_;
00949 double * ray_;
00951 double * rowScale_;
00953 double * columnScale_;
00955 int scalingFlag_;
00963 unsigned char * status_;
00965 char * integerType_;
00967 void * userPointer_;
00969 int intParam_[ClpLastIntParam];
00971 int numberIterations_;
00973 int solveType_;
00989 unsigned int whatsChanged_;
00991 int problemStatus_;
00993 int secondaryStatus_;
00995 int lengthNames_;
00997 int numberThreads_;
01001 unsigned int specialOptions_;
01003 CoinMessageHandler * handler_;
01005 bool defaultHandler_;
01007 CoinThreadRandom randomNumberGenerator_;
01009 ClpEventHandler * eventHandler_;
01010 #ifndef CLP_NO_STD
01012 std::vector<std::string> rowNames_;
01014 std::vector<std::string> columnNames_;
01015 #endif
01017 CoinMessages messages_;
01019 CoinMessages coinMessages_;
01021 int maximumColumns_;
01023 int maximumRows_;
01025 CoinPackedMatrix baseMatrix_;
01027 CoinPackedMatrix baseRowCopy_;
01029 double * savedRowScale_;
01031 double * savedColumnScale_;
01032 #ifndef CLP_NO_STD
01034 std::string strParam_[ClpLastStrParam];
01035 #endif
01036
01037 };
01040 class ClpDataSave {
01041
01042 public:
01046
01047 ClpDataSave ( );
01048
01050 ClpDataSave(const ClpDataSave &);
01052 ClpDataSave & operator=(const ClpDataSave & rhs);
01054 ~ClpDataSave ( );
01055
01057
01059 public:
01060
01063 double dualBound_;
01064 double infeasibilityCost_;
01065 double pivotTolerance_;
01066 double acceptablePivot_;
01067 double objectiveScale_;
01068 int sparseThreshold_;
01069 int perturbation_;
01070 int forceFactorization_;
01071 int scalingFlag_;
01073 };
01074
01075 #endif