src/lemon/lp_glpk.cc
author athos
Thu, 07 Apr 2005 15:22:03 +0000
changeset 1319 6e277ba3fc76
parent 1312 48f9299b390d
child 1321 bc3a4c498eb2
permissions -rw-r--r--
Cplex interface has improved a lot.
     1 /* -*- C++ -*-
     2  * src/lemon/lp_glpk.cc - Part of LEMON, a generic C++ optimization library
     3  *
     4  * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     5  * (Egervary Combinatorial Optimization Research Group, EGRES).
     6  *
     7  * Permission to use, modify and distribute this software is granted
     8  * provided that this copyright notice appears in all copies. For
     9  * precise terms see the accompanying LICENSE file.
    10  *
    11  * This software is provided "AS IS" with no warranty of any kind,
    12  * express or implied, and with no claim as to its suitability for any
    13  * purpose.
    14  *
    15  */
    16 
    17 #ifndef LEMON_LP_GLPK_CC
    18 #define LEMON_LP_GLPK_CC
    19 
    20 ///\file
    21 ///\brief Implementation of the LEMON-GLPK lp solver interface.
    22 
    23 #include <lemon/lp_glpk.h>
    24 
    25 namespace lemon {
    26 
    27     /// \e
    28     int LpGlpk::_addCol() { 
    29 	int i=lpx_add_cols(lp, 1);
    30 	_setColLowerBound(i, -INF);
    31 	_setColUpperBound(i, INF);
    32 	return i;
    33     }
    34 
    35     /// \e
    36     int LpGlpk::_addRow() { 
    37 	int i=lpx_add_rows(lp, 1);
    38 	return i;
    39     }
    40 
    41   
    42     void LpGlpk::_setRowCoeffs(int i, 
    43 			       int length,
    44 			       const int   * indices, 
    45 			       const Value   * values )
    46     {
    47       lpx_set_mat_row(lp, i, length,
    48 		      const_cast<int * >(indices) ,
    49 		      const_cast<Value * >(values));
    50     }
    51   
    52     void LpGlpk::_setColCoeffs(int i, 
    53 			       int length,
    54 			       const int   * indices, 
    55 			       const Value   * values)
    56     {
    57       lpx_set_mat_col(lp, i, length,
    58 		      const_cast<int * >(indices),
    59 		      const_cast<Value * >(values));
    60     }
    61   
    62     void LpGlpk::_setColLowerBound(int i, Value lo)
    63     {
    64       if (lo==INF) {
    65 	//FIXME error
    66       }
    67       int b=lpx_get_col_type(lp, i);
    68       double up=lpx_get_col_ub(lp, i);	
    69       if (lo==-INF) {
    70 	switch (b) {
    71 	case LPX_FR:
    72 	case LPX_LO:
    73 	  lpx_set_col_bnds(lp, i, LPX_FR, lo, up);
    74 	  break;
    75 	case LPX_UP:
    76 	  break;
    77 	case LPX_DB:
    78 	case LPX_FX:
    79 	  lpx_set_col_bnds(lp, i, LPX_UP, lo, up);
    80 	  break;
    81 	default: ;
    82 	  //FIXME error
    83 	}
    84       } else {
    85 	switch (b) {
    86 	case LPX_FR:
    87 	case LPX_LO:
    88 	  lpx_set_col_bnds(lp, i, LPX_LO, lo, up);
    89 	  break;
    90 	case LPX_UP:	  
    91 	case LPX_DB:
    92 	case LPX_FX:
    93 	  if (lo==up) 
    94 	    lpx_set_col_bnds(lp, i, LPX_FX, lo, up);
    95 	  else 
    96 	    lpx_set_col_bnds(lp, i, LPX_DB, lo, up);
    97 	  break;
    98 	default: ;
    99 	  //FIXME error
   100 	}
   101       }
   102 
   103     }
   104   
   105     void LpGlpk::_setColUpperBound(int i, Value up)
   106     {
   107       if (up==-INF) {
   108 	//FIXME error
   109       }
   110       int b=lpx_get_col_type(lp, i);
   111       double lo=lpx_get_col_lb(lp, i);
   112       if (up==INF) {
   113 	switch (b) {
   114 	case LPX_FR:
   115 	case LPX_LO:
   116 	  break;
   117 	case LPX_UP:
   118 	  lpx_set_col_bnds(lp, i, LPX_FR, lo, up);
   119 	  break;
   120 	case LPX_DB:
   121 	case LPX_FX:
   122 	  lpx_set_col_bnds(lp, i, LPX_LO, lo, up);
   123 	  break;
   124 	default: ;
   125 	  //FIXME error
   126 	}
   127       } else {
   128 	switch (b) {
   129 	case LPX_FR:
   130 	  lpx_set_col_bnds(lp, i, LPX_UP, lo, up);
   131 	  break;
   132 	case LPX_UP:
   133 	  lpx_set_col_bnds(lp, i, LPX_UP, lo, up);
   134 	  break;
   135 	case LPX_LO:
   136 	case LPX_DB:
   137 	case LPX_FX:
   138 	  if (lo==up) 
   139 	    lpx_set_col_bnds(lp, i, LPX_FX, lo, up);
   140 	  else 
   141 	    lpx_set_col_bnds(lp, i, LPX_DB, lo, up);
   142 	  break;
   143 	default: ;
   144 	  //FIXME error
   145 	}
   146       }
   147     }
   148   
   149     void LpGlpk::_setRowLowerBound(int i, Value lo)
   150     {
   151       if (lo==INF) {
   152 	//FIXME error
   153       }
   154       int b=lpx_get_row_type(lp, i);
   155       double up=lpx_get_row_ub(lp, i);	
   156       if (lo==-INF) {
   157 	switch (b) {
   158 	case LPX_FR:
   159 	case LPX_LO:
   160 	  lpx_set_row_bnds(lp, i, LPX_FR, lo, up);
   161 	  break;
   162 	case LPX_UP:
   163 	  break;
   164 	case LPX_DB:
   165 	case LPX_FX:
   166 	  lpx_set_row_bnds(lp, i, LPX_UP, lo, up);
   167 	  break;
   168 	default: ;
   169 	  //FIXME error
   170 	}
   171       } else {
   172 	switch (b) {
   173 	case LPX_FR:
   174 	case LPX_LO:
   175 	  lpx_set_row_bnds(lp, i, LPX_LO, lo, up);
   176 	  break;
   177 	case LPX_UP:	  
   178 	case LPX_DB:
   179 	case LPX_FX:
   180 	  if (lo==up) 
   181 	    lpx_set_row_bnds(lp, i, LPX_FX, lo, up);
   182 	  else 
   183 	    lpx_set_row_bnds(lp, i, LPX_DB, lo, up);
   184 	  break;
   185 	default: ;
   186 	  //FIXME error
   187 	}
   188       }
   189     }
   190   
   191   void LpGlpk::_setRowUpperBound(int i, Value up)
   192   {
   193     if (up==-INF) {
   194       //FIXME error
   195     }
   196     int b=lpx_get_row_type(lp, i);
   197     double lo=lpx_get_row_lb(lp, i);
   198     if (up==INF) {
   199       switch (b) {
   200       case LPX_FR:
   201       case LPX_LO:
   202 	break;
   203       case LPX_UP:
   204 	lpx_set_row_bnds(lp, i, LPX_FR, lo, up);
   205 	break;
   206       case LPX_DB:
   207       case LPX_FX:
   208 	lpx_set_row_bnds(lp, i, LPX_LO, lo, up);
   209 	break;
   210       default: ;
   211 	//FIXME error
   212       }
   213     } else {
   214       switch (b) {
   215       case LPX_FR:
   216 	lpx_set_row_bnds(lp, i, LPX_UP, lo, up);
   217 	break;
   218       case LPX_UP:
   219 	lpx_set_row_bnds(lp, i, LPX_UP, lo, up);
   220 	break;
   221       case LPX_LO:
   222       case LPX_DB:
   223       case LPX_FX:
   224 	if (lo==up) 
   225 	  lpx_set_row_bnds(lp, i, LPX_FX, lo, up);
   226 	else 
   227 	  lpx_set_row_bnds(lp, i, LPX_DB, lo, up);
   228 	break;
   229       default: ;
   230 	//FIXME error
   231       }
   232     }
   233   }
   234   
   235   void LpGlpk::_setObjCoeff(int i, Value obj_coef)
   236   {
   237     lpx_set_obj_coef(lp, i, obj_coef);
   238   }
   239 
   240 
   241   LpGlpk::SolveExitStatus LpGlpk::_solve()
   242   {
   243     int i=  lpx_simplex(lp);
   244     switch (i) {
   245     case LPX_E_OK: 
   246       return SOLVED;
   247       break;
   248     default:
   249       return UNSOLVED;
   250     }
   251   }
   252 
   253   LpGlpk::Value LpGlpk::_getPrimal(int i)
   254   {
   255     return lpx_get_col_prim(lp,i);
   256   }
   257   
   258   LpGlpk::Value LpGlpk::_getPrimalValue()
   259   {
   260     return lpx_get_obj_val(lp);
   261   }
   262   
   263  
   264   LpGlpk::SolutionStatus LpGlpk::_getPrimalStatus()
   265   {
   266     int stat=  lpx_get_status(lp);
   267     switch (stat) {
   268     case LPX_UNDEF://Undefined (no solve has been run yet)
   269       return UNDEFINED;
   270       break;
   271     case LPX_NOFEAS://There is no feasible solution (primal, I guess)
   272     case LPX_INFEAS://Infeasible 
   273       return INFEASIBLE;
   274       break;
   275     case LPX_UNBND://Unbounded
   276       return INFINITE;
   277       break;
   278     case LPX_FEAS://Feasible
   279       return FEASIBLE;
   280       break;
   281     case LPX_OPT://Feasible
   282       return OPTIMAL;
   283       break;
   284     default:
   285       return UNDEFINED; //to avoid gcc warning
   286       //FIXME error
   287     }
   288   }
   289 
   290 
   291   void LpGlpk::_setMax()
   292   {
   293       lpx_set_obj_dir(lp, LPX_MAX);
   294    }
   295   void LpGlpk::_setMin()
   296   {
   297       lpx_set_obj_dir(lp, LPX_MIN);
   298    }
   299 
   300  
   301 } //END OF NAMESPACE LEMON
   302 
   303 #endif //LEMON_LP_GLPK_CC