1 /* prmip.c (write MIP solution in printable 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 "prob.h"
24 
25 #define xfprintf glp_format
26 
glp_print_mip(glp_prob * P,const char * fname)27 int glp_print_mip(glp_prob *P, const char *fname)
28 {     /* write MIP solution in printable format */
29       glp_file *fp;
30       GLPROW *row;
31       GLPCOL *col;
32       int i, j, t, ae_ind, re_ind, ret;
33       double ae_max, re_max;
34       xprintf("Writing MIP solution to '%s'...\n", fname);
35       fp = glp_open(fname, "w");
36       if (fp == NULL)
37       {  xprintf("Unable to create '%s' - %s\n", fname, get_err_msg());
38          ret = 1;
39          goto done;
40       }
41       xfprintf(fp, "%-12s%s\n", "Problem:",
42          P->name == NULL ? "" : P->name);
43       xfprintf(fp, "%-12s%d\n", "Rows:", P->m);
44       xfprintf(fp, "%-12s%d (%d integer, %d binary)\n", "Columns:",
45          P->n, glp_get_num_int(P), glp_get_num_bin(P));
46       xfprintf(fp, "%-12s%d\n", "Non-zeros:", P->nnz);
47       t = glp_mip_status(P);
48       xfprintf(fp, "%-12s%s\n", "Status:",
49          t == GLP_OPT    ? "INTEGER OPTIMAL" :
50          t == GLP_FEAS   ? "INTEGER NON-OPTIMAL" :
51          t == GLP_NOFEAS ? "INTEGER EMPTY" :
52          t == GLP_UNDEF  ? "INTEGER UNDEFINED" : "???");
53       xfprintf(fp, "%-12s%s%s%.10g (%s)\n", "Objective:",
54          P->obj == NULL ? "" : P->obj,
55          P->obj == NULL ? "" : " = ", P->mip_obj,
56          P->dir == GLP_MIN ? "MINimum" :
57          P->dir == GLP_MAX ? "MAXimum" : "???");
58       xfprintf(fp, "\n");
59       xfprintf(fp, "   No.   Row name        Activity     Lower bound  "
60          " Upper bound\n");
61       xfprintf(fp, "------ ------------    ------------- ------------- "
62          "-------------\n");
63       for (i = 1; i <= P->m; i++)
64       {  row = P->row[i];
65          xfprintf(fp, "%6d ", i);
66          if (row->name == NULL || strlen(row->name) <= 12)
67             xfprintf(fp, "%-12s ", row->name == NULL ? "" : row->name);
68          else
69             xfprintf(fp, "%s\n%20s", row->name, "");
70          xfprintf(fp, "%3s", "");
71          xfprintf(fp, "%13.6g ",
72             fabs(row->mipx) <= 1e-9 ? 0.0 : row->mipx);
73          if (row->type == GLP_LO || row->type == GLP_DB ||
74              row->type == GLP_FX)
75             xfprintf(fp, "%13.6g ", row->lb);
76          else
77             xfprintf(fp, "%13s ", "");
78          if (row->type == GLP_UP || row->type == GLP_DB)
79             xfprintf(fp, "%13.6g ", row->ub);
80          else
81             xfprintf(fp, "%13s ", row->type == GLP_FX ? "=" : "");
82          xfprintf(fp, "\n");
83       }
84       xfprintf(fp, "\n");
85       xfprintf(fp, "   No. Column name       Activity     Lower bound  "
86          " Upper bound\n");
87       xfprintf(fp, "------ ------------    ------------- ------------- "
88          "-------------\n");
89       for (j = 1; j <= P->n; j++)
90       {  col = P->col[j];
91          xfprintf(fp, "%6d ", j);
92          if (col->name == NULL || strlen(col->name) <= 12)
93             xfprintf(fp, "%-12s ", col->name == NULL ? "" : col->name);
94          else
95             xfprintf(fp, "%s\n%20s", col->name, "");
96          xfprintf(fp, "%s  ",
97             col->kind == GLP_CV ? " " :
98             col->kind == GLP_IV ? "*" : "?");
99          xfprintf(fp, "%13.6g ",
100             fabs(col->mipx) <= 1e-9 ? 0.0 : col->mipx);
101          if (col->type == GLP_LO || col->type == GLP_DB ||
102              col->type == GLP_FX)
103             xfprintf(fp, "%13.6g ", col->lb);
104          else
105             xfprintf(fp, "%13s ", "");
106          if (col->type == GLP_UP || col->type == GLP_DB)
107             xfprintf(fp, "%13.6g ", col->ub);
108          else
109             xfprintf(fp, "%13s ", col->type == GLP_FX ? "=" : "");
110          xfprintf(fp, "\n");
111       }
112       xfprintf(fp, "\n");
113       xfprintf(fp, "Integer feasibility conditions:\n");
114       xfprintf(fp, "\n");
115       glp_check_kkt(P, GLP_MIP, GLP_KKT_PE, &ae_max, &ae_ind, &re_max,
116          &re_ind);
117       xfprintf(fp, "KKT.PE: max.abs.err = %.2e on row %d\n",
118          ae_max, ae_ind);
119       xfprintf(fp, "        max.rel.err = %.2e on row %d\n",
120          re_max, re_ind);
121       xfprintf(fp, "%8s%s\n", "",
122          re_max <= 1e-9 ? "High quality" :
123          re_max <= 1e-6 ? "Medium quality" :
124          re_max <= 1e-3 ? "Low quality" : "SOLUTION IS WRONG");
125       xfprintf(fp, "\n");
126       glp_check_kkt(P, GLP_MIP, GLP_KKT_PB, &ae_max, &ae_ind, &re_max,
127          &re_ind);
128       xfprintf(fp, "KKT.PB: max.abs.err = %.2e on %s %d\n",
129             ae_max, ae_ind <= P->m ? "row" : "column",
130             ae_ind <= P->m ? ae_ind : ae_ind - P->m);
131       xfprintf(fp, "        max.rel.err = %.2e on %s %d\n",
132             re_max, re_ind <= P->m ? "row" : "column",
133             re_ind <= P->m ? re_ind : re_ind - P->m);
134       xfprintf(fp, "%8s%s\n", "",
135          re_max <= 1e-9 ? "High quality" :
136          re_max <= 1e-6 ? "Medium quality" :
137          re_max <= 1e-3 ? "Low quality" : "SOLUTION IS INFEASIBLE");
138       xfprintf(fp, "\n");
139       xfprintf(fp, "End of output\n");
140 #if 0 /* FIXME */
141       xfflush(fp);
142 #endif
143       if (glp_ioerr(fp))
144       {  xprintf("Write error on '%s' - %s\n", fname, get_err_msg());
145          ret = 1;
146          goto done;
147       }
148       ret = 0;
149 done: if (fp != NULL) glp_close(fp);
150       return ret;
151 }
152 
153 /* eof */
154