OpenGM  2.3.x
Discrete Graphical Model Library
lp_solver_interface.hxx
Go to the documentation of this file.
1 #ifndef OPENGM_LP_SOLVER_INTERFACE_HXX_
2 #define OPENGM_LP_SOLVER_INTERFACE_HXX_
3 
5 
6 /*********************
7  * class definition *
8  *********************/
9 namespace opengm {
10 
11 template <class LP_SOLVER_TYPE, class VALUE_TYPE, class INDEX_TYPE, class SOLUTION_ITERATOR_TYPE, class SOLVER_TIMING_TYPE>
13 public:
14  // typedefs
15  typedef LP_SOLVER_TYPE SolverType;
16  typedef VALUE_TYPE SolverValueType;
17  typedef INDEX_TYPE SolverIndexType;
18  typedef SOLUTION_ITERATOR_TYPE SolverSolutionIteratorType;
19  typedef SOLVER_TIMING_TYPE SolverTimingType;
20 
21  // enums
23 
24  // parameter
25  struct Parameter {
26  // constructor
27  Parameter();
28 
29  // parameter
31  bool verbose_;
32  double cutUp_;
33  double epOpt_;
34  double epMrk_;
35  double epRHS_;
36  double epInt_;
37  double epAGap_;
38  double epGap_;
39  double workMem_;
41  double timeLimit_;
57  };
58 
59  // solver infinity value
60  static SolverValueType infinity();
61 
62  // constructor
63  LPSolverInterface(const Parameter& parameter = Parameter());
64 
65  // destructor
67 
68  // add Variables
69  void addContinuousVariables(const SolverIndexType numVariables, const SolverValueType lowerBound, const SolverValueType upperBound);
70  void addIntegerVariables(const SolverIndexType numVariables, const SolverValueType lowerBound, const SolverValueType upperBound);
71  void addBinaryVariables(const SolverIndexType numVariables);
72 
73  // objective function
74  void setObjective(const Objective objective);
75  void setObjectiveValue(const SolverIndexType variable, const SolverValueType value);
76  template<class ITERATOR_TYPE>
77  void setObjectiveValue(ITERATOR_TYPE begin, const ITERATOR_TYPE end);
78  template<class VARIABLES_ITERATOR_TYPE, class COEFFICIENTS_ITERATOR_TYPE>
79  void setObjectiveValue(VARIABLES_ITERATOR_TYPE variableIDsBegin, const VARIABLES_ITERATOR_TYPE variableIDsEnd, COEFFICIENTS_ITERATOR_TYPE coefficientsBegin);
80 
81  // constraints
82  template<class VARIABLES_ITERATOR_TYPE, class COEFFICIENTS_ITERATOR_TYPE>
83  void addEqualityConstraint(VARIABLES_ITERATOR_TYPE variableIDsBegin, const VARIABLES_ITERATOR_TYPE variableIDsEnd, COEFFICIENTS_ITERATOR_TYPE coefficientsBegin, const SolverValueType bound, const std::string& constraintName = "");
84  template<class VARIABLES_ITERATOR_TYPE, class COEFFICIENTS_ITERATOR_TYPE>
85  void addLessEqualConstraint(VARIABLES_ITERATOR_TYPE variableIDsBegin, const VARIABLES_ITERATOR_TYPE variableIDsEnd, COEFFICIENTS_ITERATOR_TYPE coefficientsBegin, const SolverValueType bound, const std::string& constraintName = "");
86  template<class VARIABLES_ITERATOR_TYPE, class COEFFICIENTS_ITERATOR_TYPE>
87  void addGreaterEqualConstraint(VARIABLES_ITERATOR_TYPE variableIDsBegin, const VARIABLES_ITERATOR_TYPE variableIDsEnd, COEFFICIENTS_ITERATOR_TYPE coefficientsBegin, const SolverValueType bound, const std::string& constraintName = "");
88 
90  void addConstraintsFinished(SolverTimingType& timing);
91 
92  // parameter
93  template <class PARAMETER_TYPE, class PARAMETER_VALUE_TYPE>
94  void setParameter(const PARAMETER_TYPE parameter, const PARAMETER_VALUE_TYPE value);
95 
96  // solve
97  bool solve();
98  bool solve(SolverTimingType& timing);
99 
100  // solution
101  SolverSolutionIteratorType solutionBegin() const;
102  SolverSolutionIteratorType solutionEnd() const;
103  SolverValueType solution(const SolverIndexType variable) const;
104 
105  SolverValueType objectiveFunctionValue() const;
106  SolverValueType objectiveFunctionValueBound() const;
107 
108  // model export
109  void exportModel(const std::string& filename) const;
110 protected:
111  // storage
112  const Parameter parameter_;
113 };
114 
115 } // namespace opengm
116 
117 /***********************
118  * class documentation *
119  ***********************/
626 /******************
627  * implementation *
628  ******************/
629 namespace opengm {
630 
631 template <class LP_SOLVER_TYPE, class VALUE_TYPE, class INDEX_TYPE, class SOLUTION_ITERATOR_TYPE, class SOLVER_TIMING_TYPE>
633  : numberOfThreads_(LPDef::default_numberOfThreads_),
634  verbose_(LPDef::default_verbose_), cutUp_(LPDef::default_cutUp_),
635  epOpt_(LPDef::default_epOpt_), epMrk_(LPDef::default_epMrk_),
636  epRHS_(LPDef::default_epRHS_), epInt_(LPDef::default_epInt_),
637  epAGap_(LPDef::default_epAGap_), epGap_(LPDef::default_epGap_),
638  workMem_(LPDef::default_workMem_),
639  treeMemoryLimit_(LPDef::default_treeMemoryLimit_),
640  timeLimit_(LPDef::default_timeLimit_),
641  probingLevel_(LPDef::default_probingLevel_),
642  rootAlg_(LPDef::default_rootAlg_), nodeAlg_(LPDef::default_nodeAlg_),
643  mipEmphasis_(LPDef::default_mipEmphasis_),
644  presolve_(LPDef::default_presolve_), cutLevel_(LPDef::default_cutLevel_),
645  cliqueCutLevel_(LPDef::default_cliqueCutLevel_),
646  coverCutLevel_(LPDef::default_coverCutLevel_),
647  gubCutLevel_(LPDef::default_gubCutLevel_),
648  mirCutLevel_(LPDef::default_mirCutLevel_),
649  iboundCutLevel_(LPDef::default_iboundCutLevel_),
650  flowcoverCutLevel_(LPDef::default_flowcoverCutLevel_),
651  flowpathCutLevel_(LPDef::default_flowpathCutLevel_),
652  disjunctCutLevel_(LPDef::default_disjunctCutLevel_),
653  gomoryCutLevel_(LPDef::default_gomoryCutLevel_) {
654 
655 }
656 
657 template <class LP_SOLVER_TYPE, class VALUE_TYPE, class INDEX_TYPE, class SOLUTION_ITERATOR_TYPE, class SOLVER_TIMING_TYPE>
659  return SolverType::infinity_impl();
660 }
661 
662 template <class LP_SOLVER_TYPE, class VALUE_TYPE, class INDEX_TYPE, class SOLUTION_ITERATOR_TYPE, class SOLVER_TIMING_TYPE>
664  : parameter_(parameter) {
665 
666 }
667 
668 template <class LP_SOLVER_TYPE, class VALUE_TYPE, class INDEX_TYPE, class SOLUTION_ITERATOR_TYPE, class SOLVER_TIMING_TYPE>
670 
671 }
672 
673 template <class LP_SOLVER_TYPE, class VALUE_TYPE, class INDEX_TYPE, class SOLUTION_ITERATOR_TYPE, class SOLVER_TIMING_TYPE>
675  static_cast<SolverType*>(this)->addContinuousVariables_impl(numVariables, lowerBound, upperBound);
676 }
677 
678 template <class LP_SOLVER_TYPE, class VALUE_TYPE, class INDEX_TYPE, class SOLUTION_ITERATOR_TYPE, class SOLVER_TIMING_TYPE>
680  static_cast<SolverType*>(this)->addIntegerVariables_impl(numVariables, lowerBound, upperBound);
681 }
682 
683 template <class LP_SOLVER_TYPE, class VALUE_TYPE, class INDEX_TYPE, class SOLUTION_ITERATOR_TYPE, class SOLVER_TIMING_TYPE>
685  static_cast<SolverType*>(this)->addBinaryVariables_impl(numVariables);
686 }
687 
688 template <class LP_SOLVER_TYPE, class VALUE_TYPE, class INDEX_TYPE, class SOLUTION_ITERATOR_TYPE, class SOLVER_TIMING_TYPE>
690  static_cast<SolverType*>(this)->setObjective_impl(objective);
691 }
692 
693 template <class LP_SOLVER_TYPE, class VALUE_TYPE, class INDEX_TYPE, class SOLUTION_ITERATOR_TYPE, class SOLVER_TIMING_TYPE>
695  static_cast<SolverType*>(this)->setObjectiveValue_impl(variable, value);
696 }
697 
698 template <class LP_SOLVER_TYPE, class VALUE_TYPE, class INDEX_TYPE, class SOLUTION_ITERATOR_TYPE, class SOLVER_TIMING_TYPE>
699 template<class ITERATOR_TYPE>
701  static_cast<SolverType*>(this)->setObjectiveValue_impl(begin, end);
702 }
703 
704 template <class LP_SOLVER_TYPE, class VALUE_TYPE, class INDEX_TYPE, class SOLUTION_ITERATOR_TYPE, class SOLVER_TIMING_TYPE>
705 template<class VARIABLES_ITERATOR_TYPE, class COEFFICIENTS_ITERATOR_TYPE>
706 inline void LPSolverInterface<LP_SOLVER_TYPE, VALUE_TYPE, INDEX_TYPE, SOLUTION_ITERATOR_TYPE, SOLVER_TIMING_TYPE>::setObjectiveValue(VARIABLES_ITERATOR_TYPE variableIDsBegin, const VARIABLES_ITERATOR_TYPE variableIDsEnd, COEFFICIENTS_ITERATOR_TYPE coefficientsBegin) {
707  static_cast<SolverType*>(this)->setObjectiveValue_impl(variableIDsBegin, variableIDsEnd, coefficientsBegin);
708 }
709 
710 template <class LP_SOLVER_TYPE, class VALUE_TYPE, class INDEX_TYPE, class SOLUTION_ITERATOR_TYPE, class SOLVER_TIMING_TYPE>
711 template<class VARIABLES_ITERATOR_TYPE, class COEFFICIENTS_ITERATOR_TYPE>
712 inline void LPSolverInterface<LP_SOLVER_TYPE, VALUE_TYPE, INDEX_TYPE, SOLUTION_ITERATOR_TYPE, SOLVER_TIMING_TYPE>::addEqualityConstraint(VARIABLES_ITERATOR_TYPE variableIDsBegin, const VARIABLES_ITERATOR_TYPE variableIDsEnd, COEFFICIENTS_ITERATOR_TYPE coefficientsBegin, const SolverValueType bound, const std::string& constraintName) {
713  static_cast<SolverType*>(this)->addEqualityConstraint_impl(variableIDsBegin, variableIDsEnd, coefficientsBegin, bound, constraintName);
714 }
715 
716 template <class LP_SOLVER_TYPE, class VALUE_TYPE, class INDEX_TYPE, class SOLUTION_ITERATOR_TYPE, class SOLVER_TIMING_TYPE>
717 template<class VARIABLES_ITERATOR_TYPE, class COEFFICIENTS_ITERATOR_TYPE>
718 inline void LPSolverInterface<LP_SOLVER_TYPE, VALUE_TYPE, INDEX_TYPE, SOLUTION_ITERATOR_TYPE, SOLVER_TIMING_TYPE>::addLessEqualConstraint(VARIABLES_ITERATOR_TYPE variableIDsBegin, const VARIABLES_ITERATOR_TYPE variableIDsEnd, COEFFICIENTS_ITERATOR_TYPE coefficientsBegin, const SolverValueType bound, const std::string& constraintName) {
719  static_cast<SolverType*>(this)->addLessEqualConstraint_impl(variableIDsBegin, variableIDsEnd, coefficientsBegin, bound, constraintName);
720 }
721 
722 template <class LP_SOLVER_TYPE, class VALUE_TYPE, class INDEX_TYPE, class SOLUTION_ITERATOR_TYPE, class SOLVER_TIMING_TYPE>
723 template<class VARIABLES_ITERATOR_TYPE, class COEFFICIENTS_ITERATOR_TYPE>
724 inline void LPSolverInterface<LP_SOLVER_TYPE, VALUE_TYPE, INDEX_TYPE, SOLUTION_ITERATOR_TYPE, SOLVER_TIMING_TYPE>::addGreaterEqualConstraint(VARIABLES_ITERATOR_TYPE variableIDsBegin, const VARIABLES_ITERATOR_TYPE variableIDsEnd, COEFFICIENTS_ITERATOR_TYPE coefficientsBegin, const SolverValueType bound, const std::string& constraintName) {
725  static_cast<SolverType*>(this)->addGreaterEqualConstraint_impl(variableIDsBegin, variableIDsEnd, coefficientsBegin, bound, constraintName);
726 }
727 
728 template <class LP_SOLVER_TYPE, class VALUE_TYPE, class INDEX_TYPE, class SOLUTION_ITERATOR_TYPE, class SOLVER_TIMING_TYPE>
730  static_cast<SolverType*>(this)->addConstraintsFinished_impl();
731 }
732 
733 template <class LP_SOLVER_TYPE, class VALUE_TYPE, class INDEX_TYPE, class SOLUTION_ITERATOR_TYPE, class SOLVER_TIMING_TYPE>
735  static_cast<SolverType*>(this)->addConstraintsFinished_impl(timing);
736 }
737 
738 template <class LP_SOLVER_TYPE, class VALUE_TYPE, class INDEX_TYPE, class SOLUTION_ITERATOR_TYPE, class SOLVER_TIMING_TYPE>
739 template <class PARAMETER_TYPE, class PARAMETER_VALUE_TYPE>
740 inline void LPSolverInterface<LP_SOLVER_TYPE, VALUE_TYPE, INDEX_TYPE, SOLUTION_ITERATOR_TYPE, SOLVER_TIMING_TYPE>::setParameter(const PARAMETER_TYPE parameter, const PARAMETER_VALUE_TYPE value) {
741  static_cast<SolverType*>(this)->setParameter_impl(parameter, value);
742 }
743 
744 template <class LP_SOLVER_TYPE, class VALUE_TYPE, class INDEX_TYPE, class SOLUTION_ITERATOR_TYPE, class SOLVER_TIMING_TYPE>
746  return static_cast<SolverType*>(this)->solve_impl();
747 }
748 
749 template <class LP_SOLVER_TYPE, class VALUE_TYPE, class INDEX_TYPE, class SOLUTION_ITERATOR_TYPE, class SOLVER_TIMING_TYPE>
751  return static_cast<SolverType*>(this)->solve_impl(timing);
752 }
753 
754 template <class LP_SOLVER_TYPE, class VALUE_TYPE, class INDEX_TYPE, class SOLUTION_ITERATOR_TYPE, class SOLVER_TIMING_TYPE>
756  return static_cast<const SolverType*>(this)->solutionBegin_impl();
757 }
758 
759 template <class LP_SOLVER_TYPE, class VALUE_TYPE, class INDEX_TYPE, class SOLUTION_ITERATOR_TYPE, class SOLVER_TIMING_TYPE>
761  return static_cast<const SolverType*>(this)->solutionEnd_impl();
762 }
763 
764 template <class LP_SOLVER_TYPE, class VALUE_TYPE, class INDEX_TYPE, class SOLUTION_ITERATOR_TYPE, class SOLVER_TIMING_TYPE>
766  return static_cast<const SolverType*>(this)->solution_impl(variable);
767 }
768 
769 template <class LP_SOLVER_TYPE, class VALUE_TYPE, class INDEX_TYPE, class SOLUTION_ITERATOR_TYPE, class SOLVER_TIMING_TYPE>
771  return static_cast<const SolverType*>(this)->objectiveFunctionValue_impl();
772 }
773 
774 template <class LP_SOLVER_TYPE, class VALUE_TYPE, class INDEX_TYPE, class SOLUTION_ITERATOR_TYPE, class SOLVER_TIMING_TYPE>
776  return static_cast<const SolverType*>(this)->objectiveFunctionValueBound_impl();
777 }
778 
779 template <class LP_SOLVER_TYPE, class VALUE_TYPE, class INDEX_TYPE, class SOLUTION_ITERATOR_TYPE, class SOLVER_TIMING_TYPE>
781  static_cast<const SolverType*>(this)->exportModel_impl(filename);
782 }
783 
784 } // namespace opengm
785 
786 #endif /* OPENGM_LP_SOLVER_INTERFACE_HXX_ */
INDEX_TYPE SolverIndexType
Defines the index type used by the LP Solver.
void addBinaryVariables(const SolverIndexType numVariables)
Add new binary variables to the model.
LPDef::MIP_CUT flowcoverCutLevel_
Determines whether or not to generate flow cover cuts for the problem and how aggressively.
SolverValueType objectiveFunctionValueBound() const
Get the best known bound for the optimal solution of the current model.
The OpenGM namespace.
Definition: config.hxx:43
void addLessEqualConstraint(VARIABLES_ITERATOR_TYPE variableIDsBegin, const VARIABLES_ITERATOR_TYPE variableIDsEnd, COEFFICIENTS_ITERATOR_TYPE coefficientsBegin, const SolverValueType bound, const std::string &constraintName="")
Add a new less equal constraint to the model.
LPDef::MIP_CUT gubCutLevel_
Determines whether or not to generate generalized upper bound (GUB) cuts for the problem and how aggr...
LPDef::LP_SOLVER rootAlg_
Select which algorithm is used to solve continuous models or to solve the root relaxation of a MIP...
Objective function will be maximized.
int numberOfThreads_
The number of threads used for Optimization (0 = autoselect).
SolverValueType solution(const SolverIndexType variable) const
Get the solution value of a variable computed by the Solver.
double epRHS_
Feasibility tolerance.
SOLUTION_ITERATOR_TYPE SolverSolutionIteratorType
Defines the iterator type which can be used to iterate over the solution of the LP Solver...
LPDef::MIP_CUT flowpathCutLevel_
Determines whether or not to generate flow path cuts for the problem and how aggressively.
LP_SOLVER_TYPE SolverType
Defines the type of the child class which inherits from LPSolverInterface.
VALUE_TYPE SolverValueType
Defines the value type used by the LP Solver.
void addContinuousVariables(const SolverIndexType numVariables, const SolverValueType lowerBound, const SolverValueType upperBound)
Add new continuous variables to the model.
LPSolverInterface(const Parameter &parameter=Parameter())
Default constructor of class LPSolverInterface.
void addIntegerVariables(const SolverIndexType numVariables, const SolverValueType lowerBound, const SolverValueType upperBound)
Add new integer variables to the model.
double epOpt_
Optimality tolerance.
SOLVER_TIMING_TYPE SolverTimingType
Defines the timing type used by the LP Solver.
double epGap_
Relative MIP gap tolerance.
LPDef::MIP_CUT disjunctCutLevel_
Determines whether or not to generate disjunctive cuts for the problem and how aggressively.
LPDef::MIP_CUT cliqueCutLevel_
Determines whether or not to generate clique cuts for the problem and how aggressively.
void exportModel(const std::string &filename) const
Export model to file.
void addEqualityConstraint(VARIABLES_ITERATOR_TYPE variableIDsBegin, const VARIABLES_ITERATOR_TYPE variableIDsEnd, COEFFICIENTS_ITERATOR_TYPE coefficientsBegin, const SolverValueType bound, const std::string &constraintName="")
Add a new equality constraint to the model.
void setParameter(const PARAMETER_TYPE parameter, const PARAMETER_VALUE_TYPE value)
Set Solver parameter.
void setObjective(const Objective objective)
Set objective to minimize or maximize.
SolverSolutionIteratorType solutionBegin() const
Get an iterator which is pointing to the begin of the solution computed by the Solver.
int probingLevel_
Amount of probing on variables to be performed before MIP branching.
static SolverValueType infinity()
Get the value which is used by the LP Solver to represent infinity.
LPDef::MIP_CUT mirCutLevel_
Determines whether or not mixed integer rounding (MIR) cuts should be generated for the problem and h...
LPDef::MIP_CUT coverCutLevel_
Determines whether or not to generate cover cuts for the problem and how aggressively.
double epAGap_
Absolute MIP gap tolerance.
Interface definition for wrapper of LP Solvers like CPLEX and Gurobi.
LPDef::MIP_EMPHASIS mipEmphasis_
Controls trade-offs between speed, feasibility, optimality, and moving bounds in a MIP...
double epInt_
Amount by which an integer variable can differ from an integer.
double treeMemoryLimit_
Maximal amount of memory in MB used for tree.
const Parameter parameter_
Storage for parameter.
double timeLimit_
Maximal time in seconds the solver has.
Parameter()
Default constructor of class Parameter. Sets default values provided by class LPDef for all options...
LPDef::MIP_CUT cutLevel_
Determines whether or not to generate cuts for the problem and how aggressively (will be overruled by...
Parameter class provides options to modify LP Solver behavior.
LPDef::LP_SOLVER nodeAlg_
Select which algorithm is used to solve the subproblems in a MIP after the initial relaxation has bee...
void addGreaterEqualConstraint(VARIABLES_ITERATOR_TYPE variableIDsBegin, const VARIABLES_ITERATOR_TYPE variableIDsEnd, COEFFICIENTS_ITERATOR_TYPE coefficientsBegin, const SolverValueType bound, const std::string &constraintName="")
Add a new greater equal constraint to the model.
void setObjectiveValue(const SolverIndexType variable, const SolverValueType value)
Set the coefficient of a variable in the objective function.
~LPSolverInterface()
Default destructor of class LPSolverInterface.
bool verbose_
Enable verbose output if set to true.
bool solve()
Solve the current model.
Objective function will be minimized.
double cutUp_
Upper cutoff tolerance.
double workMem_
Maximal amount of memory in MB used for workspace.
SolverSolutionIteratorType solutionEnd() const
Get an iterator which is pointing to the end of the solution computed by the Solver.
LPDef::LP_PRESOLVE presolve_
Controls how aggressive presolve is performed during preprocessing.
LPDef::MIP_CUT iboundCutLevel_
Determines whether or not to generate implied bound cuts for the problem and how aggressively.
void addConstraintsFinished()
Join all constraints added via LPSolverInterface::addEqualityConstraint, LPSolverInterface::addLessEq...
SolverValueType objectiveFunctionValue() const
Get the objective function value from the Solver.
LPDef::MIP_CUT gomoryCutLevel_
Determines whether or not to generate gomory fractional cuts for the problem and how aggressively...