lemon-project-template-glpk

comparison deps/glpk/src/glpapi02.c @ 11:4fc6ad2fb8a6

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