alpar@1: /* glpdmx.c (reading/writing data in DIMACS format) */ alpar@1: alpar@1: /*********************************************************************** alpar@1: * This code is part of GLPK (GNU Linear Programming Kit). alpar@1: * alpar@1: * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, alpar@1: * 2009, 2010 Andrew Makhorin, Department for Applied Informatics, alpar@1: * Moscow Aviation Institute, Moscow, Russia. All rights reserved. alpar@1: * E-mail: . alpar@1: * alpar@1: * GLPK is free software: you can redistribute it and/or modify it alpar@1: * under the terms of the GNU General Public License as published by alpar@1: * the Free Software Foundation, either version 3 of the License, or alpar@1: * (at your option) any later version. alpar@1: * alpar@1: * GLPK is distributed in the hope that it will be useful, but WITHOUT alpar@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY alpar@1: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public alpar@1: * License for more details. alpar@1: * alpar@1: * You should have received a copy of the GNU General Public License alpar@1: * along with GLPK. If not, see . alpar@1: ***********************************************************************/ alpar@1: alpar@1: #define _GLPSTD_STDIO alpar@1: #include "glpapi.h" alpar@1: alpar@1: struct csa alpar@1: { /* common storage area */ alpar@1: jmp_buf jump; alpar@1: /* label for go to in case of error */ alpar@1: const char *fname; alpar@1: /* name of input text file */ alpar@1: XFILE *fp; alpar@1: /* stream assigned to input text file */ alpar@1: int count; alpar@1: /* line count */ alpar@1: int c; alpar@1: /* current character */ alpar@1: char field[255+1]; alpar@1: /* data field */ alpar@1: int empty; alpar@1: /* warning 'empty line ignored' was printed */ alpar@1: int nonint; alpar@1: /* warning 'non-integer data detected' was printed */ alpar@1: }; alpar@1: alpar@1: static void error(struct csa *csa, const char *fmt, ...) alpar@1: { /* print error message and terminate processing */ alpar@1: va_list arg; alpar@1: xprintf("%s:%d: error: ", csa->fname, csa->count); alpar@1: va_start(arg, fmt); alpar@1: xvprintf(fmt, arg); alpar@1: va_end(arg); alpar@1: xprintf("\n"); alpar@1: longjmp(csa->jump, 1); alpar@1: /* no return */ alpar@1: } alpar@1: alpar@1: static void warning(struct csa *csa, const char *fmt, ...) alpar@1: { /* print warning message and continue processing */ alpar@1: va_list arg; alpar@1: xprintf("%s:%d: warning: ", csa->fname, csa->count); alpar@1: va_start(arg, fmt); alpar@1: xvprintf(fmt, arg); alpar@1: va_end(arg); alpar@1: xprintf("\n"); alpar@1: return; alpar@1: } alpar@1: alpar@1: static void read_char(struct csa *csa) alpar@1: { /* read character from input text file */ alpar@1: int c; alpar@1: if (csa->c == '\n') csa->count++; alpar@1: c = xfgetc(csa->fp); alpar@1: if (c < 0) alpar@1: { if (xferror(csa->fp)) alpar@1: error(csa, "read error - %s", xerrmsg()); alpar@1: else if (csa->c == '\n') alpar@1: error(csa, "unexpected end of file"); alpar@1: else alpar@1: { warning(csa, "missing final end of line"); alpar@1: c = '\n'; alpar@1: } alpar@1: } alpar@1: else if (c == '\n') alpar@1: ; alpar@1: else if (isspace(c)) alpar@1: c = ' '; alpar@1: else if (iscntrl(c)) alpar@1: error(csa, "invalid control character 0x%02X", c); alpar@1: csa->c = c; alpar@1: return; alpar@1: } alpar@1: alpar@1: static void read_designator(struct csa *csa) alpar@1: { /* read one-character line designator */ alpar@1: xassert(csa->c == '\n'); alpar@1: read_char(csa); alpar@1: for (;;) alpar@1: { /* skip preceding white-space characters */ alpar@1: while (csa->c == ' ') alpar@1: read_char(csa); alpar@1: if (csa->c == '\n') alpar@1: { /* ignore empty line */ alpar@1: if (!csa->empty) alpar@1: { warning(csa, "empty line ignored"); alpar@1: csa->empty = 1; alpar@1: } alpar@1: read_char(csa); alpar@1: } alpar@1: else if (csa->c == 'c') alpar@1: { /* skip comment line */ alpar@1: while (csa->c != '\n') alpar@1: read_char(csa); alpar@1: read_char(csa); alpar@1: } alpar@1: else alpar@1: { /* hmm... looks like a line designator */ alpar@1: csa->field[0] = (char)csa->c, csa->field[1] = '\0'; alpar@1: /* check that it is followed by a white-space character */ alpar@1: read_char(csa); alpar@1: if (!(csa->c == ' ' || csa->c == '\n')) alpar@1: error(csa, "line designator missing or invalid"); alpar@1: break; alpar@1: } alpar@1: } alpar@1: return; alpar@1: } alpar@1: alpar@1: static void read_field(struct csa *csa) alpar@1: { /* read data field */ alpar@1: int len = 0; alpar@1: /* skip preceding white-space characters */ alpar@1: while (csa->c == ' ') alpar@1: read_char(csa); alpar@1: /* scan data field */ alpar@1: if (csa->c == '\n') alpar@1: error(csa, "unexpected end of line"); alpar@1: while (!(csa->c == ' ' || csa->c == '\n')) alpar@1: { if (len == sizeof(csa->field)-1) alpar@1: error(csa, "data field `%.15s...' too long", csa->field); alpar@1: csa->field[len++] = (char)csa->c; alpar@1: read_char(csa); alpar@1: } alpar@1: csa->field[len] = '\0'; alpar@1: return; alpar@1: } alpar@1: alpar@1: static void end_of_line(struct csa *csa) alpar@1: { /* skip white-space characters until end of line */ alpar@1: while (csa->c == ' ') alpar@1: read_char(csa); alpar@1: if (csa->c != '\n') alpar@1: error(csa, "too many data fields specified"); alpar@1: return; alpar@1: } alpar@1: alpar@1: static void check_int(struct csa *csa, double num) alpar@1: { /* print a warning if non-integer data are detected */ alpar@1: if (!csa->nonint && num != floor(num)) alpar@1: { warning(csa, "non-integer data detected"); alpar@1: csa->nonint = 1; alpar@1: } alpar@1: return; alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * glp_read_mincost - read min-cost flow problem data in DIMACS format alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * int glp_read_mincost(glp_graph *G, int v_rhs, int a_low, int a_cap, alpar@1: * int a_cost, const char *fname); alpar@1: * alpar@1: * DESCRIPTION alpar@1: * alpar@1: * The routine glp_read_mincost reads minimum cost flow problem data in alpar@1: * DIMACS format from a text file. alpar@1: * alpar@1: * RETURNS alpar@1: * alpar@1: * If the operation was successful, the routine returns zero. Otherwise alpar@1: * it prints an error message and returns non-zero. */ alpar@1: alpar@1: int glp_read_mincost(glp_graph *G, int v_rhs, int a_low, int a_cap, alpar@1: int a_cost, const char *fname) alpar@1: { struct csa _csa, *csa = &_csa; alpar@1: glp_vertex *v; alpar@1: glp_arc *a; alpar@1: int i, j, k, nv, na, ret = 0; alpar@1: double rhs, low, cap, cost; alpar@1: char *flag = NULL; alpar@1: if (v_rhs >= 0 && v_rhs > G->v_size - (int)sizeof(double)) alpar@1: xerror("glp_read_mincost: v_rhs = %d; invalid offset\n", alpar@1: v_rhs); alpar@1: if (a_low >= 0 && a_low > G->a_size - (int)sizeof(double)) alpar@1: xerror("glp_read_mincost: a_low = %d; invalid offset\n", alpar@1: a_low); alpar@1: if (a_cap >= 0 && a_cap > G->a_size - (int)sizeof(double)) alpar@1: xerror("glp_read_mincost: a_cap = %d; invalid offset\n", alpar@1: a_cap); alpar@1: if (a_cost >= 0 && a_cost > G->a_size - (int)sizeof(double)) alpar@1: xerror("glp_read_mincost: a_cost = %d; invalid offset\n", alpar@1: a_cost); alpar@1: glp_erase_graph(G, G->v_size, G->a_size); alpar@1: if (setjmp(csa->jump)) alpar@1: { ret = 1; alpar@1: goto done; alpar@1: } alpar@1: csa->fname = fname; alpar@1: csa->fp = NULL; alpar@1: csa->count = 0; alpar@1: csa->c = '\n'; alpar@1: csa->field[0] = '\0'; alpar@1: csa->empty = csa->nonint = 0; alpar@1: xprintf("Reading min-cost flow problem data from `%s'...\n", alpar@1: fname); alpar@1: csa->fp = xfopen(fname, "r"); alpar@1: if (csa->fp == NULL) alpar@1: { xprintf("Unable to open `%s' - %s\n", fname, xerrmsg()); alpar@1: longjmp(csa->jump, 1); alpar@1: } alpar@1: /* read problem line */ alpar@1: read_designator(csa); alpar@1: if (strcmp(csa->field, "p") != 0) alpar@1: error(csa, "problem line missing or invalid"); alpar@1: read_field(csa); alpar@1: if (strcmp(csa->field, "min") != 0) alpar@1: error(csa, "wrong problem designator; `min' expected"); alpar@1: read_field(csa); alpar@1: if (!(str2int(csa->field, &nv) == 0 && nv >= 0)) alpar@1: error(csa, "number of nodes missing or invalid"); alpar@1: read_field(csa); alpar@1: if (!(str2int(csa->field, &na) == 0 && na >= 0)) alpar@1: error(csa, "number of arcs missing or invalid"); alpar@1: xprintf("Flow network has %d node%s and %d arc%s\n", alpar@1: nv, nv == 1 ? "" : "s", na, na == 1 ? "" : "s"); alpar@1: if (nv > 0) glp_add_vertices(G, nv); alpar@1: end_of_line(csa); alpar@1: /* read node descriptor lines */ alpar@1: flag = xcalloc(1+nv, sizeof(char)); alpar@1: memset(&flag[1], 0, nv * sizeof(char)); alpar@1: if (v_rhs >= 0) alpar@1: { rhs = 0.0; alpar@1: for (i = 1; i <= nv; i++) alpar@1: { v = G->v[i]; alpar@1: memcpy((char *)v->data + v_rhs, &rhs, sizeof(double)); alpar@1: } alpar@1: } alpar@1: for (;;) alpar@1: { read_designator(csa); alpar@1: if (strcmp(csa->field, "n") != 0) break; alpar@1: read_field(csa); alpar@1: if (str2int(csa->field, &i) != 0) alpar@1: error(csa, "node number missing or invalid"); alpar@1: if (!(1 <= i && i <= nv)) alpar@1: error(csa, "node number %d out of range", i); alpar@1: if (flag[i]) alpar@1: error(csa, "duplicate descriptor of node %d", i); alpar@1: read_field(csa); alpar@1: if (str2num(csa->field, &rhs) != 0) alpar@1: error(csa, "node supply/demand missing or invalid"); alpar@1: check_int(csa, rhs); alpar@1: if (v_rhs >= 0) alpar@1: { v = G->v[i]; alpar@1: memcpy((char *)v->data + v_rhs, &rhs, sizeof(double)); alpar@1: } alpar@1: flag[i] = 1; alpar@1: end_of_line(csa); alpar@1: } alpar@1: xfree(flag), flag = NULL; alpar@1: /* read arc descriptor lines */ alpar@1: for (k = 1; k <= na; k++) alpar@1: { if (k > 1) read_designator(csa); alpar@1: if (strcmp(csa->field, "a") != 0) alpar@1: error(csa, "wrong line designator; `a' expected"); alpar@1: read_field(csa); alpar@1: if (str2int(csa->field, &i) != 0) alpar@1: error(csa, "starting node number missing or invalid"); alpar@1: if (!(1 <= i && i <= nv)) alpar@1: error(csa, "starting node number %d out of range", i); alpar@1: read_field(csa); alpar@1: if (str2int(csa->field, &j) != 0) alpar@1: error(csa, "ending node number missing or invalid"); alpar@1: if (!(1 <= j && j <= nv)) alpar@1: error(csa, "ending node number %d out of range", j); alpar@1: read_field(csa); alpar@1: if (!(str2num(csa->field, &low) == 0 && low >= 0.0)) alpar@1: error(csa, "lower bound of arc flow missing or invalid"); alpar@1: check_int(csa, low); alpar@1: read_field(csa); alpar@1: if (!(str2num(csa->field, &cap) == 0 && cap >= low)) alpar@1: error(csa, "upper bound of arc flow missing or invalid"); alpar@1: check_int(csa, cap); alpar@1: read_field(csa); alpar@1: if (str2num(csa->field, &cost) != 0) alpar@1: error(csa, "per-unit cost of arc flow missing or invalid"); alpar@1: check_int(csa, cost); alpar@1: a = glp_add_arc(G, i, j); alpar@1: if (a_low >= 0) alpar@1: memcpy((char *)a->data + a_low, &low, sizeof(double)); alpar@1: if (a_cap >= 0) alpar@1: memcpy((char *)a->data + a_cap, &cap, sizeof(double)); alpar@1: if (a_cost >= 0) alpar@1: memcpy((char *)a->data + a_cost, &cost, sizeof(double)); alpar@1: end_of_line(csa); alpar@1: } alpar@1: xprintf("%d lines were read\n", csa->count); alpar@1: done: if (ret) glp_erase_graph(G, G->v_size, G->a_size); alpar@1: if (csa->fp != NULL) xfclose(csa->fp); alpar@1: if (flag != NULL) xfree(flag); alpar@1: return ret; alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * glp_write_mincost - write min-cost flow problem data in DIMACS format alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * int glp_write_mincost(glp_graph *G, int v_rhs, int a_low, int a_cap, alpar@1: * int a_cost, const char *fname); alpar@1: * alpar@1: * DESCRIPTION alpar@1: * alpar@1: * The routine glp_write_mincost writes minimum cost flow problem data alpar@1: * in DIMACS format to a text file. alpar@1: * alpar@1: * RETURNS alpar@1: * alpar@1: * If the operation was successful, the routine returns zero. Otherwise alpar@1: * it prints an error message and returns non-zero. */ alpar@1: alpar@1: int glp_write_mincost(glp_graph *G, int v_rhs, int a_low, int a_cap, alpar@1: int a_cost, const char *fname) alpar@1: { XFILE *fp; alpar@1: glp_vertex *v; alpar@1: glp_arc *a; alpar@1: int i, count = 0, ret; alpar@1: double rhs, low, cap, cost; alpar@1: if (v_rhs >= 0 && v_rhs > G->v_size - (int)sizeof(double)) alpar@1: xerror("glp_write_mincost: v_rhs = %d; invalid offset\n", alpar@1: v_rhs); alpar@1: if (a_low >= 0 && a_low > G->a_size - (int)sizeof(double)) alpar@1: xerror("glp_write_mincost: a_low = %d; invalid offset\n", alpar@1: a_low); alpar@1: if (a_cap >= 0 && a_cap > G->a_size - (int)sizeof(double)) alpar@1: xerror("glp_write_mincost: a_cap = %d; invalid offset\n", alpar@1: a_cap); alpar@1: if (a_cost >= 0 && a_cost > G->a_size - (int)sizeof(double)) alpar@1: xerror("glp_write_mincost: a_cost = %d; invalid offset\n", alpar@1: a_cost); alpar@1: xprintf("Writing min-cost flow problem data to `%s'...\n", alpar@1: fname); alpar@1: fp = xfopen(fname, "w"); alpar@1: if (fp == NULL) alpar@1: { xprintf("Unable to create `%s' - %s\n", fname, xerrmsg()); alpar@1: ret = 1; alpar@1: goto done; alpar@1: } alpar@1: xfprintf(fp, "c %s\n", alpar@1: G->name == NULL ? "unknown" : G->name), count++; alpar@1: xfprintf(fp, "p min %d %d\n", G->nv, G->na), count++; alpar@1: if (v_rhs >= 0) alpar@1: { for (i = 1; i <= G->nv; i++) alpar@1: { v = G->v[i]; alpar@1: memcpy(&rhs, (char *)v->data + v_rhs, sizeof(double)); alpar@1: if (rhs != 0.0) alpar@1: xfprintf(fp, "n %d %.*g\n", i, DBL_DIG, rhs), count++; alpar@1: } alpar@1: } alpar@1: for (i = 1; i <= G->nv; i++) alpar@1: { v = G->v[i]; alpar@1: for (a = v->out; a != NULL; a = a->t_next) alpar@1: { if (a_low >= 0) alpar@1: memcpy(&low, (char *)a->data + a_low, sizeof(double)); alpar@1: else alpar@1: low = 0.0; alpar@1: if (a_cap >= 0) alpar@1: memcpy(&cap, (char *)a->data + a_cap, sizeof(double)); alpar@1: else alpar@1: cap = 1.0; alpar@1: if (a_cost >= 0) alpar@1: memcpy(&cost, (char *)a->data + a_cost, sizeof(double)); alpar@1: else alpar@1: cost = 0.0; alpar@1: xfprintf(fp, "a %d %d %.*g %.*g %.*g\n", alpar@1: a->tail->i, a->head->i, DBL_DIG, low, DBL_DIG, cap, alpar@1: DBL_DIG, cost), count++; alpar@1: } alpar@1: } alpar@1: xfprintf(fp, "c eof\n"), count++; alpar@1: xfflush(fp); alpar@1: if (xferror(fp)) alpar@1: { xprintf("Write error on `%s' - %s\n", fname, xerrmsg()); alpar@1: ret = 1; alpar@1: goto done; alpar@1: } alpar@1: xprintf("%d lines were written\n", count); alpar@1: ret = 0; alpar@1: done: if (fp != NULL) xfclose(fp); alpar@1: return ret; alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * glp_read_maxflow - read maximum flow problem data in DIMACS format alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * int glp_read_maxflow(glp_graph *G, int *s, int *t, int a_cap, alpar@1: * const char *fname); alpar@1: * alpar@1: * DESCRIPTION alpar@1: * alpar@1: * The routine glp_read_maxflow reads maximum flow problem data in alpar@1: * DIMACS format from a text file. alpar@1: * alpar@1: * RETURNS alpar@1: * alpar@1: * If the operation was successful, the routine returns zero. Otherwise alpar@1: * it prints an error message and returns non-zero. */ alpar@1: alpar@1: int glp_read_maxflow(glp_graph *G, int *_s, int *_t, int a_cap, alpar@1: const char *fname) alpar@1: { struct csa _csa, *csa = &_csa; alpar@1: glp_arc *a; alpar@1: int i, j, k, s, t, nv, na, ret = 0; alpar@1: double cap; alpar@1: if (a_cap >= 0 && a_cap > G->a_size - (int)sizeof(double)) alpar@1: xerror("glp_read_maxflow: a_cap = %d; invalid offset\n", alpar@1: a_cap); alpar@1: glp_erase_graph(G, G->v_size, G->a_size); alpar@1: if (setjmp(csa->jump)) alpar@1: { ret = 1; alpar@1: goto done; alpar@1: } alpar@1: csa->fname = fname; alpar@1: csa->fp = NULL; alpar@1: csa->count = 0; alpar@1: csa->c = '\n'; alpar@1: csa->field[0] = '\0'; alpar@1: csa->empty = csa->nonint = 0; alpar@1: xprintf("Reading maximum flow problem data from `%s'...\n", alpar@1: fname); alpar@1: csa->fp = xfopen(fname, "r"); alpar@1: if (csa->fp == NULL) alpar@1: { xprintf("Unable to open `%s' - %s\n", fname, xerrmsg()); alpar@1: longjmp(csa->jump, 1); alpar@1: } alpar@1: /* read problem line */ alpar@1: read_designator(csa); alpar@1: if (strcmp(csa->field, "p") != 0) alpar@1: error(csa, "problem line missing or invalid"); alpar@1: read_field(csa); alpar@1: if (strcmp(csa->field, "max") != 0) alpar@1: error(csa, "wrong problem designator; `max' expected"); alpar@1: read_field(csa); alpar@1: if (!(str2int(csa->field, &nv) == 0 && nv >= 2)) alpar@1: error(csa, "number of nodes missing or invalid"); alpar@1: read_field(csa); alpar@1: if (!(str2int(csa->field, &na) == 0 && na >= 0)) alpar@1: error(csa, "number of arcs missing or invalid"); alpar@1: xprintf("Flow network has %d node%s and %d arc%s\n", alpar@1: nv, nv == 1 ? "" : "s", na, na == 1 ? "" : "s"); alpar@1: if (nv > 0) glp_add_vertices(G, nv); alpar@1: end_of_line(csa); alpar@1: /* read node descriptor lines */ alpar@1: s = t = 0; alpar@1: for (;;) alpar@1: { read_designator(csa); alpar@1: if (strcmp(csa->field, "n") != 0) break; alpar@1: read_field(csa); alpar@1: if (str2int(csa->field, &i) != 0) alpar@1: error(csa, "node number missing or invalid"); alpar@1: if (!(1 <= i && i <= nv)) alpar@1: error(csa, "node number %d out of range", i); alpar@1: read_field(csa); alpar@1: if (strcmp(csa->field, "s") == 0) alpar@1: { if (s > 0) alpar@1: error(csa, "only one source node allowed"); alpar@1: s = i; alpar@1: } alpar@1: else if (strcmp(csa->field, "t") == 0) alpar@1: { if (t > 0) alpar@1: error(csa, "only one sink node allowed"); alpar@1: t = i; alpar@1: } alpar@1: else alpar@1: error(csa, "wrong node designator; `s' or `t' expected"); alpar@1: if (s > 0 && s == t) alpar@1: error(csa, "source and sink nodes must be distinct"); alpar@1: end_of_line(csa); alpar@1: } alpar@1: if (s == 0) alpar@1: error(csa, "source node descriptor missing\n"); alpar@1: if (t == 0) alpar@1: error(csa, "sink node descriptor missing\n"); alpar@1: if (_s != NULL) *_s = s; alpar@1: if (_t != NULL) *_t = t; alpar@1: /* read arc descriptor lines */ alpar@1: for (k = 1; k <= na; k++) alpar@1: { if (k > 1) read_designator(csa); alpar@1: if (strcmp(csa->field, "a") != 0) alpar@1: error(csa, "wrong line designator; `a' expected"); alpar@1: read_field(csa); alpar@1: if (str2int(csa->field, &i) != 0) alpar@1: error(csa, "starting node number missing or invalid"); alpar@1: if (!(1 <= i && i <= nv)) alpar@1: error(csa, "starting node number %d out of range", i); alpar@1: read_field(csa); alpar@1: if (str2int(csa->field, &j) != 0) alpar@1: error(csa, "ending node number missing or invalid"); alpar@1: if (!(1 <= j && j <= nv)) alpar@1: error(csa, "ending node number %d out of range", j); alpar@1: read_field(csa); alpar@1: if (!(str2num(csa->field, &cap) == 0 && cap >= 0.0)) alpar@1: error(csa, "arc capacity missing or invalid"); alpar@1: check_int(csa, cap); alpar@1: a = glp_add_arc(G, i, j); alpar@1: if (a_cap >= 0) alpar@1: memcpy((char *)a->data + a_cap, &cap, sizeof(double)); alpar@1: end_of_line(csa); alpar@1: } alpar@1: xprintf("%d lines were read\n", csa->count); alpar@1: done: if (ret) glp_erase_graph(G, G->v_size, G->a_size); alpar@1: if (csa->fp != NULL) xfclose(csa->fp); alpar@1: return ret; alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * glp_write_maxflow - write maximum flow problem data in DIMACS format alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * int glp_write_maxflow(glp_graph *G, int s, int t, int a_cap, alpar@1: * const char *fname); alpar@1: * alpar@1: * DESCRIPTION alpar@1: * alpar@1: * The routine glp_write_maxflow writes maximum flow problem data in alpar@1: * DIMACS format to a text file. alpar@1: * alpar@1: * RETURNS alpar@1: * alpar@1: * If the operation was successful, the routine returns zero. Otherwise alpar@1: * it prints an error message and returns non-zero. */ alpar@1: alpar@1: int glp_write_maxflow(glp_graph *G, int s, int t, int a_cap, alpar@1: const char *fname) alpar@1: { XFILE *fp; alpar@1: glp_vertex *v; alpar@1: glp_arc *a; alpar@1: int i, count = 0, ret; alpar@1: double cap; alpar@1: if (!(1 <= s && s <= G->nv)) alpar@1: xerror("glp_write_maxflow: s = %d; source node number out of r" alpar@1: "ange\n", s); alpar@1: if (!(1 <= t && t <= G->nv)) alpar@1: xerror("glp_write_maxflow: t = %d: sink node number out of ran" alpar@1: "ge\n", t); alpar@1: if (a_cap >= 0 && a_cap > G->a_size - (int)sizeof(double)) alpar@1: xerror("glp_write_mincost: a_cap = %d; invalid offset\n", alpar@1: a_cap); alpar@1: xprintf("Writing maximum flow problem data to `%s'...\n", alpar@1: fname); alpar@1: fp = xfopen(fname, "w"); alpar@1: if (fp == NULL) alpar@1: { xprintf("Unable to create `%s' - %s\n", fname, xerrmsg()); alpar@1: ret = 1; alpar@1: goto done; alpar@1: } alpar@1: xfprintf(fp, "c %s\n", alpar@1: G->name == NULL ? "unknown" : G->name), count++; alpar@1: xfprintf(fp, "p max %d %d\n", G->nv, G->na), count++; alpar@1: xfprintf(fp, "n %d s\n", s), count++; alpar@1: xfprintf(fp, "n %d t\n", t), count++; alpar@1: for (i = 1; i <= G->nv; i++) alpar@1: { v = G->v[i]; alpar@1: for (a = v->out; a != NULL; a = a->t_next) alpar@1: { if (a_cap >= 0) alpar@1: memcpy(&cap, (char *)a->data + a_cap, sizeof(double)); alpar@1: else alpar@1: cap = 1.0; alpar@1: xfprintf(fp, "a %d %d %.*g\n", alpar@1: a->tail->i, a->head->i, DBL_DIG, cap), count++; alpar@1: } alpar@1: } alpar@1: xfprintf(fp, "c eof\n"), count++; alpar@1: xfflush(fp); alpar@1: if (xferror(fp)) alpar@1: { xprintf("Write error on `%s' - %s\n", fname, xerrmsg()); alpar@1: ret = 1; alpar@1: goto done; alpar@1: } alpar@1: xprintf("%d lines were written\n", count); alpar@1: ret = 0; alpar@1: done: if (fp != NULL) xfclose(fp); alpar@1: return ret; alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * glp_read_asnprob - read assignment problem data in DIMACS format alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * int glp_read_asnprob(glp_graph *G, int v_set, int a_cost, alpar@1: * const char *fname); alpar@1: * alpar@1: * DESCRIPTION alpar@1: * alpar@1: * The routine glp_read_asnprob reads assignment problem data in DIMACS alpar@1: * format from a text file. alpar@1: * alpar@1: * RETURNS alpar@1: * alpar@1: * If the operation was successful, the routine returns zero. Otherwise alpar@1: * it prints an error message and returns non-zero. */ alpar@1: alpar@1: int glp_read_asnprob(glp_graph *G, int v_set, int a_cost, const char alpar@1: *fname) alpar@1: { struct csa _csa, *csa = &_csa; alpar@1: glp_vertex *v; alpar@1: glp_arc *a; alpar@1: int nv, na, n1, i, j, k, ret = 0; alpar@1: double cost; alpar@1: char *flag = NULL; alpar@1: if (v_set >= 0 && v_set > G->v_size - (int)sizeof(int)) alpar@1: xerror("glp_read_asnprob: v_set = %d; invalid offset\n", alpar@1: v_set); alpar@1: if (a_cost >= 0 && a_cost > G->a_size - (int)sizeof(double)) alpar@1: xerror("glp_read_asnprob: a_cost = %d; invalid offset\n", alpar@1: a_cost); alpar@1: glp_erase_graph(G, G->v_size, G->a_size); alpar@1: if (setjmp(csa->jump)) alpar@1: { ret = 1; alpar@1: goto done; alpar@1: } alpar@1: csa->fname = fname; alpar@1: csa->fp = NULL; alpar@1: csa->count = 0; alpar@1: csa->c = '\n'; alpar@1: csa->field[0] = '\0'; alpar@1: csa->empty = csa->nonint = 0; alpar@1: xprintf("Reading assignment problem data from `%s'...\n", fname); alpar@1: csa->fp = xfopen(fname, "r"); alpar@1: if (csa->fp == NULL) alpar@1: { xprintf("Unable to open `%s' - %s\n", fname, xerrmsg()); alpar@1: longjmp(csa->jump, 1); alpar@1: } alpar@1: /* read problem line */ alpar@1: read_designator(csa); alpar@1: if (strcmp(csa->field, "p") != 0) alpar@1: error(csa, "problem line missing or invalid"); alpar@1: read_field(csa); alpar@1: if (strcmp(csa->field, "asn") != 0) alpar@1: error(csa, "wrong problem designator; `asn' expected"); alpar@1: read_field(csa); alpar@1: if (!(str2int(csa->field, &nv) == 0 && nv >= 0)) alpar@1: error(csa, "number of nodes missing or invalid"); alpar@1: read_field(csa); alpar@1: if (!(str2int(csa->field, &na) == 0 && na >= 0)) alpar@1: error(csa, "number of arcs missing or invalid"); alpar@1: if (nv > 0) glp_add_vertices(G, nv); alpar@1: end_of_line(csa); alpar@1: /* read node descriptor lines */ alpar@1: flag = xcalloc(1+nv, sizeof(char)); alpar@1: memset(&flag[1], 0, nv * sizeof(char)); alpar@1: n1 = 0; alpar@1: for (;;) alpar@1: { read_designator(csa); alpar@1: if (strcmp(csa->field, "n") != 0) break; alpar@1: read_field(csa); alpar@1: if (str2int(csa->field, &i) != 0) alpar@1: error(csa, "node number missing or invalid"); alpar@1: if (!(1 <= i && i <= nv)) alpar@1: error(csa, "node number %d out of range", i); alpar@1: if (flag[i]) alpar@1: error(csa, "duplicate descriptor of node %d", i); alpar@1: flag[i] = 1, n1++; alpar@1: end_of_line(csa); alpar@1: } alpar@1: xprintf( alpar@1: "Assignment problem has %d + %d = %d node%s and %d arc%s\n", alpar@1: n1, nv - n1, nv, nv == 1 ? "" : "s", na, na == 1 ? "" : "s"); alpar@1: if (v_set >= 0) alpar@1: { for (i = 1; i <= nv; i++) alpar@1: { v = G->v[i]; alpar@1: k = (flag[i] ? 0 : 1); alpar@1: memcpy((char *)v->data + v_set, &k, sizeof(int)); alpar@1: } alpar@1: } alpar@1: /* read arc descriptor lines */ alpar@1: for (k = 1; k <= na; k++) alpar@1: { if (k > 1) read_designator(csa); alpar@1: if (strcmp(csa->field, "a") != 0) alpar@1: error(csa, "wrong line designator; `a' expected"); alpar@1: read_field(csa); alpar@1: if (str2int(csa->field, &i) != 0) alpar@1: error(csa, "starting node number missing or invalid"); alpar@1: if (!(1 <= i && i <= nv)) alpar@1: error(csa, "starting node number %d out of range", i); alpar@1: if (!flag[i]) alpar@1: error(csa, "node %d cannot be a starting node", i); alpar@1: read_field(csa); alpar@1: if (str2int(csa->field, &j) != 0) alpar@1: error(csa, "ending node number missing or invalid"); alpar@1: if (!(1 <= j && j <= nv)) alpar@1: error(csa, "ending node number %d out of range", j); alpar@1: if (flag[j]) alpar@1: error(csa, "node %d cannot be an ending node", j); alpar@1: read_field(csa); alpar@1: if (str2num(csa->field, &cost) != 0) alpar@1: error(csa, "arc cost missing or invalid"); alpar@1: check_int(csa, cost); alpar@1: a = glp_add_arc(G, i, j); alpar@1: if (a_cost >= 0) alpar@1: memcpy((char *)a->data + a_cost, &cost, sizeof(double)); alpar@1: end_of_line(csa); alpar@1: } alpar@1: xprintf("%d lines were read\n", csa->count); alpar@1: done: if (ret) glp_erase_graph(G, G->v_size, G->a_size); alpar@1: if (csa->fp != NULL) xfclose(csa->fp); alpar@1: if (flag != NULL) xfree(flag); alpar@1: return ret; alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * glp_write_asnprob - write assignment problem data in DIMACS format alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * int glp_write_asnprob(glp_graph *G, int v_set, int a_cost, alpar@1: * const char *fname); alpar@1: * alpar@1: * DESCRIPTION alpar@1: * alpar@1: * The routine glp_write_asnprob writes assignment problem data in alpar@1: * DIMACS format to a text file. alpar@1: * alpar@1: * RETURNS alpar@1: * alpar@1: * If the operation was successful, the routine returns zero. Otherwise alpar@1: * it prints an error message and returns non-zero. */ alpar@1: alpar@1: int glp_write_asnprob(glp_graph *G, int v_set, int a_cost, const char alpar@1: *fname) alpar@1: { XFILE *fp; alpar@1: glp_vertex *v; alpar@1: glp_arc *a; alpar@1: int i, k, count = 0, ret; alpar@1: double cost; alpar@1: if (v_set >= 0 && v_set > G->v_size - (int)sizeof(int)) alpar@1: xerror("glp_write_asnprob: v_set = %d; invalid offset\n", alpar@1: v_set); alpar@1: if (a_cost >= 0 && a_cost > G->a_size - (int)sizeof(double)) alpar@1: xerror("glp_write_asnprob: a_cost = %d; invalid offset\n", alpar@1: a_cost); alpar@1: xprintf("Writing assignment problem data to `%s'...\n", fname); alpar@1: fp = xfopen(fname, "w"); alpar@1: if (fp == NULL) alpar@1: { xprintf("Unable to create `%s' - %s\n", fname, xerrmsg()); alpar@1: ret = 1; alpar@1: goto done; alpar@1: } alpar@1: xfprintf(fp, "c %s\n", alpar@1: G->name == NULL ? "unknown" : G->name), count++; alpar@1: xfprintf(fp, "p asn %d %d\n", G->nv, G->na), count++; alpar@1: for (i = 1; i <= G->nv; i++) alpar@1: { v = G->v[i]; alpar@1: if (v_set >= 0) alpar@1: memcpy(&k, (char *)v->data + v_set, sizeof(int)); alpar@1: else alpar@1: k = (v->out != NULL ? 0 : 1); alpar@1: if (k == 0) alpar@1: xfprintf(fp, "n %d\n", i), count++; alpar@1: } alpar@1: for (i = 1; i <= G->nv; i++) alpar@1: { v = G->v[i]; alpar@1: for (a = v->out; a != NULL; a = a->t_next) alpar@1: { if (a_cost >= 0) alpar@1: memcpy(&cost, (char *)a->data + a_cost, sizeof(double)); alpar@1: else alpar@1: cost = 1.0; alpar@1: xfprintf(fp, "a %d %d %.*g\n", alpar@1: a->tail->i, a->head->i, DBL_DIG, cost), count++; alpar@1: } alpar@1: } alpar@1: xfprintf(fp, "c eof\n"), count++; alpar@1: xfflush(fp); alpar@1: if (xferror(fp)) alpar@1: { xprintf("Write error on `%s' - %s\n", fname, xerrmsg()); alpar@1: ret = 1; alpar@1: goto done; alpar@1: } alpar@1: xprintf("%d lines were written\n", count); alpar@1: ret = 0; alpar@1: done: if (fp != NULL) xfclose(fp); alpar@1: return ret; alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * glp_read_ccdata - read graph in DIMACS clique/coloring format alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * int glp_read_ccdata(glp_graph *G, int v_wgt, const char *fname); alpar@1: * alpar@1: * DESCRIPTION alpar@1: * alpar@1: * The routine glp_read_ccdata reads an (undirected) graph in DIMACS alpar@1: * clique/coloring format from a text file. alpar@1: * alpar@1: * RETURNS alpar@1: * alpar@1: * If the operation was successful, the routine returns zero. Otherwise alpar@1: * it prints an error message and returns non-zero. */ alpar@1: alpar@1: int glp_read_ccdata(glp_graph *G, int v_wgt, const char *fname) alpar@1: { struct csa _csa, *csa = &_csa; alpar@1: glp_vertex *v; alpar@1: int i, j, k, nv, ne, ret = 0; alpar@1: double w; alpar@1: char *flag = NULL; alpar@1: if (v_wgt >= 0 && v_wgt > G->v_size - (int)sizeof(double)) alpar@1: xerror("glp_read_ccdata: v_wgt = %d; invalid offset\n", alpar@1: v_wgt); alpar@1: glp_erase_graph(G, G->v_size, G->a_size); alpar@1: if (setjmp(csa->jump)) alpar@1: { ret = 1; alpar@1: goto done; alpar@1: } alpar@1: csa->fname = fname; alpar@1: csa->fp = NULL; alpar@1: csa->count = 0; alpar@1: csa->c = '\n'; alpar@1: csa->field[0] = '\0'; alpar@1: csa->empty = csa->nonint = 0; alpar@1: xprintf("Reading graph from `%s'...\n", fname); alpar@1: csa->fp = xfopen(fname, "r"); alpar@1: if (csa->fp == NULL) alpar@1: { xprintf("Unable to open `%s' - %s\n", fname, xerrmsg()); alpar@1: longjmp(csa->jump, 1); alpar@1: } alpar@1: /* read problem line */ alpar@1: read_designator(csa); alpar@1: if (strcmp(csa->field, "p") != 0) alpar@1: error(csa, "problem line missing or invalid"); alpar@1: read_field(csa); alpar@1: if (strcmp(csa->field, "edge") != 0) alpar@1: error(csa, "wrong problem designator; `edge' expected"); alpar@1: read_field(csa); alpar@1: if (!(str2int(csa->field, &nv) == 0 && nv >= 0)) alpar@1: error(csa, "number of vertices missing or invalid"); alpar@1: read_field(csa); alpar@1: if (!(str2int(csa->field, &ne) == 0 && ne >= 0)) alpar@1: error(csa, "number of edges missing or invalid"); alpar@1: xprintf("Graph has %d vert%s and %d edge%s\n", alpar@1: nv, nv == 1 ? "ex" : "ices", ne, ne == 1 ? "" : "s"); alpar@1: if (nv > 0) glp_add_vertices(G, nv); alpar@1: end_of_line(csa); alpar@1: /* read node descriptor lines */ alpar@1: flag = xcalloc(1+nv, sizeof(char)); alpar@1: memset(&flag[1], 0, nv * sizeof(char)); alpar@1: if (v_wgt >= 0) alpar@1: { w = 1.0; alpar@1: for (i = 1; i <= nv; i++) alpar@1: { v = G->v[i]; alpar@1: memcpy((char *)v->data + v_wgt, &w, sizeof(double)); alpar@1: } alpar@1: } alpar@1: for (;;) alpar@1: { read_designator(csa); alpar@1: if (strcmp(csa->field, "n") != 0) break; alpar@1: read_field(csa); alpar@1: if (str2int(csa->field, &i) != 0) alpar@1: error(csa, "vertex number missing or invalid"); alpar@1: if (!(1 <= i && i <= nv)) alpar@1: error(csa, "vertex number %d out of range", i); alpar@1: if (flag[i]) alpar@1: error(csa, "duplicate descriptor of vertex %d", i); alpar@1: read_field(csa); alpar@1: if (str2num(csa->field, &w) != 0) alpar@1: error(csa, "vertex weight missing or invalid"); alpar@1: check_int(csa, w); alpar@1: if (v_wgt >= 0) alpar@1: { v = G->v[i]; alpar@1: memcpy((char *)v->data + v_wgt, &w, sizeof(double)); alpar@1: } alpar@1: flag[i] = 1; alpar@1: end_of_line(csa); alpar@1: } alpar@1: xfree(flag), flag = NULL; alpar@1: /* read edge descriptor lines */ alpar@1: for (k = 1; k <= ne; k++) alpar@1: { if (k > 1) read_designator(csa); alpar@1: if (strcmp(csa->field, "e") != 0) alpar@1: error(csa, "wrong line designator; `e' expected"); alpar@1: read_field(csa); alpar@1: if (str2int(csa->field, &i) != 0) alpar@1: error(csa, "first vertex number missing or invalid"); alpar@1: if (!(1 <= i && i <= nv)) alpar@1: error(csa, "first vertex number %d out of range", i); alpar@1: read_field(csa); alpar@1: if (str2int(csa->field, &j) != 0) alpar@1: error(csa, "second vertex number missing or invalid"); alpar@1: if (!(1 <= j && j <= nv)) alpar@1: error(csa, "second vertex number %d out of range", j); alpar@1: glp_add_arc(G, i, j); alpar@1: end_of_line(csa); alpar@1: } alpar@1: xprintf("%d lines were read\n", csa->count); alpar@1: done: if (ret) glp_erase_graph(G, G->v_size, G->a_size); alpar@1: if (csa->fp != NULL) xfclose(csa->fp); alpar@1: if (flag != NULL) xfree(flag); alpar@1: return ret; alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * glp_write_ccdata - write graph in DIMACS clique/coloring format alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * int glp_write_ccdata(glp_graph *G, int v_wgt, const char *fname); alpar@1: * alpar@1: * DESCRIPTION alpar@1: * alpar@1: * The routine glp_write_ccdata writes the specified graph in DIMACS alpar@1: * clique/coloring format to a text file. alpar@1: * alpar@1: * RETURNS alpar@1: * alpar@1: * If the operation was successful, the routine returns zero. Otherwise alpar@1: * it prints an error message and returns non-zero. */ alpar@1: alpar@1: int glp_write_ccdata(glp_graph *G, int v_wgt, const char *fname) alpar@1: { XFILE *fp; alpar@1: glp_vertex *v; alpar@1: glp_arc *e; alpar@1: int i, count = 0, ret; alpar@1: double w; alpar@1: if (v_wgt >= 0 && v_wgt > G->v_size - (int)sizeof(double)) alpar@1: xerror("glp_write_ccdata: v_wgt = %d; invalid offset\n", alpar@1: v_wgt); alpar@1: xprintf("Writing graph to `%s'\n", fname); alpar@1: fp = xfopen(fname, "w"); alpar@1: if (fp == NULL) alpar@1: { xprintf("Unable to create `%s' - %s\n", fname, xerrmsg()); alpar@1: ret = 1; alpar@1: goto done; alpar@1: } alpar@1: xfprintf(fp, "c %s\n", alpar@1: G->name == NULL ? "unknown" : G->name), count++; alpar@1: xfprintf(fp, "p edge %d %d\n", G->nv, G->na), count++; alpar@1: if (v_wgt >= 0) alpar@1: { for (i = 1; i <= G->nv; i++) alpar@1: { v = G->v[i]; alpar@1: memcpy(&w, (char *)v->data + v_wgt, sizeof(double)); alpar@1: if (w != 1.0) alpar@1: xfprintf(fp, "n %d %.*g\n", i, DBL_DIG, w), count++; alpar@1: } alpar@1: } alpar@1: for (i = 1; i <= G->nv; i++) alpar@1: { v = G->v[i]; alpar@1: for (e = v->out; e != NULL; e = e->t_next) alpar@1: xfprintf(fp, "e %d %d\n", e->tail->i, e->head->i), count++; alpar@1: } alpar@1: xfprintf(fp, "c eof\n"), count++; alpar@1: xfflush(fp); alpar@1: if (xferror(fp)) alpar@1: { xprintf("Write error on `%s' - %s\n", fname, xerrmsg()); alpar@1: ret = 1; alpar@1: goto done; alpar@1: } alpar@1: xprintf("%d lines were written\n", count); alpar@1: ret = 0; alpar@1: done: if (fp != NULL) xfclose(fp); alpar@1: return ret; alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * glp_read_prob - read problem data in GLPK format alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * int glp_read_prob(glp_prob *P, int flags, const char *fname); alpar@1: * alpar@1: * The routine glp_read_prob reads problem data in GLPK LP/MIP format alpar@1: * from a text file. alpar@1: * alpar@1: * RETURNS alpar@1: * alpar@1: * If the operation was successful, the routine returns zero. Otherwise alpar@1: * it prints an error message and returns non-zero. */ alpar@1: alpar@1: int glp_read_prob(glp_prob *P, int flags, const char *fname) alpar@1: { struct csa _csa, *csa = &_csa; alpar@1: int mip, m, n, nnz, ne, i, j, k, type, kind, ret, *ln = NULL, alpar@1: *ia = NULL, *ja = NULL; alpar@1: double lb, ub, temp, *ar = NULL; alpar@1: char *rf = NULL, *cf = NULL; alpar@1: if (P == NULL || P->magic != GLP_PROB_MAGIC) alpar@1: xerror("glp_read_prob: P = %p; invalid problem object\n", alpar@1: P); alpar@1: if (flags != 0) alpar@1: xerror("glp_read_prob: flags = %d; invalid parameter\n", alpar@1: flags); alpar@1: if (fname == NULL) alpar@1: xerror("glp_read_prob: fname = %d; invalid parameter\n", alpar@1: fname); alpar@1: glp_erase_prob(P); alpar@1: if (setjmp(csa->jump)) alpar@1: { ret = 1; alpar@1: goto done; alpar@1: } alpar@1: csa->fname = fname; alpar@1: csa->fp = NULL; alpar@1: csa->count = 0; alpar@1: csa->c = '\n'; alpar@1: csa->field[0] = '\0'; alpar@1: csa->empty = csa->nonint = 0; alpar@1: xprintf("Reading problem data from `%s'...\n", fname); alpar@1: csa->fp = xfopen(fname, "r"); alpar@1: if (csa->fp == NULL) alpar@1: { xprintf("Unable to open `%s' - %s\n", fname, xerrmsg()); alpar@1: longjmp(csa->jump, 1); alpar@1: } alpar@1: /* read problem line */ alpar@1: read_designator(csa); alpar@1: if (strcmp(csa->field, "p") != 0) alpar@1: error(csa, "problem line missing or invalid"); alpar@1: read_field(csa); alpar@1: if (strcmp(csa->field, "lp") == 0) alpar@1: mip = 0; alpar@1: else if (strcmp(csa->field, "mip") == 0) alpar@1: mip = 1; alpar@1: else alpar@1: error(csa, "wrong problem designator; `lp' or `mip' expected\n" alpar@1: ); alpar@1: read_field(csa); alpar@1: if (strcmp(csa->field, "min") == 0) alpar@1: glp_set_obj_dir(P, GLP_MIN); alpar@1: else if (strcmp(csa->field, "max") == 0) alpar@1: glp_set_obj_dir(P, GLP_MAX); alpar@1: else alpar@1: error(csa, "objective sense missing or invalid"); alpar@1: read_field(csa); alpar@1: if (!(str2int(csa->field, &m) == 0 && m >= 0)) alpar@1: error(csa, "number of rows missing or invalid"); alpar@1: read_field(csa); alpar@1: if (!(str2int(csa->field, &n) == 0 && n >= 0)) alpar@1: error(csa, "number of columns missing or invalid"); alpar@1: read_field(csa); alpar@1: if (!(str2int(csa->field, &nnz) == 0 && nnz >= 0)) alpar@1: error(csa, "number of constraint coefficients missing or inval" alpar@1: "id"); alpar@1: if (m > 0) alpar@1: { glp_add_rows(P, m); alpar@1: for (i = 1; i <= m; i++) alpar@1: glp_set_row_bnds(P, i, GLP_FX, 0.0, 0.0); alpar@1: } alpar@1: if (n > 0) alpar@1: { glp_add_cols(P, n); alpar@1: for (j = 1; j <= n; j++) alpar@1: { if (!mip) alpar@1: glp_set_col_bnds(P, j, GLP_LO, 0.0, 0.0); alpar@1: else alpar@1: glp_set_col_kind(P, j, GLP_BV); alpar@1: } alpar@1: } alpar@1: end_of_line(csa); alpar@1: /* allocate working arrays */ alpar@1: rf = xcalloc(1+m, sizeof(char)); alpar@1: memset(rf, 0, 1+m); alpar@1: cf = xcalloc(1+n, sizeof(char)); alpar@1: memset(cf, 0, 1+n); alpar@1: ln = xcalloc(1+nnz, sizeof(int)); alpar@1: ia = xcalloc(1+nnz, sizeof(int)); alpar@1: ja = xcalloc(1+nnz, sizeof(int)); alpar@1: ar = xcalloc(1+nnz, sizeof(double)); alpar@1: /* read descriptor lines */ alpar@1: ne = 0; alpar@1: for (;;) alpar@1: { read_designator(csa); alpar@1: if (strcmp(csa->field, "i") == 0) alpar@1: { /* row descriptor */ alpar@1: read_field(csa); alpar@1: if (str2int(csa->field, &i) != 0) alpar@1: error(csa, "row number missing or invalid"); alpar@1: if (!(1 <= i && i <= m)) alpar@1: error(csa, "row number out of range"); alpar@1: read_field(csa); alpar@1: if (strcmp(csa->field, "f") == 0) alpar@1: type = GLP_FR; alpar@1: else if (strcmp(csa->field, "l") == 0) alpar@1: type = GLP_LO; alpar@1: else if (strcmp(csa->field, "u") == 0) alpar@1: type = GLP_UP; alpar@1: else if (strcmp(csa->field, "d") == 0) alpar@1: type = GLP_DB; alpar@1: else if (strcmp(csa->field, "s") == 0) alpar@1: type = GLP_FX; alpar@1: else alpar@1: error(csa, "row type missing or invalid"); alpar@1: if (type == GLP_LO || type == GLP_DB || type == GLP_FX) alpar@1: { read_field(csa); alpar@1: if (str2num(csa->field, &lb) != 0) alpar@1: error(csa, "row lower bound/fixed value missing or in" alpar@1: "valid"); alpar@1: } alpar@1: else alpar@1: lb = 0.0; alpar@1: if (type == GLP_UP || type == GLP_DB) alpar@1: { read_field(csa); alpar@1: if (str2num(csa->field, &ub) != 0) alpar@1: error(csa, "row upper bound missing or invalid"); alpar@1: } alpar@1: else alpar@1: ub = 0.0; alpar@1: if (rf[i] & 0x01) alpar@1: error(csa, "duplicate row descriptor"); alpar@1: glp_set_row_bnds(P, i, type, lb, ub), rf[i] |= 0x01; alpar@1: } alpar@1: else if (strcmp(csa->field, "j") == 0) alpar@1: { /* column descriptor */ alpar@1: read_field(csa); alpar@1: if (str2int(csa->field, &j) != 0) alpar@1: error(csa, "column number missing or invalid"); alpar@1: if (!(1 <= j && j <= n)) alpar@1: error(csa, "column number out of range"); alpar@1: if (!mip) alpar@1: kind = GLP_CV; alpar@1: else alpar@1: { read_field(csa); alpar@1: if (strcmp(csa->field, "c") == 0) alpar@1: kind = GLP_CV; alpar@1: else if (strcmp(csa->field, "i") == 0) alpar@1: kind = GLP_IV; alpar@1: else if (strcmp(csa->field, "b") == 0) alpar@1: { kind = GLP_IV; alpar@1: type = GLP_DB, lb = 0.0, ub = 1.0; alpar@1: goto skip; alpar@1: } alpar@1: else alpar@1: error(csa, "column kind missing or invalid"); alpar@1: } alpar@1: read_field(csa); alpar@1: if (strcmp(csa->field, "f") == 0) alpar@1: type = GLP_FR; alpar@1: else if (strcmp(csa->field, "l") == 0) alpar@1: type = GLP_LO; alpar@1: else if (strcmp(csa->field, "u") == 0) alpar@1: type = GLP_UP; alpar@1: else if (strcmp(csa->field, "d") == 0) alpar@1: type = GLP_DB; alpar@1: else if (strcmp(csa->field, "s") == 0) alpar@1: type = GLP_FX; alpar@1: else alpar@1: error(csa, "column type missing or invalid"); alpar@1: if (type == GLP_LO || type == GLP_DB || type == GLP_FX) alpar@1: { read_field(csa); alpar@1: if (str2num(csa->field, &lb) != 0) alpar@1: error(csa, "column lower bound/fixed value missing or" alpar@1: " invalid"); alpar@1: } alpar@1: else alpar@1: lb = 0.0; alpar@1: if (type == GLP_UP || type == GLP_DB) alpar@1: { read_field(csa); alpar@1: if (str2num(csa->field, &ub) != 0) alpar@1: error(csa, "column upper bound missing or invalid"); alpar@1: } alpar@1: else alpar@1: ub = 0.0; alpar@1: skip: if (cf[j] & 0x01) alpar@1: error(csa, "duplicate column descriptor"); alpar@1: glp_set_col_kind(P, j, kind); alpar@1: glp_set_col_bnds(P, j, type, lb, ub), cf[j] |= 0x01; alpar@1: } alpar@1: else if (strcmp(csa->field, "a") == 0) alpar@1: { /* coefficient descriptor */ alpar@1: read_field(csa); alpar@1: if (str2int(csa->field, &i) != 0) alpar@1: error(csa, "row number missing or invalid"); alpar@1: if (!(0 <= i && i <= m)) alpar@1: error(csa, "row number out of range"); alpar@1: read_field(csa); alpar@1: if (str2int(csa->field, &j) != 0) alpar@1: error(csa, "column number missing or invalid"); alpar@1: if (!((i == 0 ? 0 : 1) <= j && j <= n)) alpar@1: error(csa, "column number out of range"); alpar@1: read_field(csa); alpar@1: if (i == 0) alpar@1: { if (str2num(csa->field, &temp) != 0) alpar@1: error(csa, "objective %s missing or invalid", alpar@1: j == 0 ? "constant term" : "coefficient"); alpar@1: if (cf[j] & 0x10) alpar@1: error(csa, "duplicate objective %s", alpar@1: j == 0 ? "constant term" : "coefficient"); alpar@1: glp_set_obj_coef(P, j, temp), cf[j] |= 0x10; alpar@1: } alpar@1: else alpar@1: { if (str2num(csa->field, &temp) != 0) alpar@1: error(csa, "constraint coefficient missing or invalid" alpar@1: ); alpar@1: if (ne == nnz) alpar@1: error(csa, "too many constraint coefficient descripto" alpar@1: "rs"); alpar@1: ln[++ne] = csa->count; alpar@1: ia[ne] = i, ja[ne] = j, ar[ne] = temp; alpar@1: } alpar@1: } alpar@1: else if (strcmp(csa->field, "n") == 0) alpar@1: { /* symbolic name descriptor */ alpar@1: read_field(csa); alpar@1: if (strcmp(csa->field, "p") == 0) alpar@1: { /* problem name */ alpar@1: read_field(csa); alpar@1: if (P->name != NULL) alpar@1: error(csa, "duplicate problem name"); alpar@1: glp_set_prob_name(P, csa->field); alpar@1: } alpar@1: else if (strcmp(csa->field, "z") == 0) alpar@1: { /* objective name */ alpar@1: read_field(csa); alpar@1: if (P->obj != NULL) alpar@1: error(csa, "duplicate objective name"); alpar@1: glp_set_obj_name(P, csa->field); alpar@1: } alpar@1: else if (strcmp(csa->field, "i") == 0) alpar@1: { /* row name */ alpar@1: read_field(csa); alpar@1: if (str2int(csa->field, &i) != 0) alpar@1: error(csa, "row number missing or invalid"); alpar@1: if (!(1 <= i && i <= m)) alpar@1: error(csa, "row number out of range"); alpar@1: read_field(csa); alpar@1: if (P->row[i]->name != NULL) alpar@1: error(csa, "duplicate row name"); alpar@1: glp_set_row_name(P, i, csa->field); alpar@1: } alpar@1: else if (strcmp(csa->field, "j") == 0) alpar@1: { /* column name */ alpar@1: read_field(csa); alpar@1: if (str2int(csa->field, &j) != 0) alpar@1: error(csa, "column number missing or invalid"); alpar@1: if (!(1 <= j && j <= n)) alpar@1: error(csa, "column number out of range"); alpar@1: read_field(csa); alpar@1: if (P->col[j]->name != NULL) alpar@1: error(csa, "duplicate column name"); alpar@1: glp_set_col_name(P, j, csa->field); alpar@1: } alpar@1: else alpar@1: error(csa, "object designator missing or invalid"); alpar@1: } alpar@1: else if (strcmp(csa->field, "e") == 0) alpar@1: break; alpar@1: else alpar@1: error(csa, "line designator missing or invalid"); alpar@1: end_of_line(csa); alpar@1: } alpar@1: if (ne < nnz) alpar@1: error(csa, "too few constraint coefficient descriptors"); alpar@1: xassert(ne == nnz); alpar@1: k = glp_check_dup(m, n, ne, ia, ja); alpar@1: xassert(0 <= k && k <= nnz); alpar@1: if (k > 0) alpar@1: { csa->count = ln[k]; alpar@1: error(csa, "duplicate constraint coefficient"); alpar@1: } alpar@1: glp_load_matrix(P, ne, ia, ja, ar); alpar@1: /* print some statistics */ alpar@1: if (P->name != NULL) alpar@1: xprintf("Problem: %s\n", P->name); alpar@1: if (P->obj != NULL) alpar@1: xprintf("Objective: %s\n", P->obj); alpar@1: xprintf("%d row%s, %d column%s, %d non-zero%s\n", alpar@1: m, m == 1 ? "" : "s", n, n == 1 ? "" : "s", nnz, nnz == 1 ? alpar@1: "" : "s"); alpar@1: if (glp_get_num_int(P) > 0) alpar@1: { int ni = glp_get_num_int(P); alpar@1: int nb = glp_get_num_bin(P); alpar@1: if (ni == 1) alpar@1: { if (nb == 0) alpar@1: xprintf("One variable is integer\n"); alpar@1: else alpar@1: xprintf("One variable is binary\n"); alpar@1: } alpar@1: else alpar@1: { xprintf("%d integer variables, ", ni); alpar@1: if (nb == 0) alpar@1: xprintf("none"); alpar@1: else if (nb == 1) alpar@1: xprintf("one"); alpar@1: else if (nb == ni) alpar@1: xprintf("all"); alpar@1: else alpar@1: xprintf("%d", nb); alpar@1: xprintf(" of which %s binary\n", nb == 1 ? "is" : "are"); alpar@1: } alpar@1: } alpar@1: xprintf("%d lines were read\n", csa->count); alpar@1: /* problem data has been successfully read */ alpar@1: glp_sort_matrix(P); alpar@1: ret = 0; alpar@1: done: if (csa->fp != NULL) xfclose(csa->fp); alpar@1: if (rf != NULL) xfree(rf); alpar@1: if (cf != NULL) xfree(cf); alpar@1: if (ln != NULL) xfree(ln); alpar@1: if (ia != NULL) xfree(ia); alpar@1: if (ja != NULL) xfree(ja); alpar@1: if (ar != NULL) xfree(ar); alpar@1: if (ret) glp_erase_prob(P); alpar@1: return ret; alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * glp_write_prob - write problem data in GLPK format alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * int glp_write_prob(glp_prob *P, int flags, const char *fname); alpar@1: * alpar@1: * The routine glp_write_prob writes problem data in GLPK LP/MIP format alpar@1: * to a text file. alpar@1: * alpar@1: * RETURNS alpar@1: * alpar@1: * If the operation was successful, the routine returns zero. Otherwise alpar@1: * it prints an error message and returns non-zero. */ alpar@1: alpar@1: int glp_write_prob(glp_prob *P, int flags, const char *fname) alpar@1: { XFILE *fp; alpar@1: GLPROW *row; alpar@1: GLPCOL *col; alpar@1: GLPAIJ *aij; alpar@1: int mip, i, j, count, ret; alpar@1: if (P == NULL || P->magic != GLP_PROB_MAGIC) alpar@1: xerror("glp_write_prob: P = %p; invalid problem object\n", alpar@1: P); alpar@1: if (flags != 0) alpar@1: xerror("glp_write_prob: flags = %d; invalid parameter\n", alpar@1: flags); alpar@1: if (fname == NULL) alpar@1: xerror("glp_write_prob: fname = %d; invalid parameter\n", alpar@1: fname); alpar@1: xprintf("Writing problem data to `%s'...\n", fname); alpar@1: fp = xfopen(fname, "w"), count = 0; alpar@1: if (fp == NULL) alpar@1: { xprintf("Unable to create `%s' - %s\n", fname, xerrmsg()); alpar@1: ret = 1; alpar@1: goto done; alpar@1: } alpar@1: /* write problem line */ alpar@1: mip = (glp_get_num_int(P) > 0); alpar@1: xfprintf(fp, "p %s %s %d %d %d\n", !mip ? "lp" : "mip", alpar@1: P->dir == GLP_MIN ? "min" : P->dir == GLP_MAX ? "max" : "???", alpar@1: P->m, P->n, P->nnz), count++; alpar@1: if (P->name != NULL) alpar@1: xfprintf(fp, "n p %s\n", P->name), count++; alpar@1: if (P->obj != NULL) alpar@1: xfprintf(fp, "n z %s\n", P->obj), count++; alpar@1: /* write row descriptors */ alpar@1: for (i = 1; i <= P->m; i++) alpar@1: { row = P->row[i]; alpar@1: if (row->type == GLP_FX && row->lb == 0.0) alpar@1: goto skip1; alpar@1: xfprintf(fp, "i %d ", i), count++; alpar@1: if (row->type == GLP_FR) alpar@1: xfprintf(fp, "f\n"); alpar@1: else if (row->type == GLP_LO) alpar@1: xfprintf(fp, "l %.*g\n", DBL_DIG, row->lb); alpar@1: else if (row->type == GLP_UP) alpar@1: xfprintf(fp, "u %.*g\n", DBL_DIG, row->ub); alpar@1: else if (row->type == GLP_DB) alpar@1: xfprintf(fp, "d %.*g %.*g\n", DBL_DIG, row->lb, DBL_DIG, alpar@1: row->ub); alpar@1: else if (row->type == GLP_FX) alpar@1: xfprintf(fp, "s %.*g\n", DBL_DIG, row->lb); alpar@1: else alpar@1: xassert(row != row); alpar@1: skip1: if (row->name != NULL) alpar@1: xfprintf(fp, "n i %d %s\n", i, row->name), count++; alpar@1: } alpar@1: /* write column descriptors */ alpar@1: for (j = 1; j <= P->n; j++) alpar@1: { col = P->col[j]; alpar@1: if (!mip && col->type == GLP_LO && col->lb == 0.0) alpar@1: goto skip2; alpar@1: if (mip && col->kind == GLP_IV && col->type == GLP_DB && alpar@1: col->lb == 0.0 && col->ub == 1.0) alpar@1: goto skip2; alpar@1: xfprintf(fp, "j %d ", j), count++; alpar@1: if (mip) alpar@1: { if (col->kind == GLP_CV) alpar@1: xfprintf(fp, "c "); alpar@1: else if (col->kind == GLP_IV) alpar@1: xfprintf(fp, "i "); alpar@1: else alpar@1: xassert(col != col); alpar@1: } alpar@1: if (col->type == GLP_FR) alpar@1: xfprintf(fp, "f\n"); alpar@1: else if (col->type == GLP_LO) alpar@1: xfprintf(fp, "l %.*g\n", DBL_DIG, col->lb); alpar@1: else if (col->type == GLP_UP) alpar@1: xfprintf(fp, "u %.*g\n", DBL_DIG, col->ub); alpar@1: else if (col->type == GLP_DB) alpar@1: xfprintf(fp, "d %.*g %.*g\n", DBL_DIG, col->lb, DBL_DIG, alpar@1: col->ub); alpar@1: else if (col->type == GLP_FX) alpar@1: xfprintf(fp, "s %.*g\n", DBL_DIG, col->lb); alpar@1: else alpar@1: xassert(col != col); alpar@1: skip2: if (col->name != NULL) alpar@1: xfprintf(fp, "n j %d %s\n", j, col->name), count++; alpar@1: } alpar@1: /* write objective coefficient descriptors */ alpar@1: if (P->c0 != 0.0) alpar@1: xfprintf(fp, "a 0 0 %.*g\n", DBL_DIG, P->c0), count++; alpar@1: for (j = 1; j <= P->n; j++) alpar@1: { col = P->col[j]; alpar@1: if (col->coef != 0.0) alpar@1: xfprintf(fp, "a 0 %d %.*g\n", j, DBL_DIG, col->coef), alpar@1: count++; alpar@1: } alpar@1: /* write constraint coefficient descriptors */ alpar@1: for (i = 1; i <= P->m; i++) alpar@1: { row = P->row[i]; alpar@1: for (aij = row->ptr; aij != NULL; aij = aij->r_next) alpar@1: xfprintf(fp, "a %d %d %.*g\n", i, aij->col->j, DBL_DIG, alpar@1: aij->val), count++; alpar@1: } alpar@1: /* write end line */ alpar@1: xfprintf(fp, "e o f\n"), count++; alpar@1: xfflush(fp); alpar@1: if (xferror(fp)) alpar@1: { xprintf("Write error on `%s' - %s\n", fname, xerrmsg()); alpar@1: ret = 1; alpar@1: goto done; alpar@1: } alpar@1: xprintf("%d lines were written\n", count); alpar@1: ret = 0; alpar@1: done: if (fp != NULL) xfclose(fp); alpar@1: return ret; alpar@1: } alpar@1: alpar@1: /* eof */