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
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_),
635  cutUp_(LPDef::default_cutUp_),
636  epOpt_(LPDef::default_epOpt_),
637  epMrk_(LPDef::default_epMrk_),
638  epRHS_(LPDef::default_epRHS_),
639  epInt_(LPDef::default_epInt_),
640  epAGap_(LPDef::default_epAGap_),
641  epGap_(LPDef::default_epGap_),
642  workMem_(LPDef::default_workMem_),
643  treeMemoryLimit_(LPDef::default_treeMemoryLimit_),
644  timeLimit_(LPDef::default_timeLimit_),
645  probingLevel_(LPDef::default_probingLevel_),
646  rootAlg_(LPDef::default_rootAlg_), nodeAlg_(LPDef::default_nodeAlg_),
647  mipEmphasis_(LPDef::default_mipEmphasis_),
648  presolve_(LPDef::default_presolve_), cutLevel_(LPDef::default_cutLevel_),
649  cliqueCutLevel_(LPDef::default_cliqueCutLevel_),
650  coverCutLevel_(LPDef::default_coverCutLevel_),
651  gubCutLevel_(LPDef::default_gubCutLevel_),
652  mirCutLevel_(LPDef::default_mirCutLevel_),
653  iboundCutLevel_(LPDef::default_iboundCutLevel_),
654  flowcoverCutLevel_(LPDef::default_flowcoverCutLevel_),
655  flowpathCutLevel_(LPDef::default_flowpathCutLevel_),
656  disjunctCutLevel_(LPDef::default_disjunctCutLevel_),
657  gomoryCutLevel_(LPDef::default_gomoryCutLevel_) {
658 
659 }
660 
661 template <class LP_SOLVER_TYPE, class VALUE_TYPE, class INDEX_TYPE, class SOLUTION_ITERATOR_TYPE, class SOLVER_TIMING_TYPE>
663  return SolverType::infinity_impl();
664 }
665 
666 template <class LP_SOLVER_TYPE, class VALUE_TYPE, class INDEX_TYPE, class SOLUTION_ITERATOR_TYPE, class SOLVER_TIMING_TYPE>
668  : parameter_(parameter) {
669 
670 }
671 
672 template <class LP_SOLVER_TYPE, class VALUE_TYPE, class INDEX_TYPE, class SOLUTION_ITERATOR_TYPE, class SOLVER_TIMING_TYPE>
674 
675 }
676 
677 template <class LP_SOLVER_TYPE, class VALUE_TYPE, class INDEX_TYPE, class SOLUTION_ITERATOR_TYPE, class SOLVER_TIMING_TYPE>
679  static_cast<SolverType*>(this)->addContinuousVariables_impl(numVariables, lowerBound, upperBound);
680 }
681 
682 template <class LP_SOLVER_TYPE, class VALUE_TYPE, class INDEX_TYPE, class SOLUTION_ITERATOR_TYPE, class SOLVER_TIMING_TYPE>
684  static_cast<SolverType*>(this)->addIntegerVariables_impl(numVariables, lowerBound, upperBound);
685 }
686 
687 template <class LP_SOLVER_TYPE, class VALUE_TYPE, class INDEX_TYPE, class SOLUTION_ITERATOR_TYPE, class SOLVER_TIMING_TYPE>
689  static_cast<SolverType*>(this)->addBinaryVariables_impl(numVariables);
690 }
691 
692 template <class LP_SOLVER_TYPE, class VALUE_TYPE, class INDEX_TYPE, class SOLUTION_ITERATOR_TYPE, class SOLVER_TIMING_TYPE>
694  static_cast<SolverType*>(this)->setObjective_impl(objective);
695 }
696 
697 template <class LP_SOLVER_TYPE, class VALUE_TYPE, class INDEX_TYPE, class SOLUTION_ITERATOR_TYPE, class SOLVER_TIMING_TYPE>
699  static_cast<SolverType*>(this)->setObjectiveValue_impl(variable, value);
700 }
701 
702 template <class LP_SOLVER_TYPE, class VALUE_TYPE, class INDEX_TYPE, class SOLUTION_ITERATOR_TYPE, class SOLVER_TIMING_TYPE>
703 template<class ITERATOR_TYPE>
705  static_cast<SolverType*>(this)->setObjectiveValue_impl(begin, end);
706 }
707 
708 template <class LP_SOLVER_TYPE, class VALUE_TYPE, class INDEX_TYPE, class SOLUTION_ITERATOR_TYPE, class SOLVER_TIMING_TYPE>
709 template<class VARIABLES_ITERATOR_TYPE, class COEFFICIENTS_ITERATOR_TYPE>
710 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) {
711  static_cast<SolverType*>(this)->setObjectiveValue_impl(variableIDsBegin, variableIDsEnd, coefficientsBegin);
712 }
713 
714 template <class LP_SOLVER_TYPE, class VALUE_TYPE, class INDEX_TYPE, class SOLUTION_ITERATOR_TYPE, class SOLVER_TIMING_TYPE>
715 template<class VARIABLES_ITERATOR_TYPE, class COEFFICIENTS_ITERATOR_TYPE>
716 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) {
717  static_cast<SolverType*>(this)->addEqualityConstraint_impl(variableIDsBegin, variableIDsEnd, coefficientsBegin, bound, constraintName);
718 }
719 
720 template <class LP_SOLVER_TYPE, class VALUE_TYPE, class INDEX_TYPE, class SOLUTION_ITERATOR_TYPE, class SOLVER_TIMING_TYPE>
721 template<class VARIABLES_ITERATOR_TYPE, class COEFFICIENTS_ITERATOR_TYPE>
722 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) {
723  static_cast<SolverType*>(this)->addLessEqualConstraint_impl(variableIDsBegin, variableIDsEnd, coefficientsBegin, bound, constraintName);
724 }
725 
726 template <class LP_SOLVER_TYPE, class VALUE_TYPE, class INDEX_TYPE, class SOLUTION_ITERATOR_TYPE, class SOLVER_TIMING_TYPE>
727 template<class VARIABLES_ITERATOR_TYPE, class COEFFICIENTS_ITERATOR_TYPE>
728 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) {
729  static_cast<SolverType*>(this)->addGreaterEqualConstraint_impl(variableIDsBegin, variableIDsEnd, coefficientsBegin, bound, constraintName);
730 }
731 
732 template <class LP_SOLVER_TYPE, class VALUE_TYPE, class INDEX_TYPE, class SOLUTION_ITERATOR_TYPE, class SOLVER_TIMING_TYPE>
734  static_cast<SolverType*>(this)->addConstraintsFinished_impl();
735 }
736 
737 template <class LP_SOLVER_TYPE, class VALUE_TYPE, class INDEX_TYPE, class SOLUTION_ITERATOR_TYPE, class SOLVER_TIMING_TYPE>
739  static_cast<SolverType*>(this)->addConstraintsFinished_impl(timing);
740 }
741 
742 template <class LP_SOLVER_TYPE, class VALUE_TYPE, class INDEX_TYPE, class SOLUTION_ITERATOR_TYPE, class SOLVER_TIMING_TYPE>
743 template <class PARAMETER_TYPE, class PARAMETER_VALUE_TYPE>
744 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) {
745  static_cast<SolverType*>(this)->setParameter_impl(parameter, value);
746 }
747 
748 template <class LP_SOLVER_TYPE, class VALUE_TYPE, class INDEX_TYPE, class SOLUTION_ITERATOR_TYPE, class SOLVER_TIMING_TYPE>
750  return static_cast<SolverType*>(this)->solve_impl();
751 }
752 
753 template <class LP_SOLVER_TYPE, class VALUE_TYPE, class INDEX_TYPE, class SOLUTION_ITERATOR_TYPE, class SOLVER_TIMING_TYPE>
755  return static_cast<SolverType*>(this)->solve_impl(timing);
756 }
757 
758 template <class LP_SOLVER_TYPE, class VALUE_TYPE, class INDEX_TYPE, class SOLUTION_ITERATOR_TYPE, class SOLVER_TIMING_TYPE>
760  return static_cast<const SolverType*>(this)->solutionBegin_impl();
761 }
762 
763 template <class LP_SOLVER_TYPE, class VALUE_TYPE, class INDEX_TYPE, class SOLUTION_ITERATOR_TYPE, class SOLVER_TIMING_TYPE>
765  return static_cast<const SolverType*>(this)->solutionEnd_impl();
766 }
767 
768 template <class LP_SOLVER_TYPE, class VALUE_TYPE, class INDEX_TYPE, class SOLUTION_ITERATOR_TYPE, class SOLVER_TIMING_TYPE>
770  return static_cast<const SolverType*>(this)->solution_impl(variable);
771 }
772 
773 template <class LP_SOLVER_TYPE, class VALUE_TYPE, class INDEX_TYPE, class SOLUTION_ITERATOR_TYPE, class SOLVER_TIMING_TYPE>
775  return static_cast<const SolverType*>(this)->objectiveFunctionValue_impl();
776 }
777 
778 template <class LP_SOLVER_TYPE, class VALUE_TYPE, class INDEX_TYPE, class SOLUTION_ITERATOR_TYPE, class SOLVER_TIMING_TYPE>
780  return static_cast<const SolverType*>(this)->objectiveFunctionValueBound_impl();
781 }
782 
783 template <class LP_SOLVER_TYPE, class VALUE_TYPE, class INDEX_TYPE, class SOLUTION_ITERATOR_TYPE, class SOLVER_TIMING_TYPE>
785  static_cast<const SolverType*>(this)->exportModel_impl(filename);
786 }
787 
788 } // namespace opengm
789 
790 #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.
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).
double epRHS_
Feasibility tolerance.
Objective
This enum defines the type of the objective. It is used to select either to minimize or to maxime the...
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.
SolverSolutionIteratorType solutionEnd() const
Get an iterator which is pointing to the end of the solution computed by the Solver.
LPDef::MIP_CUT cliqueCutLevel_
Determines whether or not to generate clique cuts for the problem and how aggressively.
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.
void exportModel(const std::string &filename) const
Export model to file.
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.
Wrapper class for the IBM ILOG CPLEX optimizer.
const Parameter parameter_
Storage for parameter.
double timeLimit_
Maximal time in seconds the solver has.
SolverValueType solution(const SolverIndexType variable) const
Get the solution value of a variable computed by the Solver.
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.
SolverValueType objectiveFunctionValueBound() const
Get the best known bound for the optimal solution of the current model.
SolverValueType objectiveFunctionValue() const
Get the objective function value from the Solver.
double workMem_
Maximal amount of memory in MB used for workspace.
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...
SolverSolutionIteratorType solutionBegin() const
Get an iterator which is pointing to the begin of the solution computed by the Solver.
LPDef::MIP_CUT gomoryCutLevel_
Determines whether or not to generate gomory fractional cuts for the problem and how aggressively...