1 /* glpapi06.c (simplex method routines) */
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 /***********************************************************************
32 * glp_simplex - solve LP problem with the simplex method
36 * int glp_simplex(glp_prob *P, const glp_smcp *parm);
40 * The routine glp_simplex is a driver to the LP solver based on the
41 * simplex method. This routine retrieves problem data from the
42 * specified problem object, calls the solver to solve the problem
43 * instance, and stores results of computations back into the problem
46 * The simplex solver has a set of control parameters. Values of the
47 * control parameters can be passed in a structure glp_smcp, which the
48 * parameter parm points to.
50 * The parameter parm can be specified as NULL, in which case the LP
51 * solver uses default settings.
55 * 0 The LP problem instance has been successfully solved. This code
56 * does not necessarily mean that the solver has found optimal
57 * solution. It only means that the solution process was successful.
60 * Unable to start the search, because the initial basis specified
61 * in the problem object is invalid--the number of basic (auxiliary
62 * and structural) variables is not the same as the number of rows in
66 * Unable to start the search, because the basis matrix correspodning
67 * to the initial basis is singular within the working precision.
70 * Unable to start the search, because the basis matrix correspodning
71 * to the initial basis is ill-conditioned, i.e. its condition number
75 * Unable to start the search, because some double-bounded variables
76 * have incorrect bounds.
79 * The search was prematurely terminated due to the solver failure.
82 * The search was prematurely terminated, because the objective
83 * function being maximized has reached its lower limit and continues
84 * decreasing (dual simplex only).
87 * The search was prematurely terminated, because the objective
88 * function being minimized has reached its upper limit and continues
89 * increasing (dual simplex only).
92 * The search was prematurely terminated, because the simplex
93 * iteration limit has been exceeded.
96 * The search was prematurely terminated, because the time limit has
100 * The LP problem instance has no primal feasible solution (only if
101 * the LP presolver is used).
104 * The LP problem instance has no dual feasible solution (only if the
105 * LP presolver is used). */
107 static void trivial_lp(glp_prob *P, const glp_smcp *parm)
108 { /* solve trivial LP which has empty constraint matrix */
112 double p_infeas, d_infeas, zeta;
114 P->pbs_stat = P->dbs_stat = GLP_FEAS;
117 p_infeas = d_infeas = 0.0;
118 /* make all auxiliary variables basic */
119 for (i = 1; i <= P->m; i++)
122 row->prim = row->dual = 0.0;
123 /* check primal feasibility */
124 if (row->type == GLP_LO || row->type == GLP_DB ||
126 { /* row has lower bound */
127 if (row->lb > + parm->tol_bnd)
128 { P->pbs_stat = GLP_NOFEAS;
129 if (P->some == 0 && parm->meth != GLP_PRIMAL)
132 if (p_infeas < + row->lb)
133 p_infeas = + row->lb;
135 if (row->type == GLP_UP || row->type == GLP_DB ||
137 { /* row has upper bound */
138 if (row->ub < - parm->tol_bnd)
139 { P->pbs_stat = GLP_NOFEAS;
140 if (P->some == 0 && parm->meth != GLP_PRIMAL)
143 if (p_infeas < - row->ub)
144 p_infeas = - row->ub;
147 /* determine scale factor for the objective row */
149 for (j = 1; j <= P->n; j++)
151 if (zeta < fabs(col->coef)) zeta = fabs(col->coef);
153 zeta = (P->dir == GLP_MIN ? +1.0 : -1.0) / zeta;
154 /* make all structural variables non-basic */
155 for (j = 1; j <= P->n; j++)
157 if (col->type == GLP_FR)
158 col->stat = GLP_NF, col->prim = 0.0;
159 else if (col->type == GLP_LO)
160 lo: col->stat = GLP_NL, col->prim = col->lb;
161 else if (col->type == GLP_UP)
162 up: col->stat = GLP_NU, col->prim = col->ub;
163 else if (col->type == GLP_DB)
164 { if (zeta * col->coef > 0.0)
166 else if (zeta * col->coef < 0.0)
168 else if (fabs(col->lb) <= fabs(col->ub))
173 else if (col->type == GLP_FX)
174 col->stat = GLP_NS, col->prim = col->lb;
175 col->dual = col->coef;
176 P->obj_val += col->coef * col->prim;
177 /* check dual feasibility */
178 if (col->type == GLP_FR || col->type == GLP_LO)
179 { /* column has no upper bound */
180 if (zeta * col->dual < - parm->tol_dj)
181 { P->dbs_stat = GLP_NOFEAS;
182 if (P->some == 0 && parm->meth == GLP_PRIMAL)
185 if (d_infeas < - zeta * col->dual)
186 d_infeas = - zeta * col->dual;
188 if (col->type == GLP_FR || col->type == GLP_UP)
189 { /* column has no lower bound */
190 if (zeta * col->dual > + parm->tol_dj)
191 { P->dbs_stat = GLP_NOFEAS;
192 if (P->some == 0 && parm->meth == GLP_PRIMAL)
195 if (d_infeas < + zeta * col->dual)
196 d_infeas = + zeta * col->dual;
199 /* simulate the simplex solver output */
200 if (parm->msg_lev >= GLP_MSG_ON && parm->out_dly == 0)
201 { xprintf("~%6d: obj = %17.9e infeas = %10.3e\n", P->it_cnt,
202 P->obj_val, parm->meth == GLP_PRIMAL ? p_infeas : d_infeas);
204 if (parm->msg_lev >= GLP_MSG_ALL && parm->out_dly == 0)
205 { if (P->pbs_stat == GLP_FEAS && P->dbs_stat == GLP_FEAS)
206 xprintf("OPTIMAL SOLUTION FOUND\n");
207 else if (P->pbs_stat == GLP_NOFEAS)
208 xprintf("PROBLEM HAS NO FEASIBLE SOLUTION\n");
209 else if (parm->meth == GLP_PRIMAL)
210 xprintf("PROBLEM HAS UNBOUNDED SOLUTION\n");
212 xprintf("PROBLEM HAS NO DUAL FEASIBLE SOLUTION\n");
217 static int solve_lp(glp_prob *P, const glp_smcp *parm)
218 { /* solve LP directly without using the preprocessor */
220 if (!glp_bf_exists(P))
221 { ret = glp_factorize(P);
224 else if (ret == GLP_EBADB)
225 { if (parm->msg_lev >= GLP_MSG_ERR)
226 xprintf("glp_simplex: initial basis is invalid\n");
228 else if (ret == GLP_ESING)
229 { if (parm->msg_lev >= GLP_MSG_ERR)
230 xprintf("glp_simplex: initial basis is singular\n");
232 else if (ret == GLP_ECOND)
233 { if (parm->msg_lev >= GLP_MSG_ERR)
235 "glp_simplex: initial basis is ill-conditioned\n");
239 if (ret != 0) goto done;
241 if (parm->meth == GLP_PRIMAL)
242 ret = spx_primal(P, parm);
243 else if (parm->meth == GLP_DUALP)
244 { ret = spx_dual(P, parm);
245 if (ret == GLP_EFAIL && P->valid)
246 ret = spx_primal(P, parm);
248 else if (parm->meth == GLP_DUAL)
249 ret = spx_dual(P, parm);
251 xassert(parm != parm);
255 static int preprocess_and_solve_lp(glp_prob *P, const glp_smcp *parm)
256 { /* solve LP using the preprocessor */
261 if (parm->msg_lev >= GLP_MSG_ALL)
262 xprintf("Preprocessing...\n");
263 /* create preprocessor workspace */
264 npp = npp_create_wksp();
265 /* load original problem into the preprocessor workspace */
266 npp_load_prob(npp, P, GLP_OFF, GLP_SOL, GLP_OFF);
267 /* process LP prior to applying primal/dual simplex method */
268 ret = npp_simplex(npp, parm);
271 else if (ret == GLP_ENOPFS)
272 { if (parm->msg_lev >= GLP_MSG_ALL)
273 xprintf("PROBLEM HAS NO PRIMAL FEASIBLE SOLUTION\n");
275 else if (ret == GLP_ENODFS)
276 { if (parm->msg_lev >= GLP_MSG_ALL)
277 xprintf("PROBLEM HAS NO DUAL FEASIBLE SOLUTION\n");
281 if (ret != 0) goto done;
282 /* build transformed LP */
283 lp = glp_create_prob();
284 npp_build_prob(npp, lp);
285 /* if the transformed LP is empty, it has empty solution, which
287 if (lp->m == 0 && lp->n == 0)
288 { lp->pbs_stat = lp->dbs_stat = GLP_FEAS;
289 lp->obj_val = lp->c0;
290 if (parm->msg_lev >= GLP_MSG_ON && parm->out_dly == 0)
291 { xprintf("~%6d: obj = %17.9e infeas = %10.3e\n", P->it_cnt,
294 if (parm->msg_lev >= GLP_MSG_ALL)
295 xprintf("OPTIMAL SOLUTION FOUND BY LP PREPROCESSOR\n");
298 if (parm->msg_lev >= GLP_MSG_ALL)
299 { xprintf("%d row%s, %d column%s, %d non-zero%s\n",
300 lp->m, lp->m == 1 ? "" : "s", lp->n, lp->n == 1 ? "" : "s",
301 lp->nnz, lp->nnz == 1 ? "" : "s");
303 /* inherit basis factorization control parameters */
304 glp_get_bfcp(P, &bfcp);
305 glp_set_bfcp(lp, &bfcp);
306 /* scale the transformed problem */
307 { ENV *env = get_env_ptr();
308 int term_out = env->term_out;
309 if (!term_out || parm->msg_lev < GLP_MSG_ALL)
310 env->term_out = GLP_OFF;
312 env->term_out = GLP_ON;
313 glp_scale_prob(lp, GLP_SF_AUTO);
314 env->term_out = term_out;
316 /* build advanced initial basis */
317 { ENV *env = get_env_ptr();
318 int term_out = env->term_out;
319 if (!term_out || parm->msg_lev < GLP_MSG_ALL)
320 env->term_out = GLP_OFF;
322 env->term_out = GLP_ON;
323 glp_adv_basis(lp, 0);
324 env->term_out = term_out;
326 /* solve the transformed LP */
327 lp->it_cnt = P->it_cnt;
328 ret = solve_lp(lp, parm);
329 P->it_cnt = lp->it_cnt;
330 /* only optimal solution can be postprocessed */
331 if (!(ret == 0 && lp->pbs_stat == GLP_FEAS && lp->dbs_stat ==
333 { if (parm->msg_lev >= GLP_MSG_ERR)
334 xprintf("glp_simplex: unable to recover undefined or non-op"
337 { if (lp->pbs_stat == GLP_NOFEAS)
339 else if (lp->dbs_stat == GLP_NOFEAS)
346 post: /* postprocess solution from the transformed LP */
347 npp_postprocess(npp, lp);
348 /* the transformed LP is no longer needed */
349 glp_delete_prob(lp), lp = NULL;
350 /* store solution to the original problem */
351 npp_unload_sol(npp, P);
352 /* the original LP has been successfully solved */
354 done: /* delete the transformed LP, if it exists */
355 if (lp != NULL) glp_delete_prob(lp);
356 /* delete preprocessor workspace */
357 npp_delete_wksp(npp);
361 int glp_simplex(glp_prob *P, const glp_smcp *parm)
362 { /* solve LP problem with the simplex method */
365 /* check problem object */
366 if (P == NULL || P->magic != GLP_PROB_MAGIC)
367 xerror("glp_simplex: P = %p; invalid problem object\n", P);
368 if (P->tree != NULL && P->tree->reason != 0)
369 xerror("glp_simplex: operation not allowed\n");
370 /* check control parameters */
372 parm = &_parm, glp_init_smcp((glp_smcp *)parm);
373 if (!(parm->msg_lev == GLP_MSG_OFF ||
374 parm->msg_lev == GLP_MSG_ERR ||
375 parm->msg_lev == GLP_MSG_ON ||
376 parm->msg_lev == GLP_MSG_ALL ||
377 parm->msg_lev == GLP_MSG_DBG))
378 xerror("glp_simplex: msg_lev = %d; invalid parameter\n",
380 if (!(parm->meth == GLP_PRIMAL ||
381 parm->meth == GLP_DUALP ||
382 parm->meth == GLP_DUAL))
383 xerror("glp_simplex: meth = %d; invalid parameter\n",
385 if (!(parm->pricing == GLP_PT_STD ||
386 parm->pricing == GLP_PT_PSE))
387 xerror("glp_simplex: pricing = %d; invalid parameter\n",
389 if (!(parm->r_test == GLP_RT_STD ||
390 parm->r_test == GLP_RT_HAR))
391 xerror("glp_simplex: r_test = %d; invalid parameter\n",
393 if (!(0.0 < parm->tol_bnd && parm->tol_bnd < 1.0))
394 xerror("glp_simplex: tol_bnd = %g; invalid parameter\n",
396 if (!(0.0 < parm->tol_dj && parm->tol_dj < 1.0))
397 xerror("glp_simplex: tol_dj = %g; invalid parameter\n",
399 if (!(0.0 < parm->tol_piv && parm->tol_piv < 1.0))
400 xerror("glp_simplex: tol_piv = %g; invalid parameter\n",
402 if (parm->it_lim < 0)
403 xerror("glp_simplex: it_lim = %d; invalid parameter\n",
405 if (parm->tm_lim < 0)
406 xerror("glp_simplex: tm_lim = %d; invalid parameter\n",
408 if (parm->out_frq < 1)
409 xerror("glp_simplex: out_frq = %d; invalid parameter\n",
411 if (parm->out_dly < 0)
412 xerror("glp_simplex: out_dly = %d; invalid parameter\n",
414 if (!(parm->presolve == GLP_ON || parm->presolve == GLP_OFF))
415 xerror("glp_simplex: presolve = %d; invalid parameter\n",
417 /* basic solution is currently undefined */
418 P->pbs_stat = P->dbs_stat = GLP_UNDEF;
421 /* check bounds of double-bounded variables */
422 for (i = 1; i <= P->m; i++)
423 { GLPROW *row = P->row[i];
424 if (row->type == GLP_DB && row->lb >= row->ub)
425 { if (parm->msg_lev >= GLP_MSG_ERR)
426 xprintf("glp_simplex: row %d: lb = %g, ub = %g; incorrec"
427 "t bounds\n", i, row->lb, row->ub);
432 for (j = 1; j <= P->n; j++)
433 { GLPCOL *col = P->col[j];
434 if (col->type == GLP_DB && col->lb >= col->ub)
435 { if (parm->msg_lev >= GLP_MSG_ERR)
436 xprintf("glp_simplex: column %d: lb = %g, ub = %g; incor"
437 "rect bounds\n", j, col->lb, col->ub);
442 /* solve LP problem */
443 if (parm->msg_lev >= GLP_MSG_ALL)
444 { xprintf("GLPK Simplex Optimizer, v%s\n", glp_version());
445 xprintf("%d row%s, %d column%s, %d non-zero%s\n",
446 P->m, P->m == 1 ? "" : "s", P->n, P->n == 1 ? "" : "s",
447 P->nnz, P->nnz == 1 ? "" : "s");
450 trivial_lp(P, parm), ret = 0;
451 else if (!parm->presolve)
452 ret = solve_lp(P, parm);
454 ret = preprocess_and_solve_lp(P, parm);
455 done: /* return to the application program */
459 /***********************************************************************
462 * glp_init_smcp - initialize simplex method control parameters
466 * void glp_init_smcp(glp_smcp *parm);
470 * The routine glp_init_smcp initializes control parameters, which are
471 * used by the simplex solver, with default values.
473 * Default values of the control parameters are stored in a glp_smcp
474 * structure, which the parameter parm points to. */
476 void glp_init_smcp(glp_smcp *parm)
477 { parm->msg_lev = GLP_MSG_ALL;
478 parm->meth = GLP_PRIMAL;
479 parm->pricing = GLP_PT_PSE;
480 parm->r_test = GLP_RT_HAR;
481 parm->tol_bnd = 1e-7;
483 parm->tol_piv = 1e-10;
484 parm->obj_ll = -DBL_MAX;
485 parm->obj_ul = +DBL_MAX;
486 parm->it_lim = INT_MAX;
487 parm->tm_lim = INT_MAX;
490 parm->presolve = GLP_OFF;
494 /***********************************************************************
497 * glp_get_status - retrieve generic status of basic solution
501 * int glp_get_status(glp_prob *lp);
505 * The routine glp_get_status reports the generic status of the basic
506 * solution for the specified problem object as follows:
508 * GLP_OPT - solution is optimal;
509 * GLP_FEAS - solution is feasible;
510 * GLP_INFEAS - solution is infeasible;
511 * GLP_NOFEAS - problem has no feasible solution;
512 * GLP_UNBND - problem has unbounded solution;
513 * GLP_UNDEF - solution is undefined. */
515 int glp_get_status(glp_prob *lp)
517 status = glp_get_prim_stat(lp);
520 switch (glp_get_dual_stat(lp))
546 /***********************************************************************
549 * glp_get_prim_stat - retrieve status of primal basic solution
553 * int glp_get_prim_stat(glp_prob *lp);
557 * The routine glp_get_prim_stat reports the status of the primal basic
558 * solution for the specified problem object as follows:
560 * GLP_UNDEF - primal solution is undefined;
561 * GLP_FEAS - primal solution is feasible;
562 * GLP_INFEAS - primal solution is infeasible;
563 * GLP_NOFEAS - no primal feasible solution exists. */
565 int glp_get_prim_stat(glp_prob *lp)
566 { int pbs_stat = lp->pbs_stat;
570 /***********************************************************************
573 * glp_get_dual_stat - retrieve status of dual basic solution
577 * int glp_get_dual_stat(glp_prob *lp);
581 * The routine glp_get_dual_stat reports the status of the dual basic
582 * solution for the specified problem object as follows:
584 * GLP_UNDEF - dual solution is undefined;
585 * GLP_FEAS - dual solution is feasible;
586 * GLP_INFEAS - dual solution is infeasible;
587 * GLP_NOFEAS - no dual feasible solution exists. */
589 int glp_get_dual_stat(glp_prob *lp)
590 { int dbs_stat = lp->dbs_stat;
594 /***********************************************************************
597 * glp_get_obj_val - retrieve objective value (basic solution)
601 * double glp_get_obj_val(glp_prob *lp);
605 * The routine glp_get_obj_val returns value of the objective function
606 * for basic solution. */
608 double glp_get_obj_val(glp_prob *lp)
609 { /*struct LPXCPS *cps = lp->cps;*/
612 /*if (cps->round && fabs(z) < 1e-9) z = 0.0;*/
616 /***********************************************************************
619 * glp_get_row_stat - retrieve row status
623 * int glp_get_row_stat(glp_prob *lp, int i);
627 * The routine glp_get_row_stat returns current status assigned to the
628 * auxiliary variable associated with i-th row as follows:
630 * GLP_BS - basic variable;
631 * GLP_NL - non-basic variable on its lower bound;
632 * GLP_NU - non-basic variable on its upper bound;
633 * GLP_NF - non-basic free (unbounded) variable;
634 * GLP_NS - non-basic fixed variable. */
636 int glp_get_row_stat(glp_prob *lp, int i)
637 { if (!(1 <= i && i <= lp->m))
638 xerror("glp_get_row_stat: i = %d; row number out of range\n",
640 return lp->row[i]->stat;
643 /***********************************************************************
646 * glp_get_row_prim - retrieve row primal value (basic solution)
650 * double glp_get_row_prim(glp_prob *lp, int i);
654 * The routine glp_get_row_prim returns primal value of the auxiliary
655 * variable associated with i-th row. */
657 double glp_get_row_prim(glp_prob *lp, int i)
658 { /*struct LPXCPS *cps = lp->cps;*/
660 if (!(1 <= i && i <= lp->m))
661 xerror("glp_get_row_prim: i = %d; row number out of range\n",
663 prim = lp->row[i]->prim;
664 /*if (cps->round && fabs(prim) < 1e-9) prim = 0.0;*/
668 /***********************************************************************
671 * glp_get_row_dual - retrieve row dual value (basic solution)
675 * double glp_get_row_dual(glp_prob *lp, int i);
679 * The routine glp_get_row_dual returns dual value (i.e. reduced cost)
680 * of the auxiliary variable associated with i-th row. */
682 double glp_get_row_dual(glp_prob *lp, int i)
683 { /*struct LPXCPS *cps = lp->cps;*/
685 if (!(1 <= i && i <= lp->m))
686 xerror("glp_get_row_dual: i = %d; row number out of range\n",
688 dual = lp->row[i]->dual;
689 /*if (cps->round && fabs(dual) < 1e-9) dual = 0.0;*/
693 /***********************************************************************
696 * glp_get_col_stat - retrieve column status
700 * int glp_get_col_stat(glp_prob *lp, int j);
704 * The routine glp_get_col_stat returns current status assigned to the
705 * structural variable associated with j-th column as follows:
707 * GLP_BS - basic variable;
708 * GLP_NL - non-basic variable on its lower bound;
709 * GLP_NU - non-basic variable on its upper bound;
710 * GLP_NF - non-basic free (unbounded) variable;
711 * GLP_NS - non-basic fixed variable. */
713 int glp_get_col_stat(glp_prob *lp, int j)
714 { if (!(1 <= j && j <= lp->n))
715 xerror("glp_get_col_stat: j = %d; column number out of range\n"
717 return lp->col[j]->stat;
720 /***********************************************************************
723 * glp_get_col_prim - retrieve column primal value (basic solution)
727 * double glp_get_col_prim(glp_prob *lp, int j);
731 * The routine glp_get_col_prim returns primal value of the structural
732 * variable associated with j-th column. */
734 double glp_get_col_prim(glp_prob *lp, int j)
735 { /*struct LPXCPS *cps = lp->cps;*/
737 if (!(1 <= j && j <= lp->n))
738 xerror("glp_get_col_prim: j = %d; column number out of range\n"
740 prim = lp->col[j]->prim;
741 /*if (cps->round && fabs(prim) < 1e-9) prim = 0.0;*/
745 /***********************************************************************
748 * glp_get_col_dual - retrieve column dual value (basic solution)
752 * double glp_get_col_dual(glp_prob *lp, int j);
756 * The routine glp_get_col_dual returns dual value (i.e. reduced cost)
757 * of the structural variable associated with j-th column. */
759 double glp_get_col_dual(glp_prob *lp, int j)
760 { /*struct LPXCPS *cps = lp->cps;*/
762 if (!(1 <= j && j <= lp->n))
763 xerror("glp_get_col_dual: j = %d; column number out of range\n"
765 dual = lp->col[j]->dual;
766 /*if (cps->round && fabs(dual) < 1e-9) dual = 0.0;*/
770 /***********************************************************************
773 * glp_get_unbnd_ray - determine variable causing unboundedness
777 * int glp_get_unbnd_ray(glp_prob *lp);
781 * The routine glp_get_unbnd_ray returns the number k of a variable,
782 * which causes primal or dual unboundedness. If 1 <= k <= m, it is
783 * k-th auxiliary variable, and if m+1 <= k <= m+n, it is (k-m)-th
784 * structural variable, where m is the number of rows, n is the number
785 * of columns in the problem object. If such variable is not defined,
786 * the routine returns 0.
790 * If it is not exactly known which version of the simplex solver
791 * detected unboundedness, i.e. whether the unboundedness is primal or
792 * dual, it is sufficient to check the status of the variable reported
793 * with the routine glp_get_row_stat or glp_get_col_stat. If the
794 * variable is non-basic, the unboundedness is primal, otherwise, if
795 * the variable is basic, the unboundedness is dual (the latter case
796 * means that the problem has no primal feasible dolution). */
798 int glp_get_unbnd_ray(glp_prob *lp)
802 if (k > lp->m + lp->n) k = 0;