OpenGM  2.3.x
Discrete Graphical Model Library
lp_solver_cplex_old.hxx
Go to the documentation of this file.
1 #pragma once
2 #ifndef OPENGM_LP_SOLVER_CPLEX_HXX
3 #define OPENGM_LP_SOLVER_CPLEX_HXX
4 
5 #include <ilcplex/ilocplex.h>
6 
8 
9 
10 
11 namespace opengm{
12 
13 
15 {
16 public:
17  typedef double LpValueType;
18  typedef int LpIndexType;
19 
20  class Parameter {
21  public:
25  Parameter
26  (
27  int numberOfThreads = 0,
28  double cutUp = 1.0e+75,
29  double epGap=0
30  )
31  : numberOfThreads_(numberOfThreads),
32  verbose_(false),
33  cutUp_(cutUp),
34  epGap_(epGap),
35  workMem_(128.0),
36  treeMemoryLimit_(1e+75),
37  timeLimit_(1e+75),
38  probeingLevel_(0),
39  coverCutLevel_(0),
41  cliqueCutLevel_(0),
42  MIRCutLevel_(0)
43  {
44  numberOfThreads_ = numberOfThreads;
45  };
46 
47  int numberOfThreads_; // number of threads (0=autosect)
48  bool verbose_; // switch on/off verbode mode
49  double cutUp_; // upper cutoff
50  double epGap_; // relative optimality gap tolerance
51  double workMem_; // maximal ammount of memory in MB used for workspace
52  double treeMemoryLimit_; // maximal ammount of memory in MB used for treee
53  double timeLimit_; // maximal time in seconds the solver has
59  };
60 
61 
63  ConstraintType(const LpValueType lb,const LpValueType ub){
64 
65  }
66  };
67 
68 
69  // costructor
70  LpSolverCplex(const Parameter & parameter = Parameter())
71  :
72  env_(),
73  model_(env_),
74  x_(env_),
75  c_(env_),
76  obj_(IloMinimize(env_)),
77  sol_(env_),
78  cplex_(),
79  objBuffer_(NULL),
80  param_(parameter),
81  numVar_(0),
82  constraintCounter_(0)
83  {
84 
85  }
86 
87 
89  const UInt64Type numVar,
90  const LpVarType varType,
91  const LpValueType lowerBound = 1.0,
92  const LpValueType upperBound = 1.0
93  ){
94  if(varType==Continous){
95  x_.add(IloNumVarArray(env_, numVar, lowerBound, upperBound));
96  }
97  else if(varType==Binary ){
98  x_.add(IloNumVarArray(env_, numVar, lowerBound, upperBound, ILOBOOL));
99  }
100  else{
101  OPENGM_CHECK(false,"not yet implemented");
102  }
103  numVar_+=numVar;
104  }
105 
106 
108  const LpValueType lowerBound,
109  const LpValueType upperBound,
110  const std::string & name = std::string()
111  ){
112  c_.add(IloRange(env_, lowerBound, upperBound));
113  constraintCounter_+=1;
114  }
115 
117  const UInt64Type constraintIndex,
118  const UInt64Type lpVarIndex,
119  const LpValueType coeff
120  ){
121  OPENGM_CHECK_OP(constraintIndex,<,constraintCounter_,"");
122  c_[constraintIndex].setLinearCoef(x_[lpVarIndex], coeff);
123  }
124 
125  template<class LPVariableIndexIterator,class CoefficientIterator>
127  LPVariableIndexIterator lpVarBegin,
128  LPVariableIndexIterator lpVarEnd,
129  CoefficientIterator coeffBegin,
130  const LpValueType lowerBound,
131  const LpValueType upperBound,
132  const std::string & name = std::string()
133  ){
134  c_.add(IloRange(env_, lowerBound, upperBound, name.c_str()));
135  while(lpVarBegin != lpVarEnd) {
136  OPENGM_CHECK_OP(*lpVarBegin,<,numVar_,"");
137  c_[constraintCounter_].setLinearCoef(x_[*lpVarBegin], *coeffBegin);
138  ++lpVarBegin;
139  ++coeffBegin;
140  }
141  //model_.add(constraint);
142  // adding constraints does not require a re-initialization of the
143  // object cplex_. cplex_ is initialized in the constructor.
144  constraintCounter_+=1;
145  }
146 
148  OPENGM_CHECK(objBuffer_==NULL,"");
149  objBuffer_ = new IloNumArray(env_, numVar_);
150  }
151 
152 
153  void setObjective(const UInt64Type lpVi,const LpValueType obj){
154  OPENGM_CHECK(objBuffer_!=NULL,"");
155  (*objBuffer_)[lpVi]=obj;
156  }
157 
158 
160  OPENGM_CHECK(objBuffer_!=NULL,"");
161  obj_.setLinearCoefs(x_, *objBuffer_);
162  delete objBuffer_;
163  }
164 
166  /*
167  model_.add(obj_);
168  model_.add(c_);
169  // initialize solver
170  try {
171  cplex_ = IloCplex(model_);
172  }
173  catch(IloCplex::Exception& e) {
174  std::cout << e << std::endl;
175  throw RuntimeError("CPLEX exception");
176  }
177  */
178  }
179 
181 
182  }
184 
185  }
187  return numVar_;
188  }
189 
190  void optimize() {
191 
192  model_.add(obj_);
193  model_.add(c_);
194  // initialize solver
195  try {
196  cplex_ = IloCplex(model_);
197  }
198  catch(IloCplex::Exception& e) {
199  std::cout << e << std::endl;
200  throw RuntimeError("CPLEX exception");
201  }
202 
203 
204  try {
205 
206 
207  // verbose options
208  if(param_.verbose_ == false) {
209  cplex_.setParam(IloCplex::MIPDisplay, 0);
210  cplex_.setParam(IloCplex::SimDisplay, 0);
211  cplex_.setParam(IloCplex::SiftDisplay, 0);
212  }
213 
214  // tolarance settings
215  cplex_.setParam(IloCplex::EpOpt, 1e-9); // Optimality Tolerance
216  cplex_.setParam(IloCplex::EpInt, 0); // amount by which an integer variable can differ from an integer
217  cplex_.setParam(IloCplex::EpAGap, 0); // Absolute MIP gap tolerance
218  cplex_.setParam(IloCplex::EpGap, param_.epGap_); // Relative MIP gap tolerance
219 
220  // set hints
221  cplex_.setParam(IloCplex::CutUp, param_.cutUp_);
222 
223  // memory setting
224  cplex_.setParam(IloCplex::WorkMem, param_.workMem_);
225  cplex_.setParam(IloCplex::ClockType,2);//wall-clock-time=2 cpu-time=1
226  cplex_.setParam(IloCplex::TiLim,param_.treeMemoryLimit_);
227  cplex_.setParam(IloCplex::MemoryEmphasis, 1);
228 
229  // time limit
230  cplex_.setParam(IloCplex::TiLim, param_.timeLimit_);
231 
232  // multo-threading options
233  cplex_.setParam(IloCplex::Threads, param_.numberOfThreads_);
234 
235  // Tuning
236  cplex_.setParam(IloCplex::Probe, param_.probeingLevel_);
237  cplex_.setParam(IloCplex::Covers, param_.coverCutLevel_);
238  cplex_.setParam(IloCplex::DisjCuts, param_.disjunctiverCutLevel_);
239  cplex_.setParam(IloCplex::Cliques, param_.cliqueCutLevel_);
240  cplex_.setParam(IloCplex::MIRCuts, param_.MIRCutLevel_);
241 
242  // solve problem
243  if(!cplex_.solve()) {
244  throw RuntimeError( "failed to optimize.");
245  }
246  cplex_.getValues(sol_, x_);
247  }
248  catch(IloCplex::Exception e) {
249  std::cout << "caught CPLEX exception: " << e << std::endl;
250  throw RuntimeError( "caught CPLEX exception:");
251  }
252  }
253 
254  LpValueType lpArg(const LpIndexType lpVi)const{
255  OPENGM_CHECK_OP(lpVi,<,numVar_,"");
256  return sol_[lpVi];
257  }
258 
259  LpValueType lpValue()const{
260  return cplex_.getObjValue();
261  //return cplex_.getBestObjValue();
262  }
263 
264  LpValueType bestLpValue()const{
265  return cplex_.getBestObjValue();
266  }
267 
269  return numVar_;
270  }
271 
273  return constraintCounter_;
274  }
275 
276 
277 private:
278 
279  // mebers of cplex itself
280  IloEnv env_;
281  IloModel model_;
282  IloNumVarArray x_;
283  IloRangeArray c_;
284  IloObjective obj_;
285  IloNumArray sol_;
286  IloCplex cplex_;
287  IloNumArray * objBuffer_;
288  //IloNumArray objBuffer_;
289  // param
290  Parameter param_;
291  UInt64Type numVar_;
292  UInt64Type constraintCounter_;
293 
294 };
295 
296 
297 
298 }
299 #endif
UInt64Type numbefOfVariables() const
The OpenGM namespace.
Definition: config.hxx:43
LpValueType lpArg(const LpIndexType lpVi) const
void addConstraint(const LpValueType lowerBound, const LpValueType upperBound, const std::string &name=std::string())
void addConstraint(LPVariableIndexIterator lpVarBegin, LPVariableIndexIterator lpVarEnd, CoefficientIterator coeffBegin, const LpValueType lowerBound, const LpValueType upperBound, const std::string &name=std::string())
void addVariables(const UInt64Type numVar, const LpVarType varType, const LpValueType lowerBound=1.0, const LpValueType upperBound=1.0)
LpValueType bestLpValue() const
detail_types::UInt64Type UInt64Type
uint64
Definition: config.hxx:300
LpSolverCplex(const Parameter &parameter=Parameter())
void setObjective(const UInt64Type lpVi, const LpValueType obj)
Parameter(int numberOfThreads=0, double cutUp=1.0e+75, double epGap=0)
constructor
#define OPENGM_CHECK_OP(A, OP, B, TXT)
Definition: submodel2.hxx:24
UInt64Type numberOfVariables() const
#define OPENGM_CHECK(B, TXT)
Definition: submodel2.hxx:28
LpValueType lpValue() const
void addToConstraint(const UInt64Type constraintIndex, const UInt64Type lpVarIndex, const LpValueType coeff)
OpenGM runtime error.
Definition: opengm.hxx:100
UInt64Type numberOfConstraints() const