... | ... |
@@ -397,610 +397,613 @@ |
397 | 397 |
} |
398 | 398 |
|
399 | 399 |
} |
400 | 400 |
|
401 | 401 |
GlpkBase::Value GlpkBase::_getRowLowerBound(int i) const { |
402 | 402 |
int b = glp_get_row_type(lp, i); |
403 | 403 |
switch (b) { |
404 | 404 |
case GLP_LO: |
405 | 405 |
case GLP_DB: |
406 | 406 |
case GLP_FX: |
407 | 407 |
return glp_get_row_lb(lp, i); |
408 | 408 |
default: |
409 | 409 |
return -INF; |
410 | 410 |
} |
411 | 411 |
} |
412 | 412 |
|
413 | 413 |
void GlpkBase::_setRowUpperBound(int i, Value up) { |
414 | 414 |
LEMON_ASSERT(up != -INF, "Invalid bound"); |
415 | 415 |
|
416 | 416 |
int b = glp_get_row_type(lp, i); |
417 | 417 |
double lo = glp_get_row_lb(lp, i); |
418 | 418 |
if (up == INF) { |
419 | 419 |
switch (b) { |
420 | 420 |
case GLP_FR: |
421 | 421 |
case GLP_LO: |
422 | 422 |
break; |
423 | 423 |
case GLP_UP: |
424 | 424 |
glp_set_row_bnds(lp, i, GLP_FR, lo, up); |
425 | 425 |
break; |
426 | 426 |
case GLP_DB: |
427 | 427 |
case GLP_FX: |
428 | 428 |
glp_set_row_bnds(lp, i, GLP_LO, lo, up); |
429 | 429 |
break; |
430 | 430 |
default: |
431 | 431 |
break; |
432 | 432 |
} |
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 | 529 |
GlpkBase::FreeEnvHelper GlpkBase::freeEnvHelper; |
530 | 530 |
|
531 | 531 |
// GlpkLp members |
532 | 532 |
|
533 | 533 |
GlpkLp::GlpkLp() |
534 | 534 |
: LpBase(), LpSolver(), GlpkBase() { |
535 | 535 |
messageLevel(MESSAGE_NO_OUTPUT); |
536 | 536 |
presolver(false); |
537 | 537 |
} |
538 | 538 |
|
539 | 539 |
GlpkLp::GlpkLp(const GlpkLp& other) |
540 | 540 |
: LpBase(other), LpSolver(other), GlpkBase(other) { |
541 | 541 |
messageLevel(MESSAGE_NO_OUTPUT); |
542 | 542 |
presolver(false); |
543 | 543 |
} |
544 | 544 |
|
545 | 545 |
GlpkLp* GlpkLp::newSolver() const { return new GlpkLp; } |
546 | 546 |
GlpkLp* GlpkLp::cloneSolver() const { return new GlpkLp(*this); } |
547 | 547 |
|
548 | 548 |
const char* GlpkLp::_solverName() const { return "GlpkLp"; } |
549 | 549 |
|
550 | 550 |
void GlpkLp::_clear_temporals() { |
551 | 551 |
_primal_ray.clear(); |
552 | 552 |
_dual_ray.clear(); |
553 | 553 |
} |
554 | 554 |
|
555 | 555 |
GlpkLp::SolveExitStatus GlpkLp::_solve() { |
556 | 556 |
return solvePrimal(); |
557 | 557 |
} |
558 | 558 |
|
559 | 559 |
GlpkLp::SolveExitStatus GlpkLp::solvePrimal() { |
560 | 560 |
_clear_temporals(); |
561 | 561 |
|
562 | 562 |
glp_smcp smcp; |
563 | 563 |
glp_init_smcp(&smcp); |
564 | 564 |
|
565 | 565 |
switch (_message_level) { |
566 | 566 |
case MESSAGE_NO_OUTPUT: |
567 | 567 |
smcp.msg_lev = GLP_MSG_OFF; |
568 | 568 |
break; |
569 | 569 |
case MESSAGE_ERROR_MESSAGE: |
570 | 570 |
smcp.msg_lev = GLP_MSG_ERR; |
571 | 571 |
break; |
572 | 572 |
case MESSAGE_NORMAL_OUTPUT: |
573 | 573 |
smcp.msg_lev = GLP_MSG_ON; |
574 | 574 |
break; |
575 | 575 |
case MESSAGE_FULL_OUTPUT: |
576 | 576 |
smcp.msg_lev = GLP_MSG_ALL; |
577 | 577 |
break; |
578 | 578 |
} |
579 | 579 |
smcp.presolve = _presolve; |
580 | 580 |
|
581 | 581 |
// If the basis is not valid we get an error return value. |
582 | 582 |
// In this case we can try to create a new basis. |
583 | 583 |
switch (glp_simplex(lp, &smcp)) { |
584 | 584 |
case 0: |
585 | 585 |
break; |
586 | 586 |
case GLP_EBADB: |
587 | 587 |
case GLP_ESING: |
588 | 588 |
case GLP_ECOND: |
589 |
|
|
589 |
glp_term_out(false); |
|
590 | 590 |
glp_adv_basis(lp, 0); |
591 |
glp_term_out(true); |
|
591 | 592 |
if (glp_simplex(lp, &smcp) != 0) return UNSOLVED; |
592 | 593 |
break; |
593 | 594 |
default: |
594 | 595 |
return UNSOLVED; |
595 | 596 |
} |
596 | 597 |
|
597 | 598 |
return SOLVED; |
598 | 599 |
} |
599 | 600 |
|
600 | 601 |
GlpkLp::SolveExitStatus GlpkLp::solveDual() { |
601 | 602 |
_clear_temporals(); |
602 | 603 |
|
603 | 604 |
glp_smcp smcp; |
604 | 605 |
glp_init_smcp(&smcp); |
605 | 606 |
|
606 | 607 |
switch (_message_level) { |
607 | 608 |
case MESSAGE_NO_OUTPUT: |
608 | 609 |
smcp.msg_lev = GLP_MSG_OFF; |
609 | 610 |
break; |
610 | 611 |
case MESSAGE_ERROR_MESSAGE: |
611 | 612 |
smcp.msg_lev = GLP_MSG_ERR; |
612 | 613 |
break; |
613 | 614 |
case MESSAGE_NORMAL_OUTPUT: |
614 | 615 |
smcp.msg_lev = GLP_MSG_ON; |
615 | 616 |
break; |
616 | 617 |
case MESSAGE_FULL_OUTPUT: |
617 | 618 |
smcp.msg_lev = GLP_MSG_ALL; |
618 | 619 |
break; |
619 | 620 |
} |
620 | 621 |
smcp.meth = GLP_DUAL; |
621 | 622 |
smcp.presolve = _presolve; |
622 | 623 |
|
623 | 624 |
// If the basis is not valid we get an error return value. |
624 | 625 |
// In this case we can try to create a new basis. |
625 | 626 |
switch (glp_simplex(lp, &smcp)) { |
626 | 627 |
case 0: |
627 | 628 |
break; |
628 | 629 |
case GLP_EBADB: |
629 | 630 |
case GLP_ESING: |
630 | 631 |
case GLP_ECOND: |
631 |
|
|
632 |
glp_term_out(false); |
|
632 | 633 |
glp_adv_basis(lp, 0); |
634 |
glp_term_out(true); |
|
633 | 635 |
if (glp_simplex(lp, &smcp) != 0) return UNSOLVED; |
634 | 636 |
break; |
635 | 637 |
default: |
636 | 638 |
return UNSOLVED; |
637 | 639 |
} |
638 | 640 |
return SOLVED; |
639 | 641 |
} |
640 | 642 |
|
641 | 643 |
GlpkLp::Value GlpkLp::_getPrimal(int i) const { |
642 | 644 |
return glp_get_col_prim(lp, i); |
643 | 645 |
} |
644 | 646 |
|
645 | 647 |
GlpkLp::Value GlpkLp::_getDual(int i) const { |
646 | 648 |
return glp_get_row_dual(lp, i); |
647 | 649 |
} |
648 | 650 |
|
649 | 651 |
GlpkLp::Value GlpkLp::_getPrimalValue() const { |
650 | 652 |
return glp_get_obj_val(lp); |
651 | 653 |
} |
652 | 654 |
|
653 | 655 |
GlpkLp::VarStatus GlpkLp::_getColStatus(int i) const { |
654 | 656 |
switch (glp_get_col_stat(lp, i)) { |
655 | 657 |
case GLP_BS: |
656 | 658 |
return BASIC; |
657 | 659 |
case GLP_UP: |
658 | 660 |
return UPPER; |
659 | 661 |
case GLP_LO: |
660 | 662 |
return LOWER; |
661 | 663 |
case GLP_NF: |
662 | 664 |
return FREE; |
663 | 665 |
case GLP_NS: |
664 | 666 |
return FIXED; |
665 | 667 |
default: |
666 | 668 |
LEMON_ASSERT(false, "Wrong column status"); |
667 | 669 |
return GlpkLp::VarStatus(); |
668 | 670 |
} |
669 | 671 |
} |
670 | 672 |
|
671 | 673 |
GlpkLp::VarStatus GlpkLp::_getRowStatus(int i) const { |
672 | 674 |
switch (glp_get_row_stat(lp, i)) { |
673 | 675 |
case GLP_BS: |
674 | 676 |
return BASIC; |
675 | 677 |
case GLP_UP: |
676 | 678 |
return UPPER; |
677 | 679 |
case GLP_LO: |
678 | 680 |
return LOWER; |
679 | 681 |
case GLP_NF: |
680 | 682 |
return FREE; |
681 | 683 |
case GLP_NS: |
682 | 684 |
return FIXED; |
683 | 685 |
default: |
684 | 686 |
LEMON_ASSERT(false, "Wrong row status"); |
685 | 687 |
return GlpkLp::VarStatus(); |
686 | 688 |
} |
687 | 689 |
} |
688 | 690 |
|
689 | 691 |
GlpkLp::Value GlpkLp::_getPrimalRay(int i) const { |
690 | 692 |
if (_primal_ray.empty()) { |
691 | 693 |
int row_num = glp_get_num_rows(lp); |
692 | 694 |
int col_num = glp_get_num_cols(lp); |
693 | 695 |
|
694 | 696 |
_primal_ray.resize(col_num + 1, 0.0); |
695 | 697 |
|
696 | 698 |
int index = glp_get_unbnd_ray(lp); |
697 | 699 |
if (index != 0) { |
698 | 700 |
// The primal ray is found in primal simplex second phase |
699 | 701 |
LEMON_ASSERT((index <= row_num ? glp_get_row_stat(lp, index) : |
700 | 702 |
glp_get_col_stat(lp, index - row_num)) != GLP_BS, |
701 | 703 |
"Wrong primal ray"); |
702 | 704 |
|
703 | 705 |
bool negate = glp_get_obj_dir(lp) == GLP_MAX; |
704 | 706 |
|
705 | 707 |
if (index > row_num) { |
706 | 708 |
_primal_ray[index - row_num] = 1.0; |
707 | 709 |
if (glp_get_col_dual(lp, index - row_num) > 0) { |
708 | 710 |
negate = !negate; |
709 | 711 |
} |
710 | 712 |
} else { |
711 | 713 |
if (glp_get_row_dual(lp, index) > 0) { |
712 | 714 |
negate = !negate; |
713 | 715 |
} |
714 | 716 |
} |
715 | 717 |
|
716 | 718 |
std::vector<int> ray_indexes(row_num + 1); |
717 | 719 |
std::vector<Value> ray_values(row_num + 1); |
718 | 720 |
int ray_length = glp_eval_tab_col(lp, index, &ray_indexes.front(), |
719 | 721 |
&ray_values.front()); |
720 | 722 |
|
721 | 723 |
for (int i = 1; i <= ray_length; ++i) { |
722 | 724 |
if (ray_indexes[i] > row_num) { |
723 | 725 |
_primal_ray[ray_indexes[i] - row_num] = ray_values[i]; |
724 | 726 |
} |
725 | 727 |
} |
726 | 728 |
|
727 | 729 |
if (negate) { |
728 | 730 |
for (int i = 1; i <= col_num; ++i) { |
729 | 731 |
_primal_ray[i] = - _primal_ray[i]; |
730 | 732 |
} |
731 | 733 |
} |
732 | 734 |
} else { |
733 | 735 |
for (int i = 1; i <= col_num; ++i) { |
734 | 736 |
_primal_ray[i] = glp_get_col_prim(lp, i); |
735 | 737 |
} |
736 | 738 |
} |
737 | 739 |
} |
738 | 740 |
return _primal_ray[i]; |
739 | 741 |
} |
740 | 742 |
|
741 | 743 |
GlpkLp::Value GlpkLp::_getDualRay(int i) const { |
742 | 744 |
if (_dual_ray.empty()) { |
743 | 745 |
int row_num = glp_get_num_rows(lp); |
744 | 746 |
|
745 | 747 |
_dual_ray.resize(row_num + 1, 0.0); |
746 | 748 |
|
747 | 749 |
int index = glp_get_unbnd_ray(lp); |
748 | 750 |
if (index != 0) { |
749 | 751 |
// The dual ray is found in dual simplex second phase |
750 | 752 |
LEMON_ASSERT((index <= row_num ? glp_get_row_stat(lp, index) : |
751 | 753 |
glp_get_col_stat(lp, index - row_num)) == GLP_BS, |
752 | 754 |
|
753 | 755 |
"Wrong dual ray"); |
754 | 756 |
|
755 | 757 |
int idx; |
756 | 758 |
bool negate = false; |
757 | 759 |
|
758 | 760 |
if (index > row_num) { |
759 | 761 |
idx = glp_get_col_bind(lp, index - row_num); |
760 | 762 |
if (glp_get_col_prim(lp, index - row_num) > |
761 | 763 |
glp_get_col_ub(lp, index - row_num)) { |
762 | 764 |
negate = true; |
763 | 765 |
} |
764 | 766 |
} else { |
765 | 767 |
idx = glp_get_row_bind(lp, index); |
766 | 768 |
if (glp_get_row_prim(lp, index) > glp_get_row_ub(lp, index)) { |
767 | 769 |
negate = true; |
768 | 770 |
} |
769 | 771 |
} |
770 | 772 |
|
771 | 773 |
_dual_ray[idx] = negate ? - 1.0 : 1.0; |
772 | 774 |
|
773 | 775 |
glp_btran(lp, &_dual_ray.front()); |
774 | 776 |
} else { |
775 | 777 |
double eps = 1e-7; |
776 | 778 |
// The dual ray is found in primal simplex first phase |
777 | 779 |
// We assume that the glpk minimizes the slack to get feasible solution |
778 | 780 |
for (int i = 1; i <= row_num; ++i) { |
779 | 781 |
int index = glp_get_bhead(lp, i); |
780 | 782 |
if (index <= row_num) { |
781 | 783 |
double res = glp_get_row_prim(lp, index); |
782 | 784 |
if (res > glp_get_row_ub(lp, index) + eps) { |
783 | 785 |
_dual_ray[i] = -1; |
784 | 786 |
} else if (res < glp_get_row_lb(lp, index) - eps) { |
785 | 787 |
_dual_ray[i] = 1; |
786 | 788 |
} else { |
787 | 789 |
_dual_ray[i] = 0; |
788 | 790 |
} |
789 | 791 |
_dual_ray[i] *= glp_get_rii(lp, index); |
790 | 792 |
} else { |
791 | 793 |
double res = glp_get_col_prim(lp, index - row_num); |
792 | 794 |
if (res > glp_get_col_ub(lp, index - row_num) + eps) { |
793 | 795 |
_dual_ray[i] = -1; |
794 | 796 |
} else if (res < glp_get_col_lb(lp, index - row_num) - eps) { |
795 | 797 |
_dual_ray[i] = 1; |
796 | 798 |
} else { |
797 | 799 |
_dual_ray[i] = 0; |
798 | 800 |
} |
799 | 801 |
_dual_ray[i] /= glp_get_sjj(lp, index - row_num); |
800 | 802 |
} |
801 | 803 |
} |
802 | 804 |
|
803 | 805 |
glp_btran(lp, &_dual_ray.front()); |
804 | 806 |
|
805 | 807 |
for (int i = 1; i <= row_num; ++i) { |
806 | 808 |
_dual_ray[i] /= glp_get_rii(lp, i); |
807 | 809 |
} |
808 | 810 |
} |
809 | 811 |
} |
810 | 812 |
return _dual_ray[i]; |
811 | 813 |
} |
812 | 814 |
|
813 | 815 |
GlpkLp::ProblemType GlpkLp::_getPrimalType() const { |
814 | 816 |
if (glp_get_status(lp) == GLP_OPT) |
815 | 817 |
return OPTIMAL; |
816 | 818 |
switch (glp_get_prim_stat(lp)) { |
817 | 819 |
case GLP_UNDEF: |
818 | 820 |
return UNDEFINED; |
819 | 821 |
case GLP_FEAS: |
820 | 822 |
case GLP_INFEAS: |
821 | 823 |
if (glp_get_dual_stat(lp) == GLP_NOFEAS) { |
822 | 824 |
return UNBOUNDED; |
823 | 825 |
} else { |
824 | 826 |
return UNDEFINED; |
825 | 827 |
} |
826 | 828 |
case GLP_NOFEAS: |
827 | 829 |
return INFEASIBLE; |
828 | 830 |
default: |
829 | 831 |
LEMON_ASSERT(false, "Wrong primal type"); |
830 | 832 |
return GlpkLp::ProblemType(); |
831 | 833 |
} |
832 | 834 |
} |
833 | 835 |
|
834 | 836 |
GlpkLp::ProblemType GlpkLp::_getDualType() const { |
835 | 837 |
if (glp_get_status(lp) == GLP_OPT) |
836 | 838 |
return OPTIMAL; |
837 | 839 |
switch (glp_get_dual_stat(lp)) { |
838 | 840 |
case GLP_UNDEF: |
839 | 841 |
return UNDEFINED; |
840 | 842 |
case GLP_FEAS: |
841 | 843 |
case GLP_INFEAS: |
842 | 844 |
if (glp_get_prim_stat(lp) == GLP_NOFEAS) { |
843 | 845 |
return UNBOUNDED; |
844 | 846 |
} else { |
845 | 847 |
return UNDEFINED; |
846 | 848 |
} |
847 | 849 |
case GLP_NOFEAS: |
848 | 850 |
return INFEASIBLE; |
849 | 851 |
default: |
850 | 852 |
LEMON_ASSERT(false, "Wrong primal type"); |
851 | 853 |
return GlpkLp::ProblemType(); |
852 | 854 |
} |
853 | 855 |
} |
854 | 856 |
|
855 | 857 |
void GlpkLp::presolver(bool presolve) { |
856 | 858 |
_presolve = presolve; |
857 | 859 |
} |
858 | 860 |
|
859 | 861 |
void GlpkLp::messageLevel(MessageLevel m) { |
860 | 862 |
_message_level = m; |
861 | 863 |
} |
862 | 864 |
|
863 | 865 |
// GlpkMip members |
864 | 866 |
|
865 | 867 |
GlpkMip::GlpkMip() |
866 | 868 |
: LpBase(), MipSolver(), GlpkBase() { |
867 | 869 |
messageLevel(MESSAGE_NO_OUTPUT); |
868 | 870 |
} |
869 | 871 |
|
870 | 872 |
GlpkMip::GlpkMip(const GlpkMip& other) |
871 | 873 |
: LpBase(), MipSolver(), GlpkBase(other) { |
872 | 874 |
messageLevel(MESSAGE_NO_OUTPUT); |
873 | 875 |
} |
874 | 876 |
|
875 | 877 |
void GlpkMip::_setColType(int i, GlpkMip::ColTypes col_type) { |
876 | 878 |
switch (col_type) { |
877 | 879 |
case INTEGER: |
878 | 880 |
glp_set_col_kind(lp, i, GLP_IV); |
879 | 881 |
break; |
880 | 882 |
case REAL: |
881 | 883 |
glp_set_col_kind(lp, i, GLP_CV); |
882 | 884 |
break; |
883 | 885 |
} |
884 | 886 |
} |
885 | 887 |
|
886 | 888 |
GlpkMip::ColTypes GlpkMip::_getColType(int i) const { |
887 | 889 |
switch (glp_get_col_kind(lp, i)) { |
888 | 890 |
case GLP_IV: |
889 | 891 |
case GLP_BV: |
890 | 892 |
return INTEGER; |
891 | 893 |
default: |
892 | 894 |
return REAL; |
893 | 895 |
} |
894 | 896 |
|
895 | 897 |
} |
896 | 898 |
|
897 | 899 |
GlpkMip::SolveExitStatus GlpkMip::_solve() { |
898 | 900 |
glp_smcp smcp; |
899 | 901 |
glp_init_smcp(&smcp); |
900 | 902 |
|
901 | 903 |
switch (_message_level) { |
902 | 904 |
case MESSAGE_NO_OUTPUT: |
903 | 905 |
smcp.msg_lev = GLP_MSG_OFF; |
904 | 906 |
break; |
905 | 907 |
case MESSAGE_ERROR_MESSAGE: |
906 | 908 |
smcp.msg_lev = GLP_MSG_ERR; |
907 | 909 |
break; |
908 | 910 |
case MESSAGE_NORMAL_OUTPUT: |
909 | 911 |
smcp.msg_lev = GLP_MSG_ON; |
910 | 912 |
break; |
911 | 913 |
case MESSAGE_FULL_OUTPUT: |
912 | 914 |
smcp.msg_lev = GLP_MSG_ALL; |
913 | 915 |
break; |
914 | 916 |
} |
915 | 917 |
smcp.meth = GLP_DUAL; |
916 | 918 |
|
917 | 919 |
// If the basis is not valid we get an error return value. |
918 | 920 |
// In this case we can try to create a new basis. |
919 | 921 |
switch (glp_simplex(lp, &smcp)) { |
920 | 922 |
case 0: |
921 | 923 |
break; |
922 | 924 |
case GLP_EBADB: |
923 | 925 |
case GLP_ESING: |
924 | 926 |
case GLP_ECOND: |
925 |
|
|
927 |
glp_term_out(false); |
|
926 | 928 |
glp_adv_basis(lp, 0); |
929 |
glp_term_out(true); |
|
927 | 930 |
if (glp_simplex(lp, &smcp) != 0) return UNSOLVED; |
928 | 931 |
break; |
929 | 932 |
default: |
930 | 933 |
return UNSOLVED; |
931 | 934 |
} |
932 | 935 |
|
933 | 936 |
if (glp_get_status(lp) != GLP_OPT) return SOLVED; |
934 | 937 |
|
935 | 938 |
glp_iocp iocp; |
936 | 939 |
glp_init_iocp(&iocp); |
937 | 940 |
|
938 | 941 |
switch (_message_level) { |
939 | 942 |
case MESSAGE_NO_OUTPUT: |
940 | 943 |
iocp.msg_lev = GLP_MSG_OFF; |
941 | 944 |
break; |
942 | 945 |
case MESSAGE_ERROR_MESSAGE: |
943 | 946 |
iocp.msg_lev = GLP_MSG_ERR; |
944 | 947 |
break; |
945 | 948 |
case MESSAGE_NORMAL_OUTPUT: |
946 | 949 |
iocp.msg_lev = GLP_MSG_ON; |
947 | 950 |
break; |
948 | 951 |
case MESSAGE_FULL_OUTPUT: |
949 | 952 |
iocp.msg_lev = GLP_MSG_ALL; |
950 | 953 |
break; |
951 | 954 |
} |
952 | 955 |
|
953 | 956 |
if (glp_intopt(lp, &iocp) != 0) return UNSOLVED; |
954 | 957 |
return SOLVED; |
955 | 958 |
} |
956 | 959 |
|
957 | 960 |
|
958 | 961 |
GlpkMip::ProblemType GlpkMip::_getType() const { |
959 | 962 |
switch (glp_get_status(lp)) { |
960 | 963 |
case GLP_OPT: |
961 | 964 |
switch (glp_mip_status(lp)) { |
962 | 965 |
case GLP_UNDEF: |
963 | 966 |
return UNDEFINED; |
964 | 967 |
case GLP_NOFEAS: |
965 | 968 |
return INFEASIBLE; |
966 | 969 |
case GLP_FEAS: |
967 | 970 |
return FEASIBLE; |
968 | 971 |
case GLP_OPT: |
969 | 972 |
return OPTIMAL; |
970 | 973 |
default: |
971 | 974 |
LEMON_ASSERT(false, "Wrong problem type."); |
972 | 975 |
return GlpkMip::ProblemType(); |
973 | 976 |
} |
974 | 977 |
case GLP_NOFEAS: |
975 | 978 |
return INFEASIBLE; |
976 | 979 |
case GLP_INFEAS: |
977 | 980 |
case GLP_FEAS: |
978 | 981 |
if (glp_get_dual_stat(lp) == GLP_NOFEAS) { |
979 | 982 |
return UNBOUNDED; |
980 | 983 |
} else { |
981 | 984 |
return UNDEFINED; |
982 | 985 |
} |
983 | 986 |
default: |
984 | 987 |
LEMON_ASSERT(false, "Wrong problem type."); |
985 | 988 |
return GlpkMip::ProblemType(); |
986 | 989 |
} |
987 | 990 |
} |
988 | 991 |
|
989 | 992 |
GlpkMip::Value GlpkMip::_getSol(int i) const { |
990 | 993 |
return glp_mip_col_val(lp, i); |
991 | 994 |
} |
992 | 995 |
|
993 | 996 |
GlpkMip::Value GlpkMip::_getSolValue() const { |
994 | 997 |
return glp_mip_obj_val(lp); |
995 | 998 |
} |
996 | 999 |
|
997 | 1000 |
GlpkMip* GlpkMip::newSolver() const { return new GlpkMip; } |
998 | 1001 |
GlpkMip* GlpkMip::cloneSolver() const {return new GlpkMip(*this); } |
999 | 1002 |
|
1000 | 1003 |
const char* GlpkMip::_solverName() const { return "GlpkMip"; } |
1001 | 1004 |
|
1002 | 1005 |
void GlpkMip::messageLevel(MessageLevel m) { |
1003 | 1006 |
_message_level = m; |
1004 | 1007 |
} |
1005 | 1008 |
|
1006 | 1009 |
} //END OF NAMESPACE LEMON |
0 comments (0 inline)