OsiDylpSolverInterface.hpp
Go to the documentation of this file.
1 #ifndef OsiDylpSolverInterface_H
2 #define OsiDylpSolverInterface_H
3 
18 /*
19  sccs: @(#)OsiDylpSolverInterface.hpp 1.12 09/16/04
20  cvs: $Id: OsiDylpSolverInterface.hpp 1312 2008-10-10 00:26:32Z lou $
21 */
22 
23 #include "OsiConfig.h"
24 #include <CoinPackedMatrix.hpp>
25 #include <OsiSolverInterface.hpp>
26 #include <CoinWarmStart.hpp>
27 #include <CoinMessageHandler.hpp>
28 #include <CoinMpsIO.hpp>
29 #include <CoinPresolveMatrix.hpp>
30 
31 #define DYLP_INTERNAL
32 extern "C" {
33 #include "dylp.h"
34 }
35 
38 typedef enum { startInvalid = 0,
40 
41 
101 
102 { friend int OsiDylpSolverInterfaceUnitTest(const std::string &mpsDir,
103  const std::string &netLibDir) ;
104 
105 /*
106  Consult the COIN OSI documentation or relevant source code for details
107  not covered here. Supported functions are listed first, followed by
108  unsupported functions.
109 */
110 
111 public:
112 
115 
119 
123 
126  OsiSolverInterface* clone(bool copyData = true) const ;
127 
131 
135 
140  void reset() ;
141 
143 
146 
149  int readMps(const char *filename, const char *extension = "mps") ;
150 
155  int readMps(const char *filename, const char *extension,
156  int &numberSets, CoinSet **&sets) ;
157 
165  void writeMps(const char *basename,
166  const char *extension = "mps",
167  double objsense = 0.0) const ;
168 
172  void loadProblem(const CoinPackedMatrix &matrix,
173  const double *collb, const double *colub, const double *obj,
174  const char *rowsen, const double *rowrhs,
175  const double *rowrng) ;
176 
180  void loadProblem(const CoinPackedMatrix &matrix,
181  const double *collb, const double *colub, const double *obj,
182  const double *rowlb, const double *rowub) ;
183 
187  void loadProblem(const int colcnt, const int rowcnt,
188  const int *start, const int *index, const double *value,
189  const double *collb, const double *colub, const double *obj,
190  const char *sense, const double *rhsin, const double *range) ;
191 
195  void loadProblem(const int colcnt, const int rowcnt,
196  const int *start, const int *index, const double *value,
197  const double *collb, const double *colub, const double *obj,
198  const double *row_lower, const double *row_upper) ;
199 
203  void assignProblem(CoinPackedMatrix *&matrix,
204  double *&collb, double *&colub, double *&obj,
205  char *&rowsen, double *&rowrhs, double *&rowrng) ;
206 
210  void assignProblem(CoinPackedMatrix *&matrix,
211  double *&collb, double *&colub, double *&obj,
212  double *&rowlb, double *&rowub) ;
213 
215 
219 
222  int getNumCols() const ;
223 
226  int getNumRows() const ;
227 
230  int getNumElements() const ;
231 
237  int getNumIntegers() const ;
238 
241  const double* getColLower() const ;
242 
245  const double* getColUpper() const ;
246 
249  bool isContinuous(int colIndex) const ;
250 
253  bool isBinary(int colIndex) const ;
254 
257  bool isIntegerNonBinary (int colIndex) const ;
258 
261  bool isInteger (int colIndex) const ;
262 
265  const char* getRowSense() const ;
266 
269  const double* getRightHandSide() const ;
270 
273  const double* getRowRange() const ;
274 
277  const double* getRowLower() const ;
278 
281  const double* getRowUpper() const ;
282 
285  const double* getObjCoefficients() const ;
286 
292  double getObjSense() const ;
293 
296  const CoinPackedMatrix *getMatrixByRow() const ;
297 
300  const CoinPackedMatrix *getMatrixByCol() const ;
302 
305 
308  void setContinuous(int index) ;
309 
312  void setContinuous(const int *indices, int len) ;
313 
316  void setInteger(int index) ;
317 
320  void setInteger(const int *indices, int len) ;
321 
324  void setColLower(int index, double value) ;
326 
329  void setColUpper(int index, double value) ;
331 
334  void setRowLower(int index, double value) ;
335 
338  void setRowUpper(int index, double value) ;
339 
342  void setRowType(int index, char rowsen, double rowrhs, double rowrng) ;
343 
346  void setObjCoeff (int index, double value) ;
347 
354  void setObjSense(double sense) ;
355 
358  void setColSolution(const double *colsol) ;
359 
362  void setRowPrice(const double*) ;
363 
364  /* For overload resolution with OSI::addCol functions. */
365 
367 
370  void addCol(const CoinPackedVectorBase &vec,
371  const double collb, const double colub, const double obj) ;
372 
375  void deleteCols(const int num, const int *colIndices) ;
376 
377  /* For overload resolution with OSI::addRow functions. */
378 
380 
383  void addRow(const CoinPackedVectorBase &row,
384  const double rowlb, const double rowub) ;
385 
388  void addRow(const CoinPackedVectorBase &row,
389  const char rowsen, const double rowrhs, const double rowrng) ;
390 
393  void deleteRows(const int num, const int *rowIndices) ;
394 
397  void applyRowCut(const OsiRowCut &cut) ;
398 
401  void applyColCut(const OsiColCut &cut) ;
403 
406 
409  void initialSolve() ;
410 
413  CoinWarmStart *getEmptyWarmStart () const ;
414 
417  CoinWarmStart *getWarmStart() const ;
418 
426  bool setWarmStart(const CoinWarmStart *warmStart) ;
427 
430  void resolve() ;
431 
434  void markHotStart() ;
435 
438  void solveFromHotStart() ;
439 
442  void unmarkHotStart() ;
443 
445 
448 
451  bool isAbandoned() const ;
452 
455  bool isProvenOptimal() const ;
456 
459  bool isProvenPrimalInfeasible() const ;
460 
464  bool isProvenDualInfeasible() const ;
465 
468  bool isIterationLimitReached() const ;
469 
472  int getIterationCount() const ;
473 
479  bool isPrimalObjectiveLimitReached() const ;
480 
486  bool isDualObjectiveLimitReached() const ;
488 
489 
492 
495  double getInfinity() const ;
496 
499  bool setIntParam(OsiIntParam key, int value) ;
500 
503  bool setDblParam(OsiDblParam key, double value) ;
504 
507  bool setStrParam(OsiStrParam key, const std::string& value) ;
508 
511  bool setHintParam(OsiHintParam key, bool sense = true,
512  OsiHintStrength strength = OsiHintTry, void *info = 0) ;
513 
516  bool getIntParam(OsiIntParam key, int &value) const ;
517 
520  bool getDblParam(OsiDblParam key, double &value) const ;
521 
524  bool getStrParam(OsiStrParam key, std::string &value) const ;
525 
526  /* For overload resolution with OSI::getHintParam functions. */
527 
529 
532  bool getHintParam(OsiHintParam key, bool &sense,
533  OsiHintStrength &strength, void *&info) const ;
534 
537  inline void newLanguage(CoinMessages::Language language)
538  { setOsiDylpMessages(language) ; }
539 
542  inline void setLanguage(CoinMessages::Language language)
543  { setOsiDylpMessages(language) ; }
544 
546 
549 
552  const double* getColSolution() const ;
553 
556  const double* getRowPrice() const ;
557 
560  const double* getReducedCost() const ;
561 
564  const double* getRowActivity() const ;
565 
568  double getObjValue() const ;
569 
571 
578 
581  void setObjName (std::string name) ;
582 
588  void setRowName(int ndx, std::string name) ;
589 
595  void setColName(int ndx, std::string name) ;
596 
598 
601 
608  void activateRowCutDebugger (const char * modelName) ;
609 
617  void activateRowCutDebugger (const double *solution) ;
618 
619 # if ODSI_PARANOIA >= 1
620 
629  void indexCheck (int k, bool isCol, std::string rtnnme) ;
630 # endif
631 
633 
636 
639  void dylp_controlfile(const char* name, const bool silent,
640  const bool mustexist = true) ;
641 
644  void dylp_logfile(const char* name, bool echo = false) ;
645 
648  void dylp_outfile(const char* name) ;
649 
652  void dylp_printsoln(bool wantSoln, bool wantStats) ;
653 
656  void setOsiDylpMessages(CoinMessages::Language local_language) ;
657 
659 
662 
663 
666  void branchAndBound() ;
667 
670  std::vector<double *> getDualRays(int) const ;
671 
674  std::vector<double *> getPrimalRays(int) const ;
676 
681 /*
682  Leave them visible to the public for the nonce, until a better programmatic
683  interface is available. Initialized by the constructor.
684 */
686 
688  lptols_struct *tolerances ;
689 
691 
692 private:
693 
694 /*
695  Private implementation state and helper functions. If you're contemplating
696  using any of these, you should have a look at the code.
697  See OsiDylpSolverInterface.cpp for descriptions.
698 */
705 
706  consys_struct *consys ;
707  lpprob_struct *lpprob ;
708  lpstats_struct *statistics ;
709 
711 
714 
715  static int reference_count ;
716  static bool basis_ready ;
718 
720 
721 
727 
735 
742 
748 
754 
764  lpret_enum lp_retval ;
765 
771  double obj_sense ;
772 
775  double odsiInfinity ;
776 
779  const std::string solvername ;
780 
783  mutable void *info_[OsiLastHintParam] ;
784 
787  bool mps_debug ;
788 
796  CoinWarmStart *hotstart_fallback ;
797 
820 
838  struct
839  { CoinWarmStart *basis ;
842 
848  bool solnIsFresh ;
849 
851 
852 
853 
860 
861  mutable double _objval ;
862  mutable double* _col_obj ;
863  mutable double* _col_x ;
864  mutable double* _col_cbar ;
865 
866  mutable double* _row_rhs ;
867  mutable double* _row_lower ;
868  mutable double* _row_upper ;
869  mutable char* _row_sense ;
870  mutable double* _row_range ;
871  mutable double* _row_lhs ;
872  mutable double* _row_price ;
873 
874  mutable CoinPackedMatrix* _matrix_by_col ;
875  mutable CoinPackedMatrix* _matrix_by_row ;
876 
878 
885 
891  CoinPresolveMatrix *preObj_ ;
892 
899  const CoinPresolveAction *postActions_ ;
900 
908  CoinPostsolveMatrix *postObj_ ;
909 
912 
915 
917  consys_struct *savedConsys_ ;
918 
920  mutable double* saved_col_obj ;
921  mutable double* saved_row_rhs ;
922  mutable double* saved_row_lower ;
923  mutable double* saved_row_upper ;
924  mutable char* saved_row_sense ;
925  mutable double* saved_row_range ;
926  mutable CoinPackedMatrix* saved_matrix_by_col ;
927  mutable CoinPackedMatrix* saved_matrix_by_row ;
928 
930 
940  CoinPresolveMatrix *initialisePresolve(bool keepIntegers) ;
942 
944  void doPresolve() ;
945 
947  bool evalPresolve() ;
948 
950  void saveOriginalSys() ;
951 
953  void installPresolve() ;
954 
956  CoinPostsolveMatrix *initialisePostsolve(CoinPresolveMatrix *&preObj) ;
957 
959  void doPostsolve() ;
960 
962  void installPostsolve() ;
963 
965  void destruct_presolve() ;
966 
968 
972  void construct_lpprob() ;
973  void construct_options() ;
974  void construct_consys(int cols, int rows) ;
975  void dylp_ioinit() ;
976  void gen_rowparms(int rowcnt,
977  double *rhs, double *rhslow, contyp_enum *ctyp,
978  const double *rowlb, const double *rowub) ;
979  void gen_rowparms(int rowcnt,
980  double *rhs, double *rhslow, contyp_enum *ctyp,
981  const char *sense, const double *rhsin, const double *range) ;
982  void load_problem(const CoinMpsIO &mps) ;
983  void load_problem(const CoinPackedMatrix &matrix,
984  const double* col_lower, const double* col_upper, const double* obj,
985  const contyp_enum *ctyp, const double* rhs, const double* rhslow) ;
986  void load_problem (const int colcnt, const int rowcnt,
987  const int *start, const int *lens,
988  const int *index, const double *value,
989  const double* col_lower, const double* col_upper, const double* obj,
990  const contyp_enum *ctyp, const double* rhs, const double* rhslow) ;
992 
996  lpret_enum do_lp (ODSI_start_enum start) ;
998 
1001  void destruct_col_cache(bool structure) ;
1002  void destruct_row_cache(bool structure) ;
1003  void destruct_cache(bool rowStructure, bool colStructure) ;
1004  void destruct_problem(bool preserve_interface) ;
1005  void detach_dylp() ;
1007 
1008 
1010 /*
1011  There are separate groups for member and static methods so that doxygen
1012  won't promote the group to the top level.
1013 */
1015 
1016  void add_col(const CoinPackedVectorBase& coin_coli,
1017  vartyp_enum vtypi, double vlbi,
1018  double vubi, double obji, const std::string *nme) ;
1019  void add_row(const CoinPackedVectorBase& coin_rowi,
1020  char clazzi, contyp_enum ctypi,
1021  double rhsi, double rhslowi, const std::string *nme) ;
1022  void calc_objval() ;
1023  contyp_enum bound_to_type(double lower, double upper) ;
1024  void gen_rowiparms(contyp_enum* ctypi, double* rhsi, double* rhslowi,
1025  char sensei, double rhsini, double rangei) ;
1026  void gen_rowiparms(contyp_enum* ctypi, double* rhsi, double* rhslowi,
1027  double rowlbi, double rowubi) ;
1028  void unimp_hint(bool dylpSense, bool hintSense,
1029  OsiHintStrength hintStrength, const char *msgString) ;
1030  void pessimal_primal() ;
1031  void reduceActiveBasis() ;
1032 
1034 
1037  static contyp_enum sense_to_type(char type) ;
1038  static char type_to_sense(contyp_enum type) ;
1040 
1047  template<class T> static void copy(const T* src, T* dst, int n) ;
1048  template<class T> static T* copy(const T* src, int n) ;
1049  template<class T> static T* copy(const T* src) ;
1050 /*
1051  Specializations for more complicated structures.
1052 */
1053  static basis_struct* copy_basis(const basis_struct* src, int dstsze) ;
1054  static void copy_basis(const basis_struct* src, basis_struct* dst) ;
1055  static lpprob_struct* copy_lpprob(const lpprob_struct* src) ;
1057 
1058 #ifndef _MSC_VER
1059 
1064  template<class T> static void assert_same(const T& t1, const T& t2,
1065  bool exact) ;
1066  template<class T> static void assert_same(const T* t1, const T* t2,
1067  int n, bool exact) ;
1068 
1069  static void assert_same(double d1, double d2, bool exact) ;
1070 
1071  static void assert_same(const basis_struct& b1,
1072  const basis_struct& b2, bool exact) ;
1073  static void assert_same(const consys_struct& c1, const
1074  consys_struct& c2, bool exact) ;
1075  static void assert_same(const conbnd_struct& c1, const
1076  conbnd_struct& c2, bool exact) ;
1077  static void assert_same(const lpprob_struct& l1,
1078  const lpprob_struct& l2, bool exact) ;
1079  static void assert_same(const OsiDylpSolverInterface& o1,
1080  const OsiDylpSolverInterface& o2, bool exact) ;
1082 #endif /* ! _MSC_VER */
1083 
1086  template<class T> static T* idx_vec(T* data) ;
1087  static int idx(int i) ;
1088  template<class T> static T* inv_vec(T* data) ;
1089  static int inv(int i) ;
1090 
1091  static pkvec_struct* packed_vector(
1092  const CoinShallowPackedVector vector, int dimension) ;
1093  static void packed_vector(
1094  const CoinShallowPackedVector vector, int dimension, pkvec_struct *dst) ;
1096 
1099  static std::string make_filename(const char *filename,
1100  const char *ext1, const char *ext2) ;
1102 
1103 } ;
1104 
1105 
1106 /*
1107  OsiDylpSolverInterfaceTest.cpp
1108 */
1109 
1117 int OsiDylpSolverInterfaceUnitTest(const std::string & mpsDir,
1118  const std::string &netLibDir) ;
1119 
1120 #endif // OsiDylpSolverInterface_H