00001
00002
00003 #ifndef ClpModel_H
00004 #define ClpModel_H
00005
00006 #include <iostream>
00007 #include <cassert>
00008 #include <cmath>
00009 #include <vector>
00010 #include <string>
00011
00012 #include "ClpMatrixBase.hpp"
00013 #include "CoinMessageHandler.hpp"
00014 #include "ClpParameters.hpp"
00015 #include "ClpObjective.hpp"
00016
00017
00018 #ifndef COIN_DBL_MAX
00019 #define COIN_DBL_MAX DBL_MAX
00020 #endif
00021
00032 class ClpModel {
00033
00034 public:
00035
00041
00042 ClpModel ( );
00043
00045 ClpModel(const ClpModel &);
00047 ClpModel & operator=(const ClpModel & rhs);
00052 ClpModel (const ClpModel * wholeModel,
00053 int numberRows, const int * whichRows,
00054 int numberColumns, const int * whichColumns,
00055 bool dropNames=true, bool dropIntegers=true);
00057 ~ClpModel ( );
00059
00073 void loadProblem ( const ClpMatrixBase& matrix,
00074 const double* collb, const double* colub,
00075 const double* obj,
00076 const double* rowlb, const double* rowub,
00077 const double * rowObjective=NULL);
00078 void loadProblem ( const CoinPackedMatrix& matrix,
00079 const double* collb, const double* colub,
00080 const double* obj,
00081 const double* rowlb, const double* rowub,
00082 const double * rowObjective=NULL);
00083
00086 void loadProblem ( const int numcols, const int numrows,
00087 const CoinBigIndex* start, const int* index,
00088 const double* value,
00089 const double* collb, const double* colub,
00090 const double* obj,
00091 const double* rowlb, const double* rowub,
00092 const double * rowObjective=NULL);
00094 void loadProblem ( const int numcols, const int numrows,
00095 const CoinBigIndex* start, const int* index,
00096 const double* value,const int * length,
00097 const double* collb, const double* colub,
00098 const double* obj,
00099 const double* rowlb, const double* rowub,
00100 const double * rowObjective=NULL);
00102 void loadQuadraticObjective(const int numberColumns,
00103 const CoinBigIndex * start,
00104 const int * column, const double * element);
00105 void loadQuadraticObjective ( const CoinPackedMatrix& matrix);
00107 void deleteQuadraticObjective();
00109 void setRowObjective(const double * rowObjective);
00111 int readMps(const char *filename,
00112 bool keepNames=false,
00113 bool ignoreErrors = false);
00115 void copyInIntegerInformation(const char * information);
00117 void deleteIntegerInformation();
00119 void resize (int newNumberRows, int newNumberColumns);
00121 void deleteRows(int number, const int * which);
00123 void addRows(int number, const double * rowLower,
00124 const double * rowUpper,
00125 const int * rowStarts, const int * columns,
00126 const double * elements);
00128 void addRows(int number, const double * rowLower,
00129 const double * rowUpper,
00130 const int * rowStarts, const int * rowLengths,
00131 const int * columns,
00132 const double * elements);
00133 void addRows(int number, const double * rowLower,
00134 const double * rowUpper,
00135 const CoinPackedVectorBase * const * rows);
00136
00138 void deleteColumns(int number, const int * which);
00140 void addColumns(int number, const double * columnLower,
00141 const double * columnUpper,
00142 const double * objective,
00143 const int * columnStarts, const int * rows,
00144 const double * elements);
00145 void addColumns(int number, const double * columnLower,
00146 const double * columnUpper,
00147 const double * objective,
00148 const int * columnStarts, const int * columnLengths,
00149 const int * rows,
00150 const double * elements);
00151 void addColumns(int number, const double * columnLower,
00152 const double * columnUpper,
00153 const double * objective,
00154 const CoinPackedVectorBase * const * columns);
00158 void borrowModel(ClpModel & otherModel);
00161 void returnModel(ClpModel & otherModel);
00162
00164 void createEmptyMatrix();
00166 void dropNames();
00168 void copyNames(std::vector<std::string> & rowNames,
00169 std::vector<std::string> & columnNames);
00170
00172
00174
00175 inline int numberRows() const {
00176 return numberRows_;
00177 }
00178 inline int getNumRows() const {
00179 return numberRows_;
00180 }
00182 inline int getNumCols() const {
00183 return numberColumns_;
00184 }
00185 inline int numberColumns() const {
00186 return numberColumns_;
00187 }
00189 inline double primalTolerance() const {
00190 return dblParam_[ClpPrimalTolerance];
00191 }
00192 void setPrimalTolerance( double value) ;
00194 inline double dualTolerance() const { return dblParam_[ClpDualTolerance]; }
00195 void setDualTolerance( double value) ;
00197 inline double dualObjectiveLimit() const { return dblParam_[ClpDualObjectiveLimit];}
00198 void setDualObjectiveLimit(double value);
00200 inline double objectiveOffset() const { return dblParam_[ClpObjOffset];}
00201 void setObjectiveOffset(double value);
00202 inline std::string problemName() const { return strParam_[ClpProbName]; };
00204 inline int numberIterations() const { return numberIterations_; }
00205 inline int getIterationCount() const { return numberIterations_; }
00206 inline void setNumberIterations(int numberIterations)
00207 { numberIterations_ = numberIterations;};
00209 inline int solveType() const
00210 { return solveType_;};
00211 inline void setSolveType(int type)
00212 { solveType_=type;};
00214 inline int maximumIterations() const { return intParam_[ClpMaxNumIteration]; }
00215 void setMaximumIterations(int value);
00217 inline double maximumSeconds() const { return dblParam_[ClpMaxSeconds]; }
00218 void setMaximumSeconds(double value);
00220 bool hitMaximumIterations() const;
00228 inline int status() const { return problemStatus_; }
00230 inline void setProblemStatus(int problemStatus)
00231 { problemStatus_ = problemStatus;};
00239 inline int secondaryStatus() const { return secondaryStatus_; }
00240 inline void setSecondaryStatus(int status)
00241 { secondaryStatus_ = status;};
00243 bool isAbandoned() const { return problemStatus_==4; }
00245 bool isProvenOptimal() const { return problemStatus_==0; }
00247 bool isProvenPrimalInfeasible() const { return problemStatus_==1; }
00249 bool isProvenDualInfeasible() const { return problemStatus_==2; }
00251 bool isPrimalObjectiveLimitReached() const ;
00253 bool isDualObjectiveLimitReached() const ;
00255 bool isIterationLimitReached() const { return problemStatus_==3; }
00257 inline double optimizationDirection() const {
00258 return optimizationDirection_;
00259 }
00260 inline double getObjSense() const { return optimizationDirection_; }
00261 void setOptimizationDirection(double value);
00263 inline double * primalRowSolution() const { return rowActivity_; }
00264 inline const double * getRowActivity() const { return rowActivity_; }
00266 inline double * primalColumnSolution() const { return columnActivity_; }
00267 inline const double * getColSolution() const { return columnActivity_; }
00268 inline void setColSolution(const double * input)
00269 { memcpy(columnActivity_,input,numberColumns_*sizeof(double));};
00271 inline double * dualRowSolution() const { return dual_; }
00272 inline const double * getRowPrice() const { return dual_; }
00274 inline double * dualColumnSolution() const { return reducedCost_; }
00275 inline const double * getReducedCost() const { return reducedCost_; }
00277 inline double* rowLower() const { return rowLower_; }
00278 inline const double* getRowLower() const { return rowLower_; }
00280 inline double* rowUpper() const { return rowUpper_; }
00281 inline const double* getRowUpper() const { return rowUpper_; }
00283 inline double * objective() const
00284 {
00285 if (objective_) {
00286 double offset;
00287 return objective_->gradient(NULL,offset,false);
00288 } else {
00289 return NULL;
00290 }
00291 }
00292 inline double * objective(const double * solution, double & offset,bool refresh=true) const
00293 {
00294 offset=0.0;
00295 if (objective_) {
00296 return objective_->gradient(solution,offset,refresh);
00297 } else {
00298 return NULL;
00299 }
00300 }
00301 inline const double * getObjCoefficients() const
00302 {
00303 if (objective_) {
00304 double offset;
00305 return objective_->gradient(NULL,offset,false);
00306 } else {
00307 return NULL;
00308 }
00309 }
00311 inline double * rowObjective() const { return rowObjective_; }
00312 inline const double * getRowObjCoefficients() const {
00313 return rowObjective_;
00314 }
00316 inline double * columnLower() const { return columnLower_; }
00317 inline const double * getColLower() const { return columnLower_; }
00319 inline double * columnUpper() const { return columnUpper_; }
00320 inline const double * getColUpper() const { return columnUpper_; }
00322 inline CoinPackedMatrix * matrix() const {
00323 if ( matrix_ == NULL ) return NULL;
00324 else return matrix_->getPackedMatrix();
00325 }
00327 inline int getNumElements() const
00328 { return matrix_->getNumElements();};
00331 inline double getSmallElementValue() const
00332 { return smallElement_;};
00333 inline void setSmallElementValue(double value)
00334 { smallElement_=value;};
00336 inline ClpMatrixBase * rowCopy() const { return rowCopy_; }
00338 inline ClpMatrixBase * clpMatrix() const { return matrix_; }
00342 void replaceMatrix(ClpMatrixBase * matrix);
00344 inline double objectiveValue() const {
00345 return objectiveValue_*optimizationDirection_ - dblParam_[ClpObjOffset];
00346 }
00347 inline double getObjValue() const {
00348 return objectiveValue_*optimizationDirection_ - dblParam_[ClpObjOffset];
00349 }
00351 inline char * integerInformation() const { return integerType_; }
00354 double * infeasibilityRay() const;
00355 double * unboundedRay() const;
00357 inline bool statusExists() const
00358 { return (status_!=NULL);};
00360 inline unsigned char * statusArray() const
00361 { return status_;};
00364 unsigned char * statusCopy() const;
00366 void copyinStatus(const unsigned char * statusArray);
00367
00369 inline void setUserPointer (void * pointer)
00370 { userPointer_=pointer;};
00371 inline void * getUserPointer () const
00372 { return userPointer_;};
00374
00376
00377 void passInMessageHandler(CoinMessageHandler * handler);
00379 void newLanguage(CoinMessages::Language language);
00380 void setLanguage(CoinMessages::Language language) { newLanguage(language); }
00382 CoinMessageHandler * messageHandler() const { return handler_; }
00384 CoinMessages messages() const { return messages_; }
00386 CoinMessages * messagesPointer() { return & messages_; }
00395 void setLogLevel(int value) { handler_->setLogLevel(value); }
00396 int logLevel() const { return handler_->logLevel(); }
00398 inline int lengthNames() const { return lengthNames_; }
00400 const std::vector<std::string> * rowNames() const {
00401 return &rowNames_;
00402 }
00403 const std::string& rowName(int iRow) const {
00404 return rowNames_[iRow];
00405 }
00407 const std::vector<std::string> * columnNames() const {
00408 return &columnNames_;
00409 }
00410 const std::string& columnName(int iColumn) const {
00411 return columnNames_[iColumn];
00412 }
00414 inline ClpObjective * objectiveAsObject() const
00415 { return objective_;};
00416 void setObjective(ClpObjective * objective);
00417 void setObjectivePointer(ClpObjective * objective)
00418 { objective_ = objective;};
00420
00421
00422
00440
00441 bool setIntParam(ClpIntParam key, int value) ;
00443 bool setDblParam(ClpDblParam key, double value) ;
00445 bool setStrParam(ClpStrParam key, const std::string & value);
00446
00447 inline bool getIntParam(ClpIntParam key, int& value) const {
00448 if (key!=ClpLastIntParam) {
00449 value = intParam_[key];
00450 return true;
00451 } else {
00452 return false;
00453 }
00454 }
00455
00456 inline bool getDblParam(ClpDblParam key, double& value) const {
00457 if (key!=ClpLastDblParam) {
00458 value = dblParam_[key];
00459 return true;
00460 } else {
00461 return false;
00462 }
00463 }
00464
00465 inline bool getStrParam(ClpStrParam key, std::string& value) const {
00466 if (key!=ClpLastStrParam) {
00467 value = strParam_[key];
00468 return true;
00469 } else {
00470 return false;
00471 }
00472 }
00474
00477 protected:
00479 void gutsOfDelete();
00482 void gutsOfCopy(const ClpModel & rhs, bool trueCopy=true);
00484 void getRowBound(int iRow, double& lower, double& upper) const;
00486 void gutsOfLoadModel ( int numberRows, int numberColumns,
00487 const double* collb, const double* colub,
00488 const double* obj,
00489 const double* rowlb, const double* rowub,
00490 const double * rowObjective=NULL);
00492
00493
00495 protected:
00496
00499
00500 double optimizationDirection_;
00502 double dblParam_[ClpLastDblParam];
00504 double objectiveValue_;
00506 double smallElement_;
00508 int numberRows_;
00510 int numberColumns_;
00512 double * rowActivity_;
00514 double * columnActivity_;
00516 double * dual_;
00518 double * reducedCost_;
00520 double* rowLower_;
00522 double* rowUpper_;
00524 ClpObjective * objective_;
00526 double * rowObjective_;
00528 double * columnLower_;
00530 double * columnUpper_;
00532 ClpMatrixBase * matrix_;
00534 ClpMatrixBase * rowCopy_;
00536 double * ray_;
00544 unsigned char * status_;
00546 char * integerType_;
00548 void * userPointer_;
00550 int intParam_[ClpLastIntParam];
00552 int numberIterations_;
00554 int solveType_;
00556 int problemStatus_;
00558 int secondaryStatus_;
00560 int lengthNames_;
00562 CoinMessageHandler * handler_;
00564 bool defaultHandler_;
00566 std::vector<std::string> rowNames_;
00568 std::vector<std::string> columnNames_;
00570 CoinMessages messages_;
00572 std::string strParam_[ClpLastStrParam];
00574 };
00576 class ClpDataSave {
00577
00578 public:
00582
00583 ClpDataSave ( );
00584
00586 ClpDataSave(const ClpDataSave &);
00588 ClpDataSave & operator=(const ClpDataSave & rhs);
00590 ~ClpDataSave ( );
00591
00593
00595 public:
00596
00599 double dualBound_;
00600 double infeasibilityCost_;
00601 int sparseThreshold_;
00602 int perturbation_;
00603
00605 };
00606
00607 #endif