gravatar
deba@inf.elte.hu
deba@inf.elte.hu
Force cplex checking (#260)
0 2 0
default
2 files changed with 0 insertions and 10 deletions:
↑ Collapse diff ↑
Ignore white space 98304 line context
1 1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
2 2
 *
3 3
 * This file is a part of LEMON, a generic C++ optimization library.
4 4
 *
5 5
 * Copyright (C) 2003-2009
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
#include <sstream>
20 20
#include <lemon/lp_skeleton.h>
21 21
#include "test_tools.h"
22 22
#include <lemon/tolerance.h>
23 23

	
24 24
#ifdef HAVE_CONFIG_H
25 25
#include <lemon/config.h>
26 26
#endif
27 27

	
28 28
#ifdef HAVE_GLPK
29 29
#include <lemon/glpk.h>
30 30
#endif
31 31

	
32 32
#ifdef HAVE_CPLEX
33 33
#include <lemon/cplex.h>
34 34
#endif
35 35

	
36 36
#ifdef HAVE_SOPLEX
37 37
#include <lemon/soplex.h>
38 38
#endif
39 39

	
40 40
#ifdef HAVE_CLP
41 41
#include <lemon/clp.h>
42 42
#endif
43 43

	
44 44
using namespace lemon;
45 45

	
46 46
void lpTest(LpSolver& lp)
47 47
{
48 48

	
49 49
  typedef LpSolver LP;
50 50

	
51 51
  std::vector<LP::Col> x(10);
52 52
  //  for(int i=0;i<10;i++) x.push_back(lp.addCol());
53 53
  lp.addColSet(x);
54 54
  lp.colLowerBound(x,1);
55 55
  lp.colUpperBound(x,1);
56 56
  lp.colBounds(x,1,2);
57 57

	
58 58
  std::vector<LP::Col> y(10);
59 59
  lp.addColSet(y);
60 60

	
61 61
  lp.colLowerBound(y,1);
62 62
  lp.colUpperBound(y,1);
63 63
  lp.colBounds(y,1,2);
64 64

	
65 65
  std::map<int,LP::Col> z;
66 66

	
67 67
  z.insert(std::make_pair(12,INVALID));
68 68
  z.insert(std::make_pair(2,INVALID));
69 69
  z.insert(std::make_pair(7,INVALID));
70 70
  z.insert(std::make_pair(5,INVALID));
71 71

	
72 72
  lp.addColSet(z);
73 73

	
74 74
  lp.colLowerBound(z,1);
75 75
  lp.colUpperBound(z,1);
76 76
  lp.colBounds(z,1,2);
77 77

	
78 78
  {
79 79
    LP::Expr e,f,g;
80 80
    LP::Col p1,p2,p3,p4,p5;
81 81
    LP::Constr c;
82 82

	
83 83
    p1=lp.addCol();
84 84
    p2=lp.addCol();
85 85
    p3=lp.addCol();
86 86
    p4=lp.addCol();
87 87
    p5=lp.addCol();
88 88

	
89 89
    e[p1]=2;
90 90
    *e=12;
91 91
    e[p1]+=2;
92 92
    *e+=12;
93 93
    e[p1]-=2;
94 94
    *e-=12;
95 95

	
96 96
    e=2;
97 97
    e=2.2;
98 98
    e=p1;
99 99
    e=f;
100 100

	
101 101
    e+=2;
102 102
    e+=2.2;
103 103
    e+=p1;
104 104
    e+=f;
105 105

	
106 106
    e-=2;
107 107
    e-=2.2;
108 108
    e-=p1;
109 109
    e-=f;
110 110

	
111 111
    e*=2;
112 112
    e*=2.2;
113 113
    e/=2;
114 114
    e/=2.2;
115 115

	
116 116
    e=((p1+p2)+(p1-p2)+(p1+12)+(12+p1)+(p1-12)+(12-p1)+
117 117
       (f+12)+(12+f)+(p1+f)+(f+p1)+(f+g)+
118 118
       (f-12)+(12-f)+(p1-f)+(f-p1)+(f-g)+
119 119
       2.2*f+f*2.2+f/2.2+
120 120
       2*f+f*2+f/2+
121 121
       2.2*p1+p1*2.2+p1/2.2+
122 122
       2*p1+p1*2+p1/2
123 123
       );
124 124

	
125 125

	
126 126
    c = (e  <= f  );
127 127
    c = (e  <= 2.2);
128 128
    c = (e  <= 2  );
129 129
    c = (e  <= p1 );
130 130
    c = (2.2<= f  );
131 131
    c = (2  <= f  );
132 132
    c = (p1 <= f  );
133 133
    c = (p1 <= p2 );
134 134
    c = (p1 <= 2.2);
135 135
    c = (p1 <= 2  );
136 136
    c = (2.2<= p2 );
137 137
    c = (2  <= p2 );
138 138

	
139 139
    c = (e  >= f  );
140 140
    c = (e  >= 2.2);
141 141
    c = (e  >= 2  );
142 142
    c = (e  >= p1 );
143 143
    c = (2.2>= f  );
144 144
    c = (2  >= f  );
145 145
    c = (p1 >= f  );
146 146
    c = (p1 >= p2 );
147 147
    c = (p1 >= 2.2);
148 148
    c = (p1 >= 2  );
149 149
    c = (2.2>= p2 );
150 150
    c = (2  >= p2 );
151 151

	
152 152
    c = (e  == f  );
153 153
    c = (e  == 2.2);
154 154
    c = (e  == 2  );
155 155
    c = (e  == p1 );
156 156
    c = (2.2== f  );
157 157
    c = (2  == f  );
158 158
    c = (p1 == f  );
159 159
    //c = (p1 == p2 );
160 160
    c = (p1 == 2.2);
161 161
    c = (p1 == 2  );
162 162
    c = (2.2== p2 );
163 163
    c = (2  == p2 );
164 164

	
165 165
    c = ((2 <= e) <= 3);
166 166
    c = ((2 <= p1) <= 3);
167 167

	
168 168
    c = ((2 >= e) >= 3);
169 169
    c = ((2 >= p1) >= 3);
170 170

	
171 171
    e[x[3]]=2;
172 172
    e[x[3]]=4;
173 173
    e[x[3]]=1;
174 174
    *e=12;
175 175

	
176 176
    lp.addRow(-LP::INF,e,23);
177 177
    lp.addRow(-LP::INF,3.0*(x[1]+x[2]/2)-x[3],23);
178 178
    lp.addRow(-LP::INF,3.0*(x[1]+x[2]*2-5*x[3]+12-x[4]/3)+2*x[4]-4,23);
179 179

	
180 180
    lp.addRow(x[1]+x[3]<=x[5]-3);
181 181
    lp.addRow((-7<=x[1]+x[3]-12)<=3);
182 182
    lp.addRow(x[1]<=x[5]);
183 183

	
184 184
    std::ostringstream buf;
185 185

	
186 186

	
187 187
    e=((p1+p2)+(p1-0.99*p2));
188 188
    //e.prettyPrint(std::cout);
189 189
    //(e<=2).prettyPrint(std::cout);
190 190
    double tolerance=0.001;
191 191
    e.simplify(tolerance);
192 192
    buf << "Coeff. of p2 should be 0.01";
193 193
    check(e[p2]>0, buf.str());
194 194

	
195 195
    tolerance=0.02;
196 196
    e.simplify(tolerance);
197 197
    buf << "Coeff. of p2 should be 0";
198 198
    check(const_cast<const LpSolver::Expr&>(e)[p2]==0, buf.str());
199 199

	
200 200
    //Test for clone/new
201 201
    LP* lpnew = lp.newSolver();
202 202
    LP* lpclone = lp.cloneSolver();
203 203
    delete lpnew;
204 204
    delete lpclone;
205 205

	
206 206
  }
207 207

	
208 208
  {
209 209
    LP::DualExpr e,f,g;
210 210
    LP::Row p1 = INVALID, p2 = INVALID, p3 = INVALID,
211 211
      p4 = INVALID, p5 = INVALID;
212 212

	
213 213
    e[p1]=2;
214 214
    e[p1]+=2;
215 215
    e[p1]-=2;
216 216

	
217 217
    e=p1;
218 218
    e=f;
219 219

	
220 220
    e+=p1;
221 221
    e+=f;
222 222

	
223 223
    e-=p1;
224 224
    e-=f;
225 225

	
226 226
    e*=2;
227 227
    e*=2.2;
228 228
    e/=2;
229 229
    e/=2.2;
230 230

	
231 231
    e=((p1+p2)+(p1-p2)+
232 232
       (p1+f)+(f+p1)+(f+g)+
233 233
       (p1-f)+(f-p1)+(f-g)+
234 234
       2.2*f+f*2.2+f/2.2+
235 235
       2*f+f*2+f/2+
236 236
       2.2*p1+p1*2.2+p1/2.2+
237 237
       2*p1+p1*2+p1/2
238 238
       );
239 239
  }
240 240

	
241 241
}
242 242

	
243 243
void solveAndCheck(LpSolver& lp, LpSolver::ProblemType stat,
244 244
                   double exp_opt) {
245 245
  using std::string;
246 246
  lp.solve();
247 247

	
248 248
  std::ostringstream buf;
249 249
  buf << "PrimalType should be: " << int(stat) << int(lp.primalType());
250 250

	
251 251
  check(lp.primalType()==stat, buf.str());
252 252

	
253 253
  if (stat ==  LpSolver::OPTIMAL) {
254 254
    std::ostringstream sbuf;
255 255
    sbuf << "Wrong optimal value (" << lp.primal() <<") with "
256 256
         << lp.solverName() <<"\n     the right optimum is " << exp_opt;
257 257
    check(std::abs(lp.primal()-exp_opt) < 1e-3, sbuf.str());
258 258
  }
259 259
}
260 260

	
261 261
void aTest(LpSolver & lp)
262 262
{
263 263
  typedef LpSolver LP;
264 264

	
265 265
 //The following example is very simple
266 266

	
267 267
  typedef LpSolver::Row Row;
268 268
  typedef LpSolver::Col Col;
269 269

	
270 270

	
271 271
  Col x1 = lp.addCol();
272 272
  Col x2 = lp.addCol();
273 273

	
274 274

	
275 275
  //Constraints
276 276
  Row upright=lp.addRow(x1+2*x2 <=1);
277 277
  lp.addRow(x1+x2 >=-1);
278 278
  lp.addRow(x1-x2 <=1);
279 279
  lp.addRow(x1-x2 >=-1);
280 280
  //Nonnegativity of the variables
281 281
  lp.colLowerBound(x1, 0);
282 282
  lp.colLowerBound(x2, 0);
283 283
  //Objective function
284 284
  lp.obj(x1+x2);
285 285

	
286 286
  lp.sense(lp.MAX);
287 287

	
288 288
  //Testing the problem retrieving routines
289 289
  check(lp.objCoeff(x1)==1,"First term should be 1 in the obj function!");
290 290
  check(lp.sense() == lp.MAX,"This is a maximization!");
291 291
  check(lp.coeff(upright,x1)==1,"The coefficient in question is 1!");
292 292
  check(lp.colLowerBound(x1)==0,
293 293
        "The lower bound for variable x1 should be 0.");
294 294
  check(lp.colUpperBound(x1)==LpSolver::INF,
295 295
        "The upper bound for variable x1 should be infty.");
296 296
  check(lp.rowLowerBound(upright) == -LpSolver::INF,
297 297
        "The lower bound for the first row should be -infty.");
298 298
  check(lp.rowUpperBound(upright)==1,
299 299
        "The upper bound for the first row should be 1.");
300 300
  LpSolver::Expr e = lp.row(upright);
301 301
  check(e[x1] == 1, "The first coefficient should 1.");
302 302
  check(e[x2] == 2, "The second coefficient should 1.");
303 303

	
304 304
  lp.row(upright, x1+x2 <=1);
305 305
  e = lp.row(upright);
306 306
  check(e[x1] == 1, "The first coefficient should 1.");
307 307
  check(e[x2] == 1, "The second coefficient should 1.");
308 308

	
309 309
  LpSolver::DualExpr de = lp.col(x1);
310 310
  check(  de[upright] == 1, "The first coefficient should 1.");
311 311

	
312 312
  LpSolver* clp = lp.cloneSolver();
313 313

	
314 314
  //Testing the problem retrieving routines
315 315
  check(clp->objCoeff(x1)==1,"First term should be 1 in the obj function!");
316 316
  check(clp->sense() == clp->MAX,"This is a maximization!");
317 317
  check(clp->coeff(upright,x1)==1,"The coefficient in question is 1!");
318 318
  //  std::cout<<lp.colLowerBound(x1)<<std::endl;
319 319
  check(clp->colLowerBound(x1)==0,
320 320
        "The lower bound for variable x1 should be 0.");
321 321
  check(clp->colUpperBound(x1)==LpSolver::INF,
322 322
        "The upper bound for variable x1 should be infty.");
323 323

	
324 324
  check(lp.rowLowerBound(upright)==-LpSolver::INF,
325 325
        "The lower bound for the first row should be -infty.");
326 326
  check(lp.rowUpperBound(upright)==1,
327 327
        "The upper bound for the first row should be 1.");
328 328
  e = clp->row(upright);
329 329
  check(e[x1] == 1, "The first coefficient should 1.");
330 330
  check(e[x2] == 1, "The second coefficient should 1.");
331 331

	
332 332
  de = clp->col(x1);
333 333
  check(de[upright] == 1, "The first coefficient should 1.");
334 334

	
335 335
  delete clp;
336 336

	
337 337
  //Maximization of x1+x2
338 338
  //over the triangle with vertices (0,0) (0,1) (1,0)
339 339
  double expected_opt=1;
340 340
  solveAndCheck(lp, LpSolver::OPTIMAL, expected_opt);
341 341

	
342 342
  //Minimization
343 343
  lp.sense(lp.MIN);
344 344
  expected_opt=0;
345 345
  solveAndCheck(lp, LpSolver::OPTIMAL, expected_opt);
346 346

	
347 347
  //Vertex (-1,0) instead of (0,0)
348 348
  lp.colLowerBound(x1, -LpSolver::INF);
349 349
  expected_opt=-1;
350 350
  solveAndCheck(lp, LpSolver::OPTIMAL, expected_opt);
351 351

	
352 352
  //Erase one constraint and return to maximization
353 353
  lp.erase(upright);
354 354
  lp.sense(lp.MAX);
355 355
  expected_opt=LpSolver::INF;
356 356
  solveAndCheck(lp, LpSolver::UNBOUNDED, expected_opt);
357 357

	
358 358
  //Infeasibilty
359 359
  lp.addRow(x1+x2 <=-2);
360 360
  solveAndCheck(lp, LpSolver::INFEASIBLE, expected_opt);
361 361

	
362 362
}
363 363

	
364 364
template<class LP>
365 365
void cloneTest()
366 366
{
367 367
  //Test for clone/new
368 368

	
369 369
  LP* lp = new LP();
370 370
  LP* lpnew = lp->newSolver();
371 371
  LP* lpclone = lp->cloneSolver();
372 372
  delete lp;
373 373
  delete lpnew;
374 374
  delete lpclone;
375 375
}
376 376

	
377 377
int main()
378 378
{
379 379
  LpSkeleton lp_skel;
380 380
  lpTest(lp_skel);
381 381

	
382 382
#ifdef HAVE_GLPK
383 383
  {
384 384
    GlpkLp lp_glpk1,lp_glpk2;
385 385
    lpTest(lp_glpk1);
386 386
    aTest(lp_glpk2);
387 387
    cloneTest<GlpkLp>();
388 388
  }
389 389
#endif
390 390

	
391 391
#ifdef HAVE_CPLEX
392 392
  try {
393 393
    CplexLp lp_cplex1,lp_cplex2;
394 394
    lpTest(lp_cplex1);
395 395
    aTest(lp_cplex2);
396 396
    cloneTest<CplexLp>();
397 397
  } catch (CplexEnv::LicenseError& error) {
398
#ifdef LEMON_FORCE_CPLEX_CHECK
399 398
    check(false, error.what());
400
#else
401
    std::cerr << error.what() << std::endl;
402
    std::cerr << "Cplex license check failed, lp check skipped" << std::endl;
403
#endif
404 399
  }
405 400
#endif
406 401

	
407 402
#ifdef HAVE_SOPLEX
408 403
  {
409 404
    SoplexLp lp_soplex1,lp_soplex2;
410 405
    lpTest(lp_soplex1);
411 406
    aTest(lp_soplex2);
412 407
    cloneTest<SoplexLp>();
413 408
  }
414 409
#endif
415 410

	
416 411
#ifdef HAVE_CLP
417 412
  {
418 413
    ClpLp lp_clp1,lp_clp2;
419 414
    lpTest(lp_clp1);
420 415
    aTest(lp_clp2);
421 416
    cloneTest<ClpLp>();
422 417
  }
423 418
#endif
424 419

	
425 420
  return 0;
426 421
}
Ignore white space 6 line context
1 1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
2 2
 *
3 3
 * This file is a part of LEMON, a generic C++ optimization library.
4 4
 *
5 5
 * Copyright (C) 2003-2009
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
#include "test_tools.h"
20 20

	
21 21
#ifdef HAVE_CONFIG_H
22 22
#include <lemon/config.h>
23 23
#endif
24 24

	
25 25
#ifdef HAVE_CPLEX
26 26
#include <lemon/cplex.h>
27 27
#endif
28 28

	
29 29
#ifdef HAVE_GLPK
30 30
#include <lemon/glpk.h>
31 31
#endif
32 32

	
33 33
#ifdef HAVE_CBC
34 34
#include <lemon/cbc.h>
35 35
#endif
36 36

	
37 37

	
38 38
using namespace lemon;
39 39

	
40 40
void solveAndCheck(MipSolver& mip, MipSolver::ProblemType stat,
41 41
                   double exp_opt) {
42 42
  using std::string;
43 43

	
44 44
  mip.solve();
45 45
  //int decimal,sign;
46 46
  std::ostringstream buf;
47 47
  buf << "Type should be: " << int(stat)<<" and it is "<<int(mip.type());
48 48

	
49 49

	
50 50
  //  itoa(stat,buf1, 10);
51 51
  check(mip.type()==stat, buf.str());
52 52

	
53 53
  if (stat ==  MipSolver::OPTIMAL) {
54 54
    std::ostringstream sbuf;
55 55
    buf << "Wrong optimal value: the right optimum is " << exp_opt;
56 56
    check(std::abs(mip.solValue()-exp_opt) < 1e-3, sbuf.str());
57 57
    //+ecvt(exp_opt,2)
58 58
  }
59 59
}
60 60

	
61 61
void aTest(MipSolver& mip)
62 62
{
63 63
  //The following example is very simple
64 64

	
65 65

	
66 66
  typedef MipSolver::Row Row;
67 67
  typedef MipSolver::Col Col;
68 68

	
69 69

	
70 70
  Col x1 = mip.addCol();
71 71
  Col x2 = mip.addCol();
72 72

	
73 73

	
74 74
  //Objective function
75 75
  mip.obj(x1);
76 76

	
77 77
  mip.max();
78 78

	
79 79
  //Unconstrained optimization
80 80
  mip.solve();
81 81
  //Check it out!
82 82

	
83 83
  //Constraints
84 84
  mip.addRow(2 * x1 + x2 <= 2);
85 85
  Row y2 = mip.addRow(x1 - 2 * x2 <= 0);
86 86

	
87 87
  //Nonnegativity of the variable x1
88 88
  mip.colLowerBound(x1, 0);
89 89

	
90 90

	
91 91
  //Maximization of x1
92 92
  //over the triangle with vertices (0,0),(4/5,2/5),(0,2)
93 93
  double expected_opt=4.0/5.0;
94 94
  solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt);
95 95

	
96 96

	
97 97
  //Restrict x2 to integer
98 98
  mip.colType(x2,MipSolver::INTEGER);
99 99
  expected_opt=1.0/2.0;
100 100
  solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt);
101 101

	
102 102

	
103 103
  //Restrict both to integer
104 104
  mip.colType(x1,MipSolver::INTEGER);
105 105
  expected_opt=0;
106 106
  solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt);
107 107

	
108 108
  //Erase a variable
109 109
  mip.erase(x2);
110 110
  mip.rowUpperBound(y2, 8);
111 111
  expected_opt=1;
112 112
  solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt);
113 113

	
114 114
}
115 115

	
116 116

	
117 117
template<class MIP>
118 118
void cloneTest()
119 119
{
120 120

	
121 121
  MIP* mip = new MIP();
122 122
  MIP* mipnew = mip->newSolver();
123 123
  MIP* mipclone = mip->cloneSolver();
124 124
  delete mip;
125 125
  delete mipnew;
126 126
  delete mipclone;
127 127
}
128 128

	
129 129
int main()
130 130
{
131 131

	
132 132
#ifdef HAVE_GLPK
133 133
  {
134 134
    GlpkMip mip1;
135 135
    aTest(mip1);
136 136
    cloneTest<GlpkMip>();
137 137
  }
138 138
#endif
139 139

	
140 140
#ifdef HAVE_CPLEX
141 141
  try {
142 142
    CplexMip mip2;
143 143
    aTest(mip2);
144 144
    cloneTest<CplexMip>();
145 145
  } catch (CplexEnv::LicenseError& error) {
146
#ifdef LEMON_FORCE_CPLEX_CHECK
147 146
    check(false, error.what());
148
#else
149
    std::cerr << error.what() << std::endl;
150
    std::cerr << "Cplex license check failed, lp check skipped" << std::endl;
151
#endif
152 147
  }
153 148
#endif
154 149

	
155 150
#ifdef HAVE_CBC
156 151
  {
157 152
    CbcMip mip1;
158 153
    aTest(mip1);
159 154
    cloneTest<CbcMip>();
160 155
  }
161 156
#endif
162 157

	
163 158
  return 0;
164 159

	
165 160
}
0 comments (0 inline)