lemon-project-template-glpk

annotate deps/glpk/src/glpcpx.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
rev   line source
alpar@9 1 /* glpcpx.c (CPLEX LP format routines) */
alpar@9 2
alpar@9 3 /***********************************************************************
alpar@9 4 * This code is part of GLPK (GNU Linear Programming Kit).
alpar@9 5 *
alpar@9 6 * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
alpar@9 7 * 2009, 2010, 2011 Andrew Makhorin, Department for Applied Informatics,
alpar@9 8 * Moscow Aviation Institute, Moscow, Russia. All rights reserved.
alpar@9 9 * E-mail: <mao@gnu.org>.
alpar@9 10 *
alpar@9 11 * GLPK is free software: you can redistribute it and/or modify it
alpar@9 12 * under the terms of the GNU General Public License as published by
alpar@9 13 * the Free Software Foundation, either version 3 of the License, or
alpar@9 14 * (at your option) any later version.
alpar@9 15 *
alpar@9 16 * GLPK is distributed in the hope that it will be useful, but WITHOUT
alpar@9 17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
alpar@9 18 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
alpar@9 19 * License for more details.
alpar@9 20 *
alpar@9 21 * You should have received a copy of the GNU General Public License
alpar@9 22 * along with GLPK. If not, see <http://www.gnu.org/licenses/>.
alpar@9 23 ***********************************************************************/
alpar@9 24
alpar@9 25 #include "glpapi.h"
alpar@9 26
alpar@9 27 /***********************************************************************
alpar@9 28 * NAME
alpar@9 29 *
alpar@9 30 * glp_init_cpxcp - initialize CPLEX LP format control parameters
alpar@9 31 *
alpar@9 32 * SYNOPSIS
alpar@9 33 *
alpar@9 34 * void glp_init_cpxcp(glp_cpxcp *parm):
alpar@9 35 *
alpar@9 36 * The routine glp_init_cpxcp initializes control parameters used by
alpar@9 37 * the CPLEX LP input/output routines glp_read_lp and glp_write_lp with
alpar@9 38 * default values.
alpar@9 39 *
alpar@9 40 * Default values of the control parameters are stored in the glp_cpxcp
alpar@9 41 * structure, which the parameter parm points to. */
alpar@9 42
alpar@9 43 void glp_init_cpxcp(glp_cpxcp *parm)
alpar@9 44 { xassert(parm != NULL);
alpar@9 45 return;
alpar@9 46 }
alpar@9 47
alpar@9 48 static void check_parm(const char *func, const glp_cpxcp *parm)
alpar@9 49 { /* check control parameters */
alpar@9 50 xassert(func != NULL);
alpar@9 51 xassert(parm != NULL);
alpar@9 52 return;
alpar@9 53 }
alpar@9 54
alpar@9 55 /***********************************************************************
alpar@9 56 * NAME
alpar@9 57 *
alpar@9 58 * glp_read_lp - read problem data in CPLEX LP format
alpar@9 59 *
alpar@9 60 * SYNOPSIS
alpar@9 61 *
alpar@9 62 * int glp_read_lp(glp_prob *P, const glp_cpxcp *parm, const char
alpar@9 63 * *fname);
alpar@9 64 *
alpar@9 65 * DESCRIPTION
alpar@9 66 *
alpar@9 67 * The routine glp_read_lp reads problem data in CPLEX LP format from
alpar@9 68 * a text file.
alpar@9 69 *
alpar@9 70 * The parameter parm is a pointer to the structure glp_cpxcp, which
alpar@9 71 * specifies control parameters used by the routine. If parm is NULL,
alpar@9 72 * the routine uses default settings.
alpar@9 73 *
alpar@9 74 * The character string fname specifies a name of the text file to be
alpar@9 75 * read.
alpar@9 76 *
alpar@9 77 * Note that before reading data the current content of the problem
alpar@9 78 * object is completely erased with the routine glp_erase_prob.
alpar@9 79 *
alpar@9 80 * RETURNS
alpar@9 81 *
alpar@9 82 * If the operation was successful, the routine glp_read_lp returns
alpar@9 83 * zero. Otherwise, it prints an error message and returns non-zero. */
alpar@9 84
alpar@9 85 struct csa
alpar@9 86 { /* common storage area */
alpar@9 87 glp_prob *P;
alpar@9 88 /* LP/MIP problem object */
alpar@9 89 const glp_cpxcp *parm;
alpar@9 90 /* pointer to control parameters */
alpar@9 91 const char *fname;
alpar@9 92 /* name of input CPLEX LP file */
alpar@9 93 XFILE *fp;
alpar@9 94 /* stream assigned to input CPLEX LP file */
alpar@9 95 jmp_buf jump;
alpar@9 96 /* label for go to in case of error */
alpar@9 97 int count;
alpar@9 98 /* line count */
alpar@9 99 int c;
alpar@9 100 /* current character or XEOF */
alpar@9 101 int token;
alpar@9 102 /* current token: */
alpar@9 103 #define T_EOF 0x00 /* end of file */
alpar@9 104 #define T_MINIMIZE 0x01 /* keyword 'minimize' */
alpar@9 105 #define T_MAXIMIZE 0x02 /* keyword 'maximize' */
alpar@9 106 #define T_SUBJECT_TO 0x03 /* keyword 'subject to' */
alpar@9 107 #define T_BOUNDS 0x04 /* keyword 'bounds' */
alpar@9 108 #define T_GENERAL 0x05 /* keyword 'general' */
alpar@9 109 #define T_INTEGER 0x06 /* keyword 'integer' */
alpar@9 110 #define T_BINARY 0x07 /* keyword 'binary' */
alpar@9 111 #define T_END 0x08 /* keyword 'end' */
alpar@9 112 #define T_NAME 0x09 /* symbolic name */
alpar@9 113 #define T_NUMBER 0x0A /* numeric constant */
alpar@9 114 #define T_PLUS 0x0B /* delimiter '+' */
alpar@9 115 #define T_MINUS 0x0C /* delimiter '-' */
alpar@9 116 #define T_COLON 0x0D /* delimiter ':' */
alpar@9 117 #define T_LE 0x0E /* delimiter '<=' */
alpar@9 118 #define T_GE 0x0F /* delimiter '>=' */
alpar@9 119 #define T_EQ 0x10 /* delimiter '=' */
alpar@9 120 char image[255+1];
alpar@9 121 /* image of current token */
alpar@9 122 int imlen;
alpar@9 123 /* length of token image */
alpar@9 124 double value;
alpar@9 125 /* value of numeric constant */
alpar@9 126 int n_max;
alpar@9 127 /* length of the following five arrays (enlarged automatically,
alpar@9 128 if necessary) */
alpar@9 129 int *ind; /* int ind[1+n_max]; */
alpar@9 130 double *val; /* double val[1+n_max]; */
alpar@9 131 char *flag; /* char flag[1+n_max]; */
alpar@9 132 /* working arrays used to construct linear forms */
alpar@9 133 double *lb; /* double lb[1+n_max]; */
alpar@9 134 double *ub; /* double ub[1+n_max]; */
alpar@9 135 /* lower and upper bounds of variables (columns) */
alpar@9 136 };
alpar@9 137
alpar@9 138 #define CHAR_SET "!\"#$%&()/,.;?@_`'{}|~"
alpar@9 139 /* characters, which may appear in symbolic names */
alpar@9 140
alpar@9 141 static void error(struct csa *csa, const char *fmt, ...)
alpar@9 142 { /* print error message and terminate processing */
alpar@9 143 va_list arg;
alpar@9 144 xprintf("%s:%d: ", csa->fname, csa->count);
alpar@9 145 va_start(arg, fmt);
alpar@9 146 xvprintf(fmt, arg);
alpar@9 147 va_end(arg);
alpar@9 148 longjmp(csa->jump, 1);
alpar@9 149 /* no return */
alpar@9 150 }
alpar@9 151
alpar@9 152 static void warning(struct csa *csa, const char *fmt, ...)
alpar@9 153 { /* print warning message and continue processing */
alpar@9 154 va_list arg;
alpar@9 155 xprintf("%s:%d: warning: ", csa->fname, csa->count);
alpar@9 156 va_start(arg, fmt);
alpar@9 157 xvprintf(fmt, arg);
alpar@9 158 va_end(arg);
alpar@9 159 return;
alpar@9 160 }
alpar@9 161
alpar@9 162 static void read_char(struct csa *csa)
alpar@9 163 { /* read next character from input file */
alpar@9 164 int c;
alpar@9 165 xassert(csa->c != XEOF);
alpar@9 166 if (csa->c == '\n') csa->count++;
alpar@9 167 c = xfgetc(csa->fp);
alpar@9 168 if (c < 0)
alpar@9 169 { if (xferror(csa->fp))
alpar@9 170 error(csa, "read error - %s\n", xerrmsg());
alpar@9 171 else if (csa->c == '\n')
alpar@9 172 { csa->count--;
alpar@9 173 c = XEOF;
alpar@9 174 }
alpar@9 175 else
alpar@9 176 { warning(csa, "missing final end of line\n");
alpar@9 177 c = '\n';
alpar@9 178 }
alpar@9 179 }
alpar@9 180 else if (c == '\n')
alpar@9 181 ;
alpar@9 182 else if (isspace(c))
alpar@9 183 c = ' ';
alpar@9 184 else if (iscntrl(c))
alpar@9 185 error(csa, "invalid control character 0x%02X\n", c);
alpar@9 186 csa->c = c;
alpar@9 187 return;
alpar@9 188 }
alpar@9 189
alpar@9 190 static void add_char(struct csa *csa)
alpar@9 191 { /* append current character to current token */
alpar@9 192 if (csa->imlen == sizeof(csa->image)-1)
alpar@9 193 error(csa, "token `%.15s...' too long\n", csa->image);
alpar@9 194 csa->image[csa->imlen++] = (char)csa->c;
alpar@9 195 csa->image[csa->imlen] = '\0';
alpar@9 196 read_char(csa);
alpar@9 197 return;
alpar@9 198 }
alpar@9 199
alpar@9 200 static int the_same(char *s1, char *s2)
alpar@9 201 { /* compare two character strings ignoring case sensitivity */
alpar@9 202 for (; *s1 != '\0'; s1++, s2++)
alpar@9 203 { if (tolower((unsigned char)*s1) != tolower((unsigned char)*s2))
alpar@9 204 return 0;
alpar@9 205 }
alpar@9 206 return 1;
alpar@9 207 }
alpar@9 208
alpar@9 209 static void scan_token(struct csa *csa)
alpar@9 210 { /* scan next token */
alpar@9 211 int flag;
alpar@9 212 csa->token = -1;
alpar@9 213 csa->image[0] = '\0';
alpar@9 214 csa->imlen = 0;
alpar@9 215 csa->value = 0.0;
alpar@9 216 loop: flag = 0;
alpar@9 217 /* skip non-significant characters */
alpar@9 218 while (csa->c == ' ') read_char(csa);
alpar@9 219 /* recognize and scan current token */
alpar@9 220 if (csa->c == XEOF)
alpar@9 221 csa->token = T_EOF;
alpar@9 222 else if (csa->c == '\n')
alpar@9 223 { read_char(csa);
alpar@9 224 /* if the next character is letter, it may begin a keyword */
alpar@9 225 if (isalpha(csa->c))
alpar@9 226 { flag = 1;
alpar@9 227 goto name;
alpar@9 228 }
alpar@9 229 goto loop;
alpar@9 230 }
alpar@9 231 else if (csa->c == '\\')
alpar@9 232 { /* comment; ignore everything until end-of-line */
alpar@9 233 while (csa->c != '\n') read_char(csa);
alpar@9 234 goto loop;
alpar@9 235 }
alpar@9 236 else if (isalpha(csa->c) || csa->c != '.' && strchr(CHAR_SET,
alpar@9 237 csa->c) != NULL)
alpar@9 238 name: { /* symbolic name */
alpar@9 239 csa->token = T_NAME;
alpar@9 240 while (isalnum(csa->c) || strchr(CHAR_SET, csa->c) != NULL)
alpar@9 241 add_char(csa);
alpar@9 242 if (flag)
alpar@9 243 { /* check for keyword */
alpar@9 244 if (the_same(csa->image, "minimize"))
alpar@9 245 csa->token = T_MINIMIZE;
alpar@9 246 else if (the_same(csa->image, "minimum"))
alpar@9 247 csa->token = T_MINIMIZE;
alpar@9 248 else if (the_same(csa->image, "min"))
alpar@9 249 csa->token = T_MINIMIZE;
alpar@9 250 else if (the_same(csa->image, "maximize"))
alpar@9 251 csa->token = T_MAXIMIZE;
alpar@9 252 else if (the_same(csa->image, "maximum"))
alpar@9 253 csa->token = T_MAXIMIZE;
alpar@9 254 else if (the_same(csa->image, "max"))
alpar@9 255 csa->token = T_MAXIMIZE;
alpar@9 256 else if (the_same(csa->image, "subject"))
alpar@9 257 { if (csa->c == ' ')
alpar@9 258 { read_char(csa);
alpar@9 259 if (tolower(csa->c) == 't')
alpar@9 260 { csa->token = T_SUBJECT_TO;
alpar@9 261 csa->image[csa->imlen++] = ' ';
alpar@9 262 csa->image[csa->imlen] = '\0';
alpar@9 263 add_char(csa);
alpar@9 264 if (tolower(csa->c) != 'o')
alpar@9 265 error(csa, "keyword `subject to' incomplete\n");
alpar@9 266 add_char(csa);
alpar@9 267 if (isalpha(csa->c))
alpar@9 268 error(csa, "keyword `%s%c...' not recognized\n",
alpar@9 269 csa->image, csa->c);
alpar@9 270 }
alpar@9 271 }
alpar@9 272 }
alpar@9 273 else if (the_same(csa->image, "such"))
alpar@9 274 { if (csa->c == ' ')
alpar@9 275 { read_char(csa);
alpar@9 276 if (tolower(csa->c) == 't')
alpar@9 277 { csa->token = T_SUBJECT_TO;
alpar@9 278 csa->image[csa->imlen++] = ' ';
alpar@9 279 csa->image[csa->imlen] = '\0';
alpar@9 280 add_char(csa);
alpar@9 281 if (tolower(csa->c) != 'h')
alpar@9 282 err: error(csa, "keyword `such that' incomplete\n");
alpar@9 283 add_char(csa);
alpar@9 284 if (tolower(csa->c) != 'a') goto err;
alpar@9 285 add_char(csa);
alpar@9 286 if (tolower(csa->c) != 't') goto err;
alpar@9 287 add_char(csa);
alpar@9 288 if (isalpha(csa->c))
alpar@9 289 error(csa, "keyword `%s%c...' not recognized\n",
alpar@9 290 csa->image, csa->c);
alpar@9 291 }
alpar@9 292 }
alpar@9 293 }
alpar@9 294 else if (the_same(csa->image, "st"))
alpar@9 295 csa->token = T_SUBJECT_TO;
alpar@9 296 else if (the_same(csa->image, "s.t."))
alpar@9 297 csa->token = T_SUBJECT_TO;
alpar@9 298 else if (the_same(csa->image, "st."))
alpar@9 299 csa->token = T_SUBJECT_TO;
alpar@9 300 else if (the_same(csa->image, "bounds"))
alpar@9 301 csa->token = T_BOUNDS;
alpar@9 302 else if (the_same(csa->image, "bound"))
alpar@9 303 csa->token = T_BOUNDS;
alpar@9 304 else if (the_same(csa->image, "general"))
alpar@9 305 csa->token = T_GENERAL;
alpar@9 306 else if (the_same(csa->image, "generals"))
alpar@9 307 csa->token = T_GENERAL;
alpar@9 308 else if (the_same(csa->image, "gen"))
alpar@9 309 csa->token = T_GENERAL;
alpar@9 310 else if (the_same(csa->image, "integer"))
alpar@9 311 csa->token = T_INTEGER;
alpar@9 312 else if (the_same(csa->image, "integers"))
alpar@9 313 csa->token = T_INTEGER;
alpar@9 314 else if (the_same(csa->image, "int"))
alpar@9 315 csa->token = T_INTEGER;
alpar@9 316 else if (the_same(csa->image, "binary"))
alpar@9 317 csa->token = T_BINARY;
alpar@9 318 else if (the_same(csa->image, "binaries"))
alpar@9 319 csa->token = T_BINARY;
alpar@9 320 else if (the_same(csa->image, "bin"))
alpar@9 321 csa->token = T_BINARY;
alpar@9 322 else if (the_same(csa->image, "end"))
alpar@9 323 csa->token = T_END;
alpar@9 324 }
alpar@9 325 }
alpar@9 326 else if (isdigit(csa->c) || csa->c == '.')
alpar@9 327 { /* numeric constant */
alpar@9 328 csa->token = T_NUMBER;
alpar@9 329 /* scan integer part */
alpar@9 330 while (isdigit(csa->c)) add_char(csa);
alpar@9 331 /* scan optional fractional part (it is mandatory, if there is
alpar@9 332 no integer part) */
alpar@9 333 if (csa->c == '.')
alpar@9 334 { add_char(csa);
alpar@9 335 if (csa->imlen == 1 && !isdigit(csa->c))
alpar@9 336 error(csa, "invalid use of decimal point\n");
alpar@9 337 while (isdigit(csa->c)) add_char(csa);
alpar@9 338 }
alpar@9 339 /* scan optional decimal exponent */
alpar@9 340 if (csa->c == 'e' || csa->c == 'E')
alpar@9 341 { add_char(csa);
alpar@9 342 if (csa->c == '+' || csa->c == '-') add_char(csa);
alpar@9 343 if (!isdigit(csa->c))
alpar@9 344 error(csa, "numeric constant `%s' incomplete\n",
alpar@9 345 csa->image);
alpar@9 346 while (isdigit(csa->c)) add_char(csa);
alpar@9 347 }
alpar@9 348 /* convert the numeric constant to floating-point */
alpar@9 349 if (str2num(csa->image, &csa->value))
alpar@9 350 error(csa, "numeric constant `%s' out of range\n",
alpar@9 351 csa->image);
alpar@9 352 }
alpar@9 353 else if (csa->c == '+')
alpar@9 354 csa->token = T_PLUS, add_char(csa);
alpar@9 355 else if (csa->c == '-')
alpar@9 356 csa->token = T_MINUS, add_char(csa);
alpar@9 357 else if (csa->c == ':')
alpar@9 358 csa->token = T_COLON, add_char(csa);
alpar@9 359 else if (csa->c == '<')
alpar@9 360 { csa->token = T_LE, add_char(csa);
alpar@9 361 if (csa->c == '=') add_char(csa);
alpar@9 362 }
alpar@9 363 else if (csa->c == '>')
alpar@9 364 { csa->token = T_GE, add_char(csa);
alpar@9 365 if (csa->c == '=') add_char(csa);
alpar@9 366 }
alpar@9 367 else if (csa->c == '=')
alpar@9 368 { csa->token = T_EQ, add_char(csa);
alpar@9 369 if (csa->c == '<')
alpar@9 370 csa->token = T_LE, add_char(csa);
alpar@9 371 else if (csa->c == '>')
alpar@9 372 csa->token = T_GE, add_char(csa);
alpar@9 373 }
alpar@9 374 else
alpar@9 375 error(csa, "character `%c' not recognized\n", csa->c);
alpar@9 376 /* skip non-significant characters */
alpar@9 377 while (csa->c == ' ') read_char(csa);
alpar@9 378 return;
alpar@9 379 }
alpar@9 380
alpar@9 381 static int find_col(struct csa *csa, char *name)
alpar@9 382 { /* find column by its symbolic name */
alpar@9 383 int j;
alpar@9 384 j = glp_find_col(csa->P, name);
alpar@9 385 if (j == 0)
alpar@9 386 { /* not found; create new column */
alpar@9 387 j = glp_add_cols(csa->P, 1);
alpar@9 388 glp_set_col_name(csa->P, j, name);
alpar@9 389 /* enlarge working arrays, if necessary */
alpar@9 390 if (csa->n_max < j)
alpar@9 391 { int n_max = csa->n_max;
alpar@9 392 int *ind = csa->ind;
alpar@9 393 double *val = csa->val;
alpar@9 394 char *flag = csa->flag;
alpar@9 395 double *lb = csa->lb;
alpar@9 396 double *ub = csa->ub;
alpar@9 397 csa->n_max += csa->n_max;
alpar@9 398 csa->ind = xcalloc(1+csa->n_max, sizeof(int));
alpar@9 399 memcpy(&csa->ind[1], &ind[1], n_max * sizeof(int));
alpar@9 400 xfree(ind);
alpar@9 401 csa->val = xcalloc(1+csa->n_max, sizeof(double));
alpar@9 402 memcpy(&csa->val[1], &val[1], n_max * sizeof(double));
alpar@9 403 xfree(val);
alpar@9 404 csa->flag = xcalloc(1+csa->n_max, sizeof(char));
alpar@9 405 memset(&csa->flag[1], 0, csa->n_max * sizeof(char));
alpar@9 406 memcpy(&csa->flag[1], &flag[1], n_max * sizeof(char));
alpar@9 407 xfree(flag);
alpar@9 408 csa->lb = xcalloc(1+csa->n_max, sizeof(double));
alpar@9 409 memcpy(&csa->lb[1], &lb[1], n_max * sizeof(double));
alpar@9 410 xfree(lb);
alpar@9 411 csa->ub = xcalloc(1+csa->n_max, sizeof(double));
alpar@9 412 memcpy(&csa->ub[1], &ub[1], n_max * sizeof(double));
alpar@9 413 xfree(ub);
alpar@9 414 }
alpar@9 415 csa->lb[j] = +DBL_MAX, csa->ub[j] = -DBL_MAX;
alpar@9 416 }
alpar@9 417 return j;
alpar@9 418 }
alpar@9 419
alpar@9 420 /***********************************************************************
alpar@9 421 * parse_linear_form - parse linear form
alpar@9 422 *
alpar@9 423 * This routine parses the linear form using the following syntax:
alpar@9 424 *
alpar@9 425 * <variable> ::= <symbolic name>
alpar@9 426 * <coefficient> ::= <numeric constant>
alpar@9 427 * <term> ::= <variable> | <numeric constant> <variable>
alpar@9 428 * <linear form> ::= <term> | + <term> | - <term> |
alpar@9 429 * <linear form> + <term> | <linear form> - <term>
alpar@9 430 *
alpar@9 431 * The routine returns the number of terms in the linear form. */
alpar@9 432
alpar@9 433 static int parse_linear_form(struct csa *csa)
alpar@9 434 { int j, k, len = 0, newlen;
alpar@9 435 double s, coef;
alpar@9 436 loop: /* parse an optional sign */
alpar@9 437 if (csa->token == T_PLUS)
alpar@9 438 s = +1.0, scan_token(csa);
alpar@9 439 else if (csa->token == T_MINUS)
alpar@9 440 s = -1.0, scan_token(csa);
alpar@9 441 else
alpar@9 442 s = +1.0;
alpar@9 443 /* parse an optional coefficient */
alpar@9 444 if (csa->token == T_NUMBER)
alpar@9 445 coef = csa->value, scan_token(csa);
alpar@9 446 else
alpar@9 447 coef = 1.0;
alpar@9 448 /* parse a variable name */
alpar@9 449 if (csa->token != T_NAME)
alpar@9 450 error(csa, "missing variable name\n");
alpar@9 451 /* find the corresponding column */
alpar@9 452 j = find_col(csa, csa->image);
alpar@9 453 /* check if the variable is already used in the linear form */
alpar@9 454 if (csa->flag[j])
alpar@9 455 error(csa, "multiple use of variable `%s' not allowed\n",
alpar@9 456 csa->image);
alpar@9 457 /* add new term to the linear form */
alpar@9 458 len++, csa->ind[len] = j, csa->val[len] = s * coef;
alpar@9 459 /* and mark that the variable is used in the linear form */
alpar@9 460 csa->flag[j] = 1;
alpar@9 461 scan_token(csa);
alpar@9 462 /* if the next token is a sign, there is another term */
alpar@9 463 if (csa->token == T_PLUS || csa->token == T_MINUS) goto loop;
alpar@9 464 /* clear marks of the variables used in the linear form */
alpar@9 465 for (k = 1; k <= len; k++) csa->flag[csa->ind[k]] = 0;
alpar@9 466 /* remove zero coefficients */
alpar@9 467 newlen = 0;
alpar@9 468 for (k = 1; k <= len; k++)
alpar@9 469 { if (csa->val[k] != 0.0)
alpar@9 470 { newlen++;
alpar@9 471 csa->ind[newlen] = csa->ind[k];
alpar@9 472 csa->val[newlen] = csa->val[k];
alpar@9 473 }
alpar@9 474 }
alpar@9 475 return newlen;
alpar@9 476 }
alpar@9 477
alpar@9 478 /***********************************************************************
alpar@9 479 * parse_objective - parse objective function
alpar@9 480 *
alpar@9 481 * This routine parses definition of the objective function using the
alpar@9 482 * following syntax:
alpar@9 483 *
alpar@9 484 * <obj sense> ::= minimize | minimum | min | maximize | maximum | max
alpar@9 485 * <obj name> ::= <empty> | <symbolic name> :
alpar@9 486 * <obj function> ::= <obj sense> <obj name> <linear form> */
alpar@9 487
alpar@9 488 static void parse_objective(struct csa *csa)
alpar@9 489 { /* parse objective sense */
alpar@9 490 int k, len;
alpar@9 491 /* parse the keyword 'minimize' or 'maximize' */
alpar@9 492 if (csa->token == T_MINIMIZE)
alpar@9 493 glp_set_obj_dir(csa->P, GLP_MIN);
alpar@9 494 else if (csa->token == T_MAXIMIZE)
alpar@9 495 glp_set_obj_dir(csa->P, GLP_MAX);
alpar@9 496 else
alpar@9 497 xassert(csa != csa);
alpar@9 498 scan_token(csa);
alpar@9 499 /* parse objective name */
alpar@9 500 if (csa->token == T_NAME && csa->c == ':')
alpar@9 501 { /* objective name is followed by a colon */
alpar@9 502 glp_set_obj_name(csa->P, csa->image);
alpar@9 503 scan_token(csa);
alpar@9 504 xassert(csa->token == T_COLON);
alpar@9 505 scan_token(csa);
alpar@9 506 }
alpar@9 507 else
alpar@9 508 { /* objective name is not specified; use default */
alpar@9 509 glp_set_obj_name(csa->P, "obj");
alpar@9 510 }
alpar@9 511 /* parse linear form */
alpar@9 512 len = parse_linear_form(csa);
alpar@9 513 for (k = 1; k <= len; k++)
alpar@9 514 glp_set_obj_coef(csa->P, csa->ind[k], csa->val[k]);
alpar@9 515 return;
alpar@9 516 }
alpar@9 517
alpar@9 518 /***********************************************************************
alpar@9 519 * parse_constraints - parse constraints section
alpar@9 520 *
alpar@9 521 * This routine parses the constraints section using the following
alpar@9 522 * syntax:
alpar@9 523 *
alpar@9 524 * <row name> ::= <empty> | <symbolic name> :
alpar@9 525 * <row sense> ::= < | <= | =< | > | >= | => | =
alpar@9 526 * <right-hand side> ::= <numeric constant> | + <numeric constant> |
alpar@9 527 * - <numeric constant>
alpar@9 528 * <constraint> ::= <row name> <linear form> <row sense>
alpar@9 529 * <right-hand side>
alpar@9 530 * <subject to> ::= subject to | such that | st | s.t. | st.
alpar@9 531 * <constraints section> ::= <subject to> <constraint> |
alpar@9 532 * <constraints section> <constraint> */
alpar@9 533
alpar@9 534 static void parse_constraints(struct csa *csa)
alpar@9 535 { int i, len, type;
alpar@9 536 double s;
alpar@9 537 /* parse the keyword 'subject to' */
alpar@9 538 xassert(csa->token == T_SUBJECT_TO);
alpar@9 539 scan_token(csa);
alpar@9 540 loop: /* create new row (constraint) */
alpar@9 541 i = glp_add_rows(csa->P, 1);
alpar@9 542 /* parse row name */
alpar@9 543 if (csa->token == T_NAME && csa->c == ':')
alpar@9 544 { /* row name is followed by a colon */
alpar@9 545 if (glp_find_row(csa->P, csa->image) != 0)
alpar@9 546 error(csa, "constraint `%s' multiply defined\n",
alpar@9 547 csa->image);
alpar@9 548 glp_set_row_name(csa->P, i, csa->image);
alpar@9 549 scan_token(csa);
alpar@9 550 xassert(csa->token == T_COLON);
alpar@9 551 scan_token(csa);
alpar@9 552 }
alpar@9 553 else
alpar@9 554 { /* row name is not specified; use default */
alpar@9 555 char name[50];
alpar@9 556 sprintf(name, "r.%d", csa->count);
alpar@9 557 glp_set_row_name(csa->P, i, name);
alpar@9 558 }
alpar@9 559 /* parse linear form */
alpar@9 560 len = parse_linear_form(csa);
alpar@9 561 glp_set_mat_row(csa->P, i, len, csa->ind, csa->val);
alpar@9 562 /* parse constraint sense */
alpar@9 563 if (csa->token == T_LE)
alpar@9 564 type = GLP_UP, scan_token(csa);
alpar@9 565 else if (csa->token == T_GE)
alpar@9 566 type = GLP_LO, scan_token(csa);
alpar@9 567 else if (csa->token == T_EQ)
alpar@9 568 type = GLP_FX, scan_token(csa);
alpar@9 569 else
alpar@9 570 error(csa, "missing constraint sense\n");
alpar@9 571 /* parse right-hand side */
alpar@9 572 if (csa->token == T_PLUS)
alpar@9 573 s = +1.0, scan_token(csa);
alpar@9 574 else if (csa->token == T_MINUS)
alpar@9 575 s = -1.0, scan_token(csa);
alpar@9 576 else
alpar@9 577 s = +1.0;
alpar@9 578 if (csa->token != T_NUMBER)
alpar@9 579 error(csa, "missing right-hand side\n");
alpar@9 580 glp_set_row_bnds(csa->P, i, type, s * csa->value, s * csa->value);
alpar@9 581 /* the rest of the current line must be empty */
alpar@9 582 if (!(csa->c == '\n' || csa->c == XEOF))
alpar@9 583 error(csa, "invalid symbol(s) beyond right-hand side\n");
alpar@9 584 scan_token(csa);
alpar@9 585 /* if the next token is a sign, numeric constant, or a symbolic
alpar@9 586 name, here is another constraint */
alpar@9 587 if (csa->token == T_PLUS || csa->token == T_MINUS ||
alpar@9 588 csa->token == T_NUMBER || csa->token == T_NAME) goto loop;
alpar@9 589 return;
alpar@9 590 }
alpar@9 591
alpar@9 592 static void set_lower_bound(struct csa *csa, int j, double lb)
alpar@9 593 { /* set lower bound of j-th variable */
alpar@9 594 if (csa->lb[j] != +DBL_MAX)
alpar@9 595 { warning(csa, "lower bound of variable `%s' redefined\n",
alpar@9 596 glp_get_col_name(csa->P, j));
alpar@9 597 }
alpar@9 598 csa->lb[j] = lb;
alpar@9 599 return;
alpar@9 600 }
alpar@9 601
alpar@9 602 static void set_upper_bound(struct csa *csa, int j, double ub)
alpar@9 603 { /* set upper bound of j-th variable */
alpar@9 604 if (csa->ub[j] != -DBL_MAX)
alpar@9 605 { warning(csa, "upper bound of variable `%s' redefined\n",
alpar@9 606 glp_get_col_name(csa->P, j));
alpar@9 607 }
alpar@9 608 csa->ub[j] = ub;
alpar@9 609 return;
alpar@9 610 }
alpar@9 611
alpar@9 612 /***********************************************************************
alpar@9 613 * parse_bounds - parse bounds section
alpar@9 614 *
alpar@9 615 * This routine parses the bounds section using the following syntax:
alpar@9 616 *
alpar@9 617 * <variable> ::= <symbolic name>
alpar@9 618 * <infinity> ::= infinity | inf
alpar@9 619 * <bound> ::= <numeric constant> | + <numeric constant> |
alpar@9 620 * - <numeric constant> | + <infinity> | - <infinity>
alpar@9 621 * <lt> ::= < | <= | =<
alpar@9 622 * <gt> ::= > | >= | =>
alpar@9 623 * <bound definition> ::= <bound> <lt> <variable> <lt> <bound> |
alpar@9 624 * <bound> <lt> <variable> | <variable> <lt> <bound> |
alpar@9 625 * <variable> <gt> <bound> | <variable> = <bound> | <variable> free
alpar@9 626 * <bounds> ::= bounds | bound
alpar@9 627 * <bounds section> ::= <bounds> |
alpar@9 628 * <bounds section> <bound definition> */
alpar@9 629
alpar@9 630 static void parse_bounds(struct csa *csa)
alpar@9 631 { int j, lb_flag;
alpar@9 632 double lb, s;
alpar@9 633 /* parse the keyword 'bounds' */
alpar@9 634 xassert(csa->token == T_BOUNDS);
alpar@9 635 scan_token(csa);
alpar@9 636 loop: /* bound definition can start with a sign, numeric constant, or
alpar@9 637 a symbolic name */
alpar@9 638 if (!(csa->token == T_PLUS || csa->token == T_MINUS ||
alpar@9 639 csa->token == T_NUMBER || csa->token == T_NAME)) goto done;
alpar@9 640 /* parse bound definition */
alpar@9 641 if (csa->token == T_PLUS || csa->token == T_MINUS)
alpar@9 642 { /* parse signed lower bound */
alpar@9 643 lb_flag = 1;
alpar@9 644 s = (csa->token == T_PLUS ? +1.0 : -1.0);
alpar@9 645 scan_token(csa);
alpar@9 646 if (csa->token == T_NUMBER)
alpar@9 647 lb = s * csa->value, scan_token(csa);
alpar@9 648 else if (the_same(csa->image, "infinity") ||
alpar@9 649 the_same(csa->image, "inf"))
alpar@9 650 { if (s > 0.0)
alpar@9 651 error(csa, "invalid use of `+inf' as lower bound\n");
alpar@9 652 lb = -DBL_MAX, scan_token(csa);
alpar@9 653 }
alpar@9 654 else
alpar@9 655 error(csa, "missing lower bound\n");
alpar@9 656 }
alpar@9 657 else if (csa->token == T_NUMBER)
alpar@9 658 { /* parse unsigned lower bound */
alpar@9 659 lb_flag = 1;
alpar@9 660 lb = csa->value, scan_token(csa);
alpar@9 661 }
alpar@9 662 else
alpar@9 663 { /* lower bound is not specified */
alpar@9 664 lb_flag = 0;
alpar@9 665 }
alpar@9 666 /* parse the token that should follow the lower bound */
alpar@9 667 if (lb_flag)
alpar@9 668 { if (csa->token != T_LE)
alpar@9 669 error(csa, "missing `<', `<=', or `=<' after lower bound\n")
alpar@9 670 ;
alpar@9 671 scan_token(csa);
alpar@9 672 }
alpar@9 673 /* parse variable name */
alpar@9 674 if (csa->token != T_NAME)
alpar@9 675 error(csa, "missing variable name\n");
alpar@9 676 j = find_col(csa, csa->image);
alpar@9 677 /* set lower bound */
alpar@9 678 if (lb_flag) set_lower_bound(csa, j, lb);
alpar@9 679 scan_token(csa);
alpar@9 680 /* parse the context that follows the variable name */
alpar@9 681 if (csa->token == T_LE)
alpar@9 682 { /* parse upper bound */
alpar@9 683 scan_token(csa);
alpar@9 684 if (csa->token == T_PLUS || csa->token == T_MINUS)
alpar@9 685 { /* parse signed upper bound */
alpar@9 686 s = (csa->token == T_PLUS ? +1.0 : -1.0);
alpar@9 687 scan_token(csa);
alpar@9 688 if (csa->token == T_NUMBER)
alpar@9 689 { set_upper_bound(csa, j, s * csa->value);
alpar@9 690 scan_token(csa);
alpar@9 691 }
alpar@9 692 else if (the_same(csa->image, "infinity") ||
alpar@9 693 the_same(csa->image, "inf"))
alpar@9 694 { if (s < 0.0)
alpar@9 695 error(csa, "invalid use of `-inf' as upper bound\n");
alpar@9 696 set_upper_bound(csa, j, +DBL_MAX);
alpar@9 697 scan_token(csa);
alpar@9 698 }
alpar@9 699 else
alpar@9 700 error(csa, "missing upper bound\n");
alpar@9 701 }
alpar@9 702 else if (csa->token == T_NUMBER)
alpar@9 703 { /* parse unsigned upper bound */
alpar@9 704 set_upper_bound(csa, j, csa->value);
alpar@9 705 scan_token(csa);
alpar@9 706 }
alpar@9 707 else
alpar@9 708 error(csa, "missing upper bound\n");
alpar@9 709 }
alpar@9 710 else if (csa->token == T_GE)
alpar@9 711 { /* parse lower bound */
alpar@9 712 if (lb_flag)
alpar@9 713 { /* the context '... <= x >= ...' is invalid */
alpar@9 714 error(csa, "invalid bound definition\n");
alpar@9 715 }
alpar@9 716 scan_token(csa);
alpar@9 717 if (csa->token == T_PLUS || csa->token == T_MINUS)
alpar@9 718 { /* parse signed lower bound */
alpar@9 719 s = (csa->token == T_PLUS ? +1.0 : -1.0);
alpar@9 720 scan_token(csa);
alpar@9 721 if (csa->token == T_NUMBER)
alpar@9 722 { set_lower_bound(csa, j, s * csa->value);
alpar@9 723 scan_token(csa);
alpar@9 724 }
alpar@9 725 else if (the_same(csa->image, "infinity") ||
alpar@9 726 the_same(csa->image, "inf") == 0)
alpar@9 727 { if (s > 0.0)
alpar@9 728 error(csa, "invalid use of `+inf' as lower bound\n");
alpar@9 729 set_lower_bound(csa, j, -DBL_MAX);
alpar@9 730 scan_token(csa);
alpar@9 731 }
alpar@9 732 else
alpar@9 733 error(csa, "missing lower bound\n");
alpar@9 734 }
alpar@9 735 else if (csa->token == T_NUMBER)
alpar@9 736 { /* parse unsigned lower bound */
alpar@9 737 set_lower_bound(csa, j, csa->value);
alpar@9 738 scan_token(csa);
alpar@9 739 }
alpar@9 740 else
alpar@9 741 error(csa, "missing lower bound\n");
alpar@9 742 }
alpar@9 743 else if (csa->token == T_EQ)
alpar@9 744 { /* parse fixed value */
alpar@9 745 if (lb_flag)
alpar@9 746 { /* the context '... <= x = ...' is invalid */
alpar@9 747 error(csa, "invalid bound definition\n");
alpar@9 748 }
alpar@9 749 scan_token(csa);
alpar@9 750 if (csa->token == T_PLUS || csa->token == T_MINUS)
alpar@9 751 { /* parse signed fixed value */
alpar@9 752 s = (csa->token == T_PLUS ? +1.0 : -1.0);
alpar@9 753 scan_token(csa);
alpar@9 754 if (csa->token == T_NUMBER)
alpar@9 755 { set_lower_bound(csa, j, s * csa->value);
alpar@9 756 set_upper_bound(csa, j, s * csa->value);
alpar@9 757 scan_token(csa);
alpar@9 758 }
alpar@9 759 else
alpar@9 760 error(csa, "missing fixed value\n");
alpar@9 761 }
alpar@9 762 else if (csa->token == T_NUMBER)
alpar@9 763 { /* parse unsigned fixed value */
alpar@9 764 set_lower_bound(csa, j, csa->value);
alpar@9 765 set_upper_bound(csa, j, csa->value);
alpar@9 766 scan_token(csa);
alpar@9 767 }
alpar@9 768 else
alpar@9 769 error(csa, "missing fixed value\n");
alpar@9 770 }
alpar@9 771 else if (the_same(csa->image, "free"))
alpar@9 772 { /* parse the keyword 'free' */
alpar@9 773 if (lb_flag)
alpar@9 774 { /* the context '... <= x free ...' is invalid */
alpar@9 775 error(csa, "invalid bound definition\n");
alpar@9 776 }
alpar@9 777 set_lower_bound(csa, j, -DBL_MAX);
alpar@9 778 set_upper_bound(csa, j, +DBL_MAX);
alpar@9 779 scan_token(csa);
alpar@9 780 }
alpar@9 781 else if (!lb_flag)
alpar@9 782 { /* neither lower nor upper bounds are specified */
alpar@9 783 error(csa, "invalid bound definition\n");
alpar@9 784 }
alpar@9 785 goto loop;
alpar@9 786 done: return;
alpar@9 787 }
alpar@9 788
alpar@9 789 /***********************************************************************
alpar@9 790 * parse_integer - parse general, integer, or binary section
alpar@9 791 *
alpar@9 792 * <variable> ::= <symbolic name>
alpar@9 793 * <general> ::= general | generals | gen
alpar@9 794 * <integer> ::= integer | integers | int
alpar@9 795 * <binary> ::= binary | binaries | bin
alpar@9 796 * <section head> ::= <general> <integer> <binary>
alpar@9 797 * <additional section> ::= <section head> |
alpar@9 798 * <additional section> <variable> */
alpar@9 799
alpar@9 800 static void parse_integer(struct csa *csa)
alpar@9 801 { int j, binary;
alpar@9 802 /* parse the keyword 'general', 'integer', or 'binary' */
alpar@9 803 if (csa->token == T_GENERAL)
alpar@9 804 binary = 0, scan_token(csa);
alpar@9 805 else if (csa->token == T_INTEGER)
alpar@9 806 binary = 0, scan_token(csa);
alpar@9 807 else if (csa->token == T_BINARY)
alpar@9 808 binary = 1, scan_token(csa);
alpar@9 809 else
alpar@9 810 xassert(csa != csa);
alpar@9 811 /* parse list of variables (may be empty) */
alpar@9 812 while (csa->token == T_NAME)
alpar@9 813 { /* find the corresponding column */
alpar@9 814 j = find_col(csa, csa->image);
alpar@9 815 /* change kind of the variable */
alpar@9 816 glp_set_col_kind(csa->P, j, GLP_IV);
alpar@9 817 /* set 0-1 bounds for the binary variable */
alpar@9 818 if (binary)
alpar@9 819 { set_lower_bound(csa, j, 0.0);
alpar@9 820 set_upper_bound(csa, j, 1.0);
alpar@9 821 }
alpar@9 822 scan_token(csa);
alpar@9 823 }
alpar@9 824 return;
alpar@9 825 }
alpar@9 826
alpar@9 827 int glp_read_lp(glp_prob *P, const glp_cpxcp *parm, const char *fname)
alpar@9 828 { /* read problem data in CPLEX LP format */
alpar@9 829 glp_cpxcp _parm;
alpar@9 830 struct csa _csa, *csa = &_csa;
alpar@9 831 int ret;
alpar@9 832 xprintf("Reading problem data from `%s'...\n", fname);
alpar@9 833 if (parm == NULL)
alpar@9 834 glp_init_cpxcp(&_parm), parm = &_parm;
alpar@9 835 /* check control parameters */
alpar@9 836 check_parm("glp_read_lp", parm);
alpar@9 837 /* initialize common storage area */
alpar@9 838 csa->P = P;
alpar@9 839 csa->parm = parm;
alpar@9 840 csa->fname = fname;
alpar@9 841 csa->fp = NULL;
alpar@9 842 if (setjmp(csa->jump))
alpar@9 843 { ret = 1;
alpar@9 844 goto done;
alpar@9 845 }
alpar@9 846 csa->count = 0;
alpar@9 847 csa->c = '\n';
alpar@9 848 csa->token = T_EOF;
alpar@9 849 csa->image[0] = '\0';
alpar@9 850 csa->imlen = 0;
alpar@9 851 csa->value = 0.0;
alpar@9 852 csa->n_max = 100;
alpar@9 853 csa->ind = xcalloc(1+csa->n_max, sizeof(int));
alpar@9 854 csa->val = xcalloc(1+csa->n_max, sizeof(double));
alpar@9 855 csa->flag = xcalloc(1+csa->n_max, sizeof(char));
alpar@9 856 memset(&csa->flag[1], 0, csa->n_max * sizeof(char));
alpar@9 857 csa->lb = xcalloc(1+csa->n_max, sizeof(double));
alpar@9 858 csa->ub = xcalloc(1+csa->n_max, sizeof(double));
alpar@9 859 /* erase problem object */
alpar@9 860 glp_erase_prob(P);
alpar@9 861 glp_create_index(P);
alpar@9 862 /* open input CPLEX LP file */
alpar@9 863 csa->fp = xfopen(fname, "r");
alpar@9 864 if (csa->fp == NULL)
alpar@9 865 { xprintf("Unable to open `%s' - %s\n", fname, xerrmsg());
alpar@9 866 ret = 1;
alpar@9 867 goto done;
alpar@9 868 }
alpar@9 869 /* scan very first token */
alpar@9 870 scan_token(csa);
alpar@9 871 /* parse definition of the objective function */
alpar@9 872 if (!(csa->token == T_MINIMIZE || csa->token == T_MAXIMIZE))
alpar@9 873 error(csa, "`minimize' or `maximize' keyword missing\n");
alpar@9 874 parse_objective(csa);
alpar@9 875 /* parse constraints section */
alpar@9 876 if (csa->token != T_SUBJECT_TO)
alpar@9 877 error(csa, "constraints section missing\n");
alpar@9 878 parse_constraints(csa);
alpar@9 879 /* parse optional bounds section */
alpar@9 880 if (csa->token == T_BOUNDS) parse_bounds(csa);
alpar@9 881 /* parse optional general, integer, and binary sections */
alpar@9 882 while (csa->token == T_GENERAL ||
alpar@9 883 csa->token == T_INTEGER ||
alpar@9 884 csa->token == T_BINARY) parse_integer(csa);
alpar@9 885 /* check for the keyword 'end' */
alpar@9 886 if (csa->token == T_END)
alpar@9 887 scan_token(csa);
alpar@9 888 else if (csa->token == T_EOF)
alpar@9 889 warning(csa, "keyword `end' missing\n");
alpar@9 890 else
alpar@9 891 error(csa, "symbol `%s' in wrong position\n", csa->image);
alpar@9 892 /* nothing must follow the keyword 'end' (except comments) */
alpar@9 893 if (csa->token != T_EOF)
alpar@9 894 error(csa, "extra symbol(s) detected beyond `end'\n");
alpar@9 895 /* set bounds of variables */
alpar@9 896 { int j, type;
alpar@9 897 double lb, ub;
alpar@9 898 for (j = 1; j <= P->n; j++)
alpar@9 899 { lb = csa->lb[j];
alpar@9 900 ub = csa->ub[j];
alpar@9 901 if (lb == +DBL_MAX) lb = 0.0; /* default lb */
alpar@9 902 if (ub == -DBL_MAX) ub = +DBL_MAX; /* default ub */
alpar@9 903 if (lb == -DBL_MAX && ub == +DBL_MAX)
alpar@9 904 type = GLP_FR;
alpar@9 905 else if (ub == +DBL_MAX)
alpar@9 906 type = GLP_LO;
alpar@9 907 else if (lb == -DBL_MAX)
alpar@9 908 type = GLP_UP;
alpar@9 909 else if (lb != ub)
alpar@9 910 type = GLP_DB;
alpar@9 911 else
alpar@9 912 type = GLP_FX;
alpar@9 913 glp_set_col_bnds(csa->P, j, type, lb, ub);
alpar@9 914 }
alpar@9 915 }
alpar@9 916 /* print some statistics */
alpar@9 917 xprintf("%d row%s, %d column%s, %d non-zero%s\n",
alpar@9 918 P->m, P->m == 1 ? "" : "s", P->n, P->n == 1 ? "" : "s",
alpar@9 919 P->nnz, P->nnz == 1 ? "" : "s");
alpar@9 920 if (glp_get_num_int(P) > 0)
alpar@9 921 { int ni = glp_get_num_int(P);
alpar@9 922 int nb = glp_get_num_bin(P);
alpar@9 923 if (ni == 1)
alpar@9 924 { if (nb == 0)
alpar@9 925 xprintf("One variable is integer\n");
alpar@9 926 else
alpar@9 927 xprintf("One variable is binary\n");
alpar@9 928 }
alpar@9 929 else
alpar@9 930 { xprintf("%d integer variables, ", ni);
alpar@9 931 if (nb == 0)
alpar@9 932 xprintf("none");
alpar@9 933 else if (nb == 1)
alpar@9 934 xprintf("one");
alpar@9 935 else if (nb == ni)
alpar@9 936 xprintf("all");
alpar@9 937 else
alpar@9 938 xprintf("%d", nb);
alpar@9 939 xprintf(" of which %s binary\n", nb == 1 ? "is" : "are");
alpar@9 940 }
alpar@9 941 }
alpar@9 942 xprintf("%d lines were read\n", csa->count);
alpar@9 943 /* problem data has been successfully read */
alpar@9 944 glp_delete_index(P);
alpar@9 945 glp_sort_matrix(P);
alpar@9 946 ret = 0;
alpar@9 947 done: if (csa->fp != NULL) xfclose(csa->fp);
alpar@9 948 xfree(csa->ind);
alpar@9 949 xfree(csa->val);
alpar@9 950 xfree(csa->flag);
alpar@9 951 xfree(csa->lb);
alpar@9 952 xfree(csa->ub);
alpar@9 953 if (ret != 0) glp_erase_prob(P);
alpar@9 954 return ret;
alpar@9 955 }
alpar@9 956
alpar@9 957 /***********************************************************************
alpar@9 958 * NAME
alpar@9 959 *
alpar@9 960 * glp_write_lp - write problem data in CPLEX LP format
alpar@9 961 *
alpar@9 962 * SYNOPSIS
alpar@9 963 *
alpar@9 964 * int glp_write_lp(glp_prob *P, const glp_cpxcp *parm, const char
alpar@9 965 * *fname);
alpar@9 966 *
alpar@9 967 * DESCRIPTION
alpar@9 968 *
alpar@9 969 * The routine glp_write_lp writes problem data in CPLEX LP format to
alpar@9 970 * a text file.
alpar@9 971 *
alpar@9 972 * The parameter parm is a pointer to the structure glp_cpxcp, which
alpar@9 973 * specifies control parameters used by the routine. If parm is NULL,
alpar@9 974 * the routine uses default settings.
alpar@9 975 *
alpar@9 976 * The character string fname specifies a name of the text file to be
alpar@9 977 * written.
alpar@9 978 *
alpar@9 979 * RETURNS
alpar@9 980 *
alpar@9 981 * If the operation was successful, the routine glp_write_lp returns
alpar@9 982 * zero. Otherwise, it prints an error message and returns non-zero. */
alpar@9 983
alpar@9 984 #define csa csa1
alpar@9 985
alpar@9 986 struct csa
alpar@9 987 { /* common storage area */
alpar@9 988 glp_prob *P;
alpar@9 989 /* pointer to problem object */
alpar@9 990 const glp_cpxcp *parm;
alpar@9 991 /* pointer to control parameters */
alpar@9 992 };
alpar@9 993
alpar@9 994 static int check_name(char *name)
alpar@9 995 { /* check if specified name is valid for CPLEX LP format */
alpar@9 996 if (*name == '.') return 1;
alpar@9 997 if (isdigit((unsigned char)*name)) return 1;
alpar@9 998 for (; *name; name++)
alpar@9 999 { if (!isalnum((unsigned char)*name) &&
alpar@9 1000 strchr(CHAR_SET, (unsigned char)*name) == NULL) return 1;
alpar@9 1001 }
alpar@9 1002 return 0; /* name is ok */
alpar@9 1003 }
alpar@9 1004
alpar@9 1005 static void adjust_name(char *name)
alpar@9 1006 { /* attempt to adjust specified name to make it valid for CPLEX LP
alpar@9 1007 format */
alpar@9 1008 for (; *name; name++)
alpar@9 1009 { if (*name == ' ')
alpar@9 1010 *name = '_';
alpar@9 1011 else if (*name == '-')
alpar@9 1012 *name = '~';
alpar@9 1013 else if (*name == '[')
alpar@9 1014 *name = '(';
alpar@9 1015 else if (*name == ']')
alpar@9 1016 *name = ')';
alpar@9 1017 }
alpar@9 1018 return;
alpar@9 1019 }
alpar@9 1020
alpar@9 1021 static char *row_name(struct csa *csa, int i, char rname[255+1])
alpar@9 1022 { /* construct symbolic name of i-th row (constraint) */
alpar@9 1023 const char *name;
alpar@9 1024 if (i == 0)
alpar@9 1025 name = glp_get_obj_name(csa->P);
alpar@9 1026 else
alpar@9 1027 name = glp_get_row_name(csa->P, i);
alpar@9 1028 if (name == NULL) goto fake;
alpar@9 1029 strcpy(rname, name);
alpar@9 1030 adjust_name(rname);
alpar@9 1031 if (check_name(rname)) goto fake;
alpar@9 1032 return rname;
alpar@9 1033 fake: if (i == 0)
alpar@9 1034 strcpy(rname, "obj");
alpar@9 1035 else
alpar@9 1036 sprintf(rname, "r_%d", i);
alpar@9 1037 return rname;
alpar@9 1038 }
alpar@9 1039
alpar@9 1040 static char *col_name(struct csa *csa, int j, char cname[255+1])
alpar@9 1041 { /* construct symbolic name of j-th column (variable) */
alpar@9 1042 const char *name;
alpar@9 1043 name = glp_get_col_name(csa->P, j);
alpar@9 1044 if (name == NULL) goto fake;
alpar@9 1045 strcpy(cname, name);
alpar@9 1046 adjust_name(cname);
alpar@9 1047 if (check_name(cname)) goto fake;
alpar@9 1048 return cname;
alpar@9 1049 fake: sprintf(cname, "x_%d", j);
alpar@9 1050 return cname;
alpar@9 1051 }
alpar@9 1052
alpar@9 1053 int glp_write_lp(glp_prob *P, const glp_cpxcp *parm, const char *fname)
alpar@9 1054 { /* write problem data in CPLEX LP format */
alpar@9 1055 glp_cpxcp _parm;
alpar@9 1056 struct csa _csa, *csa = &_csa;
alpar@9 1057 XFILE *fp;
alpar@9 1058 GLPROW *row;
alpar@9 1059 GLPCOL *col;
alpar@9 1060 GLPAIJ *aij;
alpar@9 1061 int i, j, len, flag, count, ret;
alpar@9 1062 char line[1000+1], term[500+1], name[255+1];
alpar@9 1063 xprintf("Writing problem data to `%s'...\n", fname);
alpar@9 1064 if (parm == NULL)
alpar@9 1065 glp_init_cpxcp(&_parm), parm = &_parm;
alpar@9 1066 /* check control parameters */
alpar@9 1067 check_parm("glp_write_lp", parm);
alpar@9 1068 /* initialize common storage area */
alpar@9 1069 csa->P = P;
alpar@9 1070 csa->parm = parm;
alpar@9 1071 /* create output CPLEX LP file */
alpar@9 1072 fp = xfopen(fname, "w"), count = 0;
alpar@9 1073 if (fp == NULL)
alpar@9 1074 { xprintf("Unable to create `%s' - %s\n", fname, xerrmsg());
alpar@9 1075 ret = 1;
alpar@9 1076 goto done;
alpar@9 1077 }
alpar@9 1078 /* write problem name */
alpar@9 1079 xfprintf(fp, "\\* Problem: %s *\\\n",
alpar@9 1080 P->name == NULL ? "Unknown" : P->name), count++;
alpar@9 1081 xfprintf(fp, "\n"), count++;
alpar@9 1082 /* the problem should contain at least one row and one column */
alpar@9 1083 if (!(P->m > 0 && P->n > 0))
alpar@9 1084 { xprintf("Warning: problem has no rows/columns\n");
alpar@9 1085 xfprintf(fp, "\\* WARNING: PROBLEM HAS NO ROWS/COLUMNS *\\\n"),
alpar@9 1086 count++;
alpar@9 1087 xfprintf(fp, "\n"), count++;
alpar@9 1088 goto skip;
alpar@9 1089 }
alpar@9 1090 /* write the objective function definition */
alpar@9 1091 if (P->dir == GLP_MIN)
alpar@9 1092 xfprintf(fp, "Minimize\n"), count++;
alpar@9 1093 else if (P->dir == GLP_MAX)
alpar@9 1094 xfprintf(fp, "Maximize\n"), count++;
alpar@9 1095 else
alpar@9 1096 xassert(P != P);
alpar@9 1097 row_name(csa, 0, name);
alpar@9 1098 sprintf(line, " %s:", name);
alpar@9 1099 len = 0;
alpar@9 1100 for (j = 1; j <= P->n; j++)
alpar@9 1101 { col = P->col[j];
alpar@9 1102 if (col->coef != 0.0 || col->ptr == NULL)
alpar@9 1103 { len++;
alpar@9 1104 col_name(csa, j, name);
alpar@9 1105 if (col->coef == 0.0)
alpar@9 1106 sprintf(term, " + 0 %s", name); /* empty column */
alpar@9 1107 else if (col->coef == +1.0)
alpar@9 1108 sprintf(term, " + %s", name);
alpar@9 1109 else if (col->coef == -1.0)
alpar@9 1110 sprintf(term, " - %s", name);
alpar@9 1111 else if (col->coef > 0.0)
alpar@9 1112 sprintf(term, " + %.*g %s", DBL_DIG, +col->coef, name);
alpar@9 1113 else
alpar@9 1114 sprintf(term, " - %.*g %s", DBL_DIG, -col->coef, name);
alpar@9 1115 if (strlen(line) + strlen(term) > 72)
alpar@9 1116 xfprintf(fp, "%s\n", line), line[0] = '\0', count++;
alpar@9 1117 strcat(line, term);
alpar@9 1118 }
alpar@9 1119 }
alpar@9 1120 if (len == 0)
alpar@9 1121 { /* empty objective */
alpar@9 1122 sprintf(term, " 0 %s", col_name(csa, 1, name));
alpar@9 1123 strcat(line, term);
alpar@9 1124 }
alpar@9 1125 xfprintf(fp, "%s\n", line), count++;
alpar@9 1126 if (P->c0 != 0.0)
alpar@9 1127 xfprintf(fp, "\\* constant term = %.*g *\\\n", DBL_DIG, P->c0),
alpar@9 1128 count++;
alpar@9 1129 xfprintf(fp, "\n"), count++;
alpar@9 1130 /* write the constraints section */
alpar@9 1131 xfprintf(fp, "Subject To\n"), count++;
alpar@9 1132 for (i = 1; i <= P->m; i++)
alpar@9 1133 { row = P->row[i];
alpar@9 1134 if (row->type == GLP_FR) continue; /* skip free row */
alpar@9 1135 row_name(csa, i, name);
alpar@9 1136 sprintf(line, " %s:", name);
alpar@9 1137 /* linear form */
alpar@9 1138 for (aij = row->ptr; aij != NULL; aij = aij->r_next)
alpar@9 1139 { col_name(csa, aij->col->j, name);
alpar@9 1140 if (aij->val == +1.0)
alpar@9 1141 sprintf(term, " + %s", name);
alpar@9 1142 else if (aij->val == -1.0)
alpar@9 1143 sprintf(term, " - %s", name);
alpar@9 1144 else if (aij->val > 0.0)
alpar@9 1145 sprintf(term, " + %.*g %s", DBL_DIG, +aij->val, name);
alpar@9 1146 else
alpar@9 1147 sprintf(term, " - %.*g %s", DBL_DIG, -aij->val, name);
alpar@9 1148 if (strlen(line) + strlen(term) > 72)
alpar@9 1149 xfprintf(fp, "%s\n", line), line[0] = '\0', count++;
alpar@9 1150 strcat(line, term);
alpar@9 1151 }
alpar@9 1152 if (row->type == GLP_DB)
alpar@9 1153 { /* double-bounded (ranged) constraint */
alpar@9 1154 sprintf(term, " - ~r_%d", i);
alpar@9 1155 if (strlen(line) + strlen(term) > 72)
alpar@9 1156 xfprintf(fp, "%s\n", line), line[0] = '\0', count++;
alpar@9 1157 strcat(line, term);
alpar@9 1158 }
alpar@9 1159 else if (row->ptr == NULL)
alpar@9 1160 { /* empty constraint */
alpar@9 1161 sprintf(term, " 0 %s", col_name(csa, 1, name));
alpar@9 1162 strcat(line, term);
alpar@9 1163 }
alpar@9 1164 /* right hand-side */
alpar@9 1165 if (row->type == GLP_LO)
alpar@9 1166 sprintf(term, " >= %.*g", DBL_DIG, row->lb);
alpar@9 1167 else if (row->type == GLP_UP)
alpar@9 1168 sprintf(term, " <= %.*g", DBL_DIG, row->ub);
alpar@9 1169 else if (row->type == GLP_DB || row->type == GLP_FX)
alpar@9 1170 sprintf(term, " = %.*g", DBL_DIG, row->lb);
alpar@9 1171 else
alpar@9 1172 xassert(row != row);
alpar@9 1173 if (strlen(line) + strlen(term) > 72)
alpar@9 1174 xfprintf(fp, "%s\n", line), line[0] = '\0', count++;
alpar@9 1175 strcat(line, term);
alpar@9 1176 xfprintf(fp, "%s\n", line), count++;
alpar@9 1177 }
alpar@9 1178 xfprintf(fp, "\n"), count++;
alpar@9 1179 /* write the bounds section */
alpar@9 1180 flag = 0;
alpar@9 1181 for (i = 1; i <= P->m; i++)
alpar@9 1182 { row = P->row[i];
alpar@9 1183 if (row->type != GLP_DB) continue;
alpar@9 1184 if (!flag)
alpar@9 1185 xfprintf(fp, "Bounds\n"), flag = 1, count++;
alpar@9 1186 xfprintf(fp, " 0 <= ~r_%d <= %.*g\n",
alpar@9 1187 i, DBL_DIG, row->ub - row->lb), count++;
alpar@9 1188 }
alpar@9 1189 for (j = 1; j <= P->n; j++)
alpar@9 1190 { col = P->col[j];
alpar@9 1191 if (col->type == GLP_LO && col->lb == 0.0) continue;
alpar@9 1192 if (!flag)
alpar@9 1193 xfprintf(fp, "Bounds\n"), flag = 1, count++;
alpar@9 1194 col_name(csa, j, name);
alpar@9 1195 if (col->type == GLP_FR)
alpar@9 1196 xfprintf(fp, " %s free\n", name), count++;
alpar@9 1197 else if (col->type == GLP_LO)
alpar@9 1198 xfprintf(fp, " %s >= %.*g\n",
alpar@9 1199 name, DBL_DIG, col->lb), count++;
alpar@9 1200 else if (col->type == GLP_UP)
alpar@9 1201 xfprintf(fp, " -Inf <= %s <= %.*g\n",
alpar@9 1202 name, DBL_DIG, col->ub), count++;
alpar@9 1203 else if (col->type == GLP_DB)
alpar@9 1204 xfprintf(fp, " %.*g <= %s <= %.*g\n",
alpar@9 1205 DBL_DIG, col->lb, name, DBL_DIG, col->ub), count++;
alpar@9 1206 else if (col->type == GLP_FX)
alpar@9 1207 xfprintf(fp, " %s = %.*g\n",
alpar@9 1208 name, DBL_DIG, col->lb), count++;
alpar@9 1209 else
alpar@9 1210 xassert(col != col);
alpar@9 1211 }
alpar@9 1212 if (flag) xfprintf(fp, "\n"), count++;
alpar@9 1213 /* write the integer section */
alpar@9 1214 flag = 0;
alpar@9 1215 for (j = 1; j <= P->n; j++)
alpar@9 1216 { col = P->col[j];
alpar@9 1217 if (col->kind == GLP_CV) continue;
alpar@9 1218 xassert(col->kind == GLP_IV);
alpar@9 1219 if (!flag)
alpar@9 1220 xfprintf(fp, "Generals\n"), flag = 1, count++;
alpar@9 1221 xfprintf(fp, " %s\n", col_name(csa, j, name)), count++;
alpar@9 1222 }
alpar@9 1223 if (flag) xfprintf(fp, "\n"), count++;
alpar@9 1224 skip: /* write the end keyword */
alpar@9 1225 xfprintf(fp, "End\n"), count++;
alpar@9 1226 xfflush(fp);
alpar@9 1227 if (xferror(fp))
alpar@9 1228 { xprintf("Write error on `%s' - %s\n", fname, xerrmsg());
alpar@9 1229 ret = 1;
alpar@9 1230 goto done;
alpar@9 1231 }
alpar@9 1232 /* problem data has been successfully written */
alpar@9 1233 xprintf("%d lines were written\n", count);
alpar@9 1234 ret = 0;
alpar@9 1235 done: if (fp != NULL) xfclose(fp);
alpar@9 1236 return ret;
alpar@9 1237 }
alpar@9 1238
alpar@9 1239 /* eof */