2 #ifndef OPENGM_LP_SOLVER_GUROBI_HXX
3 #define OPENGM_LP_SOLVER_GUROBI_HXX
5 #include "gurobi_c++.h"
23 const double cutoff = std::numeric_limits<double>::infinity(),
24 const double iterationLimit = std::numeric_limits<double>::infinity(),
25 const double nodeLimit = std::numeric_limits<double>::infinity(),
26 const double solutionLimit = std::numeric_limits<int>::max(),
27 const double timeLimit = std::numeric_limits<double>::infinity(),
28 const double barIterLimit = std::numeric_limits<int>::max()
142 const LpValueType lowerBound = 0.0,
143 const LpValueType upperBound = 1.0
147 grbModel_.addVar(lowerBound,upperBound,0.0,GRB_CONTINUOUS);
149 else if (varType ==
Binary){
151 grbModel_.addVar(lowerBound,upperBound,0.0,GRB_BINARY);
154 throw RuntimeError(
"General Integer VarType is not yet implemented");
160 grbModel_.getVars()[lpVi].set(GRB_DoubleAttr_Obj, obj);
163 template<
class LPVariableIndexIterator,
class CoefficientIterator>
165 LPVariableIndexIterator lpVarBegin,
166 LPVariableIndexIterator lpVarEnd,
167 CoefficientIterator coeffBegin,
168 const LpValueType lowerBound,
169 const LpValueType upperBound,
170 const std::string & name = std::string()
172 GRBVar * gvars = grbModel_.getVars();
174 if(upperBound-lowerBound < 0.000000001){
176 while(lpVarBegin!=lpVarEnd){
177 const LpIndexType lpVi =
static_cast<LpIndexType
>(*lpVarBegin);
178 const LpValueType coeff =
static_cast<LpValueType
>(*coeffBegin);
179 linExp.addTerms(&coeff,&gvars[lpVi],1);
184 grbModel_.addConstr(linExp,GRB_EQUAL,static_cast<LpValueType>(lowerBound),name.c_str());
187 grbModel_.addConstr(linExp,GRB_EQUAL,static_cast<LpValueType>(lowerBound));
191 GRBLinExpr linExpLower;
192 GRBLinExpr linExpUpper;
193 while(lpVarBegin!=lpVarEnd){
194 const LpIndexType lpVi =
static_cast<LpIndexType
>(*lpVarBegin);
195 const LpValueType coeff =
static_cast<LpValueType
>(*coeffBegin);
196 linExpLower.addTerms(&coeff,&gvars[lpVi],1);
197 linExpUpper.addTerms(&coeff,&gvars[lpVi],1);
202 std::string nameLower = name + std::string(
"_lower");
203 std::string nameUpper = name + std::string(
"_upper");
204 grbModel_.addConstr(linExpLower,GRB_GREATER_EQUAL ,static_cast<LpValueType>(lowerBound),nameLower);
205 grbModel_.addConstr(linExpUpper,GRB_LESS_EQUAL ,static_cast<LpValueType>(upperBound),nameUpper);
208 grbModel_.addConstr(linExpLower,GRB_GREATER_EQUAL ,static_cast<LpValueType>(lowerBound));
209 grbModel_.addConstr(linExpUpper,GRB_LESS_EQUAL ,static_cast<LpValueType>(upperBound));
234 grbModel_.optimize();
236 catch(GRBException e) {
237 std::cout <<
"Error code = " << e.getErrorCode() <<
"\n";
238 std::cout << e.getMessage() <<
"\n";
239 throw RuntimeError(
"Exception during gurobi optimization");
242 throw RuntimeError(
"Exception during gurobi optimization");
246 LpValueType
lpArg(
const LpIndexType lpVi)
const{
247 GRBVar * gvars = grbModel_.getVars();
248 return gvars[lpVi].get(GRB_DoubleAttr_X);
252 const double objval = grbModel_.get(GRB_DoubleAttr_ObjVal);
253 return static_cast<LpValueType
>(objval);
LpSolverGurobi(const Parameter ¶meter=Parameter())
Parameter(const Termination &termination=Termination(), const Tolerances &tolerances=Tolerances(), const Simplex &simplex=Simplex(), const Barrier &barrier=Barrier(), const MIP &mip=MIP(), const MIPCuts &mipCuts=MIPCuts(), const Others &other=Others())
void setObjective(const UInt64Type lpVi, const LpValueType obj)
Termination(const double cutoff=std::numeric_limits< double >::infinity(), const double iterationLimit=std::numeric_limits< double >::infinity(), const double nodeLimit=std::numeric_limits< double >::infinity(), const double solutionLimit=std::numeric_limits< int >::max(), const double timeLimit=std::numeric_limits< double >::infinity(), const double barIterLimit=std::numeric_limits< int >::max())
UInt64Type numberOfVariables() const
LpValueType lpArg(const LpIndexType lpVi) const
void addVariables(const UInt64Type numVar, const LpVarType varType, const LpValueType lowerBound=0.0, const LpValueType upperBound=1.0)
detail_types::UInt64Type UInt64Type
uint64
void addConstraint(LPVariableIndexIterator lpVarBegin, LPVariableIndexIterator lpVarEnd, CoefficientIterator coeffBegin, const LpValueType lowerBound, const LpValueType upperBound, const std::string &name=std::string())
LpValueType bestLpValue() const
LpValueType lpValue() const