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 static void show_progress(SSX *ssx, int phase)
29 { /* this auxiliary routine displays information about progress of
32 for (i = 1; i <= ssx->m; i++)
33 if (ssx->type[ssx->Q_col[i]] == SSX_FX) def++;
34 xprintf("%s%6d: %s = %22.15g (%d)\n", phase == 1 ? " " : "*",
35 ssx->it_cnt, phase == 1 ? "infsum" : "objval",
36 mpq_get_d(ssx->bbar[0]), def);
38 ssx->tm_lag = utime();
40 ssx->tm_lag = xtime();
45 /*----------------------------------------------------------------------
46 // ssx_phase_I - find primal feasible solution.
48 // This routine implements phase I of the primal simplex method.
50 // On exit the routine returns one of the following codes:
52 // 0 - feasible solution found;
53 // 1 - problem has no feasible solution;
54 // 2 - iterations limit exceeded;
55 // 3 - time limit exceeded.
56 ----------------------------------------------------------------------*/
58 int ssx_phase_I(SSX *ssx)
61 int *type = ssx->type;
64 mpq_t *coef = ssx->coef;
65 int *A_ptr = ssx->A_ptr;
66 int *A_ind = ssx->A_ind;
67 mpq_t *A_val = ssx->A_val;
68 int *Q_col = ssx->Q_col;
69 mpq_t *bbar = ssx->bbar;
71 mpq_t *cbar = ssx->cbar;
72 int *orig_type, orig_dir;
73 mpq_t *orig_lb, *orig_ub, *orig_coef;
75 /* save components of the original LP problem, which are changed
77 orig_type = xcalloc(1+m+n, sizeof(int));
78 orig_lb = xcalloc(1+m+n, sizeof(mpq_t));
79 orig_ub = xcalloc(1+m+n, sizeof(mpq_t));
80 orig_coef = xcalloc(1+m+n, sizeof(mpq_t));
81 for (k = 1; k <= m+n; k++)
82 { orig_type[k] = type[k];
84 mpq_set(orig_lb[k], lb[k]);
86 mpq_set(orig_ub[k], ub[k]);
89 for (k = 0; k <= m+n; k++)
90 { mpq_init(orig_coef[k]);
91 mpq_set(orig_coef[k], coef[k]);
93 /* build an artificial basic solution, which is primal feasible,
94 and also build an auxiliary objective function to minimize the
95 sum of infeasibilities for the original problem */
97 for (k = 0; k <= m+n; k++) mpq_set_si(coef[k], 0, 1);
98 mpq_set_si(bbar[0], 0, 1);
99 for (i = 1; i <= m; i++)
101 k = Q_col[i]; /* x[k] = xB[i] */
103 if (t == SSX_LO || t == SSX_DB || t == SSX_FX)
104 { /* in the original problem x[k] has lower bound */
105 if (mpq_cmp(bbar[i], lb[k]) < 0)
106 { /* which is violated */
108 mpq_set(ub[k], lb[k]);
109 mpq_set_si(lb[k], 0, 1);
110 mpq_set_si(coef[k], -1, 1);
111 mpq_add(bbar[0], bbar[0], ub[k]);
112 mpq_sub(bbar[0], bbar[0], bbar[i]);
115 if (t == SSX_UP || t == SSX_DB || t == SSX_FX)
116 { /* in the original problem x[k] has upper bound */
117 if (mpq_cmp(bbar[i], ub[k]) > 0)
118 { /* which is violated */
120 mpq_set(lb[k], ub[k]);
121 mpq_set_si(ub[k], 0, 1);
122 mpq_set_si(coef[k], +1, 1);
123 mpq_add(bbar[0], bbar[0], bbar[i]);
124 mpq_sub(bbar[0], bbar[0], lb[k]);
128 /* now the initial basic solution should be primal feasible due
129 to changes of bounds of some basic variables, which turned to
130 implicit artifical variables */
131 /* compute simplex multipliers and reduced costs */
134 /* display initial progress of the search */
135 show_progress(ssx, 1);
136 /* main loop starts here */
138 { /* display current progress of the search */
140 if (utime() - ssx->tm_lag >= ssx->out_frq - 0.001)
142 if (xdifftime(xtime(), ssx->tm_lag) >= ssx->out_frq - 0.001)
144 show_progress(ssx, 1);
145 /* we do not need to wait until all artificial variables have
147 if (mpq_sgn(bbar[0]) == 0)
148 { /* the sum of infeasibilities is zero, therefore the current
149 solution is primal feasible for the original problem */
153 /* check if the iterations limit has been exhausted */
154 if (ssx->it_lim == 0)
158 /* check if the time limit has been exhausted */
160 if (ssx->tm_lim >= 0.0 && ssx->tm_lim <= utime() - ssx->tm_beg)
162 if (ssx->tm_lim >= 0.0 &&
163 ssx->tm_lim <= xdifftime(xtime(), ssx->tm_beg))
168 /* choose non-basic variable xN[q] */
170 /* if xN[q] cannot be chosen, the sum of infeasibilities is
171 minimal but non-zero; therefore the original problem has no
172 primal feasible solution */
177 /* compute q-th column of the simplex table */
179 /* choose basic variable xB[p] */
181 /* the sum of infeasibilities cannot be negative, therefore
182 the auxiliary lp problem cannot have unbounded solution */
183 xassert(ssx->p != 0);
184 /* update values of basic variables */
185 ssx_update_bbar(ssx);
187 { /* compute p-th row of the inverse inv(B) */
189 /* compute p-th row of the simplex table */
191 xassert(mpq_cmp(ssx->aq[ssx->p], ssx->ap[ssx->q]) == 0);
192 /* update simplex multipliers */
194 /* update reduced costs of non-basic variables */
195 ssx_update_cbar(ssx);
197 /* xB[p] is leaving the basis; if it is implicit artificial
198 variable, the corresponding residual vanishes; therefore
199 bounds of this variable should be restored to the original
202 { k = Q_col[ssx->p]; /* x[k] = xB[p] */
203 if (type[k] != orig_type[k])
204 { /* x[k] is implicit artificial variable */
205 type[k] = orig_type[k];
206 mpq_set(lb[k], orig_lb[k]);
207 mpq_set(ub[k], orig_ub[k]);
208 xassert(ssx->p_stat == SSX_NL || ssx->p_stat == SSX_NU);
209 ssx->p_stat = (ssx->p_stat == SSX_NL ? SSX_NU : SSX_NL);
210 if (type[k] == SSX_FX) ssx->p_stat = SSX_NS;
211 /* nullify the objective coefficient at x[k] */
212 mpq_set_si(coef[k], 0, 1);
213 /* since coef[k] has been changed, we need to compute
214 new reduced cost of x[k], which it will have in the
216 /* the formula d[j] = cN[j] - pi' * N[j] is used (note
217 that the vector pi is not changed, because it depends
218 on objective coefficients at basic variables, but in
219 the adjacent basis, for which the vector pi has been
220 just recomputed, x[k] is non-basic) */
222 { /* x[k] is auxiliary variable */
223 mpq_neg(cbar[ssx->q], pi[k]);
226 { /* x[k] is structural variable */
230 mpq_set_si(cbar[ssx->q], 0, 1);
231 for (ptr = A_ptr[k-m]; ptr < A_ptr[k-m+1]; ptr++)
232 { mpq_mul(temp, pi[A_ind[ptr]], A_val[ptr]);
233 mpq_add(cbar[ssx->q], cbar[ssx->q], temp);
239 /* jump to the adjacent vertex of the polyhedron */
240 ssx_change_basis(ssx);
241 /* one simplex iteration has been performed */
242 if (ssx->it_lim > 0) ssx->it_lim--;
245 /* display final progress of the search */
246 show_progress(ssx, 1);
247 /* restore components of the original problem, which were changed
249 for (k = 1; k <= m+n; k++)
250 { type[k] = orig_type[k];
251 mpq_set(lb[k], orig_lb[k]);
252 mpq_clear(orig_lb[k]);
253 mpq_set(ub[k], orig_ub[k]);
254 mpq_clear(orig_ub[k]);
257 for (k = 0; k <= m+n; k++)
258 { mpq_set(coef[k], orig_coef[k]);
259 mpq_clear(orig_coef[k]);
265 /* return to the calling program */
269 /*----------------------------------------------------------------------
270 // ssx_phase_II - find optimal solution.
272 // This routine implements phase II of the primal simplex method.
274 // On exit the routine returns one of the following codes:
276 // 0 - optimal solution found;
277 // 1 - problem has unbounded solution;
278 // 2 - iterations limit exceeded;
279 // 3 - time limit exceeded.
280 ----------------------------------------------------------------------*/
282 int ssx_phase_II(SSX *ssx)
284 /* display initial progress of the search */
285 show_progress(ssx, 2);
286 /* main loop starts here */
288 { /* display current progress of the search */
290 if (utime() - ssx->tm_lag >= ssx->out_frq - 0.001)
292 if (xdifftime(xtime(), ssx->tm_lag) >= ssx->out_frq - 0.001)
294 show_progress(ssx, 2);
295 /* check if the iterations limit has been exhausted */
296 if (ssx->it_lim == 0)
300 /* check if the time limit has been exhausted */
302 if (ssx->tm_lim >= 0.0 && ssx->tm_lim <= utime() - ssx->tm_beg)
304 if (ssx->tm_lim >= 0.0 &&
305 ssx->tm_lim <= xdifftime(xtime(), ssx->tm_beg))
310 /* choose non-basic variable xN[q] */
312 /* if xN[q] cannot be chosen, the current basic solution is
313 dual feasible and therefore optimal */
318 /* compute q-th column of the simplex table */
320 /* choose basic variable xB[p] */
322 /* if xB[p] cannot be chosen, the problem has no dual feasible
323 solution (i.e. unbounded) */
328 /* update values of basic variables */
329 ssx_update_bbar(ssx);
331 { /* compute p-th row of the inverse inv(B) */
333 /* compute p-th row of the simplex table */
335 xassert(mpq_cmp(ssx->aq[ssx->p], ssx->ap[ssx->q]) == 0);
337 /* update simplex multipliers */
340 /* update reduced costs of non-basic variables */
341 ssx_update_cbar(ssx);
343 /* jump to the adjacent vertex of the polyhedron */
344 ssx_change_basis(ssx);
345 /* one simplex iteration has been performed */
346 if (ssx->it_lim > 0) ssx->it_lim--;
349 /* display final progress of the search */
350 show_progress(ssx, 2);
351 /* return to the calling program */
355 /*----------------------------------------------------------------------
356 // ssx_driver - base driver to exact simplex method.
358 // This routine is a base driver to a version of the primal simplex
359 // method using exact (bignum) arithmetic.
361 // On exit the routine returns one of the following codes:
363 // 0 - optimal solution found;
364 // 1 - problem has no feasible solution;
365 // 2 - problem has unbounded solution;
366 // 3 - iterations limit exceeded (phase I);
367 // 4 - iterations limit exceeded (phase II);
368 // 5 - time limit exceeded (phase I);
369 // 6 - time limit exceeded (phase II);
370 // 7 - initial basis matrix is exactly singular.
371 ----------------------------------------------------------------------*/
373 int ssx_driver(SSX *ssx)
375 int *type = ssx->type;
378 int *Q_col = ssx->Q_col;
379 mpq_t *bbar = ssx->bbar;
381 ssx->tm_beg = xtime();
382 /* factorize the initial basis matrix */
383 if (ssx_factorize(ssx))
384 { xprintf("Initial basis matrix is singular\n");
388 /* compute values of basic variables */
390 /* check if the initial basic solution is primal feasible */
391 for (i = 1; i <= m; i++)
393 k = Q_col[i]; /* x[k] = xB[i] */
395 if (t == SSX_LO || t == SSX_DB || t == SSX_FX)
396 { /* x[k] has lower bound */
397 if (mpq_cmp(bbar[i], lb[k]) < 0)
398 { /* which is violated */
402 if (t == SSX_UP || t == SSX_DB || t == SSX_FX)
403 { /* x[k] has upper bound */
404 if (mpq_cmp(bbar[i], ub[k]) > 0)
405 { /* which is violated */
411 { /* no basic variable violates its bounds */
415 /* phase I: find primal feasible solution */
416 ret = ssx_phase_I(ssx);
422 xprintf("PROBLEM HAS NO FEASIBLE SOLUTION\n");
426 xprintf("ITERATIONS LIMIT EXCEEDED; SEARCH TERMINATED\n");
430 xprintf("TIME LIMIT EXCEEDED; SEARCH TERMINATED\n");
436 /* compute values of basic variables (actually only the objective
437 value needs to be computed) */
439 skip: /* compute simplex multipliers */
441 /* compute reduced costs of non-basic variables */
443 /* if phase I failed, do not start phase II */
444 if (ret != 0) goto done;
445 /* phase II: find optimal solution */
446 ret = ssx_phase_II(ssx);
449 xprintf("OPTIMAL SOLUTION FOUND\n");
453 xprintf("PROBLEM HAS UNBOUNDED SOLUTION\n");
457 xprintf("ITERATIONS LIMIT EXCEEDED; SEARCH TERMINATED\n");
461 xprintf("TIME LIMIT EXCEEDED; SEARCH TERMINATED\n");
467 done: /* decrease the time limit by the spent amount of time */
468 if (ssx->tm_lim >= 0.0)
470 { ssx->tm_lim -= utime() - ssx->tm_beg;
472 { ssx->tm_lim -= xdifftime(xtime(), ssx->tm_beg);
474 if (ssx->tm_lim < 0.0) ssx->tm_lim = 0.0;