1 /* wrasn.c (write assignment problem data in DIMACS format) */
2 
3 /***********************************************************************
4 *  This code is part of GLPK (GNU Linear Programming Kit).
5 *  Copyright (C) 2009-2016 Free Software Foundation, Inc.
6 *  Written by Andrew Makhorin <mao@gnu.org>.
7 *
8 *  GLPK is free software: you can redistribute it and/or modify it
9 *  under the terms of the GNU General Public License as published by
10 *  the Free Software Foundation, either version 3 of the License, or
11 *  (at your option) any later version.
12 *
13 *  GLPK is distributed in the hope that it will be useful, but WITHOUT
14 *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 *  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
16 *  License for more details.
17 *
18 *  You should have received a copy of the GNU General Public License
19 *  along with GLPK. If not, see <http://www.gnu.org/licenses/>.
20 ***********************************************************************/
21 
22 #include "env.h"
23 #include "glpk.h"
24 
25 #define xfprintf        glp_format
26 
27 /***********************************************************************
28 *  NAME
29 *
30 *  glp_write_asnprob - write assignment problem data in DIMACS format
31 *
32 *  SYNOPSIS
33 *
34 *  int glp_write_asnprob(glp_graph *G, int v_set, int a_cost,
35 *     const char *fname);
36 *
37 *  DESCRIPTION
38 *
39 *  The routine glp_write_asnprob writes assignment problem data in
40 *  DIMACS format to a text file.
41 *
42 *  RETURNS
43 *
44 *  If the operation was successful, the routine returns zero. Otherwise
45 *  it prints an error message and returns non-zero. */
46 
glp_write_asnprob(glp_graph * G,int v_set,int a_cost,const char * fname)47 int glp_write_asnprob(glp_graph *G, int v_set, int a_cost, const char
48       *fname)
49 {     glp_file *fp;
50       glp_vertex *v;
51       glp_arc *a;
52       int i, k, count = 0, ret;
53       double cost;
54       if (v_set >= 0 && v_set > G->v_size - (int)sizeof(int))
55          xerror("glp_write_asnprob: v_set = %d; invalid offset\n",
56             v_set);
57       if (a_cost >= 0 && a_cost > G->a_size - (int)sizeof(double))
58          xerror("glp_write_asnprob: a_cost = %d; invalid offset\n",
59             a_cost);
60       xprintf("Writing assignment problem data to '%s'...\n", fname);
61       fp = glp_open(fname, "w");
62       if (fp == NULL)
63       {  xprintf("Unable to create '%s' - %s\n", fname, get_err_msg());
64          ret = 1;
65          goto done;
66       }
67       xfprintf(fp, "c %s\n",
68          G->name == NULL ? "unknown" : G->name), count++;
69       xfprintf(fp, "p asn %d %d\n", G->nv, G->na), count++;
70       for (i = 1; i <= G->nv; i++)
71       {  v = G->v[i];
72          if (v_set >= 0)
73             memcpy(&k, (char *)v->data + v_set, sizeof(int));
74          else
75             k = (v->out != NULL ? 0 : 1);
76          if (k == 0)
77             xfprintf(fp, "n %d\n", i), count++;
78       }
79       for (i = 1; i <= G->nv; i++)
80       {  v = G->v[i];
81          for (a = v->out; a != NULL; a = a->t_next)
82          {  if (a_cost >= 0)
83                memcpy(&cost, (char *)a->data + a_cost, sizeof(double));
84             else
85                cost = 1.0;
86             xfprintf(fp, "a %d %d %.*g\n",
87                a->tail->i, a->head->i, DBL_DIG, cost), count++;
88          }
89       }
90       xfprintf(fp, "c eof\n"), count++;
91 #if 0 /* FIXME */
92       xfflush(fp);
93 #endif
94       if (glp_ioerr(fp))
95       {  xprintf("Write error on '%s' - %s\n", fname, get_err_msg());
96          ret = 1;
97          goto done;
98       }
99       xprintf("%d lines were written\n", count);
100       ret = 0;
101 done: if (fp != NULL) glp_close(fp);
102       return ret;
103 }
104 
105 /* eof */
106