1 /* glpcpx.c (CPLEX LP format routines) */
3 /***********************************************************************
4 * This code is part of GLPK (GNU Linear Programming Kit).
6 * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
7 * 2009, 2010 Andrew Makhorin, Department for Applied Informatics,
8 * Moscow Aviation Institute, Moscow, Russia. All rights reserved.
9 * E-mail: <mao@gnu.org>.
11 * GLPK is free software: you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * GLPK is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
19 * License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with GLPK. If not, see <http://www.gnu.org/licenses/>.
23 ***********************************************************************/
27 /***********************************************************************
30 * glp_init_cpxcp - initialize CPLEX LP format control parameters
34 * void glp_init_cpxcp(glp_cpxcp *parm):
36 * The routine glp_init_cpxcp initializes control parameters used by
37 * the CPLEX LP input/output routines glp_read_lp and glp_write_lp with
40 * Default values of the control parameters are stored in the glp_cpxcp
41 * structure, which the parameter parm points to. */
43 void glp_init_cpxcp(glp_cpxcp *parm)
44 { xassert(parm != NULL);
48 static void check_parm(const char *func, const glp_cpxcp *parm)
49 { /* check control parameters */
50 xassert(func != NULL);
51 xassert(parm != NULL);
55 /***********************************************************************
58 * glp_read_lp - read problem data in CPLEX LP format
62 * int glp_read_lp(glp_prob *P, const glp_cpxcp *parm, const char
67 * The routine glp_read_lp reads problem data in CPLEX LP format from
70 * The parameter parm is a pointer to the structure glp_cpxcp, which
71 * specifies control parameters used by the routine. If parm is NULL,
72 * the routine uses default settings.
74 * The character string fname specifies a name of the text file to be
77 * Note that before reading data the current content of the problem
78 * object is completely erased with the routine glp_erase_prob.
82 * If the operation was successful, the routine glp_read_lp returns
83 * zero. Otherwise, it prints an error message and returns non-zero. */
86 { /* common storage area */
88 /* LP/MIP problem object */
89 const glp_cpxcp *parm;
90 /* pointer to control parameters */
92 /* name of input CPLEX LP file */
94 /* stream assigned to input CPLEX LP file */
96 /* label for go to in case of error */
100 /* current character or XEOF */
103 #define T_EOF 0x00 /* end of file */
104 #define T_MINIMIZE 0x01 /* keyword 'minimize' */
105 #define T_MAXIMIZE 0x02 /* keyword 'maximize' */
106 #define T_SUBJECT_TO 0x03 /* keyword 'subject to' */
107 #define T_BOUNDS 0x04 /* keyword 'bounds' */
108 #define T_GENERAL 0x05 /* keyword 'general' */
109 #define T_INTEGER 0x06 /* keyword 'integer' */
110 #define T_BINARY 0x07 /* keyword 'binary' */
111 #define T_END 0x08 /* keyword 'end' */
112 #define T_NAME 0x09 /* symbolic name */
113 #define T_NUMBER 0x0A /* numeric constant */
114 #define T_PLUS 0x0B /* delimiter '+' */
115 #define T_MINUS 0x0C /* delimiter '-' */
116 #define T_COLON 0x0D /* delimiter ':' */
117 #define T_LE 0x0E /* delimiter '<=' */
118 #define T_GE 0x0F /* delimiter '>=' */
119 #define T_EQ 0x10 /* delimiter '=' */
121 /* image of current token */
123 /* length of token image */
125 /* value of numeric constant */
127 /* length of the following five arrays (enlarged automatically,
129 int *ind; /* int ind[1+n_max]; */
130 double *val; /* double val[1+n_max]; */
131 char *flag; /* char flag[1+n_max]; */
132 /* working arrays used to construct linear forms */
133 double *lb; /* double lb[1+n_max]; */
134 double *ub; /* double ub[1+n_max]; */
135 /* lower and upper bounds of variables (columns) */
138 #define CHAR_SET "!\"#$%&()/,.;?@_`'{}|~"
139 /* characters, which may appear in symbolic names */
141 static void error(struct csa *csa, const char *fmt, ...)
142 { /* print error message and terminate processing */
144 xprintf("%s:%d: ", csa->fname, csa->count);
148 longjmp(csa->jump, 1);
152 static void warning(struct csa *csa, const char *fmt, ...)
153 { /* print warning message and continue processing */
155 xprintf("%s:%d: warning: ", csa->fname, csa->count);
162 static void read_char(struct csa *csa)
163 { /* read next character from input file */
165 xassert(csa->c != XEOF);
166 if (csa->c == '\n') csa->count++;
169 { if (xferror(csa->fp))
170 error(csa, "read error - %s\n", xerrmsg());
171 else if (csa->c == '\n')
176 { warning(csa, "missing final end of line\n");
185 error(csa, "invalid control character 0x%02X\n", c);
190 static void add_char(struct csa *csa)
191 { /* append current character to current token */
192 if (csa->imlen == sizeof(csa->image)-1)
193 error(csa, "token `%.15s...' too long\n", csa->image);
194 csa->image[csa->imlen++] = (char)csa->c;
195 csa->image[csa->imlen] = '\0';
200 static int the_same(char *s1, char *s2)
201 { /* compare two character strings ignoring case sensitivity */
202 for (; *s1 != '\0'; s1++, s2++)
203 { if (tolower((unsigned char)*s1) != tolower((unsigned char)*s2))
209 static void scan_token(struct csa *csa)
210 { /* scan next token */
213 csa->image[0] = '\0';
217 /* skip non-significant characters */
218 while (csa->c == ' ') read_char(csa);
219 /* recognize and scan current token */
222 else if (csa->c == '\n')
224 /* if the next character is letter, it may begin a keyword */
231 else if (csa->c == '\\')
232 { /* comment; ignore everything until end-of-line */
233 while (csa->c != '\n') read_char(csa);
236 else if (isalpha(csa->c) || csa->c != '.' && strchr(CHAR_SET,
238 name: { /* symbolic name */
240 while (isalnum(csa->c) || strchr(CHAR_SET, csa->c) != NULL)
243 { /* check for keyword */
244 if (the_same(csa->image, "minimize"))
245 csa->token = T_MINIMIZE;
246 else if (the_same(csa->image, "minimum"))
247 csa->token = T_MINIMIZE;
248 else if (the_same(csa->image, "min"))
249 csa->token = T_MINIMIZE;
250 else if (the_same(csa->image, "maximize"))
251 csa->token = T_MAXIMIZE;
252 else if (the_same(csa->image, "maximum"))
253 csa->token = T_MAXIMIZE;
254 else if (the_same(csa->image, "max"))
255 csa->token = T_MAXIMIZE;
256 else if (the_same(csa->image, "subject"))
259 if (tolower(csa->c) == 't')
260 { csa->token = T_SUBJECT_TO;
261 csa->image[csa->imlen++] = ' ';
262 csa->image[csa->imlen] = '\0';
264 if (tolower(csa->c) != 'o')
265 error(csa, "keyword `subject to' incomplete\n");
268 error(csa, "keyword `%s%c...' not recognized\n",
273 else if (the_same(csa->image, "such"))
276 if (tolower(csa->c) == 't')
277 { csa->token = T_SUBJECT_TO;
278 csa->image[csa->imlen++] = ' ';
279 csa->image[csa->imlen] = '\0';
281 if (tolower(csa->c) != 'h')
282 err: error(csa, "keyword `such that' incomplete\n");
284 if (tolower(csa->c) != 'a') goto err;
286 if (tolower(csa->c) != 't') goto err;
289 error(csa, "keyword `%s%c...' not recognized\n",
294 else if (the_same(csa->image, "st"))
295 csa->token = T_SUBJECT_TO;
296 else if (the_same(csa->image, "s.t."))
297 csa->token = T_SUBJECT_TO;
298 else if (the_same(csa->image, "st."))
299 csa->token = T_SUBJECT_TO;
300 else if (the_same(csa->image, "bounds"))
301 csa->token = T_BOUNDS;
302 else if (the_same(csa->image, "bound"))
303 csa->token = T_BOUNDS;
304 else if (the_same(csa->image, "general"))
305 csa->token = T_GENERAL;
306 else if (the_same(csa->image, "generals"))
307 csa->token = T_GENERAL;
308 else if (the_same(csa->image, "gen"))
309 csa->token = T_GENERAL;
310 else if (the_same(csa->image, "integer"))
311 csa->token = T_INTEGER;
312 else if (the_same(csa->image, "integers"))
313 csa->token = T_INTEGER;
314 else if (the_same(csa->image, "int"))
315 csa->token = T_INTEGER;
316 else if (the_same(csa->image, "binary"))
317 csa->token = T_BINARY;
318 else if (the_same(csa->image, "binaries"))
319 csa->token = T_BINARY;
320 else if (the_same(csa->image, "bin"))
321 csa->token = T_BINARY;
322 else if (the_same(csa->image, "end"))
326 else if (isdigit(csa->c) || csa->c == '.')
327 { /* numeric constant */
328 csa->token = T_NUMBER;
329 /* scan integer part */
330 while (isdigit(csa->c)) add_char(csa);
331 /* scan optional fractional part (it is mandatory, if there is
335 if (csa->imlen == 1 && !isdigit(csa->c))
336 error(csa, "invalid use of decimal point\n");
337 while (isdigit(csa->c)) add_char(csa);
339 /* scan optional decimal exponent */
340 if (csa->c == 'e' || csa->c == 'E')
342 if (csa->c == '+' || csa->c == '-') add_char(csa);
343 if (!isdigit(csa->c))
344 error(csa, "numeric constant `%s' incomplete\n",
346 while (isdigit(csa->c)) add_char(csa);
348 /* convert the numeric constant to floating-point */
349 if (str2num(csa->image, &csa->value))
350 error(csa, "numeric constant `%s' out of range\n",
353 else if (csa->c == '+')
354 csa->token = T_PLUS, add_char(csa);
355 else if (csa->c == '-')
356 csa->token = T_MINUS, add_char(csa);
357 else if (csa->c == ':')
358 csa->token = T_COLON, add_char(csa);
359 else if (csa->c == '<')
360 { csa->token = T_LE, add_char(csa);
361 if (csa->c == '=') add_char(csa);
363 else if (csa->c == '>')
364 { csa->token = T_GE, add_char(csa);
365 if (csa->c == '=') add_char(csa);
367 else if (csa->c == '=')
368 { csa->token = T_EQ, add_char(csa);
370 csa->token = T_LE, add_char(csa);
371 else if (csa->c == '>')
372 csa->token = T_GE, add_char(csa);
375 error(csa, "character `%c' not recognized\n", csa->c);
376 /* skip non-significant characters */
377 while (csa->c == ' ') read_char(csa);
381 static int find_col(struct csa *csa, char *name)
382 { /* find column by its symbolic name */
384 j = glp_find_col(csa->P, name);
386 { /* not found; create new column */
387 j = glp_add_cols(csa->P, 1);
388 glp_set_col_name(csa->P, j, name);
389 /* enlarge working arrays, if necessary */
391 { int n_max = csa->n_max;
393 double *val = csa->val;
394 char *flag = csa->flag;
395 double *lb = csa->lb;
396 double *ub = csa->ub;
397 csa->n_max += csa->n_max;
398 csa->ind = xcalloc(1+csa->n_max, sizeof(int));
399 memcpy(&csa->ind[1], &ind[1], n_max * sizeof(int));
401 csa->val = xcalloc(1+csa->n_max, sizeof(double));
402 memcpy(&csa->val[1], &val[1], n_max * sizeof(double));
404 csa->flag = xcalloc(1+csa->n_max, sizeof(char));
405 memset(&csa->flag[1], 0, csa->n_max * sizeof(char));
406 memcpy(&csa->flag[1], &flag[1], n_max * sizeof(char));
408 csa->lb = xcalloc(1+csa->n_max, sizeof(double));
409 memcpy(&csa->lb[1], &lb[1], n_max * sizeof(double));
411 csa->ub = xcalloc(1+csa->n_max, sizeof(double));
412 memcpy(&csa->ub[1], &ub[1], n_max * sizeof(double));
415 csa->lb[j] = +DBL_MAX, csa->ub[j] = -DBL_MAX;
420 /***********************************************************************
421 * parse_linear_form - parse linear form
423 * This routine parses the linear form using the following syntax:
425 * <variable> ::= <symbolic name>
426 * <coefficient> ::= <numeric constant>
427 * <term> ::= <variable> | <numeric constant> <variable>
428 * <linear form> ::= <term> | + <term> | - <term> |
429 * <linear form> + <term> | <linear form> - <term>
431 * The routine returns the number of terms in the linear form. */
433 static int parse_linear_form(struct csa *csa)
434 { int j, k, len = 0, newlen;
436 loop: /* parse an optional sign */
437 if (csa->token == T_PLUS)
438 s = +1.0, scan_token(csa);
439 else if (csa->token == T_MINUS)
440 s = -1.0, scan_token(csa);
443 /* parse an optional coefficient */
444 if (csa->token == T_NUMBER)
445 coef = csa->value, scan_token(csa);
448 /* parse a variable name */
449 if (csa->token != T_NAME)
450 error(csa, "missing variable name\n");
451 /* find the corresponding column */
452 j = find_col(csa, csa->image);
453 /* check if the variable is already used in the linear form */
455 error(csa, "multiple use of variable `%s' not allowed\n",
457 /* add new term to the linear form */
458 len++, csa->ind[len] = j, csa->val[len] = s * coef;
459 /* and mark that the variable is used in the linear form */
462 /* if the next token is a sign, there is another term */
463 if (csa->token == T_PLUS || csa->token == T_MINUS) goto loop;
464 /* clear marks of the variables used in the linear form */
465 for (k = 1; k <= len; k++) csa->flag[csa->ind[k]] = 0;
466 /* remove zero coefficients */
468 for (k = 1; k <= len; k++)
469 { if (csa->val[k] != 0.0)
471 csa->ind[newlen] = csa->ind[k];
472 csa->val[newlen] = csa->val[k];
478 /***********************************************************************
479 * parse_objective - parse objective function
481 * This routine parses definition of the objective function using the
484 * <obj sense> ::= minimize | minimum | min | maximize | maximum | max
485 * <obj name> ::= <empty> | <symbolic name> :
486 * <obj function> ::= <obj sense> <obj name> <linear form> */
488 static void parse_objective(struct csa *csa)
489 { /* parse objective sense */
491 /* parse the keyword 'minimize' or 'maximize' */
492 if (csa->token == T_MINIMIZE)
493 glp_set_obj_dir(csa->P, GLP_MIN);
494 else if (csa->token == T_MAXIMIZE)
495 glp_set_obj_dir(csa->P, GLP_MAX);
499 /* parse objective name */
500 if (csa->token == T_NAME && csa->c == ':')
501 { /* objective name is followed by a colon */
502 glp_set_obj_name(csa->P, csa->image);
504 xassert(csa->token == T_COLON);
508 { /* objective name is not specified; use default */
509 glp_set_obj_name(csa->P, "obj");
511 /* parse linear form */
512 len = parse_linear_form(csa);
513 for (k = 1; k <= len; k++)
514 glp_set_obj_coef(csa->P, csa->ind[k], csa->val[k]);
518 /***********************************************************************
519 * parse_constraints - parse constraints section
521 * This routine parses the constraints section using the following
524 * <row name> ::= <empty> | <symbolic name> :
525 * <row sense> ::= < | <= | =< | > | >= | => | =
526 * <right-hand side> ::= <numeric constant> | + <numeric constant> |
527 * - <numeric constant>
528 * <constraint> ::= <row name> <linear form> <row sense>
530 * <subject to> ::= subject to | such that | st | s.t. | st.
531 * <constraints section> ::= <subject to> <constraint> |
532 * <constraints section> <constraint> */
534 static void parse_constraints(struct csa *csa)
537 /* parse the keyword 'subject to' */
538 xassert(csa->token == T_SUBJECT_TO);
540 loop: /* create new row (constraint) */
541 i = glp_add_rows(csa->P, 1);
543 if (csa->token == T_NAME && csa->c == ':')
544 { /* row name is followed by a colon */
545 if (glp_find_row(csa->P, csa->image) != 0)
546 error(csa, "constraint `%s' multiply defined\n",
548 glp_set_row_name(csa->P, i, csa->image);
550 xassert(csa->token == T_COLON);
554 { /* row name is not specified; use default */
556 sprintf(name, "r.%d", csa->count);
557 glp_set_row_name(csa->P, i, name);
559 /* parse linear form */
560 len = parse_linear_form(csa);
561 glp_set_mat_row(csa->P, i, len, csa->ind, csa->val);
562 /* parse constraint sense */
563 if (csa->token == T_LE)
564 type = GLP_UP, scan_token(csa);
565 else if (csa->token == T_GE)
566 type = GLP_LO, scan_token(csa);
567 else if (csa->token == T_EQ)
568 type = GLP_FX, scan_token(csa);
570 error(csa, "missing constraint sense\n");
571 /* parse right-hand side */
572 if (csa->token == T_PLUS)
573 s = +1.0, scan_token(csa);
574 else if (csa->token == T_MINUS)
575 s = -1.0, scan_token(csa);
578 if (csa->token != T_NUMBER)
579 error(csa, "missing right-hand side\n");
580 glp_set_row_bnds(csa->P, i, type, s * csa->value, s * csa->value);
581 /* the rest of the current line must be empty */
582 if (!(csa->c == '\n' || csa->c == XEOF))
583 error(csa, "invalid symbol(s) beyond right-hand side\n");
585 /* if the next token is a sign, numeric constant, or a symbolic
586 name, here is another constraint */
587 if (csa->token == T_PLUS || csa->token == T_MINUS ||
588 csa->token == T_NUMBER || csa->token == T_NAME) goto loop;
592 static void set_lower_bound(struct csa *csa, int j, double lb)
593 { /* set lower bound of j-th variable */
594 if (csa->lb[j] != +DBL_MAX)
595 { warning(csa, "lower bound of variable `%s' redefined\n",
596 glp_get_col_name(csa->P, j));
602 static void set_upper_bound(struct csa *csa, int j, double ub)
603 { /* set upper bound of j-th variable */
604 if (csa->ub[j] != -DBL_MAX)
605 { warning(csa, "upper bound of variable `%s' redefined\n",
606 glp_get_col_name(csa->P, j));
612 /***********************************************************************
613 * parse_bounds - parse bounds section
615 * This routine parses the bounds section using the following syntax:
617 * <variable> ::= <symbolic name>
618 * <infinity> ::= infinity | inf
619 * <bound> ::= <numeric constant> | + <numeric constant> |
620 * - <numeric constant> | + <infinity> | - <infinity>
621 * <lt> ::= < | <= | =<
622 * <gt> ::= > | >= | =>
623 * <bound definition> ::= <bound> <lt> <variable> <lt> <bound> |
624 * <bound> <lt> <variable> | <variable> <lt> <bound> |
625 * <variable> <gt> <bound> | <variable> = <bound> | <variable> free
626 * <bounds> ::= bounds | bound
627 * <bounds section> ::= <bounds> |
628 * <bounds section> <bound definition> */
630 static void parse_bounds(struct csa *csa)
633 /* parse the keyword 'bounds' */
634 xassert(csa->token == T_BOUNDS);
636 loop: /* bound definition can start with a sign, numeric constant, or
638 if (!(csa->token == T_PLUS || csa->token == T_MINUS ||
639 csa->token == T_NUMBER || csa->token == T_NAME)) goto done;
640 /* parse bound definition */
641 if (csa->token == T_PLUS || csa->token == T_MINUS)
642 { /* parse signed lower bound */
644 s = (csa->token == T_PLUS ? +1.0 : -1.0);
646 if (csa->token == T_NUMBER)
647 lb = s * csa->value, scan_token(csa);
648 else if (the_same(csa->image, "infinity") ||
649 the_same(csa->image, "inf"))
651 error(csa, "invalid use of `+inf' as lower bound\n");
652 lb = -DBL_MAX, scan_token(csa);
655 error(csa, "missing lower bound\n");
657 else if (csa->token == T_NUMBER)
658 { /* parse unsigned lower bound */
660 lb = csa->value, scan_token(csa);
663 { /* lower bound is not specified */
666 /* parse the token that should follow the lower bound */
668 { if (csa->token != T_LE)
669 error(csa, "missing `<', `<=', or `=<' after lower bound\n")
673 /* parse variable name */
674 if (csa->token != T_NAME)
675 error(csa, "missing variable name\n");
676 j = find_col(csa, csa->image);
677 /* set lower bound */
678 if (lb_flag) set_lower_bound(csa, j, lb);
680 /* parse the context that follows the variable name */
681 if (csa->token == T_LE)
682 { /* parse upper bound */
684 if (csa->token == T_PLUS || csa->token == T_MINUS)
685 { /* parse signed upper bound */
686 s = (csa->token == T_PLUS ? +1.0 : -1.0);
688 if (csa->token == T_NUMBER)
689 { set_upper_bound(csa, j, s * csa->value);
692 else if (the_same(csa->image, "infinity") ||
693 the_same(csa->image, "inf"))
695 error(csa, "invalid use of `-inf' as upper bound\n");
696 set_upper_bound(csa, j, +DBL_MAX);
700 error(csa, "missing upper bound\n");
702 else if (csa->token == T_NUMBER)
703 { /* parse unsigned upper bound */
704 set_upper_bound(csa, j, csa->value);
708 error(csa, "missing upper bound\n");
710 else if (csa->token == T_GE)
711 { /* parse lower bound */
713 { /* the context '... <= x >= ...' is invalid */
714 error(csa, "invalid bound definition\n");
717 if (csa->token == T_PLUS || csa->token == T_MINUS)
718 { /* parse signed lower bound */
719 s = (csa->token == T_PLUS ? +1.0 : -1.0);
721 if (csa->token == T_NUMBER)
722 { set_lower_bound(csa, j, s * csa->value);
725 else if (the_same(csa->image, "infinity") ||
726 the_same(csa->image, "inf") == 0)
728 error(csa, "invalid use of `+inf' as lower bound\n");
729 set_lower_bound(csa, j, -DBL_MAX);
733 error(csa, "missing lower bound\n");
735 else if (csa->token == T_NUMBER)
736 { /* parse unsigned lower bound */
737 set_lower_bound(csa, j, csa->value);
741 error(csa, "missing lower bound\n");
743 else if (csa->token == T_EQ)
744 { /* parse fixed value */
746 { /* the context '... <= x = ...' is invalid */
747 error(csa, "invalid bound definition\n");
750 if (csa->token == T_PLUS || csa->token == T_MINUS)
751 { /* parse signed fixed value */
752 s = (csa->token == T_PLUS ? +1.0 : -1.0);
754 if (csa->token == T_NUMBER)
755 { set_lower_bound(csa, j, s * csa->value);
756 set_upper_bound(csa, j, s * csa->value);
760 error(csa, "missing fixed value\n");
762 else if (csa->token == T_NUMBER)
763 { /* parse unsigned fixed value */
764 set_lower_bound(csa, j, csa->value);
765 set_upper_bound(csa, j, csa->value);
769 error(csa, "missing fixed value\n");
771 else if (the_same(csa->image, "free"))
772 { /* parse the keyword 'free' */
774 { /* the context '... <= x free ...' is invalid */
775 error(csa, "invalid bound definition\n");
777 set_lower_bound(csa, j, -DBL_MAX);
778 set_upper_bound(csa, j, +DBL_MAX);
782 { /* neither lower nor upper bounds are specified */
783 error(csa, "invalid bound definition\n");
789 /***********************************************************************
790 * parse_integer - parse general, integer, or binary section
792 * <variable> ::= <symbolic name>
793 * <general> ::= general | generals | gen
794 * <integer> ::= integer | integers | int
795 * <binary> ::= binary | binaries | bin
796 * <section head> ::= <general> <integer> <binary>
797 * <additional section> ::= <section head> |
798 * <additional section> <variable> */
800 static void parse_integer(struct csa *csa)
802 /* parse the keyword 'general', 'integer', or 'binary' */
803 if (csa->token == T_GENERAL)
804 binary = 0, scan_token(csa);
805 else if (csa->token == T_INTEGER)
806 binary = 0, scan_token(csa);
807 else if (csa->token == T_BINARY)
808 binary = 1, scan_token(csa);
811 /* parse list of variables (may be empty) */
812 while (csa->token == T_NAME)
813 { /* find the corresponding column */
814 j = find_col(csa, csa->image);
815 /* change kind of the variable */
816 glp_set_col_kind(csa->P, j, GLP_IV);
817 /* set 0-1 bounds for the binary variable */
819 { set_lower_bound(csa, j, 0.0);
820 set_upper_bound(csa, j, 1.0);
827 int glp_read_lp(glp_prob *P, const glp_cpxcp *parm, const char *fname)
828 { /* read problem data in CPLEX LP format */
830 struct csa _csa, *csa = &_csa;
832 xprintf("Reading problem data from `%s'...\n", fname);
834 glp_init_cpxcp(&_parm), parm = &_parm;
835 /* check control parameters */
836 check_parm("glp_read_lp", parm);
837 /* initialize common storage area */
842 if (setjmp(csa->jump))
849 csa->image[0] = '\0';
853 csa->ind = xcalloc(1+csa->n_max, sizeof(int));
854 csa->val = xcalloc(1+csa->n_max, sizeof(double));
855 csa->flag = xcalloc(1+csa->n_max, sizeof(char));
856 memset(&csa->flag[1], 0, csa->n_max * sizeof(char));
857 csa->lb = xcalloc(1+csa->n_max, sizeof(double));
858 csa->ub = xcalloc(1+csa->n_max, sizeof(double));
859 /* erase problem object */
862 /* open input CPLEX LP file */
863 csa->fp = xfopen(fname, "r");
865 { xprintf("Unable to open `%s' - %s\n", fname, xerrmsg());
869 /* scan very first token */
871 /* parse definition of the objective function */
872 if (!(csa->token == T_MINIMIZE || csa->token == T_MAXIMIZE))
873 error(csa, "`minimize' or `maximize' keyword missing\n");
874 parse_objective(csa);
875 /* parse constraints section */
876 if (csa->token != T_SUBJECT_TO)
877 error(csa, "constraints section missing\n");
878 parse_constraints(csa);
879 /* parse optional bounds section */
880 if (csa->token == T_BOUNDS) parse_bounds(csa);
881 /* parse optional general, integer, and binary sections */
882 while (csa->token == T_GENERAL ||
883 csa->token == T_INTEGER ||
884 csa->token == T_BINARY) parse_integer(csa);
885 /* check for the keyword 'end' */
886 if (csa->token == T_END)
888 else if (csa->token == T_EOF)
889 warning(csa, "keyword `end' missing\n");
891 error(csa, "symbol `%s' in wrong position\n", csa->image);
892 /* nothing must follow the keyword 'end' (except comments) */
893 if (csa->token != T_EOF)
894 error(csa, "extra symbol(s) detected beyond `end'\n");
895 /* set bounds of variables */
898 for (j = 1; j <= P->n; j++)
901 if (lb == +DBL_MAX) lb = 0.0; /* default lb */
902 if (ub == -DBL_MAX) ub = +DBL_MAX; /* default ub */
903 if (lb == -DBL_MAX && ub == +DBL_MAX)
905 else if (ub == +DBL_MAX)
907 else if (lb == -DBL_MAX)
913 glp_set_col_bnds(csa->P, j, type, lb, ub);
916 /* print some statistics */
917 xprintf("%d row%s, %d column%s, %d non-zero%s\n",
918 P->m, P->m == 1 ? "" : "s", P->n, P->n == 1 ? "" : "s",
919 P->nnz, P->nnz == 1 ? "" : "s");
920 if (glp_get_num_int(P) > 0)
921 { int ni = glp_get_num_int(P);
922 int nb = glp_get_num_bin(P);
925 xprintf("One variable is integer\n");
927 xprintf("One variable is binary\n");
930 { xprintf("%d integer variables, ", ni);
939 xprintf(" of which %s binary\n", nb == 1 ? "is" : "are");
942 xprintf("%d lines were read\n", csa->count);
943 /* problem data has been successfully read */
947 done: if (csa->fp != NULL) xfclose(csa->fp);
953 if (ret != 0) glp_erase_prob(P);
957 /***********************************************************************
960 * glp_write_lp - write problem data in CPLEX LP format
964 * int glp_write_lp(glp_prob *P, const glp_cpxcp *parm, const char
969 * The routine glp_write_lp writes problem data in CPLEX LP format to
972 * The parameter parm is a pointer to the structure glp_cpxcp, which
973 * specifies control parameters used by the routine. If parm is NULL,
974 * the routine uses default settings.
976 * The character string fname specifies a name of the text file to be
981 * If the operation was successful, the routine glp_write_lp returns
982 * zero. Otherwise, it prints an error message and returns non-zero. */
987 { /* common storage area */
989 /* pointer to problem object */
990 const glp_cpxcp *parm;
991 /* pointer to control parameters */
994 static int check_name(char *name)
995 { /* check if specified name is valid for CPLEX LP format */
996 if (*name == '.') return 1;
997 if (isdigit((unsigned char)*name)) return 1;
998 for (; *name; name++)
999 { if (!isalnum((unsigned char)*name) &&
1000 strchr(CHAR_SET, (unsigned char)*name) == NULL) return 1;
1002 return 0; /* name is ok */
1005 static void adjust_name(char *name)
1006 { /* attempt to adjust specified name to make it valid for CPLEX LP
1008 for (; *name; name++)
1011 else if (*name == '-')
1013 else if (*name == '[')
1015 else if (*name == ']')
1021 static char *row_name(struct csa *csa, int i, char rname[255+1])
1022 { /* construct symbolic name of i-th row (constraint) */
1025 name = glp_get_obj_name(csa->P);
1027 name = glp_get_row_name(csa->P, i);
1028 if (name == NULL) goto fake;
1029 strcpy(rname, name);
1031 if (check_name(rname)) goto fake;
1034 strcpy(rname, "obj");
1036 sprintf(rname, "r_%d", i);
1040 static char *col_name(struct csa *csa, int j, char cname[255+1])
1041 { /* construct symbolic name of j-th column (variable) */
1043 name = glp_get_col_name(csa->P, j);
1044 if (name == NULL) goto fake;
1045 strcpy(cname, name);
1047 if (check_name(cname)) goto fake;
1049 fake: sprintf(cname, "x_%d", j);
1053 int glp_write_lp(glp_prob *P, const glp_cpxcp *parm, const char *fname)
1054 { /* write problem data in CPLEX LP format */
1056 struct csa _csa, *csa = &_csa;
1061 int i, j, len, flag, count, ret;
1062 char line[1000+1], term[500+1], name[255+1];
1063 xprintf("Writing problem data to `%s'...\n", fname);
1065 glp_init_cpxcp(&_parm), parm = &_parm;
1066 /* check control parameters */
1067 check_parm("glp_write_lp", parm);
1068 /* initialize common storage area */
1071 /* create output CPLEX LP file */
1072 fp = xfopen(fname, "w"), count = 0;
1074 { xprintf("Unable to create `%s' - %s\n", fname, xerrmsg());
1078 /* write problem name */
1079 xfprintf(fp, "\\* Problem: %s *\\\n",
1080 P->name == NULL ? "Unknown" : P->name), count++;
1081 xfprintf(fp, "\n"), count++;
1082 /* the problem should contain at least one row and one column */
1083 if (!(P->m > 0 && P->n > 0))
1084 { xprintf("Warning: problem has no rows/columns\n");
1085 xfprintf(fp, "\\* WARNING: PROBLEM HAS NO ROWS/COLUMNS *\\\n"),
1087 xfprintf(fp, "\n"), count++;
1090 /* write the objective function definition */
1091 if (P->dir == GLP_MIN)
1092 xfprintf(fp, "Minimize\n"), count++;
1093 else if (P->dir == GLP_MAX)
1094 xfprintf(fp, "Maximize\n"), count++;
1097 row_name(csa, 0, name);
1098 sprintf(line, " %s:", name);
1100 for (j = 1; j <= P->n; j++)
1102 if (col->coef != 0.0 || col->ptr == NULL)
1104 col_name(csa, j, name);
1105 if (col->coef == 0.0)
1106 sprintf(term, " + 0 %s", name); /* empty column */
1107 else if (col->coef == +1.0)
1108 sprintf(term, " + %s", name);
1109 else if (col->coef == -1.0)
1110 sprintf(term, " - %s", name);
1111 else if (col->coef > 0.0)
1112 sprintf(term, " + %.*g %s", DBL_DIG, +col->coef, name);
1114 sprintf(term, " - %.*g %s", DBL_DIG, -col->coef, name);
1115 if (strlen(line) + strlen(term) > 72)
1116 xfprintf(fp, "%s\n", line), line[0] = '\0', count++;
1121 { /* empty objective */
1122 sprintf(term, " 0 %s", col_name(csa, 1, name));
1125 xfprintf(fp, "%s\n", line), count++;
1127 xfprintf(fp, "\\* constant term = %.*g *\\\n", DBL_DIG, P->c0),
1129 xfprintf(fp, "\n"), count++;
1130 /* write the constraints section */
1131 xfprintf(fp, "Subject To\n"), count++;
1132 for (i = 1; i <= P->m; i++)
1134 if (row->type == GLP_FR) continue; /* skip free row */
1135 row_name(csa, i, name);
1136 sprintf(line, " %s:", name);
1138 for (aij = row->ptr; aij != NULL; aij = aij->r_next)
1139 { col_name(csa, aij->col->j, name);
1140 if (aij->val == +1.0)
1141 sprintf(term, " + %s", name);
1142 else if (aij->val == -1.0)
1143 sprintf(term, " - %s", name);
1144 else if (aij->val > 0.0)
1145 sprintf(term, " + %.*g %s", DBL_DIG, +aij->val, name);
1147 sprintf(term, " - %.*g %s", DBL_DIG, -aij->val, name);
1148 if (strlen(line) + strlen(term) > 72)
1149 xfprintf(fp, "%s\n", line), line[0] = '\0', count++;
1152 if (row->type == GLP_DB)
1153 { /* double-bounded (ranged) constraint */
1154 sprintf(term, " - ~r_%d", i);
1155 if (strlen(line) + strlen(term) > 72)
1156 xfprintf(fp, "%s\n", line), line[0] = '\0', count++;
1159 else if (row->ptr == NULL)
1160 { /* empty constraint */
1161 sprintf(term, " 0 %s", col_name(csa, 1, name));
1164 /* right hand-side */
1165 if (row->type == GLP_LO)
1166 sprintf(term, " >= %.*g", DBL_DIG, row->lb);
1167 else if (row->type == GLP_UP)
1168 sprintf(term, " <= %.*g", DBL_DIG, row->ub);
1169 else if (row->type == GLP_DB || row->type == GLP_FX)
1170 sprintf(term, " = %.*g", DBL_DIG, row->lb);
1172 xassert(row != row);
1173 if (strlen(line) + strlen(term) > 72)
1174 xfprintf(fp, "%s\n", line), line[0] = '\0', count++;
1176 xfprintf(fp, "%s\n", line), count++;
1178 xfprintf(fp, "\n"), count++;
1179 /* write the bounds section */
1181 for (i = 1; i <= P->m; i++)
1183 if (row->type != GLP_DB) continue;
1185 xfprintf(fp, "Bounds\n"), flag = 1, count++;
1186 xfprintf(fp, " 0 <= ~r_%d <= %.*g\n",
1187 i, DBL_DIG, row->ub - row->lb), count++;
1189 for (j = 1; j <= P->n; j++)
1191 if (col->type == GLP_LO && col->lb == 0.0) continue;
1193 xfprintf(fp, "Bounds\n"), flag = 1, count++;
1194 col_name(csa, j, name);
1195 if (col->type == GLP_FR)
1196 xfprintf(fp, " %s free\n", name), count++;
1197 else if (col->type == GLP_LO)
1198 xfprintf(fp, " %s >= %.*g\n",
1199 name, DBL_DIG, col->lb), count++;
1200 else if (col->type == GLP_UP)
1201 xfprintf(fp, " -Inf <= %s <= %.*g\n",
1202 name, DBL_DIG, col->ub), count++;
1203 else if (col->type == GLP_DB)
1204 xfprintf(fp, " %.*g <= %s <= %.*g\n",
1205 DBL_DIG, col->lb, name, DBL_DIG, col->ub), count++;
1206 else if (col->type == GLP_FX)
1207 xfprintf(fp, " %s = %.*g\n",
1208 name, DBL_DIG, col->lb), count++;
1210 xassert(col != col);
1212 if (flag) xfprintf(fp, "\n"), count++;
1213 /* write the integer section */
1215 for (j = 1; j <= P->n; j++)
1217 if (col->kind == GLP_CV) continue;
1218 xassert(col->kind == GLP_IV);
1220 xfprintf(fp, "Generals\n"), flag = 1, count++;
1221 xfprintf(fp, " %s\n", col_name(csa, j, name)), count++;
1223 if (flag) xfprintf(fp, "\n"), count++;
1224 skip: /* write the end keyword */
1225 xfprintf(fp, "End\n"), count++;
1228 { xprintf("Write error on `%s' - %s\n", fname, xerrmsg());
1232 /* problem data has been successfully written */
1233 xprintf("%d lines were written\n", count);
1235 done: if (fp != NULL) xfclose(fp);