alpar@1: /* glpapi02.c (problem retrieving routines) */ alpar@1: alpar@1: /*********************************************************************** alpar@1: * This code is part of GLPK (GNU Linear Programming Kit). alpar@1: * alpar@1: * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, alpar@1: * 2009, 2010 Andrew Makhorin, Department for Applied Informatics, alpar@1: * Moscow Aviation Institute, Moscow, Russia. All rights reserved. alpar@1: * E-mail: . alpar@1: * alpar@1: * GLPK is free software: you can redistribute it and/or modify it alpar@1: * under the terms of the GNU General Public License as published by alpar@1: * the Free Software Foundation, either version 3 of the License, or alpar@1: * (at your option) any later version. alpar@1: * alpar@1: * GLPK is distributed in the hope that it will be useful, but WITHOUT alpar@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY alpar@1: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public alpar@1: * License for more details. alpar@1: * alpar@1: * You should have received a copy of the GNU General Public License alpar@1: * along with GLPK. If not, see . alpar@1: ***********************************************************************/ alpar@1: alpar@1: #include "glpapi.h" alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * glp_get_prob_name - retrieve problem name alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * const char *glp_get_prob_name(glp_prob *lp); alpar@1: * alpar@1: * RETURNS alpar@1: * alpar@1: * The routine glp_get_prob_name returns a pointer to an internal alpar@1: * buffer, which contains symbolic name of the problem. However, if the alpar@1: * problem has no assigned name, the routine returns NULL. */ alpar@1: alpar@1: const char *glp_get_prob_name(glp_prob *lp) alpar@1: { char *name; alpar@1: name = lp->name; alpar@1: return name; alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * glp_get_obj_name - retrieve objective function name alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * const char *glp_get_obj_name(glp_prob *lp); alpar@1: * alpar@1: * RETURNS alpar@1: * alpar@1: * The routine glp_get_obj_name returns a pointer to an internal alpar@1: * buffer, which contains a symbolic name of the objective function. alpar@1: * However, if the objective function has no assigned name, the routine alpar@1: * returns NULL. */ alpar@1: alpar@1: const char *glp_get_obj_name(glp_prob *lp) alpar@1: { char *name; alpar@1: name = lp->obj; alpar@1: return name; alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * glp_get_obj_dir - retrieve optimization direction flag alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * int glp_get_obj_dir(glp_prob *lp); alpar@1: * alpar@1: * RETURNS alpar@1: * alpar@1: * The routine glp_get_obj_dir returns the optimization direction flag alpar@1: * (i.e. "sense" of the objective function): alpar@1: * alpar@1: * GLP_MIN - minimization; alpar@1: * GLP_MAX - maximization. */ alpar@1: alpar@1: int glp_get_obj_dir(glp_prob *lp) alpar@1: { int dir = lp->dir; alpar@1: return dir; alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * glp_get_num_rows - retrieve number of rows alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * int glp_get_num_rows(glp_prob *lp); alpar@1: * alpar@1: * RETURNS alpar@1: * alpar@1: * The routine glp_get_num_rows returns the current number of rows in alpar@1: * the specified problem object. */ alpar@1: alpar@1: int glp_get_num_rows(glp_prob *lp) alpar@1: { int m = lp->m; alpar@1: return m; alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * glp_get_num_cols - retrieve number of columns alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * int glp_get_num_cols(glp_prob *lp); alpar@1: * alpar@1: * RETURNS alpar@1: * alpar@1: * The routine glp_get_num_cols returns the current number of columns alpar@1: * in the specified problem object. */ alpar@1: alpar@1: int glp_get_num_cols(glp_prob *lp) alpar@1: { int n = lp->n; alpar@1: return n; alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * glp_get_row_name - retrieve row name alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * const char *glp_get_row_name(glp_prob *lp, int i); alpar@1: * alpar@1: * RETURNS alpar@1: * alpar@1: * The routine glp_get_row_name returns a pointer to an internal alpar@1: * buffer, which contains symbolic name of i-th row. However, if i-th alpar@1: * row has no assigned name, the routine returns NULL. */ alpar@1: alpar@1: const char *glp_get_row_name(glp_prob *lp, int i) alpar@1: { char *name; alpar@1: if (!(1 <= i && i <= lp->m)) alpar@1: xerror("glp_get_row_name: i = %d; row number out of range\n", alpar@1: i); alpar@1: name = lp->row[i]->name; alpar@1: return name; alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * glp_get_col_name - retrieve column name alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * const char *glp_get_col_name(glp_prob *lp, int j); alpar@1: * alpar@1: * RETURNS alpar@1: * alpar@1: * The routine glp_get_col_name returns a pointer to an internal alpar@1: * buffer, which contains symbolic name of j-th column. However, if j-th alpar@1: * column has no assigned name, the routine returns NULL. */ alpar@1: alpar@1: const char *glp_get_col_name(glp_prob *lp, int j) alpar@1: { char *name; alpar@1: if (!(1 <= j && j <= lp->n)) alpar@1: xerror("glp_get_col_name: j = %d; column number out of range\n" alpar@1: , j); alpar@1: name = lp->col[j]->name; alpar@1: return name; alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * glp_get_row_type - retrieve row type alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * int glp_get_row_type(glp_prob *lp, int i); alpar@1: * alpar@1: * RETURNS alpar@1: * alpar@1: * The routine glp_get_row_type returns the type of i-th row, i.e. the alpar@1: * type of corresponding auxiliary variable, as follows: alpar@1: * alpar@1: * GLP_FR - free (unbounded) variable; alpar@1: * GLP_LO - variable with lower bound; alpar@1: * GLP_UP - variable with upper bound; alpar@1: * GLP_DB - double-bounded variable; alpar@1: * GLP_FX - fixed variable. */ alpar@1: alpar@1: int glp_get_row_type(glp_prob *lp, int i) alpar@1: { if (!(1 <= i && i <= lp->m)) alpar@1: xerror("glp_get_row_type: i = %d; row number out of range\n", alpar@1: i); alpar@1: return lp->row[i]->type; alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * glp_get_row_lb - retrieve row lower bound alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * double glp_get_row_lb(glp_prob *lp, int i); alpar@1: * alpar@1: * RETURNS alpar@1: * alpar@1: * The routine glp_get_row_lb returns the lower bound of i-th row, i.e. alpar@1: * the lower bound of corresponding auxiliary variable. However, if the alpar@1: * row has no lower bound, the routine returns -DBL_MAX. */ alpar@1: alpar@1: double glp_get_row_lb(glp_prob *lp, int i) alpar@1: { double lb; alpar@1: if (!(1 <= i && i <= lp->m)) alpar@1: xerror("glp_get_row_lb: i = %d; row number out of range\n", i); alpar@1: switch (lp->row[i]->type) alpar@1: { case GLP_FR: alpar@1: case GLP_UP: alpar@1: lb = -DBL_MAX; break; alpar@1: case GLP_LO: alpar@1: case GLP_DB: alpar@1: case GLP_FX: alpar@1: lb = lp->row[i]->lb; break; alpar@1: default: alpar@1: xassert(lp != lp); alpar@1: } alpar@1: return lb; alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * glp_get_row_ub - retrieve row upper bound alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * double glp_get_row_ub(glp_prob *lp, int i); alpar@1: * alpar@1: * RETURNS alpar@1: * alpar@1: * The routine glp_get_row_ub returns the upper bound of i-th row, i.e. alpar@1: * the upper bound of corresponding auxiliary variable. However, if the alpar@1: * row has no upper bound, the routine returns +DBL_MAX. */ alpar@1: alpar@1: double glp_get_row_ub(glp_prob *lp, int i) alpar@1: { double ub; alpar@1: if (!(1 <= i && i <= lp->m)) alpar@1: xerror("glp_get_row_ub: i = %d; row number out of range\n", i); alpar@1: switch (lp->row[i]->type) alpar@1: { case GLP_FR: alpar@1: case GLP_LO: alpar@1: ub = +DBL_MAX; break; alpar@1: case GLP_UP: alpar@1: case GLP_DB: alpar@1: case GLP_FX: alpar@1: ub = lp->row[i]->ub; break; alpar@1: default: alpar@1: xassert(lp != lp); alpar@1: } alpar@1: return ub; alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * glp_get_col_type - retrieve column type alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * int glp_get_col_type(glp_prob *lp, int j); alpar@1: * alpar@1: * RETURNS alpar@1: * alpar@1: * The routine glp_get_col_type returns the type of j-th column, i.e. alpar@1: * the type of corresponding structural variable, as follows: alpar@1: * alpar@1: * GLP_FR - free (unbounded) variable; alpar@1: * GLP_LO - variable with lower bound; alpar@1: * GLP_UP - variable with upper bound; alpar@1: * GLP_DB - double-bounded variable; alpar@1: * GLP_FX - fixed variable. */ alpar@1: alpar@1: int glp_get_col_type(glp_prob *lp, int j) alpar@1: { if (!(1 <= j && j <= lp->n)) alpar@1: xerror("glp_get_col_type: j = %d; column number out of range\n" alpar@1: , j); alpar@1: return lp->col[j]->type; alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * glp_get_col_lb - retrieve column lower bound alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * double glp_get_col_lb(glp_prob *lp, int j); alpar@1: * alpar@1: * RETURNS alpar@1: * alpar@1: * The routine glp_get_col_lb returns the lower bound of j-th column, alpar@1: * i.e. the lower bound of corresponding structural variable. However, alpar@1: * if the column has no lower bound, the routine returns -DBL_MAX. */ alpar@1: alpar@1: double glp_get_col_lb(glp_prob *lp, int j) alpar@1: { double lb; alpar@1: if (!(1 <= j && j <= lp->n)) alpar@1: xerror("glp_get_col_lb: j = %d; column number out of range\n", alpar@1: j); alpar@1: switch (lp->col[j]->type) alpar@1: { case GLP_FR: alpar@1: case GLP_UP: alpar@1: lb = -DBL_MAX; break; alpar@1: case GLP_LO: alpar@1: case GLP_DB: alpar@1: case GLP_FX: alpar@1: lb = lp->col[j]->lb; break; alpar@1: default: alpar@1: xassert(lp != lp); alpar@1: } alpar@1: return lb; alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * glp_get_col_ub - retrieve column upper bound alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * double glp_get_col_ub(glp_prob *lp, int j); alpar@1: * alpar@1: * RETURNS alpar@1: * alpar@1: * The routine glp_get_col_ub returns the upper bound of j-th column, alpar@1: * i.e. the upper bound of corresponding structural variable. However, alpar@1: * if the column has no upper bound, the routine returns +DBL_MAX. */ alpar@1: alpar@1: double glp_get_col_ub(glp_prob *lp, int j) alpar@1: { double ub; alpar@1: if (!(1 <= j && j <= lp->n)) alpar@1: xerror("glp_get_col_ub: j = %d; column number out of range\n", alpar@1: j); alpar@1: switch (lp->col[j]->type) alpar@1: { case GLP_FR: alpar@1: case GLP_LO: alpar@1: ub = +DBL_MAX; break; alpar@1: case GLP_UP: alpar@1: case GLP_DB: alpar@1: case GLP_FX: alpar@1: ub = lp->col[j]->ub; break; alpar@1: default: alpar@1: xassert(lp != lp); alpar@1: } alpar@1: return ub; alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * glp_get_obj_coef - retrieve obj. coefficient or constant term alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * double glp_get_obj_coef(glp_prob *lp, int j); alpar@1: * alpar@1: * RETURNS alpar@1: * alpar@1: * The routine glp_get_obj_coef returns the objective coefficient at alpar@1: * j-th structural variable (column) of the specified problem object. alpar@1: * alpar@1: * If the parameter j is zero, the routine returns the constant term alpar@1: * ("shift") of the objective function. */ alpar@1: alpar@1: double glp_get_obj_coef(glp_prob *lp, int j) alpar@1: { if (!(0 <= j && j <= lp->n)) alpar@1: xerror("glp_get_obj_coef: j = %d; column number out of range\n" alpar@1: , j); alpar@1: return j == 0 ? lp->c0 : lp->col[j]->coef; alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * glp_get_num_nz - retrieve number of constraint coefficients alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * int glp_get_num_nz(glp_prob *lp); alpar@1: * alpar@1: * RETURNS alpar@1: * alpar@1: * The routine glp_get_num_nz returns the number of (non-zero) elements alpar@1: * in the constraint matrix of the specified problem object. */ alpar@1: alpar@1: int glp_get_num_nz(glp_prob *lp) alpar@1: { int nnz = lp->nnz; alpar@1: return nnz; alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * glp_get_mat_row - retrieve row of the constraint matrix alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * int glp_get_mat_row(glp_prob *lp, int i, int ind[], double val[]); alpar@1: * alpar@1: * DESCRIPTION alpar@1: * alpar@1: * The routine glp_get_mat_row scans (non-zero) elements of i-th row alpar@1: * of the constraint matrix of the specified problem object and stores alpar@1: * their column indices and numeric values to locations ind[1], ..., alpar@1: * ind[len] and val[1], ..., val[len], respectively, where 0 <= len <= n alpar@1: * is the number of elements in i-th row, n is the number of columns. alpar@1: * alpar@1: * The parameter ind and/or val can be specified as NULL, in which case alpar@1: * corresponding information is not stored. alpar@1: * alpar@1: * RETURNS alpar@1: * alpar@1: * The routine glp_get_mat_row returns the length len, i.e. the number alpar@1: * of (non-zero) elements in i-th row. */ alpar@1: alpar@1: int glp_get_mat_row(glp_prob *lp, int i, int ind[], double val[]) alpar@1: { GLPAIJ *aij; alpar@1: int len; alpar@1: if (!(1 <= i && i <= lp->m)) alpar@1: xerror("glp_get_mat_row: i = %d; row number out of range\n", alpar@1: i); alpar@1: len = 0; alpar@1: for (aij = lp->row[i]->ptr; aij != NULL; aij = aij->r_next) alpar@1: { len++; alpar@1: if (ind != NULL) ind[len] = aij->col->j; alpar@1: if (val != NULL) val[len] = aij->val; alpar@1: } alpar@1: xassert(len <= lp->n); alpar@1: return len; alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * glp_get_mat_col - retrieve column of the constraint matrix alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * int glp_get_mat_col(glp_prob *lp, int j, int ind[], double val[]); alpar@1: * alpar@1: * DESCRIPTION alpar@1: * alpar@1: * The routine glp_get_mat_col scans (non-zero) elements of j-th column alpar@1: * of the constraint matrix of the specified problem object and stores alpar@1: * their row indices and numeric values to locations ind[1], ..., alpar@1: * ind[len] and val[1], ..., val[len], respectively, where 0 <= len <= m alpar@1: * is the number of elements in j-th column, m is the number of rows. alpar@1: * alpar@1: * The parameter ind or/and val can be specified as NULL, in which case alpar@1: * corresponding information is not stored. alpar@1: * alpar@1: * RETURNS alpar@1: * alpar@1: * The routine glp_get_mat_col returns the length len, i.e. the number alpar@1: * of (non-zero) elements in j-th column. */ alpar@1: alpar@1: int glp_get_mat_col(glp_prob *lp, int j, int ind[], double val[]) alpar@1: { GLPAIJ *aij; alpar@1: int len; alpar@1: if (!(1 <= j && j <= lp->n)) alpar@1: xerror("glp_get_mat_col: j = %d; column number out of range\n", alpar@1: j); alpar@1: len = 0; alpar@1: for (aij = lp->col[j]->ptr; aij != NULL; aij = aij->c_next) alpar@1: { len++; alpar@1: if (ind != NULL) ind[len] = aij->row->i; alpar@1: if (val != NULL) val[len] = aij->val; alpar@1: } alpar@1: xassert(len <= lp->m); alpar@1: return len; alpar@1: } alpar@1: alpar@1: /* eof */