Low level interface for GLPK (Marci ut?n szabadon)
authorathos
Fri, 25 Mar 2005 15:24:18 +0000
changeset 12619d0deeea8c08
parent 1260 d8491fce6751
child 1262 61f989e3e525
Low level interface for GLPK (Marci ut?n szabadon)
src/work/athos/lp/lp_glpk.cc
src/work/athos/lp/lp_glpk.h
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/work/athos/lp/lp_glpk.cc	Fri Mar 25 15:24:18 2005 +0000
     1.3 @@ -0,0 +1,238 @@
     1.4 +/* -*- C++ -*-
     1.5 + * src/lemon/lp_glpk.cc - Part of LEMON, a generic C++ optimization library
     1.6 + *
     1.7 + * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     1.8 + * (Egervary Combinatorial Optimization Research Group, EGRES).
     1.9 + *
    1.10 + * Permission to use, modify and distribute this software is granted
    1.11 + * provided that this copyright notice appears in all copies. For
    1.12 + * precise terms see the accompanying LICENSE file.
    1.13 + *
    1.14 + * This software is provided "AS IS" with no warranty of any kind,
    1.15 + * express or implied, and with no claim as to its suitability for any
    1.16 + * purpose.
    1.17 + *
    1.18 + */
    1.19 +
    1.20 +#ifndef LEMON_LP_GLPK_CC
    1.21 +#define LEMON_LP_GLPK_CC
    1.22 +
    1.23 +///\file
    1.24 +///\brief Implementation of the LEMON-GLPK lp solver interface.
    1.25 +
    1.26 +#include "lp_glpk.h"
    1.27 +
    1.28 +namespace lemon {
    1.29 +
    1.30 +    /// \e
    1.31 +    int LpGlpk::_addCol() { 
    1.32 +	int i=lpx_add_cols(lp, 1);
    1.33 +	_setColLowerBound(i, -INF);
    1.34 +	_setColUpperBound(i, INF);
    1.35 +	return i;
    1.36 +    }
    1.37 +
    1.38 +    /// \e
    1.39 +    int LpGlpk::_addRow() { 
    1.40 +	int i=lpx_add_rows(lp, 1);
    1.41 +	return i;
    1.42 +    }
    1.43 +
    1.44 +  
    1.45 +    void LpGlpk::_setRowCoeffs(int i, 
    1.46 +			       int length,
    1.47 +			       int   * indices, 
    1.48 +			       Value   * values )
    1.49 +    {
    1.50 +	lpx_set_mat_row(lp, i, length, indices, values);
    1.51 +    }
    1.52 +  
    1.53 +    void LpGlpk::_setColCoeffs(int i, 
    1.54 +			       int length,
    1.55 +			       int   * indices, 
    1.56 +			       Value   * values)
    1.57 +    {
    1.58 +	lpx_set_mat_col(lp, i, length, indices, values);
    1.59 +    }
    1.60 +  
    1.61 +    void LpGlpk::_setColLowerBound(int i, Value lo)
    1.62 +    {
    1.63 +      if (lo==INF) {
    1.64 +	//FIXME error
    1.65 +      }
    1.66 +      int b=lpx_get_col_type(lp, i);
    1.67 +      double up=lpx_get_col_ub(lp, i);	
    1.68 +      if (lo==-INF) {
    1.69 +	switch (b) {
    1.70 +	case LPX_FR:
    1.71 +	case LPX_LO:
    1.72 +	  lpx_set_col_bnds(lp, i, LPX_FR, lo, up);
    1.73 +	  break;
    1.74 +	case LPX_UP:
    1.75 +	  break;
    1.76 +	case LPX_DB:
    1.77 +	case LPX_FX:
    1.78 +	  lpx_set_col_bnds(lp, i, LPX_UP, lo, up);
    1.79 +	  break;
    1.80 +	default: ;
    1.81 +	  //FIXME error
    1.82 +	}
    1.83 +      } else {
    1.84 +	switch (b) {
    1.85 +	case LPX_FR:
    1.86 +	case LPX_LO:
    1.87 +	  lpx_set_col_bnds(lp, i, LPX_LO, lo, up);
    1.88 +	  break;
    1.89 +	case LPX_UP:	  
    1.90 +	case LPX_DB:
    1.91 +	case LPX_FX:
    1.92 +	  if (lo==up) 
    1.93 +	    lpx_set_col_bnds(lp, i, LPX_FX, lo, up);
    1.94 +	  else 
    1.95 +	    lpx_set_col_bnds(lp, i, LPX_DB, lo, up);
    1.96 +	  break;
    1.97 +	default: ;
    1.98 +	  //FIXME error
    1.99 +	}
   1.100 +      }
   1.101 +
   1.102 +    }
   1.103 +  
   1.104 +    void LpGlpk::_setColUpperBound(int i, Value up)
   1.105 +    {
   1.106 +      if (up==-INF) {
   1.107 +	//FIXME error
   1.108 +      }
   1.109 +      int b=lpx_get_col_type(lp, i);
   1.110 +      double lo=lpx_get_col_lb(lp, i);
   1.111 +      if (up==INF) {
   1.112 +	switch (b) {
   1.113 +	case LPX_FR:
   1.114 +	case LPX_LO:
   1.115 +	  break;
   1.116 +	case LPX_UP:
   1.117 +	  lpx_set_col_bnds(lp, i, LPX_FR, lo, up);
   1.118 +	  break;
   1.119 +	case LPX_DB:
   1.120 +	case LPX_FX:
   1.121 +	  lpx_set_col_bnds(lp, i, LPX_LO, lo, up);
   1.122 +	  break;
   1.123 +	default: ;
   1.124 +	  //FIXME error
   1.125 +	}
   1.126 +      } else {
   1.127 +	switch (b) {
   1.128 +	case LPX_FR:
   1.129 +	  lpx_set_col_bnds(lp, i, LPX_UP, lo, up);
   1.130 +	  break;
   1.131 +	case LPX_UP:
   1.132 +	  lpx_set_col_bnds(lp, i, LPX_UP, lo, up);
   1.133 +	  break;
   1.134 +	case LPX_LO:
   1.135 +	case LPX_DB:
   1.136 +	case LPX_FX:
   1.137 +	  if (lo==up) 
   1.138 +	    lpx_set_col_bnds(lp, i, LPX_FX, lo, up);
   1.139 +	  else 
   1.140 +	    lpx_set_col_bnds(lp, i, LPX_DB, lo, up);
   1.141 +	  break;
   1.142 +	default: ;
   1.143 +	  //FIXME error
   1.144 +	}
   1.145 +      }
   1.146 +    }
   1.147 +  
   1.148 +    void LpGlpk::_setRowLowerBound(int i, Value lo)
   1.149 +    {
   1.150 +      if (lo==INF) {
   1.151 +	//FIXME error
   1.152 +      }
   1.153 +      int b=lpx_get_row_type(lp, i);
   1.154 +      double up=lpx_get_row_ub(lp, i);	
   1.155 +      if (lo==-INF) {
   1.156 +	switch (b) {
   1.157 +	case LPX_FR:
   1.158 +	case LPX_LO:
   1.159 +	  lpx_set_row_bnds(lp, i, LPX_FR, lo, up);
   1.160 +	  break;
   1.161 +	case LPX_UP:
   1.162 +	  break;
   1.163 +	case LPX_DB:
   1.164 +	case LPX_FX:
   1.165 +	  lpx_set_row_bnds(lp, i, LPX_UP, lo, up);
   1.166 +	  break;
   1.167 +	default: ;
   1.168 +	  //FIXME error
   1.169 +	}
   1.170 +      } else {
   1.171 +	switch (b) {
   1.172 +	case LPX_FR:
   1.173 +	case LPX_LO:
   1.174 +	  lpx_set_row_bnds(lp, i, LPX_LO, lo, up);
   1.175 +	  break;
   1.176 +	case LPX_UP:	  
   1.177 +	case LPX_DB:
   1.178 +	case LPX_FX:
   1.179 +	  if (lo==up) 
   1.180 +	    lpx_set_row_bnds(lp, i, LPX_FX, lo, up);
   1.181 +	  else 
   1.182 +	    lpx_set_row_bnds(lp, i, LPX_DB, lo, up);
   1.183 +	  break;
   1.184 +	default: ;
   1.185 +	  //FIXME error
   1.186 +	}
   1.187 +      }
   1.188 +    }
   1.189 +  
   1.190 +    void LpGlpk::_setRowUpperBound(int i, Value up)
   1.191 +    {
   1.192 +      if (up==-INF) {
   1.193 +	//FIXME error
   1.194 +      }
   1.195 +      int b=lpx_get_row_type(lp, i);
   1.196 +      double lo=lpx_get_row_lb(lp, i);
   1.197 +      if (up==INF) {
   1.198 +	switch (b) {
   1.199 +	case LPX_FR:
   1.200 +	case LPX_LO:
   1.201 +	  break;
   1.202 +	case LPX_UP:
   1.203 +	  lpx_set_row_bnds(lp, i, LPX_FR, lo, up);
   1.204 +	  break;
   1.205 +	case LPX_DB:
   1.206 +	case LPX_FX:
   1.207 +	  lpx_set_row_bnds(lp, i, LPX_LO, lo, up);
   1.208 +	  break;
   1.209 +	default: ;
   1.210 +	  //FIXME error
   1.211 +	}
   1.212 +      } else {
   1.213 +	switch (b) {
   1.214 +	case LPX_FR:
   1.215 +	  lpx_set_row_bnds(lp, i, LPX_UP, lo, up);
   1.216 +	  break;
   1.217 +	case LPX_UP:
   1.218 +	  lpx_set_row_bnds(lp, i, LPX_UP, lo, up);
   1.219 +	  break;
   1.220 +	case LPX_LO:
   1.221 +	case LPX_DB:
   1.222 +	case LPX_FX:
   1.223 +	  if (lo==up) 
   1.224 +	    lpx_set_row_bnds(lp, i, LPX_FX, lo, up);
   1.225 +	  else 
   1.226 +	    lpx_set_row_bnds(lp, i, LPX_DB, lo, up);
   1.227 +	  break;
   1.228 +	default: ;
   1.229 +	  //FIXME error
   1.230 +	}
   1.231 +      }
   1.232 +    }
   1.233 +  
   1.234 +    void LpGlpk::_setObjCoeff(int i, Value obj_coef)
   1.235 +    {
   1.236 +      lpx_set_obj_coef(lp, i, obj_coef);
   1.237 +    }
   1.238 +
   1.239 +} //END OF NAMESPACE LEMON
   1.240 +
   1.241 +#endif //LEMON_LP_GLPK_CC
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/work/athos/lp/lp_glpk.h	Fri Mar 25 15:24:18 2005 +0000
     2.3 @@ -0,0 +1,74 @@
     2.4 +/* -*- C++ -*-
     2.5 + * src/lemon/lp_glpk.h - Part of LEMON, a generic C++ optimization library
     2.6 + *
     2.7 + * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     2.8 + * (Egervary Combinatorial Optimization Research Group, EGRES).
     2.9 + *
    2.10 + * Permission to use, modify and distribute this software is granted
    2.11 + * provided that this copyright notice appears in all copies. For
    2.12 + * precise terms see the accompanying LICENSE file.
    2.13 + *
    2.14 + * This software is provided "AS IS" with no warranty of any kind,
    2.15 + * express or implied, and with no claim as to its suitability for any
    2.16 + * purpose.
    2.17 + *
    2.18 + */
    2.19 +
    2.20 +#ifndef LEMON_LP_GLPK_H
    2.21 +#define LEMON_LP_GLPK_H
    2.22 +
    2.23 +///\file
    2.24 +///\brief Header of the LEMON-GLPK lp solver interface.
    2.25 +
    2.26 +#include "lp_base.h"
    2.27 +extern "C" {
    2.28 +#include "glpk.h"
    2.29 +}
    2.30 +
    2.31 +namespace lemon {
    2.32 +
    2.33 +
    2.34 +  /// \brief Wrapper for GLPK solver
    2.35 +  /// 
    2.36 +  /// This class implements a lemon wrapper for GLPK.
    2.37 +  class LpGlpk : public LpSolverBase {
    2.38 +
    2.39 +  public:
    2.40 +
    2.41 +    typedef LpSolverBase Parent;
    2.42 +    
    2.43 +    /// \e
    2.44 +    LPX* lp;
    2.45 +
    2.46 +    /// \e
    2.47 +    LpGlpk() : Parent(), 
    2.48 +			lp(lpx_create_prob()) {
    2.49 +      lpx_set_int_parm(lp, LPX_K_DUAL, 1);
    2.50 +    }
    2.51 +    /// \e
    2.52 +    ~LpGlpk() {
    2.53 +      lpx_delete_prob(lp);
    2.54 +    }
    2.55 +
    2.56 +  protected:
    2.57 +    virtual int _addCol();
    2.58 +    virtual int _addRow();
    2.59 +    virtual void _setRowCoeffs(int i, 
    2.60 +			       int length,
    2.61 +                               int   * indices, 
    2.62 +                               Value   * values );
    2.63 +    virtual void _setColCoeffs(int i, 
    2.64 +			       int length,
    2.65 +                               int   * indices, 
    2.66 +                               Value   * values);
    2.67 +    virtual void _setColLowerBound(int i, Value value);
    2.68 +    virtual void _setColUpperBound(int i, Value value);
    2.69 +    virtual void _setRowLowerBound(int i, Value value);
    2.70 +    virtual void _setRowUpperBound(int i, Value value);
    2.71 +    virtual void _setObjCoeff(int i, Value obj_coef);
    2.72 +
    2.73 +  };
    2.74 +} //END OF NAMESPACE LEMON
    2.75 +
    2.76 +#endif //LEMON_LP_GLPK_H
    2.77 +