0
4
0
... | ... |
@@ -433,192 +433,194 @@ |
433 | 433 |
} else { |
434 | 434 |
switch (b) { |
435 | 435 |
case GLP_FR: |
436 | 436 |
glp_set_row_bnds(lp, i, GLP_UP, lo, up); |
437 | 437 |
break; |
438 | 438 |
case GLP_UP: |
439 | 439 |
glp_set_row_bnds(lp, i, GLP_UP, lo, up); |
440 | 440 |
break; |
441 | 441 |
case GLP_LO: |
442 | 442 |
case GLP_DB: |
443 | 443 |
case GLP_FX: |
444 | 444 |
if (lo == up) |
445 | 445 |
glp_set_row_bnds(lp, i, GLP_FX, lo, up); |
446 | 446 |
else |
447 | 447 |
glp_set_row_bnds(lp, i, GLP_DB, lo, up); |
448 | 448 |
break; |
449 | 449 |
default: |
450 | 450 |
break; |
451 | 451 |
} |
452 | 452 |
} |
453 | 453 |
} |
454 | 454 |
|
455 | 455 |
GlpkBase::Value GlpkBase::_getRowUpperBound(int i) const { |
456 | 456 |
int b = glp_get_row_type(lp, i); |
457 | 457 |
switch (b) { |
458 | 458 |
case GLP_UP: |
459 | 459 |
case GLP_DB: |
460 | 460 |
case GLP_FX: |
461 | 461 |
return glp_get_row_ub(lp, i); |
462 | 462 |
default: |
463 | 463 |
return INF; |
464 | 464 |
} |
465 | 465 |
} |
466 | 466 |
|
467 | 467 |
void GlpkBase::_setObjCoeffs(ExprIterator b, ExprIterator e) { |
468 | 468 |
for (int i = 1; i <= glp_get_num_cols(lp); ++i) { |
469 | 469 |
glp_set_obj_coef(lp, i, 0.0); |
470 | 470 |
} |
471 | 471 |
for (ExprIterator it = b; it != e; ++it) { |
472 | 472 |
glp_set_obj_coef(lp, it->first, it->second); |
473 | 473 |
} |
474 | 474 |
} |
475 | 475 |
|
476 | 476 |
void GlpkBase::_getObjCoeffs(InsertIterator b) const { |
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 | 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(); |
553 | 555 |
} |
554 | 556 |
|
555 | 557 |
GlpkLp::SolveExitStatus GlpkLp::solvePrimal() { |
556 | 558 |
_clear_temporals(); |
557 | 559 |
|
558 | 560 |
glp_smcp smcp; |
559 | 561 |
glp_init_smcp(&smcp); |
560 | 562 |
|
561 | 563 |
switch (_message_level) { |
562 | 564 |
case MESSAGE_NO_OUTPUT: |
563 | 565 |
smcp.msg_lev = GLP_MSG_OFF; |
564 | 566 |
break; |
565 | 567 |
case MESSAGE_ERROR_MESSAGE: |
566 | 568 |
smcp.msg_lev = GLP_MSG_ERR; |
567 | 569 |
break; |
568 | 570 |
case MESSAGE_NORMAL_OUTPUT: |
569 | 571 |
smcp.msg_lev = GLP_MSG_ON; |
570 | 572 |
break; |
571 | 573 |
case MESSAGE_FULL_OUTPUT: |
572 | 574 |
smcp.msg_lev = GLP_MSG_ALL; |
573 | 575 |
break; |
574 | 576 |
} |
575 | 577 |
|
576 | 578 |
if (glp_simplex(lp, &smcp) != 0) return UNSOLVED; |
577 | 579 |
return SOLVED; |
578 | 580 |
} |
579 | 581 |
|
580 | 582 |
GlpkLp::SolveExitStatus GlpkLp::solveDual() { |
581 | 583 |
_clear_temporals(); |
582 | 584 |
|
583 | 585 |
glp_smcp smcp; |
584 | 586 |
glp_init_smcp(&smcp); |
585 | 587 |
|
586 | 588 |
switch (_message_level) { |
587 | 589 |
case MESSAGE_NO_OUTPUT: |
588 | 590 |
smcp.msg_lev = GLP_MSG_OFF; |
589 | 591 |
break; |
590 | 592 |
case MESSAGE_ERROR_MESSAGE: |
591 | 593 |
smcp.msg_lev = GLP_MSG_ERR; |
592 | 594 |
break; |
593 | 595 |
case MESSAGE_NORMAL_OUTPUT: |
594 | 596 |
smcp.msg_lev = GLP_MSG_ON; |
595 | 597 |
break; |
596 | 598 |
case MESSAGE_FULL_OUTPUT: |
597 | 599 |
smcp.msg_lev = GLP_MSG_ALL; |
598 | 600 |
break; |
599 | 601 |
} |
600 | 602 |
smcp.meth = GLP_DUAL; |
601 | 603 |
|
602 | 604 |
if (glp_simplex(lp, &smcp) != 0) return UNSOLVED; |
603 | 605 |
return SOLVED; |
604 | 606 |
} |
605 | 607 |
|
606 | 608 |
GlpkLp::Value GlpkLp::_getPrimal(int i) const { |
607 | 609 |
return glp_get_col_prim(lp, i); |
608 | 610 |
} |
609 | 611 |
|
610 | 612 |
GlpkLp::Value GlpkLp::_getDual(int i) const { |
611 | 613 |
return glp_get_row_dual(lp, i); |
612 | 614 |
} |
613 | 615 |
|
614 | 616 |
GlpkLp::Value GlpkLp::_getPrimalValue() const { |
615 | 617 |
return glp_get_obj_val(lp); |
616 | 618 |
} |
617 | 619 |
|
618 | 620 |
GlpkLp::VarStatus GlpkLp::_getColStatus(int i) const { |
619 | 621 |
switch (glp_get_col_stat(lp, i)) { |
620 | 622 |
case GLP_BS: |
621 | 623 |
return BASIC; |
622 | 624 |
case GLP_UP: |
623 | 625 |
return UPPER; |
624 | 626 |
case GLP_LO: |
... | ... |
@@ -7,202 +7,206 @@ |
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 |
#ifndef LEMON_GLPK_H |
20 | 20 |
#define LEMON_GLPK_H |
21 | 21 |
|
22 | 22 |
///\file |
23 | 23 |
///\brief Header of the LEMON-GLPK lp solver interface. |
24 | 24 |
///\ingroup lp_group |
25 | 25 |
|
26 | 26 |
#include <lemon/lp_base.h> |
27 | 27 |
|
28 | 28 |
// forward declaration |
29 | 29 |
#ifndef _GLP_PROB |
30 | 30 |
#define _GLP_PROB |
31 | 31 |
typedef struct { double _prob; } glp_prob; |
32 | 32 |
/* LP/MIP problem object */ |
33 | 33 |
#endif |
34 | 34 |
|
35 | 35 |
namespace lemon { |
36 | 36 |
|
37 | 37 |
|
38 | 38 |
/// \brief Base interface for the GLPK LP and MIP solver |
39 | 39 |
/// |
40 | 40 |
/// This class implements the common interface of the GLPK LP and MIP solver. |
41 | 41 |
/// \ingroup lp_group |
42 | 42 |
class GlpkBase : virtual public LpBase { |
43 | 43 |
protected: |
44 | 44 |
|
45 | 45 |
typedef glp_prob LPX; |
46 | 46 |
glp_prob* lp; |
47 | 47 |
|
48 | 48 |
GlpkBase(); |
49 | 49 |
GlpkBase(const GlpkBase&); |
50 | 50 |
virtual ~GlpkBase(); |
51 | 51 |
|
52 | 52 |
protected: |
53 | 53 |
|
54 | 54 |
virtual int _addCol(); |
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 |
/// \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&); |
137 | 141 |
|
138 | 142 |
private: |
139 | 143 |
|
140 | 144 |
mutable std::vector<double> _primal_ray; |
141 | 145 |
mutable std::vector<double> _dual_ray; |
142 | 146 |
|
143 | 147 |
void _clear_temporals(); |
144 | 148 |
|
145 | 149 |
protected: |
146 | 150 |
|
147 | 151 |
virtual GlpkLp* _cloneSolver() const; |
148 | 152 |
virtual GlpkLp* _newSolver() const; |
149 | 153 |
|
150 | 154 |
virtual const char* _solverName() const; |
151 | 155 |
|
152 | 156 |
virtual SolveExitStatus _solve(); |
153 | 157 |
virtual Value _getPrimal(int i) const; |
154 | 158 |
virtual Value _getDual(int i) const; |
155 | 159 |
|
156 | 160 |
virtual Value _getPrimalValue() const; |
157 | 161 |
|
158 | 162 |
virtual VarStatus _getColStatus(int i) const; |
159 | 163 |
virtual VarStatus _getRowStatus(int i) const; |
160 | 164 |
|
161 | 165 |
virtual Value _getPrimalRay(int i) const; |
162 | 166 |
virtual Value _getDualRay(int i) const; |
163 | 167 |
|
164 | 168 |
///\todo It should be clarified |
165 | 169 |
/// |
166 | 170 |
virtual ProblemType _getPrimalType() const; |
167 | 171 |
virtual ProblemType _getDualType() const; |
168 | 172 |
|
169 | 173 |
public: |
170 | 174 |
|
171 | 175 |
///Solve with primal simplex |
172 | 176 |
SolveExitStatus solvePrimal(); |
173 | 177 |
|
174 | 178 |
///Solve with dual simplex |
175 | 179 |
SolveExitStatus solveDual(); |
176 | 180 |
|
177 | 181 |
///Turns on or off the presolver |
178 | 182 |
|
179 | 183 |
///Turns on (\c b is \c true) or off (\c b is \c false) the presolver |
180 | 184 |
/// |
181 | 185 |
///The presolver is off by default. |
182 | 186 |
void presolver(bool b); |
183 | 187 |
|
184 | 188 |
///Enum for \c messageLevel() parameter |
185 | 189 |
enum MessageLevel { |
186 | 190 |
/// no output (default value) |
187 | 191 |
MESSAGE_NO_OUTPUT = 0, |
188 | 192 |
/// error messages only |
189 | 193 |
MESSAGE_ERROR_MESSAGE = 1, |
190 | 194 |
/// normal output |
191 | 195 |
MESSAGE_NORMAL_OUTPUT = 2, |
192 | 196 |
/// full output (includes informational messages) |
193 | 197 |
MESSAGE_FULL_OUTPUT = 3 |
194 | 198 |
}; |
195 | 199 |
|
196 | 200 |
private: |
197 | 201 |
|
198 | 202 |
MessageLevel _message_level; |
199 | 203 |
|
200 | 204 |
public: |
201 | 205 |
|
202 | 206 |
///Set the verbosity of the messages |
203 | 207 |
|
204 | 208 |
///Set the verbosity of the messages |
205 | 209 |
/// |
206 | 210 |
///\param m is the level of the messages output by the solver routines. |
207 | 211 |
void messageLevel(MessageLevel m); |
208 | 212 |
}; |
... | ... |
@@ -273,132 +273,131 @@ |
273 | 273 |
lp.addRow(x1-x2 >=-1); |
274 | 274 |
//Nonnegativity of the variables |
275 | 275 |
lp.colLowerBound(x1, 0); |
276 | 276 |
lp.colLowerBound(x2, 0); |
277 | 277 |
//Objective function |
278 | 278 |
lp.obj(x1+x2); |
279 | 279 |
|
280 | 280 |
lp.sense(lp.MAX); |
281 | 281 |
|
282 | 282 |
//Testing the problem retrieving routines |
283 | 283 |
check(lp.objCoeff(x1)==1,"First term should be 1 in the obj function!"); |
284 | 284 |
check(lp.sense() == lp.MAX,"This is a maximization!"); |
285 | 285 |
check(lp.coeff(upright,x1)==1,"The coefficient in question is 1!"); |
286 | 286 |
check(lp.colLowerBound(x1)==0, |
287 | 287 |
"The lower bound for variable x1 should be 0."); |
288 | 288 |
check(lp.colUpperBound(x1)==LpSolver::INF, |
289 | 289 |
"The upper bound for variable x1 should be infty."); |
290 | 290 |
check(lp.rowLowerBound(upright) == -LpSolver::INF, |
291 | 291 |
"The lower bound for the first row should be -infty."); |
292 | 292 |
check(lp.rowUpperBound(upright)==1, |
293 | 293 |
"The upper bound for the first row should be 1."); |
294 | 294 |
LpSolver::Expr e = lp.row(upright); |
295 | 295 |
check(e[x1] == 1, "The first coefficient should 1."); |
296 | 296 |
check(e[x2] == 2, "The second coefficient should 1."); |
297 | 297 |
|
298 | 298 |
lp.row(upright, x1+x2 <=1); |
299 | 299 |
e = lp.row(upright); |
300 | 300 |
check(e[x1] == 1, "The first coefficient should 1."); |
301 | 301 |
check(e[x2] == 1, "The second coefficient should 1."); |
302 | 302 |
|
303 | 303 |
LpSolver::DualExpr de = lp.col(x1); |
304 | 304 |
check( de[upright] == 1, "The first coefficient should 1."); |
305 | 305 |
|
306 | 306 |
LpSolver* clp = lp.cloneSolver(); |
307 | 307 |
|
308 | 308 |
//Testing the problem retrieving routines |
309 | 309 |
check(clp->objCoeff(x1)==1,"First term should be 1 in the obj function!"); |
310 | 310 |
check(clp->sense() == clp->MAX,"This is a maximization!"); |
311 | 311 |
check(clp->coeff(upright,x1)==1,"The coefficient in question is 1!"); |
312 | 312 |
// std::cout<<lp.colLowerBound(x1)<<std::endl; |
313 | 313 |
check(clp->colLowerBound(x1)==0, |
314 | 314 |
"The lower bound for variable x1 should be 0."); |
315 | 315 |
check(clp->colUpperBound(x1)==LpSolver::INF, |
316 | 316 |
"The upper bound for variable x1 should be infty."); |
317 | 317 |
|
318 | 318 |
check(lp.rowLowerBound(upright)==-LpSolver::INF, |
319 | 319 |
"The lower bound for the first row should be -infty."); |
320 | 320 |
check(lp.rowUpperBound(upright)==1, |
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(); |
|
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 |
394 | 393 |
|
395 | 394 |
#ifdef HAVE_CLP |
396 | 395 |
{ |
397 | 396 |
ClpLp lp_clp1,lp_clp2; |
398 | 397 |
lpTest(lp_clp1); |
399 | 398 |
aTest(lp_clp2); |
400 | 399 |
} |
401 | 400 |
#endif |
402 | 401 |
|
403 | 402 |
return 0; |
404 | 403 |
} |
... | ... |
@@ -22,116 +22,115 @@ |
22 | 22 |
#ifdef HAVE_CONFIG_H |
23 | 23 |
#include <lemon/config.h> |
24 | 24 |
#endif |
25 | 25 |
|
26 | 26 |
#ifdef HAVE_CPLEX |
27 | 27 |
#include <lemon/cplex.h> |
28 | 28 |
#endif |
29 | 29 |
|
30 | 30 |
#ifdef HAVE_GLPK |
31 | 31 |
#include <lemon/glpk.h> |
32 | 32 |
#endif |
33 | 33 |
|
34 | 34 |
|
35 | 35 |
using namespace lemon; |
36 | 36 |
|
37 | 37 |
void solveAndCheck(MipSolver& mip, MipSolver::ProblemType stat, |
38 | 38 |
double exp_opt) { |
39 | 39 |
using std::string; |
40 | 40 |
|
41 | 41 |
mip.solve(); |
42 | 42 |
//int decimal,sign; |
43 | 43 |
std::ostringstream buf; |
44 | 44 |
buf << "Type should be: " << int(stat)<<" and it is "<<int(mip.type()); |
45 | 45 |
|
46 | 46 |
|
47 | 47 |
// itoa(stat,buf1, 10); |
48 | 48 |
check(mip.type()==stat, buf.str()); |
49 | 49 |
|
50 | 50 |
if (stat == MipSolver::OPTIMAL) { |
51 | 51 |
std::ostringstream sbuf; |
52 | 52 |
buf << "Wrong optimal value: the right optimum is " << exp_opt; |
53 | 53 |
check(std::abs(mip.solValue()-exp_opt) < 1e-3, sbuf.str()); |
54 | 54 |
//+ecvt(exp_opt,2) |
55 | 55 |
} |
56 | 56 |
} |
57 | 57 |
|
58 | 58 |
void aTest(MipSolver& mip) |
59 | 59 |
{ |
60 | 60 |
//The following example is very simple |
61 | 61 |
|
62 | 62 |
|
63 | 63 |
typedef MipSolver::Row Row; |
64 | 64 |
typedef MipSolver::Col Col; |
65 | 65 |
|
66 | 66 |
|
67 | 67 |
|
68 | 68 |
Col x1 = mip.addCol(); |
69 | 69 |
Col x2 = mip.addCol(); |
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(); |
|
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)