lemon-project-template-glpk

diff deps/glpk/src/glpapi21.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/glpapi21.c	Sun Nov 06 20:59:10 2011 +0100
     1.3 @@ -0,0 +1,1324 @@
     1.4 +/* glpapi21.c (stand-alone LP/MIP solver) */
     1.5 +
     1.6 +/***********************************************************************
     1.7 +*  This code is part of GLPK (GNU Linear Programming Kit).
     1.8 +*
     1.9 +*  Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
    1.10 +*  2009, 2010, 2011 Andrew Makhorin, Department for Applied Informatics,
    1.11 +*  Moscow Aviation Institute, Moscow, Russia. All rights reserved.
    1.12 +*  E-mail: <mao@gnu.org>.
    1.13 +*
    1.14 +*  GLPK is free software: you can redistribute it and/or modify it
    1.15 +*  under the terms of the GNU General Public License as published by
    1.16 +*  the Free Software Foundation, either version 3 of the License, or
    1.17 +*  (at your option) any later version.
    1.18 +*
    1.19 +*  GLPK is distributed in the hope that it will be useful, but WITHOUT
    1.20 +*  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    1.21 +*  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
    1.22 +*  License for more details.
    1.23 +*
    1.24 +*  You should have received a copy of the GNU General Public License
    1.25 +*  along with GLPK. If not, see <http://www.gnu.org/licenses/>.
    1.26 +***********************************************************************/
    1.27 +
    1.28 +#include "glpapi.h"
    1.29 +#include "glpgmp.h"
    1.30 +
    1.31 +struct csa
    1.32 +{     /* common storage area */
    1.33 +      glp_prob *prob;
    1.34 +      /* LP/MIP problem object */
    1.35 +      glp_bfcp bfcp;
    1.36 +      /* basis factorization control parameters */
    1.37 +      glp_smcp smcp;
    1.38 +      /* simplex method control parameters */
    1.39 +      glp_iptcp iptcp;
    1.40 +      /* interior-point method control parameters */
    1.41 +      glp_iocp iocp;
    1.42 +      /* integer optimizer control parameters */
    1.43 +      glp_tran *tran;
    1.44 +      /* model translator workspace */
    1.45 +      glp_graph *graph;
    1.46 +      /* network problem object */
    1.47 +      int format;
    1.48 +      /* problem file format: */
    1.49 +#define FMT_MPS_DECK    1  /* fixed MPS */
    1.50 +#define FMT_MPS_FILE    2  /* free MPS */
    1.51 +#define FMT_LP          3  /* CPLEX LP */
    1.52 +#define FMT_GLP         4  /* GLPK LP/MIP */
    1.53 +#define FMT_MATHPROG    5  /* MathProg */
    1.54 +#define FMT_MIN_COST    6  /* DIMACS min-cost flow */
    1.55 +#define FMT_MAX_FLOW    7  /* DIMACS maximum flow */
    1.56 +#if 1 /* 06/VIII-2011 */
    1.57 +#define FMT_CNF         8  /* DIMACS CNF-SAT */
    1.58 +#endif
    1.59 +      const char *in_file;
    1.60 +      /* name of input problem file */
    1.61 +#define DATA_MAX 10
    1.62 +      /* maximal number of input data files */
    1.63 +      int ndf;
    1.64 +      /* number of input data files specified */
    1.65 +      const char *in_data[1+DATA_MAX];
    1.66 +      /* name(s) of input data file(s) */
    1.67 +      const char *out_dpy;
    1.68 +      /* name of output file to send display output; NULL means the
    1.69 +         display output is sent to the terminal */
    1.70 +      int seed;
    1.71 +      /* seed value to be passed to the MathProg translator; initially
    1.72 +         set to 1; 0x80000000 means the value is omitted */
    1.73 +      int solution;
    1.74 +      /* solution type flag: */
    1.75 +#define SOL_BASIC       1  /* basic */
    1.76 +#define SOL_INTERIOR    2  /* interior-point */
    1.77 +#define SOL_INTEGER     3  /* mixed integer */
    1.78 +      const char *in_res;
    1.79 +      /* name of input solution file in raw format */
    1.80 +      int dir;
    1.81 +      /* optimization direction flag:
    1.82 +         0       - not specified
    1.83 +         GLP_MIN - minimization
    1.84 +         GLP_MAX - maximization */
    1.85 +      int scale;
    1.86 +      /* automatic problem scaling flag */
    1.87 +      const char *out_sol;
    1.88 +      /* name of output solution file in printable format */
    1.89 +      const char *out_res;
    1.90 +      /* name of output solution file in raw format */
    1.91 +      const char *out_ranges;
    1.92 +      /* name of output file to write sensitivity analysis report */
    1.93 +      int check;
    1.94 +      /* input data checking flag; no solution is performed */
    1.95 +      const char *new_name;
    1.96 +      /* new name to be assigned to the problem */
    1.97 +      const char *out_mps;
    1.98 +      /* name of output problem file in fixed MPS format */
    1.99 +      const char *out_freemps;
   1.100 +      /* name of output problem file in free MPS format */
   1.101 +      const char *out_cpxlp;
   1.102 +      /* name of output problem file in CPLEX LP format */
   1.103 +      const char *out_glp;
   1.104 +      /* name of output problem file in GLPK format */
   1.105 +      const char *out_pb;
   1.106 +      /* name of output problem file in OPB format */
   1.107 +      const char *out_npb;
   1.108 +      /* name of output problem file in normalized OPB format */
   1.109 +#if 1 /* 06/VIII-2011 */
   1.110 +      const char *out_cnf;
   1.111 +      /* name of output problem file in DIMACS CNF-SAT format */
   1.112 +#endif
   1.113 +      const char *log_file;
   1.114 +      /* name of output file to hardcopy terminal output */
   1.115 +      int crash;
   1.116 +      /* initial basis option: */
   1.117 +#define USE_STD_BASIS   1  /* use standard basis */
   1.118 +#define USE_ADV_BASIS   2  /* use advanced basis */
   1.119 +#define USE_CPX_BASIS   3  /* use Bixby's basis */
   1.120 +#define USE_INI_BASIS   4  /* use initial basis from ini_file */
   1.121 +      const char *ini_file;
   1.122 +      /* name of input file containing initial basis */
   1.123 +      int exact;
   1.124 +      /* flag to use glp_exact rather than glp_simplex */
   1.125 +      int xcheck;
   1.126 +      /* flag to check final basis with glp_exact */
   1.127 +      int nomip;
   1.128 +      /* flag to consider MIP as pure LP */
   1.129 +#if 1 /* 15/VIII-2011 */
   1.130 +      int minisat;
   1.131 +      /* option to solve feasibility problem with MiniSat solver */
   1.132 +      int use_bnd;
   1.133 +      /* option to bound objective function */
   1.134 +      int obj_bnd;
   1.135 +      /* upper (minization) or lower (maximization) objective bound */
   1.136 +#endif
   1.137 +};
   1.138 +
   1.139 +static void print_help(const char *my_name)
   1.140 +{     /* print help information */
   1.141 +      xprintf("Usage: %s [options...] filename\n", my_name);
   1.142 +      xprintf("\n");
   1.143 +      xprintf("General options:\n");
   1.144 +      xprintf("   --mps             read LP/MIP problem in fixed MPS fo"
   1.145 +         "rmat\n");
   1.146 +      xprintf("   --freemps         read LP/MIP problem in free MPS for"
   1.147 +         "mat (default)\n");
   1.148 +      xprintf("   --lp              read LP/MIP problem in CPLEX LP for"
   1.149 +         "mat\n");
   1.150 +      xprintf("   --glp             read LP/MIP problem in GLPK format "
   1.151 +         "\n");
   1.152 +      xprintf("   --math            read LP/MIP model written in GNU Ma"
   1.153 +         "thProg modeling\n");
   1.154 +      xprintf("                     language\n");
   1.155 +      xprintf("   -m filename, --model filename\n");
   1.156 +      xprintf("                     read model section and optional dat"
   1.157 +         "a section from\n");
   1.158 +      xprintf("                     filename (same as --math)\n");
   1.159 +      xprintf("   -d filename, --data filename\n");
   1.160 +      xprintf("                     read data section from filename (fo"
   1.161 +         "r --math only);\n");
   1.162 +      xprintf("                     if model file also has data section"
   1.163 +         ", it is ignored\n");
   1.164 +      xprintf("   -y filename, --display filename\n");
   1.165 +      xprintf("                     send display output to filename (fo"
   1.166 +         "r --math only);\n");
   1.167 +      xprintf("                     by default the output is sent to te"
   1.168 +         "rminal\n");
   1.169 +      xprintf("   --seed value      initialize pseudo-random number gen"
   1.170 +         "erator used in\n");
   1.171 +      xprintf("                     MathProg model with specified seed "
   1.172 +         "(any integer);\n");
   1.173 +      xprintf("                     if seed value is ?, some random see"
   1.174 +         "d will be used\n");
   1.175 +      xprintf("   --mincost         read min-cost flow problem in DIMAC"
   1.176 +         "S format\n");
   1.177 +      xprintf("   --maxflow         read maximum flow problem in DIMACS"
   1.178 +         " format\n");
   1.179 +#if 1 /* 06/VIII-2011 */
   1.180 +      xprintf("   --cnf             read CNF-SAT problem in DIMACS form"
   1.181 +         "at\n");
   1.182 +#endif
   1.183 +      xprintf("   --simplex         use simplex method (default)\n");
   1.184 +      xprintf("   --interior        use interior point method (LP only)"
   1.185 +         "\n");
   1.186 +      xprintf("   -r filename, --read filename\n");
   1.187 +      xprintf("                     read solution from filename rather "
   1.188 +         "to find it with\n");
   1.189 +      xprintf("                     the solver\n");
   1.190 +      xprintf("   --min             minimization\n");
   1.191 +      xprintf("   --max             maximization\n");
   1.192 +      xprintf("   --scale           scale problem (default)\n");
   1.193 +      xprintf("   --noscale         do not scale problem\n");
   1.194 +      xprintf("   -o filename, --output filename\n");
   1.195 +      xprintf("                     write solution to filename in print"
   1.196 +         "able format\n");
   1.197 +      xprintf("   -w filename, --write filename\n");
   1.198 +      xprintf("                     write solution to filename in plain"
   1.199 +         " text format\n");
   1.200 +      xprintf("   --ranges filename\n");
   1.201 +      xprintf("                     write sensitivity analysis report t"
   1.202 +         "o filename in\n");
   1.203 +      xprintf("                     printable format (simplex only)\n");
   1.204 +      xprintf("   --tmlim nnn       limit solution time to nnn seconds "
   1.205 +         "\n");
   1.206 +      xprintf("   --memlim nnn      limit available memory to nnn megab"
   1.207 +         "ytes\n");
   1.208 +      xprintf("   --check           do not solve problem, check input d"
   1.209 +         "ata only\n");
   1.210 +      xprintf("   --name probname   change problem name to probname\n");
   1.211 +      xprintf("   --wmps filename   write problem to filename in fixed "
   1.212 +         "MPS format\n");
   1.213 +      xprintf("   --wfreemps filename\n");
   1.214 +      xprintf("                     write problem to filename in free M"
   1.215 +         "PS format\n");
   1.216 +      xprintf("   --wlp filename    write problem to filename in CPLEX "
   1.217 +         "LP format\n");
   1.218 +      xprintf("   --wglp filename   write problem to filename in GLPK f"
   1.219 +         "ormat\n");
   1.220 +#if 0
   1.221 +      xprintf("   --wpb filename    write problem to filename in OPB fo"
   1.222 +         "rmat\n");
   1.223 +      xprintf("   --wnpb filename   write problem to filename in normal"
   1.224 +         "ized OPB format\n");
   1.225 +#endif
   1.226 +#if 1 /* 06/VIII-2011 */
   1.227 +      xprintf("   --wcnf filename   write problem to filename in DIMACS"
   1.228 +         " CNF-SAT format\n");
   1.229 +#endif
   1.230 +      xprintf("   --log filename    write copy of terminal output to fi"
   1.231 +         "lename\n");
   1.232 +      xprintf("   -h, --help        display this help information and e"
   1.233 +         "xit\n");
   1.234 +      xprintf("   -v, --version     display program version and exit\n")
   1.235 +         ;
   1.236 +      xprintf("\n");
   1.237 +      xprintf("LP basis factorization options:\n");
   1.238 +      xprintf("   --luf             LU + Forrest-Tomlin update\n");
   1.239 +      xprintf("                     (faster, less stable; default)\n");
   1.240 +      xprintf("   --cbg             LU + Schur complement + Bartels-Gol"
   1.241 +         "ub update\n");
   1.242 +      xprintf("                     (slower, more stable)\n");
   1.243 +      xprintf("   --cgr             LU + Schur complement + Givens rota"
   1.244 +         "tion update\n");
   1.245 +      xprintf("                     (slower, more stable)\n");
   1.246 +      xprintf("\n");
   1.247 +      xprintf("Options specific to simplex solver:\n");
   1.248 +      xprintf("   --primal          use primal simplex (default)\n");
   1.249 +      xprintf("   --dual            use dual simplex\n");
   1.250 +      xprintf("   --std             use standard initial basis of all s"
   1.251 +         "lacks\n");
   1.252 +      xprintf("   --adv             use advanced initial basis (default"
   1.253 +         ")\n");
   1.254 +      xprintf("   --bib             use Bixby's initial basis\n");
   1.255 +      xprintf("   --ini filename    use as initial basis previously sav"
   1.256 +         "ed with -w\n");
   1.257 +      xprintf("                     (disables LP presolver)\n");
   1.258 +      xprintf("   --steep           use steepest edge technique (defaul"
   1.259 +         "t)\n");
   1.260 +      xprintf("   --nosteep         use standard \"textbook\" pricing\n"
   1.261 +         );
   1.262 +      xprintf("   --relax           use Harris' two-pass ratio test (de"
   1.263 +         "fault)\n");
   1.264 +      xprintf("   --norelax         use standard \"textbook\" ratio tes"
   1.265 +         "t\n");
   1.266 +      xprintf("   --presol          use presolver (default; assumes --s"
   1.267 +         "cale and --adv)\n");
   1.268 +      xprintf("   --nopresol        do not use presolver\n");
   1.269 +      xprintf("   --exact           use simplex method based on exact a"
   1.270 +         "rithmetic\n");
   1.271 +      xprintf("   --xcheck          check final basis using exact arith"
   1.272 +         "metic\n");
   1.273 +      xprintf("\n");
   1.274 +      xprintf("Options specific to interior-point solver:\n");
   1.275 +      xprintf("   --nord            use natural (original) ordering\n");
   1.276 +      xprintf("   --qmd             use quotient minimum degree orderin"
   1.277 +         "g\n");
   1.278 +      xprintf("   --amd             use approximate minimum degree orde"
   1.279 +         "ring (default)\n");
   1.280 +      xprintf("   --symamd          use approximate minimum degree orde"
   1.281 +         "ring\n");
   1.282 +      xprintf("\n");
   1.283 +      xprintf("Options specific to MIP solver:\n");
   1.284 +      xprintf("   --nomip           consider all integer variables as c"
   1.285 +         "ontinuous\n");
   1.286 +      xprintf("                     (allows solving MIP as pure LP)\n");
   1.287 +      xprintf("   --first           branch on first integer variable\n")
   1.288 +         ;
   1.289 +      xprintf("   --last            branch on last integer variable\n");
   1.290 +      xprintf("   --mostf           branch on most fractional variable "
   1.291 +         "\n");
   1.292 +      xprintf("   --drtom           branch using heuristic by Driebeck "
   1.293 +         "and Tomlin\n");
   1.294 +      xprintf("                     (default)\n");
   1.295 +      xprintf("   --pcost           branch using hybrid pseudocost heur"
   1.296 +         "istic (may be\n");
   1.297 +      xprintf("                     useful for hard instances)\n");
   1.298 +      xprintf("   --dfs             backtrack using depth first search "
   1.299 +         "\n");
   1.300 +      xprintf("   --bfs             backtrack using breadth first searc"
   1.301 +         "h\n");
   1.302 +      xprintf("   --bestp           backtrack using the best projection"
   1.303 +         " heuristic\n");
   1.304 +      xprintf("   --bestb           backtrack using node with best loca"
   1.305 +         "l bound\n");
   1.306 +      xprintf("                     (default)\n");
   1.307 +      xprintf("   --intopt          use MIP presolver (default)\n");
   1.308 +      xprintf("   --nointopt        do not use MIP presolver\n");
   1.309 +      xprintf("   --binarize        replace general integer variables b"
   1.310 +         "y binary ones\n");
   1.311 +      xprintf("                     (assumes --intopt)\n");
   1.312 +      xprintf("   --fpump           apply feasibility pump heuristic\n")
   1.313 +         ;
   1.314 +      xprintf("   --gomory          generate Gomory's mixed integer cut"
   1.315 +         "s\n");
   1.316 +      xprintf("   --mir             generate MIR (mixed integer roundin"
   1.317 +         "g) cuts\n");
   1.318 +      xprintf("   --cover           generate mixed cover cuts\n");
   1.319 +      xprintf("   --clique          generate clique cuts\n");
   1.320 +      xprintf("   --cuts            generate all cuts above\n");
   1.321 +      xprintf("   --mipgap tol      set relative mip gap tolerance to t"
   1.322 +         "ol\n");
   1.323 +#if 1 /* 15/VIII-2011 */
   1.324 +      xprintf("   --minisat         translate integer feasibility probl"
   1.325 +         "em to CNF-SAT\n");
   1.326 +      xprintf("                     and solve it with MiniSat solver\n")
   1.327 +         ;
   1.328 +      xprintf("   --objbnd bound    add inequality obj <= bound (minimi"
   1.329 +         "zation) or\n");
   1.330 +      xprintf("                     obj >= bound (maximization) to inte"
   1.331 +         "ger feasibility\n");
   1.332 +      xprintf("                     problem (assumes --minisat)\n");
   1.333 +#endif
   1.334 +      xprintf("\n");
   1.335 +      xprintf("For description of the MPS and CPLEX LP formats see Refe"
   1.336 +         "rence Manual.\n");
   1.337 +      xprintf("For description of the modeling language see \"GLPK: Mod"
   1.338 +         "eling Language\n");
   1.339 +      xprintf("GNU MathProg\". Both documents are included in the GLPK "
   1.340 +         "distribution.\n");
   1.341 +      xprintf("\n");
   1.342 +      xprintf("See GLPK web page at <http://www.gnu.org/software/glpk/g"
   1.343 +         "lpk.html>.\n");
   1.344 +      xprintf("\n");
   1.345 +      xprintf("Please report bugs to <bug-glpk@gnu.org>.\n");
   1.346 +      return;
   1.347 +}
   1.348 +
   1.349 +static void print_version(int briefly)
   1.350 +{     /* print version information */
   1.351 +      xprintf("GLPSOL: GLPK LP/MIP Solver, v%s\n", glp_version());
   1.352 +      if (briefly) goto done;
   1.353 +      xprintf("\n");
   1.354 +      xprintf("Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, "
   1.355 +         "2007, 2008,\n");
   1.356 +      xprintf("2009, 2010, 2011 Andrew Makhorin, Department for Applied"
   1.357 +         " Informatics,\n");
   1.358 +      xprintf("Moscow Aviation Institute, Moscow, Russia. All rights re"
   1.359 +         "served.\n");
   1.360 +      xprintf("\n");
   1.361 +      xprintf("This program has ABSOLUTELY NO WARRANTY.\n");
   1.362 +      xprintf("\n");
   1.363 +      xprintf("This program is free software; you may re-distribute it "
   1.364 +         "under the terms\n");
   1.365 +      xprintf("of the GNU General Public License version 3 or later.\n")
   1.366 +         ;
   1.367 +done: return;
   1.368 +}
   1.369 +
   1.370 +static int parse_cmdline(struct csa *csa, int argc, const char *argv[])
   1.371 +{     /* parse command-line parameters */
   1.372 +      int k;
   1.373 +#define p(str) (strcmp(argv[k], str) == 0)
   1.374 +      for (k = 1; k < argc; k++)
   1.375 +      {  if (p("--mps"))
   1.376 +            csa->format = FMT_MPS_DECK;
   1.377 +         else if (p("--freemps"))
   1.378 +            csa->format = FMT_MPS_FILE;
   1.379 +         else if (p("--lp") || p("--cpxlp"))
   1.380 +            csa->format = FMT_LP;
   1.381 +         else if (p("--glp"))
   1.382 +            csa->format = FMT_GLP;
   1.383 +         else if (p("--math") || p("-m") || p("--model"))
   1.384 +            csa->format = FMT_MATHPROG;
   1.385 +         else if (p("-d") || p("--data"))
   1.386 +         {  k++;
   1.387 +            if (k == argc || argv[k][0] == '\0' || argv[k][0] == '-')
   1.388 +            {  xprintf("No input data file specified\n");
   1.389 +               return 1;
   1.390 +            }
   1.391 +            if (csa->ndf == DATA_MAX)
   1.392 +            {  xprintf("Too many input data files\n");
   1.393 +               return 1;
   1.394 +            }
   1.395 +            csa->in_data[++(csa->ndf)] = argv[k];
   1.396 +         }
   1.397 +         else if (p("-y") || p("--display"))
   1.398 +         {  k++;
   1.399 +            if (k == argc || argv[k][0] == '\0' || argv[k][0] == '-')
   1.400 +            {  xprintf("No display output file specified\n");
   1.401 +               return 1;
   1.402 +            }
   1.403 +            if (csa->out_dpy != NULL)
   1.404 +            {  xprintf("Only one display output file allowed\n");
   1.405 +               return 1;
   1.406 +            }
   1.407 +            csa->out_dpy = argv[k];
   1.408 +         }
   1.409 +         else if (p("--seed"))
   1.410 +         {  k++;
   1.411 +            if (k == argc || argv[k][0] == '\0' ||
   1.412 +               argv[k][0] == '-' && !isdigit((unsigned char)argv[k][1]))
   1.413 +            {  xprintf("No seed value specified\n");
   1.414 +               return 1;
   1.415 +            }
   1.416 +            if (strcmp(argv[k], "?") == 0)
   1.417 +               csa->seed = 0x80000000;
   1.418 +            else if (str2int(argv[k], &csa->seed))
   1.419 +            {  xprintf("Invalid seed value `%s'\n", argv[k]);
   1.420 +               return 1;
   1.421 +            }
   1.422 +         }
   1.423 +         else if (p("--mincost"))
   1.424 +            csa->format = FMT_MIN_COST;
   1.425 +         else if (p("--maxflow"))
   1.426 +            csa->format = FMT_MAX_FLOW;
   1.427 +#if 1 /* 06/VIII-2011 */
   1.428 +         else if (p("--cnf"))
   1.429 +            csa->format = FMT_CNF;
   1.430 +#endif
   1.431 +         else if (p("--simplex"))
   1.432 +            csa->solution = SOL_BASIC;
   1.433 +         else if (p("--interior"))
   1.434 +            csa->solution = SOL_INTERIOR;
   1.435 +#if 1 /* 28/V-2010 */
   1.436 +         else if (p("--alien"))
   1.437 +            csa->iocp.alien = GLP_ON;
   1.438 +#endif
   1.439 +         else if (p("-r") || p("--read"))
   1.440 +         {  k++;
   1.441 +            if (k == argc || argv[k][0] == '\0' || argv[k][0] == '-')
   1.442 +            {  xprintf("No input solution file specified\n");
   1.443 +               return 1;
   1.444 +            }
   1.445 +            if (csa->in_res != NULL)
   1.446 +            {  xprintf("Only one input solution file allowed\n");
   1.447 +               return 1;
   1.448 +            }
   1.449 +            csa->in_res = argv[k];
   1.450 +         }
   1.451 +         else if (p("--min"))
   1.452 +            csa->dir = GLP_MIN;
   1.453 +         else if (p("--max"))
   1.454 +            csa->dir = GLP_MAX;
   1.455 +         else if (p("--scale"))
   1.456 +            csa->scale = 1;
   1.457 +         else if (p("--noscale"))
   1.458 +            csa->scale = 0;
   1.459 +         else if (p("-o") || p("--output"))
   1.460 +         {  k++;
   1.461 +            if (k == argc || argv[k][0] == '\0' || argv[k][0] == '-')
   1.462 +            {  xprintf("No output solution file specified\n");
   1.463 +               return 1;
   1.464 +            }
   1.465 +            if (csa->out_sol != NULL)
   1.466 +            {  xprintf("Only one output solution file allowed\n");
   1.467 +               return 1;
   1.468 +            }
   1.469 +            csa->out_sol = argv[k];
   1.470 +         }
   1.471 +         else if (p("-w") || p("--write"))
   1.472 +         {  k++;
   1.473 +            if (k == argc || argv[k][0] == '\0' || argv[k][0] == '-')
   1.474 +            {  xprintf("No output solution file specified\n");
   1.475 +               return 1;
   1.476 +            }
   1.477 +            if (csa->out_res != NULL)
   1.478 +            {  xprintf("Only one output solution file allowed\n");
   1.479 +               return 1;
   1.480 +            }
   1.481 +            csa->out_res = argv[k];
   1.482 +         }
   1.483 +         else if (p("--ranges") || p("--bounds"))
   1.484 +         {  k++;
   1.485 +            if (k == argc || argv[k][0] == '\0' || argv[k][0] == '-')
   1.486 +            {  xprintf("No output file specified to write sensitivity a"
   1.487 +                  "nalysis report\n");
   1.488 +               return 1;
   1.489 +            }
   1.490 +            if (csa->out_ranges != NULL)
   1.491 +            {  xprintf("Only one output file allowed to write sensitivi"
   1.492 +                  "ty analysis report\n");
   1.493 +               return 1;
   1.494 +            }
   1.495 +            csa->out_ranges = argv[k];
   1.496 +         }
   1.497 +         else if (p("--tmlim"))
   1.498 +         {  int tm_lim;
   1.499 +            k++;
   1.500 +            if (k == argc || argv[k][0] == '\0' || argv[k][0] == '-')
   1.501 +            {  xprintf("No time limit specified\n");
   1.502 +               return 1;
   1.503 +            }
   1.504 +            if (str2int(argv[k], &tm_lim) || tm_lim < 0)
   1.505 +            {  xprintf("Invalid time limit `%s'\n", argv[k]);
   1.506 +               return 1;
   1.507 +            }
   1.508 +            if (tm_lim <= INT_MAX / 1000)
   1.509 +               csa->smcp.tm_lim = csa->iocp.tm_lim = 1000 * tm_lim;
   1.510 +            else
   1.511 +               csa->smcp.tm_lim = csa->iocp.tm_lim = INT_MAX;
   1.512 +         }
   1.513 +         else if (p("--memlim"))
   1.514 +         {  int mem_lim;
   1.515 +            k++;
   1.516 +            if (k == argc || argv[k][0] == '\0' || argv[k][0] == '-')
   1.517 +            {  xprintf("No memory limit specified\n");
   1.518 +               return 1;
   1.519 +            }
   1.520 +            if (str2int(argv[k], &mem_lim) || mem_lim < 1)
   1.521 +            {  xprintf("Invalid memory limit `%s'\n", argv[k]);
   1.522 +               return 1;
   1.523 +            }
   1.524 +            glp_mem_limit(mem_lim);
   1.525 +         }
   1.526 +         else if (p("--check"))
   1.527 +            csa->check = 1;
   1.528 +         else if (p("--name"))
   1.529 +         {  k++;
   1.530 +            if (k == argc || argv[k][0] == '\0' || argv[k][0] == '-')
   1.531 +            {  xprintf("No problem name specified\n");
   1.532 +               return 1;
   1.533 +            }
   1.534 +            if (csa->new_name != NULL)
   1.535 +            {  xprintf("Only one problem name allowed\n");
   1.536 +               return 1;
   1.537 +            }
   1.538 +            csa->new_name = argv[k];
   1.539 +         }
   1.540 +         else if (p("--wmps"))
   1.541 +         {  k++;
   1.542 +            if (k == argc || argv[k][0] == '\0' || argv[k][0] == '-')
   1.543 +            {  xprintf("No fixed MPS output file specified\n");
   1.544 +               return 1;
   1.545 +            }
   1.546 +            if (csa->out_mps != NULL)
   1.547 +            {  xprintf("Only one fixed MPS output file allowed\n");
   1.548 +               return 1;
   1.549 +            }
   1.550 +            csa->out_mps = argv[k];
   1.551 +         }
   1.552 +         else if (p("--wfreemps"))
   1.553 +         {  k++;
   1.554 +            if (k == argc || argv[k][0] == '\0' || argv[k][0] == '-')
   1.555 +            {  xprintf("No free MPS output file specified\n");
   1.556 +               return 1;
   1.557 +            }
   1.558 +            if (csa->out_freemps != NULL)
   1.559 +            {  xprintf("Only one free MPS output file allowed\n");
   1.560 +               return 1;
   1.561 +            }
   1.562 +            csa->out_freemps = argv[k];
   1.563 +         }
   1.564 +         else if (p("--wlp") || p("--wcpxlp") || p("--wlpt"))
   1.565 +         {  k++;
   1.566 +            if (k == argc || argv[k][0] == '\0' || argv[k][0] == '-')
   1.567 +            {  xprintf("No CPLEX LP output file specified\n");
   1.568 +               return 1;
   1.569 +            }
   1.570 +            if (csa->out_cpxlp != NULL)
   1.571 +            {  xprintf("Only one CPLEX LP output file allowed\n");
   1.572 +               return 1;
   1.573 +            }
   1.574 +            csa->out_cpxlp = argv[k];
   1.575 +         }
   1.576 +         else if (p("--wglp"))
   1.577 +         {  k++;
   1.578 +            if (k == argc || argv[k][0] == '\0' || argv[k][0] == '-')
   1.579 +            {  xprintf("No GLPK LP/MIP output file specified\n");
   1.580 +               return 1;
   1.581 +            }
   1.582 +            if (csa->out_glp != NULL)
   1.583 +            {  xprintf("Only one GLPK LP/MIP output file allowed\n");
   1.584 +               return 1;
   1.585 +            }
   1.586 +            csa->out_glp = argv[k];
   1.587 +         }
   1.588 +         else if (p("--wpb"))
   1.589 +         {  k++;
   1.590 +            if (k == argc || argv[k][0] == '\0' || argv[k][0] == '-')
   1.591 +            {  xprintf("No problem output file specified\n");
   1.592 +               return 1;
   1.593 +            }
   1.594 +            if (csa->out_pb != NULL)
   1.595 +            {  xprintf("Only one OPB output file allowed\n");
   1.596 +               return 1;
   1.597 +            }
   1.598 +            csa->out_pb = argv[k];
   1.599 +         }
   1.600 +         else if (p("--wnpb"))
   1.601 +         {  k++;
   1.602 +            if (k == argc || argv[k][0] == '\0' || argv[k][0] == '-')
   1.603 +            {  xprintf("No problem output file specified\n");
   1.604 +               return 1;
   1.605 +            }
   1.606 +            if (csa->out_npb != NULL)
   1.607 +            {  xprintf("Only one normalized OPB output file allowed\n");
   1.608 +               return 1;
   1.609 +            }
   1.610 +            csa->out_npb = argv[k];
   1.611 +         }
   1.612 +#if 1 /* 06/VIII-2011 */
   1.613 +         else if (p("--wcnf"))
   1.614 +         {  k++;
   1.615 +            if (k == argc || argv[k][0] == '\0' || argv[k][0] == '-')
   1.616 +            {  xprintf("No problem output file specified\n");
   1.617 +               return 1;
   1.618 +            }
   1.619 +            if (csa->out_cnf != NULL)
   1.620 +            {  xprintf("Only one output DIMACS CNF-SAT file allowed\n");
   1.621 +               return 1;
   1.622 +            }
   1.623 +            csa->out_cnf = argv[k];
   1.624 +         }
   1.625 +#endif
   1.626 +         else if (p("--log"))
   1.627 +         {  k++;
   1.628 +            if (k == argc || argv[k][0] == '\0' || argv[k][0] == '-')
   1.629 +            {  xprintf("No log file specified\n");
   1.630 +               return 1;
   1.631 +            }
   1.632 +            if (csa->log_file != NULL)
   1.633 +            {  xprintf("Only one log file allowed\n");
   1.634 +               return 1;
   1.635 +            }
   1.636 +            csa->log_file = argv[k];
   1.637 +         }
   1.638 +         else if (p("-h") || p("--help"))
   1.639 +         {  print_help(argv[0]);
   1.640 +            return -1;
   1.641 +         }
   1.642 +         else if (p("-v") || p("--version"))
   1.643 +         {  print_version(0);
   1.644 +            return -1;
   1.645 +         }
   1.646 +         else if (p("--luf"))
   1.647 +            csa->bfcp.type = GLP_BF_FT;
   1.648 +         else if (p("--cbg"))
   1.649 +            csa->bfcp.type = GLP_BF_BG;
   1.650 +         else if (p("--cgr"))
   1.651 +            csa->bfcp.type = GLP_BF_GR;
   1.652 +         else if (p("--primal"))
   1.653 +            csa->smcp.meth = GLP_PRIMAL;
   1.654 +         else if (p("--dual"))
   1.655 +            csa->smcp.meth = GLP_DUAL;
   1.656 +         else if (p("--std"))
   1.657 +            csa->crash = USE_STD_BASIS;
   1.658 +         else if (p("--adv"))
   1.659 +            csa->crash = USE_ADV_BASIS;
   1.660 +         else if (p("--bib"))
   1.661 +            csa->crash = USE_CPX_BASIS;
   1.662 +         else if (p("--ini"))
   1.663 +         {  csa->crash = USE_INI_BASIS;
   1.664 +            csa->smcp.presolve = GLP_OFF;
   1.665 +            k++;
   1.666 +            if (k == argc || argv[k][0] == '\0' || argv[k][0] == '-')
   1.667 +            {  xprintf("No initial basis file specified\n");
   1.668 +               return 1;
   1.669 +            }
   1.670 +            if (csa->ini_file != NULL)
   1.671 +            {  xprintf("Only one initial basis file allowed\n");
   1.672 +               return 1;
   1.673 +            }
   1.674 +            csa->ini_file = argv[k];
   1.675 +         }
   1.676 +         else if (p("--steep"))
   1.677 +            csa->smcp.pricing = GLP_PT_PSE;
   1.678 +         else if (p("--nosteep"))
   1.679 +            csa->smcp.pricing = GLP_PT_STD;
   1.680 +         else if (p("--relax"))
   1.681 +            csa->smcp.r_test = GLP_RT_HAR;
   1.682 +         else if (p("--norelax"))
   1.683 +            csa->smcp.r_test = GLP_RT_STD;
   1.684 +         else if (p("--presol"))
   1.685 +            csa->smcp.presolve = GLP_ON;
   1.686 +         else if (p("--nopresol"))
   1.687 +            csa->smcp.presolve = GLP_OFF;
   1.688 +         else if (p("--exact"))
   1.689 +            csa->exact = 1;
   1.690 +         else if (p("--xcheck"))
   1.691 +            csa->xcheck = 1;
   1.692 +         else if (p("--nord"))
   1.693 +            csa->iptcp.ord_alg = GLP_ORD_NONE;
   1.694 +         else if (p("--qmd"))
   1.695 +            csa->iptcp.ord_alg = GLP_ORD_QMD;
   1.696 +         else if (p("--amd"))
   1.697 +            csa->iptcp.ord_alg = GLP_ORD_AMD;
   1.698 +         else if (p("--symamd"))
   1.699 +            csa->iptcp.ord_alg = GLP_ORD_SYMAMD;
   1.700 +         else if (p("--nomip"))
   1.701 +            csa->nomip = 1;
   1.702 +         else if (p("--first"))
   1.703 +            csa->iocp.br_tech = GLP_BR_FFV;
   1.704 +         else if (p("--last"))
   1.705 +            csa->iocp.br_tech = GLP_BR_LFV;
   1.706 +         else if (p("--drtom"))
   1.707 +            csa->iocp.br_tech = GLP_BR_DTH;
   1.708 +         else if (p("--mostf"))
   1.709 +            csa->iocp.br_tech = GLP_BR_MFV;
   1.710 +         else if (p("--pcost"))
   1.711 +            csa->iocp.br_tech = GLP_BR_PCH;
   1.712 +         else if (p("--dfs"))
   1.713 +            csa->iocp.bt_tech = GLP_BT_DFS;
   1.714 +         else if (p("--bfs"))
   1.715 +            csa->iocp.bt_tech = GLP_BT_BFS;
   1.716 +         else if (p("--bestp"))
   1.717 +            csa->iocp.bt_tech = GLP_BT_BPH;
   1.718 +         else if (p("--bestb"))
   1.719 +            csa->iocp.bt_tech = GLP_BT_BLB;
   1.720 +         else if (p("--intopt"))
   1.721 +            csa->iocp.presolve = GLP_ON;
   1.722 +         else if (p("--nointopt"))
   1.723 +            csa->iocp.presolve = GLP_OFF;
   1.724 +         else if (p("--binarize"))
   1.725 +            csa->iocp.presolve = csa->iocp.binarize = GLP_ON;
   1.726 +         else if (p("--fpump"))
   1.727 +            csa->iocp.fp_heur = GLP_ON;
   1.728 +         else if (p("--gomory"))
   1.729 +            csa->iocp.gmi_cuts = GLP_ON;
   1.730 +         else if (p("--mir"))
   1.731 +            csa->iocp.mir_cuts = GLP_ON;
   1.732 +         else if (p("--cover"))
   1.733 +            csa->iocp.cov_cuts = GLP_ON;
   1.734 +         else if (p("--clique"))
   1.735 +            csa->iocp.clq_cuts = GLP_ON;
   1.736 +         else if (p("--cuts"))
   1.737 +            csa->iocp.gmi_cuts = csa->iocp.mir_cuts =
   1.738 +            csa->iocp.cov_cuts = csa->iocp.clq_cuts = GLP_ON;
   1.739 +         else if (p("--mipgap"))
   1.740 +         {  double mip_gap;
   1.741 +            k++;
   1.742 +            if (k == argc || argv[k][0] == '\0' || argv[k][0] == '-')
   1.743 +            {  xprintf("No relative gap tolerance specified\n");
   1.744 +               return 1;
   1.745 +            }
   1.746 +            if (str2num(argv[k], &mip_gap) || mip_gap < 0.0)
   1.747 +            {  xprintf("Invalid relative mip gap tolerance `%s'\n",
   1.748 +                  argv[k]);
   1.749 +               return 1;
   1.750 +            }
   1.751 +            csa->iocp.mip_gap = mip_gap;
   1.752 +         }
   1.753 +#if 1 /* 15/VIII-2011 */
   1.754 +         else if (p("--minisat"))
   1.755 +            csa->minisat = 1;
   1.756 +         else if (p("--objbnd"))
   1.757 +         {  k++;
   1.758 +            if (k == argc || argv[k][0] == '\0' ||
   1.759 +               argv[k][0] == '-' && !isdigit((unsigned char)argv[k][1]))
   1.760 +            {  xprintf("No objective bound specified\n");
   1.761 +               return 1;
   1.762 +            }
   1.763 +            csa->minisat = 1;
   1.764 +            csa->use_bnd = 1;
   1.765 +            if (str2int(argv[k], &csa->obj_bnd))
   1.766 +            {  xprintf("Invalid objective bound `%s' (should be integer"
   1.767 +                  " value)\n", argv[k]);
   1.768 +               return 1;
   1.769 +            }
   1.770 +         }
   1.771 +#endif
   1.772 +         else if (argv[k][0] == '-' ||
   1.773 +                 (argv[k][0] == '-' && argv[k][1] == '-'))
   1.774 +         {  xprintf("Invalid option `%s'; try %s --help\n",
   1.775 +               argv[k], argv[0]);
   1.776 +            return 1;
   1.777 +         }
   1.778 +         else
   1.779 +         {  if (csa->in_file != NULL)
   1.780 +            {  xprintf("Only one input problem file allowed\n");
   1.781 +               return 1;
   1.782 +            }
   1.783 +            csa->in_file = argv[k];
   1.784 +         }
   1.785 +      }
   1.786 +#undef p
   1.787 +      return 0;
   1.788 +}
   1.789 +
   1.790 +typedef struct { double rhs, pi; } v_data;
   1.791 +typedef struct { double low, cap, cost, x; } a_data;
   1.792 +
   1.793 +int glp_main(int argc, const char *argv[])
   1.794 +{     /* stand-alone LP/MIP solver */
   1.795 +      struct csa _csa, *csa = &_csa;
   1.796 +      int ret;
   1.797 +      glp_long start;
   1.798 +      /* perform initialization */
   1.799 +      csa->prob = glp_create_prob();
   1.800 +      glp_get_bfcp(csa->prob, &csa->bfcp);
   1.801 +      glp_init_smcp(&csa->smcp);
   1.802 +      csa->smcp.presolve = GLP_ON;
   1.803 +      glp_init_iptcp(&csa->iptcp);
   1.804 +      glp_init_iocp(&csa->iocp);
   1.805 +      csa->iocp.presolve = GLP_ON;
   1.806 +      csa->tran = NULL;
   1.807 +      csa->graph = NULL;
   1.808 +      csa->format = FMT_MPS_FILE;
   1.809 +      csa->in_file = NULL;
   1.810 +      csa->ndf = 0;
   1.811 +      csa->out_dpy = NULL;
   1.812 +      csa->seed = 1;
   1.813 +      csa->solution = SOL_BASIC;
   1.814 +      csa->in_res = NULL;
   1.815 +      csa->dir = 0;
   1.816 +      csa->scale = 1;
   1.817 +      csa->out_sol = NULL;
   1.818 +      csa->out_res = NULL;
   1.819 +      csa->out_ranges = NULL;
   1.820 +      csa->check = 0;
   1.821 +      csa->new_name = NULL;
   1.822 +      csa->out_mps = NULL;
   1.823 +      csa->out_freemps = NULL;
   1.824 +      csa->out_cpxlp = NULL;
   1.825 +      csa->out_glp = NULL;
   1.826 +      csa->out_pb = NULL;
   1.827 +      csa->out_npb = NULL;
   1.828 +#if 1 /* 06/VIII-2011 */
   1.829 +      csa->out_cnf = NULL;
   1.830 +#endif
   1.831 +      csa->log_file = NULL;
   1.832 +      csa->crash = USE_ADV_BASIS;
   1.833 +      csa->ini_file = NULL;
   1.834 +      csa->exact = 0;
   1.835 +      csa->xcheck = 0;
   1.836 +      csa->nomip = 0;
   1.837 +#if 1 /* 15/VIII-2011 */
   1.838 +      csa->minisat = 0;
   1.839 +      csa->use_bnd = 0;
   1.840 +      csa->obj_bnd = 0;
   1.841 +#endif
   1.842 +      /* parse command-line parameters */
   1.843 +      ret = parse_cmdline(csa, argc, argv);
   1.844 +      if (ret < 0)
   1.845 +      {  ret = EXIT_SUCCESS;
   1.846 +         goto done;
   1.847 +      }
   1.848 +      if (ret > 0)
   1.849 +      {  ret = EXIT_FAILURE;
   1.850 +         goto done;
   1.851 +      }
   1.852 +      /*--------------------------------------------------------------*/
   1.853 +      /* remove all output files specified in the command line */
   1.854 +      if (csa->out_dpy != NULL) remove(csa->out_dpy);
   1.855 +      if (csa->out_sol != NULL) remove(csa->out_sol);
   1.856 +      if (csa->out_res != NULL) remove(csa->out_res);
   1.857 +      if (csa->out_ranges != NULL) remove(csa->out_ranges);
   1.858 +      if (csa->out_mps != NULL) remove(csa->out_mps);
   1.859 +      if (csa->out_freemps != NULL) remove(csa->out_freemps);
   1.860 +      if (csa->out_cpxlp != NULL) remove(csa->out_cpxlp);
   1.861 +      if (csa->out_glp != NULL) remove(csa->out_glp);
   1.862 +      if (csa->out_pb != NULL) remove(csa->out_pb);
   1.863 +      if (csa->out_npb != NULL) remove(csa->out_npb);
   1.864 +#if 1 /* 06/VIII-2011 */
   1.865 +      if (csa->out_cnf != NULL) remove(csa->out_cnf);
   1.866 +#endif
   1.867 +      if (csa->log_file != NULL) remove(csa->log_file);
   1.868 +      /*--------------------------------------------------------------*/
   1.869 +      /* open log file, if required */
   1.870 +      if (csa->log_file != NULL)
   1.871 +      {  if (glp_open_tee(csa->log_file))
   1.872 +         {  xprintf("Unable to create log file\n");
   1.873 +            ret = EXIT_FAILURE;
   1.874 +            goto done;
   1.875 +         }
   1.876 +      }
   1.877 +      /*--------------------------------------------------------------*/
   1.878 +      /* print version information */
   1.879 +      print_version(1);
   1.880 +      /*--------------------------------------------------------------*/
   1.881 +      /* print parameters specified in the command line */
   1.882 +      if (argc > 1)
   1.883 +      {  int k, len = INT_MAX;
   1.884 +         xprintf("Parameter(s) specified in the command line:");
   1.885 +         for (k = 1; k < argc; k++)
   1.886 +         {  if (len > 72)
   1.887 +               xprintf("\n"), len = 0;
   1.888 +            xprintf(" %s", argv[k]);
   1.889 +            len += 1 + strlen(argv[k]);
   1.890 +         }
   1.891 +         xprintf("\n");
   1.892 +      }
   1.893 +      /*--------------------------------------------------------------*/
   1.894 +      /* read problem data from the input file */
   1.895 +      if (csa->in_file == NULL)
   1.896 +      {  xprintf("No input problem file specified; try %s --help\n",
   1.897 +            argv[0]);
   1.898 +         ret = EXIT_FAILURE;
   1.899 +         goto done;
   1.900 +      }
   1.901 +      if (csa->format == FMT_MPS_DECK)
   1.902 +      {  ret = glp_read_mps(csa->prob, GLP_MPS_DECK, NULL,
   1.903 +            csa->in_file);
   1.904 +         if (ret != 0)
   1.905 +err1:    {  xprintf("MPS file processing error\n");
   1.906 +            ret = EXIT_FAILURE;
   1.907 +            goto done;
   1.908 +         }
   1.909 +      }
   1.910 +      else if (csa->format == FMT_MPS_FILE)
   1.911 +      {  ret = glp_read_mps(csa->prob, GLP_MPS_FILE, NULL,
   1.912 +            csa->in_file);
   1.913 +         if (ret != 0) goto err1;
   1.914 +      }
   1.915 +      else if (csa->format == FMT_LP)
   1.916 +      {  ret = glp_read_lp(csa->prob, NULL, csa->in_file);
   1.917 +         if (ret != 0)
   1.918 +         {  xprintf("CPLEX LP file processing error\n");
   1.919 +            ret = EXIT_FAILURE;
   1.920 +            goto done;
   1.921 +         }
   1.922 +      }
   1.923 +      else if (csa->format == FMT_GLP)
   1.924 +      {  ret = glp_read_prob(csa->prob, 0, csa->in_file);
   1.925 +         if (ret != 0)
   1.926 +         {  xprintf("GLPK LP/MIP file processing error\n");
   1.927 +            ret = EXIT_FAILURE;
   1.928 +            goto done;
   1.929 +         }
   1.930 +      }
   1.931 +      else if (csa->format == FMT_MATHPROG)
   1.932 +      {  int k;
   1.933 +         /* allocate the translator workspace */
   1.934 +         csa->tran = glp_mpl_alloc_wksp();
   1.935 +         /* set seed value */
   1.936 +         if (csa->seed == 0x80000000)
   1.937 +         {  csa->seed = glp_time().lo;
   1.938 +            xprintf("Seed value %d will be used\n", csa->seed);
   1.939 +         }
   1.940 +         _glp_mpl_init_rand(csa->tran, csa->seed);
   1.941 +         /* read model section and optional data section */
   1.942 +         if (glp_mpl_read_model(csa->tran, csa->in_file, csa->ndf > 0))
   1.943 +err2:    {  xprintf("MathProg model processing error\n");
   1.944 +            ret = EXIT_FAILURE;
   1.945 +            goto done;
   1.946 +         }
   1.947 +         /* read optional data section(s), if necessary */
   1.948 +         for (k = 1; k <= csa->ndf; k++)
   1.949 +         {  if (glp_mpl_read_data(csa->tran, csa->in_data[k]))
   1.950 +               goto err2;
   1.951 +         }
   1.952 +         /* generate the model */
   1.953 +         if (glp_mpl_generate(csa->tran, csa->out_dpy)) goto err2;
   1.954 +         /* build the problem instance from the model */
   1.955 +         glp_mpl_build_prob(csa->tran, csa->prob);
   1.956 +      }
   1.957 +      else if (csa->format == FMT_MIN_COST)
   1.958 +      {  csa->graph = glp_create_graph(sizeof(v_data), sizeof(a_data));
   1.959 +         ret = glp_read_mincost(csa->graph, offsetof(v_data, rhs),
   1.960 +            offsetof(a_data, low), offsetof(a_data, cap),
   1.961 +            offsetof(a_data, cost), csa->in_file);
   1.962 +         if (ret != 0)
   1.963 +         {  xprintf("DIMACS file processing error\n");
   1.964 +            ret = EXIT_FAILURE;
   1.965 +            goto done;
   1.966 +         }
   1.967 +         glp_mincost_lp(csa->prob, csa->graph, GLP_ON,
   1.968 +            offsetof(v_data, rhs), offsetof(a_data, low),
   1.969 +            offsetof(a_data, cap), offsetof(a_data, cost));
   1.970 +         glp_set_prob_name(csa->prob, csa->in_file);
   1.971 +      }
   1.972 +      else if (csa->format == FMT_MAX_FLOW)
   1.973 +      {  int s, t;
   1.974 +         csa->graph = glp_create_graph(sizeof(v_data), sizeof(a_data));
   1.975 +         ret = glp_read_maxflow(csa->graph, &s, &t,
   1.976 +            offsetof(a_data, cap), csa->in_file);
   1.977 +         if (ret != 0)
   1.978 +         {  xprintf("DIMACS file processing error\n");
   1.979 +            ret = EXIT_FAILURE;
   1.980 +            goto done;
   1.981 +         }
   1.982 +         glp_maxflow_lp(csa->prob, csa->graph, GLP_ON, s, t,
   1.983 +            offsetof(a_data, cap));
   1.984 +         glp_set_prob_name(csa->prob, csa->in_file);
   1.985 +      }
   1.986 +#if 1 /* 06/VIII-2011 */
   1.987 +      else if (csa->format == FMT_CNF)
   1.988 +      {  ret = glp_read_cnfsat(csa->prob, csa->in_file);
   1.989 +         if (ret != 0)
   1.990 +         {  xprintf("DIMACS file processing error\n");
   1.991 +            ret = EXIT_FAILURE;
   1.992 +            goto done;
   1.993 +         }
   1.994 +         glp_set_prob_name(csa->prob, csa->in_file);
   1.995 +      }
   1.996 +#endif
   1.997 +      else
   1.998 +         xassert(csa != csa);
   1.999 +      /*--------------------------------------------------------------*/
  1.1000 +      /* change problem name, if required */
  1.1001 +      if (csa->new_name != NULL)
  1.1002 +         glp_set_prob_name(csa->prob, csa->new_name);
  1.1003 +      /* change optimization direction, if required */
  1.1004 +      if (csa->dir != 0)
  1.1005 +         glp_set_obj_dir(csa->prob, csa->dir);
  1.1006 +      /* sort elements of the constraint matrix */
  1.1007 +      glp_sort_matrix(csa->prob);
  1.1008 +      /*--------------------------------------------------------------*/
  1.1009 +      /* write problem data in fixed MPS format, if required */
  1.1010 +      if (csa->out_mps != NULL)
  1.1011 +      {  ret = glp_write_mps(csa->prob, GLP_MPS_DECK, NULL,
  1.1012 +            csa->out_mps);
  1.1013 +         if (ret != 0)
  1.1014 +         {  xprintf("Unable to write problem in fixed MPS format\n");
  1.1015 +            ret = EXIT_FAILURE;
  1.1016 +            goto done;
  1.1017 +         }
  1.1018 +      }
  1.1019 +      /* write problem data in free MPS format, if required */
  1.1020 +      if (csa->out_freemps != NULL)
  1.1021 +      {  ret = glp_write_mps(csa->prob, GLP_MPS_FILE, NULL,
  1.1022 +            csa->out_freemps);
  1.1023 +         if (ret != 0)
  1.1024 +         {  xprintf("Unable to write problem in free MPS format\n");
  1.1025 +            ret = EXIT_FAILURE;
  1.1026 +            goto done;
  1.1027 +         }
  1.1028 +      }
  1.1029 +      /* write problem data in CPLEX LP format, if required */
  1.1030 +      if (csa->out_cpxlp != NULL)
  1.1031 +      {  ret = glp_write_lp(csa->prob, NULL, csa->out_cpxlp);
  1.1032 +         if (ret != 0)
  1.1033 +         {  xprintf("Unable to write problem in CPLEX LP format\n");
  1.1034 +            ret = EXIT_FAILURE;
  1.1035 +            goto done;
  1.1036 +         }
  1.1037 +      }
  1.1038 +      /* write problem data in GLPK format, if required */
  1.1039 +      if (csa->out_glp != NULL)
  1.1040 +      {  ret = glp_write_prob(csa->prob, 0, csa->out_glp);
  1.1041 +         if (ret != 0)
  1.1042 +         {  xprintf("Unable to write problem in GLPK format\n");
  1.1043 +            ret = EXIT_FAILURE;
  1.1044 +            goto done;
  1.1045 +         }
  1.1046 +      }
  1.1047 +      /* write problem data in OPB format, if required */
  1.1048 +      if (csa->out_pb != NULL)
  1.1049 +      {  ret = lpx_write_pb(csa->prob, csa->out_pb, 0, 0);
  1.1050 +         if (ret != 0)
  1.1051 +         {  xprintf("Unable to write problem in OPB format\n");
  1.1052 +            ret = EXIT_FAILURE;
  1.1053 +            goto done;
  1.1054 +         }
  1.1055 +      }
  1.1056 +      /* write problem data in normalized OPB format, if required */
  1.1057 +      if (csa->out_npb != NULL)
  1.1058 +      {  ret = lpx_write_pb(csa->prob, csa->out_npb, 1, 1);
  1.1059 +         if (ret != 0)
  1.1060 +         {  xprintf(
  1.1061 +               "Unable to write problem in normalized OPB format\n");
  1.1062 +            ret = EXIT_FAILURE;
  1.1063 +            goto done;
  1.1064 +         }
  1.1065 +      }
  1.1066 +#if 1 /* 06/VIII-2011 */
  1.1067 +      /* write problem data in DIMACS CNF-SAT format, if required */
  1.1068 +      if (csa->out_cnf != NULL)
  1.1069 +      {  ret = glp_write_cnfsat(csa->prob, csa->out_cnf);
  1.1070 +         if (ret != 0)
  1.1071 +         {  xprintf(
  1.1072 +               "Unable to write problem in DIMACS CNF-SAT format\n");
  1.1073 +            ret = EXIT_FAILURE;
  1.1074 +            goto done;
  1.1075 +         }
  1.1076 +      }
  1.1077 +#endif
  1.1078 +      /*--------------------------------------------------------------*/
  1.1079 +      /* if only problem data check is required, skip computations */
  1.1080 +      if (csa->check)
  1.1081 +      {  ret = EXIT_SUCCESS;
  1.1082 +         goto done;
  1.1083 +      }
  1.1084 +      /*--------------------------------------------------------------*/
  1.1085 +      /* determine the solution type */
  1.1086 +      if (!csa->nomip &&
  1.1087 +          glp_get_num_int(csa->prob) + glp_get_num_bin(csa->prob) > 0)
  1.1088 +      {  if (csa->solution == SOL_INTERIOR)
  1.1089 +         {  xprintf("Interior-point method is not able to solve MIP pro"
  1.1090 +               "blem; use --simplex\n");
  1.1091 +            ret = EXIT_FAILURE;
  1.1092 +            goto done;
  1.1093 +         }
  1.1094 +         csa->solution = SOL_INTEGER;
  1.1095 +      }
  1.1096 +      /*--------------------------------------------------------------*/
  1.1097 +      /* if solution is provided, read it and skip computations */
  1.1098 +      if (csa->in_res != NULL)
  1.1099 +      {  if (csa->solution == SOL_BASIC)
  1.1100 +            ret = glp_read_sol(csa->prob, csa->in_res);
  1.1101 +         else if (csa->solution == SOL_INTERIOR)
  1.1102 +            ret = glp_read_ipt(csa->prob, csa->in_res);
  1.1103 +         else if (csa->solution == SOL_INTEGER)
  1.1104 +            ret = glp_read_mip(csa->prob, csa->in_res);
  1.1105 +         else
  1.1106 +            xassert(csa != csa);
  1.1107 +         if (ret != 0)
  1.1108 +         {  xprintf("Unable to read problem solution\n");
  1.1109 +            ret = EXIT_FAILURE;
  1.1110 +            goto done;
  1.1111 +         }
  1.1112 +         goto skip;
  1.1113 +      }
  1.1114 +      /*--------------------------------------------------------------*/
  1.1115 +      /* scale the problem data, if required */
  1.1116 +      if (csa->scale)
  1.1117 +      {  if (csa->solution == SOL_BASIC && !csa->smcp.presolve ||
  1.1118 +             csa->solution == SOL_INTERIOR ||
  1.1119 +             csa->solution == SOL_INTEGER && !csa->iocp.presolve)
  1.1120 +            glp_scale_prob(csa->prob, GLP_SF_AUTO);
  1.1121 +      }
  1.1122 +      /*--------------------------------------------------------------*/
  1.1123 +      /* construct starting LP basis */
  1.1124 +      if (csa->solution == SOL_BASIC && !csa->smcp.presolve ||
  1.1125 +          csa->solution == SOL_INTEGER && !csa->iocp.presolve)
  1.1126 +      {  if (csa->crash == USE_STD_BASIS)
  1.1127 +            glp_std_basis(csa->prob);
  1.1128 +         else if (csa->crash == USE_ADV_BASIS)
  1.1129 +            glp_adv_basis(csa->prob, 0);
  1.1130 +         else if (csa->crash == USE_CPX_BASIS)
  1.1131 +            glp_cpx_basis(csa->prob);
  1.1132 +         else if (csa->crash == USE_INI_BASIS)
  1.1133 +         {  ret = glp_read_sol(csa->prob, csa->ini_file);
  1.1134 +            if (ret != 0)
  1.1135 +            {  xprintf("Unable to read initial basis\n");
  1.1136 +               ret = EXIT_FAILURE;
  1.1137 +               goto done;
  1.1138 +            }
  1.1139 +         }
  1.1140 +         else
  1.1141 +            xassert(csa != csa);
  1.1142 +      }
  1.1143 +      /*--------------------------------------------------------------*/
  1.1144 +      /* solve the problem */
  1.1145 +      start = xtime();
  1.1146 +      if (csa->solution == SOL_BASIC)
  1.1147 +      {  if (!csa->exact)
  1.1148 +         {  glp_set_bfcp(csa->prob, &csa->bfcp);
  1.1149 +            glp_simplex(csa->prob, &csa->smcp);
  1.1150 +            if (csa->xcheck)
  1.1151 +            {  if (csa->smcp.presolve &&
  1.1152 +                   glp_get_status(csa->prob) != GLP_OPT)
  1.1153 +                  xprintf("If you need to check final basis for non-opt"
  1.1154 +                     "imal solution, use --nopresol\n");
  1.1155 +               else
  1.1156 +                  glp_exact(csa->prob, &csa->smcp);
  1.1157 +            }
  1.1158 +            if (csa->out_sol != NULL || csa->out_res != NULL)
  1.1159 +            {  if (csa->smcp.presolve &&
  1.1160 +                   glp_get_status(csa->prob) != GLP_OPT)
  1.1161 +               xprintf("If you need actual output for non-optimal solut"
  1.1162 +                  "ion, use --nopresol\n");
  1.1163 +            }
  1.1164 +         }
  1.1165 +         else
  1.1166 +            glp_exact(csa->prob, &csa->smcp);
  1.1167 +      }
  1.1168 +      else if (csa->solution == SOL_INTERIOR)
  1.1169 +         glp_interior(csa->prob, &csa->iptcp);
  1.1170 +#if 1 /* 15/VIII-2011 */
  1.1171 +      else if (csa->solution == SOL_INTEGER && csa->minisat)
  1.1172 +      {  if (glp_check_cnfsat(csa->prob) == 0)
  1.1173 +            glp_minisat1(csa->prob);
  1.1174 +         else
  1.1175 +            glp_intfeas1(csa->prob, csa->use_bnd, csa->obj_bnd);
  1.1176 +      }
  1.1177 +#endif
  1.1178 +      else if (csa->solution == SOL_INTEGER)
  1.1179 +      {  if (!csa->iocp.presolve)
  1.1180 +         {  glp_set_bfcp(csa->prob, &csa->bfcp);
  1.1181 +            glp_simplex(csa->prob, &csa->smcp);
  1.1182 +         }
  1.1183 +#if 0
  1.1184 +         csa->iocp.msg_lev = GLP_MSG_DBG;
  1.1185 +         csa->iocp.pp_tech = GLP_PP_NONE;
  1.1186 +#endif
  1.1187 +         glp_intopt(csa->prob, &csa->iocp);
  1.1188 +      }
  1.1189 +      else
  1.1190 +         xassert(csa != csa);
  1.1191 +      /*--------------------------------------------------------------*/
  1.1192 +      /* display statistics */
  1.1193 +      xprintf("Time used:   %.1f secs\n", xdifftime(xtime(), start));
  1.1194 +      {  glp_long tpeak;
  1.1195 +         char buf[50];
  1.1196 +         glp_mem_usage(NULL, NULL, NULL, &tpeak);
  1.1197 +         xprintf("Memory used: %.1f Mb (%s bytes)\n",
  1.1198 +            xltod(tpeak) / 1048576.0, xltoa(tpeak, buf));
  1.1199 +      }
  1.1200 +      /*--------------------------------------------------------------*/
  1.1201 +skip: /* postsolve the model, if necessary */
  1.1202 +      if (csa->tran != NULL)
  1.1203 +      {  if (csa->solution == SOL_BASIC)
  1.1204 +         {  if (!(glp_get_status(csa->prob) == GLP_OPT ||
  1.1205 +                  glp_get_status(csa->prob) == GLP_FEAS))
  1.1206 +               ret = -1;
  1.1207 +            else
  1.1208 +               ret = glp_mpl_postsolve(csa->tran, csa->prob, GLP_SOL);
  1.1209 +         }
  1.1210 +         else if (csa->solution == SOL_INTERIOR)
  1.1211 +         {  if (!(glp_ipt_status(csa->prob) == GLP_OPT ||
  1.1212 +                  glp_ipt_status(csa->prob) == GLP_FEAS))
  1.1213 +               ret = -1;
  1.1214 +            else
  1.1215 +               ret = glp_mpl_postsolve(csa->tran, csa->prob, GLP_IPT);
  1.1216 +         }
  1.1217 +         else if (csa->solution == SOL_INTEGER)
  1.1218 +         {  if (!(glp_mip_status(csa->prob) == GLP_OPT ||
  1.1219 +                  glp_mip_status(csa->prob) == GLP_FEAS))
  1.1220 +               ret = -1;
  1.1221 +            else
  1.1222 +               ret = glp_mpl_postsolve(csa->tran, csa->prob, GLP_MIP);
  1.1223 +         }
  1.1224 +         else
  1.1225 +            xassert(csa != csa);
  1.1226 +         if (ret > 0)
  1.1227 +         {  xprintf("Model postsolving error\n");
  1.1228 +            ret = EXIT_FAILURE;
  1.1229 +            goto done;
  1.1230 +         }
  1.1231 +      }
  1.1232 +      /*--------------------------------------------------------------*/
  1.1233 +      /* write problem solution in printable format, if required */
  1.1234 +      if (csa->out_sol != NULL)
  1.1235 +      {  if (csa->solution == SOL_BASIC)
  1.1236 +            ret = lpx_print_sol(csa->prob, csa->out_sol);
  1.1237 +         else if (csa->solution == SOL_INTERIOR)
  1.1238 +            ret = lpx_print_ips(csa->prob, csa->out_sol);
  1.1239 +         else if (csa->solution == SOL_INTEGER)
  1.1240 +            ret = lpx_print_mip(csa->prob, csa->out_sol);
  1.1241 +         else
  1.1242 +            xassert(csa != csa);
  1.1243 +         if (ret != 0)
  1.1244 +         {  xprintf("Unable to write problem solution\n");
  1.1245 +            ret = EXIT_FAILURE;
  1.1246 +            goto done;
  1.1247 +         }
  1.1248 +      }
  1.1249 +      /* write problem solution in printable format, if required */
  1.1250 +      if (csa->out_res != NULL)
  1.1251 +      {  if (csa->solution == SOL_BASIC)
  1.1252 +            ret = glp_write_sol(csa->prob, csa->out_res);
  1.1253 +         else if (csa->solution == SOL_INTERIOR)
  1.1254 +            ret = glp_write_ipt(csa->prob, csa->out_res);
  1.1255 +         else if (csa->solution == SOL_INTEGER)
  1.1256 +            ret = glp_write_mip(csa->prob, csa->out_res);
  1.1257 +         else
  1.1258 +            xassert(csa != csa);
  1.1259 +         if (ret != 0)
  1.1260 +         {  xprintf("Unable to write problem solution\n");
  1.1261 +            ret = EXIT_FAILURE;
  1.1262 +            goto done;
  1.1263 +         }
  1.1264 +      }
  1.1265 +      /* write sensitivity analysis report, if required */
  1.1266 +      if (csa->out_ranges != NULL)
  1.1267 +      {  if (csa->solution == SOL_BASIC)
  1.1268 +         {  if (glp_get_status(csa->prob) == GLP_OPT)
  1.1269 +            {  if (glp_bf_exists(csa->prob))
  1.1270 +ranges:        {  ret = glp_print_ranges(csa->prob, 0, NULL, 0,
  1.1271 +                     csa->out_ranges);
  1.1272 +                  if (ret != 0)
  1.1273 +                  {  xprintf("Unable to write sensitivity analysis repo"
  1.1274 +                        "rt\n");
  1.1275 +                     ret = EXIT_FAILURE;
  1.1276 +                     goto done;
  1.1277 +                  }
  1.1278 +               }
  1.1279 +               else
  1.1280 +               {  ret = glp_factorize(csa->prob);
  1.1281 +                  if (ret == 0) goto ranges;
  1.1282 +                  xprintf("Cannot produce sensitivity analysis report d"
  1.1283 +                     "ue to error in basis factorization (glp_factorize"
  1.1284 +                     " returned %d); try --nopresol\n", ret);
  1.1285 +               }
  1.1286 +            }
  1.1287 +            else
  1.1288 +               xprintf("Cannot produce sensitivity analysis report for "
  1.1289 +                  "non-optimal basic solution\n");
  1.1290 +         }
  1.1291 +         else
  1.1292 +            xprintf("Cannot produce sensitivity analysis report for int"
  1.1293 +               "erior-point or MIP solution\n");
  1.1294 +      }
  1.1295 +      /*--------------------------------------------------------------*/
  1.1296 +      /* all seems to be ok */
  1.1297 +      ret = EXIT_SUCCESS;
  1.1298 +      /*--------------------------------------------------------------*/
  1.1299 +done: /* delete the LP/MIP problem object */
  1.1300 +      if (csa->prob != NULL)
  1.1301 +         glp_delete_prob(csa->prob);
  1.1302 +      /* free the translator workspace, if necessary */
  1.1303 +      if (csa->tran != NULL)
  1.1304 +         glp_mpl_free_wksp(csa->tran);
  1.1305 +      /* delete the network problem object, if necessary */
  1.1306 +      if (csa->graph != NULL)
  1.1307 +         glp_delete_graph(csa->graph);
  1.1308 +      xassert(gmp_pool_count() == 0);
  1.1309 +      gmp_free_mem();
  1.1310 +      /* close log file, if necessary */
  1.1311 +      if (csa->log_file != NULL) glp_close_tee();
  1.1312 +      /* check that no memory blocks are still allocated */
  1.1313 +      {  int count;
  1.1314 +         glp_long total;
  1.1315 +         glp_mem_usage(&count, NULL, &total, NULL);
  1.1316 +         if (count != 0)
  1.1317 +            xerror("Error: %d memory block(s) were lost\n", count);
  1.1318 +         xassert(count == 0);
  1.1319 +         xassert(total.lo == 0 && total.hi == 0);
  1.1320 +      }
  1.1321 +      /* free the GLPK environment */
  1.1322 +      glp_free_env();
  1.1323 +      /* return to the control program */
  1.1324 +      return ret;
  1.1325 +}
  1.1326 +
  1.1327 +/* eof */