ClpModel.hpp
Go to the documentation of this file.
1 /* $Id: ClpModel.hpp 1525 2010-02-26 17:27:59Z mjs $ */
2 // Copyright (C) 2002, International Business Machines
3 // Corporation and others. All Rights Reserved.
4 #ifndef ClpModel_H
5 #define ClpModel_H
6 
7 #include "ClpConfig.h"
8 
9 #include <iostream>
10 #include <cassert>
11 #include <cmath>
12 #include <vector>
13 #include <string>
14 //#ifndef COIN_USE_CLP
15 //#define COIN_USE_CLP
16 //#endif
17 #include "ClpPackedMatrix.hpp"
18 #include "CoinMessageHandler.hpp"
19 #include "CoinHelperFunctions.hpp"
20 #include "CoinFinite.hpp"
21 #include "ClpParameters.hpp"
22 #include "ClpObjective.hpp"
23 class ClpEventHandler;
33 class CoinBuild;
34 class CoinModel;
35 class ClpModel {
36 
37 public:
38 
44  ClpModel (bool emptyMessages = false );
46 
51  ClpModel(const ClpModel & rhs, int scalingMode = -1);
53  ClpModel & operator=(const ClpModel & rhs);
58  ClpModel (const ClpModel * wholeModel,
59  int numberRows, const int * whichRows,
60  int numberColumns, const int * whichColumns,
61  bool dropNames = true, bool dropIntegers = true);
63  ~ClpModel ( );
65 
79  void loadProblem ( const ClpMatrixBase& matrix,
80  const double* collb, const double* colub,
81  const double* obj,
82  const double* rowlb, const double* rowub,
83  const double * rowObjective = NULL);
84  void loadProblem ( const CoinPackedMatrix& matrix,
85  const double* collb, const double* colub,
86  const double* obj,
87  const double* rowlb, const double* rowub,
88  const double * rowObjective = NULL);
89 
92  void loadProblem ( const int numcols, const int numrows,
93  const CoinBigIndex* start, const int* index,
94  const double* value,
95  const double* collb, const double* colub,
96  const double* obj,
97  const double* rowlb, const double* rowub,
98  const double * rowObjective = NULL);
104  int loadProblem ( CoinModel & modelObject, bool tryPlusMinusOne = false);
106  void loadProblem ( const int numcols, const int numrows,
107  const CoinBigIndex* start, const int* index,
108  const double* value, const int * length,
109  const double* collb, const double* colub,
110  const double* obj,
111  const double* rowlb, const double* rowub,
112  const double * rowObjective = NULL);
114  void loadQuadraticObjective(const int numberColumns,
115  const CoinBigIndex * start,
116  const int * column, const double * element);
117  void loadQuadraticObjective ( const CoinPackedMatrix& matrix);
121  void setRowObjective(const double * rowObjective);
123  int readMps(const char *filename,
124  bool keepNames = false,
125  bool ignoreErrors = false);
127  int readGMPL(const char *filename, const char * dataName,
128  bool keepNames = false);
130  void copyInIntegerInformation(const char * information);
134  void setContinuous(int index);
136  void setInteger(int index);
138  bool isInteger(int index) const;
140  void resize (int newNumberRows, int newNumberColumns);
142  void deleteRows(int number, const int * which);
144  void addRow(int numberInRow, const int * columns,
145  const double * elements, double rowLower = -COIN_DBL_MAX,
146  double rowUpper = COIN_DBL_MAX);
148  void addRows(int number, const double * rowLower,
149  const double * rowUpper,
150  const CoinBigIndex * rowStarts, const int * columns,
151  const double * elements);
153  void addRows(int number, const double * rowLower,
154  const double * rowUpper,
155  const CoinBigIndex * rowStarts, const int * rowLengths,
156  const int * columns,
157  const double * elements);
158 #ifndef CLP_NO_VECTOR
159  void addRows(int number, const double * rowLower,
160  const double * rowUpper,
161  const CoinPackedVectorBase * const * rows);
162 #endif
163 
168  int addRows(const CoinBuild & buildObject, bool tryPlusMinusOne = false,
169  bool checkDuplicates = true);
178  int addRows(CoinModel & modelObject, bool tryPlusMinusOne = false,
179  bool checkDuplicates = true);
180 
182  void deleteColumns(int number, const int * which);
184  void addColumn(int numberInColumn,
185  const int * rows,
186  const double * elements,
187  double columnLower = 0.0,
188  double columnUpper = COIN_DBL_MAX,
189  double objective = 0.0);
191  void addColumns(int number, const double * columnLower,
192  const double * columnUpper,
193  const double * objective,
194  const CoinBigIndex * columnStarts, const int * rows,
195  const double * elements);
196  void addColumns(int number, const double * columnLower,
197  const double * columnUpper,
198  const double * objective,
199  const CoinBigIndex * columnStarts, const int * columnLengths,
200  const int * rows,
201  const double * elements);
202 #ifndef CLP_NO_VECTOR
203  void addColumns(int number, const double * columnLower,
204  const double * columnUpper,
205  const double * objective,
206  const CoinPackedVectorBase * const * columns);
207 #endif
208 
213  int addColumns(const CoinBuild & buildObject, bool tryPlusMinusOne = false,
214  bool checkDuplicates = true);
222  int addColumns(CoinModel & modelObject, bool tryPlusMinusOne = false,
223  bool checkDuplicates = true);
225  inline void modifyCoefficient(int row, int column, double newElement,
226  bool keepZero = false) {
227  matrix_->modifyCoefficient(row, column, newElement, keepZero);
228  }
230  void chgRowLower(const double * rowLower);
232  void chgRowUpper(const double * rowUpper);
234  void chgColumnLower(const double * columnLower);
236  void chgColumnUpper(const double * columnUpper);
238  void chgObjCoefficients(const double * objIn);
242  void borrowModel(ClpModel & otherModel);
245  void returnModel(ClpModel & otherModel);
246 
248  void createEmptyMatrix();
256  int cleanMatrix(double threshold = 1.0e-20);
258  void copy(const ClpMatrixBase * from, ClpMatrixBase * & to);
259 #ifndef CLP_NO_STD
260  void dropNames();
263  void copyNames(std::vector<std::string> & rowNames,
264  std::vector<std::string> & columnNames);
266  void copyRowNames(const std::vector<std::string> & rowNames, int first, int last);
268  void copyColumnNames(const std::vector<std::string> & columnNames, int first, int last);
270  void copyRowNames(const char * const * rowNames, int first, int last);
272  void copyColumnNames(const char * const * columnNames, int first, int last);
274  void setRowName(int rowIndex, std::string & name) ;
276  void setColumnName(int colIndex, std::string & name) ;
277 #endif
278 
285  int findNetwork(char * rotate, double fractionNeeded = 0.75);
288  CoinModel * createCoinModel() const;
289 
302  int writeMps(const char *filename,
303  int formatType = 0, int numberAcross = 2,
304  double objSense = 0.0) const ;
306 
308  inline int numberRows() const {
310  return numberRows_;
311  }
312  inline int getNumRows() const {
313  return numberRows_;
314  }
316  inline int getNumCols() const {
317  return numberColumns_;
318  }
319  inline int numberColumns() const {
320  return numberColumns_;
321  }
323  inline double primalTolerance() const {
325  }
326  void setPrimalTolerance( double value) ;
328  inline double dualTolerance() const {
329  return dblParam_[ClpDualTolerance];
330  }
331  void setDualTolerance( double value) ;
333  inline double primalObjectiveLimit() const {
335  }
336  void setPrimalObjectiveLimit(double value);
338  inline double dualObjectiveLimit() const {
340  }
341  void setDualObjectiveLimit(double value);
343  inline double objectiveOffset() const {
344  return dblParam_[ClpObjOffset];
345  }
346  void setObjectiveOffset(double value);
348  inline double presolveTolerance() const {
350  }
351 #ifndef CLP_NO_STD
352  inline std::string problemName() const {
353  return strParam_[ClpProbName];
354  }
355 #endif
356  inline int numberIterations() const {
358  return numberIterations_;
359  }
360  inline int getIterationCount() const {
361  return numberIterations_;
362  }
363  inline void setNumberIterations(int numberIterationsNew) {
364  numberIterations_ = numberIterationsNew;
365  }
367  inline int solveType() const {
368  return solveType_;
369  }
370  inline void setSolveType(int type) {
371  solveType_ = type;
372  }
374  inline int maximumIterations() const {
376  }
377  void setMaximumIterations(int value);
379  inline double maximumSeconds() const {
380  return dblParam_[ClpMaxSeconds];
381  }
382  void setMaximumSeconds(double value);
384  bool hitMaximumIterations() const;
394  inline int status() const {
395  return problemStatus_;
396  }
397  inline int problemStatus() const {
398  return problemStatus_;
399  }
401  inline void setProblemStatus(int problemStatusNew) {
402  problemStatus_ = problemStatusNew;
403  }
418  inline int secondaryStatus() const {
419  return secondaryStatus_;
420  }
421  inline void setSecondaryStatus(int newstatus) {
422  secondaryStatus_ = newstatus;
423  }
425  inline bool isAbandoned() const {
426  return problemStatus_ == 4;
427  }
429  inline bool isProvenOptimal() const {
430  return problemStatus_ == 0;
431  }
433  inline bool isProvenPrimalInfeasible() const {
434  return problemStatus_ == 1;
435  }
437  inline bool isProvenDualInfeasible() const {
438  return problemStatus_ == 2;
439  }
441  bool isPrimalObjectiveLimitReached() const ;
443  bool isDualObjectiveLimitReached() const ;
445  inline bool isIterationLimitReached() const {
446  return problemStatus_ == 3;
447  }
449  inline double optimizationDirection() const {
450  return optimizationDirection_;
451  }
452  inline double getObjSense() const {
453  return optimizationDirection_;
454  }
455  void setOptimizationDirection(double value);
457  inline double * primalRowSolution() const {
458  return rowActivity_;
459  }
460  inline const double * getRowActivity() const {
461  return rowActivity_;
462  }
464  inline double * primalColumnSolution() const {
465  return columnActivity_;
466  }
467  inline const double * getColSolution() const {
468  return columnActivity_;
469  }
470  inline void setColSolution(const double * input) {
471  memcpy(columnActivity_, input, numberColumns_ * sizeof(double));
472  }
474  inline double * dualRowSolution() const {
475  return dual_;
476  }
477  inline const double * getRowPrice() const {
478  return dual_;
479  }
481  inline double * dualColumnSolution() const {
482  return reducedCost_;
483  }
484  inline const double * getReducedCost() const {
485  return reducedCost_;
486  }
488  inline double* rowLower() const {
489  return rowLower_;
490  }
491  inline const double* getRowLower() const {
492  return rowLower_;
493  }
495  inline double* rowUpper() const {
496  return rowUpper_;
497  }
498  inline const double* getRowUpper() const {
499  return rowUpper_;
500  }
501  //-------------------------------------------------------------------------
505  void setObjectiveCoefficient( int elementIndex, double elementValue );
507  inline void setObjCoeff( int elementIndex, double elementValue ) {
508  setObjectiveCoefficient( elementIndex, elementValue);
509  }
510 
513  void setColumnLower( int elementIndex, double elementValue );
514 
517  void setColumnUpper( int elementIndex, double elementValue );
518 
520  void setColumnBounds( int elementIndex,
521  double lower, double upper );
522 
531  void setColumnSetBounds(const int* indexFirst,
532  const int* indexLast,
533  const double* boundList);
534 
537  inline void setColLower( int elementIndex, double elementValue ) {
538  setColumnLower(elementIndex, elementValue);
539  }
542  inline void setColUpper( int elementIndex, double elementValue ) {
543  setColumnUpper(elementIndex, elementValue);
544  }
545 
547  inline void setColBounds( int elementIndex,
548  double lower, double upper ) {
549  setColumnBounds(elementIndex, lower, upper);
550  }
551 
558  inline void setColSetBounds(const int* indexFirst,
559  const int* indexLast,
560  const double* boundList) {
561  setColumnSetBounds(indexFirst, indexLast, boundList);
562  }
563 
566  void setRowLower( int elementIndex, double elementValue );
567 
570  void setRowUpper( int elementIndex, double elementValue ) ;
571 
573  void setRowBounds( int elementIndex,
574  double lower, double upper ) ;
575 
582  void setRowSetBounds(const int* indexFirst,
583  const int* indexLast,
584  const double* boundList);
585 
587  inline const double * rowScale() const {
589  return rowScale_;
590  }
591  inline const double * columnScale() const {
592  return columnScale_;
593  }
594  inline const double * inverseRowScale() const {
595  return inverseRowScale_;
596  }
597  inline const double * inverseColumnScale() const {
598  return inverseColumnScale_;
599  }
600  inline double * mutableRowScale() const {
601  return rowScale_;
602  }
603  inline double * mutableColumnScale() const {
604  return columnScale_;
605  }
606  inline double * mutableInverseRowScale() const {
607  return inverseRowScale_;
608  }
609  inline double * mutableInverseColumnScale() const {
610  return inverseColumnScale_;
611  }
612  void setRowScale(double * scale) ;
613  void setColumnScale(double * scale);
615  inline double objectiveScale() const {
616  return objectiveScale_;
617  }
618  inline void setObjectiveScale(double value) {
619  objectiveScale_ = value;
620  }
622  inline double rhsScale() const {
623  return rhsScale_;
624  }
625  inline void setRhsScale(double value) {
626  rhsScale_ = value;
627  }
629  void scaling(int mode = 1);
632  void unscale();
634  inline int scalingFlag() const {
635  return scalingFlag_;
636  }
638  inline double * objective() const {
639  if (objective_) {
640  double offset;
641  return objective_->gradient(NULL, NULL, offset, false);
642  } else {
643  return NULL;
644  }
645  }
646  inline double * objective(const double * solution, double & offset, bool refresh = true) const {
647  offset = 0.0;
648  if (objective_) {
649  return objective_->gradient(NULL, solution, offset, refresh);
650  } else {
651  return NULL;
652  }
653  }
654  inline const double * getObjCoefficients() const {
655  if (objective_) {
656  double offset;
657  return objective_->gradient(NULL, NULL, offset, false);
658  } else {
659  return NULL;
660  }
661  }
663  inline double * rowObjective() const {
664  return rowObjective_;
665  }
666  inline const double * getRowObjCoefficients() const {
667  return rowObjective_;
668  }
670  inline double * columnLower() const {
671  return columnLower_;
672  }
673  inline const double * getColLower() const {
674  return columnLower_;
675  }
677  inline double * columnUpper() const {
678  return columnUpper_;
679  }
680  inline const double * getColUpper() const {
681  return columnUpper_;
682  }
684  inline CoinPackedMatrix * matrix() const {
685  if ( matrix_ == NULL ) return NULL;
686  else return matrix_->getPackedMatrix();
687  }
689  inline int getNumElements() const {
690  return matrix_->getNumElements();
691  }
694  inline double getSmallElementValue() const {
695  return smallElement_;
696  }
697  inline void setSmallElementValue(double value) {
698  smallElement_ = value;
699  }
701  inline ClpMatrixBase * rowCopy() const {
702  return rowCopy_;
703  }
705  void setNewRowCopy(ClpMatrixBase * newCopy);
707  inline ClpMatrixBase * clpMatrix() const {
708  return matrix_;
709  }
711  inline ClpPackedMatrix * clpScaledMatrix() const {
712  return scaledMatrix_;
713  }
715  inline void setClpScaledMatrix(ClpPackedMatrix * scaledMatrix) {
716  delete scaledMatrix_;
717  scaledMatrix_ = scaledMatrix;
718  }
724  void replaceMatrix(ClpMatrixBase * matrix, bool deleteCurrent = false);
730  inline void replaceMatrix(CoinPackedMatrix * newmatrix,
731  bool deleteCurrent = false) {
732  replaceMatrix(new ClpPackedMatrix(newmatrix), deleteCurrent);
733  }
735  inline double objectiveValue() const {
737  }
738  inline void setObjectiveValue(double value) {
740  }
741  inline double getObjValue() const {
743  }
745  inline char * integerInformation() const {
746  return integerType_;
747  }
750  double * infeasibilityRay() const;
751  double * unboundedRay() const;
753  inline bool statusExists() const {
754  return (status_ != NULL);
755  }
757  inline unsigned char * statusArray() const {
758  return status_;
759  }
762  unsigned char * statusCopy() const;
764  void copyinStatus(const unsigned char * statusArray);
765 
767  inline void setUserPointer (void * pointer) {
768  userPointer_ = pointer;
769  }
770  inline void * getUserPointer () const {
771  return userPointer_;
772  }
774  inline void setTrustedUserPointer (ClpTrustedData * pointer) {
775  trustedUserPointer_ = pointer;
776  }
778  return trustedUserPointer_;
779  }
781  inline int whatsChanged() const {
782  return whatsChanged_;
783  }
784  inline void setWhatsChanged(int value) {
785  whatsChanged_ = value;
786  }
788  inline int numberThreads() const {
789  return numberThreads_;
790  }
791  inline void setNumberThreads(int value) {
792  numberThreads_ = value;
793  }
795 
797  void passInMessageHandler(CoinMessageHandler * handler);
800  CoinMessageHandler * pushMessageHandler(CoinMessageHandler * handler,
801  bool & oldDefault);
803  void popMessageHandler(CoinMessageHandler * oldHandler, bool oldDefault);
805  void newLanguage(CoinMessages::Language language);
806  inline void setLanguage(CoinMessages::Language language) {
807  newLanguage(language);
808  }
810  inline CoinMessageHandler * messageHandler() const {
811  return handler_;
812  }
814  inline CoinMessages messages() const {
815  return messages_;
816  }
818  inline CoinMessages * messagesPointer() {
819  return & messages_;
820  }
822  inline CoinMessages coinMessages() const {
823  return coinMessages_;
824  }
826  inline CoinMessages * coinMessagesPointer() {
827  return & coinMessages_;
828  }
837  inline void setLogLevel(int value) {
838  handler_->setLogLevel(value);
839  }
840  inline int logLevel() const {
841  return handler_->logLevel();
842  }
844  inline bool defaultHandler() const {
845  return defaultHandler_;
846  }
850  inline ClpEventHandler * eventHandler() const {
851  return eventHandler_;
852  }
854  inline CoinThreadRandom * randomNumberGenerator() {
855  return &randomNumberGenerator_;
856  }
858  inline CoinThreadRandom & mutableRandomNumberGenerator() {
859  return randomNumberGenerator_;
860  }
862  inline void setRandomSeed(int value) {
863  randomNumberGenerator_.setSeed(value);
864  }
866  inline int lengthNames() const {
867  return lengthNames_;
868  }
869 #ifndef CLP_NO_STD
870  inline void setLengthNames(int value) {
872  lengthNames_ = value;
873  }
875  inline const std::vector<std::string> * rowNames() const {
876  return &rowNames_;
877  }
878  inline const std::string& rowName(int iRow) const {
879  return rowNames_[iRow];
880  }
882  std::string getRowName(int iRow) const;
884  inline const std::vector<std::string> * columnNames() const {
885  return &columnNames_;
886  }
887  inline const std::string& columnName(int iColumn) const {
888  return columnNames_[iColumn];
889  }
891  std::string getColumnName(int iColumn) const;
892 #endif
893  inline ClpObjective * objectiveAsObject() const {
895  return objective_;
896  }
897  void setObjective(ClpObjective * objective);
898  inline void setObjectivePointer(ClpObjective * newobjective) {
899  objective_ = newobjective;
900  }
903  int emptyProblem(int * infeasNumber = NULL, double * infeasSum = NULL, bool printMessage = true);
904 
906 
915  void times(double scalar,
916  const double * x, double * y) const;
920  void transposeTimes(double scalar,
921  const double * x, double * y) const ;
923 
924 
925  //---------------------------------------------------------------------------
943  bool setIntParam(ClpIntParam key, int value) ;
946  bool setDblParam(ClpDblParam key, double value) ;
947 #ifndef CLP_NO_STD
948  bool setStrParam(ClpStrParam key, const std::string & value);
950 #endif
951  // Get an integer parameter
952  inline bool getIntParam(ClpIntParam key, int& value) const {
953  if (key < ClpLastIntParam) {
954  value = intParam_[key];
955  return true;
956  } else {
957  return false;
958  }
959  }
960  // Get an double parameter
961  inline bool getDblParam(ClpDblParam key, double& value) const {
962  if (key < ClpLastDblParam) {
963  value = dblParam_[key];
964  return true;
965  } else {
966  return false;
967  }
968  }
969 #ifndef CLP_NO_STD
970  // Get a string parameter
971  inline bool getStrParam(ClpStrParam key, std::string& value) const {
972  if (key < ClpLastStrParam) {
973  value = strParam_[key];
974  return true;
975  } else {
976  return false;
977  }
978  }
979 #endif
980  void generateCpp( FILE * fp);
1014  inline unsigned int specialOptions() const {
1015  return specialOptions_;
1016  }
1017  void setSpecialOptions(unsigned int value);
1018 #define COIN_CBC_USING_CLP 0x01000000
1019  inline bool inCbcBranchAndBound() const {
1020  return (specialOptions_ & COIN_CBC_USING_CLP) != 0;
1021  }
1023 
1026 protected:
1028  void gutsOfDelete(int type);
1032  void gutsOfCopy(const ClpModel & rhs, int trueCopy = 1);
1034  void getRowBound(int iRow, double& lower, double& upper) const;
1036  void gutsOfLoadModel ( int numberRows, int numberColumns,
1037  const double* collb, const double* colub,
1038  const double* obj,
1039  const double* rowlb, const double* rowub,
1040  const double * rowObjective = NULL);
1042  void gutsOfScaling();
1044  inline double rawObjectiveValue() const {
1045  return objectiveValue_;
1046  }
1048  inline bool permanentArrays() const {
1049  return (specialOptions_ & 65536) != 0;
1050  }
1052  void startPermanentArrays();
1054  void stopPermanentArrays();
1056  const char * const * rowNamesAsChar() const;
1058  const char * const * columnNamesAsChar() const;
1060  void deleteNamesAsChar(const char * const * names, int number) const;
1062  void onStopped();
1064 
1065 
1067 protected:
1068 
1071  double optimizationDirection_;
1082  double rhsScale_;
1088  double * rowActivity_;
1092  double * dual_;
1094  double * reducedCost_;
1096  double* rowLower_;
1098  double* rowUpper_;
1102  double * rowObjective_;
1104  double * columnLower_;
1106  double * columnUpper_;
1114  double * ray_;
1116  double * rowScale_;
1118  double * columnScale_;
1133  unsigned char * status_;
1162  unsigned int whatsChanged_;
1174  unsigned int specialOptions_;
1176  CoinMessageHandler * handler_;
1180  CoinThreadRandom randomNumberGenerator_;
1183 #ifndef CLP_NO_STD
1184  std::vector<std::string> rowNames_;
1187  std::vector<std::string> columnNames_;
1188 #endif
1189  CoinMessages messages_;
1192  CoinMessages coinMessages_;
1202  CoinPackedMatrix baseMatrix_;
1204  CoinPackedMatrix baseRowCopy_;
1206  double * savedRowScale_;
1209 #ifndef CLP_NO_STD
1210  std::string strParam_[ClpLastStrParam];
1212 #endif
1213 
1214 };
1218 
1219 public:
1223  ClpDataSave ( );
1225 
1227  ClpDataSave(const ClpDataSave &);
1229  ClpDataSave & operator=(const ClpDataSave & rhs);
1231  ~ClpDataSave ( );
1232 
1234 
1236 public:
1237 
1240  double dualBound_;
1251  unsigned int specialOptions_;
1253 };
1254 
1255 #endif