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 main(int argc, char *argv[]) { LPX *lp; MPL *mpl = NULL; int ret; double start; /* parse command line parameters */ parse_cmdline(argc, argv); /* remove all output files specified in the command line */ if (display != NULL) remove(display); if (out_sol != NULL) remove(out_sol); if (out_bnds != NULL) remove(out_bnds); if (out_mps != NULL) remove(out_mps); if (out_lpt != NULL) remove(out_lpt); if (out_txt != NULL) remove(out_txt); if (out_glp != NULL) remove(out_glp); /* read problem from the input file */ if (in_file == NULL) { print("No input file specified; try %s --help", argv[0]); exit(EXIT_FAILURE); } switch (format) { case 0: lp = lpx_read_mps(in_file); if (lp == NULL) { print("MPS file processing error"); exit(EXIT_FAILURE); } break; case 1: lp = lpx_read_lpt(in_file); if (lp == NULL) { print("CPLEX LP file processing error"); exit(EXIT_FAILURE); } break; case 2: #if 0 /* 01/VIII-2004 */ lp = lpx_read_model(in_file, in_data, display); if (lp == NULL) { print("Model processing error"); exit(EXIT_FAILURE); } #else /* initialize the translator database */ mpl = mpl_initialize(); /* read model section and optional data section */ ret = mpl_read_model(mpl, in_file, in_data != NULL); if (ret == 4) err: { print("Model processing error"); exit(EXIT_FAILURE); } insist(ret == 1 || ret == 2); /* read data section, if necessary */ if (in_data != NULL) { insist(ret == 1); ret = mpl_read_data(mpl, in_data); if (ret == 4) goto err; insist(ret == 2); } /* generate model */ ret = mpl_generate(mpl, display); if (ret == 4) goto err; /* extract problem instance */ lp = lpx_extract_prob(mpl); insist(lp != NULL); #endif if (lpx_get_num_rows(lp) == 0) { print("Problem has no rows"); exit(EXIT_FAILURE); } if (lpx_get_num_cols(lp) == 0) { print("Problem has no columns"); exit(EXIT_FAILURE); } break; case 3: lp = lpx_read_prob(in_file); if (lp == NULL) { print("GNU LP file processing error"); exit(EXIT_FAILURE); } break; default: insist(format != format); } /* change problem name (if required) */ if (newname != NULL) lpx_set_prob_name(lp, newname); /* change optimization direction (if required) */ if (dir != 0) lpx_set_obj_dir(lp, dir); /* write problem in MPS format (if required) */ if (out_mps != NULL) { lpx_set_int_parm(lp, LPX_K_MPSORIG, orig); ret = lpx_write_mps(lp, out_mps); if (ret != 0) { print("Unable to write problem in MPS format"); exit(EXIT_FAILURE); } } /* write problem in CPLEX LP format (if required) */ if (out_lpt != NULL) { lpx_set_int_parm(lp, LPX_K_LPTORIG, orig); ret = lpx_write_lpt(lp, out_lpt); if (ret != 0) { print("Unable to write problem in CPLEX LP format"); exit(EXIT_FAILURE); } } /* write problem in plain text format (if required) */ if (out_txt != NULL) { lpx_set_int_parm(lp, LPX_K_LPTORIG, orig); ret = lpx_print_prob(lp, out_txt); if (ret != 0) { print("Unable to write problem in plain text format"); exit(EXIT_FAILURE); } } /* write problem in GNU LP format (if required) */ if (out_glp != NULL) { ret = lpx_write_prob(lp, out_glp); if (ret != 0) { print("Unable to write problem in GNU LP format"); exit(EXIT_FAILURE); } } /* if only data check is required, skip computations */ if (check) goto skip; /* scale the problem data (if required) */ if (scale && (!presol || method == 1)) lpx_scale_prob(lp); /* build advanced initial basis (if required) */ if (method == 0 && basis && !presol) lpx_adv_basis(lp); /* set some control parameters, which might be changed in the command line */ lpx_set_int_parm(lp, LPX_K_PRICE, price); if (!relax) lpx_set_real_parm(lp, LPX_K_RELAX, 0.0); lpx_set_int_parm(lp, LPX_K_PRESOL, presol); lpx_set_int_parm(lp, LPX_K_BRANCH, branch); lpx_set_int_parm(lp, LPX_K_BTRACK, btrack); lpx_set_real_parm(lp, LPX_K_TMLIM, (double)tmlim); /* solve the problem */ start = utime(); switch (method) { case 0: if (nomip || lpx_get_class(lp) == LPX_LP) { ret = lpx_simplex(lp); if (presol && ret != LPX_E_OK && out_sol != NULL) print("If you need actual output for non-optimal solu" "tion, use --nopresol"); } else { method = 2; lpx_simplex(lp); if (!intopt) lpx_integer(lp); else lpx_intopt(lp); } break; case 1: if (nomip || lpx_get_class(lp) == LPX_LP) lpx_interior(lp); else { print("Interior point method is not able to solve MIP pr" "oblem; use --simplex"); exit(EXIT_FAILURE); } break; default: insist(method != method); } /* display statistics */ print("Time used: %.1f secs", utime() - start); print("Memory used: %.1fM (%d bytes)", (double)lib_env_ptr()->mem_tpeak / (double)(1024 * 1024), lib_env_ptr()->mem_tpeak); #if 1 /* 01/VIII-2004 */ if (mpl != NULL && mpl_has_solve_stmt(mpl)) { int n, j, round; /* store the solution to the translator database */ n = lpx_get_num_cols(lp); round = lpx_get_int_parm(lp, LPX_K_ROUND); lpx_set_int_parm(lp, LPX_K_ROUND, 1); switch (method) { case 0: for (j = 1; j <= n; j++) mpl_put_col_value(mpl, j, lpx_get_col_prim(lp, j)); break; case 1: for (j = 1; j <= n; j++) mpl_put_col_value(mpl, j, lpx_ipt_col_prim(lp, j)); break; case 2: for (j = 1; j <= n; j++) mpl_put_col_value(mpl, j, lpx_mip_col_val(lp, j)); break; default: insist(method != method); } lpx_set_int_parm(lp, LPX_K_ROUND, round); /* perform postsolving */ ret = mpl_postsolve(mpl, display); if (ret == 4) { print("Model postsolving error"); exit(EXIT_FAILURE); } insist(ret == 3); } #endif /* write problem solution found by the solver (if required) */ if (out_sol != NULL) { switch (method) { case 0: ret = lpx_print_sol(lp, out_sol); break; case 1: ret = lpx_print_ips(lp, out_sol); break; case 2: ret = lpx_print_mip(lp, out_sol); break; default: insist(method != method); } if (ret != 0) { print("Unable to write problem solution"); exit(EXIT_FAILURE); } } /* write sensitivity bounds information (if required) */ if (out_bnds != NULL) { if (method != 0) { print("Cannot write sensitivity bounds information for inte" "rior-point or MIP solution"); exit(EXIT_FAILURE); } ret = lpx_print_sens_bnds(lp, out_bnds); if (ret != 0) { print("Unable to write sensitivity bounds information"); exit(EXIT_FAILURE); } } skip: /* delete the problem object */ lpx_delete_prob(lp); #if 1 /* 01/VIII-2004 */ /* if the translator database exists, destroy it */ if (mpl != NULL) mpl_terminate(mpl); #endif /* check that no memory blocks are still allocated */ insist(lib_env_ptr()->mem_total == 0); insist(lib_env_ptr()->mem_count == 0); /* return to the control program */ return 0; }
int main(int argc, char *argv[]) { LPX *lp; MPL *mpl = NULL; int ret; ulong_t start; /* parse command line parameters */ parse_cmdline(argc, argv); /* set available memory limit */ if (memlim >= 0) lib_mem_limit(ulmul(ulset(0, 1048576), ulset(0, memlim))); /* remove all output files specified in the command line */ if (display != NULL) remove(display); if (out_bas != NULL) remove(out_bas); if (out_sol != NULL) remove(out_sol); if (out_bnds != NULL) remove(out_bnds); if (out_mps != NULL) remove(out_mps); if (out_freemps != NULL) remove(out_freemps); if (out_cpxlp != NULL) remove(out_cpxlp); if (out_txt != NULL) remove(out_txt); if (out_glp != NULL) remove(out_glp); if (log_file != NULL) remove(log_file); /* open hardcopy file, if necessary */ if (log_file != NULL) { if (lib_open_log(log_file)) { print("Unable to create log file"); exit(EXIT_FAILURE); } } /* read problem data from the input file */ if (in_file == NULL) { print("No input file specified; try %s --help", argv[0]); exit(EXIT_FAILURE); } switch (format) { case 0: lp = lpx_read_mps(in_file); if (lp == NULL) { print("MPS file processing error"); exit(EXIT_FAILURE); } orig = 1; break; case 1: lp = lpx_read_cpxlp(in_file); if (lp == NULL) { print("CPLEX LP file processing error"); exit(EXIT_FAILURE); } break; case 2: /* initialize the translator database */ mpl = mpl_initialize(); /* read model section and optional data section */ ret = mpl_read_model(mpl, in_file, in_data != NULL); if (ret == 4) err: { print("Model processing error"); exit(EXIT_FAILURE); } xassert(ret == 1 || ret == 2); /* read data section, if necessary */ if (in_data != NULL) { xassert(ret == 1); ret = mpl_read_data(mpl, in_data); if (ret == 4) goto err; xassert(ret == 2); } /* generate model */ ret = mpl_generate(mpl, display); if (ret == 4) goto err; /* extract problem instance */ lp = lpx_extract_prob(mpl); xassert(lp != NULL); break; case 3: lp = lpx_read_prob(in_file); if (lp == NULL) { print("GNU LP file processing error"); exit(EXIT_FAILURE); } break; case 4: lp = lpx_read_freemps(in_file); if (lp == NULL) { print("MPS file processing error"); exit(EXIT_FAILURE); } break; default: xassert(format != format); } /* order rows and columns of the constraint matrix */ lpx_order_matrix(lp); /* change problem name (if required) */ if (newname != NULL) lpx_set_prob_name(lp, newname); /* change optimization direction (if required) */ if (dir != 0) lpx_set_obj_dir(lp, dir); /* write problem in fixed MPS format (if required) */ if (out_mps != NULL) { lpx_set_int_parm(lp, LPX_K_MPSORIG, orig); ret = lpx_write_mps(lp, out_mps); if (ret != 0) { print("Unable to write problem in fixed MPS format"); exit(EXIT_FAILURE); } } /* write problem in free MPS format (if required) */ if (out_freemps != NULL) { ret = lpx_write_freemps(lp, out_freemps); if (ret != 0) { print("Unable to write problem in free MPS format"); exit(EXIT_FAILURE); } } /* write problem in CPLEX LP format (if required) */ if (out_cpxlp != NULL) { ret = lpx_write_cpxlp(lp, out_cpxlp); if (ret != 0) { print("Unable to write problem in CPLEX LP format"); exit(EXIT_FAILURE); } } /* write problem in plain text format (if required) */ if (out_txt != NULL) { lpx_set_int_parm(lp, LPX_K_LPTORIG, orig); ret = lpx_print_prob(lp, out_txt); if (ret != 0) { print("Unable to write problem in plain text format"); exit(EXIT_FAILURE); } } /* write problem in GNU LP format (if required) */ if (out_glp != NULL) { ret = lpx_write_prob(lp, out_glp); if (ret != 0) { print("Unable to write problem in GNU LP format"); exit(EXIT_FAILURE); } } /* if only data check is required, skip computations */ if (check) goto skip; /* scale the problem data (if required) */ if (scale && (!presol || method == 1)) lpx_scale_prob(lp); /* build initial LP basis */ if (method == 0 && !presol && in_bas == NULL) { switch (basis) { case 0: lpx_std_basis(lp); break; case 1: if (lpx_get_num_rows(lp) > 0 && lpx_get_num_cols(lp) > 0) lpx_adv_basis(lp); break; case 2: if (lpx_get_num_rows(lp) > 0 && lpx_get_num_cols(lp) > 0) lpx_cpx_basis(lp); break; default: xassert(basis != basis); } } /* or read initial basis from input text file in MPS format */ if (in_bas != NULL) { if (method != 0) { print("Initial LP basis is useless for interior-point solve" "r and therefore ignored"); goto nobs; } lpx_set_int_parm(lp, LPX_K_MPSORIG, orig); ret = lpx_read_bas(lp, in_bas); if (ret != 0) { print("Unable to read initial LP basis"); exit(EXIT_FAILURE); } if (presol) { presol = 0; print("LP presolver disabled because initial LP basis has b" "een provided"); } nobs: ; } /* set some control parameters, which might be changed in the command line */ lpx_set_int_parm(lp, LPX_K_BFTYPE, bf_type); lpx_set_int_parm(lp, LPX_K_PRICE, price); if (!relax) lpx_set_real_parm(lp, LPX_K_RELAX, 0.0); lpx_set_int_parm(lp, LPX_K_PRESOL, presol); lpx_set_int_parm(lp, LPX_K_BRANCH, branch); lpx_set_int_parm(lp, LPX_K_BTRACK, btrack); lpx_set_real_parm(lp, LPX_K_TMLIM, (double)tmlim); lpx_set_int_parm(lp, LPX_K_BINARIZE, binarize); lpx_set_int_parm(lp, LPX_K_USECUTS, use_cuts); /* solve the problem */ start = xtime(); switch (method) { case 0: if (nomip || lpx_get_class(lp) == LPX_LP) { ret = (!exact ? lpx_simplex(lp) : lpx_exact(lp)); if (xcheck) { if (!presol || ret == LPX_E_OK) lpx_exact(lp); else print("If you need checking final basis for non-op" "timal solution, use --nopresol"); } if (presol && ret != LPX_E_OK && (out_bas != NULL || out_sol != NULL)) print("If you need actual output for non-optimal solu" "tion, use --nopresol"); } else { method = 2; if (!intopt) { ret = (!exact ? lpx_simplex(lp) : lpx_exact(lp)); if (xcheck && (!presol || ret == LPX_E_OK)) lpx_exact(lp); lpx_integer(lp); } else lpx_intopt(lp); } break; case 1: if (nomip || lpx_get_class(lp) == LPX_LP) lpx_interior(lp); else { print("Interior-point method is not able to solve MIP pr" "oblem; use --simplex"); exit(EXIT_FAILURE); } break; default: xassert(method != method); } /* display statistics */ print("Time used: %.1f secs", xdifftime(xtime(), start)); { ulong_t tpeak; char buf[50]; lib_mem_usage(NULL, NULL, NULL, &tpeak); print("Memory used: %.1f Mb (%s bytes)", (4294967296.0 * tpeak.hi + tpeak.lo) / 1048576.0, ultoa(tpeak, buf, 10)); } if (mpl != NULL && mpl_has_solve_stmt(mpl)) { int n, j, round; /* store the solution to the translator database */ n = lpx_get_num_cols(lp); round = lpx_get_int_parm(lp, LPX_K_ROUND); lpx_set_int_parm(lp, LPX_K_ROUND, 1); switch (method) { case 0: for (j = 1; j <= n; j++) mpl_put_col_value(mpl, j, lpx_get_col_prim(lp, j)); break; case 1: for (j = 1; j <= n; j++) mpl_put_col_value(mpl, j, lpx_ipt_col_prim(lp, j)); break; case 2: for (j = 1; j <= n; j++) mpl_put_col_value(mpl, j, lpx_mip_col_val(lp, j)); break; default: xassert(method != method); } lpx_set_int_parm(lp, LPX_K_ROUND, round); /* perform postsolving */ ret = mpl_postsolve(mpl); if (ret == 4) { print("Model postsolving error"); exit(EXIT_FAILURE); } xassert(ret == 3); } /* write final LP basis (if required) */ if (out_bas != NULL) { lpx_set_int_parm(lp, LPX_K_MPSORIG, orig); ret = lpx_write_bas(lp, out_bas); if (ret != 0) { print("Unable to write final LP basis"); exit(EXIT_FAILURE); } } /* write problem solution found by the solver (if required) */ if (out_sol != NULL) { switch (method) { case 0: ret = lpx_print_sol(lp, out_sol); break; case 1: ret = lpx_print_ips(lp, out_sol); break; case 2: ret = lpx_print_mip(lp, out_sol); break; default: xassert(method != method); } if (ret != 0) { print("Unable to write problem solution"); exit(EXIT_FAILURE); } } /* write sensitivity bounds information (if required) */ if (out_bnds != NULL) { if (method != 0) { print("Cannot write sensitivity bounds information for inte" "rior-point or MIP solution"); exit(EXIT_FAILURE); } ret = lpx_print_sens_bnds(lp, out_bnds); if (ret != 0) { print("Unable to write sensitivity bounds information"); exit(EXIT_FAILURE); } } skip: /* delete the problem object */ lpx_delete_prob(lp); /* if the translator database exists, destroy it */ if (mpl != NULL) mpl_terminate(mpl); xassert(gmp_pool_count() == 0); gmp_free_mem(); /* close the hardcopy file */ if (log_file != NULL) lib_close_log(); /* check that no memory blocks are still allocated */ { int count; ulong_t total; lib_mem_usage(&count, NULL, &total, NULL); xassert(count == 0); xassert(total.lo == 0 && total.hi == 0); } /* free the library environment */ lib_free_env(); /* return to the control program */ return 0; }