src/glpapi02.c
changeset 1 c445c931472f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/glpapi02.c	Mon Dec 06 13:09:21 2010 +0100
     1.3 @@ -0,0 +1,491 @@
     1.4 +/* glpapi02.c (problem retrieving routines) */
     1.5 +
     1.6 +/***********************************************************************
     1.7 +*  This code is part of GLPK (GNU Linear Programming Kit).
     1.8 +*
     1.9 +*  Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
    1.10 +*  2009, 2010 Andrew Makhorin, Department for Applied Informatics,
    1.11 +*  Moscow Aviation Institute, Moscow, Russia. All rights reserved.
    1.12 +*  E-mail: <mao@gnu.org>.
    1.13 +*
    1.14 +*  GLPK is free software: you can redistribute it and/or modify it
    1.15 +*  under the terms of the GNU General Public License as published by
    1.16 +*  the Free Software Foundation, either version 3 of the License, or
    1.17 +*  (at your option) any later version.
    1.18 +*
    1.19 +*  GLPK is distributed in the hope that it will be useful, but WITHOUT
    1.20 +*  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    1.21 +*  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
    1.22 +*  License for more details.
    1.23 +*
    1.24 +*  You should have received a copy of the GNU General Public License
    1.25 +*  along with GLPK. If not, see <http://www.gnu.org/licenses/>.
    1.26 +***********************************************************************/
    1.27 +
    1.28 +#include "glpapi.h"
    1.29 +
    1.30 +/***********************************************************************
    1.31 +*  NAME
    1.32 +*
    1.33 +*  glp_get_prob_name - retrieve problem name
    1.34 +*
    1.35 +*  SYNOPSIS
    1.36 +*
    1.37 +*  const char *glp_get_prob_name(glp_prob *lp);
    1.38 +*
    1.39 +*  RETURNS
    1.40 +*
    1.41 +*  The routine glp_get_prob_name returns a pointer to an internal
    1.42 +*  buffer, which contains symbolic name of the problem. However, if the
    1.43 +*  problem has no assigned name, the routine returns NULL. */
    1.44 +
    1.45 +const char *glp_get_prob_name(glp_prob *lp)
    1.46 +{     char *name;
    1.47 +      name = lp->name;
    1.48 +      return name;
    1.49 +}
    1.50 +
    1.51 +/***********************************************************************
    1.52 +*  NAME
    1.53 +*
    1.54 +*  glp_get_obj_name - retrieve objective function name
    1.55 +*
    1.56 +*  SYNOPSIS
    1.57 +*
    1.58 +*  const char *glp_get_obj_name(glp_prob *lp);
    1.59 +*
    1.60 +*  RETURNS
    1.61 +*
    1.62 +*  The routine glp_get_obj_name returns a pointer to an internal
    1.63 +*  buffer, which contains a symbolic name of the objective function.
    1.64 +*  However, if the objective function has no assigned name, the routine
    1.65 +*  returns NULL. */
    1.66 +
    1.67 +const char *glp_get_obj_name(glp_prob *lp)
    1.68 +{     char *name;
    1.69 +      name = lp->obj;
    1.70 +      return name;
    1.71 +}
    1.72 +
    1.73 +/***********************************************************************
    1.74 +*  NAME
    1.75 +*
    1.76 +*  glp_get_obj_dir - retrieve optimization direction flag
    1.77 +*
    1.78 +*  SYNOPSIS
    1.79 +*
    1.80 +*  int glp_get_obj_dir(glp_prob *lp);
    1.81 +*
    1.82 +*  RETURNS
    1.83 +*
    1.84 +*  The routine glp_get_obj_dir returns the optimization direction flag
    1.85 +*  (i.e. "sense" of the objective function):
    1.86 +*
    1.87 +*  GLP_MIN - minimization;
    1.88 +*  GLP_MAX - maximization. */
    1.89 +
    1.90 +int glp_get_obj_dir(glp_prob *lp)
    1.91 +{     int dir = lp->dir;
    1.92 +      return dir;
    1.93 +}
    1.94 +
    1.95 +/***********************************************************************
    1.96 +*  NAME
    1.97 +*
    1.98 +*  glp_get_num_rows - retrieve number of rows
    1.99 +*
   1.100 +*  SYNOPSIS
   1.101 +*
   1.102 +*  int glp_get_num_rows(glp_prob *lp);
   1.103 +*
   1.104 +*  RETURNS
   1.105 +*
   1.106 +*  The routine glp_get_num_rows returns the current number of rows in
   1.107 +*  the specified problem object. */
   1.108 +
   1.109 +int glp_get_num_rows(glp_prob *lp)
   1.110 +{     int m = lp->m;
   1.111 +      return m;
   1.112 +}
   1.113 +
   1.114 +/***********************************************************************
   1.115 +*  NAME
   1.116 +*
   1.117 +*  glp_get_num_cols - retrieve number of columns
   1.118 +*
   1.119 +*  SYNOPSIS
   1.120 +*
   1.121 +*  int glp_get_num_cols(glp_prob *lp);
   1.122 +*
   1.123 +*  RETURNS
   1.124 +*
   1.125 +*  The routine glp_get_num_cols returns the current number of columns
   1.126 +*  in the specified problem object. */
   1.127 +
   1.128 +int glp_get_num_cols(glp_prob *lp)
   1.129 +{     int n = lp->n;
   1.130 +      return n;
   1.131 +}
   1.132 +
   1.133 +/***********************************************************************
   1.134 +*  NAME
   1.135 +*
   1.136 +*  glp_get_row_name - retrieve row name
   1.137 +*
   1.138 +*  SYNOPSIS
   1.139 +*
   1.140 +*  const char *glp_get_row_name(glp_prob *lp, int i);
   1.141 +*
   1.142 +*  RETURNS
   1.143 +*
   1.144 +*  The routine glp_get_row_name returns a pointer to an internal
   1.145 +*  buffer, which contains symbolic name of i-th row. However, if i-th
   1.146 +*  row has no assigned name, the routine returns NULL. */
   1.147 +
   1.148 +const char *glp_get_row_name(glp_prob *lp, int i)
   1.149 +{     char *name;
   1.150 +      if (!(1 <= i && i <= lp->m))
   1.151 +         xerror("glp_get_row_name: i = %d; row number out of range\n",
   1.152 +            i);
   1.153 +      name = lp->row[i]->name;
   1.154 +      return name;
   1.155 +}
   1.156 +
   1.157 +/***********************************************************************
   1.158 +*  NAME
   1.159 +*
   1.160 +*  glp_get_col_name - retrieve column name
   1.161 +*
   1.162 +*  SYNOPSIS
   1.163 +*
   1.164 +*  const char *glp_get_col_name(glp_prob *lp, int j);
   1.165 +*
   1.166 +*  RETURNS
   1.167 +*
   1.168 +*  The routine glp_get_col_name returns a pointer to an internal
   1.169 +*  buffer, which contains symbolic name of j-th column. However, if j-th
   1.170 +*  column has no assigned name, the routine returns NULL. */
   1.171 +
   1.172 +const char *glp_get_col_name(glp_prob *lp, int j)
   1.173 +{     char *name;
   1.174 +      if (!(1 <= j && j <= lp->n))
   1.175 +         xerror("glp_get_col_name: j = %d; column number out of range\n"
   1.176 +            , j);
   1.177 +      name = lp->col[j]->name;
   1.178 +      return name;
   1.179 +}
   1.180 +
   1.181 +/***********************************************************************
   1.182 +*  NAME
   1.183 +*
   1.184 +*  glp_get_row_type - retrieve row type
   1.185 +*
   1.186 +*  SYNOPSIS
   1.187 +*
   1.188 +*  int glp_get_row_type(glp_prob *lp, int i);
   1.189 +*
   1.190 +*  RETURNS
   1.191 +*
   1.192 +*  The routine glp_get_row_type returns the type of i-th row, i.e. the
   1.193 +*  type of corresponding auxiliary variable, as follows:
   1.194 +*
   1.195 +*  GLP_FR - free (unbounded) variable;
   1.196 +*  GLP_LO - variable with lower bound;
   1.197 +*  GLP_UP - variable with upper bound;
   1.198 +*  GLP_DB - double-bounded variable;
   1.199 +*  GLP_FX - fixed variable. */
   1.200 +
   1.201 +int glp_get_row_type(glp_prob *lp, int i)
   1.202 +{     if (!(1 <= i && i <= lp->m))
   1.203 +         xerror("glp_get_row_type: i = %d; row number out of range\n",
   1.204 +            i);
   1.205 +      return lp->row[i]->type;
   1.206 +}
   1.207 +
   1.208 +/***********************************************************************
   1.209 +*  NAME
   1.210 +*
   1.211 +*  glp_get_row_lb - retrieve row lower bound
   1.212 +*
   1.213 +*  SYNOPSIS
   1.214 +*
   1.215 +*  double glp_get_row_lb(glp_prob *lp, int i);
   1.216 +*
   1.217 +*  RETURNS
   1.218 +*
   1.219 +*  The routine glp_get_row_lb returns the lower bound of i-th row, i.e.
   1.220 +*  the lower bound of corresponding auxiliary variable. However, if the
   1.221 +*  row has no lower bound, the routine returns -DBL_MAX. */
   1.222 +
   1.223 +double glp_get_row_lb(glp_prob *lp, int i)
   1.224 +{     double lb;
   1.225 +      if (!(1 <= i && i <= lp->m))
   1.226 +         xerror("glp_get_row_lb: i = %d; row number out of range\n", i);
   1.227 +      switch (lp->row[i]->type)
   1.228 +      {  case GLP_FR:
   1.229 +         case GLP_UP:
   1.230 +            lb = -DBL_MAX; break;
   1.231 +         case GLP_LO:
   1.232 +         case GLP_DB:
   1.233 +         case GLP_FX:
   1.234 +            lb = lp->row[i]->lb; break;
   1.235 +         default:
   1.236 +            xassert(lp != lp);
   1.237 +      }
   1.238 +      return lb;
   1.239 +}
   1.240 +
   1.241 +/***********************************************************************
   1.242 +*  NAME
   1.243 +*
   1.244 +*  glp_get_row_ub - retrieve row upper bound
   1.245 +*
   1.246 +*  SYNOPSIS
   1.247 +*
   1.248 +*  double glp_get_row_ub(glp_prob *lp, int i);
   1.249 +*
   1.250 +*  RETURNS
   1.251 +*
   1.252 +*  The routine glp_get_row_ub returns the upper bound of i-th row, i.e.
   1.253 +*  the upper bound of corresponding auxiliary variable. However, if the
   1.254 +*  row has no upper bound, the routine returns +DBL_MAX. */
   1.255 +
   1.256 +double glp_get_row_ub(glp_prob *lp, int i)
   1.257 +{     double ub;
   1.258 +      if (!(1 <= i && i <= lp->m))
   1.259 +         xerror("glp_get_row_ub: i = %d; row number out of range\n", i);
   1.260 +      switch (lp->row[i]->type)
   1.261 +      {  case GLP_FR:
   1.262 +         case GLP_LO:
   1.263 +            ub = +DBL_MAX; break;
   1.264 +         case GLP_UP:
   1.265 +         case GLP_DB:
   1.266 +         case GLP_FX:
   1.267 +            ub = lp->row[i]->ub; break;
   1.268 +         default:
   1.269 +            xassert(lp != lp);
   1.270 +      }
   1.271 +      return ub;
   1.272 +}
   1.273 +
   1.274 +/***********************************************************************
   1.275 +*  NAME
   1.276 +*
   1.277 +*  glp_get_col_type - retrieve column type
   1.278 +*
   1.279 +*  SYNOPSIS
   1.280 +*
   1.281 +*  int glp_get_col_type(glp_prob *lp, int j);
   1.282 +*
   1.283 +*  RETURNS
   1.284 +*
   1.285 +*  The routine glp_get_col_type returns the type of j-th column, i.e.
   1.286 +*  the type of corresponding structural variable, as follows:
   1.287 +*
   1.288 +*  GLP_FR - free (unbounded) variable;
   1.289 +*  GLP_LO - variable with lower bound;
   1.290 +*  GLP_UP - variable with upper bound;
   1.291 +*  GLP_DB - double-bounded variable;
   1.292 +*  GLP_FX - fixed variable. */
   1.293 +
   1.294 +int glp_get_col_type(glp_prob *lp, int j)
   1.295 +{     if (!(1 <= j && j <= lp->n))
   1.296 +         xerror("glp_get_col_type: j = %d; column number out of range\n"
   1.297 +            , j);
   1.298 +      return lp->col[j]->type;
   1.299 +}
   1.300 +
   1.301 +/***********************************************************************
   1.302 +*  NAME
   1.303 +*
   1.304 +*  glp_get_col_lb - retrieve column lower bound
   1.305 +*
   1.306 +*  SYNOPSIS
   1.307 +*
   1.308 +*  double glp_get_col_lb(glp_prob *lp, int j);
   1.309 +*
   1.310 +*  RETURNS
   1.311 +*
   1.312 +*  The routine glp_get_col_lb returns the lower bound of j-th column,
   1.313 +*  i.e. the lower bound of corresponding structural variable. However,
   1.314 +*  if the column has no lower bound, the routine returns -DBL_MAX. */
   1.315 +
   1.316 +double glp_get_col_lb(glp_prob *lp, int j)
   1.317 +{     double lb;
   1.318 +      if (!(1 <= j && j <= lp->n))
   1.319 +         xerror("glp_get_col_lb: j = %d; column number out of range\n",
   1.320 +            j);
   1.321 +      switch (lp->col[j]->type)
   1.322 +      {  case GLP_FR:
   1.323 +         case GLP_UP:
   1.324 +            lb = -DBL_MAX; break;
   1.325 +         case GLP_LO:
   1.326 +         case GLP_DB:
   1.327 +         case GLP_FX:
   1.328 +            lb = lp->col[j]->lb; break;
   1.329 +         default:
   1.330 +            xassert(lp != lp);
   1.331 +      }
   1.332 +      return lb;
   1.333 +}
   1.334 +
   1.335 +/***********************************************************************
   1.336 +*  NAME
   1.337 +*
   1.338 +*  glp_get_col_ub - retrieve column upper bound
   1.339 +*
   1.340 +*  SYNOPSIS
   1.341 +*
   1.342 +*  double glp_get_col_ub(glp_prob *lp, int j);
   1.343 +*
   1.344 +*  RETURNS
   1.345 +*
   1.346 +*  The routine glp_get_col_ub returns the upper bound of j-th column,
   1.347 +*  i.e. the upper bound of corresponding structural variable. However,
   1.348 +*  if the column has no upper bound, the routine returns +DBL_MAX. */
   1.349 +
   1.350 +double glp_get_col_ub(glp_prob *lp, int j)
   1.351 +{     double ub;
   1.352 +      if (!(1 <= j && j <= lp->n))
   1.353 +         xerror("glp_get_col_ub: j = %d; column number out of range\n",
   1.354 +            j);
   1.355 +      switch (lp->col[j]->type)
   1.356 +      {  case GLP_FR:
   1.357 +         case GLP_LO:
   1.358 +            ub = +DBL_MAX; break;
   1.359 +         case GLP_UP:
   1.360 +         case GLP_DB:
   1.361 +         case GLP_FX:
   1.362 +            ub = lp->col[j]->ub; break;
   1.363 +         default:
   1.364 +            xassert(lp != lp);
   1.365 +      }
   1.366 +      return ub;
   1.367 +}
   1.368 +
   1.369 +/***********************************************************************
   1.370 +*  NAME
   1.371 +*
   1.372 +*  glp_get_obj_coef - retrieve obj. coefficient or constant term
   1.373 +*
   1.374 +*  SYNOPSIS
   1.375 +*
   1.376 +*  double glp_get_obj_coef(glp_prob *lp, int j);
   1.377 +*
   1.378 +*  RETURNS
   1.379 +*
   1.380 +*  The routine glp_get_obj_coef returns the objective coefficient at
   1.381 +*  j-th structural variable (column) of the specified problem object.
   1.382 +*
   1.383 +*  If the parameter j is zero, the routine returns the constant term
   1.384 +*  ("shift") of the objective function. */
   1.385 +
   1.386 +double glp_get_obj_coef(glp_prob *lp, int j)
   1.387 +{     if (!(0 <= j && j <= lp->n))
   1.388 +         xerror("glp_get_obj_coef: j = %d; column number out of range\n"
   1.389 +            , j);
   1.390 +      return j == 0 ? lp->c0 : lp->col[j]->coef;
   1.391 +}
   1.392 +
   1.393 +/***********************************************************************
   1.394 +*  NAME
   1.395 +*
   1.396 +*  glp_get_num_nz - retrieve number of constraint coefficients
   1.397 +*
   1.398 +*  SYNOPSIS
   1.399 +*
   1.400 +*  int glp_get_num_nz(glp_prob *lp);
   1.401 +*
   1.402 +*  RETURNS
   1.403 +*
   1.404 +*  The routine glp_get_num_nz returns the number of (non-zero) elements
   1.405 +*  in the constraint matrix of the specified problem object. */
   1.406 +
   1.407 +int glp_get_num_nz(glp_prob *lp)
   1.408 +{     int nnz = lp->nnz;
   1.409 +      return nnz;
   1.410 +}
   1.411 +
   1.412 +/***********************************************************************
   1.413 +*  NAME
   1.414 +*
   1.415 +*  glp_get_mat_row - retrieve row of the constraint matrix
   1.416 +*
   1.417 +*  SYNOPSIS
   1.418 +*
   1.419 +*  int glp_get_mat_row(glp_prob *lp, int i, int ind[], double val[]);
   1.420 +*
   1.421 +*  DESCRIPTION
   1.422 +*
   1.423 +*  The routine glp_get_mat_row scans (non-zero) elements of i-th row
   1.424 +*  of the constraint matrix of the specified problem object and stores
   1.425 +*  their column indices and numeric values to locations ind[1], ...,
   1.426 +*  ind[len] and val[1], ..., val[len], respectively, where 0 <= len <= n
   1.427 +*  is the number of elements in i-th row, n is the number of columns.
   1.428 +*
   1.429 +*  The parameter ind and/or val can be specified as NULL, in which case
   1.430 +*  corresponding information is not stored.
   1.431 +*
   1.432 +*  RETURNS
   1.433 +*
   1.434 +*  The routine glp_get_mat_row returns the length len, i.e. the number
   1.435 +*  of (non-zero) elements in i-th row. */
   1.436 +
   1.437 +int glp_get_mat_row(glp_prob *lp, int i, int ind[], double val[])
   1.438 +{     GLPAIJ *aij;
   1.439 +      int len;
   1.440 +      if (!(1 <= i && i <= lp->m))
   1.441 +         xerror("glp_get_mat_row: i = %d; row number out of range\n",
   1.442 +            i);
   1.443 +      len = 0;
   1.444 +      for (aij = lp->row[i]->ptr; aij != NULL; aij = aij->r_next)
   1.445 +      {  len++;
   1.446 +         if (ind != NULL) ind[len] = aij->col->j;
   1.447 +         if (val != NULL) val[len] = aij->val;
   1.448 +      }
   1.449 +      xassert(len <= lp->n);
   1.450 +      return len;
   1.451 +}
   1.452 +
   1.453 +/***********************************************************************
   1.454 +*  NAME
   1.455 +*
   1.456 +*  glp_get_mat_col - retrieve column of the constraint matrix
   1.457 +*
   1.458 +*  SYNOPSIS
   1.459 +*
   1.460 +*  int glp_get_mat_col(glp_prob *lp, int j, int ind[], double val[]);
   1.461 +*
   1.462 +*  DESCRIPTION
   1.463 +*
   1.464 +*  The routine glp_get_mat_col scans (non-zero) elements of j-th column
   1.465 +*  of the constraint matrix of the specified problem object and stores
   1.466 +*  their row indices and numeric values to locations ind[1], ...,
   1.467 +*  ind[len] and val[1], ..., val[len], respectively, where 0 <= len <= m
   1.468 +*  is the number of elements in j-th column, m is the number of rows.
   1.469 +*
   1.470 +*  The parameter ind or/and val can be specified as NULL, in which case
   1.471 +*  corresponding information is not stored.
   1.472 +*
   1.473 +*  RETURNS
   1.474 +*
   1.475 +*  The routine glp_get_mat_col returns the length len, i.e. the number
   1.476 +*  of (non-zero) elements in j-th column. */
   1.477 +
   1.478 +int glp_get_mat_col(glp_prob *lp, int j, int ind[], double val[])
   1.479 +{     GLPAIJ *aij;
   1.480 +      int len;
   1.481 +      if (!(1 <= j && j <= lp->n))
   1.482 +         xerror("glp_get_mat_col: j = %d; column number out of range\n",
   1.483 +            j);
   1.484 +      len = 0;
   1.485 +      for (aij = lp->col[j]->ptr; aij != NULL; aij = aij->c_next)
   1.486 +      {  len++;
   1.487 +         if (ind != NULL) ind[len] = aij->row->i;
   1.488 +         if (val != NULL) val[len] = aij->val;
   1.489 +      }
   1.490 +      xassert(len <= lp->m);
   1.491 +      return len;
   1.492 +}
   1.493 +
   1.494 +/* eof */