1 /* glpapi07.c (exact simplex solver) */
3 /***********************************************************************
4 * This code is part of GLPK (GNU Linear Programming Kit).
6 * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
7 * 2009, 2010 Andrew Makhorin, Department for Applied Informatics,
8 * Moscow Aviation Institute, Moscow, Russia. All rights reserved.
9 * E-mail: <mao@gnu.org>.
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.
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.
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 ***********************************************************************/
28 /***********************************************************************
31 * glp_exact - solve LP problem in exact arithmetic
35 * int glp_exact(glp_prob *lp, const glp_smcp *parm);
39 * The routine glp_exact is a tentative implementation of the primal
40 * two-phase simplex method based on exact (rational) arithmetic. It is
41 * similar to the routine glp_simplex, however, for all internal
42 * computations it uses arithmetic of rational numbers, which is exact
43 * in mathematical sense, i.e. free of round-off errors unlike floating
46 * Note that the routine glp_exact uses inly two control parameters
47 * passed in the structure glp_smcp, namely, it_lim and tm_lim.
51 * 0 The LP problem instance has been successfully solved. This code
52 * does not necessarily mean that the solver has found optimal
53 * solution. It only means that the solution process was successful.
56 * Unable to start the search, because the initial basis specified
57 * in the problem object is invalid--the number of basic (auxiliary
58 * and structural) variables is not the same as the number of rows in
62 * Unable to start the search, because the basis matrix correspodning
63 * to the initial basis is exactly singular.
66 * Unable to start the search, because some double-bounded variables
67 * have incorrect bounds.
70 * The problem has no rows/columns.
73 * The search was prematurely terminated, because the simplex
74 * iteration limit has been exceeded.
77 * The search was prematurely terminated, because the time limit has
80 static void set_d_eps(mpq_t x, double val)
81 { /* convert double val to rational x obtaining a more adequate
82 fraction than provided by mpq_set_d due to allowing a small
83 approximation error specified by a given relative tolerance;
84 for example, mpq_set_d would give the following
85 1/3 ~= 0.333333333333333314829616256247391... ->
86 -> 6004799503160661/18014398509481984
87 while this routine gives exactly 1/3 */
89 double f, p, q, eps = 1e-9;
91 xassert(-DBL_MAX <= val && val <= +DBL_MAX);
92 #if 1 /* 30/VII-2008 */
93 if (val == floor(val))
94 { /* if val is integral, do not approximate */
104 { mpq_set_si(x, 0, 1);
107 f = frexp(fabs(val), &n);
108 /* |val| = f * 2^n, where 0.5 <= f < 1.0 */
109 fp2rat(f, 0.1 * eps, &p, &q);
110 /* f ~= p / q, where p and q are integers */
115 mpq_set_si(temp, 1, 1);
116 for (j = 1; j <= abs(n); j++)
117 mpq_add(temp, temp, temp);
123 if (s < 0) mpq_neg(x, x);
124 /* check that the desired tolerance has been attained */
125 xassert(fabs(val - mpq_get_d(x)) <= eps * (1.0 + fabs(val)));
129 static void load_data(SSX *ssx, LPX *lp)
130 { /* load LP problem data into simplex solver workspace */
133 int nnz = ssx->A_ptr[n+1]-1;
134 int j, k, type, loc, len, *ind;
135 double lb, ub, coef, *val;
136 xassert(lpx_get_num_rows(lp) == m);
137 xassert(lpx_get_num_cols(lp) == n);
138 xassert(lpx_get_num_nz(lp) == nnz);
139 /* types and bounds of rows and columns */
140 for (k = 1; k <= m+n; k++)
142 { type = lpx_get_row_type(lp, k);
143 lb = lpx_get_row_lb(lp, k);
144 ub = lpx_get_row_ub(lp, k);
147 { type = lpx_get_col_type(lp, k-m);
148 lb = lpx_get_col_lb(lp, k-m);
149 ub = lpx_get_col_ub(lp, k-m);
152 { case LPX_FR: type = SSX_FR; break;
153 case LPX_LO: type = SSX_LO; break;
154 case LPX_UP: type = SSX_UP; break;
155 case LPX_DB: type = SSX_DB; break;
156 case LPX_FX: type = SSX_FX; break;
157 default: xassert(type != type);
160 set_d_eps(ssx->lb[k], lb);
161 set_d_eps(ssx->ub[k], ub);
163 /* optimization direction */
164 switch (lpx_get_obj_dir(lp))
165 { case LPX_MIN: ssx->dir = SSX_MIN; break;
166 case LPX_MAX: ssx->dir = SSX_MAX; break;
167 default: xassert(lp != lp);
169 /* objective coefficients */
170 for (k = 0; k <= m+n; k++)
172 coef = lpx_get_obj_coef(lp, 0);
176 coef = lpx_get_obj_coef(lp, k-m);
177 set_d_eps(ssx->coef[k], coef);
179 /* constraint coefficients */
180 ind = xcalloc(1+m, sizeof(int));
181 val = xcalloc(1+m, sizeof(double));
183 for (j = 1; j <= n; j++)
184 { ssx->A_ptr[j] = loc+1;
185 len = lpx_get_mat_col(lp, j, ind, val);
186 for (k = 1; k <= len; k++)
188 ssx->A_ind[loc] = ind[k];
189 set_d_eps(ssx->A_val[loc], val[k]);
198 static int load_basis(SSX *ssx, LPX *lp)
199 { /* load current LP basis into simplex solver workspace */
202 int *type = ssx->type;
203 int *stat = ssx->stat;
204 int *Q_row = ssx->Q_row;
205 int *Q_col = ssx->Q_col;
207 xassert(lpx_get_num_rows(lp) == m);
208 xassert(lpx_get_num_cols(lp) == n);
209 /* statuses of rows and columns */
210 for (k = 1; k <= m+n; k++)
212 stat[k] = lpx_get_row_stat(lp, k);
214 stat[k] = lpx_get_col_stat(lp, k-m);
221 xassert(type[k] == SSX_LO || type[k] == SSX_DB);
225 xassert(type[k] == SSX_UP || type[k] == SSX_DB);
229 xassert(type[k] == SSX_FR);
233 xassert(type[k] == SSX_FX);
236 xassert(stat != stat);
239 /* build permutation matix Q */
241 for (k = 1; k <= m+n; k++)
242 { if (stat[k] == SSX_BS)
245 Q_row[k] = i, Q_col[i] = k;
250 Q_row[k] = m+j, Q_col[m+j] = k;
253 xassert(i == m && j == n);
257 int glp_exact(glp_prob *lp, const glp_smcp *parm)
260 int m = lpx_get_num_rows(lp);
261 int n = lpx_get_num_cols(lp);
262 int nnz = lpx_get_num_nz(lp);
263 int i, j, k, type, pst, dst, ret, *stat;
264 double lb, ub, *prim, *dual, sum;
266 parm = &_parm, glp_init_smcp((glp_smcp *)parm);
267 /* check control parameters */
268 if (parm->it_lim < 0)
269 xerror("glp_exact: it_lim = %d; invalid parameter\n",
271 if (parm->tm_lim < 0)
272 xerror("glp_exact: tm_lim = %d; invalid parameter\n",
274 /* the problem must have at least one row and one column */
275 if (!(m > 0 && n > 0))
276 { xprintf("glp_exact: problem has no rows/columns\n");
280 /* basic solution is currently undefined */
281 lp->pbs_stat = lp->dbs_stat = GLP_UNDEF;
285 /* check that all double-bounded variables have correct bounds */
286 for (k = 1; k <= m+n; k++)
288 { type = lpx_get_row_type(lp, k);
289 lb = lpx_get_row_lb(lp, k);
290 ub = lpx_get_row_ub(lp, k);
293 { type = lpx_get_col_type(lp, k-m);
294 lb = lpx_get_col_lb(lp, k-m);
295 ub = lpx_get_col_ub(lp, k-m);
297 if (type == LPX_DB && lb >= ub)
298 { xprintf("glp_exact: %s %d has invalid bounds\n",
299 k <= m ? "row" : "column", k <= m ? k : k-m);
303 /* create the simplex solver workspace */
304 xprintf("glp_exact: %d rows, %d columns, %d non-zeros\n",
307 xprintf("GNU MP bignum library is being used\n");
309 xprintf("GLPK bignum module is being used\n");
310 xprintf("(Consider installing GNU MP to attain a much better perf"
313 ssx = ssx_create(m, n, nnz);
314 /* load LP problem data into the workspace */
316 /* load current LP basis into the workspace */
317 if (load_basis(ssx, lp))
318 { xprintf("glp_exact: initial LP basis is invalid\n");
322 /* inherit some control parameters from the LP object */
324 ssx->it_lim = lpx_get_int_parm(lp, LPX_K_ITLIM);
325 ssx->it_cnt = lpx_get_int_parm(lp, LPX_K_ITCNT);
326 ssx->tm_lim = lpx_get_real_parm(lp, LPX_K_TMLIM);
328 ssx->it_lim = parm->it_lim;
329 ssx->it_cnt = lp->it_cnt;
330 ssx->tm_lim = (double)parm->tm_lim / 1000.0;
333 ssx->tm_beg = xtime();
334 ssx->tm_lag = xlset(0);
336 ret = ssx_driver(ssx);
337 /* copy back some statistics to the LP object */
339 lpx_set_int_parm(lp, LPX_K_ITLIM, ssx->it_lim);
340 lpx_set_int_parm(lp, LPX_K_ITCNT, ssx->it_cnt);
341 lpx_set_real_parm(lp, LPX_K_TMLIM, ssx->tm_lim);
343 lp->it_cnt = ssx->it_cnt;
345 /* analyze the return code */
348 /* optimal solution found */
350 pst = LPX_P_FEAS, dst = LPX_D_FEAS;
353 /* problem has no feasible solution */
355 pst = LPX_P_NOFEAS, dst = LPX_D_INFEAS;
358 /* problem has unbounded solution */
360 pst = LPX_P_FEAS, dst = LPX_D_NOFEAS;
362 xassert(1 <= ssx->q && ssx->q <= n);
363 lp->some = ssx->Q_col[m + ssx->q];
364 xassert(1 <= lp->some && lp->some <= m+n);
368 /* iteration limit exceeded (phase I) */
370 pst = LPX_P_INFEAS, dst = LPX_D_INFEAS;
373 /* iteration limit exceeded (phase II) */
375 pst = LPX_P_FEAS, dst = LPX_D_INFEAS;
378 /* time limit exceeded (phase I) */
380 pst = LPX_P_INFEAS, dst = LPX_D_INFEAS;
383 /* time limit exceeded (phase II) */
385 pst = LPX_P_FEAS, dst = LPX_D_INFEAS;
388 /* initial basis matrix is singular */
394 /* obtain final basic solution components */
395 stat = xcalloc(1+m+n, sizeof(int));
396 prim = xcalloc(1+m+n, sizeof(double));
397 dual = xcalloc(1+m+n, sizeof(double));
398 for (k = 1; k <= m+n; k++)
399 { if (ssx->stat[k] == SSX_BS)
400 { i = ssx->Q_row[k]; /* x[k] = xB[i] */
401 xassert(1 <= i && i <= m);
403 prim[k] = mpq_get_d(ssx->bbar[i]);
407 { j = ssx->Q_row[k] - m; /* x[k] = xN[j] */
408 xassert(1 <= j && j <= n);
409 switch (ssx->stat[k])
416 prim[k] = mpq_get_d(ssx->lb[k]);
420 prim[k] = mpq_get_d(ssx->ub[k]);
424 prim[k] = mpq_get_d(ssx->lb[k]);
429 dual[k] = mpq_get_d(ssx->cbar[j]);
432 /* and store them into the LP object */
433 pst = pst - LPX_P_UNDEF + GLP_UNDEF;
434 dst = dst - LPX_D_UNDEF + GLP_UNDEF;
435 for (k = 1; k <= m+n; k++)
436 stat[k] = stat[k] - LPX_BS + GLP_BS;
437 sum = lpx_get_obj_coef(lp, 0);
438 for (j = 1; j <= n; j++)
439 sum += lpx_get_obj_coef(lp, j) * prim[m+j];
440 lpx_put_solution(lp, 1, &pst, &dst, &sum,
441 &stat[0], &prim[0], &dual[0], &stat[m], &prim[m], &dual[m]);
445 done: /* delete the simplex solver workspace */
447 /* return to the application program */