lemon-project-template-glpk

diff deps/glpk/src/glplpx03.c @ 9:33de93886c88

Import GLPK 4.47
author Alpar Juttner <alpar@cs.elte.hu>
date Sun, 06 Nov 2011 20:59:10 +0100
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/deps/glpk/src/glplpx03.c	Sun Nov 06 20:59:10 2011 +0100
     1.3 @@ -0,0 +1,302 @@
     1.4 +/* glplpx03.c (OPB format) */
     1.5 +
     1.6 +/***********************************************************************
     1.7 +*  This code is part of GLPK (GNU Linear Programming Kit).
     1.8 +*
     1.9 +*  Author: Oscar Gustafsson <oscarg@isy.liu.se>.
    1.10 +*
    1.11 +*  Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
    1.12 +*  2009, 2010, 2011 Andrew Makhorin, Department for Applied Informatics,
    1.13 +*  Moscow Aviation Institute, Moscow, Russia. All rights reserved.
    1.14 +*  E-mail: <mao@gnu.org>.
    1.15 +*
    1.16 +*  GLPK is free software: you can redistribute it and/or modify it
    1.17 +*  under the terms of the GNU General Public License as published by
    1.18 +*  the Free Software Foundation, either version 3 of the License, or
    1.19 +*  (at your option) any later version.
    1.20 +*
    1.21 +*  GLPK is distributed in the hope that it will be useful, but WITHOUT
    1.22 +*  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    1.23 +*  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
    1.24 +*  License for more details.
    1.25 +*
    1.26 +*  You should have received a copy of the GNU General Public License
    1.27 +*  along with GLPK. If not, see <http://www.gnu.org/licenses/>.
    1.28 +***********************************************************************/
    1.29 +
    1.30 +#define _GLPSTD_ERRNO
    1.31 +#define _GLPSTD_STDIO
    1.32 +#include "glpapi.h"
    1.33 +#if 0 /* 24/XII-2009; by mao */
    1.34 +#include "glpipp.h"
    1.35 +#endif
    1.36 +
    1.37 +/*----------------------------------------------------------------------
    1.38 +-- lpx_write_pb - write problem data in (normalized) OPB format.
    1.39 +--
    1.40 +-- *Synopsis*
    1.41 +--
    1.42 +-- #include "glplpx.h"
    1.43 +-- int lpx_write_pb(LPX *lp, const char *fname, int normalized,
    1.44 +--    int binarize);
    1.45 +--
    1.46 +-- *Description*
    1.47 +--
    1.48 +-- The routine lpx_write_pb writes problem data in OPB format
    1.49 +-- to an output text file whose name is the character string fname.
    1.50 +-- If normalized is non-zero the output will be generated in a
    1.51 +-- normalized form with sequentially numbered variables, x1, x2 etc.
    1.52 +-- If binarize, any integer variable will be repalzec by binary ones,
    1.53 +-- see ipp_binarize
    1.54 +--
    1.55 +-- *Returns*
    1.56 +--
    1.57 +-- If the operation was successful, the routine returns zero. Otherwise
    1.58 +-- the routine prints an error message and returns non-zero. */
    1.59 +
    1.60 +#if 1 /* 24/XII-2009; by mao (disabled, because IPP was removed) */
    1.61 +int lpx_write_pb(LPX *lp, const char *fname, int normalized,
    1.62 +      int binarize)
    1.63 +{     xassert(lp == lp);
    1.64 +      xassert(fname == fname);
    1.65 +      xassert(normalized == normalized);
    1.66 +      xassert(binarize == binarize);
    1.67 +      xprintf("lpx_write_pb: sorry, currently this operation is not ava"
    1.68 +         "ilable\n");
    1.69 +      return 1;
    1.70 +}
    1.71 +#else
    1.72 +int lpx_write_pb(LPX *lp, const char *fname, int normalized,
    1.73 +      int binarize)
    1.74 +{
    1.75 +  FILE* fp;
    1.76 +  int m,n,i,j,k,o,nonfree=0, obj_dir, dbl, *ndx, row_type, emptylhs=0;
    1.77 +  double coeff, *val, bound, constant/*=0.0*/;
    1.78 +  char* objconstname = "dummy_one";
    1.79 +  char* emptylhsname = "dummy_zero";
    1.80 +
    1.81 +  /* Variables needed for possible binarization */
    1.82 +  /*LPX* tlp;*/
    1.83 +  IPP *ipp = NULL;
    1.84 +  /*tlp=lp;*/
    1.85 +
    1.86 +  if(binarize) /* Transform integer variables to binary ones */
    1.87 +    {
    1.88 +      ipp = ipp_create_wksp();
    1.89 +      ipp_load_orig(ipp, lp);
    1.90 +      ipp_binarize(ipp);
    1.91 +      lp = ipp_build_prob(ipp);
    1.92 +    }
    1.93 +  fp = fopen(fname, "w");
    1.94 +
    1.95 +  if(fp!= NULL)
    1.96 +    {
    1.97 +      xprintf(
    1.98 +          "lpx_write_pb: writing problem in %sOPB format to `%s'...\n",
    1.99 +              (normalized?"normalized ":""), fname);
   1.100 +
   1.101 +      m = glp_get_num_rows(lp);
   1.102 +      n = glp_get_num_cols(lp);
   1.103 +      for(i=1;i<=m;i++)
   1.104 +        {
   1.105 +          switch(glp_get_row_type(lp,i))
   1.106 +            {
   1.107 +            case GLP_LO:
   1.108 +            case GLP_UP:
   1.109 +            case GLP_FX:
   1.110 +              {
   1.111 +                nonfree += 1;
   1.112 +                break;
   1.113 +              }
   1.114 +            case GLP_DB:
   1.115 +              {
   1.116 +                nonfree += 2;
   1.117 +                break;
   1.118 +              }
   1.119 +            }
   1.120 +        }
   1.121 +      constant=glp_get_obj_coef(lp,0);
   1.122 +      fprintf(fp,"* #variables = %d #constraints = %d\n",
   1.123 +         n + (constant == 0?1:0), nonfree + (constant == 0?1:0));
   1.124 +      /* Objective function */
   1.125 +      obj_dir = glp_get_obj_dir(lp);
   1.126 +      fprintf(fp,"min: ");
   1.127 +      for(i=1;i<=n;i++)
   1.128 +        {
   1.129 +          coeff = glp_get_obj_coef(lp,i);
   1.130 +          if(coeff != 0.0)
   1.131 +            {
   1.132 +              if(obj_dir == GLP_MAX)
   1.133 +                coeff=-coeff;
   1.134 +              if(normalized)
   1.135 +                fprintf(fp, " %d x%d", (int)coeff, i);
   1.136 +              else
   1.137 +                fprintf(fp, " %d*%s", (int)coeff,
   1.138 +                  glp_get_col_name(lp,i));
   1.139 +
   1.140 +            }
   1.141 +        }
   1.142 +      if(constant)
   1.143 +        {
   1.144 +          if(normalized)
   1.145 +            fprintf(fp, " %d x%d", (int)constant, n+1);
   1.146 +          else
   1.147 +            fprintf(fp, " %d*%s", (int)constant, objconstname);
   1.148 +        }
   1.149 +      fprintf(fp,";\n");
   1.150 +
   1.151 +      if(normalized && !binarize)  /* Name substitution */
   1.152 +        {
   1.153 +          fprintf(fp,"* Variable name substitution:\n");
   1.154 +          for(j=1;j<=n;j++)
   1.155 +            {
   1.156 +              fprintf(fp, "* x%d = %s\n", j, glp_get_col_name(lp,j));
   1.157 +            }
   1.158 +          if(constant)
   1.159 +            fprintf(fp, "* x%d = %s\n", n+1, objconstname);
   1.160 +        }
   1.161 +
   1.162 +      ndx = xcalloc(1+n, sizeof(int));
   1.163 +      val = xcalloc(1+n, sizeof(double));
   1.164 +
   1.165 +      /* Constraints */
   1.166 +      for(j=1;j<=m;j++)
   1.167 +        {
   1.168 +          row_type=glp_get_row_type(lp,j);
   1.169 +          if(row_type!=GLP_FR)
   1.170 +            {
   1.171 +              if(row_type == GLP_DB)
   1.172 +                {
   1.173 +                  dbl=2;
   1.174 +                  row_type = GLP_UP;
   1.175 +                }
   1.176 +              else
   1.177 +                {
   1.178 +                  dbl=1;
   1.179 +                }
   1.180 +              k=glp_get_mat_row(lp, j, ndx, val);
   1.181 +              for(o=1;o<=dbl;o++)
   1.182 +                {
   1.183 +                  if(o==2)
   1.184 +                    {
   1.185 +                      row_type = GLP_LO;
   1.186 +                    }
   1.187 +                  if(k==0) /* Empty LHS */
   1.188 +                    {
   1.189 +                      emptylhs = 1;
   1.190 +                      if(normalized)
   1.191 +                        {
   1.192 +                          fprintf(fp, "0 x%d ", n+2);
   1.193 +                        }
   1.194 +                      else
   1.195 +                        {
   1.196 +                          fprintf(fp, "0*%s ", emptylhsname);
   1.197 +                        }
   1.198 +                    }
   1.199 +
   1.200 +                  for(i=1;i<=k;i++)
   1.201 +                    {
   1.202 +                      if(val[i] != 0.0)
   1.203 +                        {
   1.204 +
   1.205 +                          if(normalized)
   1.206 +                            {
   1.207 +                              fprintf(fp, "%d x%d ",
   1.208 +              (row_type==GLP_UP)?(-(int)val[i]):((int)val[i]), ndx[i]);
   1.209 +                            }
   1.210 +                          else
   1.211 +                            {
   1.212 +                              fprintf(fp, "%d*%s ", (int)val[i],
   1.213 +                                      glp_get_col_name(lp,ndx[i]));
   1.214 +                            }
   1.215 +                        }
   1.216 +                    }
   1.217 +                  switch(row_type)
   1.218 +                    {
   1.219 +                    case GLP_LO:
   1.220 +                      {
   1.221 +                        fprintf(fp, ">=");
   1.222 +                        bound = glp_get_row_lb(lp,j);
   1.223 +                        break;
   1.224 +                      }
   1.225 +                    case GLP_UP:
   1.226 +                      {
   1.227 +                        if(normalized)
   1.228 +                          {
   1.229 +                            fprintf(fp, ">=");
   1.230 +                            bound = -glp_get_row_ub(lp,j);
   1.231 +                          }
   1.232 +                        else
   1.233 +                          {
   1.234 +                            fprintf(fp, "<=");
   1.235 +                            bound = glp_get_row_ub(lp,j);
   1.236 +                          }
   1.237 +
   1.238 +                        break;
   1.239 +                      }
   1.240 +                    case GLP_FX:
   1.241 +                      {
   1.242 +                        fprintf(fp, "=");
   1.243 +                        bound = glp_get_row_lb(lp,j);
   1.244 +                        break;
   1.245 +                      }
   1.246 +                    }
   1.247 +                  fprintf(fp," %d;\n",(int)bound);
   1.248 +                }
   1.249 +            }
   1.250 +        }
   1.251 +      xfree(ndx);
   1.252 +      xfree(val);
   1.253 +
   1.254 +      if(constant)
   1.255 +        {
   1.256 +          xprintf(
   1.257 +        "lpx_write_pb: adding constant objective function variable\n");
   1.258 +
   1.259 +          if(normalized)
   1.260 +            fprintf(fp, "1 x%d = 1;\n", n+1);
   1.261 +          else
   1.262 +            fprintf(fp, "1*%s = 1;\n", objconstname);
   1.263 +        }
   1.264 +      if(emptylhs)
   1.265 +        {
   1.266 +          xprintf(
   1.267 +            "lpx_write_pb: adding dummy variable for empty left-hand si"
   1.268 +            "de constraint\n");
   1.269 +
   1.270 +          if(normalized)
   1.271 +            fprintf(fp, "1 x%d = 0;\n", n+2);
   1.272 +          else
   1.273 +            fprintf(fp, "1*%s = 0;\n", emptylhsname);
   1.274 +        }
   1.275 +
   1.276 +    }
   1.277 +  else
   1.278 +    {
   1.279 +      xprintf("Problems opening file for writing: %s\n", fname);
   1.280 +      return(1);
   1.281 +    }
   1.282 +  fflush(fp);
   1.283 +  if (ferror(fp))
   1.284 +    {  xprintf("lpx_write_pb: can't write to `%s' - %s\n", fname,
   1.285 +               strerror(errno));
   1.286 +    goto fail;
   1.287 +    }
   1.288 +  fclose(fp);
   1.289 +
   1.290 +
   1.291 +  if(binarize)
   1.292 +    {
   1.293 +      /* delete the resultant problem object */
   1.294 +      if (lp != NULL) lpx_delete_prob(lp);
   1.295 +      /* delete MIP presolver workspace */
   1.296 +      if (ipp != NULL) ipp_delete_wksp(ipp);
   1.297 +      /*lp=tlp;*/
   1.298 +    }
   1.299 +  return 0;
   1.300 + fail: if (fp != NULL) fclose(fp);
   1.301 +  return 1;
   1.302 +}
   1.303 +#endif
   1.304 +
   1.305 +/* eof */