glpapi11.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218
  1. /* glpapi11.c (utility routines) */
  2. /***********************************************************************
  3. * This code is part of GLPK (GNU Linear Programming Kit).
  4. *
  5. * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
  6. * 2009, 2010 Andrew Makhorin, Department for Applied Informatics,
  7. * Moscow Aviation Institute, Moscow, Russia. All rights reserved.
  8. * E-mail: <mao@gnu.org>.
  9. *
  10. * GLPK is free software: you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * GLPK is distributed in the hope that it will be useful, but WITHOUT
  16. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  17. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  18. * License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with GLPK. If not, see <http://www.gnu.org/licenses/>.
  22. ***********************************************************************/
  23. #include "glpapi.h"
  24. int glp_print_sol(glp_prob *P, const char *fname)
  25. { /* write basic solution in printable format */
  26. XFILE *fp;
  27. GLPROW *row;
  28. GLPCOL *col;
  29. int i, j, t, ae_ind, re_ind, ret;
  30. double ae_max, re_max;
  31. xprintf("Writing basic solution to `%s'...\n", fname);
  32. fp = xfopen(fname, "w");
  33. if (fp == NULL)
  34. { xprintf("Unable to create `%s' - %s\n", fname, xerrmsg());
  35. ret = 1;
  36. goto done;
  37. }
  38. xfprintf(fp, "%-12s%s\n", "Problem:",
  39. P->name == NULL ? "" : P->name);
  40. xfprintf(fp, "%-12s%d\n", "Rows:", P->m);
  41. xfprintf(fp, "%-12s%d\n", "Columns:", P->n);
  42. xfprintf(fp, "%-12s%d\n", "Non-zeros:", P->nnz);
  43. t = glp_get_status(P);
  44. xfprintf(fp, "%-12s%s\n", "Status:",
  45. t == GLP_OPT ? "OPTIMAL" :
  46. t == GLP_FEAS ? "FEASIBLE" :
  47. t == GLP_INFEAS ? "INFEASIBLE (INTERMEDIATE)" :
  48. t == GLP_NOFEAS ? "INFEASIBLE (FINAL)" :
  49. t == GLP_UNBND ? "UNBOUNDED" :
  50. t == GLP_UNDEF ? "UNDEFINED" : "???");
  51. xfprintf(fp, "%-12s%s%s%.10g (%s)\n", "Objective:",
  52. P->obj == NULL ? "" : P->obj,
  53. P->obj == NULL ? "" : " = ", P->obj_val,
  54. P->dir == GLP_MIN ? "MINimum" :
  55. P->dir == GLP_MAX ? "MAXimum" : "???");
  56. xfprintf(fp, "\n");
  57. xfprintf(fp, " No. Row name St Activity Lower bound "
  58. " Upper bound Marginal\n");
  59. xfprintf(fp, "------ ------------ -- ------------- ------------- "
  60. "------------- -------------\n");
  61. for (i = 1; i <= P->m; i++)
  62. { row = P->row[i];
  63. xfprintf(fp, "%6d ", i);
  64. if (row->name == NULL || strlen(row->name) <= 12)
  65. xfprintf(fp, "%-12s ", row->name == NULL ? "" : row->name);
  66. else
  67. xfprintf(fp, "%s\n%20s", row->name, "");
  68. xfprintf(fp, "%s ",
  69. row->stat == GLP_BS ? "B " :
  70. row->stat == GLP_NL ? "NL" :
  71. row->stat == GLP_NU ? "NU" :
  72. row->stat == GLP_NF ? "NF" :
  73. row->stat == GLP_NS ? "NS" : "??");
  74. xfprintf(fp, "%13.6g ",
  75. fabs(row->prim) <= 1e-9 ? 0.0 : row->prim);
  76. if (row->type == GLP_LO || row->type == GLP_DB ||
  77. row->type == GLP_FX)
  78. xfprintf(fp, "%13.6g ", row->lb);
  79. else
  80. xfprintf(fp, "%13s ", "");
  81. if (row->type == GLP_UP || row->type == GLP_DB)
  82. xfprintf(fp, "%13.6g ", row->ub);
  83. else
  84. xfprintf(fp, "%13s ", row->type == GLP_FX ? "=" : "");
  85. if (row->stat != GLP_BS)
  86. { if (fabs(row->dual) <= 1e-9)
  87. xfprintf(fp, "%13s", "< eps");
  88. else
  89. xfprintf(fp, "%13.6g ", row->dual);
  90. }
  91. xfprintf(fp, "\n");
  92. }
  93. xfprintf(fp, "\n");
  94. xfprintf(fp, " No. Column name St Activity Lower bound "
  95. " Upper bound Marginal\n");
  96. xfprintf(fp, "------ ------------ -- ------------- ------------- "
  97. "------------- -------------\n");
  98. for (j = 1; j <= P->n; j++)
  99. { col = P->col[j];
  100. xfprintf(fp, "%6d ", j);
  101. if (col->name == NULL || strlen(col->name) <= 12)
  102. xfprintf(fp, "%-12s ", col->name == NULL ? "" : col->name);
  103. else
  104. xfprintf(fp, "%s\n%20s", col->name, "");
  105. xfprintf(fp, "%s ",
  106. col->stat == GLP_BS ? "B " :
  107. col->stat == GLP_NL ? "NL" :
  108. col->stat == GLP_NU ? "NU" :
  109. col->stat == GLP_NF ? "NF" :
  110. col->stat == GLP_NS ? "NS" : "??");
  111. xfprintf(fp, "%13.6g ",
  112. fabs(col->prim) <= 1e-9 ? 0.0 : col->prim);
  113. if (col->type == GLP_LO || col->type == GLP_DB ||
  114. col->type == GLP_FX)
  115. xfprintf(fp, "%13.6g ", col->lb);
  116. else
  117. xfprintf(fp, "%13s ", "");
  118. if (col->type == GLP_UP || col->type == GLP_DB)
  119. xfprintf(fp, "%13.6g ", col->ub);
  120. else
  121. xfprintf(fp, "%13s ", col->type == GLP_FX ? "=" : "");
  122. if (col->stat != GLP_BS)
  123. { if (fabs(col->dual) <= 1e-9)
  124. xfprintf(fp, "%13s", "< eps");
  125. else
  126. xfprintf(fp, "%13.6g ", col->dual);
  127. }
  128. xfprintf(fp, "\n");
  129. }
  130. xfprintf(fp, "\n");
  131. xfprintf(fp, "Karush-Kuhn-Tucker optimality conditions:\n");
  132. xfprintf(fp, "\n");
  133. _glp_check_kkt(P, GLP_SOL, GLP_KKT_PE, &ae_max, &ae_ind, &re_max,
  134. &re_ind);
  135. xfprintf(fp, "KKT.PE: max.abs.err = %.2e on row %d\n",
  136. ae_max, ae_ind);
  137. xfprintf(fp, " max.rel.err = %.2e on row %d\n",
  138. re_max, re_ind);
  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 WRONG");
  143. xfprintf(fp, "\n");
  144. _glp_check_kkt(P, GLP_SOL, GLP_KKT_PB, &ae_max, &ae_ind, &re_max,
  145. &re_ind);
  146. xfprintf(fp, "KKT.PB: max.abs.err = %.2e on %s %d\n",
  147. ae_max, ae_ind <= P->m ? "row" : "column",
  148. ae_ind <= P->m ? ae_ind : ae_ind - P->m);
  149. xfprintf(fp, " max.rel.err = %.2e on %s %d\n",
  150. re_max, re_ind <= P->m ? "row" : "column",
  151. re_ind <= P->m ? re_ind : re_ind - P->m);
  152. xfprintf(fp, "%8s%s\n", "",
  153. re_max <= 1e-9 ? "High quality" :
  154. re_max <= 1e-6 ? "Medium quality" :
  155. re_max <= 1e-3 ? "Low quality" : "PRIMAL SOLUTION IS INFEASIBL"
  156. "E");
  157. xfprintf(fp, "\n");
  158. _glp_check_kkt(P, GLP_SOL, GLP_KKT_DE, &ae_max, &ae_ind, &re_max,
  159. &re_ind);
  160. xfprintf(fp, "KKT.DE: max.abs.err = %.2e on column %d\n",
  161. ae_max, ae_ind == 0 ? 0 : ae_ind - P->m);
  162. xfprintf(fp, " max.rel.err = %.2e on column %d\n",
  163. re_max, re_ind == 0 ? 0 : 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 WRONG");
  168. xfprintf(fp, "\n");
  169. _glp_check_kkt(P, GLP_SOL, GLP_KKT_DB, &ae_max, &ae_ind, &re_max,
  170. &re_ind);
  171. xfprintf(fp, "KKT.DB: max.abs.err = %.2e on %s %d\n",
  172. ae_max, ae_ind <= P->m ? "row" : "column",
  173. ae_ind <= P->m ? ae_ind : ae_ind - P->m);
  174. xfprintf(fp, " max.rel.err = %.2e on %s %d\n",
  175. re_max, re_ind <= P->m ? "row" : "column",
  176. re_ind <= P->m ? re_ind : re_ind - P->m);
  177. xfprintf(fp, "%8s%s\n", "",
  178. re_max <= 1e-9 ? "High quality" :
  179. re_max <= 1e-6 ? "Medium quality" :
  180. re_max <= 1e-3 ? "Low quality" : "DUAL SOLUTION IS INFEASIBLE")
  181. ;
  182. xfprintf(fp, "\n");
  183. xfprintf(fp, "End of output\n");
  184. xfflush(fp);
  185. if (xferror(fp))
  186. { xprintf("Write error on `%s' - %s\n", fname, xerrmsg());
  187. ret = 1;
  188. goto done;
  189. }
  190. ret = 0;
  191. done: if (fp != NULL) xfclose(fp);
  192. return ret;
  193. }
  194. /***********************************************************************
  195. * NAME
  196. *
  197. * glp_read_sol - read basic solution from text file
  198. *
  199. * SYNOPSIS
  200. *
  201. * int glp_read_sol(glp_prob *lp, const char *fname);
  202. *
  203. * DESCRIPTION
  204. *
  205. * The routine glp_read_sol reads basic solution from a text file whose
  206. * name is specified by the parameter fname into the problem object.
  207. *
  208. * For the file format see description of the routine glp_write_sol.
  209. *
  210. * RETURNS
  211. *
  212. * On success the routine returns zero, otherwise non-zero. */
  213. int glp_read_sol(glp_prob *lp, const char *fname)
  214. { glp_data *data;
  215. jmp_buf jump;
  216. int i, j, k, ret = 0;
  217. xprintf("Reading basic solution from `%s'...\n", fname);
  218. data = glp_sdf_open_file(fname);
  219. if (data == NULL)
  220. { ret = 1;
  221. goto done;
  222. }
  223. if (setjmp(jump))
  224. { ret = 1;
  225. goto done;
  226. }
  227. glp_sdf_set_jump(data, jump);
  228. /* number of rows, number of columns */
  229. k = glp_sdf_read_int(data);
  230. if (k != lp->m)
  231. glp_sdf_error(data, "wrong number of rows\n");
  232. k = glp_sdf_read_int(data);
  233. if (k != lp->n)
  234. glp_sdf_error(data, "wrong number of columns\n");
  235. /* primal status, dual status, objective value */
  236. k = glp_sdf_read_int(data);
  237. if (!(k == GLP_UNDEF || k == GLP_FEAS || k == GLP_INFEAS ||
  238. k == GLP_NOFEAS))
  239. glp_sdf_error(data, "invalid primal status\n");
  240. lp->pbs_stat = k;
  241. k = glp_sdf_read_int(data);
  242. if (!(k == GLP_UNDEF || k == GLP_FEAS || k == GLP_INFEAS ||
  243. k == GLP_NOFEAS))
  244. glp_sdf_error(data, "invalid dual status\n");
  245. lp->dbs_stat = k;
  246. lp->obj_val = glp_sdf_read_num(data);
  247. /* rows (auxiliary variables) */
  248. for (i = 1; i <= lp->m; i++)
  249. { GLPROW *row = lp->row[i];
  250. /* status, primal value, dual value */
  251. k = glp_sdf_read_int(data);
  252. if (!(k == GLP_BS || k == GLP_NL || k == GLP_NU ||
  253. k == GLP_NF || k == GLP_NS))
  254. glp_sdf_error(data, "invalid row status\n");
  255. glp_set_row_stat(lp, i, k);
  256. row->prim = glp_sdf_read_num(data);
  257. row->dual = glp_sdf_read_num(data);
  258. }
  259. /* columns (structural variables) */
  260. for (j = 1; j <= lp->n; j++)
  261. { GLPCOL *col = lp->col[j];
  262. /* status, primal value, dual value */
  263. k = glp_sdf_read_int(data);
  264. if (!(k == GLP_BS || k == GLP_NL || k == GLP_NU ||
  265. k == GLP_NF || k == GLP_NS))
  266. glp_sdf_error(data, "invalid column status\n");
  267. glp_set_col_stat(lp, j, k);
  268. col->prim = glp_sdf_read_num(data);
  269. col->dual = glp_sdf_read_num(data);
  270. }
  271. xprintf("%d lines were read\n", glp_sdf_line(data));
  272. done: if (ret) lp->pbs_stat = lp->dbs_stat = GLP_UNDEF;
  273. if (data != NULL) glp_sdf_close_file(data);
  274. return ret;
  275. }
  276. /***********************************************************************
  277. * NAME
  278. *
  279. * glp_write_sol - write basic solution to text file
  280. *
  281. * SYNOPSIS
  282. *
  283. * int glp_write_sol(glp_prob *lp, const char *fname);
  284. *
  285. * DESCRIPTION
  286. *
  287. * The routine glp_write_sol writes the current basic solution to a
  288. * text file whose name is specified by the parameter fname. This file
  289. * can be read back with the routine glp_read_sol.
  290. *
  291. * RETURNS
  292. *
  293. * On success the routine returns zero, otherwise non-zero.
  294. *
  295. * FILE FORMAT
  296. *
  297. * The file created by the routine glp_write_sol is a plain text file,
  298. * which contains the following information:
  299. *
  300. * m n
  301. * p_stat d_stat obj_val
  302. * r_stat[1] r_prim[1] r_dual[1]
  303. * . . .
  304. * r_stat[m] r_prim[m] r_dual[m]
  305. * c_stat[1] c_prim[1] c_dual[1]
  306. * . . .
  307. * c_stat[n] c_prim[n] c_dual[n]
  308. *
  309. * where:
  310. * m is the number of rows (auxiliary variables);
  311. * n is the number of columns (structural variables);
  312. * p_stat is the primal status of the basic solution (GLP_UNDEF = 1,
  313. * GLP_FEAS = 2, GLP_INFEAS = 3, or GLP_NOFEAS = 4);
  314. * d_stat is the dual status of the basic solution (GLP_UNDEF = 1,
  315. * GLP_FEAS = 2, GLP_INFEAS = 3, or GLP_NOFEAS = 4);
  316. * obj_val is the objective value;
  317. * r_stat[i], i = 1,...,m, is the status of i-th row (GLP_BS = 1,
  318. * GLP_NL = 2, GLP_NU = 3, GLP_NF = 4, or GLP_NS = 5);
  319. * r_prim[i], i = 1,...,m, is the primal value of i-th row;
  320. * r_dual[i], i = 1,...,m, is the dual value of i-th row;
  321. * c_stat[j], j = 1,...,n, is the status of j-th column (GLP_BS = 1,
  322. * GLP_NL = 2, GLP_NU = 3, GLP_NF = 4, or GLP_NS = 5);
  323. * c_prim[j], j = 1,...,n, is the primal value of j-th column;
  324. * c_dual[j], j = 1,...,n, is the dual value of j-th column. */
  325. int glp_write_sol(glp_prob *lp, const char *fname)
  326. { XFILE *fp;
  327. int i, j, ret = 0;
  328. xprintf("Writing basic solution to `%s'...\n", fname);
  329. fp = xfopen(fname, "w");
  330. if (fp == NULL)
  331. { xprintf("Unable to create `%s' - %s\n", fname, xerrmsg());
  332. ret = 1;
  333. goto done;
  334. }
  335. /* number of rows, number of columns */
  336. xfprintf(fp, "%d %d\n", lp->m, lp->n);
  337. /* primal status, dual status, objective value */
  338. xfprintf(fp, "%d %d %.*g\n", lp->pbs_stat, lp->dbs_stat, DBL_DIG,
  339. lp->obj_val);
  340. /* rows (auxiliary variables) */
  341. for (i = 1; i <= lp->m; i++)
  342. { GLPROW *row = lp->row[i];
  343. /* status, primal value, dual value */
  344. xfprintf(fp, "%d %.*g %.*g\n", row->stat, DBL_DIG, row->prim,
  345. DBL_DIG, row->dual);
  346. }
  347. /* columns (structural variables) */
  348. for (j = 1; j <= lp->n; j++)
  349. { GLPCOL *col = lp->col[j];
  350. /* status, primal value, dual value */
  351. xfprintf(fp, "%d %.*g %.*g\n", col->stat, DBL_DIG, col->prim,
  352. DBL_DIG, col->dual);
  353. }
  354. xfflush(fp);
  355. if (xferror(fp))
  356. { xprintf("Write error on `%s' - %s\n", fname, xerrmsg());
  357. ret = 1;
  358. goto done;
  359. }
  360. xprintf("%d lines were written\n", 2 + lp->m + lp->n);
  361. done: if (fp != NULL) xfclose(fp);
  362. return ret;
  363. }
  364. /**********************************************************************/
  365. static char *format(char buf[13+1], double x)
  366. { /* format floating-point number in MPS/360-like style */
  367. if (x == -DBL_MAX)
  368. strcpy(buf, " -Inf");
  369. else if (x == +DBL_MAX)
  370. strcpy(buf, " +Inf");
  371. else if (fabs(x) <= 999999.99998)
  372. { sprintf(buf, "%13.5f", x);
  373. #if 1
  374. if (strcmp(buf, " 0.00000") == 0 ||
  375. strcmp(buf, " -0.00000") == 0)
  376. strcpy(buf, " . ");
  377. else if (memcmp(buf, " 0.", 8) == 0)
  378. memcpy(buf, " .", 8);
  379. else if (memcmp(buf, " -0.", 8) == 0)
  380. memcpy(buf, " -.", 8);
  381. #endif
  382. }
  383. else
  384. sprintf(buf, "%13.6g", x);
  385. return buf;
  386. }
  387. int glp_print_ranges(glp_prob *P, int len, const int list[],
  388. int flags, const char *fname)
  389. { /* print sensitivity analysis report */
  390. XFILE *fp = NULL;
  391. GLPROW *row;
  392. GLPCOL *col;
  393. int m, n, pass, k, t, numb, type, stat, var1, var2, count, page,
  394. ret;
  395. double lb, ub, slack, coef, prim, dual, value1, value2, coef1,
  396. coef2, obj1, obj2;
  397. const char *name, *limit;
  398. char buf[13+1];
  399. /* sanity checks */
  400. if (P == NULL || P->magic != GLP_PROB_MAGIC)
  401. xerror("glp_print_ranges: P = %p; invalid problem object\n",
  402. P);
  403. m = P->m, n = P->n;
  404. if (len < 0)
  405. xerror("glp_print_ranges: len = %d; invalid list length\n",
  406. len);
  407. if (len > 0)
  408. { if (list == NULL)
  409. xerror("glp_print_ranges: list = %p: invalid parameter\n",
  410. list);
  411. for (t = 1; t <= len; t++)
  412. { k = list[t];
  413. if (!(1 <= k && k <= m+n))
  414. xerror("glp_print_ranges: list[%d] = %d; row/column numb"
  415. "er out of range\n", t, k);
  416. }
  417. }
  418. if (flags != 0)
  419. xerror("glp_print_ranges: flags = %d; invalid parameter\n",
  420. flags);
  421. if (fname == NULL)
  422. xerror("glp_print_ranges: fname = %p; invalid parameter\n",
  423. fname);
  424. if (glp_get_status(P) != GLP_OPT)
  425. { xprintf("glp_print_ranges: optimal basic solution required\n");
  426. ret = 1;
  427. goto done;
  428. }
  429. if (!glp_bf_exists(P))
  430. { xprintf("glp_print_ranges: basis factorization required\n");
  431. ret = 2;
  432. goto done;
  433. }
  434. /* start reporting */
  435. xprintf("Write sensitivity analysis report to `%s'...\n", fname);
  436. fp = xfopen(fname, "w");
  437. if (fp == NULL)
  438. { xprintf("Unable to create `%s' - %s\n", fname, xerrmsg());
  439. ret = 3;
  440. goto done;
  441. }
  442. page = count = 0;
  443. for (pass = 1; pass <= 2; pass++)
  444. for (t = 1; t <= (len == 0 ? m+n : len); t++)
  445. { if (t == 1) count = 0;
  446. k = (len == 0 ? t : list[t]);
  447. if (pass == 1 && k > m || pass == 2 && k <= m)
  448. continue;
  449. if (count == 0)
  450. { xfprintf(fp, "GLPK %-4s - SENSITIVITY ANALYSIS REPORT%73sPa"
  451. "ge%4d\n", glp_version(), "", ++page);
  452. xfprintf(fp, "\n");
  453. xfprintf(fp, "%-12s%s\n", "Problem:",
  454. P->name == NULL ? "" : P->name);
  455. xfprintf(fp, "%-12s%s%s%.10g (%s)\n", "Objective:",
  456. P->obj == NULL ? "" : P->obj,
  457. P->obj == NULL ? "" : " = ", P->obj_val,
  458. P->dir == GLP_MIN ? "MINimum" :
  459. P->dir == GLP_MAX ? "MAXimum" : "???");
  460. xfprintf(fp, "\n");
  461. xfprintf(fp, "%6s %-12s %2s %13s %13s %13s %13s %13s %13s "
  462. "%s\n", "No.", pass == 1 ? "Row name" : "Column name",
  463. "St", "Activity", pass == 1 ? "Slack" : "Obj coef",
  464. "Lower bound", "Activity", "Obj coef", "Obj value at",
  465. "Limiting");
  466. xfprintf(fp, "%6s %-12s %2s %13s %13s %13s %13s %13s %13s "
  467. "%s\n", "", "", "", "", "Marginal", "Upper bound",
  468. "range", "range", "break point", "variable");
  469. xfprintf(fp, "------ ------------ -- ------------- --------"
  470. "----- ------------- ------------- ------------- ------"
  471. "------- ------------\n");
  472. }
  473. if (pass == 1)
  474. { numb = k;
  475. xassert(1 <= numb && numb <= m);
  476. row = P->row[numb];
  477. name = row->name;
  478. type = row->type;
  479. lb = glp_get_row_lb(P, numb);
  480. ub = glp_get_row_ub(P, numb);
  481. coef = 0.0;
  482. stat = row->stat;
  483. prim = row->prim;
  484. if (type == GLP_FR)
  485. slack = - prim;
  486. else if (type == GLP_LO)
  487. slack = lb - prim;
  488. else if (type == GLP_UP || type == GLP_DB || type == GLP_FX)
  489. slack = ub - prim;
  490. dual = row->dual;
  491. }
  492. else
  493. { numb = k - m;
  494. xassert(1 <= numb && numb <= n);
  495. col = P->col[numb];
  496. name = col->name;
  497. lb = glp_get_col_lb(P, numb);
  498. ub = glp_get_col_ub(P, numb);
  499. coef = col->coef;
  500. stat = col->stat;
  501. prim = col->prim;
  502. slack = 0.0;
  503. dual = col->dual;
  504. }
  505. if (stat != GLP_BS)
  506. { glp_analyze_bound(P, k, &value1, &var1, &value2, &var2);
  507. if (stat == GLP_NF)
  508. coef1 = coef2 = coef;
  509. else if (stat == GLP_NS)
  510. coef1 = -DBL_MAX, coef2 = +DBL_MAX;
  511. else if (stat == GLP_NL && P->dir == GLP_MIN ||
  512. stat == GLP_NU && P->dir == GLP_MAX)
  513. coef1 = coef - dual, coef2 = +DBL_MAX;
  514. else
  515. coef1 = -DBL_MAX, coef2 = coef - dual;
  516. if (value1 == -DBL_MAX)
  517. { if (dual < -1e-9)
  518. obj1 = +DBL_MAX;
  519. else if (dual > +1e-9)
  520. obj1 = -DBL_MAX;
  521. else
  522. obj1 = P->obj_val;
  523. }
  524. else
  525. obj1 = P->obj_val + dual * (value1 - prim);
  526. if (value2 == +DBL_MAX)
  527. { if (dual < -1e-9)
  528. obj2 = -DBL_MAX;
  529. else if (dual > +1e-9)
  530. obj2 = +DBL_MAX;
  531. else
  532. obj2 = P->obj_val;
  533. }
  534. else
  535. obj2 = P->obj_val + dual * (value2 - prim);
  536. }
  537. else
  538. { glp_analyze_coef(P, k, &coef1, &var1, &value1, &coef2,
  539. &var2, &value2);
  540. if (coef1 == -DBL_MAX)
  541. { if (prim < -1e-9)
  542. obj1 = +DBL_MAX;
  543. else if (prim > +1e-9)
  544. obj1 = -DBL_MAX;
  545. else
  546. obj1 = P->obj_val;
  547. }
  548. else
  549. obj1 = P->obj_val + (coef1 - coef) * prim;
  550. if (coef2 == +DBL_MAX)
  551. { if (prim < -1e-9)
  552. obj2 = -DBL_MAX;
  553. else if (prim > +1e-9)
  554. obj2 = +DBL_MAX;
  555. else
  556. obj2 = P->obj_val;
  557. }
  558. else
  559. obj2 = P->obj_val + (coef2 - coef) * prim;
  560. }
  561. /*** first line ***/
  562. /* row/column number */
  563. xfprintf(fp, "%6d", numb);
  564. /* row/column name */
  565. xfprintf(fp, " %-12.12s", name == NULL ? "" : name);
  566. if (name != NULL && strlen(name) > 12)
  567. xfprintf(fp, "%s\n%6s %12s", name+12, "", "");
  568. /* row/column status */
  569. xfprintf(fp, " %2s",
  570. stat == GLP_BS ? "BS" : stat == GLP_NL ? "NL" :
  571. stat == GLP_NU ? "NU" : stat == GLP_NF ? "NF" :
  572. stat == GLP_NS ? "NS" : "??");
  573. /* row/column activity */
  574. xfprintf(fp, " %s", format(buf, prim));
  575. /* row slack, column objective coefficient */
  576. xfprintf(fp, " %s", format(buf, k <= m ? slack : coef));
  577. /* row/column lower bound */
  578. xfprintf(fp, " %s", format(buf, lb));
  579. /* row/column activity range */
  580. xfprintf(fp, " %s", format(buf, value1));
  581. /* row/column objective coefficient range */
  582. xfprintf(fp, " %s", format(buf, coef1));
  583. /* objective value at break point */
  584. xfprintf(fp, " %s", format(buf, obj1));
  585. /* limiting variable name */
  586. if (var1 != 0)
  587. { if (var1 <= m)
  588. limit = glp_get_row_name(P, var1);
  589. else
  590. limit = glp_get_col_name(P, var1 - m);
  591. if (limit != NULL)
  592. xfprintf(fp, " %s", limit);
  593. }
  594. xfprintf(fp, "\n");
  595. /*** second line ***/
  596. xfprintf(fp, "%6s %-12s %2s %13s", "", "", "", "");
  597. /* row/column reduced cost */
  598. xfprintf(fp, " %s", format(buf, dual));
  599. /* row/column upper bound */
  600. xfprintf(fp, " %s", format(buf, ub));
  601. /* row/column activity range */
  602. xfprintf(fp, " %s", format(buf, value2));
  603. /* row/column objective coefficient range */
  604. xfprintf(fp, " %s", format(buf, coef2));
  605. /* objective value at break point */
  606. xfprintf(fp, " %s", format(buf, obj2));
  607. /* limiting variable name */
  608. if (var2 != 0)
  609. { if (var2 <= m)
  610. limit = glp_get_row_name(P, var2);
  611. else
  612. limit = glp_get_col_name(P, var2 - m);
  613. if (limit != NULL)
  614. xfprintf(fp, " %s", limit);
  615. }
  616. xfprintf(fp, "\n");
  617. xfprintf(fp, "\n");
  618. /* print 10 items per page */
  619. count = (count + 1) % 10;
  620. }
  621. xfprintf(fp, "End of report\n");
  622. xfflush(fp);
  623. if (xferror(fp))
  624. { xprintf("Write error on `%s' - %s\n", fname, xerrmsg());
  625. ret = 4;
  626. goto done;
  627. }
  628. ret = 0;
  629. done: if (fp != NULL) xfclose(fp);
  630. return ret;
  631. }
  632. /**********************************************************************/
  633. int glp_print_ipt(glp_prob *P, const char *fname)
  634. { /* write interior-point solution in printable format */
  635. XFILE *fp;
  636. GLPROW *row;
  637. GLPCOL *col;
  638. int i, j, t, ae_ind, re_ind, ret;
  639. double ae_max, re_max;
  640. xprintf("Writing interior-point solution to `%s'...\n", fname);
  641. fp = xfopen(fname, "w");
  642. if (fp == NULL)
  643. { xprintf("Unable to create `%s' - %s\n", fname, xerrmsg());
  644. ret = 1;
  645. goto done;
  646. }
  647. xfprintf(fp, "%-12s%s\n", "Problem:",
  648. P->name == NULL ? "" : P->name);
  649. xfprintf(fp, "%-12s%d\n", "Rows:", P->m);
  650. xfprintf(fp, "%-12s%d\n", "Columns:", P->n);
  651. xfprintf(fp, "%-12s%d\n", "Non-zeros:", P->nnz);
  652. t = glp_ipt_status(P);
  653. xfprintf(fp, "%-12s%s\n", "Status:",
  654. t == GLP_OPT ? "OPTIMAL" :
  655. t == GLP_UNDEF ? "UNDEFINED" :
  656. t == GLP_INFEAS ? "INFEASIBLE (INTERMEDIATE)" :
  657. t == GLP_NOFEAS ? "INFEASIBLE (FINAL)" : "???");
  658. xfprintf(fp, "%-12s%s%s%.10g (%s)\n", "Objective:",
  659. P->obj == NULL ? "" : P->obj,
  660. P->obj == NULL ? "" : " = ", P->ipt_obj,
  661. P->dir == GLP_MIN ? "MINimum" :
  662. P->dir == GLP_MAX ? "MAXimum" : "???");
  663. xfprintf(fp, "\n");
  664. xfprintf(fp, " No. Row name Activity Lower bound "
  665. " Upper bound Marginal\n");
  666. xfprintf(fp, "------ ------------ ------------- ------------- "
  667. "------------- -------------\n");
  668. for (i = 1; i <= P->m; i++)
  669. { row = P->row[i];
  670. xfprintf(fp, "%6d ", i);
  671. if (row->name == NULL || strlen(row->name) <= 12)
  672. xfprintf(fp, "%-12s ", row->name == NULL ? "" : row->name);
  673. else
  674. xfprintf(fp, "%s\n%20s", row->name, "");
  675. xfprintf(fp, "%3s", "");
  676. xfprintf(fp, "%13.6g ",
  677. fabs(row->pval) <= 1e-9 ? 0.0 : row->pval);
  678. if (row->type == GLP_LO || row->type == GLP_DB ||
  679. row->type == GLP_FX)
  680. xfprintf(fp, "%13.6g ", row->lb);
  681. else
  682. xfprintf(fp, "%13s ", "");
  683. if (row->type == GLP_UP || row->type == GLP_DB)
  684. xfprintf(fp, "%13.6g ", row->ub);
  685. else
  686. xfprintf(fp, "%13s ", row->type == GLP_FX ? "=" : "");
  687. if (fabs(row->dval) <= 1e-9)
  688. xfprintf(fp, "%13s", "< eps");
  689. else
  690. xfprintf(fp, "%13.6g ", row->dval);
  691. xfprintf(fp, "\n");
  692. }
  693. xfprintf(fp, "\n");
  694. xfprintf(fp, " No. Column name Activity Lower bound "
  695. " Upper bound Marginal\n");
  696. xfprintf(fp, "------ ------------ ------------- ------------- "
  697. "------------- -------------\n");
  698. for (j = 1; j <= P->n; j++)
  699. { col = P->col[j];
  700. xfprintf(fp, "%6d ", j);
  701. if (col->name == NULL || strlen(col->name) <= 12)
  702. xfprintf(fp, "%-12s ", col->name == NULL ? "" : col->name);
  703. else
  704. xfprintf(fp, "%s\n%20s", col->name, "");
  705. xfprintf(fp, "%3s", "");
  706. xfprintf(fp, "%13.6g ",
  707. fabs(col->pval) <= 1e-9 ? 0.0 : col->pval);
  708. if (col->type == GLP_LO || col->type == GLP_DB ||
  709. col->type == GLP_FX)
  710. xfprintf(fp, "%13.6g ", col->lb);
  711. else
  712. xfprintf(fp, "%13s ", "");
  713. if (col->type == GLP_UP || col->type == GLP_DB)
  714. xfprintf(fp, "%13.6g ", col->ub);
  715. else
  716. xfprintf(fp, "%13s ", col->type == GLP_FX ? "=" : "");
  717. if (fabs(col->dval) <= 1e-9)
  718. xfprintf(fp, "%13s", "< eps");
  719. else
  720. xfprintf(fp, "%13.6g ", col->dval);
  721. xfprintf(fp, "\n");
  722. }
  723. xfprintf(fp, "\n");
  724. xfprintf(fp, "Karush-Kuhn-Tucker optimality conditions:\n");
  725. xfprintf(fp, "\n");
  726. _glp_check_kkt(P, GLP_IPT, GLP_KKT_PE, &ae_max, &ae_ind, &re_max,
  727. &re_ind);
  728. xfprintf(fp, "KKT.PE: max.abs.err = %.2e on row %d\n",
  729. ae_max, ae_ind);
  730. xfprintf(fp, " max.rel.err = %.2e on row %d\n",
  731. re_max, re_ind);
  732. xfprintf(fp, "%8s%s\n", "",
  733. re_max <= 1e-9 ? "High quality" :
  734. re_max <= 1e-6 ? "Medium quality" :
  735. re_max <= 1e-3 ? "Low quality" : "PRIMAL SOLUTION IS WRONG");
  736. xfprintf(fp, "\n");
  737. _glp_check_kkt(P, GLP_IPT, GLP_KKT_PB, &ae_max, &ae_ind, &re_max,
  738. &re_ind);
  739. xfprintf(fp, "KKT.PB: max.abs.err = %.2e on %s %d\n",
  740. ae_max, ae_ind <= P->m ? "row" : "column",
  741. ae_ind <= P->m ? ae_ind : ae_ind - P->m);
  742. xfprintf(fp, " max.rel.err = %.2e on %s %d\n",
  743. re_max, re_ind <= P->m ? "row" : "column",
  744. re_ind <= P->m ? re_ind : re_ind - P->m);
  745. xfprintf(fp, "%8s%s\n", "",
  746. re_max <= 1e-9 ? "High quality" :
  747. re_max <= 1e-6 ? "Medium quality" :
  748. re_max <= 1e-3 ? "Low quality" : "PRIMAL SOLUTION IS INFEASIBL"
  749. "E");
  750. xfprintf(fp, "\n");
  751. _glp_check_kkt(P, GLP_IPT, GLP_KKT_DE, &ae_max, &ae_ind, &re_max,
  752. &re_ind);
  753. xfprintf(fp, "KKT.DE: max.abs.err = %.2e on column %d\n",
  754. ae_max, ae_ind == 0 ? 0 : ae_ind - P->m);
  755. xfprintf(fp, " max.rel.err = %.2e on column %d\n",
  756. re_max, re_ind == 0 ? 0 : re_ind - P->m);
  757. xfprintf(fp, "%8s%s\n", "",
  758. re_max <= 1e-9 ? "High quality" :
  759. re_max <= 1e-6 ? "Medium quality" :
  760. re_max <= 1e-3 ? "Low quality" : "DUAL SOLUTION IS WRONG");
  761. xfprintf(fp, "\n");
  762. _glp_check_kkt(P, GLP_IPT, GLP_KKT_DB, &ae_max, &ae_ind, &re_max,
  763. &re_ind);
  764. xfprintf(fp, "KKT.DB: max.abs.err = %.2e on %s %d\n",
  765. ae_max, ae_ind <= P->m ? "row" : "column",
  766. ae_ind <= P->m ? ae_ind : ae_ind - P->m);
  767. xfprintf(fp, " max.rel.err = %.2e on %s %d\n",
  768. re_max, re_ind <= P->m ? "row" : "column",
  769. re_ind <= P->m ? re_ind : re_ind - P->m);
  770. xfprintf(fp, "%8s%s\n", "",
  771. re_max <= 1e-9 ? "High quality" :
  772. re_max <= 1e-6 ? "Medium quality" :
  773. re_max <= 1e-3 ? "Low quality" : "DUAL SOLUTION IS INFEASIBLE")
  774. ;
  775. xfprintf(fp, "\n");
  776. xfprintf(fp, "End of output\n");
  777. xfflush(fp);
  778. if (xferror(fp))
  779. { xprintf("Write error on `%s' - %s\n", fname, xerrmsg());
  780. ret = 1;
  781. goto done;
  782. }
  783. ret = 0;
  784. done: if (fp != NULL) xfclose(fp);
  785. return ret;
  786. }
  787. /***********************************************************************
  788. * NAME
  789. *
  790. * glp_read_ipt - read interior-point solution from text file
  791. *
  792. * SYNOPSIS
  793. *
  794. * int glp_read_ipt(glp_prob *lp, const char *fname);
  795. *
  796. * DESCRIPTION
  797. *
  798. * The routine glp_read_ipt reads interior-point solution from a text
  799. * file whose name is specified by the parameter fname into the problem
  800. * object.
  801. *
  802. * For the file format see description of the routine glp_write_ipt.
  803. *
  804. * RETURNS
  805. *
  806. * On success the routine returns zero, otherwise non-zero. */
  807. int glp_read_ipt(glp_prob *lp, const char *fname)
  808. { glp_data *data;
  809. jmp_buf jump;
  810. int i, j, k, ret = 0;
  811. xprintf("Reading interior-point solution from `%s'...\n", fname);
  812. data = glp_sdf_open_file(fname);
  813. if (data == NULL)
  814. { ret = 1;
  815. goto done;
  816. }
  817. if (setjmp(jump))
  818. { ret = 1;
  819. goto done;
  820. }
  821. glp_sdf_set_jump(data, jump);
  822. /* number of rows, number of columns */
  823. k = glp_sdf_read_int(data);
  824. if (k != lp->m)
  825. glp_sdf_error(data, "wrong number of rows\n");
  826. k = glp_sdf_read_int(data);
  827. if (k != lp->n)
  828. glp_sdf_error(data, "wrong number of columns\n");
  829. /* solution status, objective value */
  830. k = glp_sdf_read_int(data);
  831. if (!(k == GLP_UNDEF || k == GLP_OPT))
  832. glp_sdf_error(data, "invalid solution status\n");
  833. lp->ipt_stat = k;
  834. lp->ipt_obj = glp_sdf_read_num(data);
  835. /* rows (auxiliary variables) */
  836. for (i = 1; i <= lp->m; i++)
  837. { GLPROW *row = lp->row[i];
  838. /* primal value, dual value */
  839. row->pval = glp_sdf_read_num(data);
  840. row->dval = glp_sdf_read_num(data);
  841. }
  842. /* columns (structural variables) */
  843. for (j = 1; j <= lp->n; j++)
  844. { GLPCOL *col = lp->col[j];
  845. /* primal value, dual value */
  846. col->pval = glp_sdf_read_num(data);
  847. col->dval = glp_sdf_read_num(data);
  848. }
  849. xprintf("%d lines were read\n", glp_sdf_line(data));
  850. done: if (ret) lp->ipt_stat = GLP_UNDEF;
  851. if (data != NULL) glp_sdf_close_file(data);
  852. return ret;
  853. }
  854. /***********************************************************************
  855. * NAME
  856. *
  857. * glp_write_ipt - write interior-point solution to text file
  858. *
  859. * SYNOPSIS
  860. *
  861. * int glp_write_ipt(glp_prob *lp, const char *fname);
  862. *
  863. * DESCRIPTION
  864. *
  865. * The routine glp_write_ipt writes the current interior-point solution
  866. * to a text file whose name is specified by the parameter fname. This
  867. * file can be read back with the routine glp_read_ipt.
  868. *
  869. * RETURNS
  870. *
  871. * On success the routine returns zero, otherwise non-zero.
  872. *
  873. * FILE FORMAT
  874. *
  875. * The file created by the routine glp_write_ipt is a plain text file,
  876. * which contains the following information:
  877. *
  878. * m n
  879. * stat obj_val
  880. * r_prim[1] r_dual[1]
  881. * . . .
  882. * r_prim[m] r_dual[m]
  883. * c_prim[1] c_dual[1]
  884. * . . .
  885. * c_prim[n] c_dual[n]
  886. *
  887. * where:
  888. * m is the number of rows (auxiliary variables);
  889. * n is the number of columns (structural variables);
  890. * stat is the solution status (GLP_UNDEF = 1 or GLP_OPT = 5);
  891. * obj_val is the objective value;
  892. * r_prim[i], i = 1,...,m, is the primal value of i-th row;
  893. * r_dual[i], i = 1,...,m, is the dual value of i-th row;
  894. * c_prim[j], j = 1,...,n, is the primal value of j-th column;
  895. * c_dual[j], j = 1,...,n, is the dual value of j-th column. */
  896. int glp_write_ipt(glp_prob *lp, const char *fname)
  897. { XFILE *fp;
  898. int i, j, ret = 0;
  899. xprintf("Writing interior-point solution to `%s'...\n", fname);
  900. fp = xfopen(fname, "w");
  901. if (fp == NULL)
  902. { xprintf("Unable to create `%s' - %s\n", fname, xerrmsg());
  903. ret = 1;
  904. goto done;
  905. }
  906. /* number of rows, number of columns */
  907. xfprintf(fp, "%d %d\n", lp->m, lp->n);
  908. /* solution status, objective value */
  909. xfprintf(fp, "%d %.*g\n", lp->ipt_stat, DBL_DIG, lp->ipt_obj);
  910. /* rows (auxiliary variables) */
  911. for (i = 1; i <= lp->m; i++)
  912. { GLPROW *row = lp->row[i];
  913. /* primal value, dual value */
  914. xfprintf(fp, "%.*g %.*g\n", DBL_DIG, row->pval, DBL_DIG,
  915. row->dval);
  916. }
  917. /* columns (structural variables) */
  918. for (j = 1; j <= lp->n; j++)
  919. { GLPCOL *col = lp->col[j];
  920. /* primal value, dual value */
  921. xfprintf(fp, "%.*g %.*g\n", DBL_DIG, col->pval, DBL_DIG,
  922. col->dval);
  923. }
  924. xfflush(fp);
  925. if (xferror(fp))
  926. { xprintf("Write error on `%s' - %s\n", fname, xerrmsg());
  927. ret = 1;
  928. goto done;
  929. }
  930. xprintf("%d lines were written\n", 2 + lp->m + lp->n);
  931. done: if (fp != NULL) xfclose(fp);
  932. return ret;
  933. }
  934. /**********************************************************************/
  935. int glp_print_mip(glp_prob *P, const char *fname)
  936. { /* write MIP solution in printable format */
  937. XFILE *fp;
  938. GLPROW *row;
  939. GLPCOL *col;
  940. int i, j, t, ae_ind, re_ind, ret;
  941. double ae_max, re_max;
  942. xprintf("Writing MIP solution to `%s'...\n", fname);
  943. fp = xfopen(fname, "w");
  944. if (fp == NULL)
  945. { xprintf("Unable to create `%s' - %s\n", fname, xerrmsg());
  946. ret = 1;
  947. goto done;
  948. }
  949. xfprintf(fp, "%-12s%s\n", "Problem:",
  950. P->name == NULL ? "" : P->name);
  951. xfprintf(fp, "%-12s%d\n", "Rows:", P->m);
  952. xfprintf(fp, "%-12s%d (%d integer, %d binary)\n", "Columns:",
  953. P->n, glp_get_num_int(P), glp_get_num_bin(P));
  954. xfprintf(fp, "%-12s%d\n", "Non-zeros:", P->nnz);
  955. t = glp_mip_status(P);
  956. xfprintf(fp, "%-12s%s\n", "Status:",
  957. t == GLP_OPT ? "INTEGER OPTIMAL" :
  958. t == GLP_FEAS ? "INTEGER NON-OPTIMAL" :
  959. t == GLP_NOFEAS ? "INTEGER EMPTY" :
  960. t == GLP_UNDEF ? "INTEGER UNDEFINED" : "???");
  961. xfprintf(fp, "%-12s%s%s%.10g (%s)\n", "Objective:",
  962. P->obj == NULL ? "" : P->obj,
  963. P->obj == NULL ? "" : " = ", P->mip_obj,
  964. P->dir == GLP_MIN ? "MINimum" :
  965. P->dir == GLP_MAX ? "MAXimum" : "???");
  966. xfprintf(fp, "\n");
  967. xfprintf(fp, " No. Row name Activity Lower bound "
  968. " Upper bound\n");
  969. xfprintf(fp, "------ ------------ ------------- ------------- "
  970. "-------------\n");
  971. for (i = 1; i <= P->m; i++)
  972. { row = P->row[i];
  973. xfprintf(fp, "%6d ", i);
  974. if (row->name == NULL || strlen(row->name) <= 12)
  975. xfprintf(fp, "%-12s ", row->name == NULL ? "" : row->name);
  976. else
  977. xfprintf(fp, "%s\n%20s", row->name, "");
  978. xfprintf(fp, "%3s", "");
  979. xfprintf(fp, "%13.6g ",
  980. fabs(row->mipx) <= 1e-9 ? 0.0 : row->mipx);
  981. if (row->type == GLP_LO || row->type == GLP_DB ||
  982. row->type == GLP_FX)
  983. xfprintf(fp, "%13.6g ", row->lb);
  984. else
  985. xfprintf(fp, "%13s ", "");
  986. if (row->type == GLP_UP || row->type == GLP_DB)
  987. xfprintf(fp, "%13.6g ", row->ub);
  988. else
  989. xfprintf(fp, "%13s ", row->type == GLP_FX ? "=" : "");
  990. xfprintf(fp, "\n");
  991. }
  992. xfprintf(fp, "\n");
  993. xfprintf(fp, " No. Column name Activity Lower bound "
  994. " Upper bound\n");
  995. xfprintf(fp, "------ ------------ ------------- ------------- "
  996. "-------------\n");
  997. for (j = 1; j <= P->n; j++)
  998. { col = P->col[j];
  999. xfprintf(fp, "%6d ", j);
  1000. if (col->name == NULL || strlen(col->name) <= 12)
  1001. xfprintf(fp, "%-12s ", col->name == NULL ? "" : col->name);
  1002. else
  1003. xfprintf(fp, "%s\n%20s", col->name, "");
  1004. xfprintf(fp, "%s ",
  1005. col->kind == GLP_CV ? " " :
  1006. col->kind == GLP_IV ? "*" : "?");
  1007. xfprintf(fp, "%13.6g ",
  1008. fabs(col->mipx) <= 1e-9 ? 0.0 : col->mipx);
  1009. if (col->type == GLP_LO || col->type == GLP_DB ||
  1010. col->type == GLP_FX)
  1011. xfprintf(fp, "%13.6g ", col->lb);
  1012. else
  1013. xfprintf(fp, "%13s ", "");
  1014. if (col->type == GLP_UP || col->type == GLP_DB)
  1015. xfprintf(fp, "%13.6g ", col->ub);
  1016. else
  1017. xfprintf(fp, "%13s ", col->type == GLP_FX ? "=" : "");
  1018. xfprintf(fp, "\n");
  1019. }
  1020. xfprintf(fp, "\n");
  1021. xfprintf(fp, "Integer feasibility conditions:\n");
  1022. xfprintf(fp, "\n");
  1023. _glp_check_kkt(P, GLP_MIP, GLP_KKT_PE, &ae_max, &ae_ind, &re_max,
  1024. &re_ind);
  1025. xfprintf(fp, "KKT.PE: max.abs.err = %.2e on row %d\n",
  1026. ae_max, ae_ind);
  1027. xfprintf(fp, " max.rel.err = %.2e on row %d\n",
  1028. re_max, re_ind);
  1029. xfprintf(fp, "%8s%s\n", "",
  1030. re_max <= 1e-9 ? "High quality" :
  1031. re_max <= 1e-6 ? "Medium quality" :
  1032. re_max <= 1e-3 ? "Low quality" : "SOLUTION IS WRONG");
  1033. xfprintf(fp, "\n");
  1034. _glp_check_kkt(P, GLP_MIP, GLP_KKT_PB, &ae_max, &ae_ind, &re_max,
  1035. &re_ind);
  1036. xfprintf(fp, "KKT.PB: max.abs.err = %.2e on %s %d\n",
  1037. ae_max, ae_ind <= P->m ? "row" : "column",
  1038. ae_ind <= P->m ? ae_ind : ae_ind - P->m);
  1039. xfprintf(fp, " max.rel.err = %.2e on %s %d\n",
  1040. re_max, re_ind <= P->m ? "row" : "column",
  1041. re_ind <= P->m ? re_ind : re_ind - P->m);
  1042. xfprintf(fp, "%8s%s\n", "",
  1043. re_max <= 1e-9 ? "High quality" :
  1044. re_max <= 1e-6 ? "Medium quality" :
  1045. re_max <= 1e-3 ? "Low quality" : "SOLUTION IS INFEASIBLE");
  1046. xfprintf(fp, "\n");
  1047. xfprintf(fp, "End of output\n");
  1048. xfflush(fp);
  1049. if (xferror(fp))
  1050. { xprintf("Write error on `%s' - %s\n", fname, xerrmsg());
  1051. ret = 1;
  1052. goto done;
  1053. }
  1054. ret = 0;
  1055. done: if (fp != NULL) xfclose(fp);
  1056. return ret;
  1057. }
  1058. /***********************************************************************
  1059. * NAME
  1060. *
  1061. * glp_read_mip - read MIP solution from text file
  1062. *
  1063. * SYNOPSIS
  1064. *
  1065. * int glp_read_mip(glp_prob *mip, const char *fname);
  1066. *
  1067. * DESCRIPTION
  1068. *
  1069. * The routine glp_read_mip reads MIP solution from a text file whose
  1070. * name is specified by the parameter fname into the problem object.
  1071. *
  1072. * For the file format see description of the routine glp_write_mip.
  1073. *
  1074. * RETURNS
  1075. *
  1076. * On success the routine returns zero, otherwise non-zero. */
  1077. int glp_read_mip(glp_prob *mip, const char *fname)
  1078. { glp_data *data;
  1079. jmp_buf jump;
  1080. int i, j, k, ret = 0;
  1081. xprintf("Reading MIP solution from `%s'...\n", fname);
  1082. data = glp_sdf_open_file(fname);
  1083. if (data == NULL)
  1084. { ret = 1;
  1085. goto done;
  1086. }
  1087. if (setjmp(jump))
  1088. { ret = 1;
  1089. goto done;
  1090. }
  1091. glp_sdf_set_jump(data, jump);
  1092. /* number of rows, number of columns */
  1093. k = glp_sdf_read_int(data);
  1094. if (k != mip->m)
  1095. glp_sdf_error(data, "wrong number of rows\n");
  1096. k = glp_sdf_read_int(data);
  1097. if (k != mip->n)
  1098. glp_sdf_error(data, "wrong number of columns\n");
  1099. /* solution status, objective value */
  1100. k = glp_sdf_read_int(data);
  1101. if (!(k == GLP_UNDEF || k == GLP_OPT || k == GLP_FEAS ||
  1102. k == GLP_NOFEAS))
  1103. glp_sdf_error(data, "invalid solution status\n");
  1104. mip->mip_stat = k;
  1105. mip->mip_obj = glp_sdf_read_num(data);
  1106. /* rows (auxiliary variables) */
  1107. for (i = 1; i <= mip->m; i++)
  1108. { GLPROW *row = mip->row[i];
  1109. row->mipx = glp_sdf_read_num(data);
  1110. }
  1111. /* columns (structural variables) */
  1112. for (j = 1; j <= mip->n; j++)
  1113. { GLPCOL *col = mip->col[j];
  1114. col->mipx = glp_sdf_read_num(data);
  1115. if (col->kind == GLP_IV && col->mipx != floor(col->mipx))
  1116. glp_sdf_error(data, "non-integer column value");
  1117. }
  1118. xprintf("%d lines were read\n", glp_sdf_line(data));
  1119. done: if (ret) mip->mip_stat = GLP_UNDEF;
  1120. if (data != NULL) glp_sdf_close_file(data);
  1121. return ret;
  1122. }
  1123. /***********************************************************************
  1124. * NAME
  1125. *
  1126. * glp_write_mip - write MIP solution to text file
  1127. *
  1128. * SYNOPSIS
  1129. *
  1130. * int glp_write_mip(glp_prob *mip, const char *fname);
  1131. *
  1132. * DESCRIPTION
  1133. *
  1134. * The routine glp_write_mip writes the current MIP solution to a text
  1135. * file whose name is specified by the parameter fname. This file can
  1136. * be read back with the routine glp_read_mip.
  1137. *
  1138. * RETURNS
  1139. *
  1140. * On success the routine returns zero, otherwise non-zero.
  1141. *
  1142. * FILE FORMAT
  1143. *
  1144. * The file created by the routine glp_write_sol is a plain text file,
  1145. * which contains the following information:
  1146. *
  1147. * m n
  1148. * stat obj_val
  1149. * r_val[1]
  1150. * . . .
  1151. * r_val[m]
  1152. * c_val[1]
  1153. * . . .
  1154. * c_val[n]
  1155. *
  1156. * where:
  1157. * m is the number of rows (auxiliary variables);
  1158. * n is the number of columns (structural variables);
  1159. * stat is the solution status (GLP_UNDEF = 1, GLP_FEAS = 2,
  1160. * GLP_NOFEAS = 4, or GLP_OPT = 5);
  1161. * obj_val is the objective value;
  1162. * r_val[i], i = 1,...,m, is the value of i-th row;
  1163. * c_val[j], j = 1,...,n, is the value of j-th column. */
  1164. int glp_write_mip(glp_prob *mip, const char *fname)
  1165. { XFILE *fp;
  1166. int i, j, ret = 0;
  1167. xprintf("Writing MIP solution to `%s'...\n", fname);
  1168. fp = xfopen(fname, "w");
  1169. if (fp == NULL)
  1170. { xprintf("Unable to create `%s' - %s\n", fname, xerrmsg());
  1171. ret = 1;
  1172. goto done;
  1173. }
  1174. /* number of rows, number of columns */
  1175. xfprintf(fp, "%d %d\n", mip->m, mip->n);
  1176. /* solution status, objective value */
  1177. xfprintf(fp, "%d %.*g\n", mip->mip_stat, DBL_DIG, mip->mip_obj);
  1178. /* rows (auxiliary variables) */
  1179. for (i = 1; i <= mip->m; i++)
  1180. xfprintf(fp, "%.*g\n", DBL_DIG, mip->row[i]->mipx);
  1181. /* columns (structural variables) */
  1182. for (j = 1; j <= mip->n; j++)
  1183. xfprintf(fp, "%.*g\n", DBL_DIG, mip->col[j]->mipx);
  1184. xfflush(fp);
  1185. if (xferror(fp))
  1186. { xprintf("Write error on `%s' - %s\n", fname, xerrmsg());
  1187. ret = 1;
  1188. goto done;
  1189. }
  1190. xprintf("%d lines were written\n", 2 + mip->m + mip->n);
  1191. done: if (fp != NULL) xfclose(fp);
  1192. return ret;
  1193. }
  1194. /* eof */