gravatar
deba@inf.elte.hu
deba@inf.elte.hu
Fix GLPK tests (#213)
0 4 0
default
4 files changed with 14 insertions and 0 deletions:
↑ Collapse diff ↑
Ignore white space 6 line context
... ...
@@ -477,96 +477,100 @@
477 477
    for (int i = 1; i <= glp_get_num_cols(lp); ++i) {
478 478
      Value val = glp_get_obj_coef(lp, i);
479 479
      if (val != 0.0) {
480 480
        *b = std::make_pair(i, val);
481 481
        ++b;
482 482
      }
483 483
    }
484 484
  }
485 485

	
486 486
  void GlpkBase::_setObjCoeff(int i, Value obj_coef) {
487 487
    //i = 0 means the constant term (shift)
488 488
    glp_set_obj_coef(lp, i, obj_coef);
489 489
  }
490 490

	
491 491
  GlpkBase::Value GlpkBase::_getObjCoeff(int i) const {
492 492
    //i = 0 means the constant term (shift)
493 493
    return glp_get_obj_coef(lp, i);
494 494
  }
495 495

	
496 496
  void GlpkBase::_setSense(GlpkBase::Sense sense) {
497 497
    switch (sense) {
498 498
    case MIN:
499 499
      glp_set_obj_dir(lp, GLP_MIN);
500 500
      break;
501 501
    case MAX:
502 502
      glp_set_obj_dir(lp, GLP_MAX);
503 503
      break;
504 504
    }
505 505
  }
506 506

	
507 507
  GlpkBase::Sense GlpkBase::_getSense() const {
508 508
    switch(glp_get_obj_dir(lp)) {
509 509
    case GLP_MIN:
510 510
      return MIN;
511 511
    case GLP_MAX:
512 512
      return MAX;
513 513
    default:
514 514
      LEMON_ASSERT(false, "Wrong sense");
515 515
      return GlpkBase::Sense();
516 516
    }
517 517
  }
518 518

	
519 519
  void GlpkBase::_clear() {
520 520
    glp_erase_prob(lp);
521 521
    rows.clear();
522 522
    cols.clear();
523 523
  }
524 524

	
525
  void GlpkBase::freeEnv() {
526
    glp_free_env();
527
  }
528

	
525 529
  // GlpkLp members
526 530

	
527 531
  GlpkLp::GlpkLp()
528 532
    : LpBase(), GlpkBase(), LpSolver() {
529 533
    messageLevel(MESSAGE_NO_OUTPUT);
530 534
  }
531 535

	
532 536
  GlpkLp::GlpkLp(const GlpkLp& other)
533 537
    : LpBase(other), GlpkBase(other), LpSolver(other) {
534 538
    messageLevel(MESSAGE_NO_OUTPUT);
535 539
  }
536 540

	
537 541
  GlpkLp* GlpkLp::_newSolver() const { return new GlpkLp; }
538 542
  GlpkLp* GlpkLp::_cloneSolver() const { return new GlpkLp(*this); }
539 543

	
540 544
  const char* GlpkLp::_solverName() const { return "GlpkLp"; }
541 545

	
542 546
  void GlpkLp::_clear_temporals() {
543 547
    _primal_ray.clear();
544 548
    _dual_ray.clear();
545 549
  }
546 550

	
547 551
  GlpkLp::SolveExitStatus GlpkLp::_solve() {
548 552
    return solvePrimal();
549 553
  }
550 554

	
551 555
  GlpkLp::SolveExitStatus GlpkLp::solvePrimal() {
552 556
    _clear_temporals();
553 557

	
554 558
    glp_smcp smcp;
555 559
    glp_init_smcp(&smcp);
556 560

	
557 561
    switch (_message_level) {
558 562
    case MESSAGE_NO_OUTPUT:
559 563
      smcp.msg_lev = GLP_MSG_OFF;
560 564
      break;
561 565
    case MESSAGE_ERROR_MESSAGE:
562 566
      smcp.msg_lev = GLP_MSG_ERR;
563 567
      break;
564 568
    case MESSAGE_NORMAL_OUTPUT:
565 569
      smcp.msg_lev = GLP_MSG_ON;
566 570
      break;
567 571
    case MESSAGE_FULL_OUTPUT:
568 572
      smcp.msg_lev = GLP_MSG_ALL;
569 573
      break;
570 574
    }
571 575

	
572 576
    if (glp_simplex(lp, &smcp) != 0) return UNSOLVED;
Ignore white space 6 line context
... ...
@@ -57,96 +57,104 @@
57 57
    virtual void _eraseCol(int i);
58 58
    virtual void _eraseRow(int i);
59 59

	
60 60
    virtual void _eraseColId(int i);
61 61
    virtual void _eraseRowId(int i);
62 62

	
63 63
    virtual void _getColName(int col, std::string& name) const;
64 64
    virtual void _setColName(int col, const std::string& name);
65 65
    virtual int _colByName(const std::string& name) const;
66 66

	
67 67
    virtual void _getRowName(int row, std::string& name) const;
68 68
    virtual void _setRowName(int row, const std::string& name);
69 69
    virtual int _rowByName(const std::string& name) const;
70 70

	
71 71
    virtual void _setRowCoeffs(int i, ExprIterator b, ExprIterator e);
72 72
    virtual void _getRowCoeffs(int i, InsertIterator b) const;
73 73

	
74 74
    virtual void _setColCoeffs(int i, ExprIterator b, ExprIterator e);
75 75
    virtual void _getColCoeffs(int i, InsertIterator b) const;
76 76

	
77 77
    virtual void _setCoeff(int row, int col, Value value);
78 78
    virtual Value _getCoeff(int row, int col) const;
79 79

	
80 80
    virtual void _setColLowerBound(int i, Value value);
81 81
    virtual Value _getColLowerBound(int i) const;
82 82

	
83 83
    virtual void _setColUpperBound(int i, Value value);
84 84
    virtual Value _getColUpperBound(int i) const;
85 85

	
86 86
    virtual void _setRowLowerBound(int i, Value value);
87 87
    virtual Value _getRowLowerBound(int i) const;
88 88

	
89 89
    virtual void _setRowUpperBound(int i, Value value);
90 90
    virtual Value _getRowUpperBound(int i) const;
91 91

	
92 92
    virtual void _setObjCoeffs(ExprIterator b, ExprIterator e);
93 93
    virtual void _getObjCoeffs(InsertIterator b) const;
94 94

	
95 95
    virtual void _setObjCoeff(int i, Value obj_coef);
96 96
    virtual Value _getObjCoeff(int i) const;
97 97

	
98 98
    virtual void _setSense(Sense);
99 99
    virtual Sense _getSense() const;
100 100

	
101 101
    virtual void _clear();
102 102

	
103 103
  public:
104 104

	
105
    /// \brief Deallocates the globally allocated memory of GLPK.
106

	
107
    /// Deallocates the globally allocated memory of GLPK.  \note
108
    /// Usually, it do not have to be called, because the GLPK use
109
    /// only a small amount of global memory, and it is deallocated
110
    /// automatically at the end of program.
111
    static void freeEnv();
112

	
105 113
    ///Pointer to the underlying GLPK data structure.
106 114
    LPX *lpx() {return lp;}
107 115
    ///Const pointer to the underlying GLPK data structure.
108 116
    const LPX *lpx() const {return lp;}
109 117

	
110 118
    ///Returns the constraint identifier understood by GLPK.
111 119
    int lpxRow(Row r) const { return rows(id(r)); }
112 120

	
113 121
    ///Returns the variable identifier understood by GLPK.
114 122
    int lpxCol(Col c) const { return cols(id(c)); }
115 123

	
116 124
  };
117 125

	
118 126
  /// \brief Interface for the GLPK LP solver
119 127
  ///
120 128
  /// This class implements an interface for the GLPK LP solver.
121 129
  ///\ingroup lp_group
122 130
  class GlpkLp : public GlpkBase, public LpSolver {
123 131
  public:
124 132

	
125 133
    ///\e
126 134
    GlpkLp();
127 135
    ///\e
128 136
    GlpkLp(const GlpkLp&);
129 137

	
130 138
  private:
131 139

	
132 140
    mutable std::vector<double> _primal_ray;
133 141
    mutable std::vector<double> _dual_ray;
134 142

	
135 143
    void _clear_temporals();
136 144

	
137 145
  protected:
138 146

	
139 147
    virtual GlpkLp* _cloneSolver() const;
140 148
    virtual GlpkLp* _newSolver() const;
141 149

	
142 150
    virtual const char* _solverName() const;
143 151

	
144 152
    virtual SolveExitStatus _solve();
145 153
    virtual Value _getPrimal(int i) const;
146 154
    virtual Value _getDual(int i) const;
147 155

	
148 156
    virtual Value _getPrimalValue() const;
149 157

	
150 158
    virtual VarStatus _getColStatus(int i) const;
151 159
    virtual VarStatus _getRowStatus(int i) const;
152 160

	
Ignore white space 96 line context
... ...
@@ -321,83 +321,84 @@
321 321
        "The upper bound for the first row should be 1.");
322 322
  e = clp->row(upright);
323 323
  check(e[x1] == 1, "The first coefficient should 1.");
324 324
  check(e[x2] == 1, "The second coefficient should 1.");
325 325

	
326 326
  de = clp->col(x1);
327 327
  check(de[upright] == 1, "The first coefficient should 1.");
328 328

	
329 329
  delete clp;
330 330

	
331 331
  //Maximization of x1+x2
332 332
  //over the triangle with vertices (0,0) (0,1) (1,0)
333 333
  double expected_opt=1;
334 334
  solveAndCheck(lp, LpSolver::OPTIMAL, expected_opt);
335 335

	
336 336
  //Minimization
337 337
  lp.sense(lp.MIN);
338 338
  expected_opt=0;
339 339
  solveAndCheck(lp, LpSolver::OPTIMAL, expected_opt);
340 340

	
341 341
  //Vertex (-1,0) instead of (0,0)
342 342
  lp.colLowerBound(x1, -LpSolver::INF);
343 343
  expected_opt=-1;
344 344
  solveAndCheck(lp, LpSolver::OPTIMAL, expected_opt);
345 345

	
346 346
  //Erase one constraint and return to maximization
347 347
  lp.erase(upright);
348 348
  lp.sense(lp.MAX);
349 349
  expected_opt=LpSolver::INF;
350 350
  solveAndCheck(lp, LpSolver::UNBOUNDED, expected_opt);
351 351

	
352 352
  //Infeasibilty
353 353
  lp.addRow(x1+x2 <=-2);
354 354
  solveAndCheck(lp, LpSolver::INFEASIBLE, expected_opt);
355 355

	
356 356
}
357 357

	
358 358
int main()
359 359
{
360 360
  LpSkeleton lp_skel;
361 361
  lpTest(lp_skel);
362 362

	
363 363
#ifdef HAVE_GLPK
364 364
  {
365 365
    GlpkLp lp_glpk1,lp_glpk2;
366 366
    lpTest(lp_glpk1);
367 367
    aTest(lp_glpk2);
368 368
  }
369
  GlpkLp::freeEnv();
369 370
#endif
370 371

	
371 372
#ifdef HAVE_CPLEX
372 373
  try {
373 374
    CplexLp lp_cplex1,lp_cplex2;
374 375
    lpTest(lp_cplex1);
375 376
    aTest(lp_cplex2);
376 377
  } catch (CplexEnv::LicenseError& error) {
377 378
#ifdef LEMON_FORCE_CPLEX_CHECK
378 379
    check(false, error.what());
379 380
#else
380 381
    std::cerr << error.what() << std::endl;
381 382
    std::cerr << "Cplex license check failed, lp check skipped" << std::endl;
382 383
#endif
383 384
  }
384 385
#endif
385 386

	
386 387
#ifdef HAVE_SOPLEX
387 388
  {
388 389
    SoplexLp lp_soplex1,lp_soplex2;
389 390
    lpTest(lp_soplex1);
390 391
    aTest(lp_soplex2);
391 392
  }
392 393
#endif
393 394

	
394 395
#ifdef HAVE_CLP
395 396
  {
396 397
    ClpLp lp_clp1,lp_clp2;
397 398
    lpTest(lp_clp1);
398 399
    aTest(lp_clp2);
399 400
  }
400 401
#endif
401 402

	
402 403
  return 0;
403 404
}
Ignore white space 6 line context
... ...
@@ -70,67 +70,68 @@
70 70

	
71 71

	
72 72
  //Objective function
73 73
  mip.obj(x1);
74 74

	
75 75
  mip.max();
76 76

	
77 77

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

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

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

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

	
94 94
  //Restrict x2 to integer
95 95
  mip.colType(x2,MipSolver::INTEGER);
96 96
  expected_opt=1.0/2.0;
97 97
  solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt);
98 98

	
99 99

	
100 100
  //Restrict both to integer
101 101
  mip.colType(x1,MipSolver::INTEGER);
102 102
  expected_opt=0;
103 103
  solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt);
104 104

	
105 105

	
106 106

	
107 107
}
108 108

	
109 109

	
110 110
int main()
111 111
{
112 112

	
113 113
#ifdef HAVE_GLPK
114 114
  {
115 115
    GlpkMip mip1;
116 116
    aTest(mip1);
117 117
  }
118
  GlpkLp::freeEnv();
118 119
#endif
119 120

	
120 121
#ifdef HAVE_CPLEX
121 122
  try {
122 123
    CplexMip mip2;
123 124
    aTest(mip2);
124 125
  } catch (CplexEnv::LicenseError& error) {
125 126
#ifdef LEMON_FORCE_CPLEX_CHECK
126 127
    check(false, error.what());
127 128
#else
128 129
    std::cerr << error.what() << std::endl;
129 130
    std::cerr << "Cplex license check failed, lp check skipped" << std::endl;
130 131
#endif
131 132
  }
132 133
#endif
133 134

	
134 135
  return 0;
135 136

	
136 137
}
0 comments (0 inline)