1 #ifndef OPENGM_LP_SOLVER_GUROBI_HXX_
2 #define OPENGM_LP_SOLVER_GUROBI_HXX_
4 #include <gurobi_c++.h>
44 void addContinuousVariables_impl(
const GurobiIndexType numVariables,
const GurobiValueType lowerBound,
const GurobiValueType upperBound);
45 void addIntegerVariables_impl(
const GurobiIndexType numVariables,
const GurobiValueType lowerBound,
const GurobiValueType upperBound);
51 template<
class ITERATOR_TYPE>
53 template<
class VARIABLES_ITERATOR_TYPE,
class COEFFICIENTS_ITERATOR_TYPE>
54 void setObjectiveValue_impl(VARIABLES_ITERATOR_TYPE variableIDsBegin,
const VARIABLES_ITERATOR_TYPE variableIDsEnd, COEFFICIENTS_ITERATOR_TYPE coefficientsBegin);
57 template<
class VARIABLES_ITERATOR_TYPE,
class COEFFICIENTS_ITERATOR_TYPE>
58 void addEqualityConstraint_impl(VARIABLES_ITERATOR_TYPE variableIDsBegin,
const VARIABLES_ITERATOR_TYPE variableIDsEnd, COEFFICIENTS_ITERATOR_TYPE coefficientsBegin,
const GurobiValueType bound,
const std::string& constraintName =
"");
59 template<
class VARIABLES_ITERATOR_TYPE,
class COEFFICIENTS_ITERATOR_TYPE>
60 void addLessEqualConstraint_impl(VARIABLES_ITERATOR_TYPE variableIDsBegin,
const VARIABLES_ITERATOR_TYPE variableIDsEnd, COEFFICIENTS_ITERATOR_TYPE coefficientsBegin,
const GurobiValueType bound,
const std::string& constraintName =
"");
61 template<
class VARIABLES_ITERATOR_TYPE,
class COEFFICIENTS_ITERATOR_TYPE>
62 void addGreaterEqualConstraint_impl(VARIABLES_ITERATOR_TYPE variableIDsBegin,
const VARIABLES_ITERATOR_TYPE variableIDsEnd, COEFFICIENTS_ITERATOR_TYPE coefficientsBegin,
const GurobiValueType bound,
const std::string& constraintName =
"");
68 template <
class PARAMETER_TYPE,
class PARAMETER_VALUE_TYPE>
69 void setParameter_impl(
const PARAMETER_TYPE parameter,
const PARAMETER_VALUE_TYPE value);
78 GurobiValueType
solution_impl(
const GurobiIndexType variable)
const;
473 gurobiModel_(gurobiEnvironment_), gurobiVariables_(), gurobiSolution_(),
474 gurobiSolutionValid_(false) {
484 gurobiModel_.getEnv().set(GRB_IntParam_LogToConsole, 0);
520 throw std::runtime_error(
"Gurobi does not support Network Simplex");
537 throw std::runtime_error(
"Unknown Root Algorithm");
556 throw std::runtime_error(
"Gurobi does not support Network Simplex");
564 throw std::runtime_error(
"Gurobi does not support Sifting as node algorithm");
568 throw std::runtime_error(
"Gurobi does not support concurrent solvers as node algorithm");
572 throw std::runtime_error(
"Unknown Node Algorithm");
595 throw std::runtime_error(
"Unknown Presolve Option");
618 throw std::runtime_error(
"Gurobi does not support hidden feasibility as MIP-focus");
622 throw std::runtime_error(
"Unknown MIP Emphasis Option");
654 }
catch(
const GRBException& e) {
655 std::cout <<
"Gurobi Error code = " << e.getErrorCode() << std::endl;
656 std::cout << e.getMessage() << std::endl;
657 throw std::runtime_error(e.getMessage());
659 std::cout <<
"Exception while setting parameter for Gurobi model." << std::endl;
660 throw std::runtime_error(
"Exception while setting parameter for Gurobi model.");
682 }
catch(
const GRBException& e) {
683 std::cout <<
"Gurobi Error code = " << e.getErrorCode() << std::endl;
684 std::cout << e.getMessage() << std::endl;
685 throw std::runtime_error(e.getMessage());
687 std::cout <<
"Exception while adding continuous variables to Gurobi model." << std::endl;
688 throw std::runtime_error(
"Exception while adding continuous variables to Gurobi model.");
702 }
catch(
const GRBException& e) {
703 std::cout <<
"Gurobi Error code = " << e.getErrorCode() << std::endl;
704 std::cout << e.getMessage() << std::endl;
705 throw std::runtime_error(e.getMessage());
707 std::cout <<
"Exception while adding integer variables to Gurobi model." << std::endl;
708 throw std::runtime_error(
"Exception while adding integer variables to Gurobi model.");
722 }
catch(
const GRBException& e) {
723 std::cout <<
"Gurobi Error code = " << e.getErrorCode() << std::endl;
724 std::cout << e.getMessage() << std::endl;
725 throw std::runtime_error(e.getMessage());
727 std::cout <<
"Exception while adding binary variables to Gurobi model." << std::endl;
728 throw std::runtime_error(
"Exception while adding binary variables to Gurobi model.");
737 }
catch(
const GRBException& e) {
738 std::cout <<
"Gurobi Error code = " << e.getErrorCode() << std::endl;
739 std::cout << e.getMessage() << std::endl;
740 throw std::runtime_error(e.getMessage());
742 std::cout <<
"Exception while setting objective of Gurobi model." << std::endl;
743 throw std::runtime_error(
"Exception while setting objective of Gurobi model.");
750 }
catch(
const GRBException& e) {
751 std::cout <<
"Gurobi Error code = " << e.getErrorCode() << std::endl;
752 std::cout << e.getMessage() << std::endl;
753 throw std::runtime_error(e.getMessage());
755 std::cout <<
"Exception while setting objective of Gurobi model." << std::endl;
756 throw std::runtime_error(
"Exception while setting objective of Gurobi model.");
761 throw std::runtime_error(
"Unknown Objective");
769 }
catch(
const GRBException& e) {
770 std::cout <<
"Gurobi Error code = " << e.getErrorCode() << std::endl;
771 std::cout << e.getMessage() << std::endl;
772 throw std::runtime_error(e.getMessage());
774 std::cout <<
"Exception while setting objective value of Gurobi model." << std::endl;
775 throw std::runtime_error(
"Exception while setting objective value of Gurobi model.");
779 template<
class ITERATOR_TYPE>
782 GRBLinExpr objective;
785 }
catch(
const GRBException& e) {
786 std::cout <<
"Gurobi Error code = " << e.getErrorCode() << std::endl;
787 std::cout << e.getMessage() << std::endl;
788 throw std::runtime_error(e.getMessage());
790 std::cout <<
"Exception while setting objective value of Gurobi model." << std::endl;
791 throw std::runtime_error(
"Exception while setting objective value of Gurobi model.");
795 template<
class VARIABLES_ITERATOR_TYPE,
class COEFFICIENTS_ITERATOR_TYPE>
798 while(variableIDsBegin != variableIDsEnd) {
799 gurobiVariables_[*variableIDsBegin].set(GRB_DoubleAttr_Obj, *coefficientsBegin);
803 }
catch(
const GRBException& e) {
804 std::cout <<
"Gurobi Error code = " << e.getErrorCode() << std::endl;
805 std::cout << e.getMessage() << std::endl;
806 throw std::runtime_error(e.getMessage());
808 std::cout <<
"Exception while setting objective value of Gurobi model." << std::endl;
809 throw std::runtime_error(
"Exception while setting objective value of Gurobi model.");
813 template<
class VARIABLES_ITERATOR_TYPE,
class COEFFICIENTS_ITERATOR_TYPE>
815 const GurobiIndexType numConstraintVariables = std::distance(variableIDsBegin, variableIDsEnd);
816 std::vector<GRBVar> constraintVariables;
817 constraintVariables.reserve(numConstraintVariables);
818 while(variableIDsBegin != variableIDsEnd) {
824 GRBLinExpr constraint;
825 constraint.addTerms(&(*coefficientsBegin), &constraintVariables[0], numConstraintVariables);
826 gurobiModel_.addConstr(constraint, GRB_EQUAL, bound, constraintName);
827 }
catch(
const GRBException& e) {
828 std::cout <<
"Gurobi Error code = " << e.getErrorCode() << std::endl;
829 std::cout << e.getMessage() << std::endl;
830 throw std::runtime_error(e.getMessage());
832 std::cout <<
"Exception while adding equality constraint to Gurobi model." << std::endl;
833 throw std::runtime_error(
"Exception while adding equality constraint to Gurobi model.");
837 template<
class VARIABLES_ITERATOR_TYPE,
class COEFFICIENTS_ITERATOR_TYPE>
839 const GurobiIndexType numConstraintVariables = std::distance(variableIDsBegin, variableIDsEnd);
840 std::vector<GRBVar> constraintVariables;
841 constraintVariables.reserve(numConstraintVariables);
842 while(variableIDsBegin != variableIDsEnd) {
848 GRBLinExpr constraint;
849 constraint.addTerms(&(*coefficientsBegin), &constraintVariables[0], numConstraintVariables);
850 gurobiModel_.addConstr(constraint, GRB_LESS_EQUAL, bound, constraintName);
851 }
catch(
const GRBException& e) {
852 std::cout <<
"Gurobi Error code = " << e.getErrorCode() << std::endl;
853 std::cout << e.getMessage() << std::endl;
854 throw std::runtime_error(e.getMessage());
856 std::cout <<
"Exception while adding less equal constraint to Gurobi model." << std::endl;
857 throw std::runtime_error(
"Exception while adding less equal constraint to Gurobi model.");
861 template<
class VARIABLES_ITERATOR_TYPE,
class COEFFICIENTS_ITERATOR_TYPE>
863 const GurobiIndexType numConstraintVariables = std::distance(variableIDsBegin, variableIDsEnd);
864 std::vector<GRBVar> constraintVariables;
865 constraintVariables.reserve(numConstraintVariables);
866 while(variableIDsBegin != variableIDsEnd) {
872 GRBLinExpr constraint;
873 constraint.addTerms(&(*coefficientsBegin), &constraintVariables[0], numConstraintVariables);
874 gurobiModel_.addConstr(constraint, GRB_GREATER_EQUAL, bound, constraintName);
875 }
catch(
const GRBException& e) {
876 std::cout <<
"Gurobi Error code = " << e.getErrorCode() << std::endl;
877 std::cout << e.getMessage() << std::endl;
878 throw std::runtime_error(e.getMessage());
880 std::cout <<
"Exception while adding greater equal constraint to Gurobi model." << std::endl;
881 throw std::runtime_error(
"Exception while adding greater equal constraint to Gurobi model.");
888 }
catch(
const GRBException& e) {
889 std::cout <<
"Gurobi Error code = " << e.getErrorCode() << std::endl;
890 std::cout << e.getMessage() << std::endl;
891 throw std::runtime_error(e.getMessage());
893 std::cout <<
"Exception while incorporating constraints into Gurobi model." << std::endl;
894 throw std::runtime_error(
"Exception while incorporating constraints into Gurobi model.");
905 }
catch(
const GRBException& e) {
906 std::cout <<
"Gurobi Error code = " << e.getErrorCode() << std::endl;
907 std::cout << e.getMessage() << std::endl;
908 throw std::runtime_error(e.getMessage());
910 std::cout <<
"Exception while incorporating constraints into Gurobi model." << std::endl;
911 throw std::runtime_error(
"Exception while incorporating constraints into Gurobi model.");
915 template <
class PARAMETER_TYPE,
class PARAMETER_VALUE_TYPE>
919 }
catch(
const GRBException& e) {
920 std::cout <<
"Gurobi Error code = " << e.getErrorCode() << std::endl;
921 std::cout << e.getMessage() << std::endl;
922 throw std::runtime_error(e.getMessage());
924 std::cout <<
"Exception while setting parameter for Gurobi model." << std::endl;
925 throw std::runtime_error(
"Exception while setting parameter for Gurobi model.");
933 if(
gurobiModel_.get(GRB_IntAttr_Status) == GRB_OPTIMAL) {
938 }
catch(
const GRBException& e) {
939 std::cout <<
"Gurobi Error code = " << e.getErrorCode() << std::endl;
940 std::cout << e.getMessage() << std::endl;
941 throw std::runtime_error(e.getMessage());
943 std::cout <<
"Exception while solving Gurobi model." << std::endl;
944 throw std::runtime_error(
"Exception while solving Gurobi model.");
953 if(
gurobiModel_.get(GRB_IntAttr_Status) == GRB_OPTIMAL) {
958 }
catch(
const GRBException& e) {
959 std::cout <<
"Gurobi Error code = " << e.getErrorCode() << std::endl;
960 std::cout << e.getMessage() << std::endl;
961 throw std::runtime_error(e.getMessage());
963 std::cout <<
"Exception while solving Gurobi model." << std::endl;
964 throw std::runtime_error(
"Exception while solving Gurobi model.");
981 }
catch(
const GRBException& e) {
982 std::cout <<
"Gurobi Error code = " << e.getErrorCode() << std::endl;
983 std::cout << e.getMessage() << std::endl;
984 throw std::runtime_error(e.getMessage());
986 std::cout <<
"Exception while accessing Gurobi solution of variable." << std::endl;
987 throw std::runtime_error(
"Exception while accessing Gurobi solution of variable.");
994 }
catch(
const GRBException& e) {
995 std::cout <<
"Gurobi Error code = " << e.getErrorCode() << std::endl;
996 std::cout << e.getMessage() << std::endl;
997 throw std::runtime_error(e.getMessage());
999 std::cout <<
"Exception while accessing Gurobi solution for objective function value." << std::endl;
1000 throw std::runtime_error(
"Exception while accessing Gurobi solution for objective function value.");
1011 }
catch(
const GRBException& e) {
1012 std::cout <<
"Gurobi Error code = " << e.getErrorCode() << std::endl;
1013 std::cout << e.getMessage() << std::endl;
1014 throw std::runtime_error(e.getMessage());
1016 std::cout <<
"Exception while accessing Gurobi bound for objective function value." << std::endl;
1017 throw std::runtime_error(
"Exception while accessing Gurobi bound for objective function value.");
1024 }
catch(
const GRBException& e) {
1025 std::cout <<
"Gurobi Error code = " << e.getErrorCode() << std::endl;
1026 std::cout << e.getMessage() << std::endl;
1027 throw std::runtime_error(e.getMessage());
1029 std::cout <<
"Exception while writing Gurobi model to file." << std::endl;
1030 throw std::runtime_error(
"Exception while writing Gurobi model to file.");
1041 }
catch(
const GRBException& e) {
1042 std::cout <<
"Gurobi Error code = " << e.getErrorCode() << std::endl;
1043 std::cout << e.getMessage() << std::endl;
1044 throw std::runtime_error(e.getMessage());
1046 std::cout <<
"Exception while updating Gurobi solution." << std::endl;
1047 throw std::runtime_error(
"Exception while updating Gurobi solution.");
1066 throw std::runtime_error(
"Unknown Cut level.");
void updateSolution() const
Update solution if required.
void setObjective_impl(const Objective objective)
Set objective to minimize or maximize.
LPSolverInterface< LPSolverGurobi, GurobiValueType, GurobiIndexType, GurobiSolutionIteratorType, GurobiTimingType > LPSolverBaseClass
Defines the type of the base class.
void addContinuousVariables_impl(const GurobiIndexType numVariables, const GurobiValueType lowerBound, const GurobiValueType upperBound)
Add new continuous variables to the model.
Wrapper class for the Gurobi optimizer.
GurobiSolutionIteratorType solutionEnd_impl() const
Get an iterator which is pointing to the end of the solution computed by Gurobi.
Platform-independent runtime measurements.
static GurobiValueType infinity_impl()
Get the value which is used by Gurobi to represent infinity.
void addBinaryVariables_impl(const GurobiIndexType numVariables)
Add new binary variables to the model.
Provides Interface definition for wrapper of LP Solvers like CPLEX and Gurobi.
GRBModel gurobiModel_
The Gurobi model of the LP/MIP problem.
GurobiValueType solution_impl(const GurobiIndexType variable) const
Get the solution value of a variable computed by Gurobi.
GurobiValueType objectiveFunctionValueBound_impl() const
Get the best known bound for the optimal solution of the current model.
void addConstraintsFinished_impl()
Join all constraints added via LPSolverGurobi::addEqualityConstraint, LPSolverGurobi::addLessEqualCon...
Interface definition for wrapper of LP Solvers like CPLEX and Gurobi.
double GurobiTimingType
Defines the timing type used by Gurobi.
void addLessEqualConstraint_impl(VARIABLES_ITERATOR_TYPE variableIDsBegin, const VARIABLES_ITERATOR_TYPE variableIDsEnd, COEFFICIENTS_ITERATOR_TYPE coefficientsBegin, const GurobiValueType bound, const std::string &constraintName="")
Add a new less equal constraint to the model.
const Parameter parameter_
int GurobiIndexType
Defines the index type used by Gurobi.
double elapsedTime() const
void addGreaterEqualConstraint_impl(VARIABLES_ITERATOR_TYPE variableIDsBegin, const VARIABLES_ITERATOR_TYPE variableIDsEnd, COEFFICIENTS_ITERATOR_TYPE coefficientsBegin, const GurobiValueType bound, const std::string &constraintName="")
Add a new greater equal constraint to the model.
std::vector< GurobiValueType >::const_iterator GurobiSolutionIteratorType
Defines the iterator type which can be used to iterate over the solution of Gurobi.
~LPSolverGurobi()
Destructor for LPSolverGurobi.
double GurobiValueType
Defines the value type used by Gurobi.
std::vector< GRBVar > gurobiVariables_
The variables which are present in the model.
void addIntegerVariables_impl(const GurobiIndexType numVariables, const GurobiValueType lowerBound, const GurobiValueType upperBound)
Add new integer variables to the model.
void exportModel_impl(const std::string &filename) const
Export model to file.
bool solve_impl()
Solve the current model.
void setObjectiveValue_impl(const GurobiIndexType variable, const GurobiValueType value)
Set the coefficient of a variable in the objective function.
static int getCutLevelValue(const LPDef::MIP_CUT cutLevel)
Translate LPDef::MIP_CUT into corresponding Gurobi int value.
bool gurobiSolutionValid_
Tell if the currently stored solution is valid.
void addEqualityConstraint_impl(VARIABLES_ITERATOR_TYPE variableIDsBegin, const VARIABLES_ITERATOR_TYPE variableIDsEnd, COEFFICIENTS_ITERATOR_TYPE coefficientsBegin, const GurobiValueType bound, const std::string &constraintName="")
Add a new equality constraint to the model.
std::vector< GurobiValueType > gurobiSolution_
Storage for the solution computed by Gurobi.
GRBEnv gurobiEnvironment_
The Gurobi environment.
GurobiSolutionIteratorType solutionBegin_impl() const
Get an iterator which is pointing to the begin of the solution computed by Gurobi.
GurobiValueType objectiveFunctionValue_impl() const
Get the objective function value from Gurobi.
void setParameter_impl(const PARAMETER_TYPE parameter, const PARAMETER_VALUE_TYPE value)
Set Gurobi parameter.
LPSolverGurobi(const Parameter ¶meter=Parameter())
Default constructor for LPSolverGurobi.