1 /* pript.c (write interior-point 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_ipt(glp_prob * P,const char * fname)27 int glp_print_ipt(glp_prob *P, const char *fname)
28 {     /* write interior-point 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 interior-point 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\n", "Columns:", P->n);
45       xfprintf(fp, "%-12s%d\n", "Non-zeros:", P->nnz);
46       t = glp_ipt_status(P);
47       xfprintf(fp, "%-12s%s\n", "Status:",
48          t == GLP_OPT    ? "OPTIMAL" :
49          t == GLP_UNDEF  ? "UNDEFINED" :
50          t == GLP_INFEAS ? "INFEASIBLE (INTERMEDIATE)" :
51          t == GLP_NOFEAS ? "INFEASIBLE (FINAL)" : "???");
52       xfprintf(fp, "%-12s%s%s%.10g (%s)\n", "Objective:",
53          P->obj == NULL ? "" : P->obj,
54          P->obj == NULL ? "" : " = ", P->ipt_obj,
55          P->dir == GLP_MIN ? "MINimum" :
56          P->dir == GLP_MAX ? "MAXimum" : "???");
57       xfprintf(fp, "\n");
58       xfprintf(fp, "   No.   Row name        Activity     Lower bound  "
59          " Upper bound    Marginal\n");
60       xfprintf(fp, "------ ------------    ------------- ------------- "
61          "------------- -------------\n");
62       for (i = 1; i <= P->m; i++)
63       {  row = P->row[i];
64          xfprintf(fp, "%6d ", i);
65          if (row->name == NULL || strlen(row->name) <= 12)
66             xfprintf(fp, "%-12s ", row->name == NULL ? "" : row->name);
67          else
68             xfprintf(fp, "%s\n%20s", row->name, "");
69          xfprintf(fp, "%3s", "");
70          xfprintf(fp, "%13.6g ",
71             fabs(row->pval) <= 1e-9 ? 0.0 : row->pval);
72          if (row->type == GLP_LO || row->type == GLP_DB ||
73              row->type == GLP_FX)
74             xfprintf(fp, "%13.6g ", row->lb);
75          else
76             xfprintf(fp, "%13s ", "");
77          if (row->type == GLP_UP || row->type == GLP_DB)
78             xfprintf(fp, "%13.6g ", row->ub);
79          else
80             xfprintf(fp, "%13s ", row->type == GLP_FX ? "=" : "");
81          if (fabs(row->dval) <= 1e-9)
82             xfprintf(fp, "%13s", "< eps");
83          else
84             xfprintf(fp, "%13.6g ", row->dval);
85          xfprintf(fp, "\n");
86       }
87       xfprintf(fp, "\n");
88       xfprintf(fp, "   No. Column name       Activity     Lower bound  "
89          " Upper bound    Marginal\n");
90       xfprintf(fp, "------ ------------    ------------- ------------- "
91          "------------- -------------\n");
92       for (j = 1; j <= P->n; j++)
93       {  col = P->col[j];
94          xfprintf(fp, "%6d ", j);
95          if (col->name == NULL || strlen(col->name) <= 12)
96             xfprintf(fp, "%-12s ", col->name == NULL ? "" : col->name);
97          else
98             xfprintf(fp, "%s\n%20s", col->name, "");
99          xfprintf(fp, "%3s", "");
100          xfprintf(fp, "%13.6g ",
101             fabs(col->pval) <= 1e-9 ? 0.0 : col->pval);
102          if (col->type == GLP_LO || col->type == GLP_DB ||
103              col->type == GLP_FX)
104             xfprintf(fp, "%13.6g ", col->lb);
105          else
106             xfprintf(fp, "%13s ", "");
107          if (col->type == GLP_UP || col->type == GLP_DB)
108             xfprintf(fp, "%13.6g ", col->ub);
109          else
110             xfprintf(fp, "%13s ", col->type == GLP_FX ? "=" : "");
111          if (fabs(col->dval) <= 1e-9)
112             xfprintf(fp, "%13s", "< eps");
113          else
114             xfprintf(fp, "%13.6g ", col->dval);
115          xfprintf(fp, "\n");
116       }
117       xfprintf(fp, "\n");
118       xfprintf(fp, "Karush-Kuhn-Tucker optimality conditions:\n");
119       xfprintf(fp, "\n");
120       glp_check_kkt(P, GLP_IPT, GLP_KKT_PE, &ae_max, &ae_ind, &re_max,
121          &re_ind);
122       xfprintf(fp, "KKT.PE: max.abs.err = %.2e on row %d\n",
123          ae_max, ae_ind);
124       xfprintf(fp, "        max.rel.err = %.2e on row %d\n",
125          re_max, re_ind);
126       xfprintf(fp, "%8s%s\n", "",
127          re_max <= 1e-9 ? "High quality" :
128          re_max <= 1e-6 ? "Medium quality" :
129          re_max <= 1e-3 ? "Low quality" : "PRIMAL SOLUTION IS WRONG");
130       xfprintf(fp, "\n");
131       glp_check_kkt(P, GLP_IPT, GLP_KKT_PB, &ae_max, &ae_ind, &re_max,
132          &re_ind);
133       xfprintf(fp, "KKT.PB: max.abs.err = %.2e on %s %d\n",
134             ae_max, ae_ind <= P->m ? "row" : "column",
135             ae_ind <= P->m ? ae_ind : ae_ind - P->m);
136       xfprintf(fp, "        max.rel.err = %.2e on %s %d\n",
137             re_max, re_ind <= P->m ? "row" : "column",
138             re_ind <= P->m ? re_ind : re_ind - P->m);
139       xfprintf(fp, "%8s%s\n", "",
140          re_max <= 1e-9 ? "High quality" :
141          re_max <= 1e-6 ? "Medium quality" :
142          re_max <= 1e-3 ? "Low quality" : "PRIMAL SOLUTION IS INFEASIBL"
143             "E");
144       xfprintf(fp, "\n");
145       glp_check_kkt(P, GLP_IPT, GLP_KKT_DE, &ae_max, &ae_ind, &re_max,
146          &re_ind);
147       xfprintf(fp, "KKT.DE: max.abs.err = %.2e on column %d\n",
148          ae_max, ae_ind == 0 ? 0 : ae_ind - P->m);
149       xfprintf(fp, "        max.rel.err = %.2e on column %d\n",
150          re_max, re_ind == 0 ? 0 : re_ind - P->m);
151       xfprintf(fp, "%8s%s\n", "",
152          re_max <= 1e-9 ? "High quality" :
153          re_max <= 1e-6 ? "Medium quality" :
154          re_max <= 1e-3 ? "Low quality" : "DUAL SOLUTION IS WRONG");
155       xfprintf(fp, "\n");
156       glp_check_kkt(P, GLP_IPT, GLP_KKT_DB, &ae_max, &ae_ind, &re_max,
157          &re_ind);
158       xfprintf(fp, "KKT.DB: max.abs.err = %.2e on %s %d\n",
159             ae_max, ae_ind <= P->m ? "row" : "column",
160             ae_ind <= P->m ? ae_ind : ae_ind - P->m);
161       xfprintf(fp, "        max.rel.err = %.2e on %s %d\n",
162             re_max, re_ind <= P->m ? "row" : "column",
163             re_ind <= P->m ? re_ind : re_ind - P->m);
164       xfprintf(fp, "%8s%s\n", "",
165          re_max <= 1e-9 ? "High quality" :
166          re_max <= 1e-6 ? "Medium quality" :
167          re_max <= 1e-3 ? "Low quality" : "DUAL SOLUTION IS INFEASIBLE")
168             ;
169       xfprintf(fp, "\n");
170       xfprintf(fp, "End of output\n");
171 #if 0 /* FIXME */
172       xfflush(fp);
173 #endif
174       if (glp_ioerr(fp))
175       {  xprintf("Write error on '%s' - %s\n", fname, get_err_msg());
176          ret = 1;
177          goto done;
178       }
179       ret = 0;
180 done: if (fp != NULL) glp_close(fp);
181       return ret;
182 }
183 
184 /* eof */
185