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 "ClpParameters.hpp"
00019 #include "ClpObjective.hpp"
00020 class ClpEventHandler;
00021
00022
00023 #ifndef COIN_DBL_MAX
00024 #define COIN_DBL_MAX DBL_MAX
00025 #endif
00026
00036 class CoinBuild;
00037 class CoinModel;
00038 class ClpModel {
00039
00040 public:
00041
00047
00048 ClpModel (bool emptyMessages=false );
00049
00054 ClpModel(const ClpModel & rhs, int scalingMode=-1);
00056 ClpModel & operator=(const ClpModel & rhs);
00061 ClpModel (const ClpModel * wholeModel,
00062 int numberRows, const int * whichRows,
00063 int numberColumns, const int * whichColumns,
00064 bool dropNames=true, bool dropIntegers=true);
00066 ~ClpModel ( );
00068
00082 void loadProblem ( const ClpMatrixBase& matrix,
00083 const double* collb, const double* colub,
00084 const double* obj,
00085 const double* rowlb, const double* rowub,
00086 const double * rowObjective=NULL);
00087 void loadProblem ( const CoinPackedMatrix& matrix,
00088 const double* collb, const double* colub,
00089 const double* obj,
00090 const double* rowlb, const double* rowub,
00091 const double * rowObjective=NULL);
00092
00095 void loadProblem ( const int numcols, const int numrows,
00096 const CoinBigIndex* start, const int* index,
00097 const double* value,
00098 const double* collb, const double* colub,
00099 const double* obj,
00100 const double* rowlb, const double* rowub,
00101 const double * rowObjective=NULL);
00107 int loadProblem ( CoinModel & modelObject,bool tryPlusMinusOne=false);
00109 void loadProblem ( const int numcols, const int numrows,
00110 const CoinBigIndex* start, const int* index,
00111 const double* value,const int * length,
00112 const double* collb, const double* colub,
00113 const double* obj,
00114 const double* rowlb, const double* rowub,
00115 const double * rowObjective=NULL);
00117 void loadQuadraticObjective(const int numberColumns,
00118 const CoinBigIndex * start,
00119 const int * column, const double * element);
00120 void loadQuadraticObjective ( const CoinPackedMatrix& matrix);
00122 void deleteQuadraticObjective();
00124 void setRowObjective(const double * rowObjective);
00126 int readMps(const char *filename,
00127 bool keepNames=false,
00128 bool ignoreErrors = false);
00130 int readGMPL(const char *filename,const char * dataName,
00131 bool keepNames=false);
00133 void copyInIntegerInformation(const char * information);
00135 void deleteIntegerInformation();
00137 void setContinuous(int index);
00139 void setInteger(int index);
00141 bool isInteger(int index) const;
00143 void resize (int newNumberRows, int newNumberColumns);
00145 void deleteRows(int number, const int * which);
00147 void addRow(int numberInRow, const int * columns,
00148 const double * elements, double rowLower=-COIN_DBL_MAX,
00149 double rowUpper=COIN_DBL_MAX);
00151 void addRows(int number, const double * rowLower,
00152 const double * rowUpper,
00153 const CoinBigIndex * rowStarts, const int * columns,
00154 const double * elements);
00156 void addRows(int number, const double * rowLower,
00157 const double * rowUpper,
00158 const CoinBigIndex * rowStarts, const int * rowLengths,
00159 const int * columns,
00160 const double * elements);
00161 #ifndef CLP_NO_VECTOR
00162 void addRows(int number, const double * rowLower,
00163 const double * rowUpper,
00164 const CoinPackedVectorBase * const * rows);
00165 #endif
00166
00171 int addRows(const CoinBuild & buildObject,bool tryPlusMinusOne=false,
00172 bool checkDuplicates=true);
00181 int addRows(CoinModel & modelObject,bool tryPlusMinusOne=false,
00182 bool checkDuplicates=true);
00183
00185 void deleteColumns(int number, const int * which);
00187 void addColumn(int numberInColumn,
00188 const int * rows,
00189 const double * elements,
00190 double columnLower=0.0,
00191 double columnUpper=COIN_DBL_MAX,
00192 double objective=0.0);
00194 void addColumns(int number, const double * columnLower,
00195 const double * columnUpper,
00196 const double * objective,
00197 const CoinBigIndex * columnStarts, const int * rows,
00198 const double * elements);
00199 void addColumns(int number, const double * columnLower,
00200 const double * columnUpper,
00201 const double * objective,
00202 const CoinBigIndex * columnStarts, const int * columnLengths,
00203 const int * rows,
00204 const double * elements);
00205 #ifndef CLP_NO_VECTOR
00206 void addColumns(int number, const double * columnLower,
00207 const double * columnUpper,
00208 const double * objective,
00209 const CoinPackedVectorBase * const * columns);
00210 #endif
00211
00216 int addColumns(const CoinBuild & buildObject,bool tryPlusMinusOne=false,
00217 bool checkDuplicates=true);
00225 int addColumns(CoinModel & modelObject,bool tryPlusMinusOne=false,
00226 bool checkDuplicates=true);
00228 inline void modifyCoefficient(int row, int column, double newElement,
00229 bool keepZero=false)
00230 {matrix_->modifyCoefficient(row,column,newElement,keepZero);}
00232 void chgRowLower(const double * rowLower);
00234 void chgRowUpper(const double * rowUpper);
00236 void chgColumnLower(const double * columnLower);
00238 void chgColumnUpper(const double * columnUpper);
00240 void chgObjCoefficients(const double * objIn);
00244 void borrowModel(ClpModel & otherModel);
00247 void returnModel(ClpModel & otherModel);
00248
00250 void createEmptyMatrix();
00258 int cleanMatrix(double threshold=1.0e-20);
00259 #ifndef CLP_NO_STD
00261 void dropNames();
00263 void copyNames(std::vector<std::string> & rowNames,
00264 std::vector<std::string> & columnNames);
00266 void copyRowNames(const std::vector<std::string> & rowNames,int first, int last);
00268 void copyColumnNames(const std::vector<std::string> & columnNames, int first, int last);
00270 void copyRowNames(const char * const * rowNames,int first, int last);
00272 void copyColumnNames(const char * const * columnNames, int first, int last);
00274 void setRowName(int rowIndex, std::string & name) ;
00276 void setColumnName(int colIndex, std::string & name) ;
00277 #endif
00278
00280 CoinModel * createCoinModel() const;
00281
00294 int writeMps(const char *filename,
00295 int formatType=0,int numberAcross=2,
00296 double objSense=0.0) const ;
00298
00300
00301 inline int numberRows() const {
00302 return numberRows_;
00303 }
00304 inline int getNumRows() const {
00305 return numberRows_;
00306 }
00308 inline int getNumCols() const {
00309 return numberColumns_;
00310 }
00311 inline int numberColumns() const {
00312 return numberColumns_;
00313 }
00315 inline double primalTolerance() const {
00316 return dblParam_[ClpPrimalTolerance];
00317 }
00318 void setPrimalTolerance( double value) ;
00320 inline double dualTolerance() const { return dblParam_[ClpDualTolerance]; }
00321 void setDualTolerance( double value) ;
00323 inline double primalObjectiveLimit() const { return dblParam_[ClpPrimalObjectiveLimit];}
00324 void setPrimalObjectiveLimit(double value);
00326 inline double dualObjectiveLimit() const { return dblParam_[ClpDualObjectiveLimit];}
00327 void setDualObjectiveLimit(double value);
00329 inline double objectiveOffset() const { return dblParam_[ClpObjOffset];}
00330 void setObjectiveOffset(double value);
00331 #ifndef CLP_NO_STD
00332 inline std::string problemName() const { return strParam_[ClpProbName]; }
00333 #endif
00335 inline int numberIterations() const { return numberIterations_; }
00336 inline int getIterationCount() const { return numberIterations_; }
00337 inline void setNumberIterations(int numberIterations)
00338 { numberIterations_ = numberIterations;}
00340 inline int solveType() const
00341 { return solveType_;}
00342 inline void setSolveType(int type)
00343 { solveType_=type;}
00345 inline int maximumIterations() const { return intParam_[ClpMaxNumIteration]; }
00346 void setMaximumIterations(int value);
00348 inline double maximumSeconds() const { return dblParam_[ClpMaxSeconds]; }
00349 void setMaximumSeconds(double value);
00351 bool hitMaximumIterations() const;
00361 inline int status() const { return problemStatus_; }
00362 inline int problemStatus() const { return problemStatus_; }
00364 inline void setProblemStatus(int problemStatus)
00365 { problemStatus_ = problemStatus;}
00380 inline int secondaryStatus() const { return secondaryStatus_; }
00381 inline void setSecondaryStatus(int status)
00382 { secondaryStatus_ = status;}
00384 inline bool isAbandoned() const { return problemStatus_==4; }
00386 inline bool isProvenOptimal() const { return problemStatus_==0; }
00388 inline bool isProvenPrimalInfeasible() const { return problemStatus_==1; }
00390 inline bool isProvenDualInfeasible() const { return problemStatus_==2; }
00392 bool isPrimalObjectiveLimitReached() const ;
00394 bool isDualObjectiveLimitReached() const ;
00396 inline bool isIterationLimitReached() const { return problemStatus_==3; }
00398 inline double optimizationDirection() const {
00399 return optimizationDirection_;
00400 }
00401 inline double getObjSense() const { return optimizationDirection_; }
00402 void setOptimizationDirection(double value);
00404 inline double * primalRowSolution() const { return rowActivity_; }
00405 inline const double * getRowActivity() const { return rowActivity_; }
00407 inline double * primalColumnSolution() const { return columnActivity_; }
00408 inline const double * getColSolution() const { return columnActivity_; }
00409 inline void setColSolution(const double * input)
00410 { memcpy(columnActivity_,input,numberColumns_*sizeof(double));}
00412 inline double * dualRowSolution() const { return dual_; }
00413 inline const double * getRowPrice() const { return dual_; }
00415 inline double * dualColumnSolution() const { return reducedCost_; }
00416 inline const double * getReducedCost() const { return reducedCost_; }
00418 inline double* rowLower() const { return rowLower_; }
00419 inline const double* getRowLower() const { return rowLower_; }
00421 inline double* rowUpper() const { return rowUpper_; }
00422 inline const double* getRowUpper() const { return rowUpper_; }
00423
00427 void setObjectiveCoefficient( int elementIndex, double elementValue );
00429 inline void setObjCoeff( int elementIndex, double elementValue )
00430 { setObjectiveCoefficient( elementIndex, elementValue);}
00431
00434 void setColumnLower( int elementIndex, double elementValue );
00435
00438 void setColumnUpper( int elementIndex, double elementValue );
00439
00441 void setColumnBounds( int elementIndex,
00442 double lower, double upper );
00443
00452 void setColumnSetBounds(const int* indexFirst,
00453 const int* indexLast,
00454 const double* boundList);
00455
00458 inline void setColLower( int elementIndex, double elementValue )
00459 { setColumnLower(elementIndex, elementValue);}
00462 inline void setColUpper( int elementIndex, double elementValue )
00463 { setColumnUpper(elementIndex, elementValue);}
00464
00466 inline void setColBounds( int elementIndex,
00467 double lower, double upper )
00468 { setColumnBounds(elementIndex, lower, upper);}
00469
00476 inline void setColSetBounds(const int* indexFirst,
00477 const int* indexLast,
00478 const double* boundList)
00479 { setColumnSetBounds(indexFirst, indexLast, boundList);}
00480
00483 void setRowLower( int elementIndex, double elementValue );
00484
00487 void setRowUpper( int elementIndex, double elementValue ) ;
00488
00490 void setRowBounds( int elementIndex,
00491 double lower, double upper ) ;
00492
00499 void setRowSetBounds(const int* indexFirst,
00500 const int* indexLast,
00501 const double* boundList);
00502
00504
00505 inline const double * rowScale() const {return rowScale_;}
00506 inline const double * columnScale() const {return columnScale_;}
00507 inline void setRowScale(double * scale) { delete [] (double *) rowScale_; rowScale_ = scale;}
00508 inline void setColumnScale(double * scale) { delete [] (double *) columnScale_; columnScale_ = scale;}
00510 inline double objectiveScale() const
00511 { return objectiveScale_;}
00512 inline void setObjectiveScale(double value)
00513 { objectiveScale_ = value;}
00515 inline double rhsScale() const
00516 { return rhsScale_;}
00517 inline void setRhsScale(double value)
00518 { rhsScale_ = value;}
00520 void scaling(int mode=1);
00523 void unscale();
00525 inline int scalingFlag() const {return scalingFlag_;}
00527 inline double * objective() const
00528 {
00529 if (objective_) {
00530 double offset;
00531 return objective_->gradient(NULL,NULL,offset,false);
00532 } else {
00533 return NULL;
00534 }
00535 }
00536 inline double * objective(const double * solution, double & offset,bool refresh=true) const
00537 {
00538 offset=0.0;
00539 if (objective_) {
00540 return objective_->gradient(NULL,solution,offset,refresh);
00541 } else {
00542 return NULL;
00543 }
00544 }
00545 inline const double * getObjCoefficients() const
00546 {
00547 if (objective_) {
00548 double offset;
00549 return objective_->gradient(NULL,NULL,offset,false);
00550 } else {
00551 return NULL;
00552 }
00553 }
00555 inline double * rowObjective() const { return rowObjective_; }
00556 inline const double * getRowObjCoefficients() const {
00557 return rowObjective_;
00558 }
00560 inline double * columnLower() const { return columnLower_; }
00561 inline const double * getColLower() const { return columnLower_; }
00563 inline double * columnUpper() const { return columnUpper_; }
00564 inline const double * getColUpper() const { return columnUpper_; }
00566 inline CoinPackedMatrix * matrix() const {
00567 if ( matrix_ == NULL ) return NULL;
00568 else return matrix_->getPackedMatrix();
00569 }
00571 inline int getNumElements() const
00572 { return matrix_->getNumElements();}
00575 inline double getSmallElementValue() const
00576 { return smallElement_;}
00577 inline void setSmallElementValue(double value)
00578 { smallElement_=value;}
00580 inline ClpMatrixBase * rowCopy() const { return rowCopy_; }
00582 inline ClpMatrixBase * clpMatrix() const { return matrix_; }
00588 void replaceMatrix(ClpMatrixBase * matrix,bool deleteCurrent=false);
00594 inline void replaceMatrix(CoinPackedMatrix * matrix,
00595 bool deleteCurrent=false)
00596 { replaceMatrix(new ClpPackedMatrix(matrix),deleteCurrent);}
00598 inline double objectiveValue() const {
00599 return objectiveValue_*optimizationDirection_ - dblParam_[ClpObjOffset];
00600 }
00601 inline void setObjectiveValue(double value) {
00602 objectiveValue_ = (value+ dblParam_[ClpObjOffset])/optimizationDirection_;
00603 }
00604 inline double getObjValue() const {
00605 return objectiveValue_*optimizationDirection_ - dblParam_[ClpObjOffset];
00606 }
00608 inline char * integerInformation() const { return integerType_; }
00611 double * infeasibilityRay() const;
00612 double * unboundedRay() const;
00614 inline bool statusExists() const
00615 { return (status_!=NULL);}
00617 inline unsigned char * statusArray() const
00618 { return status_;}
00621 unsigned char * statusCopy() const;
00623 void copyinStatus(const unsigned char * statusArray);
00624
00626 inline void setUserPointer (void * pointer)
00627 { userPointer_=pointer;}
00628 inline void * getUserPointer () const
00629 { return userPointer_;}
00631 inline int whatsChanged() const
00632 { return whatsChanged_;}
00633 inline void setWhatsChanged(int value)
00634 { whatsChanged_ = value;}
00636 inline int numberThreads() const
00637 { return numberThreads_;}
00638 inline void setNumberThreads(int value)
00639 { numberThreads_ = value;}
00641
00643
00644 void passInMessageHandler(CoinMessageHandler * handler);
00646 CoinMessageHandler * pushMessageHandler(CoinMessageHandler * handler,
00647 bool & oldDefault);
00649 void popMessageHandler(CoinMessageHandler * oldHandler,bool oldDefault);
00651 void newLanguage(CoinMessages::Language language);
00652 inline void setLanguage(CoinMessages::Language language) { newLanguage(language); }
00654 inline CoinMessageHandler * messageHandler() const { return handler_; }
00656 inline CoinMessages messages() const { return messages_; }
00658 inline CoinMessages * messagesPointer() { return & messages_; }
00660 inline CoinMessages coinMessages() const { return coinMessages_; }
00662 inline CoinMessages * coinMessagesPointer() { return & coinMessages_; }
00671 inline void setLogLevel(int value) { handler_->setLogLevel(value); }
00672 inline int logLevel() const { return handler_->logLevel(); }
00674 inline bool defaultHandler() const
00675 { return defaultHandler_;}
00677 void passInEventHandler(const ClpEventHandler * eventHandler);
00679 inline ClpEventHandler * eventHandler() const
00680 { return eventHandler_;}
00682 inline int lengthNames() const { return lengthNames_; }
00683 #ifndef CLP_NO_STD
00685 inline void setLengthNames(int value) { lengthNames_=value; }
00687 inline const std::vector<std::string> * rowNames() const {
00688 return &rowNames_;
00689 }
00690 inline const std::string& rowName(int iRow) const {
00691 return rowNames_[iRow];
00692 }
00694 std::string getRowName(int iRow) const;
00696 inline const std::vector<std::string> * columnNames() const {
00697 return &columnNames_;
00698 }
00699 inline const std::string& columnName(int iColumn) const {
00700 return columnNames_[iColumn];
00701 }
00703 std::string getColumnName(int iColumn) const;
00704 #endif
00706 inline ClpObjective * objectiveAsObject() const
00707 { return objective_;}
00708 void setObjective(ClpObjective * objective);
00709 inline void setObjectivePointer(ClpObjective * objective)
00710 { objective_ = objective;}
00713 int emptyProblem(int * infeasNumber=NULL, double * infeasSum=NULL,bool printMessage=true);
00714
00716
00725 void times(double scalar,
00726 const double * x, double * y) const;
00730 void transposeTimes(double scalar,
00731 const double * x, double * y) const ;
00733
00734
00735
00753
00754 bool setIntParam(ClpIntParam key, int value) ;
00756 bool setDblParam(ClpDblParam key, double value) ;
00757 #ifndef CLP_NO_STD
00759 bool setStrParam(ClpStrParam key, const std::string & value);
00760 #endif
00761
00762 inline bool getIntParam(ClpIntParam key, int& value) const {
00763 if (key<ClpLastIntParam) {
00764 value = intParam_[key];
00765 return true;
00766 } else {
00767 return false;
00768 }
00769 }
00770
00771 inline bool getDblParam(ClpDblParam key, double& value) const {
00772 if (key<ClpLastDblParam) {
00773 value = dblParam_[key];
00774 return true;
00775 } else {
00776 return false;
00777 }
00778 }
00779 #ifndef CLP_NO_STD
00780
00781 inline bool getStrParam(ClpStrParam key, std::string& value) const {
00782 if (key<ClpLastStrParam) {
00783 value = strParam_[key];
00784 return true;
00785 } else {
00786 return false;
00787 }
00788 }
00789 #endif
00791 void generateCpp( FILE * fp);
00792
00821 #define COIN_CBC_USING_CLP 0x01000000
00822 inline unsigned int specialOptions() const
00823 { return specialOptions_;}
00824 void setSpecialOptions(unsigned int value);
00826
00829 protected:
00831 void gutsOfDelete();
00834 void gutsOfCopy(const ClpModel & rhs, bool trueCopy=true);
00836 void getRowBound(int iRow, double& lower, double& upper) const;
00838 void gutsOfLoadModel ( int numberRows, int numberColumns,
00839 const double* collb, const double* colub,
00840 const double* obj,
00841 const double* rowlb, const double* rowub,
00842 const double * rowObjective=NULL);
00844 void gutsOfScaling();
00846 inline double rawObjectiveValue() const {
00847 return objectiveValue_;
00848 }
00850 const char * const * rowNamesAsChar() const;
00852 const char * const * columnNamesAsChar() const;
00854 void deleteNamesAsChar(const char * const * names,int number) const;
00856 void onStopped();
00858
00859
00861 protected:
00862
00865
00866 double optimizationDirection_;
00868 double dblParam_[ClpLastDblParam];
00870 double objectiveValue_;
00872 double smallElement_;
00874 double objectiveScale_;
00876 double rhsScale_;
00878 int numberRows_;
00880 int numberColumns_;
00882 double * rowActivity_;
00884 double * columnActivity_;
00886 double * dual_;
00888 double * reducedCost_;
00890 double* rowLower_;
00892 double* rowUpper_;
00894 ClpObjective * objective_;
00896 double * rowObjective_;
00898 double * columnLower_;
00900 double * columnUpper_;
00902 ClpMatrixBase * matrix_;
00904 ClpMatrixBase * rowCopy_;
00906 double * ray_;
00908 double * rowScale_;
00910 double * columnScale_;
00912 int scalingFlag_;
00920 unsigned char * status_;
00922 char * integerType_;
00924 void * userPointer_;
00926 int intParam_[ClpLastIntParam];
00928 int numberIterations_;
00930 int solveType_;
00946 unsigned int whatsChanged_;
00948 int problemStatus_;
00950 int secondaryStatus_;
00952 int lengthNames_;
00954 int numberThreads_;
00958 unsigned int specialOptions_;
00960 CoinMessageHandler * handler_;
00962 bool defaultHandler_;
00964 ClpEventHandler * eventHandler_;
00965 #ifndef CLP_NO_STD
00967 std::vector<std::string> rowNames_;
00969 std::vector<std::string> columnNames_;
00970 #endif
00972 CoinMessages messages_;
00974 CoinMessages coinMessages_;
00975 #ifndef CLP_NO_STD
00977 std::string strParam_[ClpLastStrParam];
00978 #endif
00979
00980 };
00983 class ClpDataSave {
00984
00985 public:
00989
00990 ClpDataSave ( );
00991
00993 ClpDataSave(const ClpDataSave &);
00995 ClpDataSave & operator=(const ClpDataSave & rhs);
00997 ~ClpDataSave ( );
00998
01000
01002 public:
01003
01006 double dualBound_;
01007 double infeasibilityCost_;
01008 double pivotTolerance_;
01009 double acceptablePivot_;
01010 double objectiveScale_;
01011 int sparseThreshold_;
01012 int perturbation_;
01013 int forceFactorization_;
01014 int scalingFlag_;
01016 };
01017
01018 #endif