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 ( );
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();
00251 #ifndef CLP_NO_STD
00253 void dropNames();
00255 void copyNames(std::vector<std::string> & rowNames,
00256 std::vector<std::string> & columnNames);
00258 void copyRowNames(const std::vector<std::string> & rowNames,int first, int last);
00260 void copyColumnNames(const std::vector<std::string> & columnNames, int first, int last);
00262 void copyRowNames(const char * const * rowNames,int first, int last);
00264 void copyColumnNames(const char * const * columnNames, int first, int last);
00266 void setRowName(int rowIndex, std::string & name) ;
00268 void setColumnName(int colIndex, std::string & name) ;
00269 #endif
00270
00283 int writeMps(const char *filename,
00284 int formatType=0,int numberAcross=2,
00285 double objSense=0.0) const ;
00287
00289
00290 inline int numberRows() const {
00291 return numberRows_;
00292 }
00293 inline int getNumRows() const {
00294 return numberRows_;
00295 }
00297 inline int getNumCols() const {
00298 return numberColumns_;
00299 }
00300 inline int numberColumns() const {
00301 return numberColumns_;
00302 }
00304 inline double primalTolerance() const {
00305 return dblParam_[ClpPrimalTolerance];
00306 }
00307 void setPrimalTolerance( double value) ;
00309 inline double dualTolerance() const { return dblParam_[ClpDualTolerance]; }
00310 void setDualTolerance( double value) ;
00312 inline double primalObjectiveLimit() const { return dblParam_[ClpPrimalObjectiveLimit];}
00313 void setPrimalObjectiveLimit(double value);
00315 inline double dualObjectiveLimit() const { return dblParam_[ClpDualObjectiveLimit];}
00316 void setDualObjectiveLimit(double value);
00318 inline double objectiveOffset() const { return dblParam_[ClpObjOffset];}
00319 void setObjectiveOffset(double value);
00320 #ifndef CLP_NO_STD
00321 inline std::string problemName() const { return strParam_[ClpProbName]; };
00322 #endif
00324 inline int numberIterations() const { return numberIterations_; }
00325 inline int getIterationCount() const { return numberIterations_; }
00326 inline void setNumberIterations(int numberIterations)
00327 { numberIterations_ = numberIterations;};
00329 inline int solveType() const
00330 { return solveType_;};
00331 inline void setSolveType(int type)
00332 { solveType_=type;};
00334 inline int maximumIterations() const { return intParam_[ClpMaxNumIteration]; }
00335 void setMaximumIterations(int value);
00337 inline double maximumSeconds() const { return dblParam_[ClpMaxSeconds]; }
00338 void setMaximumSeconds(double value);
00340 bool hitMaximumIterations() const;
00350 inline int status() const { return problemStatus_; }
00351 inline int problemStatus() const { return problemStatus_; }
00353 inline void setProblemStatus(int problemStatus)
00354 { problemStatus_ = problemStatus;};
00367 inline int secondaryStatus() const { return secondaryStatus_; }
00368 inline void setSecondaryStatus(int status)
00369 { secondaryStatus_ = status;};
00371 inline bool isAbandoned() const { return problemStatus_==4; }
00373 inline bool isProvenOptimal() const { return problemStatus_==0; }
00375 inline bool isProvenPrimalInfeasible() const { return problemStatus_==1; }
00377 inline bool isProvenDualInfeasible() const { return problemStatus_==2; }
00379 bool isPrimalObjectiveLimitReached() const ;
00381 bool isDualObjectiveLimitReached() const ;
00383 inline bool isIterationLimitReached() const { return problemStatus_==3; }
00385 inline double optimizationDirection() const {
00386 return optimizationDirection_;
00387 }
00388 inline double getObjSense() const { return optimizationDirection_; }
00389 void setOptimizationDirection(double value);
00391 inline double * primalRowSolution() const { return rowActivity_; }
00392 inline const double * getRowActivity() const { return rowActivity_; }
00394 inline double * primalColumnSolution() const { return columnActivity_; }
00395 inline const double * getColSolution() const { return columnActivity_; }
00396 inline void setColSolution(const double * input)
00397 { memcpy(columnActivity_,input,numberColumns_*sizeof(double));};
00399 inline double * dualRowSolution() const { return dual_; }
00400 inline const double * getRowPrice() const { return dual_; }
00402 inline double * dualColumnSolution() const { return reducedCost_; }
00403 inline const double * getReducedCost() const { return reducedCost_; }
00405 inline double* rowLower() const { return rowLower_; }
00406 inline const double* getRowLower() const { return rowLower_; }
00408 inline double* rowUpper() const { return rowUpper_; }
00409 inline const double* getRowUpper() const { return rowUpper_; }
00410
00414 void setObjectiveCoefficient( int elementIndex, double elementValue );
00416 inline void setObjCoeff( int elementIndex, double elementValue )
00417 { setObjectiveCoefficient( elementIndex, elementValue);};
00418
00421 void setColumnLower( int elementIndex, double elementValue );
00422
00425 void setColumnUpper( int elementIndex, double elementValue );
00426
00428 void setColumnBounds( int elementIndex,
00429 double lower, double upper );
00430
00439 void setColumnSetBounds(const int* indexFirst,
00440 const int* indexLast,
00441 const double* boundList);
00442
00445 inline void setColLower( int elementIndex, double elementValue )
00446 { setColumnLower(elementIndex, elementValue);};
00449 inline void setColUpper( int elementIndex, double elementValue )
00450 { setColumnUpper(elementIndex, elementValue);};
00451
00453 inline void setColBounds( int elementIndex,
00454 double lower, double upper )
00455 { setColumnBounds(elementIndex, lower, upper);};
00456
00463 inline void setColSetBounds(const int* indexFirst,
00464 const int* indexLast,
00465 const double* boundList)
00466 { setColumnSetBounds(indexFirst, indexLast, boundList);};
00467
00470 void setRowLower( int elementIndex, double elementValue );
00471
00474 void setRowUpper( int elementIndex, double elementValue ) ;
00475
00477 void setRowBounds( int elementIndex,
00478 double lower, double upper ) ;
00479
00486 void setRowSetBounds(const int* indexFirst,
00487 const int* indexLast,
00488 const double* boundList);
00489
00491
00492 inline const double * rowScale() const {return rowScale_;};
00493 inline const double * columnScale() const {return columnScale_;};
00494 inline void setRowScale(double * scale) { delete [] (double *) rowScale_; rowScale_ = scale;};
00495 inline void setColumnScale(double * scale) { delete [] (double *) columnScale_; columnScale_ = scale;};
00497 inline double objectiveScale() const
00498 { return objectiveScale_;} ;
00499 inline void setObjectiveScale(double value)
00500 { objectiveScale_ = value;} ;
00502 inline double rhsScale() const
00503 { return rhsScale_;} ;
00504 inline void setRhsScale(double value)
00505 { rhsScale_ = value;} ;
00507 void scaling(int mode=1);
00510 void unscale();
00512 inline int scalingFlag() const {return scalingFlag_;};
00514 inline double * objective() const
00515 {
00516 if (objective_) {
00517 double offset;
00518 return objective_->gradient(NULL,NULL,offset,false);
00519 } else {
00520 return NULL;
00521 }
00522 }
00523 inline double * objective(const double * solution, double & offset,bool refresh=true) const
00524 {
00525 offset=0.0;
00526 if (objective_) {
00527 return objective_->gradient(NULL,solution,offset,refresh);
00528 } else {
00529 return NULL;
00530 }
00531 }
00532 inline const double * getObjCoefficients() const
00533 {
00534 if (objective_) {
00535 double offset;
00536 return objective_->gradient(NULL,NULL,offset,false);
00537 } else {
00538 return NULL;
00539 }
00540 }
00542 inline double * rowObjective() const { return rowObjective_; }
00543 inline const double * getRowObjCoefficients() const {
00544 return rowObjective_;
00545 }
00547 inline double * columnLower() const { return columnLower_; }
00548 inline const double * getColLower() const { return columnLower_; }
00550 inline double * columnUpper() const { return columnUpper_; }
00551 inline const double * getColUpper() const { return columnUpper_; }
00553 inline CoinPackedMatrix * matrix() const {
00554 if ( matrix_ == NULL ) return NULL;
00555 else return matrix_->getPackedMatrix();
00556 }
00558 inline int getNumElements() const
00559 { return matrix_->getNumElements();};
00562 inline double getSmallElementValue() const
00563 { return smallElement_;};
00564 inline void setSmallElementValue(double value)
00565 { smallElement_=value;};
00567 inline ClpMatrixBase * rowCopy() const { return rowCopy_; }
00569 inline ClpMatrixBase * clpMatrix() const { return matrix_; }
00575 void replaceMatrix(ClpMatrixBase * matrix,bool deleteCurrent=false);
00581 inline void replaceMatrix(CoinPackedMatrix * matrix,
00582 bool deleteCurrent=false)
00583 { replaceMatrix(new ClpPackedMatrix(matrix),deleteCurrent);};
00585 inline double objectiveValue() const {
00586 return objectiveValue_*optimizationDirection_ - dblParam_[ClpObjOffset];
00587 }
00588 inline void setObjectiveValue(double value) {
00589 objectiveValue_ = (value+ dblParam_[ClpObjOffset])/optimizationDirection_;
00590 }
00591 inline double getObjValue() const {
00592 return objectiveValue_*optimizationDirection_ - dblParam_[ClpObjOffset];
00593 }
00595 inline char * integerInformation() const { return integerType_; }
00598 double * infeasibilityRay() const;
00599 double * unboundedRay() const;
00601 inline bool statusExists() const
00602 { return (status_!=NULL);};
00604 inline unsigned char * statusArray() const
00605 { return status_;};
00608 unsigned char * statusCopy() const;
00610 void copyinStatus(const unsigned char * statusArray);
00611
00613 inline void setUserPointer (void * pointer)
00614 { userPointer_=pointer;};
00615 inline void * getUserPointer () const
00616 { return userPointer_;};
00618 inline int whatsChanged() const
00619 { return whatsChanged_;} ;
00620 inline void setWhatsChanged(int value)
00621 { whatsChanged_ = value;} ;
00623 inline int numberThreads() const
00624 { return numberThreads_;} ;
00625 inline void setNumberThreads(int value)
00626 { numberThreads_ = value;} ;
00628
00630
00631 void passInMessageHandler(CoinMessageHandler * handler);
00633 CoinMessageHandler * pushMessageHandler(CoinMessageHandler * handler,
00634 bool & oldDefault);
00636 void popMessageHandler(CoinMessageHandler * oldHandler,bool oldDefault);
00638 void newLanguage(CoinMessages::Language language);
00639 inline void setLanguage(CoinMessages::Language language) { newLanguage(language); }
00641 inline CoinMessageHandler * messageHandler() const { return handler_; }
00643 inline CoinMessages messages() const { return messages_; }
00645 inline CoinMessages * messagesPointer() { return & messages_; }
00647 inline CoinMessages coinMessages() const { return coinMessages_; }
00649 inline CoinMessages * coinMessagesPointer() { return & coinMessages_; }
00658 inline void setLogLevel(int value) { handler_->setLogLevel(value); }
00659 inline int logLevel() const { return handler_->logLevel(); }
00661 void passInEventHandler(const ClpEventHandler * eventHandler);
00663 inline ClpEventHandler * eventHandler() const
00664 { return eventHandler_;};
00666 inline int lengthNames() const { return lengthNames_; }
00667 #ifndef CLP_NO_STD
00669 inline void setLengthNames(int value) { lengthNames_=value; }
00671 inline const std::vector<std::string> * rowNames() const {
00672 return &rowNames_;
00673 }
00674 inline const std::string& rowName(int iRow) const {
00675 return rowNames_[iRow];
00676 }
00678 std::string getRowName(int iRow) const;
00680 inline const std::vector<std::string> * columnNames() const {
00681 return &columnNames_;
00682 }
00683 inline const std::string& columnName(int iColumn) const {
00684 return columnNames_[iColumn];
00685 }
00687 std::string getColumnName(int iColumn) const;
00688 #endif
00690 inline ClpObjective * objectiveAsObject() const
00691 { return objective_;};
00692 void setObjective(ClpObjective * objective);
00693 inline void setObjectivePointer(ClpObjective * objective)
00694 { objective_ = objective;};
00697 int emptyProblem(int * infeasNumber=NULL, double * infeasSum=NULL,bool printMessage=true);
00698
00700
00709 void times(double scalar,
00710 const double * x, double * y) const;
00714 void transposeTimes(double scalar,
00715 const double * x, double * y) const ;
00717
00718
00719
00737
00738 bool setIntParam(ClpIntParam key, int value) ;
00740 bool setDblParam(ClpDblParam key, double value) ;
00741 #ifndef CLP_NO_STD
00743 bool setStrParam(ClpStrParam key, const std::string & value);
00744 #endif
00745
00746 inline bool getIntParam(ClpIntParam key, int& value) const {
00747 if (key!=ClpLastIntParam) {
00748 value = intParam_[key];
00749 return true;
00750 } else {
00751 return false;
00752 }
00753 }
00754
00755 inline bool getDblParam(ClpDblParam key, double& value) const {
00756 if (key!=ClpLastDblParam) {
00757 value = dblParam_[key];
00758 return true;
00759 } else {
00760 return false;
00761 }
00762 }
00763 #ifndef CLP_NO_STD
00764
00765 inline bool getStrParam(ClpStrParam key, std::string& value) const {
00766 if (key!=ClpLastStrParam) {
00767 value = strParam_[key];
00768 return true;
00769 } else {
00770 return false;
00771 }
00772 }
00773 #endif
00775 void generateCpp( FILE * fp);
00776
00777
00780 protected:
00782 void gutsOfDelete();
00785 void gutsOfCopy(const ClpModel & rhs, bool trueCopy=true);
00787 void getRowBound(int iRow, double& lower, double& upper) const;
00789 void gutsOfLoadModel ( int numberRows, int numberColumns,
00790 const double* collb, const double* colub,
00791 const double* obj,
00792 const double* rowlb, const double* rowub,
00793 const double * rowObjective=NULL);
00795 void gutsOfScaling();
00797 inline double rawObjectiveValue() const {
00798 return objectiveValue_;
00799 }
00801 const char * const * const rowNamesAsChar() const;
00803 const char * const * const columnNamesAsChar() const;
00805 void deleteNamesAsChar(const char * const * const names,int number) const;
00807
00808
00810 protected:
00811
00814
00815 double optimizationDirection_;
00817 double dblParam_[ClpLastDblParam];
00819 double objectiveValue_;
00821 double smallElement_;
00823 double objectiveScale_;
00825 double rhsScale_;
00827 int numberRows_;
00829 int numberColumns_;
00831 double * rowActivity_;
00833 double * columnActivity_;
00835 double * dual_;
00837 double * reducedCost_;
00839 double* rowLower_;
00841 double* rowUpper_;
00843 ClpObjective * objective_;
00845 double * rowObjective_;
00847 double * columnLower_;
00849 double * columnUpper_;
00851 ClpMatrixBase * matrix_;
00853 ClpMatrixBase * rowCopy_;
00855 double * ray_;
00857 double * rowScale_;
00859 double * columnScale_;
00861 int scalingFlag_;
00869 unsigned char * status_;
00871 char * integerType_;
00873 void * userPointer_;
00875 int intParam_[ClpLastIntParam];
00877 int numberIterations_;
00879 int solveType_;
00895 unsigned int whatsChanged_;
00897 int problemStatus_;
00899 int secondaryStatus_;
00901 int lengthNames_;
00903 int numberThreads_;
00905 CoinMessageHandler * handler_;
00907 bool defaultHandler_;
00909 ClpEventHandler * eventHandler_;
00910 #ifndef CLP_NO_STD
00912 std::vector<std::string> rowNames_;
00914 std::vector<std::string> columnNames_;
00915 #endif
00917 CoinMessages messages_;
00919 CoinMessages coinMessages_;
00920 #ifndef CLP_NO_STD
00922 std::string strParam_[ClpLastStrParam];
00923 #endif
00924
00925 };
00928 class ClpDataSave {
00929
00930 public:
00934
00935 ClpDataSave ( );
00936
00938 ClpDataSave(const ClpDataSave &);
00940 ClpDataSave & operator=(const ClpDataSave & rhs);
00942 ~ClpDataSave ( );
00943
00945
00947 public:
00948
00951 double dualBound_;
00952 double infeasibilityCost_;
00953 double pivotTolerance_;
00954 double acceptablePivot_;
00955 double objectiveScale_;
00956 int sparseThreshold_;
00957 int perturbation_;
00958 int forceFactorization_;
00959 int scalingFlag_;
00961 };
00962
00963 #endif