OpenGM  2.3.x
Discrete Graphical Model Library
lp_solver_gurobi_old.hxx
Go to the documentation of this file.
1 #pragma once
2 #ifndef OPENGM_LP_SOLVER_GUROBI_HXX
3 #define OPENGM_LP_SOLVER_GUROBI_HXX
4 
5 #include "gurobi_c++.h"
7 
8 
9 namespace opengm{
10 
11 
13 public:
14  typedef double LpValueType;
15  typedef int LpIndexType;
16 
17  struct Parameter{
18 
19 
20 
21  struct Termination{
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()
29  )
30  :
31  cutoff_(cutoff),
32  iterationLimit_(iterationLimit),
33  nodeLimit_(nodeLimit),
34  solutionLimit_(solutionLimit),
35  timeLimit_(timeLimit),
36  barIterLimit_(barIterLimit)
37  {
38 
39  }
40 
41  double cutoff_;
43  double nodeLimit_;
45  double timeLimit_;
47  };
48 
49  struct Tolerances{
51 
52  ){
53 
54  }
55  };
56 
57  struct Simplex{
59 
60  ){
61 
62  }
63  };
64 
65  struct Barrier{
67 
68  ){
69 
70  }
71  };
72 
73  struct MIP{
74  MIP(
75 
76  ){
77 
78  }
79  };
80 
81  struct MIPCuts{
83 
84  ){
85 
86  }
87  };
88 
89  struct Others{
91 
92  ){
93 
94  }
95  };
96 
98  const Termination & termination = Termination(),
99  const Tolerances & tolerances = Tolerances(),
100  const Simplex & simplex = Simplex(),
101  const Barrier & barrier = Barrier(),
102  const MIP & mip = MIP(),
103  const MIPCuts & mipCuts = MIPCuts(),
104  const Others & other = Others()
105  )
106  : termination_(termination),
107  tolerances_(tolerances),
108  simplex_(simplex),
109  barrier_(barrier),
110  mip_(mip),
111  mipCuts_(mipCuts),
112  other_(other)
113  {
114  }
115 
123 
124  };
125 
126 
127  // costructor
128  LpSolverGurobi(const Parameter & parameter = Parameter())
129  :
130  grbEnv_(),
131  grbModel_(grbEnv_),
132  param_(parameter),
133  numVar_(0)
134  {
135 
136  }
137 
138 
140  const UInt64Type numVar,
141  const LpVarType varType,
142  const LpValueType lowerBound = 0.0,
143  const LpValueType upperBound = 1.0
144  ){
145  if(varType==Continous){
146  for(UInt64Type i=0;i<numVar;++i)
147  grbModel_.addVar(lowerBound,upperBound,0.0,GRB_CONTINUOUS);
148  }
149  else if (varType == Binary){
150  for(UInt64Type i=0;i<numVar;++i)
151  grbModel_.addVar(lowerBound,upperBound,0.0,GRB_BINARY);
152  }
153  else{
154  throw RuntimeError("General Integer VarType is not yet implemented");
155  }
156  numVar_+=numVar;
157  }
158 
159  void setObjective(const UInt64Type lpVi,const LpValueType obj){
160  grbModel_.getVars()[lpVi].set(GRB_DoubleAttr_Obj, obj);
161  }
162 
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()
171  ){
172  GRBVar * gvars = grbModel_.getVars();
173 
174  if(upperBound-lowerBound < 0.000000001){
175  GRBLinExpr linExp;// = new GRBLinExpr();
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);
180  ++lpVarBegin;
181  ++coeffBegin;
182  }
183  if(name.size()>0){
184  grbModel_.addConstr(linExp,GRB_EQUAL,static_cast<LpValueType>(lowerBound),name.c_str());
185  }
186  else{
187  grbModel_.addConstr(linExp,GRB_EQUAL,static_cast<LpValueType>(lowerBound));
188  }
189  }
190  else{
191  GRBLinExpr linExpLower;// = new GRBLinExpr();
192  GRBLinExpr linExpUpper;// = new GRBLinExpr();
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);
198  ++lpVarBegin;
199  ++coeffBegin;
200  }
201  if(name.size()>0){
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);
206  }
207  else{
208  grbModel_.addConstr(linExpLower,GRB_GREATER_EQUAL ,static_cast<LpValueType>(lowerBound));
209  grbModel_.addConstr(linExpUpper,GRB_LESS_EQUAL ,static_cast<LpValueType>(upperBound));
210  }
211  }
212  }
213 
214 
216  grbModel_.update();
217  }
218 
220 
221  }
222 
224 
225  }
226 
227 
229  return numVar_;
230  }
231 
232  void optimize() {
233  try{
234  grbModel_.optimize();
235  }
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");
240  }
241  catch(...) {
242  throw RuntimeError("Exception during gurobi optimization");
243  }
244  }
245 
246  LpValueType lpArg(const LpIndexType lpVi)const{
247  GRBVar * gvars = grbModel_.getVars();
248  return gvars[lpVi].get(GRB_DoubleAttr_X);
249  }
250 
251  LpValueType lpValue()const{
252  const double objval = grbModel_.get(GRB_DoubleAttr_ObjVal);
253  return static_cast<LpValueType>(objval);
254  }
255 
256  LpValueType bestLpValue()const{
257  return this->lpValue();
258  }
259 
260 private:
261 
262 
263  // mebers of gurobi itself
264  GRBEnv grbEnv_;
265  GRBModel grbModel_;
266 
267  // param
268  Parameter param_;
269  UInt64Type numVar_;
270 
271 };
272 
273 
274 
275 }
276 #endif
The OpenGM namespace.
Definition: config.hxx:43
LpSolverGurobi(const Parameter &parameter=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
Definition: config.hxx:300
void addConstraint(LPVariableIndexIterator lpVarBegin, LPVariableIndexIterator lpVarEnd, CoefficientIterator coeffBegin, const LpValueType lowerBound, const LpValueType upperBound, const std::string &name=std::string())
LpValueType bestLpValue() const
OpenGM runtime error.
Definition: opengm.hxx:100
LpValueType lpValue() const