int lpx_write_cpxlp(LPX *lp, const char *fname) { /* write problem data in CPLEX LP format */ FILE *fp; int nrows, ncols, i, j, t, len, typx, flag, kind, *ind; double lb, ub, temp, *val; char line[1023+1], term[1023+1], rname[255+1], cname[255+1]; print("lpx_write_cpxlp: writing problem data to `%s'...", fname); /* open the output text file */ fp = xfopen(fname, "w"); if (fp == NULL) { print("lpx_write_cpxlp: unable to create `%s' - %s", fname, strerror(errno)); goto fail; } /* determine the number of rows and columns */ nrows = lpx_get_num_rows(lp); ncols = lpx_get_num_cols(lp); /* the problem should contain at least one row and one column */ if (!(nrows > 0 && ncols > 0)) fault("lpx_write_cpxlp: problem has no rows/columns"); /* write problem name */ { const char *name = lpx_get_prob_name(lp); if (name == NULL) name = "Unknown"; fprintf(fp, "\\* Problem: %s *\\\n", name); fprintf(fp, "\n"); } /* allocate working arrays */ ind = xcalloc(1+ncols, sizeof(int)); val = xcalloc(1+ncols, sizeof(double)); /* write the objective function definition and the constraints section */ for (i = 0; i <= nrows; i++) { if (i == 0) { switch (lpx_get_obj_dir(lp)) { case LPX_MIN: fprintf(fp, "Minimize\n"); break; case LPX_MAX: fprintf(fp, "Maximize\n"); break; default: xassert(lp != lp); } } else if (i == 1) { temp = lpx_get_obj_coef(lp, 0); if (temp != 0.0) fprintf(fp, "\\* constant term = %.*g *\\\n", DBL_DIG, temp); fprintf(fp, "\n"); fprintf(fp, "Subject To\n"); } row_name(lp, i, rname); if (i == 0) { len = 0; for (j = 1; j <= ncols; j++) { temp = lpx_get_obj_coef(lp, j); if (temp != 0.0) len++, ind[len] = j, val[len] = temp; } } else { lpx_get_row_bnds(lp, i, &typx, &lb, &ub); if (typx == LPX_FR) continue; len = lpx_get_mat_row(lp, i, ind, val); } flag = 0; more: if (!flag) sprintf(line, " %s:", rname); else sprintf(line, " %*s ", strlen(rname), ""); for (t = 1; t <= len; t++) { col_name(lp, ind[t], cname); if (val[t] == +1.0) sprintf(term, " + %s", cname); else if (val[t] == -1.0) sprintf(term, " - %s", cname); else if (val[t] > 0.0) sprintf(term, " + %.*g %s", DBL_DIG, +val[t], cname); else if (val[t] < 0.0) sprintf(term, " - %.*g %s", DBL_DIG, -val[t], cname); else xassert(lp != lp); if (strlen(line) + strlen(term) > 72) fprintf(fp, "%s\n", line), line[0] = '\0'; strcat(line, term); } if (len == 0) { /* empty row */ sprintf(term, " 0 %s", col_name(lp, 1, cname)); strcat(line, term); } if (i > 0) { switch (typx) { case LPX_LO: case LPX_DB: sprintf(term, " >= %.*g", DBL_DIG, lb); break; case LPX_UP: sprintf(term, " <= %.*g", DBL_DIG, ub); break; case LPX_FX: sprintf(term, " = %.*g", DBL_DIG, lb); break; default: xassert(typx != typx); } if (strlen(line) + strlen(term) > 72) fprintf(fp, "%s\n", line), line[0] = '\0'; strcat(line, term); } fprintf(fp, "%s\n", line); if (i > 0 && typx == LPX_DB) { /* double-bounded row needs a copy for its upper bound */ flag = 1; typx = LPX_UP; goto more; } } /* free working arrays */ xfree(ind); xfree(val); /* write the bounds section */ flag = 0; for (j = 1; j <= ncols; j++) { col_name(lp, j, cname); lpx_get_col_bnds(lp, j, &typx, &lb, &ub); if (typx == LPX_LO && lb == 0.0) continue; if (!flag) { fprintf(fp, "\n"); fprintf(fp, "Bounds\n"); flag = 1; } switch (typx) { case LPX_FR: fprintf(fp, " %s free\n", cname); break; case LPX_LO: fprintf(fp, " %s >= %.*g\n", cname, DBL_DIG, lb); break; case LPX_UP: fprintf(fp, " %s <= %.*g\n", cname, DBL_DIG, ub); break; case LPX_DB: fprintf(fp, " %.*g <= %s <= %.*g\n", DBL_DIG, lb, cname, DBL_DIG, ub); break; case LPX_FX: fprintf(fp, " %s = %.*g\n", cname, DBL_DIG, lb); break; default: xassert(typx != typx); } } /* write the general section */ if (lpx_get_class(lp) == LPX_MIP) { flag = 0; for (j = 1; j <= ncols; j++) { kind = lpx_get_col_kind(lp, j); if (kind == LPX_CV) continue; xassert(kind == LPX_IV); if (!flag) { fprintf(fp, "\n"); fprintf(fp, "Generals\n"); flag = 1; } fprintf(fp, " %s\n", col_name(lp, j, cname)); } } /* write the end keyword */ fprintf(fp, "\n"); fprintf(fp, "End\n"); /* close the output text file */ fflush(fp); if (ferror(fp)) { print("lpx_write_cpxlp: write error on `%s' - %s", fname, strerror(errno)); goto fail; } xfclose(fp); /* return to the calling program */ return 0; fail: /* the operation failed */ if (fp != NULL) xfclose(fp); return 1; }
int lpx_print_prob(LPX *lp, const char *fname) { XFILE *fp; int m, n, mip, i, j, len, t, type, *ndx; double coef, lb, ub, *val; char *str, name[255+1]; xprintf("lpx_write_prob: writing problem data to `%s'...\n", fname); fp = xfopen(fname, "w"); if (fp == NULL) { xprintf("lpx_write_prob: unable to create `%s' - %s\n", fname, strerror(errno)); goto fail; } m = lpx_get_num_rows(lp); n = lpx_get_num_cols(lp); mip = (lpx_get_class(lp) == LPX_MIP); str = (void *)lpx_get_prob_name(lp); xfprintf(fp, "Problem: %s\n", str == NULL ? "(unnamed)" : str); xfprintf(fp, "Class: %s\n", !mip ? "LP" : "MIP"); xfprintf(fp, "Rows: %d\n", m); if (!mip) xfprintf(fp, "Columns: %d\n", n); else xfprintf(fp, "Columns: %d (%d integer, %d binary)\n", n, lpx_get_num_int(lp), lpx_get_num_bin(lp)); xfprintf(fp, "Non-zeros: %d\n", lpx_get_num_nz(lp)); xfprintf(fp, "\n"); xfprintf(fp, "*** OBJECTIVE FUNCTION ***\n"); xfprintf(fp, "\n"); switch (lpx_get_obj_dir(lp)) { case LPX_MIN: xfprintf(fp, "Minimize:"); break; case LPX_MAX: xfprintf(fp, "Maximize:"); break; default: xassert(lp != lp); } str = (void *)lpx_get_obj_name(lp); xfprintf(fp, " %s\n", str == NULL ? "(unnamed)" : str); coef = lpx_get_obj_coef(lp, 0); if (coef != 0.0) xfprintf(fp, "%*.*g %s\n", DBL_DIG+7, DBL_DIG, coef, "(constant term)"); for (i = 1; i <= m; i++) #if 0 { coef = lpx_get_row_coef(lp, i); #else { coef = 0.0; #endif if (coef != 0.0) xfprintf(fp, "%*.*g %s\n", DBL_DIG+7, DBL_DIG, coef, row_name(lp, i, name)); } for (j = 1; j <= n; j++) { coef = lpx_get_obj_coef(lp, j); if (coef != 0.0) xfprintf(fp, "%*.*g %s\n", DBL_DIG+7, DBL_DIG, coef, col_name(lp, j, name)); } xfprintf(fp, "\n"); xfprintf(fp, "*** ROWS (CONSTRAINTS) ***\n"); ndx = xcalloc(1+n, sizeof(int)); val = xcalloc(1+n, sizeof(double)); for (i = 1; i <= m; i++) { xfprintf(fp, "\n"); xfprintf(fp, "Row %d: %s", i, row_name(lp, i, name)); lpx_get_row_bnds(lp, i, &type, &lb, &ub); switch (type) { case LPX_FR: xfprintf(fp, " free"); break; case LPX_LO: xfprintf(fp, " >= %.*g", DBL_DIG, lb); break; case LPX_UP: xfprintf(fp, " <= %.*g", DBL_DIG, ub); break; case LPX_DB: xfprintf(fp, " >= %.*g <= %.*g", DBL_DIG, lb, DBL_DIG, ub); break; case LPX_FX: xfprintf(fp, " = %.*g", DBL_DIG, lb); break; default: xassert(type != type); } xfprintf(fp, "\n"); #if 0 coef = lpx_get_row_coef(lp, i); #else coef = 0.0; #endif if (coef != 0.0) xfprintf(fp, "%*.*g %s\n", DBL_DIG+7, DBL_DIG, coef, "(objective)"); len = lpx_get_mat_row(lp, i, ndx, val); for (t = 1; t <= len; t++) xfprintf(fp, "%*.*g %s\n", DBL_DIG+7, DBL_DIG, val[t], col_name(lp, ndx[t], name)); } xfree(ndx); xfree(val); xfprintf(fp, "\n"); xfprintf(fp, "*** COLUMNS (VARIABLES) ***\n"); ndx = xcalloc(1+m, sizeof(int)); val = xcalloc(1+m, sizeof(double)); for (j = 1; j <= n; j++) { xfprintf(fp, "\n"); xfprintf(fp, "Col %d: %s", j, col_name(lp, j, name)); if (mip) { switch (lpx_get_col_kind(lp, j)) { case LPX_CV: break; case LPX_IV: xfprintf(fp, " integer"); break; default: xassert(lp != lp); } } lpx_get_col_bnds(lp, j, &type, &lb, &ub); switch (type) { case LPX_FR: xfprintf(fp, " free"); break; case LPX_LO: xfprintf(fp, " >= %.*g", DBL_DIG, lb); break; case LPX_UP: xfprintf(fp, " <= %.*g", DBL_DIG, ub); break; case LPX_DB: xfprintf(fp, " >= %.*g <= %.*g", DBL_DIG, lb, DBL_DIG, ub); break; case LPX_FX: xfprintf(fp, " = %.*g", DBL_DIG, lb); break; default: xassert(type != type); } xfprintf(fp, "\n"); coef = lpx_get_obj_coef(lp, j); if (coef != 0.0) xfprintf(fp, "%*.*g %s\n", DBL_DIG+7, DBL_DIG, coef, "(objective)"); len = lpx_get_mat_col(lp, j, ndx, val); for (t = 1; t <= len; t++) xfprintf(fp, "%*.*g %s\n", DBL_DIG+7, DBL_DIG, val[t], row_name(lp, ndx[t], name)); } xfree(ndx); xfree(val); xfprintf(fp, "\n"); xfprintf(fp, "End of output\n"); xfflush(fp); if (xferror(fp)) { xprintf("lpx_write_prob: write error on `%s' - %s\n", fname, strerror(errno)); goto fail; } xfclose(fp); return 0; fail: if (fp != NULL) xfclose(fp); return 1; } #undef row_name #undef col_name /*---------------------------------------------------------------------- -- lpx_print_sol - write LP problem solution in printable format. -- -- *Synopsis* -- -- #include "glplpx.h" -- int lpx_print_sol(LPX *lp, char *fname); -- -- *Description* -- -- The routine lpx_print_sol writes the current basic solution of an LP -- problem, which is specified by the pointer lp, to a text file, whose -- name is the character string fname, in printable format. -- -- Information reported by the routine lpx_print_sol is intended mainly -- for visual analysis. -- -- *Returns* -- -- If the operation was successful, the routine returns zero. Otherwise -- the routine prints an error message and returns non-zero. */ int lpx_print_sol(LPX *lp, const char *fname) { XFILE *fp; int what, round; xprintf( "lpx_print_sol: writing LP problem solution to `%s'...\n", fname); fp = xfopen(fname, "w"); if (fp == NULL) { xprintf("lpx_print_sol: can't create `%s' - %s\n", fname, strerror(errno)); goto fail; } /* problem name */ { const char *name; name = lpx_get_prob_name(lp); if (name == NULL) name = ""; xfprintf(fp, "%-12s%s\n", "Problem:", name); } /* number of rows (auxiliary variables) */ { int nr; nr = lpx_get_num_rows(lp); xfprintf(fp, "%-12s%d\n", "Rows:", nr); } /* number of columns (structural variables) */ { int nc; nc = lpx_get_num_cols(lp); xfprintf(fp, "%-12s%d\n", "Columns:", nc); } /* number of non-zeros (constraint coefficients) */ { int nz; nz = lpx_get_num_nz(lp); xfprintf(fp, "%-12s%d\n", "Non-zeros:", nz); } /* solution status */ { int status; status = lpx_get_status(lp); xfprintf(fp, "%-12s%s\n", "Status:", status == LPX_OPT ? "OPTIMAL" : status == LPX_FEAS ? "FEASIBLE" : status == LPX_INFEAS ? "INFEASIBLE (INTERMEDIATE)" : status == LPX_NOFEAS ? "INFEASIBLE (FINAL)" : status == LPX_UNBND ? "UNBOUNDED" : status == LPX_UNDEF ? "UNDEFINED" : "???"); } /* objective function */ { char *name; int dir; double obj; name = (void *)lpx_get_obj_name(lp); dir = lpx_get_obj_dir(lp); obj = lpx_get_obj_val(lp); xfprintf(fp, "%-12s%s%s%.10g %s\n", "Objective:", name == NULL ? "" : name, name == NULL ? "" : " = ", obj, dir == LPX_MIN ? "(MINimum)" : dir == LPX_MAX ? "(MAXimum)" : "(" "???" ")"); } /* main sheet */ for (what = 1; what <= 2; what++) { int mn, ij; xfprintf(fp, "\n"); xfprintf(fp, " No. %-12s St Activity Lower bound Upp" "er bound Marginal\n", what == 1 ? " Row name" : "Column name"); xfprintf(fp, "------ ------------ -- ------------- -----------" "-- ------------- -------------\n"); mn = (what == 1 ? lpx_get_num_rows(lp) : lpx_get_num_cols(lp)); for (ij = 1; ij <= mn; ij++) { const char *name; int typx, tagx; double lb, ub, vx, dx; if (what == 1) { name = lpx_get_row_name(lp, ij); if (name == NULL) name = ""; lpx_get_row_bnds(lp, ij, &typx, &lb, &ub); round = lpx_get_int_parm(lp, LPX_K_ROUND); lpx_set_int_parm(lp, LPX_K_ROUND, 1); lpx_get_row_info(lp, ij, &tagx, &vx, &dx); lpx_set_int_parm(lp, LPX_K_ROUND, round); } else { name = lpx_get_col_name(lp, ij); if (name == NULL) name = ""; lpx_get_col_bnds(lp, ij, &typx, &lb, &ub); round = lpx_get_int_parm(lp, LPX_K_ROUND); lpx_set_int_parm(lp, LPX_K_ROUND, 1); lpx_get_col_info(lp, ij, &tagx, &vx, &dx); lpx_set_int_parm(lp, LPX_K_ROUND, round); } /* row/column ordinal number */ xfprintf(fp, "%6d ", ij); /* row column/name */ if (strlen(name) <= 12) xfprintf(fp, "%-12s ", name); else xfprintf(fp, "%s\n%20s", name, ""); /* row/column status */ xfprintf(fp, "%s ", tagx == LPX_BS ? "B " : tagx == LPX_NL ? "NL" : tagx == LPX_NU ? "NU" : tagx == LPX_NF ? "NF" : tagx == LPX_NS ? "NS" : "??"); /* row/column primal activity */ xfprintf(fp, "%13.6g ", vx); /* row/column lower bound */ if (typx == LPX_LO || typx == LPX_DB || typx == LPX_FX) xfprintf(fp, "%13.6g ", lb); else xfprintf(fp, "%13s ", ""); /* row/column upper bound */ if (typx == LPX_UP || typx == LPX_DB) xfprintf(fp, "%13.6g ", ub); else if (typx == LPX_FX) xfprintf(fp, "%13s ", "="); else xfprintf(fp, "%13s ", ""); /* row/column dual activity */ if (tagx != LPX_BS) { if (dx == 0.0) xfprintf(fp, "%13s", "< eps"); else xfprintf(fp, "%13.6g", dx); } /* end of line */ xfprintf(fp, "\n"); } } xfprintf(fp, "\n"); #if 1 if (lpx_get_prim_stat(lp) != LPX_P_UNDEF && lpx_get_dual_stat(lp) != LPX_D_UNDEF) { int m = lpx_get_num_rows(lp); LPXKKT kkt; xfprintf(fp, "Karush-Kuhn-Tucker optimality conditions:\n\n"); lpx_check_kkt(lp, 1, &kkt); xfprintf(fp, "KKT.PE: max.abs.err. = %.2e on row %d\n", kkt.pe_ae_max, kkt.pe_ae_row); xfprintf(fp, " max.rel.err. = %.2e on row %d\n", kkt.pe_re_max, kkt.pe_re_row); switch (kkt.pe_quality) { case 'H': xfprintf(fp, " High quality\n"); break; case 'M': xfprintf(fp, " Medium quality\n"); break; case 'L': xfprintf(fp, " Low quality\n"); break; default: xfprintf(fp, " PRIMAL SOLUTION IS WRONG\n"); break; } xfprintf(fp, "\n"); xfprintf(fp, "KKT.PB: max.abs.err. = %.2e on %s %d\n", kkt.pb_ae_max, kkt.pb_ae_ind <= m ? "row" : "column", kkt.pb_ae_ind <= m ? kkt.pb_ae_ind : kkt.pb_ae_ind - m); xfprintf(fp, " max.rel.err. = %.2e on %s %d\n", kkt.pb_re_max, kkt.pb_re_ind <= m ? "row" : "column", kkt.pb_re_ind <= m ? kkt.pb_re_ind : kkt.pb_re_ind - m); switch (kkt.pb_quality) { case 'H': xfprintf(fp, " High quality\n"); break; case 'M': xfprintf(fp, " Medium quality\n"); break; case 'L': xfprintf(fp, " Low quality\n"); break; default: xfprintf(fp, " PRIMAL SOLUTION IS INFEASIBLE\n"); break; } xfprintf(fp, "\n"); xfprintf(fp, "KKT.DE: max.abs.err. = %.2e on column %d\n", kkt.de_ae_max, kkt.de_ae_col); xfprintf(fp, " max.rel.err. = %.2e on column %d\n", kkt.de_re_max, kkt.de_re_col); switch (kkt.de_quality) { case 'H': xfprintf(fp, " High quality\n"); break; case 'M': xfprintf(fp, " Medium quality\n"); break; case 'L': xfprintf(fp, " Low quality\n"); break; default: xfprintf(fp, " DUAL SOLUTION IS WRONG\n"); break; } xfprintf(fp, "\n"); xfprintf(fp, "KKT.DB: max.abs.err. = %.2e on %s %d\n", kkt.db_ae_max, kkt.db_ae_ind <= m ? "row" : "column", kkt.db_ae_ind <= m ? kkt.db_ae_ind : kkt.db_ae_ind - m); xfprintf(fp, " max.rel.err. = %.2e on %s %d\n", kkt.db_re_max, kkt.db_re_ind <= m ? "row" : "column", kkt.db_re_ind <= m ? kkt.db_re_ind : kkt.db_re_ind - m); switch (kkt.db_quality) { case 'H': xfprintf(fp, " High quality\n"); break; case 'M': xfprintf(fp, " Medium quality\n"); break; case 'L': xfprintf(fp, " Low quality\n"); break; default: xfprintf(fp, " DUAL SOLUTION IS INFEASIBLE\n"); break; } xfprintf(fp, "\n"); } #endif #if 1 if (lpx_get_status(lp) == LPX_UNBND) { int m = lpx_get_num_rows(lp); int k = lpx_get_ray_info(lp); xfprintf(fp, "Unbounded ray: %s %d\n", k <= m ? "row" : "column", k <= m ? k : k - m); xfprintf(fp, "\n"); } #endif xfprintf(fp, "End of output\n"); xfflush(fp); if (xferror(fp)) { xprintf("lpx_print_sol: can't write to `%s' - %s\n", fname, strerror(errno)); goto fail; } xfclose(fp); return 0; fail: if (fp != NULL) xfclose(fp); return 1; }
int lpx_print_ips(LPX *lp, const char *fname) { XFILE *fp; int what, round; xprintf("lpx_print_ips: writing LP problem solution to `%s'...\n", fname); fp = xfopen(fname, "w"); if (fp == NULL) { xprintf("lpx_print_ips: can't create `%s' - %s\n", fname, strerror(errno)); goto fail; } /* problem name */ { const char *name; name = lpx_get_prob_name(lp); if (name == NULL) name = ""; xfprintf(fp, "%-12s%s\n", "Problem:", name); } /* number of rows (auxiliary variables) */ { int nr; nr = lpx_get_num_rows(lp); xfprintf(fp, "%-12s%d\n", "Rows:", nr); } /* number of columns (structural variables) */ { int nc; nc = lpx_get_num_cols(lp); xfprintf(fp, "%-12s%d\n", "Columns:", nc); } /* number of non-zeros (constraint coefficients) */ { int nz; nz = lpx_get_num_nz(lp); xfprintf(fp, "%-12s%d\n", "Non-zeros:", nz); } /* solution status */ { int status; status = lpx_ipt_status(lp); xfprintf(fp, "%-12s%s\n", "Status:", status == LPX_T_UNDEF ? "INTERIOR UNDEFINED" : status == LPX_T_OPT ? "INTERIOR OPTIMAL" : "???"); } /* objective function */ { char *name; int dir; double obj; name = (void *)lpx_get_obj_name(lp); dir = lpx_get_obj_dir(lp); obj = lpx_ipt_obj_val(lp); xfprintf(fp, "%-12s%s%s%.10g %s\n", "Objective:", name == NULL ? "" : name, name == NULL ? "" : " = ", obj, dir == LPX_MIN ? "(MINimum)" : dir == LPX_MAX ? "(MAXimum)" : "(" "???" ")"); } /* main sheet */ for (what = 1; what <= 2; what++) { int mn, ij; xfprintf(fp, "\n"); xfprintf(fp, " No. %-12s Activity Lower bound Upp" "er bound Marginal\n", what == 1 ? " Row name" : "Column name"); xfprintf(fp, "------ ------------ ------------- -----------" "-- ------------- -------------\n"); mn = (what == 1 ? lpx_get_num_rows(lp) : lpx_get_num_cols(lp)); for (ij = 1; ij <= mn; ij++) { const char *name; int typx /*, tagx */; double lb, ub, vx, dx; if (what == 1) { name = lpx_get_row_name(lp, ij); if (name == NULL) name = ""; lpx_get_row_bnds(lp, ij, &typx, &lb, &ub); round = lpx_get_int_parm(lp, LPX_K_ROUND); lpx_set_int_parm(lp, LPX_K_ROUND, 1); vx = lpx_ipt_row_prim(lp, ij); dx = lpx_ipt_row_dual(lp, ij); lpx_set_int_parm(lp, LPX_K_ROUND, round); } else { name = lpx_get_col_name(lp, ij); if (name == NULL) name = ""; lpx_get_col_bnds(lp, ij, &typx, &lb, &ub); round = lpx_get_int_parm(lp, LPX_K_ROUND); lpx_set_int_parm(lp, LPX_K_ROUND, 1); vx = lpx_ipt_col_prim(lp, ij); dx = lpx_ipt_col_dual(lp, ij); lpx_set_int_parm(lp, LPX_K_ROUND, round); } /* row/column ordinal number */ xfprintf(fp, "%6d ", ij); /* row column/name */ if (strlen(name) <= 12) xfprintf(fp, "%-12s ", name); else xfprintf(fp, "%s\n%20s", name, ""); /* two positions are currently not used */ xfprintf(fp, " "); /* row/column primal activity */ xfprintf(fp, "%13.6g ", vx); /* row/column lower bound */ if (typx == LPX_LO || typx == LPX_DB || typx == LPX_FX) xfprintf(fp, "%13.6g ", lb); else xfprintf(fp, "%13s ", ""); /* row/column upper bound */ if (typx == LPX_UP || typx == LPX_DB) xfprintf(fp, "%13.6g ", ub); else if (typx == LPX_FX) xfprintf(fp, "%13s ", "="); else xfprintf(fp, "%13s ", ""); /* row/column dual activity */ xfprintf(fp, "%13.6g", dx); /* end of line */ xfprintf(fp, "\n"); } } xfprintf(fp, "\n"); xfprintf(fp, "End of output\n"); xfflush(fp); if (xferror(fp)) { xprintf("lpx_print_ips: can't write to `%s' - %s\n", fname, strerror(errno)); goto fail; } xfclose(fp); return 0; fail: if (fp != NULL) xfclose(fp); return 1; }
int lpx_print_mip(LPX *lp, const char *fname) { XFILE *fp; int what, round; #if 0 if (lpx_get_class(lp) != LPX_MIP) fault("lpx_print_mip: error -- not a MIP problem"); #endif xprintf( "lpx_print_mip: writing MIP problem solution to `%s'...\n", fname); fp = xfopen(fname, "w"); if (fp == NULL) { xprintf("lpx_print_mip: can't create `%s' - %s\n", fname, strerror(errno)); goto fail; } /* problem name */ { const char *name; name = lpx_get_prob_name(lp); if (name == NULL) name = ""; xfprintf(fp, "%-12s%s\n", "Problem:", name); } /* number of rows (auxiliary variables) */ { int nr; nr = lpx_get_num_rows(lp); xfprintf(fp, "%-12s%d\n", "Rows:", nr); } /* number of columns (structural variables) */ { int nc, nc_int, nc_bin; nc = lpx_get_num_cols(lp); nc_int = lpx_get_num_int(lp); nc_bin = lpx_get_num_bin(lp); xfprintf(fp, "%-12s%d (%d integer, %d binary)\n", "Columns:", nc, nc_int, nc_bin); } /* number of non-zeros (constraint coefficients) */ { int nz; nz = lpx_get_num_nz(lp); xfprintf(fp, "%-12s%d\n", "Non-zeros:", nz); } /* solution status */ { int status; status = lpx_mip_status(lp); xfprintf(fp, "%-12s%s\n", "Status:", status == LPX_I_UNDEF ? "INTEGER UNDEFINED" : status == LPX_I_OPT ? "INTEGER OPTIMAL" : status == LPX_I_FEAS ? "INTEGER NON-OPTIMAL" : status == LPX_I_NOFEAS ? "INTEGER EMPTY" : "???"); } /* objective function */ { char *name; int dir; double mip_obj; name = (void *)lpx_get_obj_name(lp); dir = lpx_get_obj_dir(lp); mip_obj = lpx_mip_obj_val(lp); xfprintf(fp, "%-12s%s%s%.10g %s\n", "Objective:", name == NULL ? "" : name, name == NULL ? "" : " = ", mip_obj, dir == LPX_MIN ? "(MINimum)" : dir == LPX_MAX ? "(MAXimum)" : "(" "???" ")"); } /* main sheet */ for (what = 1; what <= 2; what++) { int mn, ij; xfprintf(fp, "\n"); xfprintf(fp, " No. %-12s Activity Lower bound Upp" "er bound\n", what == 1 ? " Row name" : "Column name"); xfprintf(fp, "------ ------------ ------------- -----------" "-- -------------\n"); mn = (what == 1 ? lpx_get_num_rows(lp) : lpx_get_num_cols(lp)); for (ij = 1; ij <= mn; ij++) { const char *name; int kind, typx; double lb, ub, vx; if (what == 1) { name = lpx_get_row_name(lp, ij); if (name == NULL) name = ""; kind = LPX_CV; lpx_get_row_bnds(lp, ij, &typx, &lb, &ub); round = lpx_get_int_parm(lp, LPX_K_ROUND); lpx_set_int_parm(lp, LPX_K_ROUND, 1); vx = lpx_mip_row_val(lp, ij); lpx_set_int_parm(lp, LPX_K_ROUND, round); } else { name = lpx_get_col_name(lp, ij); if (name == NULL) name = ""; kind = lpx_get_col_kind(lp, ij); lpx_get_col_bnds(lp, ij, &typx, &lb, &ub); round = lpx_get_int_parm(lp, LPX_K_ROUND); lpx_set_int_parm(lp, LPX_K_ROUND, 1); vx = lpx_mip_col_val(lp, ij); lpx_set_int_parm(lp, LPX_K_ROUND, round); } /* row/column ordinal number */ xfprintf(fp, "%6d ", ij); /* row column/name */ if (strlen(name) <= 12) xfprintf(fp, "%-12s ", name); else xfprintf(fp, "%s\n%20s", name, ""); /* row/column kind */ xfprintf(fp, "%s ", kind == LPX_CV ? " " : kind == LPX_IV ? "*" : "?"); /* row/column primal activity */ xfprintf(fp, "%13.6g", vx); /* row/column lower and upper bounds */ switch (typx) { case LPX_FR: break; case LPX_LO: xfprintf(fp, " %13.6g", lb); break; case LPX_UP: xfprintf(fp, " %13s %13.6g", "", ub); break; case LPX_DB: xfprintf(fp, " %13.6g %13.6g", lb, ub); break; case LPX_FX: xfprintf(fp, " %13.6g %13s", lb, "="); break; default: xassert(typx != typx); } /* end of line */ xfprintf(fp, "\n"); } } xfprintf(fp, "\n"); #if 1 if (lpx_mip_status(lp) != LPX_I_UNDEF) { int m = lpx_get_num_rows(lp); LPXKKT kkt; xfprintf(fp, "Integer feasibility conditions:\n\n"); lpx_check_int(lp, &kkt); xfprintf(fp, "INT.PE: max.abs.err. = %.2e on row %d\n", kkt.pe_ae_max, kkt.pe_ae_row); xfprintf(fp, " max.rel.err. = %.2e on row %d\n", kkt.pe_re_max, kkt.pe_re_row); switch (kkt.pe_quality) { case 'H': xfprintf(fp, " High quality\n"); break; case 'M': xfprintf(fp, " Medium quality\n"); break; case 'L': xfprintf(fp, " Low quality\n"); break; default: xfprintf(fp, " SOLUTION IS WRONG\n"); break; } xfprintf(fp, "\n"); xfprintf(fp, "INT.PB: max.abs.err. = %.2e on %s %d\n", kkt.pb_ae_max, kkt.pb_ae_ind <= m ? "row" : "column", kkt.pb_ae_ind <= m ? kkt.pb_ae_ind : kkt.pb_ae_ind - m); xfprintf(fp, " max.rel.err. = %.2e on %s %d\n", kkt.pb_re_max, kkt.pb_re_ind <= m ? "row" : "column", kkt.pb_re_ind <= m ? kkt.pb_re_ind : kkt.pb_re_ind - m); switch (kkt.pb_quality) { case 'H': xfprintf(fp, " High quality\n"); break; case 'M': xfprintf(fp, " Medium quality\n"); break; case 'L': xfprintf(fp, " Low quality\n"); break; default: xfprintf(fp, " SOLUTION IS INFEASIBLE\n"); break; } xfprintf(fp, "\n"); } #endif xfprintf(fp, "End of output\n"); xfflush(fp); if (xferror(fp)) { xprintf("lpx_print_mip: can't write to `%s' - %s\n", fname, strerror(errno)); goto fail; } xfclose(fp); return 0; fail: if (fp != NULL) xfclose(fp); return 1; }
int lpx_print_sens_bnds(LPX *lp, char *fname) { FILE *fp = NULL; int what, round; print("lpx_print_sens_bnds: writing LP problem solution bounds to" " `%s'...", fname); #if 1 /* added by mao */ /* this routine needs factorization of the current basis matrix which, however, does not exist if the basic solution was obtained by the lp presolver; therefore we should warm up the basis to be sure that the factorization is valid (note that if the factorization exists, lpx_warm_up does nothing) */ lpx_warm_up(lp); #endif #if 0 /* 21/XII-2003 by mao */ if (lp->b_stat == LPX_B_UNDEF) #else if (!lpx_is_b_avail(lp)) #endif { print("lpx_print_sens_bnds: basis information not available (m" "ay be a presolve issue)"); goto fail; } fp = ufopen(fname, "w"); if (fp == NULL) { print("lpx_print_sens_bnds: can't create `%s' - %s", fname, strerror(errno)); goto fail; } /* problem name */ { char *name; name = lpx_get_prob_name(lp); if (name == NULL) name = ""; fprintf(fp, "%-12s%s\n", "Problem:", name); } /* number of rows (auxiliary variables) */ { int nr; nr = lpx_get_num_rows(lp); fprintf(fp, "%-12s%d\n", "Rows:", nr); } /* number of columns (structural variables) */ { int nc; nc = lpx_get_num_cols(lp); fprintf(fp, "%-12s%d\n", "Columns:", nc); } /* number of non-zeros (constraint coefficients) */ { int nz; nz = lpx_get_num_nz(lp); fprintf(fp, "%-12s%d\n", "Non-zeros:", nz); } /* solution status */ { int status; status = lpx_get_status(lp); fprintf(fp, "%-12s%s\n", "Status:", status == LPX_OPT ? "OPTIMAL" : status == LPX_FEAS ? "FEASIBLE" : status == LPX_INFEAS ? "INFEASIBLE (INTERMEDIATE)" : status == LPX_NOFEAS ? "INFEASIBLE (FINAL)" : status == LPX_UNBND ? "UNBOUNDED" : status == LPX_UNDEF ? "UNDEFINED" : "???"); } /* explanation/warning */ { fprintf(fp, "\nExplanation: This file presents amounts by whi" "ch objective coefficients,\n"); fprintf(fp, "constraint bounds, and variable bounds may be cha" "nged in the original problem\n"); fprintf(fp, "while the optimal basis remains the same. Note t" "hat the optimal solution\n"); fprintf(fp, "and objective value may change even though the ba" "sis remains the same.\n"); fprintf(fp, "These bounds assume that all parameters remain fi" "xed except the one in\n"); fprintf(fp, "question. If more than one parameter is changed," " it is possible for the\n"); fprintf(fp, "optimal basis to change even though each paramete" "r stays within its bounds.\n"); fprintf(fp, "For more details, consult a text on linear progra" "mming.\n"); } /* Sensitivity ranges if solution was optimal */ { int status; status = lpx_get_status(lp); if (status == LPX_OPT) { int i,j,k,m,n; int dir; double max_inc, max_dec; int *index; double *val; fprintf(fp, "\nObjective Coefficient Analysis\n"); fprintf(fp, " No. Column name St Value Max incr" "ease Max decrease\n"); fprintf(fp, "------ ------------ -- ------------- ---------" "---- ------------- \n"); n = lpx_get_num_cols(lp); m = lpx_get_num_rows(lp); dir = lpx_get_obj_dir(lp); /* allocate memory for index and val arrays */ index = ucalloc(1+n+m, sizeof(int)); val = ucalloc(1+n+m, sizeof(double)); for (j = 1; j <= n; j++) { char *name; int typx, tagx; double lb, ub, vx, dx; name = lpx_get_col_name(lp, j); if (name == NULL) name = ""; lpx_get_col_bnds(lp, j, &typx, &lb, &ub); #if 0 /* 21/XII-2003 by mao */ round = lp->round, lp->round = 1; lpx_get_col_info(lp, j, &tagx, &vx, &dx); lp->round = round; #else round = lpx_get_int_parm(lp, LPX_K_ROUND); lpx_set_int_parm(lp, LPX_K_ROUND, 1); lpx_get_col_info(lp, j, &tagx, &vx, &dx); lpx_set_int_parm(lp, LPX_K_ROUND, round); #endif /* row/column ordinal number */ fprintf(fp, "%6d ", j); /* row column/name */ if (strlen(name) <= 12) fprintf(fp, "%-12s ", name); else fprintf(fp, "%s\n%20s", name, ""); /* row/column status */ fprintf(fp, "%s ", tagx == LPX_BS ? "B " : tagx == LPX_NL ? "NL" : tagx == LPX_NU ? "NU" : tagx == LPX_NF ? "NF" : tagx == LPX_NS ? "NS" : "??"); /* objective coefficient */ fprintf(fp, "%13.6g ", lpx_get_obj_coef(lp, j)); if (tagx == LPX_NL) { if (dir==LPX_MIN) { /* reduced cost must be positive */ max_inc = DBL_MAX; /* really represents infinity */ max_dec = dx; } else { /* reduced cost must be negative */ max_inc = -dx; max_dec = DBL_MAX; /* means infinity */ } } if (tagx == LPX_NU) { if (dir==LPX_MIN) { /* reduced cost must be negative */ max_inc = -dx; max_dec = DBL_MAX; } else { max_inc = DBL_MAX; max_dec = dx; } } if (tagx == LPX_NF) { /* can't change nonbasic free variables' cost */ max_inc = 0.0; max_dec = 0.0; } if (tagx == LPX_NS) { /* doesn't matter what happens to the cost */ max_inc = DBL_MAX; max_dec = DBL_MAX; } if (tagx == LPX_BS) { int len; /* We need to see how this objective coefficient affects reduced costs of other variables */ len = lpx_eval_tab_row(lp, m+j, index, val); max_inc = DBL_MAX; max_dec = DBL_MAX; for (i = 1; i <= len; i++) { /*int stat;*/ int tagx2; double vx2, dx2; double delta; if (index[i]>m) lpx_get_col_info(lp, index[i]-m, &tagx2, &vx2, &dx2); else lpx_get_row_info(lp, index[i], &tagx2, &vx2, &dx2); if (tagx2 == LPX_NL) { if (val[i] != 0.0) { delta = dx2 / val[i]; if (delta < 0 && -delta < max_inc) max_inc = -delta; else if (delta >0 && delta < max_dec) max_dec = delta; } } if (tagx2 == LPX_NU) { if (val[i] != 0.0) { delta = dx2 / val[i]; if (delta < 0 && -delta < max_inc) max_inc = -delta; else if (delta > 0 && delta < max_dec) max_dec = delta; } } if (tagx2 == LPX_NF) { if (val[i] != 0.0) { max_inc = 0.0; max_dec = 0.0; } } } } if (max_inc == -0.0) max_inc = 0.0; if (max_dec == -0.0) max_dec = 0.0; if (max_inc == DBL_MAX) fprintf(fp, "%13s ", "infinity"); else if (max_inc < 1.0e-12 && max_inc > 0) fprintf(fp, "%13s ", "< eps"); else fprintf(fp, "%13.6g ", max_inc); if (max_dec == DBL_MAX) fprintf(fp, "%13s ", "infinity"); else if (max_dec < 1.0e-12 && max_dec > 0) fprintf(fp, "%13s ", "< eps"); else fprintf(fp, "%13.6g ", max_dec); fprintf(fp, "\n"); } for (what = 1; what <= 2; what++) { int ij, mn; fprintf(fp, "\n"); fprintf(fp, "%s Analysis\n", what==1? "Constraint Bounds":"Variable Bounds"); fprintf(fp, " No. %12s St Value Max increase " " Max decrease\n", what==1 ? " Row name":"Column name"); fprintf(fp, "------ ------------ -- ------------- ------" "------- ------------- \n"); mn = what==1 ? m : n; for (ij = 1; ij <= mn; ij++) { char *name; int typx, tagx; double lb, ub, vx, dx; if (what==1) name = lpx_get_row_name(lp, ij); else name = lpx_get_col_name(lp, ij); if (name == NULL) name = ""; #if 0 /* 21/XII-2003 by mao */ if (what==1) { lpx_get_row_bnds(lp, ij, &typx, &lb, &ub); round = lp->round, lp->round = 1; lpx_get_row_info(lp, ij, &tagx, &vx, &dx); lp->round = round; } else { lpx_get_col_bnds(lp, ij, &typx, &lb, &ub); round = lp->round, lp->round = 1; lpx_get_col_info(lp, ij, &tagx, &vx, &dx); lp->round = round; } #else round = lpx_get_int_parm(lp, LPX_K_ROUND); lpx_set_int_parm(lp, LPX_K_ROUND, 1); if (what==1) { lpx_get_row_bnds(lp, ij, &typx, &lb, &ub); lpx_get_row_info(lp, ij, &tagx, &vx, &dx); } else { lpx_get_col_bnds(lp, ij, &typx, &lb, &ub); lpx_get_col_info(lp, ij, &tagx, &vx, &dx); } lpx_set_int_parm(lp, LPX_K_ROUND, round); #endif /* row/column ordinal number */ fprintf(fp, "%6d ", ij); /* row column/name */ if (strlen(name) <= 12) fprintf(fp, "%-12s ", name); else fprintf(fp, "%s\n%20s", name, ""); /* row/column status */ fprintf(fp, "%s ", tagx == LPX_BS ? "B " : tagx == LPX_NL ? "NL" : tagx == LPX_NU ? "NU" : tagx == LPX_NF ? "NF" : tagx == LPX_NS ? "NS" : "??"); fprintf(fp, "\n"); /* first check lower bound */ if (typx == LPX_LO || typx == LPX_DB || typx == LPX_FX) { int at_lower; at_lower = 0; if (tagx == LPX_BS || tagx == LPX_NU) { max_inc = vx - lb; max_dec = DBL_MAX; } if (tagx == LPX_NS) { max_inc = 0.0; max_dec = 0.0; if (dir == LPX_MIN && dx > 0) at_lower = 1; if (dir == LPX_MAX && dx < 0) at_lower = 1; } if (tagx == LPX_NL || at_lower == 1) { int len; /* we have to see how it affects basic variables */ len = lpx_eval_tab_col(lp, what==1?ij:ij+m, index, val); k = lpx_prim_ratio_test(lp, len, index, val, 1, 10e-7); max_inc = DBL_MAX; if (k != 0) { /*int stat;*/ int tagx2, typx2; double vx2, dx2, lb2, ub2; /*double delta;*/ double alpha; int l; for (l = 1; l <= len; l++) if (index[l] == k) alpha = val[l]; if (k>m) { lpx_get_col_info(lp, k-m, &tagx2, &vx2, &dx2); lpx_get_col_bnds(lp, k-m, &typx2, &lb2, &ub2); } else { lpx_get_row_info(lp, k, &tagx2, &vx2, &dx2); lpx_get_row_bnds(lp, k, &typx2, &lb2, &ub2); } /* Check which direction; remember this is upper bound */ if (alpha > 0) max_inc = (ub2 - vx2)/ alpha; else max_inc = (lb2 - vx2)/ alpha; } /* now check lower bound */ k = lpx_prim_ratio_test(lp, len, index, val, -1, 10e-7); max_dec = DBL_MAX; if (k != 0) { /*int stat;*/ int tagx2, typx2; double vx2, dx2, lb2, ub2; /*double delta;*/ double alpha; int l; for (l = 1; l <= len; l++) if (index[l] == k) alpha = val[l]; if (k>m) { lpx_get_col_info(lp, k-m, &tagx2, &vx2, &dx2); lpx_get_col_bnds(lp, k-m, &typx2, &lb2, &ub2); } else { lpx_get_row_info(lp, k, &tagx2, &vx2, &dx2); lpx_get_row_bnds(lp, k, &typx2, &lb2, &ub2); } /* Check which direction; remember this is lower bound */ if (alpha > 0) max_dec = (vx2 - lb2)/ alpha; else max_dec = (vx2 - ub2)/ alpha; } } /* bound */ if (typx == LPX_DB || typx == LPX_FX) { if (max_inc > ub - lb) max_inc = ub - lb; } fprintf(fp, " LOWER %13.6g ", lb); if (max_inc == -0.0) max_inc = 0.0; if (max_dec == -0.0) max_dec = 0.0; if (max_inc == DBL_MAX) fprintf(fp, "%13s ", "infinity"); else if (max_inc < 1.0e-12 && max_inc > 0) fprintf(fp, "%13s ", "< eps"); else fprintf(fp, "%13.6g ", max_inc); if (max_dec == DBL_MAX) fprintf(fp, "%13s ", "infinity"); else if (max_dec < 1.0e-12 && max_dec > 0) fprintf(fp, "%13s ", "< eps"); else fprintf(fp, "%13.6g ", max_dec); fprintf(fp, "\n"); } /* now check upper bound */ if (typx == LPX_UP || typx == LPX_DB || typx == LPX_FX) { int at_upper; at_upper = 0; if (tagx == LPX_BS || tagx == LPX_NL) { max_inc = DBL_MAX; max_dec = ub - vx; } if (tagx == LPX_NS) { max_inc = 0.0; max_dec = 0.0; if (dir == LPX_MIN && dx < 0) at_upper = 1; if (dir == LPX_MAX && dx > 0) at_upper = 1; } if (tagx == LPX_NU || at_upper == 1) { int len; /* we have to see how it affects basic variables */ len = lpx_eval_tab_col(lp, what==1?ij:ij+m, index, val); k = lpx_prim_ratio_test(lp, len, index, val, 1, 10e-7); max_inc = DBL_MAX; if (k != 0) { /*int stat;*/ int tagx2, typx2; double vx2, dx2, lb2, ub2; /*double delta;*/ double alpha; int l; for (l = 1; l <= len; l++) if (index[l] == k) alpha = val[l]; if (k>m) { lpx_get_col_info(lp, k-m, &tagx2, &vx2, &dx2); lpx_get_col_bnds(lp, k-m, &typx2, &lb2, &ub2); } else { lpx_get_row_info(lp, k, &tagx2, &vx2, &dx2); lpx_get_row_bnds(lp, k, &typx2, &lb2, &ub2); } /* Check which direction; remember this is upper bound */ if (alpha > 0) max_inc = (ub2 - vx2)/ alpha; else max_inc = (lb2 - vx2)/ alpha; } /* now check lower bound */ k = lpx_prim_ratio_test(lp, len, index, val, -1, 10e-7); max_dec = DBL_MAX; if (k != 0) { /*int stat;*/ int tagx2, typx2; double vx2, dx2, lb2, ub2; /*double delta;*/ double alpha; int l; for (l = 1; l <= len; l++) if (index[l] == k) alpha = val[l]; if (k>m) { lpx_get_col_info(lp, k-m, &tagx2, &vx2, &dx2); lpx_get_col_bnds(lp, k-m, &typx2, &lb2, &ub2); } else { lpx_get_row_info(lp, k, &tagx2, &vx2, &dx2); lpx_get_row_bnds(lp, k, &typx2, &lb2, &ub2); } /* Check which direction; remember this is lower bound */ if (alpha > 0) max_dec = (vx2 - lb2)/ alpha; else max_dec = (vx2 - ub2)/ alpha; } } if (typx == LPX_DB || typx == LPX_FX) { if (max_dec > ub - lb) max_dec = ub - lb; } /* bound */ fprintf(fp, " UPPER %13.6g ", ub); if (max_inc == -0.0) max_inc = 0.0; if (max_dec == -0.0) max_dec = 0.0; if (max_inc == DBL_MAX) fprintf(fp, "%13s ", "infinity"); else if (max_inc < 1.0e-12 && max_inc > 0) fprintf(fp, "%13s ", "< eps"); else fprintf(fp, "%13.6g ", max_inc); if (max_dec == DBL_MAX) fprintf(fp, "%13s ", "infinity"); else if (max_dec < 1.0e-12 && max_dec > 0) fprintf(fp, "%13s ", "< eps"); else fprintf(fp, "%13.6g ", max_dec); fprintf(fp, "\n"); } } } /* free the memory we used */ ufree(index); ufree(val); } else fprintf(fp, "No range information since solution is not o" "ptimal.\n"); } fprintf(fp, "\n"); fprintf(fp, "End of output\n"); fflush(fp); if (ferror(fp)) { print("lpx_print_sens_bnds: can't write to `%s' - %s", fname, strerror(errno)); goto fail; } ufclose(fp); return 0; fail: if (fp != NULL) ufclose(fp); return 1; }