static void parse_objective(struct dsa *dsa) { /* parse objective sense */ int k, len; /* parse the keyword 'minimize' or 'maximize' */ if (dsa->token == T_MINIMIZE) lpx_set_obj_dir(dsa->lp, LPX_MIN); else if (dsa->token == T_MAXIMIZE) lpx_set_obj_dir(dsa->lp, LPX_MAX); else xassert(dsa != dsa); scan_token(dsa); /* parse objective name */ if (dsa->token == T_NAME && dsa->c == ':') { /* objective name is followed by a colon */ lpx_set_obj_name(dsa->lp, dsa->image); scan_token(dsa); xassert(dsa->token == T_COLON); scan_token(dsa); } else { /* objective name is not specified; use default */ lpx_set_obj_name(dsa->lp, "obj"); } /* parse linear form */ len = parse_linear_form(dsa); for (k = 1; k <= len; k++) lpx_set_obj_coef(dsa->lp, dsa->ind[k], dsa->val[k]); 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; }