gravatar
alpar (Alpar Juttner)
alpar@cs.elte.hu
Merge
0 4 0
merge default
4 files changed with 18 insertions and 0 deletions:
↑ Collapse diff ↑
Ignore white space 6 line context
... ...
@@ -477,96 +477,102 @@
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

	
529
  GlpkBase::FreeEnvHelper GlpkBase::freeEnvHelper;
530

	
525 531
  // GlpkLp members
526 532

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

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

	
537 543
  GlpkLp* GlpkLp::newSolver() const { return new GlpkLp; }
538 544
  GlpkLp* GlpkLp::cloneSolver() const { return new GlpkLp(*this); }
539 545

	
540 546
  const char* GlpkLp::_solverName() const { return "GlpkLp"; }
541 547

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

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

	
551 557
  GlpkLp::SolveExitStatus GlpkLp::solvePrimal() {
552 558
    _clear_temporals();
553 559

	
554 560
    glp_smcp smcp;
555 561
    glp_init_smcp(&smcp);
556 562

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

	
572 578
    if (glp_simplex(lp, &smcp) != 0) return UNSOLVED;
Ignore white space 96 line context
... ...
@@ -55,96 +55,108 @@
55 55
    virtual int _addRow();
56 56

	
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
  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 117
    ///Pointer to the underlying GLPK data structure.
106 118
    LPX *lpx() {return lp;}
107 119
    ///Const pointer to the underlying GLPK data structure.
108 120
    const LPX *lpx() const {return lp;}
109 121

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

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

	
116 128
  };
117 129

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

	
125 137
    ///\e
126 138
    GlpkLp();
127 139
    ///\e
128 140
    GlpkLp(const GlpkLp&);
129 141

	
130 142
    ///\e
131 143
    virtual GlpkLp* cloneSolver() const;
132 144
    ///\e
133 145
    virtual GlpkLp* newSolver() const;
134 146

	
135 147
  private:
136 148

	
137 149
    mutable std::vector<double> _primal_ray;
138 150
    mutable std::vector<double> _dual_ray;
139 151

	
140 152
    void _clear_temporals();
141 153

	
142 154
  protected:
143 155

	
144 156
    virtual const char* _solverName() const;
145 157

	
146 158
    virtual SolveExitStatus _solve();
147 159
    virtual Value _getPrimal(int i) const;
148 160
    virtual Value _getDual(int i) const;
149 161

	
150 162
    virtual Value _getPrimalValue() const;
0 comments (0 inline)