void glp_mpl_build_prob(glp_tran *tran, glp_prob *prob) { /* build LP/MIP problem instance from the model */ int m, n, i, j, t, kind, type, len, *ind; double lb, ub, *val; if (tran->phase != 3) xerror("glp_mpl_build_prob: invalid call sequence\n"); /* erase the problem object */ glp_erase_prob(prob); /* set problem name */ glp_set_prob_name(prob, mpl_get_prob_name(tran)); /* build rows (constraints) */ m = mpl_get_num_rows(tran); if (m > 0) glp_add_rows(prob, m); for (i = 1; i <= m; i++) { /* set row name */ glp_set_row_name(prob, i, mpl_get_row_name(tran, i)); /* set row bounds */ type = mpl_get_row_bnds(tran, i, &lb, &ub); switch (type) { case MPL_FR: type = GLP_FR; break; case MPL_LO: type = GLP_LO; break; case MPL_UP: type = GLP_UP; break; case MPL_DB: type = GLP_DB; break; case MPL_FX: type = GLP_FX; break; default: xassert(type != type); } if (type == GLP_DB && fabs(lb - ub) < 1e-9 * (1.0 + fabs(lb))) { type = GLP_FX; if (fabs(lb) <= fabs(ub)) ub = lb; else lb = ub; } glp_set_row_bnds(prob, i, type, lb, ub); /* warn about non-zero constant term */ if (mpl_get_row_c0(tran, i) != 0.0) xprintf("glp_mpl_build_prob: row %s; constant term %.12g ig" "nored\n", mpl_get_row_name(tran, i), mpl_get_row_c0(tran, i)); } /* build columns (variables) */ n = mpl_get_num_cols(tran); if (n > 0) glp_add_cols(prob, n); for (j = 1; j <= n; j++) { /* set column name */ glp_set_col_name(prob, j, mpl_get_col_name(tran, j)); /* set column kind */ kind = mpl_get_col_kind(tran, j); switch (kind) { case MPL_NUM: break; case MPL_INT: case MPL_BIN: glp_set_col_kind(prob, j, GLP_IV); break; default: xassert(kind != kind); } /* set column bounds */ type = mpl_get_col_bnds(tran, j, &lb, &ub); switch (type) { case MPL_FR: type = GLP_FR; break; case MPL_LO: type = GLP_LO; break; case MPL_UP: type = GLP_UP; break; case MPL_DB: type = GLP_DB; break; case MPL_FX: type = GLP_FX; break; default: xassert(type != type); } if (kind == MPL_BIN) { if (type == GLP_FR || type == GLP_UP || lb < 0.0) lb = 0.0; if (type == GLP_FR || type == GLP_LO || ub > 1.0) ub = 1.0; type = GLP_DB; } if (type == GLP_DB && fabs(lb - ub) < 1e-9 * (1.0 + fabs(lb))) { type = GLP_FX; if (fabs(lb) <= fabs(ub)) ub = lb; else lb = ub; } glp_set_col_bnds(prob, j, type, lb, ub); } /* load the constraint matrix */ ind = xcalloc(1+n, sizeof(int)); val = xcalloc(1+n, sizeof(double)); for (i = 1; i <= m; i++) { len = mpl_get_mat_row(tran, i, ind, val); glp_set_mat_row(prob, i, len, ind, val); } /* build objective function (the first objective is used) */ for (i = 1; i <= m; i++) { kind = mpl_get_row_kind(tran, i); if (kind == MPL_MIN || kind == MPL_MAX) { /* set objective name */ glp_set_obj_name(prob, mpl_get_row_name(tran, i)); /* set optimization direction */ glp_set_obj_dir(prob, kind == MPL_MIN ? GLP_MIN : GLP_MAX); /* set constant term */ glp_set_obj_coef(prob, 0, mpl_get_row_c0(tran, i)); /* set objective coefficients */ len = mpl_get_mat_row(tran, i, ind, val); for (t = 1; t <= len; t++) glp_set_obj_coef(prob, ind[t], val[t]); break; } } /* free working arrays */ xfree(ind); xfree(val); return; }
LPX *lpx_extract_prob(void *_mpl) { MPL *mpl = _mpl; LPX *lp; int m, n, i, j, t, kind, type, len, *ind; double lb, ub, *val; /* create problem instance */ lp = lpx_create_prob(); /* set problem name */ lpx_set_prob_name(lp, mpl_get_prob_name(mpl)); /* build rows (constraints) */ m = mpl_get_num_rows(mpl); if (m > 0) lpx_add_rows(lp, m); for (i = 1; i <= m; i++) { /* set row name */ lpx_set_row_name(lp, i, mpl_get_row_name(mpl, i)); /* set row bounds */ type = mpl_get_row_bnds(mpl, i, &lb, &ub); switch (type) { case MPL_FR: type = LPX_FR; break; case MPL_LO: type = LPX_LO; break; case MPL_UP: type = LPX_UP; break; case MPL_DB: type = LPX_DB; break; case MPL_FX: type = LPX_FX; break; default: insist(type != type); } if (type == LPX_DB && fabs(lb - ub) < 1e-9 * (1.0 + fabs(lb))) { type = LPX_FX; if (fabs(lb) <= fabs(ub)) ub = lb; else lb = ub; } lpx_set_row_bnds(lp, i, type, lb, ub); /* warn about non-zero constant term */ if (mpl_get_row_c0(mpl, i) != 0.0) print("lpx_read_model: row %s; constant term %.12g ignored", mpl_get_row_name(mpl, i), mpl_get_row_c0(mpl, i)); } /* build columns (variables) */ n = mpl_get_num_cols(mpl); if (n > 0) lpx_add_cols(lp, n); for (j = 1; j <= n; j++) { /* set column name */ lpx_set_col_name(lp, j, mpl_get_col_name(mpl, j)); /* set column kind */ kind = mpl_get_col_kind(mpl, j); switch (kind) { case MPL_NUM: break; case MPL_INT: case MPL_BIN: lpx_set_class(lp, LPX_MIP); lpx_set_col_kind(lp, j, LPX_IV); break; default: insist(kind != kind); } /* set column bounds */ type = mpl_get_col_bnds(mpl, j, &lb, &ub); switch (type) { case MPL_FR: type = LPX_FR; break; case MPL_LO: type = LPX_LO; break; case MPL_UP: type = LPX_UP; break; case MPL_DB: type = LPX_DB; break; case MPL_FX: type = LPX_FX; break; default: insist(type != type); } if (kind == MPL_BIN) { if (type == LPX_FR || type == LPX_UP || lb < 0.0) lb = 0.0; if (type == LPX_FR || type == LPX_LO || ub > 1.0) ub = 1.0; type = LPX_DB; } if (type == LPX_DB && fabs(lb - ub) < 1e-9 * (1.0 + fabs(lb))) { type = LPX_FX; if (fabs(lb) <= fabs(ub)) ub = lb; else lb = ub; } lpx_set_col_bnds(lp, j, type, lb, ub); } /* load the constraint matrix */ ind = ucalloc(1+n, sizeof(int)); val = ucalloc(1+n, sizeof(double)); for (i = 1; i <= m; i++) { len = mpl_get_mat_row(mpl, i, ind, val); lpx_set_mat_row(lp, i, len, ind, val); } /* build objective function (the first objective is used) */ for (i = 1; i <= m; i++) { kind = mpl_get_row_kind(mpl, i); if (kind == MPL_MIN || kind == MPL_MAX) { /* set objective name */ lpx_set_obj_name(lp, mpl_get_row_name(mpl, i)); /* set optimization direction */ lpx_set_obj_dir(lp, kind == MPL_MIN ? LPX_MIN : LPX_MAX); /* set constant term */ lpx_set_obj_coef(lp, 0, mpl_get_row_c0(mpl, i)); /* set objective coefficients */ len = mpl_get_mat_row(mpl, i, ind, val); for (t = 1; t <= len; t++) lpx_set_obj_coef(lp, ind[t], val[t]); break; } } /* free working arrays */ ufree(ind); ufree(val); /* bring the problem object to the calling program */ return lp; }