src/glpapi08.c
author Alpar Juttner <alpar@cs.elte.hu>
Sun, 05 Dec 2010 17:35:23 +0100
changeset 2 4c8956a7bdf4
permissions -rw-r--r--
Set up CMAKE build environment
alpar@1
     1
/* glpapi08.c (interior-point method routines) */
alpar@1
     2
alpar@1
     3
/***********************************************************************
alpar@1
     4
*  This code is part of GLPK (GNU Linear Programming Kit).
alpar@1
     5
*
alpar@1
     6
*  Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
alpar@1
     7
*  2009, 2010 Andrew Makhorin, Department for Applied Informatics,
alpar@1
     8
*  Moscow Aviation Institute, Moscow, Russia. All rights reserved.
alpar@1
     9
*  E-mail: <mao@gnu.org>.
alpar@1
    10
*
alpar@1
    11
*  GLPK is free software: you can redistribute it and/or modify it
alpar@1
    12
*  under the terms of the GNU General Public License as published by
alpar@1
    13
*  the Free Software Foundation, either version 3 of the License, or
alpar@1
    14
*  (at your option) any later version.
alpar@1
    15
*
alpar@1
    16
*  GLPK is distributed in the hope that it will be useful, but WITHOUT
alpar@1
    17
*  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
alpar@1
    18
*  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
alpar@1
    19
*  License for more details.
alpar@1
    20
*
alpar@1
    21
*  You should have received a copy of the GNU General Public License
alpar@1
    22
*  along with GLPK. If not, see <http://www.gnu.org/licenses/>.
alpar@1
    23
***********************************************************************/
alpar@1
    24
alpar@1
    25
#include "glpapi.h"
alpar@1
    26
#include "glpipm.h"
alpar@1
    27
#include "glpnpp.h"
alpar@1
    28
alpar@1
    29
/***********************************************************************
alpar@1
    30
*  NAME
alpar@1
    31
*
alpar@1
    32
*  glp_interior - solve LP problem with the interior-point method
alpar@1
    33
*
alpar@1
    34
*  SYNOPSIS
alpar@1
    35
*
alpar@1
    36
*  int glp_interior(glp_prob *P, const glp_iptcp *parm);
alpar@1
    37
*
alpar@1
    38
*  The routine glp_interior is a driver to the LP solver based on the
alpar@1
    39
*  interior-point method.
alpar@1
    40
*
alpar@1
    41
*  The interior-point solver has a set of control parameters. Values of
alpar@1
    42
*  the control parameters can be passed in a structure glp_iptcp, which
alpar@1
    43
*  the parameter parm points to.
alpar@1
    44
*
alpar@1
    45
*  Currently this routine implements an easy variant of the primal-dual
alpar@1
    46
*  interior-point method based on Mehrotra's technique.
alpar@1
    47
*
alpar@1
    48
*  This routine transforms the original LP problem to an equivalent LP
alpar@1
    49
*  problem in the standard formulation (all constraints are equalities,
alpar@1
    50
*  all variables are non-negative), calls the routine ipm_main to solve
alpar@1
    51
*  the transformed problem, and then transforms an obtained solution to
alpar@1
    52
*  the solution of the original problem.
alpar@1
    53
*
alpar@1
    54
*  RETURNS
alpar@1
    55
*
alpar@1
    56
*  0  The LP problem instance has been successfully solved. This code
alpar@1
    57
*     does not necessarily mean that the solver has found optimal
alpar@1
    58
*     solution. It only means that the solution process was successful.
alpar@1
    59
*
alpar@1
    60
*  GLP_EFAIL
alpar@1
    61
*     The problem has no rows/columns.
alpar@1
    62
*
alpar@1
    63
*  GLP_ENOCVG
alpar@1
    64
*     Very slow convergence or divergence.
alpar@1
    65
*
alpar@1
    66
*  GLP_EITLIM
alpar@1
    67
*     Iteration limit exceeded.
alpar@1
    68
*
alpar@1
    69
*  GLP_EINSTAB
alpar@1
    70
*     Numerical instability on solving Newtonian system. */
alpar@1
    71
alpar@1
    72
static void transform(NPP *npp)
alpar@1
    73
{     /* transform LP to the standard formulation */
alpar@1
    74
      NPPROW *row, *prev_row;
alpar@1
    75
      NPPCOL *col, *prev_col;
alpar@1
    76
      for (row = npp->r_tail; row != NULL; row = prev_row)
alpar@1
    77
      {  prev_row = row->prev;
alpar@1
    78
         if (row->lb == -DBL_MAX && row->ub == +DBL_MAX)
alpar@1
    79
            npp_free_row(npp, row);
alpar@1
    80
         else if (row->lb == -DBL_MAX)
alpar@1
    81
            npp_leq_row(npp, row);
alpar@1
    82
         else if (row->ub == +DBL_MAX)
alpar@1
    83
            npp_geq_row(npp, row);
alpar@1
    84
         else if (row->lb != row->ub)
alpar@1
    85
         {  if (fabs(row->lb) < fabs(row->ub))
alpar@1
    86
               npp_geq_row(npp, row);
alpar@1
    87
            else
alpar@1
    88
               npp_leq_row(npp, row);
alpar@1
    89
         }
alpar@1
    90
      }
alpar@1
    91
      for (col = npp->c_tail; col != NULL; col = prev_col)
alpar@1
    92
      {  prev_col = col->prev;
alpar@1
    93
         if (col->lb == -DBL_MAX && col->ub == +DBL_MAX)
alpar@1
    94
            npp_free_col(npp, col);
alpar@1
    95
         else if (col->lb == -DBL_MAX)
alpar@1
    96
            npp_ubnd_col(npp, col);
alpar@1
    97
         else if (col->ub == +DBL_MAX)
alpar@1
    98
         {  if (col->lb != 0.0)
alpar@1
    99
               npp_lbnd_col(npp, col);
alpar@1
   100
         }
alpar@1
   101
         else if (col->lb != col->ub)
alpar@1
   102
         {  if (fabs(col->lb) < fabs(col->ub))
alpar@1
   103
            {  if (col->lb != 0.0)
alpar@1
   104
                  npp_lbnd_col(npp, col);
alpar@1
   105
            }
alpar@1
   106
            else
alpar@1
   107
               npp_ubnd_col(npp, col);
alpar@1
   108
            npp_dbnd_col(npp, col);
alpar@1
   109
         }
alpar@1
   110
         else
alpar@1
   111
            npp_fixed_col(npp, col);
alpar@1
   112
      }
alpar@1
   113
      for (row = npp->r_head; row != NULL; row = row->next)
alpar@1
   114
         xassert(row->lb == row->ub);
alpar@1
   115
      for (col = npp->c_head; col != NULL; col = col->next)
alpar@1
   116
         xassert(col->lb == 0.0 && col->ub == +DBL_MAX);
alpar@1
   117
      return;
alpar@1
   118
}
alpar@1
   119
alpar@1
   120
int glp_interior(glp_prob *P, const glp_iptcp *parm)
alpar@1
   121
{     glp_iptcp _parm;
alpar@1
   122
      GLPROW *row;
alpar@1
   123
      GLPCOL *col;
alpar@1
   124
      NPP *npp = NULL;
alpar@1
   125
      glp_prob *prob = NULL;
alpar@1
   126
      int i, j, ret;
alpar@1
   127
      /* check control parameters */
alpar@1
   128
      if (parm == NULL)
alpar@1
   129
         glp_init_iptcp(&_parm), parm = &_parm;
alpar@1
   130
      if (!(parm->msg_lev == GLP_MSG_OFF ||
alpar@1
   131
            parm->msg_lev == GLP_MSG_ERR ||
alpar@1
   132
            parm->msg_lev == GLP_MSG_ON  ||
alpar@1
   133
            parm->msg_lev == GLP_MSG_ALL))
alpar@1
   134
         xerror("glp_interior: msg_lev = %d; invalid parameter\n",
alpar@1
   135
            parm->msg_lev);
alpar@1
   136
      if (!(parm->ord_alg == GLP_ORD_NONE ||
alpar@1
   137
            parm->ord_alg == GLP_ORD_QMD ||
alpar@1
   138
            parm->ord_alg == GLP_ORD_AMD ||
alpar@1
   139
            parm->ord_alg == GLP_ORD_SYMAMD))
alpar@1
   140
         xerror("glp_interior: ord_alg = %d; invalid parameter\n",
alpar@1
   141
            parm->ord_alg);
alpar@1
   142
      /* interior-point solution is currently undefined */
alpar@1
   143
      P->ipt_stat = GLP_UNDEF;
alpar@1
   144
      P->ipt_obj = 0.0;
alpar@1
   145
      /* check bounds of double-bounded variables */
alpar@1
   146
      for (i = 1; i <= P->m; i++)
alpar@1
   147
      {  row = P->row[i];
alpar@1
   148
         if (row->type == GLP_DB && row->lb >= row->ub)
alpar@1
   149
         {  if (parm->msg_lev >= GLP_MSG_ERR)
alpar@1
   150
               xprintf("glp_interior: row %d: lb = %g, ub = %g; incorre"
alpar@1
   151
                  "ct bounds\n", i, row->lb, row->ub);
alpar@1
   152
            ret = GLP_EBOUND;
alpar@1
   153
            goto done;
alpar@1
   154
         }
alpar@1
   155
      }
alpar@1
   156
      for (j = 1; j <= P->n; j++)
alpar@1
   157
      {  col = P->col[j];
alpar@1
   158
         if (col->type == GLP_DB && col->lb >= col->ub)
alpar@1
   159
         {  if (parm->msg_lev >= GLP_MSG_ERR)
alpar@1
   160
               xprintf("glp_interior: column %d: lb = %g, ub = %g; inco"
alpar@1
   161
                  "rrect bounds\n", j, col->lb, col->ub);
alpar@1
   162
            ret = GLP_EBOUND;
alpar@1
   163
            goto done;
alpar@1
   164
         }
alpar@1
   165
      }
alpar@1
   166
      /* transform LP to the standard formulation */
alpar@1
   167
      if (parm->msg_lev >= GLP_MSG_ALL)
alpar@1
   168
         xprintf("Original LP has %d row(s), %d column(s), and %d non-z"
alpar@1
   169
            "ero(s)\n", P->m, P->n, P->nnz);
alpar@1
   170
      npp = npp_create_wksp();
alpar@1
   171
      npp_load_prob(npp, P, GLP_OFF, GLP_IPT, GLP_ON);
alpar@1
   172
      transform(npp);
alpar@1
   173
      prob = glp_create_prob();
alpar@1
   174
      npp_build_prob(npp, prob);
alpar@1
   175
      if (parm->msg_lev >= GLP_MSG_ALL)
alpar@1
   176
         xprintf("Working LP has %d row(s), %d column(s), and %d non-ze"
alpar@1
   177
            "ro(s)\n", prob->m, prob->n, prob->nnz);
alpar@1
   178
#if 1
alpar@1
   179
      /* currently empty problem cannot be solved */
alpar@1
   180
      if (!(prob->m > 0 && prob->n > 0))
alpar@1
   181
      {  if (parm->msg_lev >= GLP_MSG_ERR)
alpar@1
   182
            xprintf("glp_interior: unable to solve empty problem\n");
alpar@1
   183
         ret = GLP_EFAIL;
alpar@1
   184
         goto done;
alpar@1
   185
      }
alpar@1
   186
#endif
alpar@1
   187
      /* scale the resultant LP */
alpar@1
   188
      {  ENV *env = get_env_ptr();
alpar@1
   189
         int term_out = env->term_out;
alpar@1
   190
         env->term_out = GLP_OFF;
alpar@1
   191
         glp_scale_prob(prob, GLP_SF_EQ);
alpar@1
   192
         env->term_out = term_out;
alpar@1
   193
      }
alpar@1
   194
      /* warn about dense columns */
alpar@1
   195
      if (parm->msg_lev >= GLP_MSG_ON && prob->m >= 200)
alpar@1
   196
      {  int len, cnt = 0;
alpar@1
   197
         for (j = 1; j <= prob->n; j++)
alpar@1
   198
         {  len = glp_get_mat_col(prob, j, NULL, NULL);
alpar@1
   199
            if ((double)len >= 0.20 * (double)prob->m) cnt++;
alpar@1
   200
         }
alpar@1
   201
         if (cnt == 1)
alpar@1
   202
            xprintf("WARNING: PROBLEM HAS ONE DENSE COLUMN\n");
alpar@1
   203
         else if (cnt > 0)
alpar@1
   204
            xprintf("WARNING: PROBLEM HAS %d DENSE COLUMNS\n", cnt);
alpar@1
   205
      }
alpar@1
   206
      /* solve the transformed LP */
alpar@1
   207
      ret = ipm_solve(prob, parm);
alpar@1
   208
      /* postprocess solution from the transformed LP */
alpar@1
   209
      npp_postprocess(npp, prob);
alpar@1
   210
      /* and store solution to the original LP */
alpar@1
   211
      npp_unload_sol(npp, P);
alpar@1
   212
done: /* free working program objects */
alpar@1
   213
      if (npp != NULL) npp_delete_wksp(npp);
alpar@1
   214
      if (prob != NULL) glp_delete_prob(prob);
alpar@1
   215
      /* return to the application program */
alpar@1
   216
      return ret;
alpar@1
   217
}
alpar@1
   218
alpar@1
   219
/***********************************************************************
alpar@1
   220
*  NAME
alpar@1
   221
*
alpar@1
   222
*  glp_init_iptcp - initialize interior-point solver control parameters
alpar@1
   223
*
alpar@1
   224
*  SYNOPSIS
alpar@1
   225
*
alpar@1
   226
*  void glp_init_iptcp(glp_iptcp *parm);
alpar@1
   227
*
alpar@1
   228
*  DESCRIPTION
alpar@1
   229
*
alpar@1
   230
*  The routine glp_init_iptcp initializes control parameters, which are
alpar@1
   231
*  used by the interior-point solver, with default values.
alpar@1
   232
*
alpar@1
   233
*  Default values of the control parameters are stored in the glp_iptcp
alpar@1
   234
*  structure, which the parameter parm points to. */
alpar@1
   235
alpar@1
   236
void glp_init_iptcp(glp_iptcp *parm)
alpar@1
   237
{     parm->msg_lev = GLP_MSG_ALL;
alpar@1
   238
      parm->ord_alg = GLP_ORD_AMD;
alpar@1
   239
      return;
alpar@1
   240
}
alpar@1
   241
alpar@1
   242
/***********************************************************************
alpar@1
   243
*  NAME
alpar@1
   244
*
alpar@1
   245
*  glp_ipt_status - retrieve status of interior-point solution
alpar@1
   246
*
alpar@1
   247
*  SYNOPSIS
alpar@1
   248
*
alpar@1
   249
*  int glp_ipt_status(glp_prob *lp);
alpar@1
   250
*
alpar@1
   251
*  RETURNS
alpar@1
   252
*
alpar@1
   253
*  The routine glp_ipt_status reports the status of solution found by
alpar@1
   254
*  the interior-point solver as follows:
alpar@1
   255
*
alpar@1
   256
*  GLP_UNDEF  - interior-point solution is undefined;
alpar@1
   257
*  GLP_OPT    - interior-point solution is optimal;
alpar@1
   258
*  GLP_INFEAS - interior-point solution is infeasible;
alpar@1
   259
*  GLP_NOFEAS - no feasible solution exists. */
alpar@1
   260
alpar@1
   261
int glp_ipt_status(glp_prob *lp)
alpar@1
   262
{     int ipt_stat = lp->ipt_stat;
alpar@1
   263
      return ipt_stat;
alpar@1
   264
}
alpar@1
   265
alpar@1
   266
/***********************************************************************
alpar@1
   267
*  NAME
alpar@1
   268
*
alpar@1
   269
*  glp_ipt_obj_val - retrieve objective value (interior point)
alpar@1
   270
*
alpar@1
   271
*  SYNOPSIS
alpar@1
   272
*
alpar@1
   273
*  double glp_ipt_obj_val(glp_prob *lp);
alpar@1
   274
*
alpar@1
   275
*  RETURNS
alpar@1
   276
*
alpar@1
   277
*  The routine glp_ipt_obj_val returns value of the objective function
alpar@1
   278
*  for interior-point solution. */
alpar@1
   279
alpar@1
   280
double glp_ipt_obj_val(glp_prob *lp)
alpar@1
   281
{     /*struct LPXCPS *cps = lp->cps;*/
alpar@1
   282
      double z;
alpar@1
   283
      z = lp->ipt_obj;
alpar@1
   284
      /*if (cps->round && fabs(z) < 1e-9) z = 0.0;*/
alpar@1
   285
      return z;
alpar@1
   286
}
alpar@1
   287
alpar@1
   288
/***********************************************************************
alpar@1
   289
*  NAME
alpar@1
   290
*
alpar@1
   291
*  glp_ipt_row_prim - retrieve row primal value (interior point)
alpar@1
   292
*
alpar@1
   293
*  SYNOPSIS
alpar@1
   294
*
alpar@1
   295
*  double glp_ipt_row_prim(glp_prob *lp, int i);
alpar@1
   296
*
alpar@1
   297
*  RETURNS
alpar@1
   298
*
alpar@1
   299
*  The routine glp_ipt_row_prim returns primal value of the auxiliary
alpar@1
   300
*  variable associated with i-th row. */
alpar@1
   301
alpar@1
   302
double glp_ipt_row_prim(glp_prob *lp, int i)
alpar@1
   303
{     /*struct LPXCPS *cps = lp->cps;*/
alpar@1
   304
      double pval;
alpar@1
   305
      if (!(1 <= i && i <= lp->m))
alpar@1
   306
         xerror("glp_ipt_row_prim: i = %d; row number out of range\n",
alpar@1
   307
            i);
alpar@1
   308
      pval = lp->row[i]->pval;
alpar@1
   309
      /*if (cps->round && fabs(pval) < 1e-9) pval = 0.0;*/
alpar@1
   310
      return pval;
alpar@1
   311
}
alpar@1
   312
alpar@1
   313
/***********************************************************************
alpar@1
   314
*  NAME
alpar@1
   315
*
alpar@1
   316
*  glp_ipt_row_dual - retrieve row dual value (interior point)
alpar@1
   317
*
alpar@1
   318
*  SYNOPSIS
alpar@1
   319
*
alpar@1
   320
*  double glp_ipt_row_dual(glp_prob *lp, int i);
alpar@1
   321
*
alpar@1
   322
*  RETURNS
alpar@1
   323
*
alpar@1
   324
*  The routine glp_ipt_row_dual returns dual value (i.e. reduced cost)
alpar@1
   325
*  of the auxiliary variable associated with i-th row. */
alpar@1
   326
alpar@1
   327
double glp_ipt_row_dual(glp_prob *lp, int i)
alpar@1
   328
{     /*struct LPXCPS *cps = lp->cps;*/
alpar@1
   329
      double dval;
alpar@1
   330
      if (!(1 <= i && i <= lp->m))
alpar@1
   331
         xerror("glp_ipt_row_dual: i = %d; row number out of range\n",
alpar@1
   332
            i);
alpar@1
   333
      dval = lp->row[i]->dval;
alpar@1
   334
      /*if (cps->round && fabs(dval) < 1e-9) dval = 0.0;*/
alpar@1
   335
      return dval;
alpar@1
   336
}
alpar@1
   337
alpar@1
   338
/***********************************************************************
alpar@1
   339
*  NAME
alpar@1
   340
*
alpar@1
   341
*  glp_ipt_col_prim - retrieve column primal value (interior point)
alpar@1
   342
*
alpar@1
   343
*  SYNOPSIS
alpar@1
   344
*
alpar@1
   345
*  double glp_ipt_col_prim(glp_prob *lp, int j);
alpar@1
   346
*
alpar@1
   347
*  RETURNS
alpar@1
   348
*
alpar@1
   349
*  The routine glp_ipt_col_prim returns primal value of the structural
alpar@1
   350
*  variable associated with j-th column. */
alpar@1
   351
alpar@1
   352
double glp_ipt_col_prim(glp_prob *lp, int j)
alpar@1
   353
{     /*struct LPXCPS *cps = lp->cps;*/
alpar@1
   354
      double pval;
alpar@1
   355
      if (!(1 <= j && j <= lp->n))
alpar@1
   356
         xerror("glp_ipt_col_prim: j = %d; column number out of range\n"
alpar@1
   357
            , j);
alpar@1
   358
      pval = lp->col[j]->pval;
alpar@1
   359
      /*if (cps->round && fabs(pval) < 1e-9) pval = 0.0;*/
alpar@1
   360
      return pval;
alpar@1
   361
}
alpar@1
   362
alpar@1
   363
/***********************************************************************
alpar@1
   364
*  NAME
alpar@1
   365
*
alpar@1
   366
*  glp_ipt_col_dual - retrieve column dual value (interior point)
alpar@1
   367
*
alpar@1
   368
*  SYNOPSIS
alpar@1
   369
*
alpar@1
   370
*  #include "glplpx.h"
alpar@1
   371
*  double glp_ipt_col_dual(glp_prob *lp, int j);
alpar@1
   372
*
alpar@1
   373
*  RETURNS
alpar@1
   374
*
alpar@1
   375
*  The routine glp_ipt_col_dual returns dual value (i.e. reduced cost)
alpar@1
   376
*  of the structural variable associated with j-th column. */
alpar@1
   377
alpar@1
   378
double glp_ipt_col_dual(glp_prob *lp, int j)
alpar@1
   379
{     /*struct LPXCPS *cps = lp->cps;*/
alpar@1
   380
      double dval;
alpar@1
   381
      if (!(1 <= j && j <= lp->n))
alpar@1
   382
         xerror("glp_ipt_col_dual: j = %d; column number out of range\n"
alpar@1
   383
            , j);
alpar@1
   384
      dval = lp->col[j]->dval;
alpar@1
   385
      /*if (cps->round && fabs(dval) < 1e-9) dval = 0.0;*/
alpar@1
   386
      return dval;
alpar@1
   387
}
alpar@1
   388
alpar@1
   389
/* eof */