OsiVolSolverInterface.hpp
Go to the documentation of this file.
1 // Copyright (C) 2000, International Business Machines
2 // Corporation and others. All Rights Reserved.
3 
4 #ifndef OsiVolSolverInterface_H
5 #define OsiVolSolverInterface_H
6 
7 #include <string>
8 
9 #include "VolVolume.hpp"
10 
11 #include "CoinPackedMatrix.hpp"
12 
13 #include "OsiSolverInterface.hpp"
14 
15 static const double OsiVolInfinity = 1.0e31;
16 
17 //#############################################################################
18 
25  virtual public OsiSolverInterface, public VOL_user_hooks {
26  friend int OsiVolSolverInterfaceUnitTest(const std::string & mpsDir, const std::string & netlibDir);
27 
28 private:
30  int majorDim_;
31  int minorDim_;
32 
33  int plusSize_;
34  int * plusInd_;
35  int * plusStart_;
36  int * plusLength_;
37 
39  int * minusInd_;
40  int * minusStart_;
41  int * minusLength_;
42 
43  public:
44  OsiVolMatrixOneMinusOne_(const CoinPackedMatrix& m);
46  void timesMajor(const double* x, double* y) const;
47  };
48 
49 public:
50  //---------------------------------------------------------------------------
53  virtual void initialSolve();
55 
57  virtual void resolve();
58 
60  virtual void branchAndBound() {
61  throw CoinError("Sorry, the Volume Algorithm doesn't implement B&B",
62  "branchAndBound", "OsiVolSolverInterface");
63  }
65 
66  //---------------------------------------------------------------------------
82  // Set an integer parameter
83  bool setIntParam(OsiIntParam key, int value);
84  // Set an double parameter
85  bool setDblParam(OsiDblParam key, double value);
86  // Set a string parameter
87  bool setStrParam(OsiStrParam key, const std::string & value);
88  // Get an integer parameter
89  bool getIntParam(OsiIntParam key, int& value) const;
90  // Get an double parameter
91  bool getDblParam(OsiDblParam key, double& value) const;
92  // Get a string parameter
93  bool getStrParam(OsiStrParam key, std::string& value) const;
95 
96  //---------------------------------------------------------------------------
98 
99  virtual bool isAbandoned() const;
102  virtual bool isProvenOptimal() const;
104  virtual bool isProvenPrimalInfeasible() const;
106  virtual bool isProvenDualInfeasible() const;
108  virtual bool isPrimalObjectiveLimitReached() const;
110  virtual bool isDualObjectiveLimitReached() const;
112  virtual bool isIterationLimitReached() const;
114 
115  //---------------------------------------------------------------------------
124  virtual CoinWarmStart *getEmptyWarmStart () const ;
125 
127  virtual CoinWarmStart* getWarmStart() const;
130  virtual bool setWarmStart(const CoinWarmStart* warmstart);
132 
133  //---------------------------------------------------------------------------
140  virtual void markHotStart();
143  virtual void solveFromHotStart();
145  virtual void unmarkHotStart();
147 
148  //---------------------------------------------------------------------------
163  virtual int getNumCols() const {
165  return rowMatrixCurrent_?
166  rowMatrix_.getNumCols() : colMatrix_.getNumCols(); }
167 
169  virtual int getNumRows() const {
170  return rowMatrixCurrent_?
171  rowMatrix_.getNumRows() : colMatrix_.getNumRows(); }
172 
174  virtual int getNumElements() const {
175  return rowMatrixCurrent_?
176  rowMatrix_.getNumElements() : colMatrix_.getNumElements(); }
177 
179  virtual const double * getColLower() const { return collower_; }
180 
182  virtual const double * getColUpper() const { return colupper_; }
183 
193  virtual const char * getRowSense() const { return rowsense_; }
194 
203  virtual const double * getRightHandSide() const { return rhs_; }
204 
213  virtual const double * getRowRange() const { return rowrange_; }
214 
216  virtual const double * getRowLower() const { return rowlower_; }
217 
219  virtual const double * getRowUpper() const { return rowupper_; }
220 
222  virtual const double * getObjCoefficients() const { return objcoeffs_; }
223 
225  virtual double getObjSense() const { return objsense_; }
226 
228  virtual bool isContinuous(int colNumber) const;
229 
230 #if 0
231  virtual bool isBinary(int colNumber) const;
233 
238  virtual bool isInteger(int colNumber) const;
239 
241  virtual bool isIntegerNonBinary(int colNumber) const;
242 
244  virtual bool isFreeBinary(int colNumber) const;
245 #endif
246 
248  virtual const CoinPackedMatrix * getMatrixByRow() const;
249 
251  virtual const CoinPackedMatrix * getMatrixByCol() const;
252 
254  virtual double getInfinity() const { return OsiVolInfinity; }
256 
259  virtual const double * getColSolution() const { return colsol_; }
261 
263  virtual const double * getRowPrice() const { return rowprice_; }
264 
266  virtual const double * getReducedCost() const { return rc_; }
267 
270  virtual const double * getRowActivity() const { return lhs_; }
271 
273  virtual double getObjValue() const {
274 #if 1
275  // This does not pass unitTest if getObjValue is called before solve
276  return lagrangeanCost_;
277 #else
279 #endif
280  }
281 
284  virtual int getIterationCount() const { return volprob_.iter(); }
285 
297  virtual std::vector<double*> getDualRays(int maxNumRays) const;
309  virtual std::vector<double*> getPrimalRays(int maxNumRays) const;
310 
311 #if 0
312 
314  virtual OsiVectorInt getFractionalIndices(const double etol=1.e-05)
315  const;
316 #endif
317 
318 
319 
320  //---------------------------------------------------------------------------
321 
324  //-------------------------------------------------------------------------
328  virtual void setObjCoeff( int elementIndex, double elementValue ) {
329  objcoeffs_[elementIndex] = elementValue;
330  }
331 
335  virtual void setColLower( int elementIndex, double elementValue ) {
336  collower_[elementIndex] = elementValue;
337  }
338 
342  virtual void setColUpper( int elementIndex, double elementValue ) {
343  colupper_[elementIndex] = elementValue;
344  }
345 
347  virtual void setColBounds( int elementIndex,
348  double lower, double upper ) {
349  collower_[elementIndex] = lower;
350  colupper_[elementIndex] = upper;
351  }
352 
361  virtual void setColSetBounds(const int* indexFirst,
362  const int* indexLast,
363  const double* boundList);
364 
367  virtual void setRowLower( int elementIndex, double elementValue ) {
368  rowlower_[elementIndex] = elementValue;
369  convertBoundToSense(elementValue, rowupper_[elementIndex],
370  rowsense_[elementIndex], rhs_[elementIndex],
371  rowrange_[elementIndex]);
372  }
373 
376  virtual void setRowUpper( int elementIndex, double elementValue ) {
377  rowupper_[elementIndex] = elementValue;
378  convertBoundToSense(rowlower_[elementIndex], elementValue,
379  rowsense_[elementIndex], rhs_[elementIndex],
380  rowrange_[elementIndex]);
381  }
382 
384  virtual void setRowBounds( int elementIndex,
385  double lower, double upper ) {
386  rowlower_[elementIndex] = lower;
387  rowupper_[elementIndex] = upper;
388  convertBoundToSense(lower, upper,
389  rowsense_[elementIndex], rhs_[elementIndex],
390  rowrange_[elementIndex]);
391  }
392 
394  virtual void setRowType(int index, char sense, double rightHandSide,
395  double range) {
396  rowsense_[index] = sense;
397  rhs_[index] = rightHandSide;
398  rowrange_[index] = range;
399  convertSenseToBound(sense, rightHandSide, range,
400  rowlower_[index], rowupper_[index]);
401  }
402 
411  virtual void setRowSetBounds(const int* indexFirst,
412  const int* indexLast,
413  const double* boundList);
414 
425  virtual void setRowSetTypes(const int* indexFirst,
426  const int* indexLast,
427  const char* senseList,
428  const double* rhsList,
429  const double* rangeList);
431 
432  //-------------------------------------------------------------------------
436  virtual void setContinuous(int index);
438  virtual void setInteger(int index);
441  virtual void setContinuous(const int* indices, int len);
444  virtual void setInteger(const int* indices, int len);
446 
447  //-------------------------------------------------------------------------
449  virtual void setObjSense(double s ) { objsense_ = s < 0 ? -1.0 : 1.0; }
450 
461  virtual void setColSolution(const double * colsol);
462 
473  virtual void setRowPrice(const double * rowprice);
474 
475  //-------------------------------------------------------------------------
480 
483  virtual void addCol(const CoinPackedVectorBase& vec,
484  const double collb, const double colub,
485  const double obj);
486 
489  virtual void addCols(const int numcols,
490  const CoinPackedVectorBase * const * cols,
491  const double* collb, const double* colub,
492  const double* obj);
493 #if 0
494 
495  virtual void addCols(const CoinPackedMatrix& matrix,
496  const double* collb, const double* colub,
497  const double* obj);
498 #endif
499 
500  virtual void deleteCols(const int num, const int * colIndices);
501 
504  virtual void addRow(const CoinPackedVectorBase& vec,
505  const double rowlb, const double rowub);
507  virtual void addRow(const CoinPackedVectorBase& vec,
508  const char rowsen, const double rowrhs,
509  const double rowrng);
510 
513  virtual void addRows(const int numrows,
514  const CoinPackedVectorBase * const * rows,
515  const double* rowlb, const double* rowub);
517  virtual void addRows(const int numrows,
518  const CoinPackedVectorBase * const * rows,
519  const char* rowsen, const double* rowrhs,
520  const double* rowrng);
521 #if 0
522 
523  virtual void addRows(const CoinPackedMatrix& matrix,
524  const double* rowlb, const double* rowub);
526  virtual void addRows(const CoinPackedMatrix& matrix,
527  const char* rowsen, const double* rowrhs,
528  const double* rowrng);
529 #endif
530 
531  virtual void deleteRows(const int num, const int * rowIndices);
532 
533  //-----------------------------------------------------------------------
534 #if 0
535 
556  virtual ApplyCutsReturnCode applyCuts(const OsiCuts & cs,
557  double effectivenessLb = 0.0);
558 #endif
559 
560 
561 
562  //---------------------------------------------------------------------------
563 
564 protected:
566  void initFromRlbRub(const int rownum,
567  const double* rowlb, const double* rowub);
568  void initFromRhsSenseRange(const int rownum, const char* rowsen,
569  const double* rowrhs, const double* rowrng);
570  void initFromClbCubObj(const int colnum, const double* collb,
571  const double* colub, const double* obj);
572 public:
573 
587  virtual void loadProblem(const CoinPackedMatrix& matrix,
588  const double* collb, const double* colub,
589  const double* obj,
590  const double* rowlb, const double* rowub);
591 
599  virtual void assignProblem(CoinPackedMatrix*& matrix,
600  double*& collb, double*& colub, double*& obj,
601  double*& rowlb, double*& rowub);
602 
615  virtual void loadProblem(const CoinPackedMatrix& matrix,
616  const double* collb, const double* colub,
617  const double* obj,
618  const char* rowsen, const double* rowrhs,
619  const double* rowrng);
620 
628  virtual void assignProblem(CoinPackedMatrix*& matrix,
629  double*& collb, double*& colub, double*& obj,
630  char*& rowsen, double*& rowrhs,
631  double*& rowrng);
632 
635  virtual void loadProblem(const int numcols, const int numrows,
636  const int* start, const int* index,
637  const double* value,
638  const double* collb, const double* colub,
639  const double* obj,
640  const double* rowlb, const double* rowub);
641 
644  virtual void loadProblem(const int numcols, const int numrows,
645  const int* start, const int* index,
646  const double* value,
647  const double* collb, const double* colub,
648  const double* obj,
649  const char* rowsen, const double* rowrhs,
650  const double* rowrng);
651 
654  virtual int readMps(const char *filename,
655  const char *extension = "mps");
656 
661  virtual void writeMps(const char *filename,
662  const char *extension = "mps",
663  double objSense=0.0) const;
665 
666  //---------------------------------------------------------------------------
667 
670  VOL_problem* volprob() { return &volprob_; }
673 
674  //---------------------------------------------------------------------------
675 
680 
682  virtual OsiSolverInterface * clone(bool copyData = true) const;
683 
686 
689 
691  virtual ~OsiVolSolverInterface ();
693 
694  //---------------------------------------------------------------------------
695 
696 protected:
698 
699 
700  virtual void applyRowCut(const OsiRowCut& rc);
701 
703  virtual void applyColCut(const OsiColCut& cc);
705 
706  //---------------------------------------------------------------------------
707 
708 private:
711  virtual int compute_rc(const VOL_dvector& u, VOL_dvector& rc);
714  virtual int solve_subproblem(const VOL_dvector& dual,
715  const VOL_dvector& rc,
716  double& lcost, VOL_dvector& x, VOL_dvector& v,
717  double& pcost);
720  virtual int heuristics(const VOL_problem& /*p*/,
721  const VOL_dvector& /*x*/, double& heur_val) {
722  heur_val = DBL_MAX;
723  return 0;
724  }
726 
727  //---------------------------------------------------------------------------
728 
729 private:
733  void updateRowMatrix_() const;
735  void updateColMatrix_() const;
736 
739  void checkData_() const;
742  void compute_rc_(const double* u, double* rc) const;
744  void gutsOfDestructor_();
745 
748  void rowRimAllocator_();
751  void colRimAllocator_();
752 
754  void rowRimResize_(const int newSize);
756  void colRimResize_(const int newSize);
757 
759  void convertBoundsToSenses_();
761  void convertSensesToBounds_();
762 
764  bool test_zero_one_minusone_(const CoinPackedMatrix& m) const;
766 
767  //---------------------------------------------------------------------------
768 
769 private:
770 
771  //---------------------------------------------------------------------------
775  mutable bool rowMatrixCurrent_;
778  mutable CoinPackedMatrix rowMatrix_;
780  mutable bool colMatrixCurrent_;
782  mutable CoinPackedMatrix colMatrix_;
784 
785  //---------------------------------------------------------------------------
788  bool isZeroOneMinusOne_;
795 
796  //---------------------------------------------------------------------------
799  double *colupper_;
802  double *collower_;
804  bool *continuous_;
806  double *rowupper_;
808  double *rowlower_;
810  char *rowsense_;
812  double *rhs_;
815  double *rowrange_;
817  double *objcoeffs_;
819 
820  //---------------------------------------------------------------------------
822  double objsense_;
823 
824  //---------------------------------------------------------------------------
827  double *colsol_;
830  double *rowprice_;
832  double *rc_;
834  double *lhs_;
838 
839  //---------------------------------------------------------------------------
843 
848 
850  VOL_problem volprob_;
851 };
852 
853 //#############################################################################
861 int
862 OsiVolSolverInterfaceUnitTest(const std::string & mpsDir, const std::string & netlibDir);
863 
864 #endif