gravatar
deba@inf.elte.hu
deba@inf.elte.hu
Automatic GLPK env deallocation (#213)
0 4 0
default
4 files changed with 14 insertions and 10 deletions:
↑ Collapse diff ↑
Ignore white space 48 line context
... ...
@@ -505,48 +505,50 @@
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 525
  void GlpkBase::freeEnv() {
526 526
    glp_free_env();
527 527
  }
528 528

	
529
  GlpkBase::FreeEnvHelper GlpkBase::freeEnvHelper;
530

	
529 531
  // GlpkLp members
530 532

	
531 533
  GlpkLp::GlpkLp()
532 534
    : LpBase(), GlpkBase(), LpSolver() {
533 535
    messageLevel(MESSAGE_NO_OUTPUT);
534 536
  }
535 537

	
536 538
  GlpkLp::GlpkLp(const GlpkLp& other)
537 539
    : LpBase(other), GlpkBase(other), LpSolver(other) {
538 540
    messageLevel(MESSAGE_NO_OUTPUT);
539 541
  }
540 542

	
541 543
  GlpkLp* GlpkLp::_newSolver() const { return new GlpkLp; }
542 544
  GlpkLp* GlpkLp::_cloneSolver() const { return new GlpkLp(*this); }
543 545

	
544 546
  const char* GlpkLp::_solverName() const { return "GlpkLp"; }
545 547

	
546 548
  void GlpkLp::_clear_temporals() {
547 549
    _primal_ray.clear();
548 550
    _dual_ray.clear();
549 551
  }
550 552

	
551 553
  GlpkLp::SolveExitStatus GlpkLp::_solve() {
552 554
    return solvePrimal();
Ignore white space 6 line context
... ...
@@ -79,58 +79,62 @@
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
  private:
104

	
105
    static void freeEnv();
106

	
107
    struct FreeEnvHelper {
108
      ~FreeEnvHelper() {
109
        freeEnv();
110
      }
111
    };
112
    
113
    static FreeEnvHelper freeEnvHelper;
114
    
103 115
  public:
104 116

	
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

	
113 117
    ///Pointer to the underlying GLPK data structure.
114 118
    LPX *lpx() {return lp;}
115 119
    ///Const pointer to the underlying GLPK data structure.
116 120
    const LPX *lpx() const {return lp;}
117 121

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

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

	
124 128
  };
125 129

	
126 130
  /// \brief Interface for the GLPK LP solver
127 131
  ///
128 132
  /// This class implements an interface for the GLPK LP solver.
129 133
  ///\ingroup lp_group
130 134
  class GlpkLp : public GlpkBase, public LpSolver {
131 135
  public:
132 136

	
133 137
    ///\e
134 138
    GlpkLp();
135 139
    ///\e
136 140
    GlpkLp(const GlpkLp&);
Ignore white space 6 line context
... ...
@@ -345,49 +345,48 @@
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();
370 369
#endif
371 370

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

	
387 386
#ifdef HAVE_SOPLEX
388 387
  {
389 388
    SoplexLp lp_soplex1,lp_soplex2;
390 389
    lpTest(lp_soplex1);
391 390
    aTest(lp_soplex2);
392 391
  }
393 392
#endif
Ignore white space 6 line context
... ...
@@ -94,44 +94,43 @@
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();
119 118
#endif
120 119

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

	
135 134
  return 0;
136 135

	
137 136
}
0 comments (0 inline)