src/lemon/lp_glpk.cc
changeset 1321 bc3a4c498eb2
parent 1314 9269c76551cf
child 1326 85f1c483279e
equal deleted inserted replaced
2:cc22e5e8d894 3:f42e4ce91237
    22 
    22 
    23 #include <lemon/lp_glpk.h>
    23 #include <lemon/lp_glpk.h>
    24 
    24 
    25 namespace lemon {
    25 namespace lemon {
    26 
    26 
    27     /// \e
    27   LpGlpk::LpGlpk() : Parent(), 
    28     int LpGlpk::_addCol() { 
    28 		     lp(lpx_create_prob()) {
    29 	int i=lpx_add_cols(lp, 1);
    29     ///\todo constrol function for this:
    30 	_setColLowerBound(i, -INF);
    30     lpx_set_int_parm(lp, LPX_K_DUAL, 1);
    31 	_setColUpperBound(i, INF);
    31     messageLevel(0);
    32 	return i;
    32   }
    33     }
    33   
    34 
    34   LpGlpk::~LpGlpk() {
    35     /// \e
    35     lpx_delete_prob(lp);
    36     int LpGlpk::_addRow() { 
    36   }
    37 	int i=lpx_add_rows(lp, 1);
    37   
    38 	return i;
    38   int LpGlpk::_addCol() { 
    39     }
    39     int i=lpx_add_cols(lp, 1);
    40 
    40     _setColLowerBound(i, -INF);
    41   
    41     _setColUpperBound(i, INF);
    42     void LpGlpk::_setRowCoeffs(int i, 
    42     return i;
    43 			       int length,
    43   }
    44 			       const int   * indices, 
    44 
    45 			       const Value   * values )
    45   int LpGlpk::_addRow() { 
    46     {
    46     int i=lpx_add_rows(lp, 1);
    47       lpx_set_mat_row(lp, i, length,
    47     return i;
    48 		      const_cast<int * >(indices) ,
    48   }
    49 		      const_cast<Value * >(values));
    49 
    50     }
    50   
    51   
    51   void LpGlpk::_setRowCoeffs(int i, 
    52     void LpGlpk::_setColCoeffs(int i, 
    52 			     int length,
    53 			       int length,
    53 			     const int   * indices, 
    54 			       const int   * indices, 
    54 			     const Value   * values )
    55 			       const Value   * values)
    55   {
    56     {
    56     lpx_set_mat_row(lp, i, length,
    57       lpx_set_mat_col(lp, i, length,
    57 		    const_cast<int * >(indices) ,
    58 		      const_cast<int * >(indices),
    58 		    const_cast<Value * >(values));
    59 		      const_cast<Value * >(values));
    59   }
    60     }
    60   
    61   
    61   void LpGlpk::_setColCoeffs(int i, 
    62     void LpGlpk::_setColLowerBound(int i, Value lo)
    62 			     int length,
    63     {
    63 			     const int   * indices, 
    64       if (lo==INF) {
    64 			     const Value   * values)
    65 	//FIXME error
    65   {
    66       }
    66     lpx_set_mat_col(lp, i, length,
    67       int b=lpx_get_col_type(lp, i);
    67 		    const_cast<int * >(indices),
    68       double up=lpx_get_col_ub(lp, i);	
    68 		    const_cast<Value * >(values));
    69       if (lo==-INF) {
    69   }
    70 	switch (b) {
    70   
    71 	case LPX_FR:
    71   void LpGlpk::_setColLowerBound(int i, Value lo)
    72 	case LPX_LO:
    72   {
    73 	  lpx_set_col_bnds(lp, i, LPX_FR, lo, up);
    73     if (lo==INF) {
    74 	  break;
    74       //FIXME error
    75 	case LPX_UP:
    75     }
    76 	  break;
    76     int b=lpx_get_col_type(lp, i);
    77 	case LPX_DB:
    77     double up=lpx_get_col_ub(lp, i);	
    78 	case LPX_FX:
    78     if (lo==-INF) {
    79 	  lpx_set_col_bnds(lp, i, LPX_UP, lo, up);
    79       switch (b) {
    80 	  break;
    80       case LPX_FR:
    81 	default: ;
    81       case LPX_LO:
    82 	  //FIXME error
    82 	lpx_set_col_bnds(lp, i, LPX_FR, lo, up);
    83 	}
    83 	break;
    84       } else {
    84       case LPX_UP:
    85 	switch (b) {
    85 	break;
    86 	case LPX_FR:
    86       case LPX_DB:
    87 	case LPX_LO:
    87       case LPX_FX:
    88 	  lpx_set_col_bnds(lp, i, LPX_LO, lo, up);
    88 	lpx_set_col_bnds(lp, i, LPX_UP, lo, up);
    89 	  break;
    89 	break;
    90 	case LPX_UP:	  
    90       default: ;
    91 	case LPX_DB:
    91 	//FIXME error
    92 	case LPX_FX:
    92       }
    93 	  if (lo==up) 
    93     } else {
    94 	    lpx_set_col_bnds(lp, i, LPX_FX, lo, up);
    94       switch (b) {
    95 	  else 
    95       case LPX_FR:
    96 	    lpx_set_col_bnds(lp, i, LPX_DB, lo, up);
    96       case LPX_LO:
    97 	  break;
    97 	lpx_set_col_bnds(lp, i, LPX_LO, lo, up);
    98 	default: ;
    98 	break;
    99 	  //FIXME error
    99       case LPX_UP:	  
   100 	}
   100       case LPX_DB:
   101       }
   101       case LPX_FX:
   102 
   102 	if (lo==up) 
   103     }
   103 	  lpx_set_col_bnds(lp, i, LPX_FX, lo, up);
   104   
   104 	else 
   105     void LpGlpk::_setColUpperBound(int i, Value up)
   105 	  lpx_set_col_bnds(lp, i, LPX_DB, lo, up);
   106     {
   106 	break;
   107       if (up==-INF) {
   107       default: ;
   108 	//FIXME error
   108 	//FIXME error
   109       }
   109       }
   110       int b=lpx_get_col_type(lp, i);
   110     }
   111       double lo=lpx_get_col_lb(lp, i);
   111 
   112       if (up==INF) {
   112   }
   113 	switch (b) {
   113   
   114 	case LPX_FR:
   114   void LpGlpk::_setColUpperBound(int i, Value up)
   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   {
   115   {
   193     if (up==-INF) {
   116     if (up==-INF) {
   194       //FIXME error
   117       //FIXME error
   195     }
   118     }
       
   119     int b=lpx_get_col_type(lp, i);
       
   120     double lo=lpx_get_col_lb(lp, i);
       
   121     if (up==INF) {
       
   122       switch (b) {
       
   123       case LPX_FR:
       
   124       case LPX_LO:
       
   125 	break;
       
   126       case LPX_UP:
       
   127 	lpx_set_col_bnds(lp, i, LPX_FR, lo, up);
       
   128 	break;
       
   129       case LPX_DB:
       
   130       case LPX_FX:
       
   131 	lpx_set_col_bnds(lp, i, LPX_LO, lo, up);
       
   132 	break;
       
   133       default: ;
       
   134 	//FIXME error
       
   135       }
       
   136     } else {
       
   137       switch (b) {
       
   138       case LPX_FR:
       
   139 	lpx_set_col_bnds(lp, i, LPX_UP, lo, up);
       
   140 	break;
       
   141       case LPX_UP:
       
   142 	lpx_set_col_bnds(lp, i, LPX_UP, lo, up);
       
   143 	break;
       
   144       case LPX_LO:
       
   145       case LPX_DB:
       
   146       case LPX_FX:
       
   147 	if (lo==up) 
       
   148 	  lpx_set_col_bnds(lp, i, LPX_FX, lo, up);
       
   149 	else 
       
   150 	  lpx_set_col_bnds(lp, i, LPX_DB, lo, up);
       
   151 	break;
       
   152       default: ;
       
   153 	//FIXME error
       
   154       }
       
   155     }
       
   156   }
       
   157   
       
   158   void LpGlpk::_setRowLowerBound(int i, Value lo)
       
   159   {
       
   160     if (lo==INF) {
       
   161       //FIXME error
       
   162     }
   196     int b=lpx_get_row_type(lp, i);
   163     int b=lpx_get_row_type(lp, i);
   197     double lo=lpx_get_row_lb(lp, i);
   164     double up=lpx_get_row_ub(lp, i);	
   198     if (up==INF) {
   165     if (lo==-INF) {
   199       switch (b) {
   166       switch (b) {
   200       case LPX_FR:
   167       case LPX_FR:
   201       case LPX_LO:
   168       case LPX_LO:
   202 	break;
       
   203       case LPX_UP:
       
   204 	lpx_set_row_bnds(lp, i, LPX_FR, lo, up);
   169 	lpx_set_row_bnds(lp, i, LPX_FR, lo, up);
   205 	break;
   170 	break;
   206       case LPX_DB:
   171       case LPX_UP:
   207       case LPX_FX:
   172 	break;
       
   173       case LPX_DB:
       
   174       case LPX_FX:
       
   175 	lpx_set_row_bnds(lp, i, LPX_UP, lo, up);
       
   176 	break;
       
   177       default: ;
       
   178 	//FIXME error
       
   179       }
       
   180     } else {
       
   181       switch (b) {
       
   182       case LPX_FR:
       
   183       case LPX_LO:
   208 	lpx_set_row_bnds(lp, i, LPX_LO, lo, up);
   184 	lpx_set_row_bnds(lp, i, LPX_LO, lo, up);
   209 	break;
   185 	break;
   210       default: ;
   186       case LPX_UP:	  
   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:
   187       case LPX_DB:
   223       case LPX_FX:
   188       case LPX_FX:
   224 	if (lo==up) 
   189 	if (lo==up) 
   225 	  lpx_set_row_bnds(lp, i, LPX_FX, lo, up);
   190 	  lpx_set_row_bnds(lp, i, LPX_FX, lo, up);
   226 	else 
   191 	else 
   230 	//FIXME error
   195 	//FIXME error
   231       }
   196       }
   232     }
   197     }
   233   }
   198   }
   234   
   199   
       
   200   void LpGlpk::_setRowUpperBound(int i, Value up)
       
   201   {
       
   202     if (up==-INF) {
       
   203       //FIXME error
       
   204     }
       
   205     int b=lpx_get_row_type(lp, i);
       
   206     double lo=lpx_get_row_lb(lp, i);
       
   207     if (up==INF) {
       
   208       switch (b) {
       
   209       case LPX_FR:
       
   210       case LPX_LO:
       
   211 	break;
       
   212       case LPX_UP:
       
   213 	lpx_set_row_bnds(lp, i, LPX_FR, lo, up);
       
   214 	break;
       
   215       case LPX_DB:
       
   216       case LPX_FX:
       
   217 	lpx_set_row_bnds(lp, i, LPX_LO, lo, up);
       
   218 	break;
       
   219       default: ;
       
   220 	//FIXME error
       
   221       }
       
   222     } else {
       
   223       switch (b) {
       
   224       case LPX_FR:
       
   225 	lpx_set_row_bnds(lp, i, LPX_UP, lo, up);
       
   226 	break;
       
   227       case LPX_UP:
       
   228 	lpx_set_row_bnds(lp, i, LPX_UP, lo, up);
       
   229 	break;
       
   230       case LPX_LO:
       
   231       case LPX_DB:
       
   232       case LPX_FX:
       
   233 	if (lo==up) 
       
   234 	  lpx_set_row_bnds(lp, i, LPX_FX, lo, up);
       
   235 	else 
       
   236 	  lpx_set_row_bnds(lp, i, LPX_DB, lo, up);
       
   237 	break;
       
   238       default: ;
       
   239 	//FIXME error
       
   240       }
       
   241     }
       
   242   }
       
   243   
   235   void LpGlpk::_setObjCoeff(int i, Value obj_coef)
   244   void LpGlpk::_setObjCoeff(int i, Value obj_coef)
   236   {
   245   {
   237     lpx_set_obj_coef(lp, i, obj_coef);
   246     lpx_set_obj_coef(lp, i, obj_coef);
   238   }
   247   }
   239 
   248 
   288   }
   297   }
   289 
   298 
   290 
   299 
   291   void LpGlpk::_setMax()
   300   void LpGlpk::_setMax()
   292   {
   301   {
   293       lpx_set_obj_dir(lp, LPX_MAX);
   302     lpx_set_obj_dir(lp, LPX_MAX);
   294    }
   303   }
       
   304 
   295   void LpGlpk::_setMin()
   305   void LpGlpk::_setMin()
   296   {
   306   {
   297       lpx_set_obj_dir(lp, LPX_MIN);
   307     lpx_set_obj_dir(lp, LPX_MIN);
   298    }
   308   }
       
   309 
       
   310  
       
   311   void LpGlpk::messageLevel(int m)
       
   312   {
       
   313     lpx_set_int_parm(lp, LPX_K_MSGLEV, m);
       
   314   }
   299 
   315 
   300  
   316  
   301 } //END OF NAMESPACE LEMON
   317 } //END OF NAMESPACE LEMON
   302 
   318 
   303 #endif //LEMON_LP_GLPK_CC
   319 #endif //LEMON_LP_GLPK_CC