OsiGlpkSolverInterface.hpp
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2 // name: OSI Interface for GLPK
3 //-----------------------------------------------------------------------------
4 // Copyright (C) 2001, Vivian De Smedt, Braden Hunsaker
5 // Copyright (C) 2003 University of Pittsburgh
6 // University of Pittsburgh coding done by Brady Hunsaker
7 // All Rights Reserved.
8 
9 #ifndef OsiGlpkSolverInterface_H
10 #define OsiGlpkSolverInterface_H
11 
12 #include <string>
13 extern "C" {
14 #include "glpk.h"
15 }
16 #include "OsiSolverInterface.hpp"
17 #include "CoinPackedMatrix.hpp"
18 #include "CoinWarmStartBasis.hpp"
19 
26  friend int OsiGlpkSolverInterfaceUnitTest(const std::string & mpsDir, const std::string & netlibDir);
27 
28 public:
29 
30  //---------------------------------------------------------------------------
33 
34  virtual void initialSolve();
35 
37  virtual void resolve();
38 
40  virtual void branchAndBound();
42 
43  //---------------------------------------------------------------------------
59  // Set an integer parameter
60  bool setIntParam(OsiIntParam key, int value);
61  // Set an double parameter
62  bool setDblParam(OsiDblParam key, double value);
63  // Set a string parameter
64  bool setStrParam(OsiStrParam key, const std::string & value);
65  // Set a hint parameter
66  bool setHintParam(OsiHintParam key, bool sense = true,
67  OsiHintStrength strength = OsiHintTry, void *info = 0) ;
68  // Get an integer parameter
69  bool getIntParam(OsiIntParam key, int& value) const;
70  // Get an double parameter
71  bool getDblParam(OsiDblParam key, double& value) const;
72  // Get a string parameter
73  bool getStrParam(OsiStrParam key, std::string& value) const;
75 
76  //---------------------------------------------------------------------------
78 
79 
80  virtual bool isAbandoned() const;
82  virtual bool isProvenOptimal() const;
84  virtual bool isProvenPrimalInfeasible() const;
86  virtual bool isProvenDualInfeasible() const;
88  virtual bool isPrimalObjectiveLimitReached() const;
90  virtual bool isDualObjectiveLimitReached() const;
92  virtual bool isIterationLimitReached() const;
94  virtual bool isTimeLimitReached() const;
96  virtual bool isFeasible() const;
98 
99  //---------------------------------------------------------------------------
108  inline CoinWarmStart *getEmptyWarmStart () const
109  { return (dynamic_cast<CoinWarmStart *>(new CoinWarmStartBasis())) ; }
111  virtual CoinWarmStart* getWarmStart() const;
114  virtual bool setWarmStart(const CoinWarmStart* warmstart);
116 
117  //---------------------------------------------------------------------------
124 
125  virtual void markHotStart();
127  virtual void solveFromHotStart();
129  virtual void unmarkHotStart();
131 
132  //---------------------------------------------------------------------------
147 
148  virtual int getNumCols() const;
149 
151  virtual int getNumRows() const;
152 
154  virtual int getNumElements() const;
155 
157  virtual const double * getColLower() const;
158 
160  virtual const double * getColUpper() const;
161 
171  virtual const char * getRowSense() const;
172 
181  virtual const double * getRightHandSide() const;
182 
191  virtual const double * getRowRange() const;
192 
194  virtual const double * getRowLower() const;
195 
197  virtual const double * getRowUpper() const;
198 
200  virtual const double * getObjCoefficients() const;
201 
203  virtual double getObjSense() const;
204 
206  virtual bool isContinuous(int colNumber) const;
207 
208 #if 0
209 
210  virtual bool isBinary(int columnNumber) const;
211 
216  virtual bool isInteger(int columnNumber) const;
217 
219  virtual bool isIntegerNonBinary(int columnNumber) const;
220 
222  virtual bool isFreeBinary(int columnNumber) const;
223 #endif
224 
226  virtual const CoinPackedMatrix * getMatrixByRow() const;
227 
229  virtual const CoinPackedMatrix * getMatrixByCol() const;
230 
232  virtual double getInfinity() const;
234 
237 
238  virtual const double * getColSolution() const;
239 
241  virtual const double * getRowPrice() const;
242 
244  virtual const double * getReducedCost() const;
245 
248  virtual const double * getRowActivity() const;
249 
251  virtual double getObjValue() const;
252 
255  virtual int getIterationCount() const;
256 
268  virtual std::vector<double*> getDualRays(int maxNumRays) const;
280  virtual std::vector<double*> getPrimalRays(int maxNumRays) const;
281 
282 #if 0
283 
285  virtual OsiVectorInt getFractionalIndices(const double etol=1.e-05)
286  const;
287 #endif
288 
289 
290 
291  //---------------------------------------------------------------------------
292 
295  //-------------------------------------------------------------------------
299  virtual void setObjCoeff( int elementIndex, double elementValue );
300 
304  virtual void setColLower( int elementIndex, double elementValue );
305 
309  virtual void setColUpper( int elementIndex, double elementValue );
310 
314  virtual void setColBounds( int elementIndex,
315  double lower, double upper );
316 
325  virtual void setColSetBounds(const int* indexFirst,
326  const int* indexLast,
327  const double* boundList);
328 
331  virtual void setRowLower( int elementIndex, double elementValue );
332 
335  virtual void setRowUpper( int elementIndex, double elementValue );
336 
340  virtual void setRowBounds( int elementIndex,
341  double lower, double upper );
342 
344  virtual void setRowType(int index, char sense, double rightHandSide,
345  double range);
346 
355  virtual void setRowSetBounds(const int* indexFirst,
356  const int* indexLast,
357  const double* boundList);
358 
369  virtual void setRowSetTypes(const int* indexFirst,
370  const int* indexLast,
371  const char* senseList,
372  const double* rhsList,
373  const double* rangeList);
375 
376  //-------------------------------------------------------------------------
380  virtual void setContinuous(int index);
382  virtual void setInteger(int index);
385  virtual void setContinuous(const int* indices, int len);
388  virtual void setInteger(const int* indices, int len);
390 
391  //-------------------------------------------------------------------------
393  virtual void setObjSense(double s);
394 
405  virtual void setColSolution(const double * colsol);
406 
417  virtual void setRowPrice(const double * rowprice);
418 
419  //-------------------------------------------------------------------------
424 
427  virtual void addCol(const CoinPackedVectorBase& vec,
428  const double collb, const double colub,
429  const double obj);
430 
433  virtual void addCols(const int numcols,
434  const CoinPackedVectorBase * const * cols,
435  const double* collb, const double* colub,
436  const double* obj);
438  virtual void deleteCols(const int num, const int * colIndices);
439 
442  virtual void addRow(const CoinPackedVectorBase& vec,
443  const double rowlb, const double rowub);
445  virtual void addRow(const CoinPackedVectorBase& vec,
446  const char rowsen, const double rowrhs,
447  const double rowrng);
448 
451  virtual void addRows(const int numrows,
452  const CoinPackedVectorBase * const * rows,
453  const double* rowlb, const double* rowub);
455  virtual void addRows(const int numrows,
456  const CoinPackedVectorBase * const * rows,
457  const char* rowsen, const double* rowrhs,
458  const double* rowrng);
460  virtual void deleteRows(const int num, const int * rowIndices);
461 
462 #if 0
463  // ??? implemented in OsiSolverInterface
464  //-----------------------------------------------------------------------
486  virtual ApplyCutsReturnCode applyCuts(const OsiCuts & cs,
487  double effectivenessLb = 0.0);
488 #endif
489 
490 
491 
492  //---------------------------------------------------------------------------
493 
507  virtual void loadProblem(const CoinPackedMatrix& matrix,
508  const double* collb, const double* colub,
509  const double* obj,
510  const double* rowlb, const double* rowub);
511 
519  virtual void assignProblem(CoinPackedMatrix*& matrix,
520  double*& collb, double*& colub, double*& obj,
521  double*& rowlb, double*& rowub);
522 
535  virtual void loadProblem(const CoinPackedMatrix& matrix,
536  const double* collb, const double* colub,
537  const double* obj,
538  const char* rowsen, const double* rowrhs,
539  const double* rowrng);
540 
548  virtual void assignProblem(CoinPackedMatrix*& matrix,
549  double*& collb, double*& colub, double*& obj,
550  char*& rowsen, double*& rowrhs,
551  double*& rowrng);
552 
555  virtual void loadProblem(const int numcols, const int numrows,
556  const int* start, const int* index,
557  const double* value,
558  const double* collb, const double* colub,
559  const double* obj,
560  const double* rowlb, const double* rowub);
561 
564  virtual void loadProblem(const int numcols, const int numrows,
565  const int* start, const int* index,
566  const double* value,
567  const double* collb, const double* colub,
568  const double* obj,
569  const char* rowsen, const double* rowrhs,
570  const double* rowrng);
571 
574  virtual int readMps(const char *filename,
575  const char *extension = "mps");
576 
581  virtual void writeMps(const char *filename,
582  const char *extension = "mps",
583  double objSense=0.0) const;
585 
586  //---------------------------------------------------------------------------
587 
594 
597  void setObjName (std::string name) ;
598 
604  void setRowName(int ndx, std::string name) ;
605 
611  void setColName(int ndx, std::string name) ;
612 
614 
615  //---------------------------------------------------------------------------
616 
620  {
643  };
644 
646  LPX * getModelPtr();
647 
649 
659 
667  static void decrementInstanceCounter();
668 
670  static unsigned int getNumInstances() { return numInstances_; }
672 
673 
676 
678 
680  virtual OsiSolverInterface * clone(bool copyData = true) const;
681 
684 
687 
689  virtual ~OsiGlpkSolverInterface();
690 
692  virtual void reset();
694 
695 protected:
696 
699 
700  virtual void applyRowCut( const OsiRowCut & rc );
701 
705  virtual void applyColCut( const OsiColCut & cc );
706 
708  LPX * getMutableModelPtr() const;
709 
711 
712 private:
715 
717  void gutsOfCopy( const OsiGlpkSolverInterface & source );
718 
720  void gutsOfConstructor();
721 
723  void gutsOfDestructor();
724 
726  void freeCachedColRim();
727 
729  void freeCachedRowRim();
730 
732  void freeCachedResults();
733 
735  void freeCachedMatrix();
736 
738  void freeCachedData( int keepCached = KEEPCACHED_NONE );
739 
741  void freeAllMemory();
742 
744  void printBounds();
745 
747  void fillColBounds() const;
749 
750 
753 
754  mutable LPX* lp_;
755 
757  static unsigned int numInstances_;
758 
759 
760  // Remember whether simplex or b&b was most recently done
761  // 0 = simplex; 1 = b&b
763 
764  // Int parameters.
771 
772  // Double parameters.
782  double objOffset_;
783 
784  // String parameters
786  std::string probName_;
787 
789  mutable void *info_[OsiLastHintParam] ;
790 
791 
793 
799  double *hotStartCVal_;
802 
808  double *hotStartRVal_;
811 
812  // Status information
837 
840 
842  mutable int iter_used_;
843 
845  mutable double *obj_;
846 
848  mutable double *collower_;
849 
851  mutable double *colupper_;
852 
854  mutable char *ctype_;
855 
857  mutable char *rowsense_;
858 
860  mutable double *rhs_;
861 
863  mutable double *rowrange_;
864 
866  mutable double *rowlower_;
867 
869  mutable double *rowupper_;
870 
872  mutable double *colsol_;
873 
875  mutable double *rowsol_;
876 
878  mutable double *redcost_;
879 
881  mutable double *rowact_;
882 
884  mutable CoinPackedMatrix *matrixByRow_;
885 
887  mutable CoinPackedMatrix *matrixByCol_;
889 
890 };
891 
892 //#############################################################################
898 int OsiGlpkSolverInterfaceUnitTest(const std::string & mpsDir, const std::string & netlibDir);
899 
900 #endif // OsiGlpkSolverInterface_H