alpar@1: /* glpapi08.c (interior-point method routines) */ alpar@1: alpar@1: /*********************************************************************** alpar@1: * This code is part of GLPK (GNU Linear Programming Kit). alpar@1: * alpar@1: * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, alpar@1: * 2009, 2010 Andrew Makhorin, Department for Applied Informatics, alpar@1: * Moscow Aviation Institute, Moscow, Russia. All rights reserved. alpar@1: * E-mail: . alpar@1: * alpar@1: * GLPK is free software: you can redistribute it and/or modify it alpar@1: * under the terms of the GNU General Public License as published by alpar@1: * the Free Software Foundation, either version 3 of the License, or alpar@1: * (at your option) any later version. alpar@1: * alpar@1: * GLPK is distributed in the hope that it will be useful, but WITHOUT alpar@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY alpar@1: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public alpar@1: * License for more details. alpar@1: * alpar@1: * You should have received a copy of the GNU General Public License alpar@1: * along with GLPK. If not, see . alpar@1: ***********************************************************************/ alpar@1: alpar@1: #include "glpapi.h" alpar@1: #include "glpipm.h" alpar@1: #include "glpnpp.h" alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * glp_interior - solve LP problem with the interior-point method alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * int glp_interior(glp_prob *P, const glp_iptcp *parm); alpar@1: * alpar@1: * The routine glp_interior is a driver to the LP solver based on the alpar@1: * interior-point method. alpar@1: * alpar@1: * The interior-point solver has a set of control parameters. Values of alpar@1: * the control parameters can be passed in a structure glp_iptcp, which alpar@1: * the parameter parm points to. alpar@1: * alpar@1: * Currently this routine implements an easy variant of the primal-dual alpar@1: * interior-point method based on Mehrotra's technique. alpar@1: * alpar@1: * This routine transforms the original LP problem to an equivalent LP alpar@1: * problem in the standard formulation (all constraints are equalities, alpar@1: * all variables are non-negative), calls the routine ipm_main to solve alpar@1: * the transformed problem, and then transforms an obtained solution to alpar@1: * the solution of the original problem. alpar@1: * alpar@1: * RETURNS alpar@1: * alpar@1: * 0 The LP problem instance has been successfully solved. This code alpar@1: * does not necessarily mean that the solver has found optimal alpar@1: * solution. It only means that the solution process was successful. alpar@1: * alpar@1: * GLP_EFAIL alpar@1: * The problem has no rows/columns. alpar@1: * alpar@1: * GLP_ENOCVG alpar@1: * Very slow convergence or divergence. alpar@1: * alpar@1: * GLP_EITLIM alpar@1: * Iteration limit exceeded. alpar@1: * alpar@1: * GLP_EINSTAB alpar@1: * Numerical instability on solving Newtonian system. */ alpar@1: alpar@1: static void transform(NPP *npp) alpar@1: { /* transform LP to the standard formulation */ alpar@1: NPPROW *row, *prev_row; alpar@1: NPPCOL *col, *prev_col; alpar@1: for (row = npp->r_tail; row != NULL; row = prev_row) alpar@1: { prev_row = row->prev; alpar@1: if (row->lb == -DBL_MAX && row->ub == +DBL_MAX) alpar@1: npp_free_row(npp, row); alpar@1: else if (row->lb == -DBL_MAX) alpar@1: npp_leq_row(npp, row); alpar@1: else if (row->ub == +DBL_MAX) alpar@1: npp_geq_row(npp, row); alpar@1: else if (row->lb != row->ub) alpar@1: { if (fabs(row->lb) < fabs(row->ub)) alpar@1: npp_geq_row(npp, row); alpar@1: else alpar@1: npp_leq_row(npp, row); alpar@1: } alpar@1: } alpar@1: for (col = npp->c_tail; col != NULL; col = prev_col) alpar@1: { prev_col = col->prev; alpar@1: if (col->lb == -DBL_MAX && col->ub == +DBL_MAX) alpar@1: npp_free_col(npp, col); alpar@1: else if (col->lb == -DBL_MAX) alpar@1: npp_ubnd_col(npp, col); alpar@1: else if (col->ub == +DBL_MAX) alpar@1: { if (col->lb != 0.0) alpar@1: npp_lbnd_col(npp, col); alpar@1: } alpar@1: else if (col->lb != col->ub) alpar@1: { if (fabs(col->lb) < fabs(col->ub)) alpar@1: { if (col->lb != 0.0) alpar@1: npp_lbnd_col(npp, col); alpar@1: } alpar@1: else alpar@1: npp_ubnd_col(npp, col); alpar@1: npp_dbnd_col(npp, col); alpar@1: } alpar@1: else alpar@1: npp_fixed_col(npp, col); alpar@1: } alpar@1: for (row = npp->r_head; row != NULL; row = row->next) alpar@1: xassert(row->lb == row->ub); alpar@1: for (col = npp->c_head; col != NULL; col = col->next) alpar@1: xassert(col->lb == 0.0 && col->ub == +DBL_MAX); alpar@1: return; alpar@1: } alpar@1: alpar@1: int glp_interior(glp_prob *P, const glp_iptcp *parm) alpar@1: { glp_iptcp _parm; alpar@1: GLPROW *row; alpar@1: GLPCOL *col; alpar@1: NPP *npp = NULL; alpar@1: glp_prob *prob = NULL; alpar@1: int i, j, ret; alpar@1: /* check control parameters */ alpar@1: if (parm == NULL) alpar@1: glp_init_iptcp(&_parm), parm = &_parm; alpar@1: if (!(parm->msg_lev == GLP_MSG_OFF || alpar@1: parm->msg_lev == GLP_MSG_ERR || alpar@1: parm->msg_lev == GLP_MSG_ON || alpar@1: parm->msg_lev == GLP_MSG_ALL)) alpar@1: xerror("glp_interior: msg_lev = %d; invalid parameter\n", alpar@1: parm->msg_lev); alpar@1: if (!(parm->ord_alg == GLP_ORD_NONE || alpar@1: parm->ord_alg == GLP_ORD_QMD || alpar@1: parm->ord_alg == GLP_ORD_AMD || alpar@1: parm->ord_alg == GLP_ORD_SYMAMD)) alpar@1: xerror("glp_interior: ord_alg = %d; invalid parameter\n", alpar@1: parm->ord_alg); alpar@1: /* interior-point solution is currently undefined */ alpar@1: P->ipt_stat = GLP_UNDEF; alpar@1: P->ipt_obj = 0.0; alpar@1: /* check bounds of double-bounded variables */ alpar@1: for (i = 1; i <= P->m; i++) alpar@1: { row = P->row[i]; alpar@1: if (row->type == GLP_DB && row->lb >= row->ub) alpar@1: { if (parm->msg_lev >= GLP_MSG_ERR) alpar@1: xprintf("glp_interior: row %d: lb = %g, ub = %g; incorre" alpar@1: "ct bounds\n", i, row->lb, row->ub); alpar@1: ret = GLP_EBOUND; alpar@1: goto done; alpar@1: } alpar@1: } alpar@1: for (j = 1; j <= P->n; j++) alpar@1: { col = P->col[j]; alpar@1: if (col->type == GLP_DB && col->lb >= col->ub) alpar@1: { if (parm->msg_lev >= GLP_MSG_ERR) alpar@1: xprintf("glp_interior: column %d: lb = %g, ub = %g; inco" alpar@1: "rrect bounds\n", j, col->lb, col->ub); alpar@1: ret = GLP_EBOUND; alpar@1: goto done; alpar@1: } alpar@1: } alpar@1: /* transform LP to the standard formulation */ alpar@1: if (parm->msg_lev >= GLP_MSG_ALL) alpar@1: xprintf("Original LP has %d row(s), %d column(s), and %d non-z" alpar@1: "ero(s)\n", P->m, P->n, P->nnz); alpar@1: npp = npp_create_wksp(); alpar@1: npp_load_prob(npp, P, GLP_OFF, GLP_IPT, GLP_ON); alpar@1: transform(npp); alpar@1: prob = glp_create_prob(); alpar@1: npp_build_prob(npp, prob); alpar@1: if (parm->msg_lev >= GLP_MSG_ALL) alpar@1: xprintf("Working LP has %d row(s), %d column(s), and %d non-ze" alpar@1: "ro(s)\n", prob->m, prob->n, prob->nnz); alpar@1: #if 1 alpar@1: /* currently empty problem cannot be solved */ alpar@1: if (!(prob->m > 0 && prob->n > 0)) alpar@1: { if (parm->msg_lev >= GLP_MSG_ERR) alpar@1: xprintf("glp_interior: unable to solve empty problem\n"); alpar@1: ret = GLP_EFAIL; alpar@1: goto done; alpar@1: } alpar@1: #endif alpar@1: /* scale the resultant LP */ alpar@1: { ENV *env = get_env_ptr(); alpar@1: int term_out = env->term_out; alpar@1: env->term_out = GLP_OFF; alpar@1: glp_scale_prob(prob, GLP_SF_EQ); alpar@1: env->term_out = term_out; alpar@1: } alpar@1: /* warn about dense columns */ alpar@1: if (parm->msg_lev >= GLP_MSG_ON && prob->m >= 200) alpar@1: { int len, cnt = 0; alpar@1: for (j = 1; j <= prob->n; j++) alpar@1: { len = glp_get_mat_col(prob, j, NULL, NULL); alpar@1: if ((double)len >= 0.20 * (double)prob->m) cnt++; alpar@1: } alpar@1: if (cnt == 1) alpar@1: xprintf("WARNING: PROBLEM HAS ONE DENSE COLUMN\n"); alpar@1: else if (cnt > 0) alpar@1: xprintf("WARNING: PROBLEM HAS %d DENSE COLUMNS\n", cnt); alpar@1: } alpar@1: /* solve the transformed LP */ alpar@1: ret = ipm_solve(prob, parm); alpar@1: /* postprocess solution from the transformed LP */ alpar@1: npp_postprocess(npp, prob); alpar@1: /* and store solution to the original LP */ alpar@1: npp_unload_sol(npp, P); alpar@1: done: /* free working program objects */ alpar@1: if (npp != NULL) npp_delete_wksp(npp); alpar@1: if (prob != NULL) glp_delete_prob(prob); alpar@1: /* return to the application program */ alpar@1: return ret; alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * glp_init_iptcp - initialize interior-point solver control parameters alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * void glp_init_iptcp(glp_iptcp *parm); alpar@1: * alpar@1: * DESCRIPTION alpar@1: * alpar@1: * The routine glp_init_iptcp initializes control parameters, which are alpar@1: * used by the interior-point solver, with default values. alpar@1: * alpar@1: * Default values of the control parameters are stored in the glp_iptcp alpar@1: * structure, which the parameter parm points to. */ alpar@1: alpar@1: void glp_init_iptcp(glp_iptcp *parm) alpar@1: { parm->msg_lev = GLP_MSG_ALL; alpar@1: parm->ord_alg = GLP_ORD_AMD; alpar@1: return; alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * glp_ipt_status - retrieve status of interior-point solution alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * int glp_ipt_status(glp_prob *lp); alpar@1: * alpar@1: * RETURNS alpar@1: * alpar@1: * The routine glp_ipt_status reports the status of solution found by alpar@1: * the interior-point solver as follows: alpar@1: * alpar@1: * GLP_UNDEF - interior-point solution is undefined; alpar@1: * GLP_OPT - interior-point solution is optimal; alpar@1: * GLP_INFEAS - interior-point solution is infeasible; alpar@1: * GLP_NOFEAS - no feasible solution exists. */ alpar@1: alpar@1: int glp_ipt_status(glp_prob *lp) alpar@1: { int ipt_stat = lp->ipt_stat; alpar@1: return ipt_stat; alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * glp_ipt_obj_val - retrieve objective value (interior point) alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * double glp_ipt_obj_val(glp_prob *lp); alpar@1: * alpar@1: * RETURNS alpar@1: * alpar@1: * The routine glp_ipt_obj_val returns value of the objective function alpar@1: * for interior-point solution. */ alpar@1: alpar@1: double glp_ipt_obj_val(glp_prob *lp) alpar@1: { /*struct LPXCPS *cps = lp->cps;*/ alpar@1: double z; alpar@1: z = lp->ipt_obj; alpar@1: /*if (cps->round && fabs(z) < 1e-9) z = 0.0;*/ alpar@1: return z; alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * glp_ipt_row_prim - retrieve row primal value (interior point) alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * double glp_ipt_row_prim(glp_prob *lp, int i); alpar@1: * alpar@1: * RETURNS alpar@1: * alpar@1: * The routine glp_ipt_row_prim returns primal value of the auxiliary alpar@1: * variable associated with i-th row. */ alpar@1: alpar@1: double glp_ipt_row_prim(glp_prob *lp, int i) alpar@1: { /*struct LPXCPS *cps = lp->cps;*/ alpar@1: double pval; alpar@1: if (!(1 <= i && i <= lp->m)) alpar@1: xerror("glp_ipt_row_prim: i = %d; row number out of range\n", alpar@1: i); alpar@1: pval = lp->row[i]->pval; alpar@1: /*if (cps->round && fabs(pval) < 1e-9) pval = 0.0;*/ alpar@1: return pval; alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * glp_ipt_row_dual - retrieve row dual value (interior point) alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * double glp_ipt_row_dual(glp_prob *lp, int i); alpar@1: * alpar@1: * RETURNS alpar@1: * alpar@1: * The routine glp_ipt_row_dual returns dual value (i.e. reduced cost) alpar@1: * of the auxiliary variable associated with i-th row. */ alpar@1: alpar@1: double glp_ipt_row_dual(glp_prob *lp, int i) alpar@1: { /*struct LPXCPS *cps = lp->cps;*/ alpar@1: double dval; alpar@1: if (!(1 <= i && i <= lp->m)) alpar@1: xerror("glp_ipt_row_dual: i = %d; row number out of range\n", alpar@1: i); alpar@1: dval = lp->row[i]->dval; alpar@1: /*if (cps->round && fabs(dval) < 1e-9) dval = 0.0;*/ alpar@1: return dval; alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * glp_ipt_col_prim - retrieve column primal value (interior point) alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * double glp_ipt_col_prim(glp_prob *lp, int j); alpar@1: * alpar@1: * RETURNS alpar@1: * alpar@1: * The routine glp_ipt_col_prim returns primal value of the structural alpar@1: * variable associated with j-th column. */ alpar@1: alpar@1: double glp_ipt_col_prim(glp_prob *lp, int j) alpar@1: { /*struct LPXCPS *cps = lp->cps;*/ alpar@1: double pval; alpar@1: if (!(1 <= j && j <= lp->n)) alpar@1: xerror("glp_ipt_col_prim: j = %d; column number out of range\n" alpar@1: , j); alpar@1: pval = lp->col[j]->pval; alpar@1: /*if (cps->round && fabs(pval) < 1e-9) pval = 0.0;*/ alpar@1: return pval; alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * glp_ipt_col_dual - retrieve column dual value (interior point) alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * #include "glplpx.h" alpar@1: * double glp_ipt_col_dual(glp_prob *lp, int j); alpar@1: * alpar@1: * RETURNS alpar@1: * alpar@1: * The routine glp_ipt_col_dual returns dual value (i.e. reduced cost) alpar@1: * of the structural variable associated with j-th column. */ alpar@1: alpar@1: double glp_ipt_col_dual(glp_prob *lp, int j) alpar@1: { /*struct LPXCPS *cps = lp->cps;*/ alpar@1: double dval; alpar@1: if (!(1 <= j && j <= lp->n)) alpar@1: xerror("glp_ipt_col_dual: j = %d; column number out of range\n" alpar@1: , j); alpar@1: dval = lp->col[j]->dval; alpar@1: /*if (cps->round && fabs(dval) < 1e-9) dval = 0.0;*/ alpar@1: return dval; alpar@1: } alpar@1: alpar@1: /* eof */