... | ... |
@@ -493,236 +493,238 @@ |
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) { |
... | ... |
@@ -829,178 +831,179 @@ |
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)