src/glplpx03.c
author Alpar Juttner <alpar@cs.elte.hu>
Mon, 06 Dec 2010 13:09:21 +0100
changeset 1 c445c931472f
permissions -rw-r--r--
Import glpk-4.45

- Generated files and doc/notes are removed
alpar@1
     1
/* glplpx03.c (OPB format) */
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
*  Author: Oscar Gustafsson <oscarg@isy.liu.se>.
alpar@1
     7
*
alpar@1
     8
*  Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
alpar@1
     9
*  2009, 2010 Andrew Makhorin, Department for Applied Informatics,
alpar@1
    10
*  Moscow Aviation Institute, Moscow, Russia. All rights reserved.
alpar@1
    11
*  E-mail: <mao@gnu.org>.
alpar@1
    12
*
alpar@1
    13
*  GLPK is free software: you can redistribute it and/or modify it
alpar@1
    14
*  under the terms of the GNU General Public License as published by
alpar@1
    15
*  the Free Software Foundation, either version 3 of the License, or
alpar@1
    16
*  (at your option) any later version.
alpar@1
    17
*
alpar@1
    18
*  GLPK is distributed in the hope that it will be useful, but WITHOUT
alpar@1
    19
*  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
alpar@1
    20
*  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
alpar@1
    21
*  License for more details.
alpar@1
    22
*
alpar@1
    23
*  You should have received a copy of the GNU General Public License
alpar@1
    24
*  along with GLPK. If not, see <http://www.gnu.org/licenses/>.
alpar@1
    25
***********************************************************************/
alpar@1
    26
alpar@1
    27
#define _GLPSTD_ERRNO
alpar@1
    28
#define _GLPSTD_STDIO
alpar@1
    29
#include "glpapi.h"
alpar@1
    30
#if 0 /* 24/XII-2009; by mao */
alpar@1
    31
#include "glpipp.h"
alpar@1
    32
#endif
alpar@1
    33
alpar@1
    34
/*----------------------------------------------------------------------
alpar@1
    35
-- lpx_write_pb - write problem data in (normalized) OPB format.
alpar@1
    36
--
alpar@1
    37
-- *Synopsis*
alpar@1
    38
--
alpar@1
    39
-- #include "glplpx.h"
alpar@1
    40
-- int lpx_write_pb(LPX *lp, const char *fname, int normalized,
alpar@1
    41
--    int binarize);
alpar@1
    42
--
alpar@1
    43
-- *Description*
alpar@1
    44
--
alpar@1
    45
-- The routine lpx_write_pb writes problem data in OPB format
alpar@1
    46
-- to an output text file whose name is the character string fname.
alpar@1
    47
-- If normalized is non-zero the output will be generated in a
alpar@1
    48
-- normalized form with sequentially numbered variables, x1, x2 etc.
alpar@1
    49
-- If binarize, any integer variable will be repalzec by binary ones,
alpar@1
    50
-- see ipp_binarize
alpar@1
    51
--
alpar@1
    52
-- *Returns*
alpar@1
    53
--
alpar@1
    54
-- If the operation was successful, the routine returns zero. Otherwise
alpar@1
    55
-- the routine prints an error message and returns non-zero. */
alpar@1
    56
alpar@1
    57
#if 1 /* 24/XII-2009; by mao (disabled, because IPP was removed) */
alpar@1
    58
int lpx_write_pb(LPX *lp, const char *fname, int normalized,
alpar@1
    59
      int binarize)
alpar@1
    60
{     xassert(lp == lp);
alpar@1
    61
      xassert(fname == fname);
alpar@1
    62
      xassert(normalized == normalized);
alpar@1
    63
      xassert(binarize == binarize);
alpar@1
    64
      xprintf("lpx_write_pb: sorry, currently this operation is not ava"
alpar@1
    65
         "ilable\n");
alpar@1
    66
      return 1;
alpar@1
    67
}
alpar@1
    68
#else
alpar@1
    69
int lpx_write_pb(LPX *lp, const char *fname, int normalized,
alpar@1
    70
      int binarize)
alpar@1
    71
{
alpar@1
    72
  FILE* fp;
alpar@1
    73
  int m,n,i,j,k,o,nonfree=0, obj_dir, dbl, *ndx, row_type, emptylhs=0;
alpar@1
    74
  double coeff, *val, bound, constant/*=0.0*/;
alpar@1
    75
  char* objconstname = "dummy_one";
alpar@1
    76
  char* emptylhsname = "dummy_zero";
alpar@1
    77
alpar@1
    78
  /* Variables needed for possible binarization */
alpar@1
    79
  /*LPX* tlp;*/
alpar@1
    80
  IPP *ipp = NULL;
alpar@1
    81
  /*tlp=lp;*/
alpar@1
    82
alpar@1
    83
  if(binarize) /* Transform integer variables to binary ones */
alpar@1
    84
    {
alpar@1
    85
      ipp = ipp_create_wksp();
alpar@1
    86
      ipp_load_orig(ipp, lp);
alpar@1
    87
      ipp_binarize(ipp);
alpar@1
    88
      lp = ipp_build_prob(ipp);
alpar@1
    89
    }
alpar@1
    90
  fp = fopen(fname, "w");
alpar@1
    91
alpar@1
    92
  if(fp!= NULL)
alpar@1
    93
    {
alpar@1
    94
      xprintf(
alpar@1
    95
          "lpx_write_pb: writing problem in %sOPB format to `%s'...\n",
alpar@1
    96
              (normalized?"normalized ":""), fname);
alpar@1
    97
alpar@1
    98
      m = glp_get_num_rows(lp);
alpar@1
    99
      n = glp_get_num_cols(lp);
alpar@1
   100
      for(i=1;i<=m;i++)
alpar@1
   101
        {
alpar@1
   102
          switch(glp_get_row_type(lp,i))
alpar@1
   103
            {
alpar@1
   104
            case GLP_LO:
alpar@1
   105
            case GLP_UP:
alpar@1
   106
            case GLP_FX:
alpar@1
   107
              {
alpar@1
   108
                nonfree += 1;
alpar@1
   109
                break;
alpar@1
   110
              }
alpar@1
   111
            case GLP_DB:
alpar@1
   112
              {
alpar@1
   113
                nonfree += 2;
alpar@1
   114
                break;
alpar@1
   115
              }
alpar@1
   116
            }
alpar@1
   117
        }
alpar@1
   118
      constant=glp_get_obj_coef(lp,0);
alpar@1
   119
      fprintf(fp,"* #variables = %d #constraints = %d\n",
alpar@1
   120
         n + (constant == 0?1:0), nonfree + (constant == 0?1:0));
alpar@1
   121
      /* Objective function */
alpar@1
   122
      obj_dir = glp_get_obj_dir(lp);
alpar@1
   123
      fprintf(fp,"min: ");
alpar@1
   124
      for(i=1;i<=n;i++)
alpar@1
   125
        {
alpar@1
   126
          coeff = glp_get_obj_coef(lp,i);
alpar@1
   127
          if(coeff != 0.0)
alpar@1
   128
            {
alpar@1
   129
              if(obj_dir == GLP_MAX)
alpar@1
   130
                coeff=-coeff;
alpar@1
   131
              if(normalized)
alpar@1
   132
                fprintf(fp, " %d x%d", (int)coeff, i);
alpar@1
   133
              else
alpar@1
   134
                fprintf(fp, " %d*%s", (int)coeff,
alpar@1
   135
                  glp_get_col_name(lp,i));
alpar@1
   136
alpar@1
   137
            }
alpar@1
   138
        }
alpar@1
   139
      if(constant)
alpar@1
   140
        {
alpar@1
   141
          if(normalized)
alpar@1
   142
            fprintf(fp, " %d x%d", (int)constant, n+1);
alpar@1
   143
          else
alpar@1
   144
            fprintf(fp, " %d*%s", (int)constant, objconstname);
alpar@1
   145
        }
alpar@1
   146
      fprintf(fp,";\n");
alpar@1
   147
alpar@1
   148
      if(normalized && !binarize)  /* Name substitution */
alpar@1
   149
        {
alpar@1
   150
          fprintf(fp,"* Variable name substitution:\n");
alpar@1
   151
          for(j=1;j<=n;j++)
alpar@1
   152
            {
alpar@1
   153
              fprintf(fp, "* x%d = %s\n", j, glp_get_col_name(lp,j));
alpar@1
   154
            }
alpar@1
   155
          if(constant)
alpar@1
   156
            fprintf(fp, "* x%d = %s\n", n+1, objconstname);
alpar@1
   157
        }
alpar@1
   158
alpar@1
   159
      ndx = xcalloc(1+n, sizeof(int));
alpar@1
   160
      val = xcalloc(1+n, sizeof(double));
alpar@1
   161
alpar@1
   162
      /* Constraints */
alpar@1
   163
      for(j=1;j<=m;j++)
alpar@1
   164
        {
alpar@1
   165
          row_type=glp_get_row_type(lp,j);
alpar@1
   166
          if(row_type!=GLP_FR)
alpar@1
   167
            {
alpar@1
   168
              if(row_type == GLP_DB)
alpar@1
   169
                {
alpar@1
   170
                  dbl=2;
alpar@1
   171
                  row_type = GLP_UP;
alpar@1
   172
                }
alpar@1
   173
              else
alpar@1
   174
                {
alpar@1
   175
                  dbl=1;
alpar@1
   176
                }
alpar@1
   177
              k=glp_get_mat_row(lp, j, ndx, val);
alpar@1
   178
              for(o=1;o<=dbl;o++)
alpar@1
   179
                {
alpar@1
   180
                  if(o==2)
alpar@1
   181
                    {
alpar@1
   182
                      row_type = GLP_LO;
alpar@1
   183
                    }
alpar@1
   184
                  if(k==0) /* Empty LHS */
alpar@1
   185
                    {
alpar@1
   186
                      emptylhs = 1;
alpar@1
   187
                      if(normalized)
alpar@1
   188
                        {
alpar@1
   189
                          fprintf(fp, "0 x%d ", n+2);
alpar@1
   190
                        }
alpar@1
   191
                      else
alpar@1
   192
                        {
alpar@1
   193
                          fprintf(fp, "0*%s ", emptylhsname);
alpar@1
   194
                        }
alpar@1
   195
                    }
alpar@1
   196
alpar@1
   197
                  for(i=1;i<=k;i++)
alpar@1
   198
                    {
alpar@1
   199
                      if(val[i] != 0.0)
alpar@1
   200
                        {
alpar@1
   201
alpar@1
   202
                          if(normalized)
alpar@1
   203
                            {
alpar@1
   204
                              fprintf(fp, "%d x%d ",
alpar@1
   205
              (row_type==GLP_UP)?(-(int)val[i]):((int)val[i]), ndx[i]);
alpar@1
   206
                            }
alpar@1
   207
                          else
alpar@1
   208
                            {
alpar@1
   209
                              fprintf(fp, "%d*%s ", (int)val[i],
alpar@1
   210
                                      glp_get_col_name(lp,ndx[i]));
alpar@1
   211
                            }
alpar@1
   212
                        }
alpar@1
   213
                    }
alpar@1
   214
                  switch(row_type)
alpar@1
   215
                    {
alpar@1
   216
                    case GLP_LO:
alpar@1
   217
                      {
alpar@1
   218
                        fprintf(fp, ">=");
alpar@1
   219
                        bound = glp_get_row_lb(lp,j);
alpar@1
   220
                        break;
alpar@1
   221
                      }
alpar@1
   222
                    case GLP_UP:
alpar@1
   223
                      {
alpar@1
   224
                        if(normalized)
alpar@1
   225
                          {
alpar@1
   226
                            fprintf(fp, ">=");
alpar@1
   227
                            bound = -glp_get_row_ub(lp,j);
alpar@1
   228
                          }
alpar@1
   229
                        else
alpar@1
   230
                          {
alpar@1
   231
                            fprintf(fp, "<=");
alpar@1
   232
                            bound = glp_get_row_ub(lp,j);
alpar@1
   233
                          }
alpar@1
   234
alpar@1
   235
                        break;
alpar@1
   236
                      }
alpar@1
   237
                    case GLP_FX:
alpar@1
   238
                      {
alpar@1
   239
                        fprintf(fp, "=");
alpar@1
   240
                        bound = glp_get_row_lb(lp,j);
alpar@1
   241
                        break;
alpar@1
   242
                      }
alpar@1
   243
                    }
alpar@1
   244
                  fprintf(fp," %d;\n",(int)bound);
alpar@1
   245
                }
alpar@1
   246
            }
alpar@1
   247
        }
alpar@1
   248
      xfree(ndx);
alpar@1
   249
      xfree(val);
alpar@1
   250
alpar@1
   251
      if(constant)
alpar@1
   252
        {
alpar@1
   253
          xprintf(
alpar@1
   254
        "lpx_write_pb: adding constant objective function variable\n");
alpar@1
   255
alpar@1
   256
          if(normalized)
alpar@1
   257
            fprintf(fp, "1 x%d = 1;\n", n+1);
alpar@1
   258
          else
alpar@1
   259
            fprintf(fp, "1*%s = 1;\n", objconstname);
alpar@1
   260
        }
alpar@1
   261
      if(emptylhs)
alpar@1
   262
        {
alpar@1
   263
          xprintf(
alpar@1
   264
            "lpx_write_pb: adding dummy variable for empty left-hand si"
alpar@1
   265
            "de constraint\n");
alpar@1
   266
alpar@1
   267
          if(normalized)
alpar@1
   268
            fprintf(fp, "1 x%d = 0;\n", n+2);
alpar@1
   269
          else
alpar@1
   270
            fprintf(fp, "1*%s = 0;\n", emptylhsname);
alpar@1
   271
        }
alpar@1
   272
alpar@1
   273
    }
alpar@1
   274
  else
alpar@1
   275
    {
alpar@1
   276
      xprintf("Problems opening file for writing: %s\n", fname);
alpar@1
   277
      return(1);
alpar@1
   278
    }
alpar@1
   279
  fflush(fp);
alpar@1
   280
  if (ferror(fp))
alpar@1
   281
    {  xprintf("lpx_write_pb: can't write to `%s' - %s\n", fname,
alpar@1
   282
               strerror(errno));
alpar@1
   283
    goto fail;
alpar@1
   284
    }
alpar@1
   285
  fclose(fp);
alpar@1
   286
alpar@1
   287
alpar@1
   288
  if(binarize)
alpar@1
   289
    {
alpar@1
   290
      /* delete the resultant problem object */
alpar@1
   291
      if (lp != NULL) lpx_delete_prob(lp);
alpar@1
   292
      /* delete MIP presolver workspace */
alpar@1
   293
      if (ipp != NULL) ipp_delete_wksp(ipp);
alpar@1
   294
      /*lp=tlp;*/
alpar@1
   295
    }
alpar@1
   296
  return 0;
alpar@1
   297
 fail: if (fp != NULL) fclose(fp);
alpar@1
   298
  return 1;
alpar@1
   299
}
alpar@1
   300
#endif
alpar@1
   301
alpar@1
   302
/* eof */