glpapi02.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /* glpapi02.c (problem retrieving routines) */
  2. /***********************************************************************
  3. * This code is part of GLPK (GNU Linear Programming Kit).
  4. *
  5. * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
  6. * 2009, 2010 Andrew Makhorin, Department for Applied Informatics,
  7. * Moscow Aviation Institute, Moscow, Russia. All rights reserved.
  8. * E-mail: <mao@gnu.org>.
  9. *
  10. * GLPK is free software: you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * GLPK is distributed in the hope that it will be useful, but WITHOUT
  16. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  17. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  18. * License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with GLPK. If not, see <http://www.gnu.org/licenses/>.
  22. ***********************************************************************/
  23. #include "glpapi.h"
  24. /***********************************************************************
  25. * NAME
  26. *
  27. * glp_get_prob_name - retrieve problem name
  28. *
  29. * SYNOPSIS
  30. *
  31. * const char *glp_get_prob_name(glp_prob *lp);
  32. *
  33. * RETURNS
  34. *
  35. * The routine glp_get_prob_name returns a pointer to an internal
  36. * buffer, which contains symbolic name of the problem. However, if the
  37. * problem has no assigned name, the routine returns NULL. */
  38. const char *glp_get_prob_name(glp_prob *lp)
  39. { char *name;
  40. name = lp->name;
  41. return name;
  42. }
  43. /***********************************************************************
  44. * NAME
  45. *
  46. * glp_get_obj_name - retrieve objective function name
  47. *
  48. * SYNOPSIS
  49. *
  50. * const char *glp_get_obj_name(glp_prob *lp);
  51. *
  52. * RETURNS
  53. *
  54. * The routine glp_get_obj_name returns a pointer to an internal
  55. * buffer, which contains a symbolic name of the objective function.
  56. * However, if the objective function has no assigned name, the routine
  57. * returns NULL. */
  58. const char *glp_get_obj_name(glp_prob *lp)
  59. { char *name;
  60. name = lp->obj;
  61. return name;
  62. }
  63. /***********************************************************************
  64. * NAME
  65. *
  66. * glp_get_obj_dir - retrieve optimization direction flag
  67. *
  68. * SYNOPSIS
  69. *
  70. * int glp_get_obj_dir(glp_prob *lp);
  71. *
  72. * RETURNS
  73. *
  74. * The routine glp_get_obj_dir returns the optimization direction flag
  75. * (i.e. "sense" of the objective function):
  76. *
  77. * GLP_MIN - minimization;
  78. * GLP_MAX - maximization. */
  79. int glp_get_obj_dir(glp_prob *lp)
  80. { int dir = lp->dir;
  81. return dir;
  82. }
  83. /***********************************************************************
  84. * NAME
  85. *
  86. * glp_get_num_rows - retrieve number of rows
  87. *
  88. * SYNOPSIS
  89. *
  90. * int glp_get_num_rows(glp_prob *lp);
  91. *
  92. * RETURNS
  93. *
  94. * The routine glp_get_num_rows returns the current number of rows in
  95. * the specified problem object. */
  96. int glp_get_num_rows(glp_prob *lp)
  97. { int m = lp->m;
  98. return m;
  99. }
  100. /***********************************************************************
  101. * NAME
  102. *
  103. * glp_get_num_cols - retrieve number of columns
  104. *
  105. * SYNOPSIS
  106. *
  107. * int glp_get_num_cols(glp_prob *lp);
  108. *
  109. * RETURNS
  110. *
  111. * The routine glp_get_num_cols returns the current number of columns
  112. * in the specified problem object. */
  113. int glp_get_num_cols(glp_prob *lp)
  114. { int n = lp->n;
  115. return n;
  116. }
  117. /***********************************************************************
  118. * NAME
  119. *
  120. * glp_get_row_name - retrieve row name
  121. *
  122. * SYNOPSIS
  123. *
  124. * const char *glp_get_row_name(glp_prob *lp, int i);
  125. *
  126. * RETURNS
  127. *
  128. * The routine glp_get_row_name returns a pointer to an internal
  129. * buffer, which contains symbolic name of i-th row. However, if i-th
  130. * row has no assigned name, the routine returns NULL. */
  131. const char *glp_get_row_name(glp_prob *lp, int i)
  132. { char *name;
  133. if (!(1 <= i && i <= lp->m))
  134. xerror("glp_get_row_name: i = %d; row number out of range\n",
  135. i);
  136. name = lp->row[i]->name;
  137. return name;
  138. }
  139. /***********************************************************************
  140. * NAME
  141. *
  142. * glp_get_col_name - retrieve column name
  143. *
  144. * SYNOPSIS
  145. *
  146. * const char *glp_get_col_name(glp_prob *lp, int j);
  147. *
  148. * RETURNS
  149. *
  150. * The routine glp_get_col_name returns a pointer to an internal
  151. * buffer, which contains symbolic name of j-th column. However, if j-th
  152. * column has no assigned name, the routine returns NULL. */
  153. const char *glp_get_col_name(glp_prob *lp, int j)
  154. { char *name;
  155. if (!(1 <= j && j <= lp->n))
  156. xerror("glp_get_col_name: j = %d; column number out of range\n"
  157. , j);
  158. name = lp->col[j]->name;
  159. return name;
  160. }
  161. /***********************************************************************
  162. * NAME
  163. *
  164. * glp_get_row_type - retrieve row type
  165. *
  166. * SYNOPSIS
  167. *
  168. * int glp_get_row_type(glp_prob *lp, int i);
  169. *
  170. * RETURNS
  171. *
  172. * The routine glp_get_row_type returns the type of i-th row, i.e. the
  173. * type of corresponding auxiliary variable, as follows:
  174. *
  175. * GLP_FR - free (unbounded) variable;
  176. * GLP_LO - variable with lower bound;
  177. * GLP_UP - variable with upper bound;
  178. * GLP_DB - double-bounded variable;
  179. * GLP_FX - fixed variable. */
  180. int glp_get_row_type(glp_prob *lp, int i)
  181. { if (!(1 <= i && i <= lp->m))
  182. xerror("glp_get_row_type: i = %d; row number out of range\n",
  183. i);
  184. return lp->row[i]->type;
  185. }
  186. /***********************************************************************
  187. * NAME
  188. *
  189. * glp_get_row_lb - retrieve row lower bound
  190. *
  191. * SYNOPSIS
  192. *
  193. * double glp_get_row_lb(glp_prob *lp, int i);
  194. *
  195. * RETURNS
  196. *
  197. * The routine glp_get_row_lb returns the lower bound of i-th row, i.e.
  198. * the lower bound of corresponding auxiliary variable. However, if the
  199. * row has no lower bound, the routine returns -DBL_MAX. */
  200. double glp_get_row_lb(glp_prob *lp, int i)
  201. { double lb;
  202. if (!(1 <= i && i <= lp->m))
  203. xerror("glp_get_row_lb: i = %d; row number out of range\n", i);
  204. switch (lp->row[i]->type)
  205. { case GLP_FR:
  206. case GLP_UP:
  207. lb = -DBL_MAX; break;
  208. case GLP_LO:
  209. case GLP_DB:
  210. case GLP_FX:
  211. lb = lp->row[i]->lb; break;
  212. default:
  213. xassert(lp != lp);
  214. }
  215. return lb;
  216. }
  217. /***********************************************************************
  218. * NAME
  219. *
  220. * glp_get_row_ub - retrieve row upper bound
  221. *
  222. * SYNOPSIS
  223. *
  224. * double glp_get_row_ub(glp_prob *lp, int i);
  225. *
  226. * RETURNS
  227. *
  228. * The routine glp_get_row_ub returns the upper bound of i-th row, i.e.
  229. * the upper bound of corresponding auxiliary variable. However, if the
  230. * row has no upper bound, the routine returns +DBL_MAX. */
  231. double glp_get_row_ub(glp_prob *lp, int i)
  232. { double ub;
  233. if (!(1 <= i && i <= lp->m))
  234. xerror("glp_get_row_ub: i = %d; row number out of range\n", i);
  235. switch (lp->row[i]->type)
  236. { case GLP_FR:
  237. case GLP_LO:
  238. ub = +DBL_MAX; break;
  239. case GLP_UP:
  240. case GLP_DB:
  241. case GLP_FX:
  242. ub = lp->row[i]->ub; break;
  243. default:
  244. xassert(lp != lp);
  245. }
  246. return ub;
  247. }
  248. /***********************************************************************
  249. * NAME
  250. *
  251. * glp_get_col_type - retrieve column type
  252. *
  253. * SYNOPSIS
  254. *
  255. * int glp_get_col_type(glp_prob *lp, int j);
  256. *
  257. * RETURNS
  258. *
  259. * The routine glp_get_col_type returns the type of j-th column, i.e.
  260. * the type of corresponding structural variable, as follows:
  261. *
  262. * GLP_FR - free (unbounded) variable;
  263. * GLP_LO - variable with lower bound;
  264. * GLP_UP - variable with upper bound;
  265. * GLP_DB - double-bounded variable;
  266. * GLP_FX - fixed variable. */
  267. int glp_get_col_type(glp_prob *lp, int j)
  268. { if (!(1 <= j && j <= lp->n))
  269. xerror("glp_get_col_type: j = %d; column number out of range\n"
  270. , j);
  271. return lp->col[j]->type;
  272. }
  273. /***********************************************************************
  274. * NAME
  275. *
  276. * glp_get_col_lb - retrieve column lower bound
  277. *
  278. * SYNOPSIS
  279. *
  280. * double glp_get_col_lb(glp_prob *lp, int j);
  281. *
  282. * RETURNS
  283. *
  284. * The routine glp_get_col_lb returns the lower bound of j-th column,
  285. * i.e. the lower bound of corresponding structural variable. However,
  286. * if the column has no lower bound, the routine returns -DBL_MAX. */
  287. double glp_get_col_lb(glp_prob *lp, int j)
  288. { double lb;
  289. if (!(1 <= j && j <= lp->n))
  290. xerror("glp_get_col_lb: j = %d; column number out of range\n",
  291. j);
  292. switch (lp->col[j]->type)
  293. { case GLP_FR:
  294. case GLP_UP:
  295. lb = -DBL_MAX; break;
  296. case GLP_LO:
  297. case GLP_DB:
  298. case GLP_FX:
  299. lb = lp->col[j]->lb; break;
  300. default:
  301. xassert(lp != lp);
  302. }
  303. return lb;
  304. }
  305. /***********************************************************************
  306. * NAME
  307. *
  308. * glp_get_col_ub - retrieve column upper bound
  309. *
  310. * SYNOPSIS
  311. *
  312. * double glp_get_col_ub(glp_prob *lp, int j);
  313. *
  314. * RETURNS
  315. *
  316. * The routine glp_get_col_ub returns the upper bound of j-th column,
  317. * i.e. the upper bound of corresponding structural variable. However,
  318. * if the column has no upper bound, the routine returns +DBL_MAX. */
  319. double glp_get_col_ub(glp_prob *lp, int j)
  320. { double ub;
  321. if (!(1 <= j && j <= lp->n))
  322. xerror("glp_get_col_ub: j = %d; column number out of range\n",
  323. j);
  324. switch (lp->col[j]->type)
  325. { case GLP_FR:
  326. case GLP_LO:
  327. ub = +DBL_MAX; break;
  328. case GLP_UP:
  329. case GLP_DB:
  330. case GLP_FX:
  331. ub = lp->col[j]->ub; break;
  332. default:
  333. xassert(lp != lp);
  334. }
  335. return ub;
  336. }
  337. /***********************************************************************
  338. * NAME
  339. *
  340. * glp_get_obj_coef - retrieve obj. coefficient or constant term
  341. *
  342. * SYNOPSIS
  343. *
  344. * double glp_get_obj_coef(glp_prob *lp, int j);
  345. *
  346. * RETURNS
  347. *
  348. * The routine glp_get_obj_coef returns the objective coefficient at
  349. * j-th structural variable (column) of the specified problem object.
  350. *
  351. * If the parameter j is zero, the routine returns the constant term
  352. * ("shift") of the objective function. */
  353. double glp_get_obj_coef(glp_prob *lp, int j)
  354. { if (!(0 <= j && j <= lp->n))
  355. xerror("glp_get_obj_coef: j = %d; column number out of range\n"
  356. , j);
  357. return j == 0 ? lp->c0 : lp->col[j]->coef;
  358. }
  359. /***********************************************************************
  360. * NAME
  361. *
  362. * glp_get_num_nz - retrieve number of constraint coefficients
  363. *
  364. * SYNOPSIS
  365. *
  366. * int glp_get_num_nz(glp_prob *lp);
  367. *
  368. * RETURNS
  369. *
  370. * The routine glp_get_num_nz returns the number of (non-zero) elements
  371. * in the constraint matrix of the specified problem object. */
  372. int glp_get_num_nz(glp_prob *lp)
  373. { int nnz = lp->nnz;
  374. return nnz;
  375. }
  376. /***********************************************************************
  377. * NAME
  378. *
  379. * glp_get_mat_row - retrieve row of the constraint matrix
  380. *
  381. * SYNOPSIS
  382. *
  383. * int glp_get_mat_row(glp_prob *lp, int i, int ind[], double val[]);
  384. *
  385. * DESCRIPTION
  386. *
  387. * The routine glp_get_mat_row scans (non-zero) elements of i-th row
  388. * of the constraint matrix of the specified problem object and stores
  389. * their column indices and numeric values to locations ind[1], ...,
  390. * ind[len] and val[1], ..., val[len], respectively, where 0 <= len <= n
  391. * is the number of elements in i-th row, n is the number of columns.
  392. *
  393. * The parameter ind and/or val can be specified as NULL, in which case
  394. * corresponding information is not stored.
  395. *
  396. * RETURNS
  397. *
  398. * The routine glp_get_mat_row returns the length len, i.e. the number
  399. * of (non-zero) elements in i-th row. */
  400. int glp_get_mat_row(glp_prob *lp, int i, int ind[], double val[])
  401. { GLPAIJ *aij;
  402. int len;
  403. if (!(1 <= i && i <= lp->m))
  404. xerror("glp_get_mat_row: i = %d; row number out of range\n",
  405. i);
  406. len = 0;
  407. for (aij = lp->row[i]->ptr; aij != NULL; aij = aij->r_next)
  408. { len++;
  409. if (ind != NULL) ind[len] = aij->col->j;
  410. if (val != NULL) val[len] = aij->val;
  411. }
  412. xassert(len <= lp->n);
  413. return len;
  414. }
  415. /***********************************************************************
  416. * NAME
  417. *
  418. * glp_get_mat_col - retrieve column of the constraint matrix
  419. *
  420. * SYNOPSIS
  421. *
  422. * int glp_get_mat_col(glp_prob *lp, int j, int ind[], double val[]);
  423. *
  424. * DESCRIPTION
  425. *
  426. * The routine glp_get_mat_col scans (non-zero) elements of j-th column
  427. * of the constraint matrix of the specified problem object and stores
  428. * their row indices and numeric values to locations ind[1], ...,
  429. * ind[len] and val[1], ..., val[len], respectively, where 0 <= len <= m
  430. * is the number of elements in j-th column, m is the number of rows.
  431. *
  432. * The parameter ind or/and val can be specified as NULL, in which case
  433. * corresponding information is not stored.
  434. *
  435. * RETURNS
  436. *
  437. * The routine glp_get_mat_col returns the length len, i.e. the number
  438. * of (non-zero) elements in j-th column. */
  439. int glp_get_mat_col(glp_prob *lp, int j, int ind[], double val[])
  440. { GLPAIJ *aij;
  441. int len;
  442. if (!(1 <= j && j <= lp->n))
  443. xerror("glp_get_mat_col: j = %d; column number out of range\n",
  444. j);
  445. len = 0;
  446. for (aij = lp->col[j]->ptr; aij != NULL; aij = aij->c_next)
  447. { len++;
  448. if (ind != NULL) ind[len] = aij->row->i;
  449. if (val != NULL) val[len] = aij->val;
  450. }
  451. xassert(len <= lp->m);
  452. return len;
  453. }
  454. /* eof */