OsiGrbSolverInterface.hpp
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2 // name: OSI Interface for Gurobi
3 // template: OSI Cplex Interface written by T. Achterberg
4 // author: Stefan Vigerske
5 // Humboldt University Berlin
6 // date: 09/02/2009
7 // license: this file may be freely distributed under the terms of CPL
8 // comments: please scan this file for '???' and read the comments
9 //-----------------------------------------------------------------------------
10 // Copyright (C) 2009 Humboldt University Berlin and others.
11 // All Rights Reserved.
12 
13 // $Id: OsiGrbSolverInterface.hpp 1445 2010-01-09 20:33:43Z stefan $
14 
15 #ifndef OsiGrbSolverInterface_H
16 #define OsiGrbSolverInterface_H
17 
18 #include <string>
19 #include "OsiSolverInterface.hpp"
20 
21 typedef struct _GRBmodel GRBmodel;
22 typedef struct _GRBenv GRBenv;
23 
29 class OsiGrbSolverInterface : virtual public OsiSolverInterface {
30  friend void OsiGrbSolverInterfaceUnitTest(const std::string & mpsDir, const std::string & netlibDir);
31 
32 public:
33 
34  //---------------------------------------------------------------------------
37  virtual void initialSolve();
39 
41  virtual void resolve();
42 
44  virtual void branchAndBound();
46 
47  //---------------------------------------------------------------------------
63  // Set an integer parameter
64  bool setIntParam(OsiIntParam key, int value);
65  // Set an double parameter
66  bool setDblParam(OsiDblParam key, double value);
67  // Set a string parameter
68  bool setStrParam(OsiStrParam key, const std::string & value);
69  // Set a hint parameter
70  bool setHintParam(OsiHintParam key, bool yesNo = true, OsiHintStrength strength = OsiHintTry, void* = NULL);
71  // Get an integer parameter
72  bool getIntParam(OsiIntParam key, int& value) const;
73  // Get an double parameter
74  bool getDblParam(OsiDblParam key, double& value) const;
75  // Get a string parameter
76  bool getStrParam(OsiStrParam key, std::string& value) const;
77  // Get a hint parameter
78  bool getHintParam(OsiHintParam key, bool& yesNo, OsiHintStrength& strength, void*& otherInformation) const;
79  // Get a hint parameter
80  bool getHintParam(OsiHintParam key, bool& yesNo, OsiHintStrength& strength) const;
81  // Get a hint parameter
82  bool getHintParam(OsiHintParam key, bool& yesNo) const;
84 
85  //---------------------------------------------------------------------------
87 
88  virtual bool isAbandoned() const;
91  virtual bool isProvenOptimal() const;
93  virtual bool isProvenPrimalInfeasible() const;
95  virtual bool isProvenDualInfeasible() const;
97  virtual bool isPrimalObjectiveLimitReached() const;
99  virtual bool isDualObjectiveLimitReached() const;
101  virtual bool isIterationLimitReached() const;
103 
104  //---------------------------------------------------------------------------
107 
114  CoinWarmStart* getEmptyWarmStart() const;
115 
117  virtual CoinWarmStart* getWarmStart() const;
120  virtual bool setWarmStart(const CoinWarmStart* warmstart);
122 
123  //---------------------------------------------------------------------------
130  virtual void markHotStart();
133  virtual void solveFromHotStart();
135  virtual void unmarkHotStart();
137 
138  //---------------------------------------------------------------------------
153  virtual int getNumCols() const;
155 
157  virtual int getNumRows() const;
158 
160  virtual int getNumElements() const;
161 
163  virtual const double * getColLower() const;
164 
166  virtual const double * getColUpper() const;
167 
177  virtual const char * getRowSense() const;
178 
187  virtual const double * getRightHandSide() const;
188 
197  virtual const double * getRowRange() const;
198 
200  virtual const double * getRowLower() const;
201 
203  virtual const double * getRowUpper() const;
204 
206  virtual const double * getObjCoefficients() const;
207 
209  virtual double getObjSense() const;
210 
212  virtual bool isContinuous(int colNumber) const;
213 
215  virtual const CoinPackedMatrix * getMatrixByRow() const;
216 
218  virtual const CoinPackedMatrix * getMatrixByCol() const;
219 
221  virtual double getInfinity() const;
223 
226  virtual const double * getColSolution() const;
228 
230  virtual const double * getRowPrice() const;
231 
233  virtual const double * getReducedCost() const;
234 
237  virtual const double * getRowActivity() const;
238 
240  virtual double getObjValue() const;
241 
244  virtual int getIterationCount() const;
245 
257  virtual std::vector<double*> getDualRays(int maxNumRays) const;
269  virtual std::vector<double*> getPrimalRays(int maxNumRays) const;
271 
272 
273  //---------------------------------------------------------------------------
274 
277  //-------------------------------------------------------------------------
281  virtual void setObjCoeff(int elementIndex, double elementValue);
282 
284  virtual void setObjCoeffSet(const int* indexFirst, const int* indexLast,
285  const double* coeffList);
286 
290  virtual void setColLower(int elementIndex, double elementValue);
291 
295  virtual void setColUpper(int elementIndex, double elementValue);
296 
300  virtual void setColBounds(int elementIndex, double lower, double upper);
301 
309  virtual void setColSetBounds(const int* indexFirst, const int* indexLast,
310  const double* boundList);
311 
314  virtual void setRowLower(int elementIndex, double elementValue);
315 
318  virtual void setRowUpper(int elementIndex, double elementValue);
319 
323  virtual void setRowBounds(int elementIndex, double lower, double upper);
324 
326  virtual void setRowType(int index, char sense, double rightHandSide,
327  double range);
328 
336  virtual void setRowSetBounds(const int* indexFirst, const int* indexLast,
337  const double* boundList);
338 
348  virtual void setRowSetTypes(const int* indexFirst, const int* indexLast,
349  const char* senseList, const double* rhsList, const double* rangeList);
351 
352  //-------------------------------------------------------------------------
356  virtual void setContinuous(int index);
358  virtual void setInteger(int index);
361  virtual void setContinuous(const int* indices, int len);
364  virtual void setInteger(const int* indices, int len);
366 
367  //-------------------------------------------------------------------------
371  virtual void setRowName(int ndx, std::string name) ;
372 
374  virtual void setColName(int ndx, std::string name) ;
376 
377  //-------------------------------------------------------------------------
379  virtual void setObjSense(double s);
380 
391  virtual void setColSolution(const double * colsol);
392 
403  virtual void setRowPrice(const double * rowprice);
404 
405  //-------------------------------------------------------------------------
412  virtual void addCol(const CoinPackedVectorBase& vec, const double collb,
413  const double colub, const double obj);
414 
417  virtual void addCols(const int numcols,
418  const CoinPackedVectorBase * const * cols, const double* collb,
419  const double* colub, const double* obj);
421  virtual void deleteCols(const int num, const int * colIndices);
422 
425  virtual void addRow(const CoinPackedVectorBase& vec, const double rowlb,
426  const double rowub);
428  virtual void addRow(const CoinPackedVectorBase& vec, const char rowsen,
429  const double rowrhs, const double rowrng);
430 
433  virtual void addRows(const int numrows,
434  const CoinPackedVectorBase * const * rows, const double* rowlb,
435  const double* rowub);
437  virtual void addRows(const int numrows,
438  const CoinPackedVectorBase * const * rows, const char* rowsen,
439  const double* rowrhs, const double* rowrng);
441  virtual void deleteRows(const int num, const int * rowIndices);
443 
444 
445  //---------------------------------------------------------------------------
446 
460  virtual void loadProblem(const CoinPackedMatrix& matrix, const double* collb,
461  const double* colub, const double* obj, const double* rowlb,
462  const double* rowub);
463 
471  virtual void assignProblem(CoinPackedMatrix*& matrix, double*& collb,
472  double*& colub, double*& obj, double*& rowlb, double*& rowub);
473 
486  virtual void loadProblem(const CoinPackedMatrix& matrix, const double* collb,
487  const double* colub, const double* obj, const char* rowsen,
488  const double* rowrhs, const double* rowrng);
489 
497  virtual void assignProblem(CoinPackedMatrix*& matrix, double*& collb,
498  double*& colub, double*& obj, char*& rowsen, double*& rowrhs,
499  double*& rowrng);
500 
503  virtual void loadProblem(const int numcols, const int numrows,
504  const int* start, const int* index, const double* value,
505  const double* collb, const double* colub, const double* obj,
506  const double* rowlb, const double* rowub);
507 
510  virtual void loadProblem(const int numcols, const int numrows,
511  const int* start, const int* index, const double* value,
512  const double* collb, const double* colub, const double* obj,
513  const char* rowsen, const double* rowrhs, const double* rowrng);
514 
517  virtual int readMps(const char *filename, const char *extension = "mps");
518 
523  virtual void writeMps(const char *filename, const char *extension = "mps",
524  double objSense=0.0) const;
525 
527 
528  //---------------------------------------------------------------------------
529 
536  {
559  };
560 
561  GRBmodel* getLpPtr(int keepCached = KEEPCACHED_NONE);
562 
564  GRBenv* getEnvironmentPtr() const;
566 
568  bool isDemoLicense() const;
570 
572  const char* getCtype() const;
573 
583  static void incrementInstanceCounter();
584 
593  static void decrementInstanceCounter();
594 
596  static void setEnvironment(GRBenv* globalenv);
597 
599  static unsigned int getNumInstances();
601 
602 
605  OsiGrbSolverInterface(bool use_local_env = false);
607 
609  OsiGrbSolverInterface(GRBenv* localgrbenv);
610 
612  virtual OsiSolverInterface * clone(bool copyData = true) const;
613 
616 
619 
621  virtual ~OsiGrbSolverInterface();
622 
624  virtual void reset();
626 
627  /***************************************************************************/
644 
648  virtual int canDoSimplexInterface() const;
649 
654  virtual void enableSimplexInterface(int doingPrimal) {
655  }
656  ;
657 
661  virtual void disableSimplexInterface() {
662  }
663  ;
664 
668  virtual void enableFactorization() const {
669  }
670  ;
671 
675  virtual void disableFactorization() const {
676  }
677  ;
678 
680  virtual bool basisIsAvailable() const;
681 
685  virtual void getBasisStatus(int* cstat, int* rstat) const;
686 
687  // ///Get a row of the tableau (slack part in slack if not NULL)
688  // virtual void getBInvARow(int row, double* z, double * slack=NULL) const;
689  //
690  // ///Get a row of the basis inverse
691  // virtual void getBInvRow(int row, double* z) const;
692  //
693  // ///Get a column of the tableau
694  // virtual void getBInvACol(int col, double* vec) const;
695  //
696  // ///Get a column of the basis inverse
697  // virtual void getBInvCol(int col, double* vec) const;
698  //
699  // /** Get indices of the pivot variable in each row
700  // (order of indices corresponds to the
701  // order of elements in a vector retured by getBInvACol() and
702  // getBInvCol()).
703  // */
704  // virtual void getBasics(int* index) const;
706  void switchToLP();
707 
709  void switchToMIP();
710 
712  /***************************************************************************/
713 
714 protected:
715 
718  virtual void applyRowCut(const OsiRowCut & rc);
720 
724  virtual void applyColCut(const OsiColCut & cc);
726 
727 private:
730  void resizeColSpace(int minsize);
732 
734  void freeColSpace();
735 
737  void resizeAuxColSpace(int minsize);
738 
740  void resizeAuxColIndSpace();
742 
745  static GRBenv* globalenv_;
748  static bool globalenv_is_ours;
750  static unsigned int numInstances_;
752 
755 
757  GRBmodel* getMutableLpPtr() const;
758 
760  void gutsOfCopy(const OsiGrbSolverInterface & source);
761 
763  void gutsOfConstructor();
764 
766  void gutsOfDestructor();
767 
769  void freeCachedColRim();
770 
772  void freeCachedRowRim();
773 
775  void freeCachedResults();
776 
778  void freeCachedMatrix();
779 
781  void freeCachedData(int keepCached = KEEPCACHED_NONE);
782 
784  void freeAllMemory();
785 
787  void convertToRangedRow(int rowidx, double rhs, double range);
788 
790  void convertToNormalRow(int rowidx, char sense, double rhs);
792 
793 
796  mutable GRBenv* localenv_;
798 
800  mutable GRBmodel* lp_;
801 
808 
811 
814  mutable double *obj_;
816 
818  mutable double *collower_;
819 
821  mutable double *colupper_;
822 
824  mutable char *rowsense_;
825 
827  mutable double *rhs_;
828 
830  mutable double *rowrange_;
831 
833  mutable double *rowlower_;
834 
836  mutable double *rowupper_;
837 
839  mutable double *colsol_;
840 
842  mutable double *rowsol_;
843 
845  mutable double *redcost_;
846 
848  mutable double *rowact_;
849 
851  mutable CoinPackedMatrix *matrixByRow_;
852 
854  mutable CoinPackedMatrix *matrixByCol_;
856 
859  mutable bool probtypemip_;
861 
864 
866  char *coltype_;
867 
869  int nauxcols;
870 
873 
877 
883 
886 
890  int* auxcolind;
892 };
893 
894 //#############################################################################
900 void OsiGrbSolverInterfaceUnitTest(const std::string & mpsDir, const std::string & netlibDir);
901 
902 #endif