src/glplpx02.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
/* glplpx02.c */
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
alpar@1
    27
/***********************************************************************
alpar@1
    28
*  NAME
alpar@1
    29
*
alpar@1
    30
*  lpx_put_solution - store basic solution components
alpar@1
    31
*
alpar@1
    32
*  SYNOPSIS
alpar@1
    33
*
alpar@1
    34
*  void lpx_put_solution(glp_prob *lp, int inval, const int *p_stat,
alpar@1
    35
*     const int *d_stat, const double *obj_val, const int r_stat[],
alpar@1
    36
*     const double r_prim[], const double r_dual[], const int c_stat[],
alpar@1
    37
*     const double c_prim[], const double c_dual[])
alpar@1
    38
*
alpar@1
    39
*  DESCRIPTION
alpar@1
    40
*
alpar@1
    41
*  The routine lpx_put_solution stores basic solution components to the
alpar@1
    42
*  specified problem object.
alpar@1
    43
*
alpar@1
    44
*  The parameter inval is the basis factorization invalidity flag.
alpar@1
    45
*  If this flag is clear, the current status of the basis factorization
alpar@1
    46
*  remains unchanged. If this flag is set, the routine invalidates the
alpar@1
    47
*  basis factorization.
alpar@1
    48
*
alpar@1
    49
*  The parameter p_stat is a pointer to the status of primal basic
alpar@1
    50
*  solution, which should be specified as follows:
alpar@1
    51
*
alpar@1
    52
*  GLP_UNDEF  - primal solution is undefined;
alpar@1
    53
*  GLP_FEAS   - primal solution is feasible;
alpar@1
    54
*  GLP_INFEAS - primal solution is infeasible;
alpar@1
    55
*  GLP_NOFEAS - no primal feasible solution exists.
alpar@1
    56
*
alpar@1
    57
*  If the parameter p_stat is NULL, the current status of primal basic
alpar@1
    58
*  solution remains unchanged.
alpar@1
    59
*
alpar@1
    60
*  The parameter d_stat is a pointer to the status of dual basic
alpar@1
    61
*  solution, which should be specified as follows:
alpar@1
    62
*
alpar@1
    63
*  GLP_UNDEF  - dual solution is undefined;
alpar@1
    64
*  GLP_FEAS   - dual solution is feasible;
alpar@1
    65
*  GLP_INFEAS - dual solution is infeasible;
alpar@1
    66
*  GLP_NOFEAS - no dual feasible solution exists.
alpar@1
    67
*
alpar@1
    68
*  If the parameter d_stat is NULL, the current status of dual basic
alpar@1
    69
*  solution remains unchanged.
alpar@1
    70
*
alpar@1
    71
*  The parameter obj_val is a pointer to the objective function value.
alpar@1
    72
*  If it is NULL, the current value of the objective function remains
alpar@1
    73
*  unchanged.
alpar@1
    74
*
alpar@1
    75
*  The array element r_stat[i], 1 <= i <= m (where m is the number of
alpar@1
    76
*  rows in the problem object), specifies the status of i-th auxiliary
alpar@1
    77
*  variable, which should be specified as follows:
alpar@1
    78
*
alpar@1
    79
*  GLP_BS - basic variable;
alpar@1
    80
*  GLP_NL - non-basic variable on lower bound;
alpar@1
    81
*  GLP_NU - non-basic variable on upper bound;
alpar@1
    82
*  GLP_NF - non-basic free variable;
alpar@1
    83
*  GLP_NS - non-basic fixed variable.
alpar@1
    84
*
alpar@1
    85
*  If the parameter r_stat is NULL, the current statuses of auxiliary
alpar@1
    86
*  variables remain unchanged.
alpar@1
    87
*
alpar@1
    88
*  The array element r_prim[i], 1 <= i <= m (where m is the number of
alpar@1
    89
*  rows in the problem object), specifies a primal value of i-th
alpar@1
    90
*  auxiliary variable. If the parameter r_prim is NULL, the current
alpar@1
    91
*  primal values of auxiliary variables remain unchanged.
alpar@1
    92
*
alpar@1
    93
*  The array element r_dual[i], 1 <= i <= m (where m is the number of
alpar@1
    94
*  rows in the problem object), specifies a dual value (reduced cost)
alpar@1
    95
*  of i-th auxiliary variable. If the parameter r_dual is NULL, the
alpar@1
    96
*  current dual values of auxiliary variables remain unchanged.
alpar@1
    97
*
alpar@1
    98
*  The array element c_stat[j], 1 <= j <= n (where n is the number of
alpar@1
    99
*  columns in the problem object), specifies the status of j-th
alpar@1
   100
*  structural variable, which should be specified as follows:
alpar@1
   101
*
alpar@1
   102
*  GLP_BS - basic variable;
alpar@1
   103
*  GLP_NL - non-basic variable on lower bound;
alpar@1
   104
*  GLP_NU - non-basic variable on upper bound;
alpar@1
   105
*  GLP_NF - non-basic free variable;
alpar@1
   106
*  GLP_NS - non-basic fixed variable.
alpar@1
   107
*
alpar@1
   108
*  If the parameter c_stat is NULL, the current statuses of structural
alpar@1
   109
*  variables remain unchanged.
alpar@1
   110
*
alpar@1
   111
*  The array element c_prim[j], 1 <= j <= n (where n is the number of
alpar@1
   112
*  columns in the problem object), specifies a primal value of j-th
alpar@1
   113
*  structural variable. If the parameter c_prim is NULL, the current
alpar@1
   114
*  primal values of structural variables remain unchanged.
alpar@1
   115
*
alpar@1
   116
*  The array element c_dual[j], 1 <= j <= n (where n is the number of
alpar@1
   117
*  columns in the problem object), specifies a dual value (reduced cost)
alpar@1
   118
*  of j-th structural variable. If the parameter c_dual is NULL, the
alpar@1
   119
*  current dual values of structural variables remain unchanged. */
alpar@1
   120
alpar@1
   121
void lpx_put_solution(glp_prob *lp, int inval, const int *p_stat,
alpar@1
   122
      const int *d_stat, const double *obj_val, const int r_stat[],
alpar@1
   123
      const double r_prim[], const double r_dual[], const int c_stat[],
alpar@1
   124
      const double c_prim[], const double c_dual[])
alpar@1
   125
{     GLPROW *row;
alpar@1
   126
      GLPCOL *col;
alpar@1
   127
      int i, j;
alpar@1
   128
      /* invalidate the basis factorization, if required */
alpar@1
   129
      if (inval) lp->valid = 0;
alpar@1
   130
      /* store primal status */
alpar@1
   131
      if (p_stat != NULL)
alpar@1
   132
      {  if (!(*p_stat == GLP_UNDEF  || *p_stat == GLP_FEAS ||
alpar@1
   133
               *p_stat == GLP_INFEAS || *p_stat == GLP_NOFEAS))
alpar@1
   134
            xerror("lpx_put_solution: p_stat = %d; invalid primal statu"
alpar@1
   135
               "s\n", *p_stat);
alpar@1
   136
         lp->pbs_stat = *p_stat;
alpar@1
   137
      }
alpar@1
   138
      /* store dual status */
alpar@1
   139
      if (d_stat != NULL)
alpar@1
   140
      {  if (!(*d_stat == GLP_UNDEF  || *d_stat == GLP_FEAS ||
alpar@1
   141
               *d_stat == GLP_INFEAS || *d_stat == GLP_NOFEAS))
alpar@1
   142
            xerror("lpx_put_solution: d_stat = %d; invalid dual status "
alpar@1
   143
               "\n", *d_stat);
alpar@1
   144
         lp->dbs_stat = *d_stat;
alpar@1
   145
      }
alpar@1
   146
      /* store objective function value */
alpar@1
   147
      if (obj_val != NULL) lp->obj_val = *obj_val;
alpar@1
   148
      /* store row solution components */
alpar@1
   149
      for (i = 1; i <= lp->m; i++)
alpar@1
   150
      {  row = lp->row[i];
alpar@1
   151
         if (r_stat != NULL)
alpar@1
   152
         {  if (!(r_stat[i] == GLP_BS ||
alpar@1
   153
                  row->type == GLP_FR && r_stat[i] == GLP_NF ||
alpar@1
   154
                  row->type == GLP_LO && r_stat[i] == GLP_NL ||
alpar@1
   155
                  row->type == GLP_UP && r_stat[i] == GLP_NU ||
alpar@1
   156
                  row->type == GLP_DB && r_stat[i] == GLP_NL ||
alpar@1
   157
                  row->type == GLP_DB && r_stat[i] == GLP_NU ||
alpar@1
   158
                  row->type == GLP_FX && r_stat[i] == GLP_NS))
alpar@1
   159
               xerror("lpx_put_solution: r_stat[%d] = %d; invalid row s"
alpar@1
   160
                  "tatus\n", i, r_stat[i]);
alpar@1
   161
            row->stat = r_stat[i];
alpar@1
   162
         }
alpar@1
   163
         if (r_prim != NULL) row->prim = r_prim[i];
alpar@1
   164
         if (r_dual != NULL) row->dual = r_dual[i];
alpar@1
   165
      }
alpar@1
   166
      /* store column solution components */
alpar@1
   167
      for (j = 1; j <= lp->n; j++)
alpar@1
   168
      {  col = lp->col[j];
alpar@1
   169
         if (c_stat != NULL)
alpar@1
   170
         {  if (!(c_stat[j] == GLP_BS ||
alpar@1
   171
                  col->type == GLP_FR && c_stat[j] == GLP_NF ||
alpar@1
   172
                  col->type == GLP_LO && c_stat[j] == GLP_NL ||
alpar@1
   173
                  col->type == GLP_UP && c_stat[j] == GLP_NU ||
alpar@1
   174
                  col->type == GLP_DB && c_stat[j] == GLP_NL ||
alpar@1
   175
                  col->type == GLP_DB && c_stat[j] == GLP_NU ||
alpar@1
   176
                  col->type == GLP_FX && c_stat[j] == GLP_NS))
alpar@1
   177
               xerror("lpx_put_solution: c_stat[%d] = %d; invalid colum"
alpar@1
   178
                  "n status\n", j, c_stat[j]);
alpar@1
   179
            col->stat = c_stat[j];
alpar@1
   180
         }
alpar@1
   181
         if (c_prim != NULL) col->prim = c_prim[j];
alpar@1
   182
         if (c_dual != NULL) col->dual = c_dual[j];
alpar@1
   183
      }
alpar@1
   184
      return;
alpar@1
   185
}
alpar@1
   186
alpar@1
   187
/*----------------------------------------------------------------------
alpar@1
   188
-- lpx_put_mip_soln - store mixed integer solution components.
alpar@1
   189
--
alpar@1
   190
-- *Synopsis*
alpar@1
   191
--
alpar@1
   192
-- #include "glplpx.h"
alpar@1
   193
-- void lpx_put_mip_soln(glp_prob *lp, int i_stat, double row_mipx[],
alpar@1
   194
--    double col_mipx[]);
alpar@1
   195
--
alpar@1
   196
-- *Description*
alpar@1
   197
--
alpar@1
   198
-- The routine lpx_put_mip_soln stores solution components obtained by
alpar@1
   199
-- branch-and-bound solver into the specified problem object.
alpar@1
   200
--
alpar@1
   201
-- NOTE: This routine is intended for internal use only. */
alpar@1
   202
alpar@1
   203
void lpx_put_mip_soln(glp_prob *lp, int i_stat, double row_mipx[],
alpar@1
   204
      double col_mipx[])
alpar@1
   205
{     GLPROW *row;
alpar@1
   206
      GLPCOL *col;
alpar@1
   207
      int i, j;
alpar@1
   208
      double sum;
alpar@1
   209
      /* store mixed integer status */
alpar@1
   210
#if 0
alpar@1
   211
      if (!(i_stat == LPX_I_UNDEF || i_stat == LPX_I_OPT ||
alpar@1
   212
            i_stat == LPX_I_FEAS  || i_stat == LPX_I_NOFEAS))
alpar@1
   213
         fault("lpx_put_mip_soln: i_stat = %d; invalid mixed integer st"
alpar@1
   214
            "atus", i_stat);
alpar@1
   215
      lp->i_stat = i_stat;
alpar@1
   216
#else
alpar@1
   217
      switch (i_stat)
alpar@1
   218
      {  case LPX_I_UNDEF:
alpar@1
   219
            lp->mip_stat = GLP_UNDEF; break;
alpar@1
   220
         case LPX_I_OPT:
alpar@1
   221
            lp->mip_stat = GLP_OPT;  break;
alpar@1
   222
         case LPX_I_FEAS:
alpar@1
   223
            lp->mip_stat = GLP_FEAS; break;
alpar@1
   224
         case LPX_I_NOFEAS:
alpar@1
   225
            lp->mip_stat = GLP_NOFEAS; break;
alpar@1
   226
         default:
alpar@1
   227
            xerror("lpx_put_mip_soln: i_stat = %d; invalid mixed intege"
alpar@1
   228
               "r status\n", i_stat);
alpar@1
   229
      }
alpar@1
   230
#endif
alpar@1
   231
      /* store row solution components */
alpar@1
   232
      if (row_mipx != NULL)
alpar@1
   233
      {  for (i = 1; i <= lp->m; i++)
alpar@1
   234
         {  row = lp->row[i];
alpar@1
   235
            row->mipx = row_mipx[i];
alpar@1
   236
         }
alpar@1
   237
      }
alpar@1
   238
      /* store column solution components */
alpar@1
   239
      if (col_mipx != NULL)
alpar@1
   240
      {  for (j = 1; j <= lp->n; j++)
alpar@1
   241
         {  col = lp->col[j];
alpar@1
   242
            col->mipx = col_mipx[j];
alpar@1
   243
         }
alpar@1
   244
      }
alpar@1
   245
      /* if the solution is claimed to be integer feasible, check it */
alpar@1
   246
      if (lp->mip_stat == GLP_OPT || lp->mip_stat == GLP_FEAS)
alpar@1
   247
      {  for (j = 1; j <= lp->n; j++)
alpar@1
   248
         {  col = lp->col[j];
alpar@1
   249
            if (col->kind == GLP_IV && col->mipx != floor(col->mipx))
alpar@1
   250
               xerror("lpx_put_mip_soln: col_mipx[%d] = %.*g; must be i"
alpar@1
   251
                  "ntegral\n", j, DBL_DIG, col->mipx);
alpar@1
   252
         }
alpar@1
   253
      }
alpar@1
   254
      /* compute the objective function value */
alpar@1
   255
      sum = lp->c0;
alpar@1
   256
      for (j = 1; j <= lp->n; j++)
alpar@1
   257
      {  col = lp->col[j];
alpar@1
   258
         sum += col->coef * col->mipx;
alpar@1
   259
      }
alpar@1
   260
      lp->mip_obj = sum;
alpar@1
   261
      return;
alpar@1
   262
}
alpar@1
   263
alpar@1
   264
/* eof */