alpar@1: /* glpapi11.c (utility routines) */ alpar@1: alpar@1: /*********************************************************************** alpar@1: * This code is part of GLPK (GNU Linear Programming Kit). alpar@1: * alpar@1: * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, alpar@1: * 2009, 2010 Andrew Makhorin, Department for Applied Informatics, alpar@1: * Moscow Aviation Institute, Moscow, Russia. All rights reserved. alpar@1: * E-mail: . alpar@1: * alpar@1: * GLPK is free software: you can redistribute it and/or modify it alpar@1: * under the terms of the GNU General Public License as published by alpar@1: * the Free Software Foundation, either version 3 of the License, or alpar@1: * (at your option) any later version. alpar@1: * alpar@1: * GLPK is distributed in the hope that it will be useful, but WITHOUT alpar@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY alpar@1: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public alpar@1: * License for more details. alpar@1: * alpar@1: * You should have received a copy of the GNU General Public License alpar@1: * along with GLPK. If not, see . alpar@1: ***********************************************************************/ alpar@1: alpar@1: #include "glpapi.h" alpar@1: alpar@1: int glp_print_sol(glp_prob *P, const char *fname) alpar@1: { /* write basic solution in printable format */ alpar@1: XFILE *fp; alpar@1: GLPROW *row; alpar@1: GLPCOL *col; alpar@1: int i, j, t, ae_ind, re_ind, ret; alpar@1: double ae_max, re_max; alpar@1: xprintf("Writing basic solution to `%s'...\n", fname); alpar@1: fp = xfopen(fname, "w"); alpar@1: if (fp == NULL) alpar@1: { xprintf("Unable to create `%s' - %s\n", fname, xerrmsg()); alpar@1: ret = 1; alpar@1: goto done; alpar@1: } alpar@1: xfprintf(fp, "%-12s%s\n", "Problem:", alpar@1: P->name == NULL ? "" : P->name); alpar@1: xfprintf(fp, "%-12s%d\n", "Rows:", P->m); alpar@1: xfprintf(fp, "%-12s%d\n", "Columns:", P->n); alpar@1: xfprintf(fp, "%-12s%d\n", "Non-zeros:", P->nnz); alpar@1: t = glp_get_status(P); alpar@1: xfprintf(fp, "%-12s%s\n", "Status:", alpar@1: t == GLP_OPT ? "OPTIMAL" : alpar@1: t == GLP_FEAS ? "FEASIBLE" : alpar@1: t == GLP_INFEAS ? "INFEASIBLE (INTERMEDIATE)" : alpar@1: t == GLP_NOFEAS ? "INFEASIBLE (FINAL)" : alpar@1: t == GLP_UNBND ? "UNBOUNDED" : alpar@1: t == GLP_UNDEF ? "UNDEFINED" : "???"); alpar@1: xfprintf(fp, "%-12s%s%s%.10g (%s)\n", "Objective:", alpar@1: P->obj == NULL ? "" : P->obj, alpar@1: P->obj == NULL ? "" : " = ", P->obj_val, alpar@1: P->dir == GLP_MIN ? "MINimum" : alpar@1: P->dir == GLP_MAX ? "MAXimum" : "???"); alpar@1: xfprintf(fp, "\n"); alpar@1: xfprintf(fp, " No. Row name St Activity Lower bound " alpar@1: " Upper bound Marginal\n"); alpar@1: xfprintf(fp, "------ ------------ -- ------------- ------------- " alpar@1: "------------- -------------\n"); alpar@1: for (i = 1; i <= P->m; i++) alpar@1: { row = P->row[i]; alpar@1: xfprintf(fp, "%6d ", i); alpar@1: if (row->name == NULL || strlen(row->name) <= 12) alpar@1: xfprintf(fp, "%-12s ", row->name == NULL ? "" : row->name); alpar@1: else alpar@1: xfprintf(fp, "%s\n%20s", row->name, ""); alpar@1: xfprintf(fp, "%s ", alpar@1: row->stat == GLP_BS ? "B " : alpar@1: row->stat == GLP_NL ? "NL" : alpar@1: row->stat == GLP_NU ? "NU" : alpar@1: row->stat == GLP_NF ? "NF" : alpar@1: row->stat == GLP_NS ? "NS" : "??"); alpar@1: xfprintf(fp, "%13.6g ", alpar@1: fabs(row->prim) <= 1e-9 ? 0.0 : row->prim); alpar@1: if (row->type == GLP_LO || row->type == GLP_DB || alpar@1: row->type == GLP_FX) alpar@1: xfprintf(fp, "%13.6g ", row->lb); alpar@1: else alpar@1: xfprintf(fp, "%13s ", ""); alpar@1: if (row->type == GLP_UP || row->type == GLP_DB) alpar@1: xfprintf(fp, "%13.6g ", row->ub); alpar@1: else alpar@1: xfprintf(fp, "%13s ", row->type == GLP_FX ? "=" : ""); alpar@1: if (row->stat != GLP_BS) alpar@1: { if (fabs(row->dual) <= 1e-9) alpar@1: xfprintf(fp, "%13s", "< eps"); alpar@1: else alpar@1: xfprintf(fp, "%13.6g ", row->dual); alpar@1: } alpar@1: xfprintf(fp, "\n"); alpar@1: } alpar@1: xfprintf(fp, "\n"); alpar@1: xfprintf(fp, " No. Column name St Activity Lower bound " alpar@1: " Upper bound Marginal\n"); alpar@1: xfprintf(fp, "------ ------------ -- ------------- ------------- " alpar@1: "------------- -------------\n"); alpar@1: for (j = 1; j <= P->n; j++) alpar@1: { col = P->col[j]; alpar@1: xfprintf(fp, "%6d ", j); alpar@1: if (col->name == NULL || strlen(col->name) <= 12) alpar@1: xfprintf(fp, "%-12s ", col->name == NULL ? "" : col->name); alpar@1: else alpar@1: xfprintf(fp, "%s\n%20s", col->name, ""); alpar@1: xfprintf(fp, "%s ", alpar@1: col->stat == GLP_BS ? "B " : alpar@1: col->stat == GLP_NL ? "NL" : alpar@1: col->stat == GLP_NU ? "NU" : alpar@1: col->stat == GLP_NF ? "NF" : alpar@1: col->stat == GLP_NS ? "NS" : "??"); alpar@1: xfprintf(fp, "%13.6g ", alpar@1: fabs(col->prim) <= 1e-9 ? 0.0 : col->prim); alpar@1: if (col->type == GLP_LO || col->type == GLP_DB || alpar@1: col->type == GLP_FX) alpar@1: xfprintf(fp, "%13.6g ", col->lb); alpar@1: else alpar@1: xfprintf(fp, "%13s ", ""); alpar@1: if (col->type == GLP_UP || col->type == GLP_DB) alpar@1: xfprintf(fp, "%13.6g ", col->ub); alpar@1: else alpar@1: xfprintf(fp, "%13s ", col->type == GLP_FX ? "=" : ""); alpar@1: if (col->stat != GLP_BS) alpar@1: { if (fabs(col->dual) <= 1e-9) alpar@1: xfprintf(fp, "%13s", "< eps"); alpar@1: else alpar@1: xfprintf(fp, "%13.6g ", col->dual); alpar@1: } alpar@1: xfprintf(fp, "\n"); alpar@1: } alpar@1: xfprintf(fp, "\n"); alpar@1: xfprintf(fp, "Karush-Kuhn-Tucker optimality conditions:\n"); alpar@1: xfprintf(fp, "\n"); alpar@1: _glp_check_kkt(P, GLP_SOL, GLP_KKT_PE, &ae_max, &ae_ind, &re_max, alpar@1: &re_ind); alpar@1: xfprintf(fp, "KKT.PE: max.abs.err = %.2e on row %d\n", alpar@1: ae_max, ae_ind); alpar@1: xfprintf(fp, " max.rel.err = %.2e on row %d\n", alpar@1: re_max, re_ind); alpar@1: xfprintf(fp, "%8s%s\n", "", alpar@1: re_max <= 1e-9 ? "High quality" : alpar@1: re_max <= 1e-6 ? "Medium quality" : alpar@1: re_max <= 1e-3 ? "Low quality" : "PRIMAL SOLUTION IS WRONG"); alpar@1: xfprintf(fp, "\n"); alpar@1: _glp_check_kkt(P, GLP_SOL, GLP_KKT_PB, &ae_max, &ae_ind, &re_max, alpar@1: &re_ind); alpar@1: xfprintf(fp, "KKT.PB: max.abs.err = %.2e on %s %d\n", alpar@1: ae_max, ae_ind <= P->m ? "row" : "column", alpar@1: ae_ind <= P->m ? ae_ind : ae_ind - P->m); alpar@1: xfprintf(fp, " max.rel.err = %.2e on %s %d\n", alpar@1: re_max, re_ind <= P->m ? "row" : "column", alpar@1: re_ind <= P->m ? re_ind : re_ind - P->m); alpar@1: xfprintf(fp, "%8s%s\n", "", alpar@1: re_max <= 1e-9 ? "High quality" : alpar@1: re_max <= 1e-6 ? "Medium quality" : alpar@1: re_max <= 1e-3 ? "Low quality" : "PRIMAL SOLUTION IS INFEASIBL" alpar@1: "E"); alpar@1: xfprintf(fp, "\n"); alpar@1: _glp_check_kkt(P, GLP_SOL, GLP_KKT_DE, &ae_max, &ae_ind, &re_max, alpar@1: &re_ind); alpar@1: xfprintf(fp, "KKT.DE: max.abs.err = %.2e on column %d\n", alpar@1: ae_max, ae_ind == 0 ? 0 : ae_ind - P->m); alpar@1: xfprintf(fp, " max.rel.err = %.2e on column %d\n", alpar@1: re_max, re_ind == 0 ? 0 : re_ind - P->m); alpar@1: xfprintf(fp, "%8s%s\n", "", alpar@1: re_max <= 1e-9 ? "High quality" : alpar@1: re_max <= 1e-6 ? "Medium quality" : alpar@1: re_max <= 1e-3 ? "Low quality" : "DUAL SOLUTION IS WRONG"); alpar@1: xfprintf(fp, "\n"); alpar@1: _glp_check_kkt(P, GLP_SOL, GLP_KKT_DB, &ae_max, &ae_ind, &re_max, alpar@1: &re_ind); alpar@1: xfprintf(fp, "KKT.DB: max.abs.err = %.2e on %s %d\n", alpar@1: ae_max, ae_ind <= P->m ? "row" : "column", alpar@1: ae_ind <= P->m ? ae_ind : ae_ind - P->m); alpar@1: xfprintf(fp, " max.rel.err = %.2e on %s %d\n", alpar@1: re_max, re_ind <= P->m ? "row" : "column", alpar@1: re_ind <= P->m ? re_ind : re_ind - P->m); alpar@1: xfprintf(fp, "%8s%s\n", "", alpar@1: re_max <= 1e-9 ? "High quality" : alpar@1: re_max <= 1e-6 ? "Medium quality" : alpar@1: re_max <= 1e-3 ? "Low quality" : "DUAL SOLUTION IS INFEASIBLE") alpar@1: ; alpar@1: xfprintf(fp, "\n"); alpar@1: xfprintf(fp, "End of output\n"); alpar@1: xfflush(fp); alpar@1: if (xferror(fp)) alpar@1: { xprintf("Write error on `%s' - %s\n", fname, xerrmsg()); alpar@1: ret = 1; alpar@1: goto done; alpar@1: } alpar@1: ret = 0; alpar@1: done: if (fp != NULL) xfclose(fp); alpar@1: return ret; alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * glp_read_sol - read basic solution from text file alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * int glp_read_sol(glp_prob *lp, const char *fname); alpar@1: * alpar@1: * DESCRIPTION alpar@1: * alpar@1: * The routine glp_read_sol reads basic solution from a text file whose alpar@1: * name is specified by the parameter fname into the problem object. alpar@1: * alpar@1: * For the file format see description of the routine glp_write_sol. alpar@1: * alpar@1: * RETURNS alpar@1: * alpar@1: * On success the routine returns zero, otherwise non-zero. */ alpar@1: alpar@1: int glp_read_sol(glp_prob *lp, const char *fname) alpar@1: { glp_data *data; alpar@1: jmp_buf jump; alpar@1: int i, j, k, ret = 0; alpar@1: xprintf("Reading basic solution from `%s'...\n", fname); alpar@1: data = glp_sdf_open_file(fname); alpar@1: if (data == NULL) alpar@1: { ret = 1; alpar@1: goto done; alpar@1: } alpar@1: if (setjmp(jump)) alpar@1: { ret = 1; alpar@1: goto done; alpar@1: } alpar@1: glp_sdf_set_jump(data, jump); alpar@1: /* number of rows, number of columns */ alpar@1: k = glp_sdf_read_int(data); alpar@1: if (k != lp->m) alpar@1: glp_sdf_error(data, "wrong number of rows\n"); alpar@1: k = glp_sdf_read_int(data); alpar@1: if (k != lp->n) alpar@1: glp_sdf_error(data, "wrong number of columns\n"); alpar@1: /* primal status, dual status, objective value */ alpar@1: k = glp_sdf_read_int(data); alpar@1: if (!(k == GLP_UNDEF || k == GLP_FEAS || k == GLP_INFEAS || alpar@1: k == GLP_NOFEAS)) alpar@1: glp_sdf_error(data, "invalid primal status\n"); alpar@1: lp->pbs_stat = k; alpar@1: k = glp_sdf_read_int(data); alpar@1: if (!(k == GLP_UNDEF || k == GLP_FEAS || k == GLP_INFEAS || alpar@1: k == GLP_NOFEAS)) alpar@1: glp_sdf_error(data, "invalid dual status\n"); alpar@1: lp->dbs_stat = k; alpar@1: lp->obj_val = glp_sdf_read_num(data); alpar@1: /* rows (auxiliary variables) */ alpar@1: for (i = 1; i <= lp->m; i++) alpar@1: { GLPROW *row = lp->row[i]; alpar@1: /* status, primal value, dual value */ alpar@1: k = glp_sdf_read_int(data); alpar@1: if (!(k == GLP_BS || k == GLP_NL || k == GLP_NU || alpar@1: k == GLP_NF || k == GLP_NS)) alpar@1: glp_sdf_error(data, "invalid row status\n"); alpar@1: glp_set_row_stat(lp, i, k); alpar@1: row->prim = glp_sdf_read_num(data); alpar@1: row->dual = glp_sdf_read_num(data); alpar@1: } alpar@1: /* columns (structural variables) */ alpar@1: for (j = 1; j <= lp->n; j++) alpar@1: { GLPCOL *col = lp->col[j]; alpar@1: /* status, primal value, dual value */ alpar@1: k = glp_sdf_read_int(data); alpar@1: if (!(k == GLP_BS || k == GLP_NL || k == GLP_NU || alpar@1: k == GLP_NF || k == GLP_NS)) alpar@1: glp_sdf_error(data, "invalid column status\n"); alpar@1: glp_set_col_stat(lp, j, k); alpar@1: col->prim = glp_sdf_read_num(data); alpar@1: col->dual = glp_sdf_read_num(data); alpar@1: } alpar@1: xprintf("%d lines were read\n", glp_sdf_line(data)); alpar@1: done: if (ret) lp->pbs_stat = lp->dbs_stat = GLP_UNDEF; alpar@1: if (data != NULL) glp_sdf_close_file(data); alpar@1: return ret; alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * glp_write_sol - write basic solution to text file alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * int glp_write_sol(glp_prob *lp, const char *fname); alpar@1: * alpar@1: * DESCRIPTION alpar@1: * alpar@1: * The routine glp_write_sol writes the current basic solution to a alpar@1: * text file whose name is specified by the parameter fname. This file alpar@1: * can be read back with the routine glp_read_sol. alpar@1: * alpar@1: * RETURNS alpar@1: * alpar@1: * On success the routine returns zero, otherwise non-zero. alpar@1: * alpar@1: * FILE FORMAT alpar@1: * alpar@1: * The file created by the routine glp_write_sol is a plain text file, alpar@1: * which contains the following information: alpar@1: * alpar@1: * m n alpar@1: * p_stat d_stat obj_val alpar@1: * r_stat[1] r_prim[1] r_dual[1] alpar@1: * . . . alpar@1: * r_stat[m] r_prim[m] r_dual[m] alpar@1: * c_stat[1] c_prim[1] c_dual[1] alpar@1: * . . . alpar@1: * c_stat[n] c_prim[n] c_dual[n] alpar@1: * alpar@1: * where: alpar@1: * m is the number of rows (auxiliary variables); alpar@1: * n is the number of columns (structural variables); alpar@1: * p_stat is the primal status of the basic solution (GLP_UNDEF = 1, alpar@1: * GLP_FEAS = 2, GLP_INFEAS = 3, or GLP_NOFEAS = 4); alpar@1: * d_stat is the dual status of the basic solution (GLP_UNDEF = 1, alpar@1: * GLP_FEAS = 2, GLP_INFEAS = 3, or GLP_NOFEAS = 4); alpar@1: * obj_val is the objective value; alpar@1: * r_stat[i], i = 1,...,m, is the status of i-th row (GLP_BS = 1, alpar@1: * GLP_NL = 2, GLP_NU = 3, GLP_NF = 4, or GLP_NS = 5); alpar@1: * r_prim[i], i = 1,...,m, is the primal value of i-th row; alpar@1: * r_dual[i], i = 1,...,m, is the dual value of i-th row; alpar@1: * c_stat[j], j = 1,...,n, is the status of j-th column (GLP_BS = 1, alpar@1: * GLP_NL = 2, GLP_NU = 3, GLP_NF = 4, or GLP_NS = 5); alpar@1: * c_prim[j], j = 1,...,n, is the primal value of j-th column; alpar@1: * c_dual[j], j = 1,...,n, is the dual value of j-th column. */ alpar@1: alpar@1: int glp_write_sol(glp_prob *lp, const char *fname) alpar@1: { XFILE *fp; alpar@1: int i, j, ret = 0; alpar@1: xprintf("Writing basic solution to `%s'...\n", fname); alpar@1: fp = xfopen(fname, "w"); alpar@1: if (fp == NULL) alpar@1: { xprintf("Unable to create `%s' - %s\n", fname, xerrmsg()); alpar@1: ret = 1; alpar@1: goto done; alpar@1: } alpar@1: /* number of rows, number of columns */ alpar@1: xfprintf(fp, "%d %d\n", lp->m, lp->n); alpar@1: /* primal status, dual status, objective value */ alpar@1: xfprintf(fp, "%d %d %.*g\n", lp->pbs_stat, lp->dbs_stat, DBL_DIG, alpar@1: lp->obj_val); alpar@1: /* rows (auxiliary variables) */ alpar@1: for (i = 1; i <= lp->m; i++) alpar@1: { GLPROW *row = lp->row[i]; alpar@1: /* status, primal value, dual value */ alpar@1: xfprintf(fp, "%d %.*g %.*g\n", row->stat, DBL_DIG, row->prim, alpar@1: DBL_DIG, row->dual); alpar@1: } alpar@1: /* columns (structural variables) */ alpar@1: for (j = 1; j <= lp->n; j++) alpar@1: { GLPCOL *col = lp->col[j]; alpar@1: /* status, primal value, dual value */ alpar@1: xfprintf(fp, "%d %.*g %.*g\n", col->stat, DBL_DIG, col->prim, alpar@1: DBL_DIG, col->dual); alpar@1: } alpar@1: xfflush(fp); alpar@1: if (xferror(fp)) alpar@1: { xprintf("Write error on `%s' - %s\n", fname, xerrmsg()); alpar@1: ret = 1; alpar@1: goto done; alpar@1: } alpar@1: xprintf("%d lines were written\n", 2 + lp->m + lp->n); alpar@1: done: if (fp != NULL) xfclose(fp); alpar@1: return ret; alpar@1: } alpar@1: alpar@1: /**********************************************************************/ alpar@1: alpar@1: static char *format(char buf[13+1], double x) alpar@1: { /* format floating-point number in MPS/360-like style */ alpar@1: if (x == -DBL_MAX) alpar@1: strcpy(buf, " -Inf"); alpar@1: else if (x == +DBL_MAX) alpar@1: strcpy(buf, " +Inf"); alpar@1: else if (fabs(x) <= 999999.99998) alpar@1: { sprintf(buf, "%13.5f", x); alpar@1: #if 1 alpar@1: if (strcmp(buf, " 0.00000") == 0 || alpar@1: strcmp(buf, " -0.00000") == 0) alpar@1: strcpy(buf, " . "); alpar@1: else if (memcmp(buf, " 0.", 8) == 0) alpar@1: memcpy(buf, " .", 8); alpar@1: else if (memcmp(buf, " -0.", 8) == 0) alpar@1: memcpy(buf, " -.", 8); alpar@1: #endif alpar@1: } alpar@1: else alpar@1: sprintf(buf, "%13.6g", x); alpar@1: return buf; alpar@1: } alpar@1: alpar@1: int glp_print_ranges(glp_prob *P, int len, const int list[], alpar@1: int flags, const char *fname) alpar@1: { /* print sensitivity analysis report */ alpar@1: XFILE *fp = NULL; alpar@1: GLPROW *row; alpar@1: GLPCOL *col; alpar@1: int m, n, pass, k, t, numb, type, stat, var1, var2, count, page, alpar@1: ret; alpar@1: double lb, ub, slack, coef, prim, dual, value1, value2, coef1, alpar@1: coef2, obj1, obj2; alpar@1: const char *name, *limit; alpar@1: char buf[13+1]; alpar@1: /* sanity checks */ alpar@1: if (P == NULL || P->magic != GLP_PROB_MAGIC) alpar@1: xerror("glp_print_ranges: P = %p; invalid problem object\n", alpar@1: P); alpar@1: m = P->m, n = P->n; alpar@1: if (len < 0) alpar@1: xerror("glp_print_ranges: len = %d; invalid list length\n", alpar@1: len); alpar@1: if (len > 0) alpar@1: { if (list == NULL) alpar@1: xerror("glp_print_ranges: list = %p: invalid parameter\n", alpar@1: list); alpar@1: for (t = 1; t <= len; t++) alpar@1: { k = list[t]; alpar@1: if (!(1 <= k && k <= m+n)) alpar@1: xerror("glp_print_ranges: list[%d] = %d; row/column numb" alpar@1: "er out of range\n", t, k); alpar@1: } alpar@1: } alpar@1: if (flags != 0) alpar@1: xerror("glp_print_ranges: flags = %d; invalid parameter\n", alpar@1: flags); alpar@1: if (fname == NULL) alpar@1: xerror("glp_print_ranges: fname = %p; invalid parameter\n", alpar@1: fname); alpar@1: if (glp_get_status(P) != GLP_OPT) alpar@1: { xprintf("glp_print_ranges: optimal basic solution required\n"); alpar@1: ret = 1; alpar@1: goto done; alpar@1: } alpar@1: if (!glp_bf_exists(P)) alpar@1: { xprintf("glp_print_ranges: basis factorization required\n"); alpar@1: ret = 2; alpar@1: goto done; alpar@1: } alpar@1: /* start reporting */ alpar@1: xprintf("Write sensitivity analysis report to `%s'...\n", fname); alpar@1: fp = xfopen(fname, "w"); alpar@1: if (fp == NULL) alpar@1: { xprintf("Unable to create `%s' - %s\n", fname, xerrmsg()); alpar@1: ret = 3; alpar@1: goto done; alpar@1: } alpar@1: page = count = 0; alpar@1: for (pass = 1; pass <= 2; pass++) alpar@1: for (t = 1; t <= (len == 0 ? m+n : len); t++) alpar@1: { if (t == 1) count = 0; alpar@1: k = (len == 0 ? t : list[t]); alpar@1: if (pass == 1 && k > m || pass == 2 && k <= m) alpar@1: continue; alpar@1: if (count == 0) alpar@1: { xfprintf(fp, "GLPK %-4s - SENSITIVITY ANALYSIS REPORT%73sPa" alpar@1: "ge%4d\n", glp_version(), "", ++page); alpar@1: xfprintf(fp, "\n"); alpar@1: xfprintf(fp, "%-12s%s\n", "Problem:", alpar@1: P->name == NULL ? "" : P->name); alpar@1: xfprintf(fp, "%-12s%s%s%.10g (%s)\n", "Objective:", alpar@1: P->obj == NULL ? "" : P->obj, alpar@1: P->obj == NULL ? "" : " = ", P->obj_val, alpar@1: P->dir == GLP_MIN ? "MINimum" : alpar@1: P->dir == GLP_MAX ? "MAXimum" : "???"); alpar@1: xfprintf(fp, "\n"); alpar@1: xfprintf(fp, "%6s %-12s %2s %13s %13s %13s %13s %13s %13s " alpar@1: "%s\n", "No.", pass == 1 ? "Row name" : "Column name", alpar@1: "St", "Activity", pass == 1 ? "Slack" : "Obj coef", alpar@1: "Lower bound", "Activity", "Obj coef", "Obj value at", alpar@1: "Limiting"); alpar@1: xfprintf(fp, "%6s %-12s %2s %13s %13s %13s %13s %13s %13s " alpar@1: "%s\n", "", "", "", "", "Marginal", "Upper bound", alpar@1: "range", "range", "break point", "variable"); alpar@1: xfprintf(fp, "------ ------------ -- ------------- --------" alpar@1: "----- ------------- ------------- ------------- ------" alpar@1: "------- ------------\n"); alpar@1: } alpar@1: if (pass == 1) alpar@1: { numb = k; alpar@1: xassert(1 <= numb && numb <= m); alpar@1: row = P->row[numb]; alpar@1: name = row->name; alpar@1: type = row->type; alpar@1: lb = glp_get_row_lb(P, numb); alpar@1: ub = glp_get_row_ub(P, numb); alpar@1: coef = 0.0; alpar@1: stat = row->stat; alpar@1: prim = row->prim; alpar@1: if (type == GLP_FR) alpar@1: slack = - prim; alpar@1: else if (type == GLP_LO) alpar@1: slack = lb - prim; alpar@1: else if (type == GLP_UP || type == GLP_DB || type == GLP_FX) alpar@1: slack = ub - prim; alpar@1: dual = row->dual; alpar@1: } alpar@1: else alpar@1: { numb = k - m; alpar@1: xassert(1 <= numb && numb <= n); alpar@1: col = P->col[numb]; alpar@1: name = col->name; alpar@1: lb = glp_get_col_lb(P, numb); alpar@1: ub = glp_get_col_ub(P, numb); alpar@1: coef = col->coef; alpar@1: stat = col->stat; alpar@1: prim = col->prim; alpar@1: slack = 0.0; alpar@1: dual = col->dual; alpar@1: } alpar@1: if (stat != GLP_BS) alpar@1: { glp_analyze_bound(P, k, &value1, &var1, &value2, &var2); alpar@1: if (stat == GLP_NF) alpar@1: coef1 = coef2 = coef; alpar@1: else if (stat == GLP_NS) alpar@1: coef1 = -DBL_MAX, coef2 = +DBL_MAX; alpar@1: else if (stat == GLP_NL && P->dir == GLP_MIN || alpar@1: stat == GLP_NU && P->dir == GLP_MAX) alpar@1: coef1 = coef - dual, coef2 = +DBL_MAX; alpar@1: else alpar@1: coef1 = -DBL_MAX, coef2 = coef - dual; alpar@1: if (value1 == -DBL_MAX) alpar@1: { if (dual < -1e-9) alpar@1: obj1 = +DBL_MAX; alpar@1: else if (dual > +1e-9) alpar@1: obj1 = -DBL_MAX; alpar@1: else alpar@1: obj1 = P->obj_val; alpar@1: } alpar@1: else alpar@1: obj1 = P->obj_val + dual * (value1 - prim); alpar@1: if (value2 == +DBL_MAX) alpar@1: { if (dual < -1e-9) alpar@1: obj2 = -DBL_MAX; alpar@1: else if (dual > +1e-9) alpar@1: obj2 = +DBL_MAX; alpar@1: else alpar@1: obj2 = P->obj_val; alpar@1: } alpar@1: else alpar@1: obj2 = P->obj_val + dual * (value2 - prim); alpar@1: } alpar@1: else alpar@1: { glp_analyze_coef(P, k, &coef1, &var1, &value1, &coef2, alpar@1: &var2, &value2); alpar@1: if (coef1 == -DBL_MAX) alpar@1: { if (prim < -1e-9) alpar@1: obj1 = +DBL_MAX; alpar@1: else if (prim > +1e-9) alpar@1: obj1 = -DBL_MAX; alpar@1: else alpar@1: obj1 = P->obj_val; alpar@1: } alpar@1: else alpar@1: obj1 = P->obj_val + (coef1 - coef) * prim; alpar@1: if (coef2 == +DBL_MAX) alpar@1: { if (prim < -1e-9) alpar@1: obj2 = -DBL_MAX; alpar@1: else if (prim > +1e-9) alpar@1: obj2 = +DBL_MAX; alpar@1: else alpar@1: obj2 = P->obj_val; alpar@1: } alpar@1: else alpar@1: obj2 = P->obj_val + (coef2 - coef) * prim; alpar@1: } alpar@1: /*** first line ***/ alpar@1: /* row/column number */ alpar@1: xfprintf(fp, "%6d", numb); alpar@1: /* row/column name */ alpar@1: xfprintf(fp, " %-12.12s", name == NULL ? "" : name); alpar@1: if (name != NULL && strlen(name) > 12) alpar@1: xfprintf(fp, "%s\n%6s %12s", name+12, "", ""); alpar@1: /* row/column status */ alpar@1: xfprintf(fp, " %2s", alpar@1: stat == GLP_BS ? "BS" : stat == GLP_NL ? "NL" : alpar@1: stat == GLP_NU ? "NU" : stat == GLP_NF ? "NF" : alpar@1: stat == GLP_NS ? "NS" : "??"); alpar@1: /* row/column activity */ alpar@1: xfprintf(fp, " %s", format(buf, prim)); alpar@1: /* row slack, column objective coefficient */ alpar@1: xfprintf(fp, " %s", format(buf, k <= m ? slack : coef)); alpar@1: /* row/column lower bound */ alpar@1: xfprintf(fp, " %s", format(buf, lb)); alpar@1: /* row/column activity range */ alpar@1: xfprintf(fp, " %s", format(buf, value1)); alpar@1: /* row/column objective coefficient range */ alpar@1: xfprintf(fp, " %s", format(buf, coef1)); alpar@1: /* objective value at break point */ alpar@1: xfprintf(fp, " %s", format(buf, obj1)); alpar@1: /* limiting variable name */ alpar@1: if (var1 != 0) alpar@1: { if (var1 <= m) alpar@1: limit = glp_get_row_name(P, var1); alpar@1: else alpar@1: limit = glp_get_col_name(P, var1 - m); alpar@1: if (limit != NULL) alpar@1: xfprintf(fp, " %s", limit); alpar@1: } alpar@1: xfprintf(fp, "\n"); alpar@1: /*** second line ***/ alpar@1: xfprintf(fp, "%6s %-12s %2s %13s", "", "", "", ""); alpar@1: /* row/column reduced cost */ alpar@1: xfprintf(fp, " %s", format(buf, dual)); alpar@1: /* row/column upper bound */ alpar@1: xfprintf(fp, " %s", format(buf, ub)); alpar@1: /* row/column activity range */ alpar@1: xfprintf(fp, " %s", format(buf, value2)); alpar@1: /* row/column objective coefficient range */ alpar@1: xfprintf(fp, " %s", format(buf, coef2)); alpar@1: /* objective value at break point */ alpar@1: xfprintf(fp, " %s", format(buf, obj2)); alpar@1: /* limiting variable name */ alpar@1: if (var2 != 0) alpar@1: { if (var2 <= m) alpar@1: limit = glp_get_row_name(P, var2); alpar@1: else alpar@1: limit = glp_get_col_name(P, var2 - m); alpar@1: if (limit != NULL) alpar@1: xfprintf(fp, " %s", limit); alpar@1: } alpar@1: xfprintf(fp, "\n"); alpar@1: xfprintf(fp, "\n"); alpar@1: /* print 10 items per page */ alpar@1: count = (count + 1) % 10; alpar@1: } alpar@1: xfprintf(fp, "End of report\n"); alpar@1: xfflush(fp); alpar@1: if (xferror(fp)) alpar@1: { xprintf("Write error on `%s' - %s\n", fname, xerrmsg()); alpar@1: ret = 4; alpar@1: goto done; alpar@1: } alpar@1: ret = 0; alpar@1: done: if (fp != NULL) xfclose(fp); alpar@1: return ret; alpar@1: } alpar@1: alpar@1: /**********************************************************************/ alpar@1: alpar@1: int glp_print_ipt(glp_prob *P, const char *fname) alpar@1: { /* write interior-point solution in printable format */ alpar@1: XFILE *fp; alpar@1: GLPROW *row; alpar@1: GLPCOL *col; alpar@1: int i, j, t, ae_ind, re_ind, ret; alpar@1: double ae_max, re_max; alpar@1: xprintf("Writing interior-point solution to `%s'...\n", fname); alpar@1: fp = xfopen(fname, "w"); alpar@1: if (fp == NULL) alpar@1: { xprintf("Unable to create `%s' - %s\n", fname, xerrmsg()); alpar@1: ret = 1; alpar@1: goto done; alpar@1: } alpar@1: xfprintf(fp, "%-12s%s\n", "Problem:", alpar@1: P->name == NULL ? "" : P->name); alpar@1: xfprintf(fp, "%-12s%d\n", "Rows:", P->m); alpar@1: xfprintf(fp, "%-12s%d\n", "Columns:", P->n); alpar@1: xfprintf(fp, "%-12s%d\n", "Non-zeros:", P->nnz); alpar@1: t = glp_ipt_status(P); alpar@1: xfprintf(fp, "%-12s%s\n", "Status:", alpar@1: t == GLP_OPT ? "OPTIMAL" : alpar@1: t == GLP_UNDEF ? "UNDEFINED" : alpar@1: t == GLP_INFEAS ? "INFEASIBLE (INTERMEDIATE)" : alpar@1: t == GLP_NOFEAS ? "INFEASIBLE (FINAL)" : "???"); alpar@1: xfprintf(fp, "%-12s%s%s%.10g (%s)\n", "Objective:", alpar@1: P->obj == NULL ? "" : P->obj, alpar@1: P->obj == NULL ? "" : " = ", P->ipt_obj, alpar@1: P->dir == GLP_MIN ? "MINimum" : alpar@1: P->dir == GLP_MAX ? "MAXimum" : "???"); alpar@1: xfprintf(fp, "\n"); alpar@1: xfprintf(fp, " No. Row name Activity Lower bound " alpar@1: " Upper bound Marginal\n"); alpar@1: xfprintf(fp, "------ ------------ ------------- ------------- " alpar@1: "------------- -------------\n"); alpar@1: for (i = 1; i <= P->m; i++) alpar@1: { row = P->row[i]; alpar@1: xfprintf(fp, "%6d ", i); alpar@1: if (row->name == NULL || strlen(row->name) <= 12) alpar@1: xfprintf(fp, "%-12s ", row->name == NULL ? "" : row->name); alpar@1: else alpar@1: xfprintf(fp, "%s\n%20s", row->name, ""); alpar@1: xfprintf(fp, "%3s", ""); alpar@1: xfprintf(fp, "%13.6g ", alpar@1: fabs(row->pval) <= 1e-9 ? 0.0 : row->pval); alpar@1: if (row->type == GLP_LO || row->type == GLP_DB || alpar@1: row->type == GLP_FX) alpar@1: xfprintf(fp, "%13.6g ", row->lb); alpar@1: else alpar@1: xfprintf(fp, "%13s ", ""); alpar@1: if (row->type == GLP_UP || row->type == GLP_DB) alpar@1: xfprintf(fp, "%13.6g ", row->ub); alpar@1: else alpar@1: xfprintf(fp, "%13s ", row->type == GLP_FX ? "=" : ""); alpar@1: if (fabs(row->dval) <= 1e-9) alpar@1: xfprintf(fp, "%13s", "< eps"); alpar@1: else alpar@1: xfprintf(fp, "%13.6g ", row->dval); alpar@1: xfprintf(fp, "\n"); alpar@1: } alpar@1: xfprintf(fp, "\n"); alpar@1: xfprintf(fp, " No. Column name Activity Lower bound " alpar@1: " Upper bound Marginal\n"); alpar@1: xfprintf(fp, "------ ------------ ------------- ------------- " alpar@1: "------------- -------------\n"); alpar@1: for (j = 1; j <= P->n; j++) alpar@1: { col = P->col[j]; alpar@1: xfprintf(fp, "%6d ", j); alpar@1: if (col->name == NULL || strlen(col->name) <= 12) alpar@1: xfprintf(fp, "%-12s ", col->name == NULL ? "" : col->name); alpar@1: else alpar@1: xfprintf(fp, "%s\n%20s", col->name, ""); alpar@1: xfprintf(fp, "%3s", ""); alpar@1: xfprintf(fp, "%13.6g ", alpar@1: fabs(col->pval) <= 1e-9 ? 0.0 : col->pval); alpar@1: if (col->type == GLP_LO || col->type == GLP_DB || alpar@1: col->type == GLP_FX) alpar@1: xfprintf(fp, "%13.6g ", col->lb); alpar@1: else alpar@1: xfprintf(fp, "%13s ", ""); alpar@1: if (col->type == GLP_UP || col->type == GLP_DB) alpar@1: xfprintf(fp, "%13.6g ", col->ub); alpar@1: else alpar@1: xfprintf(fp, "%13s ", col->type == GLP_FX ? "=" : ""); alpar@1: if (fabs(col->dval) <= 1e-9) alpar@1: xfprintf(fp, "%13s", "< eps"); alpar@1: else alpar@1: xfprintf(fp, "%13.6g ", col->dval); alpar@1: xfprintf(fp, "\n"); alpar@1: } alpar@1: xfprintf(fp, "\n"); alpar@1: xfprintf(fp, "Karush-Kuhn-Tucker optimality conditions:\n"); alpar@1: xfprintf(fp, "\n"); alpar@1: _glp_check_kkt(P, GLP_IPT, GLP_KKT_PE, &ae_max, &ae_ind, &re_max, alpar@1: &re_ind); alpar@1: xfprintf(fp, "KKT.PE: max.abs.err = %.2e on row %d\n", alpar@1: ae_max, ae_ind); alpar@1: xfprintf(fp, " max.rel.err = %.2e on row %d\n", alpar@1: re_max, re_ind); alpar@1: xfprintf(fp, "%8s%s\n", "", alpar@1: re_max <= 1e-9 ? "High quality" : alpar@1: re_max <= 1e-6 ? "Medium quality" : alpar@1: re_max <= 1e-3 ? "Low quality" : "PRIMAL SOLUTION IS WRONG"); alpar@1: xfprintf(fp, "\n"); alpar@1: _glp_check_kkt(P, GLP_IPT, GLP_KKT_PB, &ae_max, &ae_ind, &re_max, alpar@1: &re_ind); alpar@1: xfprintf(fp, "KKT.PB: max.abs.err = %.2e on %s %d\n", alpar@1: ae_max, ae_ind <= P->m ? "row" : "column", alpar@1: ae_ind <= P->m ? ae_ind : ae_ind - P->m); alpar@1: xfprintf(fp, " max.rel.err = %.2e on %s %d\n", alpar@1: re_max, re_ind <= P->m ? "row" : "column", alpar@1: re_ind <= P->m ? re_ind : re_ind - P->m); alpar@1: xfprintf(fp, "%8s%s\n", "", alpar@1: re_max <= 1e-9 ? "High quality" : alpar@1: re_max <= 1e-6 ? "Medium quality" : alpar@1: re_max <= 1e-3 ? "Low quality" : "PRIMAL SOLUTION IS INFEASIBL" alpar@1: "E"); alpar@1: xfprintf(fp, "\n"); alpar@1: _glp_check_kkt(P, GLP_IPT, GLP_KKT_DE, &ae_max, &ae_ind, &re_max, alpar@1: &re_ind); alpar@1: xfprintf(fp, "KKT.DE: max.abs.err = %.2e on column %d\n", alpar@1: ae_max, ae_ind == 0 ? 0 : ae_ind - P->m); alpar@1: xfprintf(fp, " max.rel.err = %.2e on column %d\n", alpar@1: re_max, re_ind == 0 ? 0 : re_ind - P->m); alpar@1: xfprintf(fp, "%8s%s\n", "", alpar@1: re_max <= 1e-9 ? "High quality" : alpar@1: re_max <= 1e-6 ? "Medium quality" : alpar@1: re_max <= 1e-3 ? "Low quality" : "DUAL SOLUTION IS WRONG"); alpar@1: xfprintf(fp, "\n"); alpar@1: _glp_check_kkt(P, GLP_IPT, GLP_KKT_DB, &ae_max, &ae_ind, &re_max, alpar@1: &re_ind); alpar@1: xfprintf(fp, "KKT.DB: max.abs.err = %.2e on %s %d\n", alpar@1: ae_max, ae_ind <= P->m ? "row" : "column", alpar@1: ae_ind <= P->m ? ae_ind : ae_ind - P->m); alpar@1: xfprintf(fp, " max.rel.err = %.2e on %s %d\n", alpar@1: re_max, re_ind <= P->m ? "row" : "column", alpar@1: re_ind <= P->m ? re_ind : re_ind - P->m); alpar@1: xfprintf(fp, "%8s%s\n", "", alpar@1: re_max <= 1e-9 ? "High quality" : alpar@1: re_max <= 1e-6 ? "Medium quality" : alpar@1: re_max <= 1e-3 ? "Low quality" : "DUAL SOLUTION IS INFEASIBLE") alpar@1: ; alpar@1: xfprintf(fp, "\n"); alpar@1: xfprintf(fp, "End of output\n"); alpar@1: xfflush(fp); alpar@1: if (xferror(fp)) alpar@1: { xprintf("Write error on `%s' - %s\n", fname, xerrmsg()); alpar@1: ret = 1; alpar@1: goto done; alpar@1: } alpar@1: ret = 0; alpar@1: done: if (fp != NULL) xfclose(fp); alpar@1: return ret; alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * glp_read_ipt - read interior-point solution from text file alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * int glp_read_ipt(glp_prob *lp, const char *fname); alpar@1: * alpar@1: * DESCRIPTION alpar@1: * alpar@1: * The routine glp_read_ipt reads interior-point solution from a text alpar@1: * file whose name is specified by the parameter fname into the problem alpar@1: * object. alpar@1: * alpar@1: * For the file format see description of the routine glp_write_ipt. alpar@1: * alpar@1: * RETURNS alpar@1: * alpar@1: * On success the routine returns zero, otherwise non-zero. */ alpar@1: alpar@1: int glp_read_ipt(glp_prob *lp, const char *fname) alpar@1: { glp_data *data; alpar@1: jmp_buf jump; alpar@1: int i, j, k, ret = 0; alpar@1: xprintf("Reading interior-point solution from `%s'...\n", fname); alpar@1: data = glp_sdf_open_file(fname); alpar@1: if (data == NULL) alpar@1: { ret = 1; alpar@1: goto done; alpar@1: } alpar@1: if (setjmp(jump)) alpar@1: { ret = 1; alpar@1: goto done; alpar@1: } alpar@1: glp_sdf_set_jump(data, jump); alpar@1: /* number of rows, number of columns */ alpar@1: k = glp_sdf_read_int(data); alpar@1: if (k != lp->m) alpar@1: glp_sdf_error(data, "wrong number of rows\n"); alpar@1: k = glp_sdf_read_int(data); alpar@1: if (k != lp->n) alpar@1: glp_sdf_error(data, "wrong number of columns\n"); alpar@1: /* solution status, objective value */ alpar@1: k = glp_sdf_read_int(data); alpar@1: if (!(k == GLP_UNDEF || k == GLP_OPT)) alpar@1: glp_sdf_error(data, "invalid solution status\n"); alpar@1: lp->ipt_stat = k; alpar@1: lp->ipt_obj = glp_sdf_read_num(data); alpar@1: /* rows (auxiliary variables) */ alpar@1: for (i = 1; i <= lp->m; i++) alpar@1: { GLPROW *row = lp->row[i]; alpar@1: /* primal value, dual value */ alpar@1: row->pval = glp_sdf_read_num(data); alpar@1: row->dval = glp_sdf_read_num(data); alpar@1: } alpar@1: /* columns (structural variables) */ alpar@1: for (j = 1; j <= lp->n; j++) alpar@1: { GLPCOL *col = lp->col[j]; alpar@1: /* primal value, dual value */ alpar@1: col->pval = glp_sdf_read_num(data); alpar@1: col->dval = glp_sdf_read_num(data); alpar@1: } alpar@1: xprintf("%d lines were read\n", glp_sdf_line(data)); alpar@1: done: if (ret) lp->ipt_stat = GLP_UNDEF; alpar@1: if (data != NULL) glp_sdf_close_file(data); alpar@1: return ret; alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * glp_write_ipt - write interior-point solution to text file alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * int glp_write_ipt(glp_prob *lp, const char *fname); alpar@1: * alpar@1: * DESCRIPTION alpar@1: * alpar@1: * The routine glp_write_ipt writes the current interior-point solution alpar@1: * to a text file whose name is specified by the parameter fname. This alpar@1: * file can be read back with the routine glp_read_ipt. alpar@1: * alpar@1: * RETURNS alpar@1: * alpar@1: * On success the routine returns zero, otherwise non-zero. alpar@1: * alpar@1: * FILE FORMAT alpar@1: * alpar@1: * The file created by the routine glp_write_ipt is a plain text file, alpar@1: * which contains the following information: alpar@1: * alpar@1: * m n alpar@1: * stat obj_val alpar@1: * r_prim[1] r_dual[1] alpar@1: * . . . alpar@1: * r_prim[m] r_dual[m] alpar@1: * c_prim[1] c_dual[1] alpar@1: * . . . alpar@1: * c_prim[n] c_dual[n] alpar@1: * alpar@1: * where: alpar@1: * m is the number of rows (auxiliary variables); alpar@1: * n is the number of columns (structural variables); alpar@1: * stat is the solution status (GLP_UNDEF = 1 or GLP_OPT = 5); alpar@1: * obj_val is the objective value; alpar@1: * r_prim[i], i = 1,...,m, is the primal value of i-th row; alpar@1: * r_dual[i], i = 1,...,m, is the dual value of i-th row; alpar@1: * c_prim[j], j = 1,...,n, is the primal value of j-th column; alpar@1: * c_dual[j], j = 1,...,n, is the dual value of j-th column. */ alpar@1: alpar@1: int glp_write_ipt(glp_prob *lp, const char *fname) alpar@1: { XFILE *fp; alpar@1: int i, j, ret = 0; alpar@1: xprintf("Writing interior-point solution to `%s'...\n", fname); alpar@1: fp = xfopen(fname, "w"); alpar@1: if (fp == NULL) alpar@1: { xprintf("Unable to create `%s' - %s\n", fname, xerrmsg()); alpar@1: ret = 1; alpar@1: goto done; alpar@1: } alpar@1: /* number of rows, number of columns */ alpar@1: xfprintf(fp, "%d %d\n", lp->m, lp->n); alpar@1: /* solution status, objective value */ alpar@1: xfprintf(fp, "%d %.*g\n", lp->ipt_stat, DBL_DIG, lp->ipt_obj); alpar@1: /* rows (auxiliary variables) */ alpar@1: for (i = 1; i <= lp->m; i++) alpar@1: { GLPROW *row = lp->row[i]; alpar@1: /* primal value, dual value */ alpar@1: xfprintf(fp, "%.*g %.*g\n", DBL_DIG, row->pval, DBL_DIG, alpar@1: row->dval); alpar@1: } alpar@1: /* columns (structural variables) */ alpar@1: for (j = 1; j <= lp->n; j++) alpar@1: { GLPCOL *col = lp->col[j]; alpar@1: /* primal value, dual value */ alpar@1: xfprintf(fp, "%.*g %.*g\n", DBL_DIG, col->pval, DBL_DIG, alpar@1: col->dval); alpar@1: } alpar@1: xfflush(fp); alpar@1: if (xferror(fp)) alpar@1: { xprintf("Write error on `%s' - %s\n", fname, xerrmsg()); alpar@1: ret = 1; alpar@1: goto done; alpar@1: } alpar@1: xprintf("%d lines were written\n", 2 + lp->m + lp->n); alpar@1: done: if (fp != NULL) xfclose(fp); alpar@1: return ret; alpar@1: } alpar@1: alpar@1: /**********************************************************************/ alpar@1: alpar@1: int glp_print_mip(glp_prob *P, const char *fname) alpar@1: { /* write MIP solution in printable format */ alpar@1: XFILE *fp; alpar@1: GLPROW *row; alpar@1: GLPCOL *col; alpar@1: int i, j, t, ae_ind, re_ind, ret; alpar@1: double ae_max, re_max; alpar@1: xprintf("Writing MIP solution to `%s'...\n", fname); alpar@1: fp = xfopen(fname, "w"); alpar@1: if (fp == NULL) alpar@1: { xprintf("Unable to create `%s' - %s\n", fname, xerrmsg()); alpar@1: ret = 1; alpar@1: goto done; alpar@1: } alpar@1: xfprintf(fp, "%-12s%s\n", "Problem:", alpar@1: P->name == NULL ? "" : P->name); alpar@1: xfprintf(fp, "%-12s%d\n", "Rows:", P->m); alpar@1: xfprintf(fp, "%-12s%d (%d integer, %d binary)\n", "Columns:", alpar@1: P->n, glp_get_num_int(P), glp_get_num_bin(P)); alpar@1: xfprintf(fp, "%-12s%d\n", "Non-zeros:", P->nnz); alpar@1: t = glp_mip_status(P); alpar@1: xfprintf(fp, "%-12s%s\n", "Status:", alpar@1: t == GLP_OPT ? "INTEGER OPTIMAL" : alpar@1: t == GLP_FEAS ? "INTEGER NON-OPTIMAL" : alpar@1: t == GLP_NOFEAS ? "INTEGER EMPTY" : alpar@1: t == GLP_UNDEF ? "INTEGER UNDEFINED" : "???"); alpar@1: xfprintf(fp, "%-12s%s%s%.10g (%s)\n", "Objective:", alpar@1: P->obj == NULL ? "" : P->obj, alpar@1: P->obj == NULL ? "" : " = ", P->mip_obj, alpar@1: P->dir == GLP_MIN ? "MINimum" : alpar@1: P->dir == GLP_MAX ? "MAXimum" : "???"); alpar@1: xfprintf(fp, "\n"); alpar@1: xfprintf(fp, " No. Row name Activity Lower bound " alpar@1: " Upper bound\n"); alpar@1: xfprintf(fp, "------ ------------ ------------- ------------- " alpar@1: "-------------\n"); alpar@1: for (i = 1; i <= P->m; i++) alpar@1: { row = P->row[i]; alpar@1: xfprintf(fp, "%6d ", i); alpar@1: if (row->name == NULL || strlen(row->name) <= 12) alpar@1: xfprintf(fp, "%-12s ", row->name == NULL ? "" : row->name); alpar@1: else alpar@1: xfprintf(fp, "%s\n%20s", row->name, ""); alpar@1: xfprintf(fp, "%3s", ""); alpar@1: xfprintf(fp, "%13.6g ", alpar@1: fabs(row->mipx) <= 1e-9 ? 0.0 : row->mipx); alpar@1: if (row->type == GLP_LO || row->type == GLP_DB || alpar@1: row->type == GLP_FX) alpar@1: xfprintf(fp, "%13.6g ", row->lb); alpar@1: else alpar@1: xfprintf(fp, "%13s ", ""); alpar@1: if (row->type == GLP_UP || row->type == GLP_DB) alpar@1: xfprintf(fp, "%13.6g ", row->ub); alpar@1: else alpar@1: xfprintf(fp, "%13s ", row->type == GLP_FX ? "=" : ""); alpar@1: xfprintf(fp, "\n"); alpar@1: } alpar@1: xfprintf(fp, "\n"); alpar@1: xfprintf(fp, " No. Column name Activity Lower bound " alpar@1: " Upper bound\n"); alpar@1: xfprintf(fp, "------ ------------ ------------- ------------- " alpar@1: "-------------\n"); alpar@1: for (j = 1; j <= P->n; j++) alpar@1: { col = P->col[j]; alpar@1: xfprintf(fp, "%6d ", j); alpar@1: if (col->name == NULL || strlen(col->name) <= 12) alpar@1: xfprintf(fp, "%-12s ", col->name == NULL ? "" : col->name); alpar@1: else alpar@1: xfprintf(fp, "%s\n%20s", col->name, ""); alpar@1: xfprintf(fp, "%s ", alpar@1: col->kind == GLP_CV ? " " : alpar@1: col->kind == GLP_IV ? "*" : "?"); alpar@1: xfprintf(fp, "%13.6g ", alpar@1: fabs(col->mipx) <= 1e-9 ? 0.0 : col->mipx); alpar@1: if (col->type == GLP_LO || col->type == GLP_DB || alpar@1: col->type == GLP_FX) alpar@1: xfprintf(fp, "%13.6g ", col->lb); alpar@1: else alpar@1: xfprintf(fp, "%13s ", ""); alpar@1: if (col->type == GLP_UP || col->type == GLP_DB) alpar@1: xfprintf(fp, "%13.6g ", col->ub); alpar@1: else alpar@1: xfprintf(fp, "%13s ", col->type == GLP_FX ? "=" : ""); alpar@1: xfprintf(fp, "\n"); alpar@1: } alpar@1: xfprintf(fp, "\n"); alpar@1: xfprintf(fp, "Integer feasibility conditions:\n"); alpar@1: xfprintf(fp, "\n"); alpar@1: _glp_check_kkt(P, GLP_MIP, GLP_KKT_PE, &ae_max, &ae_ind, &re_max, alpar@1: &re_ind); alpar@1: xfprintf(fp, "KKT.PE: max.abs.err = %.2e on row %d\n", alpar@1: ae_max, ae_ind); alpar@1: xfprintf(fp, " max.rel.err = %.2e on row %d\n", alpar@1: re_max, re_ind); alpar@1: xfprintf(fp, "%8s%s\n", "", alpar@1: re_max <= 1e-9 ? "High quality" : alpar@1: re_max <= 1e-6 ? "Medium quality" : alpar@1: re_max <= 1e-3 ? "Low quality" : "SOLUTION IS WRONG"); alpar@1: xfprintf(fp, "\n"); alpar@1: _glp_check_kkt(P, GLP_MIP, GLP_KKT_PB, &ae_max, &ae_ind, &re_max, alpar@1: &re_ind); alpar@1: xfprintf(fp, "KKT.PB: max.abs.err = %.2e on %s %d\n", alpar@1: ae_max, ae_ind <= P->m ? "row" : "column", alpar@1: ae_ind <= P->m ? ae_ind : ae_ind - P->m); alpar@1: xfprintf(fp, " max.rel.err = %.2e on %s %d\n", alpar@1: re_max, re_ind <= P->m ? "row" : "column", alpar@1: re_ind <= P->m ? re_ind : re_ind - P->m); alpar@1: xfprintf(fp, "%8s%s\n", "", alpar@1: re_max <= 1e-9 ? "High quality" : alpar@1: re_max <= 1e-6 ? "Medium quality" : alpar@1: re_max <= 1e-3 ? "Low quality" : "SOLUTION IS INFEASIBLE"); alpar@1: xfprintf(fp, "\n"); alpar@1: xfprintf(fp, "End of output\n"); alpar@1: xfflush(fp); alpar@1: if (xferror(fp)) alpar@1: { xprintf("Write error on `%s' - %s\n", fname, xerrmsg()); alpar@1: ret = 1; alpar@1: goto done; alpar@1: } alpar@1: ret = 0; alpar@1: done: if (fp != NULL) xfclose(fp); alpar@1: return ret; alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * glp_read_mip - read MIP solution from text file alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * int glp_read_mip(glp_prob *mip, const char *fname); alpar@1: * alpar@1: * DESCRIPTION alpar@1: * alpar@1: * The routine glp_read_mip reads MIP solution from a text file whose alpar@1: * name is specified by the parameter fname into the problem object. alpar@1: * alpar@1: * For the file format see description of the routine glp_write_mip. alpar@1: * alpar@1: * RETURNS alpar@1: * alpar@1: * On success the routine returns zero, otherwise non-zero. */ alpar@1: alpar@1: int glp_read_mip(glp_prob *mip, const char *fname) alpar@1: { glp_data *data; alpar@1: jmp_buf jump; alpar@1: int i, j, k, ret = 0; alpar@1: xprintf("Reading MIP solution from `%s'...\n", fname); alpar@1: data = glp_sdf_open_file(fname); alpar@1: if (data == NULL) alpar@1: { ret = 1; alpar@1: goto done; alpar@1: } alpar@1: if (setjmp(jump)) alpar@1: { ret = 1; alpar@1: goto done; alpar@1: } alpar@1: glp_sdf_set_jump(data, jump); alpar@1: /* number of rows, number of columns */ alpar@1: k = glp_sdf_read_int(data); alpar@1: if (k != mip->m) alpar@1: glp_sdf_error(data, "wrong number of rows\n"); alpar@1: k = glp_sdf_read_int(data); alpar@1: if (k != mip->n) alpar@1: glp_sdf_error(data, "wrong number of columns\n"); alpar@1: /* solution status, objective value */ alpar@1: k = glp_sdf_read_int(data); alpar@1: if (!(k == GLP_UNDEF || k == GLP_OPT || k == GLP_FEAS || alpar@1: k == GLP_NOFEAS)) alpar@1: glp_sdf_error(data, "invalid solution status\n"); alpar@1: mip->mip_stat = k; alpar@1: mip->mip_obj = glp_sdf_read_num(data); alpar@1: /* rows (auxiliary variables) */ alpar@1: for (i = 1; i <= mip->m; i++) alpar@1: { GLPROW *row = mip->row[i]; alpar@1: row->mipx = glp_sdf_read_num(data); alpar@1: } alpar@1: /* columns (structural variables) */ alpar@1: for (j = 1; j <= mip->n; j++) alpar@1: { GLPCOL *col = mip->col[j]; alpar@1: col->mipx = glp_sdf_read_num(data); alpar@1: if (col->kind == GLP_IV && col->mipx != floor(col->mipx)) alpar@1: glp_sdf_error(data, "non-integer column value"); alpar@1: } alpar@1: xprintf("%d lines were read\n", glp_sdf_line(data)); alpar@1: done: if (ret) mip->mip_stat = GLP_UNDEF; alpar@1: if (data != NULL) glp_sdf_close_file(data); alpar@1: return ret; alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * glp_write_mip - write MIP solution to text file alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * int glp_write_mip(glp_prob *mip, const char *fname); alpar@1: * alpar@1: * DESCRIPTION alpar@1: * alpar@1: * The routine glp_write_mip writes the current MIP solution to a text alpar@1: * file whose name is specified by the parameter fname. This file can alpar@1: * be read back with the routine glp_read_mip. alpar@1: * alpar@1: * RETURNS alpar@1: * alpar@1: * On success the routine returns zero, otherwise non-zero. alpar@1: * alpar@1: * FILE FORMAT alpar@1: * alpar@1: * The file created by the routine glp_write_sol is a plain text file, alpar@1: * which contains the following information: alpar@1: * alpar@1: * m n alpar@1: * stat obj_val alpar@1: * r_val[1] alpar@1: * . . . alpar@1: * r_val[m] alpar@1: * c_val[1] alpar@1: * . . . alpar@1: * c_val[n] alpar@1: * alpar@1: * where: alpar@1: * m is the number of rows (auxiliary variables); alpar@1: * n is the number of columns (structural variables); alpar@1: * stat is the solution status (GLP_UNDEF = 1, GLP_FEAS = 2, alpar@1: * GLP_NOFEAS = 4, or GLP_OPT = 5); alpar@1: * obj_val is the objective value; alpar@1: * r_val[i], i = 1,...,m, is the value of i-th row; alpar@1: * c_val[j], j = 1,...,n, is the value of j-th column. */ alpar@1: alpar@1: int glp_write_mip(glp_prob *mip, const char *fname) alpar@1: { XFILE *fp; alpar@1: int i, j, ret = 0; alpar@1: xprintf("Writing MIP solution to `%s'...\n", fname); alpar@1: fp = xfopen(fname, "w"); alpar@1: if (fp == NULL) alpar@1: { xprintf("Unable to create `%s' - %s\n", fname, xerrmsg()); alpar@1: ret = 1; alpar@1: goto done; alpar@1: } alpar@1: /* number of rows, number of columns */ alpar@1: xfprintf(fp, "%d %d\n", mip->m, mip->n); alpar@1: /* solution status, objective value */ alpar@1: xfprintf(fp, "%d %.*g\n", mip->mip_stat, DBL_DIG, mip->mip_obj); alpar@1: /* rows (auxiliary variables) */ alpar@1: for (i = 1; i <= mip->m; i++) alpar@1: xfprintf(fp, "%.*g\n", DBL_DIG, mip->row[i]->mipx); alpar@1: /* columns (structural variables) */ alpar@1: for (j = 1; j <= mip->n; j++) alpar@1: xfprintf(fp, "%.*g\n", DBL_DIG, mip->col[j]->mipx); alpar@1: xfflush(fp); alpar@1: if (xferror(fp)) alpar@1: { xprintf("Write error on `%s' - %s\n", fname, xerrmsg()); alpar@1: ret = 1; alpar@1: goto done; alpar@1: } alpar@1: xprintf("%d lines were written\n", 2 + mip->m + mip->n); alpar@1: done: if (fp != NULL) xfclose(fp); alpar@1: return ret; alpar@1: } alpar@1: alpar@1: /* eof */