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 ***********************************************************************/
29 /*----------------------------------------------------------------------
30 // ssx_create - create simplex solver workspace.
32 // This routine creates the workspace used by simplex solver routines,
33 // and returns a pointer to it.
35 // Parameters m, n, and nnz specify, respectively, the number of rows,
36 // columns, and non-zero constraint coefficients.
38 // This routine only allocates the memory for the workspace components,
39 // so the workspace needs to be saturated by data. */
41 SSX *ssx_create(int m, int n, int nnz)
45 xfault("ssx_create: m = %d; invalid number of rows\n", m);
47 xfault("ssx_create: n = %d; invalid number of columns\n", n);
49 xfault("ssx_create: nnz = %d; invalid number of non-zero const"
50 "raint coefficients\n", nnz);
51 ssx = xmalloc(sizeof(SSX));
54 ssx->type = xcalloc(1+m+n, sizeof(int));
55 ssx->lb = xcalloc(1+m+n, sizeof(mpq_t));
56 for (k = 1; k <= m+n; k++) mpq_init(ssx->lb[k]);
57 ssx->ub = xcalloc(1+m+n, sizeof(mpq_t));
58 for (k = 1; k <= m+n; k++) mpq_init(ssx->ub[k]);
59 ssx->coef = xcalloc(1+m+n, sizeof(mpq_t));
60 for (k = 0; k <= m+n; k++) mpq_init(ssx->coef[k]);
61 ssx->A_ptr = xcalloc(1+n+1, sizeof(int));
62 ssx->A_ptr[n+1] = nnz+1;
63 ssx->A_ind = xcalloc(1+nnz, sizeof(int));
64 ssx->A_val = xcalloc(1+nnz, sizeof(mpq_t));
65 for (k = 1; k <= nnz; k++) mpq_init(ssx->A_val[k]);
66 ssx->stat = xcalloc(1+m+n, sizeof(int));
67 ssx->Q_row = xcalloc(1+m+n, sizeof(int));
68 ssx->Q_col = xcalloc(1+m+n, sizeof(int));
69 ssx->binv = bfx_create_binv();
70 ssx->bbar = xcalloc(1+m, sizeof(mpq_t));
71 for (i = 0; i <= m; i++) mpq_init(ssx->bbar[i]);
72 ssx->pi = xcalloc(1+m, sizeof(mpq_t));
73 for (i = 1; i <= m; i++) mpq_init(ssx->pi[i]);
74 ssx->cbar = xcalloc(1+n, sizeof(mpq_t));
75 for (j = 1; j <= n; j++) mpq_init(ssx->cbar[j]);
76 ssx->rho = xcalloc(1+m, sizeof(mpq_t));
77 for (i = 1; i <= m; i++) mpq_init(ssx->rho[i]);
78 ssx->ap = xcalloc(1+n, sizeof(mpq_t));
79 for (j = 1; j <= n; j++) mpq_init(ssx->ap[j]);
80 ssx->aq = xcalloc(1+m, sizeof(mpq_t));
81 for (i = 1; i <= m; i++) mpq_init(ssx->aq[i]);
86 /*----------------------------------------------------------------------
87 // ssx_factorize - factorize the current basis matrix.
89 // This routine computes factorization of the current basis matrix B
90 // and returns the singularity flag. If the matrix B is non-singular,
91 // the flag is zero, otherwise non-zero. */
93 static int basis_col(void *info, int j, int ind[], mpq_t val[])
94 { /* this auxiliary routine provides row indices and numeric values
95 of non-zero elements in j-th column of the matrix B */
99 int *A_ptr = ssx->A_ptr;
100 int *A_ind = ssx->A_ind;
101 mpq_t *A_val = ssx->A_val;
102 int *Q_col = ssx->Q_col;
104 xassert(1 <= j && j <= m);
105 k = Q_col[j]; /* x[k] = xB[j] */
106 xassert(1 <= k && k <= m+n);
107 /* j-th column of the matrix B is k-th column of the augmented
108 constraint matrix (I | -A) */
110 { /* it is a column of the unity matrix I */
111 len = 1, ind[1] = k, mpq_set_si(val[1], 1, 1);
114 { /* it is a column of the original constraint matrix -A */
116 for (ptr = A_ptr[k-m]; ptr < A_ptr[k-m+1]; ptr++)
118 ind[len] = A_ind[ptr];
119 mpq_neg(val[len], A_val[ptr]);
125 int ssx_factorize(SSX *ssx)
127 ret = bfx_factorize(ssx->binv, ssx->m, basis_col, ssx);
131 /*----------------------------------------------------------------------
132 // ssx_get_xNj - determine value of non-basic variable.
134 // This routine determines the value of non-basic variable xN[j] in the
135 // current basic solution defined as follows:
137 // 0, if xN[j] is free variable
138 // lN[j], if xN[j] is on its lower bound
139 // uN[j], if xN[j] is on its upper bound
140 // lN[j] = uN[j], if xN[j] is fixed variable
142 // where lN[j] and uN[j] are lower and upper bounds of xN[j]. */
144 void ssx_get_xNj(SSX *ssx, int j, mpq_t x)
149 int *stat = ssx->stat;
150 int *Q_col = ssx->Q_col;
152 xassert(1 <= j && j <= n);
153 k = Q_col[m+j]; /* x[k] = xN[j] */
154 xassert(1 <= k && k <= m+n);
157 /* xN[j] is on its lower bound */
158 mpq_set(x, lb[k]); break;
160 /* xN[j] is on its upper bound */
161 mpq_set(x, ub[k]); break;
163 /* xN[j] is free variable */
164 mpq_set_si(x, 0, 1); break;
166 /* xN[j] is fixed variable */
167 mpq_set(x, lb[k]); break;
169 xassert(stat != stat);
174 /*----------------------------------------------------------------------
175 // ssx_eval_bbar - compute values of basic variables.
177 // This routine computes values of basic variables xB in the current
178 // basic solution as follows:
180 // beta = - inv(B) * N * xN,
182 // where B is the basis matrix, N is the matrix of non-basic columns,
183 // xN is a vector of current values of non-basic variables. */
185 void ssx_eval_bbar(SSX *ssx)
188 mpq_t *coef = ssx->coef;
189 int *A_ptr = ssx->A_ptr;
190 int *A_ind = ssx->A_ind;
191 mpq_t *A_val = ssx->A_val;
192 int *Q_col = ssx->Q_col;
193 mpq_t *bbar = ssx->bbar;
199 for (i = 1; i <= m; i++)
200 mpq_set_si(bbar[i], 0, 1);
201 /* bbar := - N * xN = - N[1] * xN[1] - ... - N[n] * xN[n] */
202 for (j = 1; j <= n; j++)
203 { ssx_get_xNj(ssx, j, x);
204 if (mpq_sgn(x) == 0) continue;
205 k = Q_col[m+j]; /* x[k] = xN[j] */
207 { /* N[j] is a column of the unity matrix I */
208 mpq_sub(bbar[k], bbar[k], x);
211 { /* N[j] is a column of the original constraint matrix -A */
212 for (ptr = A_ptr[k-m]; ptr < A_ptr[k-m+1]; ptr++)
213 { mpq_mul(temp, A_val[ptr], x);
214 mpq_add(bbar[A_ind[ptr]], bbar[A_ind[ptr]], temp);
218 /* bbar := inv(B) * bbar */
219 bfx_ftran(ssx->binv, bbar, 0);
221 /* compute value of the objective function */
222 /* bbar[0] := c[0] */
223 mpq_set(bbar[0], coef[0]);
224 /* bbar[0] := bbar[0] + sum{i in B} cB[i] * xB[i] */
225 for (i = 1; i <= m; i++)
226 { k = Q_col[i]; /* x[k] = xB[i] */
227 if (mpq_sgn(coef[k]) == 0) continue;
228 mpq_mul(temp, coef[k], bbar[i]);
229 mpq_add(bbar[0], bbar[0], temp);
231 /* bbar[0] := bbar[0] + sum{j in N} cN[j] * xN[j] */
232 for (j = 1; j <= n; j++)
233 { k = Q_col[m+j]; /* x[k] = xN[j] */
234 if (mpq_sgn(coef[k]) == 0) continue;
235 ssx_get_xNj(ssx, j, x);
236 mpq_mul(temp, coef[k], x);
237 mpq_add(bbar[0], bbar[0], temp);
245 /*----------------------------------------------------------------------
246 // ssx_eval_pi - compute values of simplex multipliers.
248 // This routine computes values of simplex multipliers (shadow prices)
249 // pi in the current basic solution as follows:
251 // pi = inv(B') * cB,
253 // where B' is a matrix transposed to the basis matrix B, cB is a vector
254 // of objective coefficients at basic variables xB. */
256 void ssx_eval_pi(SSX *ssx)
258 mpq_t *coef = ssx->coef;
259 int *Q_col = ssx->Q_col;
263 for (i = 1; i <= m; i++) mpq_set(pi[i], coef[Q_col[i]]);
264 /* pi := inv(B') * cB */
265 bfx_btran(ssx->binv, pi);
269 /*----------------------------------------------------------------------
270 // ssx_eval_dj - compute reduced cost of non-basic variable.
272 // This routine computes reduced cost d[j] of non-basic variable xN[j]
273 // in the current basic solution as follows:
275 // d[j] = cN[j] - N[j] * pi,
277 // where cN[j] is an objective coefficient at xN[j], N[j] is a column
278 // of the augmented constraint matrix (I | -A) corresponding to xN[j],
279 // pi is the vector of simplex multipliers (shadow prices). */
281 void ssx_eval_dj(SSX *ssx, int j, mpq_t dj)
284 mpq_t *coef = ssx->coef;
285 int *A_ptr = ssx->A_ptr;
286 int *A_ind = ssx->A_ind;
287 mpq_t *A_val = ssx->A_val;
288 int *Q_col = ssx->Q_col;
293 xassert(1 <= j && j <= n);
294 k = Q_col[m+j]; /* x[k] = xN[j] */
295 xassert(1 <= k && k <= m+n);
296 /* j-th column of the matrix N is k-th column of the augmented
297 constraint matrix (I | -A) */
299 { /* it is a column of the unity matrix I */
300 mpq_sub(dj, coef[k], pi[k]);
303 { /* it is a column of the original constraint matrix -A */
304 mpq_set(dj, coef[k]);
305 for (ptr = A_ptr[k-m], end = A_ptr[k-m+1]; ptr < end; ptr++)
306 { mpq_mul(temp, A_val[ptr], pi[A_ind[ptr]]);
307 mpq_add(dj, dj, temp);
314 /*----------------------------------------------------------------------
315 // ssx_eval_cbar - compute reduced costs of all non-basic variables.
317 // This routine computes the vector of reduced costs pi in the current
318 // basic solution for all non-basic variables, including fixed ones. */
320 void ssx_eval_cbar(SSX *ssx)
322 mpq_t *cbar = ssx->cbar;
324 for (j = 1; j <= n; j++)
325 ssx_eval_dj(ssx, j, cbar[j]);
329 /*----------------------------------------------------------------------
330 // ssx_eval_rho - compute p-th row of the inverse.
332 // This routine computes p-th row of the matrix inv(B), where B is the
333 // current basis matrix.
335 // p-th row of the inverse is computed using the following formula:
337 // rho = inv(B') * e[p],
339 // where B' is a matrix transposed to B, e[p] is a unity vector, which
340 // contains one in p-th position. */
342 void ssx_eval_rho(SSX *ssx)
345 mpq_t *rho = ssx->rho;
347 xassert(1 <= p && p <= m);
349 for (i = 1; i <= m; i++) mpq_set_si(rho[i], 0, 1);
351 mpq_set_si(rho[p], 1, 1);
352 /* rho := inv(B') * rho */
353 bfx_btran(ssx->binv, rho);
357 /*----------------------------------------------------------------------
358 // ssx_eval_row - compute pivot row of the simplex table.
360 // This routine computes p-th (pivot) row of the current simplex table
361 // A~ = - inv(B) * N using the following formula:
363 // A~[p] = - N' * inv(B') * e[p] = - N' * rho[p],
365 // where N' is a matrix transposed to the matrix N, rho[p] is p-th row
366 // of the inverse inv(B). */
368 void ssx_eval_row(SSX *ssx)
371 int *A_ptr = ssx->A_ptr;
372 int *A_ind = ssx->A_ind;
373 mpq_t *A_val = ssx->A_val;
374 int *Q_col = ssx->Q_col;
375 mpq_t *rho = ssx->rho;
380 for (j = 1; j <= n; j++)
381 { /* ap[j] := - N'[j] * rho (inner product) */
382 k = Q_col[m+j]; /* x[k] = xN[j] */
384 mpq_neg(ap[j], rho[k]);
386 { mpq_set_si(ap[j], 0, 1);
387 for (ptr = A_ptr[k-m]; ptr < A_ptr[k-m+1]; ptr++)
388 { mpq_mul(temp, A_val[ptr], rho[A_ind[ptr]]);
389 mpq_add(ap[j], ap[j], temp);
397 /*----------------------------------------------------------------------
398 // ssx_eval_col - compute pivot column of the simplex table.
400 // This routine computes q-th (pivot) column of the current simplex
401 // table A~ = - inv(B) * N using the following formula:
403 // A~[q] = - inv(B) * N[q],
405 // where N[q] is q-th column of the matrix N corresponding to chosen
406 // non-basic variable xN[q]. */
408 void ssx_eval_col(SSX *ssx)
411 int *A_ptr = ssx->A_ptr;
412 int *A_ind = ssx->A_ind;
413 mpq_t *A_val = ssx->A_val;
414 int *Q_col = ssx->Q_col;
418 xassert(1 <= q && q <= n);
420 for (i = 1; i <= m; i++) mpq_set_si(aq[i], 0, 1);
422 k = Q_col[m+q]; /* x[k] = xN[q] */
424 { /* N[q] is a column of the unity matrix I */
425 mpq_set_si(aq[k], 1, 1);
428 { /* N[q] is a column of the original constraint matrix -A */
429 for (ptr = A_ptr[k-m]; ptr < A_ptr[k-m+1]; ptr++)
430 mpq_neg(aq[A_ind[ptr]], A_val[ptr]);
432 /* aq := inv(B) * aq */
433 bfx_ftran(ssx->binv, aq, 1);
435 for (i = 1; i <= m; i++) mpq_neg(aq[i], aq[i]);
439 /*----------------------------------------------------------------------
440 // ssx_chuzc - choose pivot column.
442 // This routine chooses non-basic variable xN[q] whose reduced cost
443 // indicates possible improving of the objective function to enter it
446 // Currently the standard (textbook) pricing is used, i.e. that
447 // non-basic variable is preferred which has greatest reduced cost (in
450 // If xN[q] has been chosen, the routine stores its number q and also
451 // sets the flag q_dir that indicates direction in which xN[q] has to
452 // change (+1 means increasing, -1 means decreasing).
454 // If the choice cannot be made, because the current basic solution is
455 // dual feasible, the routine sets the number q to 0. */
457 void ssx_chuzc(SSX *ssx)
460 int dir = (ssx->dir == SSX_MIN ? +1 : -1);
461 int *Q_col = ssx->Q_col;
462 int *stat = ssx->stat;
463 mpq_t *cbar = ssx->cbar;
464 int j, k, s, q, q_dir;
466 /* nothing is chosen so far */
467 q = 0, q_dir = 0, best = 0.0;
468 /* look through the list of non-basic variables */
469 for (j = 1; j <= n; j++)
470 { k = Q_col[m+j]; /* x[k] = xN[j] */
471 s = dir * mpq_sgn(cbar[j]);
472 if ((stat[k] == SSX_NF || stat[k] == SSX_NL) && s < 0 ||
473 (stat[k] == SSX_NF || stat[k] == SSX_NU) && s > 0)
474 { /* reduced cost of xN[j] indicates possible improving of
475 the objective function */
476 temp = fabs(mpq_get_d(cbar[j]));
477 xassert(temp != 0.0);
478 if (q == 0 || best < temp)
479 q = j, q_dir = - s, best = temp;
482 ssx->q = q, ssx->q_dir = q_dir;
486 /*----------------------------------------------------------------------
487 // ssx_chuzr - choose pivot row.
489 // This routine looks through elements of q-th column of the simplex
490 // table and chooses basic variable xB[p] which should leave the basis.
492 // The choice is based on the standard (textbook) ratio test.
494 // If xB[p] has been chosen, the routine stores its number p and also
495 // sets its non-basic status p_stat which should be assigned to xB[p]
496 // when it has left the basis and become xN[q].
498 // Special case p < 0 means that xN[q] is double-bounded variable and
499 // it reaches its opposite bound before any basic variable does that,
500 // so the current basis remains unchanged.
502 // If the choice cannot be made, because xN[q] can infinitely change in
503 // the feasible direction, the routine sets the number p to 0. */
505 void ssx_chuzr(SSX *ssx)
508 int *type = ssx->type;
511 int *Q_col = ssx->Q_col;
512 mpq_t *bbar = ssx->bbar;
515 int q_dir = ssx->q_dir;
516 int i, k, s, t, p, p_stat;
520 xassert(1 <= q && q <= n);
521 xassert(q_dir == +1 || q_dir == -1);
522 /* nothing is chosen so far */
524 /* look through the list of basic variables */
525 for (i = 1; i <= m; i++)
526 { s = q_dir * mpq_sgn(aq[i]);
528 { /* xB[i] decreases */
529 k = Q_col[i]; /* x[k] = xB[i] */
531 if (t == SSX_LO || t == SSX_DB || t == SSX_FX)
532 { /* xB[i] has finite lower bound */
533 mpq_sub(temp, bbar[i], lb[k]);
534 mpq_div(temp, temp, aq[i]);
536 if (p == 0 || mpq_cmp(teta, temp) > 0)
538 p_stat = (t == SSX_FX ? SSX_NS : SSX_NL);
544 { /* xB[i] increases */
545 k = Q_col[i]; /* x[k] = xB[i] */
547 if (t == SSX_UP || t == SSX_DB || t == SSX_FX)
548 { /* xB[i] has finite upper bound */
549 mpq_sub(temp, bbar[i], ub[k]);
550 mpq_div(temp, temp, aq[i]);
552 if (p == 0 || mpq_cmp(teta, temp) > 0)
554 p_stat = (t == SSX_FX ? SSX_NS : SSX_NU);
559 /* if something has been chosen and the ratio test indicates
560 exact degeneracy, the search can be finished */
561 if (p != 0 && mpq_sgn(teta) == 0) break;
563 /* if xN[q] is double-bounded, check if it can reach its opposite
564 bound before any basic variable */
565 k = Q_col[m+q]; /* x[k] = xN[q] */
566 if (type[k] == SSX_DB)
567 { mpq_sub(temp, ub[k], lb[k]);
568 if (p == 0 || mpq_cmp(teta, temp) > 0)
575 ssx->p_stat = p_stat;
576 /* if xB[p] has been chosen, determine its actual change in the
577 adjacent basis (it has the same sign as q_dir) */
579 { xassert(mpq_sgn(teta) >= 0);
581 mpq_set(ssx->delta, teta);
583 mpq_neg(ssx->delta, teta);
590 /*----------------------------------------------------------------------
591 // ssx_update_bbar - update values of basic variables.
593 // This routine recomputes the current values of basic variables for
594 // the adjacent basis.
596 // The simplex table for the current basis is the following:
598 // xB[i] = sum{j in 1..n} alfa[i,j] * xN[q], i = 1,...,m
602 // delta xB[i] = alfa[i,q] * delta xN[q], i = 1,...,m
604 // where delta xN[q] = xN.new[q] - xN[q] is the change of xN[q] in the
605 // adjacent basis, and delta xB[i] = xB.new[i] - xB[i] is the change of
606 // xB[i]. This gives formulae for recomputing values of xB[i]:
608 // xB.new[p] = xN[q] + delta xN[q]
610 // (because xN[q] becomes xB[p] in the adjacent basis), and
612 // xB.new[i] = xB[i] + alfa[i,q] * delta xN[q], i != p
614 // for other basic variables. */
616 void ssx_update_bbar(SSX *ssx)
619 mpq_t *bbar = ssx->bbar;
620 mpq_t *cbar = ssx->cbar;
627 xassert(1 <= q && q <= n);
629 { /* xN[q] is double-bounded and goes to its opposite bound */
633 { /* xN[q] becomes xB[p] in the adjacent basis */
634 /* xB.new[p] = xN[q] + delta xN[q] */
635 xassert(1 <= p && p <= m);
636 ssx_get_xNj(ssx, q, temp);
637 mpq_add(bbar[p], temp, ssx->delta);
639 /* update values of other basic variables depending on xN[q] */
640 for (i = 1; i <= m; i++)
641 { if (i == p) continue;
642 /* xB.new[i] = xB[i] + alfa[i,q] * delta xN[q] */
643 if (mpq_sgn(aq[i]) == 0) continue;
644 mpq_mul(temp, aq[i], ssx->delta);
645 mpq_add(bbar[i], bbar[i], temp);
648 /* update value of the objective function */
649 /* z.new = z + d[q] * delta xN[q] */
650 mpq_mul(temp, cbar[q], ssx->delta);
651 mpq_add(bbar[0], bbar[0], temp);
657 /*----------------------------------------------------------------------
658 -- ssx_update_pi - update simplex multipliers.
660 -- This routine recomputes the vector of simplex multipliers for the
661 -- adjacent basis. */
663 void ssx_update_pi(SSX *ssx)
667 mpq_t *cbar = ssx->cbar;
671 mpq_t *rho = ssx->rho;
676 xassert(1 <= p && p <= m);
677 xassert(1 <= q && q <= n);
678 /* compute d[q] in the adjacent basis */
679 mpq_div(new_dq, cbar[q], aq[p]);
680 /* update the vector of simplex multipliers */
681 for (i = 1; i <= m; i++)
682 { if (mpq_sgn(rho[i]) == 0) continue;
683 mpq_mul(temp, new_dq, rho[i]);
684 mpq_sub(pi[i], pi[i], temp);
691 /*----------------------------------------------------------------------
692 // ssx_update_cbar - update reduced costs of non-basic variables.
694 // This routine recomputes the vector of reduced costs of non-basic
695 // variables for the adjacent basis. */
697 void ssx_update_cbar(SSX *ssx)
700 mpq_t *cbar = ssx->cbar;
707 xassert(1 <= p && p <= m);
708 xassert(1 <= q && q <= n);
709 /* compute d[q] in the adjacent basis */
710 /* d.new[q] = d[q] / alfa[p,q] */
711 mpq_div(cbar[q], cbar[q], ap[q]);
712 /* update reduced costs of other non-basic variables */
713 for (j = 1; j <= n; j++)
714 { if (j == q) continue;
715 /* d.new[j] = d[j] - (alfa[p,j] / alfa[p,q]) * d[q] */
716 if (mpq_sgn(ap[j]) == 0) continue;
717 mpq_mul(temp, ap[j], cbar[q]);
718 mpq_sub(cbar[j], cbar[j], temp);
724 /*----------------------------------------------------------------------
725 // ssx_change_basis - change current basis to adjacent one.
727 // This routine changes the current basis to the adjacent one swapping
728 // basic variable xB[p] and non-basic variable xN[q]. */
730 void ssx_change_basis(SSX *ssx)
733 int *type = ssx->type;
734 int *stat = ssx->stat;
735 int *Q_row = ssx->Q_row;
736 int *Q_col = ssx->Q_col;
739 int p_stat = ssx->p_stat;
742 { /* special case: xN[q] goes to its opposite bound */
743 xassert(1 <= q && q <= n);
744 k = Q_col[m+q]; /* x[k] = xN[q] */
745 xassert(type[k] == SSX_DB);
754 xassert(stat != stat);
758 { /* xB[p] leaves the basis, xN[q] enters the basis */
759 xassert(1 <= p && p <= m);
760 xassert(1 <= q && q <= n);
761 kp = Q_col[p]; /* x[kp] = xB[p] */
762 kq = Q_col[m+q]; /* x[kq] = xN[q] */
763 /* check non-basic status of xB[p] which becomes xN[q] */
766 xassert(p_stat == SSX_NF);
769 xassert(p_stat == SSX_NL);
772 xassert(p_stat == SSX_NU);
775 xassert(p_stat == SSX_NL || p_stat == SSX_NU);
778 xassert(p_stat == SSX_NS);
781 xassert(type != type);
783 /* swap xB[p] and xN[q] */
784 stat[kp] = (char)p_stat, stat[kq] = SSX_BS;
785 Q_row[kp] = m+q, Q_row[kq] = p;
786 Q_col[p] = kq, Q_col[m+q] = kp;
787 /* update factorization of the basis matrix */
788 if (bfx_update(ssx->binv, p))
789 { if (ssx_factorize(ssx))
790 xassert(("Internal error: basis matrix is singular", 0));
796 /*----------------------------------------------------------------------
797 // ssx_delete - delete simplex solver workspace.
799 // This routine deletes the simplex solver workspace freeing all the
800 // memory allocated to this object. */
802 void ssx_delete(SSX *ssx)
805 int nnz = ssx->A_ptr[n+1]-1;
808 for (k = 1; k <= m+n; k++) mpq_clear(ssx->lb[k]);
810 for (k = 1; k <= m+n; k++) mpq_clear(ssx->ub[k]);
812 for (k = 0; k <= m+n; k++) mpq_clear(ssx->coef[k]);
816 for (k = 1; k <= nnz; k++) mpq_clear(ssx->A_val[k]);
821 bfx_delete_binv(ssx->binv);
822 for (i = 0; i <= m; i++) mpq_clear(ssx->bbar[i]);
824 for (i = 1; i <= m; i++) mpq_clear(ssx->pi[i]);
826 for (j = 1; j <= n; j++) mpq_clear(ssx->cbar[j]);
828 for (i = 1; i <= m; i++) mpq_clear(ssx->rho[i]);
830 for (j = 1; j <= n; j++) mpq_clear(ssx->ap[j]);
832 for (i = 1; i <= m; i++) mpq_clear(ssx->aq[i]);
834 mpq_clear(ssx->delta);