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