lemon/glpk.cc
changeset 536 97674155c135
parent 461 08d495d48089
child 537 0fec6a017ead
child 540 9db62975c32b
equal deleted inserted replaced
0:d72eceffcb14 1:e3b8ca1cea32
   520     glp_erase_prob(lp);
   520     glp_erase_prob(lp);
   521     rows.clear();
   521     rows.clear();
   522     cols.clear();
   522     cols.clear();
   523   }
   523   }
   524 
   524 
   525   // LpGlpk members
   525   // GlpkLp members
   526 
   526 
   527   LpGlpk::LpGlpk()
   527   GlpkLp::GlpkLp()
   528     : LpBase(), GlpkBase(), LpSolver() {
   528     : LpBase(), GlpkBase(), LpSolver() {
   529     messageLevel(MESSAGE_NO_OUTPUT);
   529     messageLevel(MESSAGE_NO_OUTPUT);
   530   }
   530   }
   531 
   531 
   532   LpGlpk::LpGlpk(const LpGlpk& other)
   532   GlpkLp::GlpkLp(const GlpkLp& other)
   533     : LpBase(other), GlpkBase(other), LpSolver(other) {
   533     : LpBase(other), GlpkBase(other), LpSolver(other) {
   534     messageLevel(MESSAGE_NO_OUTPUT);
   534     messageLevel(MESSAGE_NO_OUTPUT);
   535   }
   535   }
   536 
   536 
   537   LpGlpk* LpGlpk::_newSolver() const { return new LpGlpk; }
   537   GlpkLp* GlpkLp::_newSolver() const { return new GlpkLp; }
   538   LpGlpk* LpGlpk::_cloneSolver() const { return new LpGlpk(*this); }
   538   GlpkLp* GlpkLp::_cloneSolver() const { return new GlpkLp(*this); }
   539 
   539 
   540   const char* LpGlpk::_solverName() const { return "LpGlpk"; }
   540   const char* GlpkLp::_solverName() const { return "GlpkLp"; }
   541 
   541 
   542   void LpGlpk::_clear_temporals() {
   542   void GlpkLp::_clear_temporals() {
   543     _primal_ray.clear();
   543     _primal_ray.clear();
   544     _dual_ray.clear();
   544     _dual_ray.clear();
   545   }
   545   }
   546 
   546 
   547   LpGlpk::SolveExitStatus LpGlpk::_solve() {
   547   GlpkLp::SolveExitStatus GlpkLp::_solve() {
   548     return solvePrimal();
   548     return solvePrimal();
   549   }
   549   }
   550 
   550 
   551   LpGlpk::SolveExitStatus LpGlpk::solvePrimal() {
   551   GlpkLp::SolveExitStatus GlpkLp::solvePrimal() {
   552     _clear_temporals();
   552     _clear_temporals();
   553 
   553 
   554     glp_smcp smcp;
   554     glp_smcp smcp;
   555     glp_init_smcp(&smcp);
   555     glp_init_smcp(&smcp);
   556 
   556 
   571 
   571 
   572     if (glp_simplex(lp, &smcp) != 0) return UNSOLVED;
   572     if (glp_simplex(lp, &smcp) != 0) return UNSOLVED;
   573     return SOLVED;
   573     return SOLVED;
   574   }
   574   }
   575 
   575 
   576   LpGlpk::SolveExitStatus LpGlpk::solveDual() {
   576   GlpkLp::SolveExitStatus GlpkLp::solveDual() {
   577     _clear_temporals();
   577     _clear_temporals();
   578 
   578 
   579     glp_smcp smcp;
   579     glp_smcp smcp;
   580     glp_init_smcp(&smcp);
   580     glp_init_smcp(&smcp);
   581 
   581 
   597 
   597 
   598     if (glp_simplex(lp, &smcp) != 0) return UNSOLVED;
   598     if (glp_simplex(lp, &smcp) != 0) return UNSOLVED;
   599     return SOLVED;
   599     return SOLVED;
   600   }
   600   }
   601 
   601 
   602   LpGlpk::Value LpGlpk::_getPrimal(int i) const {
   602   GlpkLp::Value GlpkLp::_getPrimal(int i) const {
   603     return glp_get_col_prim(lp, i);
   603     return glp_get_col_prim(lp, i);
   604   }
   604   }
   605 
   605 
   606   LpGlpk::Value LpGlpk::_getDual(int i) const {
   606   GlpkLp::Value GlpkLp::_getDual(int i) const {
   607     return glp_get_row_dual(lp, i);
   607     return glp_get_row_dual(lp, i);
   608   }
   608   }
   609 
   609 
   610   LpGlpk::Value LpGlpk::_getPrimalValue() const {
   610   GlpkLp::Value GlpkLp::_getPrimalValue() const {
   611     return glp_get_obj_val(lp);
   611     return glp_get_obj_val(lp);
   612   }
   612   }
   613 
   613 
   614   LpGlpk::VarStatus LpGlpk::_getColStatus(int i) const {
   614   GlpkLp::VarStatus GlpkLp::_getColStatus(int i) const {
   615     switch (glp_get_col_stat(lp, i)) {
   615     switch (glp_get_col_stat(lp, i)) {
   616     case GLP_BS:
   616     case GLP_BS:
   617       return BASIC;
   617       return BASIC;
   618     case GLP_UP:
   618     case GLP_UP:
   619       return UPPER;
   619       return UPPER;
   623       return FREE;
   623       return FREE;
   624     case GLP_NS:
   624     case GLP_NS:
   625       return FIXED;
   625       return FIXED;
   626     default:
   626     default:
   627       LEMON_ASSERT(false, "Wrong column status");
   627       LEMON_ASSERT(false, "Wrong column status");
   628       return LpGlpk::VarStatus();
   628       return GlpkLp::VarStatus();
   629     }
   629     }
   630   }
   630   }
   631 
   631 
   632   LpGlpk::VarStatus LpGlpk::_getRowStatus(int i) const {
   632   GlpkLp::VarStatus GlpkLp::_getRowStatus(int i) const {
   633     switch (glp_get_row_stat(lp, i)) {
   633     switch (glp_get_row_stat(lp, i)) {
   634     case GLP_BS:
   634     case GLP_BS:
   635       return BASIC;
   635       return BASIC;
   636     case GLP_UP:
   636     case GLP_UP:
   637       return UPPER;
   637       return UPPER;
   641       return FREE;
   641       return FREE;
   642     case GLP_NS:
   642     case GLP_NS:
   643       return FIXED;
   643       return FIXED;
   644     default:
   644     default:
   645       LEMON_ASSERT(false, "Wrong row status");
   645       LEMON_ASSERT(false, "Wrong row status");
   646       return LpGlpk::VarStatus();
   646       return GlpkLp::VarStatus();
   647     }
   647     }
   648   }
   648   }
   649 
   649 
   650   LpGlpk::Value LpGlpk::_getPrimalRay(int i) const {
   650   GlpkLp::Value GlpkLp::_getPrimalRay(int i) const {
   651     if (_primal_ray.empty()) {
   651     if (_primal_ray.empty()) {
   652       int row_num = glp_get_num_rows(lp);
   652       int row_num = glp_get_num_rows(lp);
   653       int col_num = glp_get_num_cols(lp);
   653       int col_num = glp_get_num_cols(lp);
   654 
   654 
   655       _primal_ray.resize(col_num + 1, 0.0);
   655       _primal_ray.resize(col_num + 1, 0.0);
   697       }
   697       }
   698     }
   698     }
   699     return _primal_ray[i];
   699     return _primal_ray[i];
   700   }
   700   }
   701 
   701 
   702   LpGlpk::Value LpGlpk::_getDualRay(int i) const {
   702   GlpkLp::Value GlpkLp::_getDualRay(int i) const {
   703     if (_dual_ray.empty()) {
   703     if (_dual_ray.empty()) {
   704       int row_num = glp_get_num_rows(lp);
   704       int row_num = glp_get_num_rows(lp);
   705 
   705 
   706       _dual_ray.resize(row_num + 1, 0.0);
   706       _dual_ray.resize(row_num + 1, 0.0);
   707 
   707 
   769       }
   769       }
   770     }
   770     }
   771     return _dual_ray[i];
   771     return _dual_ray[i];
   772   }
   772   }
   773 
   773 
   774   LpGlpk::ProblemType LpGlpk::_getPrimalType() const {
   774   GlpkLp::ProblemType GlpkLp::_getPrimalType() const {
   775     if (glp_get_status(lp) == GLP_OPT)
   775     if (glp_get_status(lp) == GLP_OPT)
   776       return OPTIMAL;
   776       return OPTIMAL;
   777     switch (glp_get_prim_stat(lp)) {
   777     switch (glp_get_prim_stat(lp)) {
   778     case GLP_UNDEF:
   778     case GLP_UNDEF:
   779       return UNDEFINED;
   779       return UNDEFINED;
   786       }
   786       }
   787     case GLP_NOFEAS:
   787     case GLP_NOFEAS:
   788       return INFEASIBLE;
   788       return INFEASIBLE;
   789     default:
   789     default:
   790       LEMON_ASSERT(false, "Wrong primal type");
   790       LEMON_ASSERT(false, "Wrong primal type");
   791       return  LpGlpk::ProblemType();
   791       return  GlpkLp::ProblemType();
   792     }
   792     }
   793   }
   793   }
   794 
   794 
   795   LpGlpk::ProblemType LpGlpk::_getDualType() const {
   795   GlpkLp::ProblemType GlpkLp::_getDualType() const {
   796     if (glp_get_status(lp) == GLP_OPT)
   796     if (glp_get_status(lp) == GLP_OPT)
   797       return OPTIMAL;
   797       return OPTIMAL;
   798     switch (glp_get_dual_stat(lp)) {
   798     switch (glp_get_dual_stat(lp)) {
   799     case GLP_UNDEF:
   799     case GLP_UNDEF:
   800       return UNDEFINED;
   800       return UNDEFINED;
   807       }
   807       }
   808     case GLP_NOFEAS:
   808     case GLP_NOFEAS:
   809       return INFEASIBLE;
   809       return INFEASIBLE;
   810     default:
   810     default:
   811       LEMON_ASSERT(false, "Wrong primal type");
   811       LEMON_ASSERT(false, "Wrong primal type");
   812       return  LpGlpk::ProblemType();
   812       return  GlpkLp::ProblemType();
   813     }
   813     }
   814   }
   814   }
   815 
   815 
   816   void LpGlpk::presolver(bool b) {
   816   void GlpkLp::presolver(bool b) {
   817     lpx_set_int_parm(lp, LPX_K_PRESOL, b ? 1 : 0);
   817     lpx_set_int_parm(lp, LPX_K_PRESOL, b ? 1 : 0);
   818   }
   818   }
   819 
   819 
   820   void LpGlpk::messageLevel(MessageLevel m) {
   820   void GlpkLp::messageLevel(MessageLevel m) {
   821     _message_level = m;
   821     _message_level = m;
   822   }
   822   }
   823 
   823 
   824   // MipGlpk members
   824   // GlpkMip members
   825 
   825 
   826   MipGlpk::MipGlpk()
   826   GlpkMip::GlpkMip()
   827     : LpBase(), GlpkBase(), MipSolver() {
   827     : LpBase(), GlpkBase(), MipSolver() {
   828     messageLevel(MESSAGE_NO_OUTPUT);
   828     messageLevel(MESSAGE_NO_OUTPUT);
   829   }
   829   }
   830 
   830 
   831   MipGlpk::MipGlpk(const MipGlpk& other)
   831   GlpkMip::GlpkMip(const GlpkMip& other)
   832     : LpBase(), GlpkBase(other), MipSolver() {
   832     : LpBase(), GlpkBase(other), MipSolver() {
   833     messageLevel(MESSAGE_NO_OUTPUT);
   833     messageLevel(MESSAGE_NO_OUTPUT);
   834   }
   834   }
   835 
   835 
   836   void MipGlpk::_setColType(int i, MipGlpk::ColTypes col_type) {
   836   void GlpkMip::_setColType(int i, GlpkMip::ColTypes col_type) {
   837     switch (col_type) {
   837     switch (col_type) {
   838     case INTEGER:
   838     case INTEGER:
   839       glp_set_col_kind(lp, i, GLP_IV);
   839       glp_set_col_kind(lp, i, GLP_IV);
   840       break;
   840       break;
   841     case REAL:
   841     case REAL:
   842       glp_set_col_kind(lp, i, GLP_CV);
   842       glp_set_col_kind(lp, i, GLP_CV);
   843       break;
   843       break;
   844     }
   844     }
   845   }
   845   }
   846 
   846 
   847   MipGlpk::ColTypes MipGlpk::_getColType(int i) const {
   847   GlpkMip::ColTypes GlpkMip::_getColType(int i) const {
   848     switch (glp_get_col_kind(lp, i)) {
   848     switch (glp_get_col_kind(lp, i)) {
   849     case GLP_IV:
   849     case GLP_IV:
   850     case GLP_BV:
   850     case GLP_BV:
   851       return INTEGER;
   851       return INTEGER;
   852     default:
   852     default:
   853       return REAL;
   853       return REAL;
   854     }
   854     }
   855 
   855 
   856   }
   856   }
   857 
   857 
   858   MipGlpk::SolveExitStatus MipGlpk::_solve() {
   858   GlpkMip::SolveExitStatus GlpkMip::_solve() {
   859     glp_smcp smcp;
   859     glp_smcp smcp;
   860     glp_init_smcp(&smcp);
   860     glp_init_smcp(&smcp);
   861 
   861 
   862     switch (_message_level) {
   862     switch (_message_level) {
   863     case MESSAGE_NO_OUTPUT:
   863     case MESSAGE_NO_OUTPUT:
   899     if (glp_intopt(lp, &iocp) != 0) return UNSOLVED;
   899     if (glp_intopt(lp, &iocp) != 0) return UNSOLVED;
   900     return SOLVED;
   900     return SOLVED;
   901   }
   901   }
   902 
   902 
   903 
   903 
   904   MipGlpk::ProblemType MipGlpk::_getType() const {
   904   GlpkMip::ProblemType GlpkMip::_getType() const {
   905     switch (glp_get_status(lp)) {
   905     switch (glp_get_status(lp)) {
   906     case GLP_OPT:
   906     case GLP_OPT:
   907       switch (glp_mip_status(lp)) {
   907       switch (glp_mip_status(lp)) {
   908       case GLP_UNDEF:
   908       case GLP_UNDEF:
   909         return UNDEFINED;
   909         return UNDEFINED;
   913         return FEASIBLE;
   913         return FEASIBLE;
   914       case GLP_OPT:
   914       case GLP_OPT:
   915         return OPTIMAL;
   915         return OPTIMAL;
   916       default:
   916       default:
   917         LEMON_ASSERT(false, "Wrong problem type.");
   917         LEMON_ASSERT(false, "Wrong problem type.");
   918         return MipGlpk::ProblemType();
   918         return GlpkMip::ProblemType();
   919       }
   919       }
   920     case GLP_NOFEAS:
   920     case GLP_NOFEAS:
   921       return INFEASIBLE;
   921       return INFEASIBLE;
   922     case GLP_INFEAS:
   922     case GLP_INFEAS:
   923     case GLP_FEAS:
   923     case GLP_FEAS:
   926       } else {
   926       } else {
   927         return UNDEFINED;
   927         return UNDEFINED;
   928       }
   928       }
   929     default:
   929     default:
   930       LEMON_ASSERT(false, "Wrong problem type.");
   930       LEMON_ASSERT(false, "Wrong problem type.");
   931       return MipGlpk::ProblemType();
   931       return GlpkMip::ProblemType();
   932     }
   932     }
   933   }
   933   }
   934 
   934 
   935   MipGlpk::Value MipGlpk::_getSol(int i) const {
   935   GlpkMip::Value GlpkMip::_getSol(int i) const {
   936     return glp_mip_col_val(lp, i);
   936     return glp_mip_col_val(lp, i);
   937   }
   937   }
   938 
   938 
   939   MipGlpk::Value MipGlpk::_getSolValue() const {
   939   GlpkMip::Value GlpkMip::_getSolValue() const {
   940     return glp_mip_obj_val(lp);
   940     return glp_mip_obj_val(lp);
   941   }
   941   }
   942 
   942 
   943   MipGlpk* MipGlpk::_newSolver() const { return new MipGlpk; }
   943   GlpkMip* GlpkMip::_newSolver() const { return new GlpkMip; }
   944   MipGlpk* MipGlpk::_cloneSolver() const {return new MipGlpk(*this); }
   944   GlpkMip* GlpkMip::_cloneSolver() const {return new GlpkMip(*this); }
   945 
   945 
   946   const char* MipGlpk::_solverName() const { return "MipGlpk"; }
   946   const char* GlpkMip::_solverName() const { return "GlpkMip"; }
   947 
   947 
   948   void MipGlpk::messageLevel(MessageLevel m) {
   948   void GlpkMip::messageLevel(MessageLevel m) {
   949     _message_level = m;
   949     _message_level = m;
   950   }
   950   }
   951 
   951 
   952 } //END OF NAMESPACE LEMON
   952 } //END OF NAMESPACE LEMON