double glpk_wrapper::get_row_value(int i) { double cstr_value = 0; if (solver_type == SIMPLEX || solver_type == EXACT) { int cstr_status = glp_get_row_stat(lp, i); if (cstr_status == GLP_BS) { // basic variable; cstr_status = glp_get_row_prim(lp, i); // or glp_get_row_dual } else if (cstr_status == GLP_NL || cstr_status == GLP_NS) { // non-basic variable on its lower bound, non-basic fixed variable. cstr_value = glp_get_row_lb(lp, i); } else if (cstr_status == GLP_NU) { // non-basic variable on its upper bound cstr_value = glp_get_row_ub(lp, i); } // TODO(dzufferey): should we do something for GLP_NF — non-basic free (unbounded) variable; } else { cstr_value = glp_ipt_row_prim(lp, i); // or glp_ipt_row_dual } return cstr_value; }
int glp_mpl_postsolve(glp_tran *tran, glp_prob *prob, int sol) { /* postsolve the model */ int i, j, m, n, stat, ret; double prim, dual; if (!(tran->phase == 3 && !tran->flag_p)) xerror("glp_mpl_postsolve: invalid call sequence\n"); if (!(sol == GLP_SOL || sol == GLP_IPT || sol == GLP_MIP)) xerror("glp_mpl_postsolve: sol = %d; invalid parameter\n", sol); m = mpl_get_num_rows(tran); n = mpl_get_num_cols(tran); if (!(m == glp_get_num_rows(prob) && n == glp_get_num_cols(prob))) xerror("glp_mpl_postsolve: wrong problem object\n"); if (!mpl_has_solve_stmt(tran)) { ret = 0; goto done; } for (i = 1; i <= m; i++) { if (sol == GLP_SOL) { stat = glp_get_row_stat(prob, i); prim = glp_get_row_prim(prob, i); dual = glp_get_row_dual(prob, i); } else if (sol == GLP_IPT) { stat = 0; prim = glp_ipt_row_prim(prob, i); dual = glp_ipt_row_dual(prob, i); } else if (sol == GLP_MIP) { stat = 0; prim = glp_mip_row_val(prob, i); dual = 0.0; } else xassert(sol != sol); if (fabs(prim) < 1e-9) prim = 0.0; if (fabs(dual) < 1e-9) dual = 0.0; mpl_put_row_soln(tran, i, stat, prim, dual); } for (j = 1; j <= n; j++) { if (sol == GLP_SOL) { stat = glp_get_col_stat(prob, j); prim = glp_get_col_prim(prob, j); dual = glp_get_col_dual(prob, j); } else if (sol == GLP_IPT) { stat = 0; prim = glp_ipt_col_prim(prob, j); dual = glp_ipt_col_dual(prob, j); } else if (sol == GLP_MIP) { stat = 0; prim = glp_mip_col_val(prob, j); dual = 0.0; } else xassert(sol != sol); if (fabs(prim) < 1e-9) prim = 0.0; if (fabs(dual) < 1e-9) dual = 0.0; mpl_put_col_soln(tran, j, stat, prim, dual); } ret = mpl_postsolve(tran); if (ret == 3) ret = 0; else if (ret == 4) ret = 1; done: return ret; }
double lpx_get_row_prim(LPX *lp, int i) { /* retrieve row primal value (basic solution) */ return glp_get_row_prim(lp, i); }
/* R is the random contraint data in row major memory layout ridx is an N array of integers soln is an array of length (n+1) soln[n] is t (objective value) active_constr is an N-length 0-1 array */ void solve_lp(int N, int n, double* R, int* ridx, double* soln, int* active_constr) { double tol = 1.0e-14; int size = (N+1)*(n+1) + 1; // We add one because GLPK indexes arrays // starting at 1 instead of 0. glp_prob *lp; int* ia = malloc(size * sizeof(int)); int* ja = malloc(size * sizeof(int)); double* ar = malloc(size * sizeof(double)); int i, j; lp = glp_create_prob(); glp_set_prob_name(lp, "portfolio"); glp_set_obj_dir(lp, GLP_MAX); glp_add_rows(lp, N+1); // Sampled constraints are ">= 0" for (i = 1; i <= N; i++) { glp_set_row_bnds(lp, i, GLP_LO, 0.0, 0.0); } // Sum = 1 constraint glp_set_row_name(lp, N+1, "sum"); glp_set_row_bnds(lp, N+1, GLP_FX, 1.0, 1.0); glp_add_cols(lp, n+1); // Nonnegative variables y for (i = 1; i <= n; i++) { glp_set_col_bnds(lp, i, GLP_LO, 0.0, 0.0); glp_set_obj_coef(lp, i, 0.0); } // Free variable t glp_set_col_name(lp, n+1, "t"); glp_set_col_bnds(lp, n+1, GLP_FR, 0.0, 0.0); glp_set_obj_coef(lp, n+1, 1.0); // for (i = 0; i < N*(n-1); i++) { // printf("%d: %g\n", i, R[i]); // } int idx = 1; // Sampled constraints for (i = 1; i <= N; i++) { // Uncertain assets for (j = 1; j < n; j++) { ia[idx] = i; ja[idx] = j; ar[idx] = R[ ridx[(i-1)] * (n-1) + (j-1) ]; idx += 1; } // Fixed return asset ia[idx] = i; ja[idx] = n; ar[idx] = 1.05; idx += 1; // t ia[idx] = i; ja[idx] = n+1; ar[idx] = -1.0; idx += 1; } // Sum = 1 constraint for (i = 1; i <= n; i++) { ia[idx] = N+1; ja[idx] = i; ar[idx] = 1.0; idx += 1; } // t ia[idx] = N+1; ja[idx] = n+1; ar[idx] = 0.0; idx += 1; // for (i = 1; i < size; i++) { // printf("%d %d %g\n", ia[i], ja[i], ar[i]); // } glp_load_matrix(lp, size-1, ia, ja, ar); // glp_scale_prob(lp, GLP_SF_AUTO); glp_smcp param; glp_init_smcp(¶m); param.meth = GLP_PRIMAL; //glp_std_basis(lp); glp_simplex(lp, ¶m); double z = glp_get_obj_val(lp); // printf("z = %g\n", z); if (soln) { for (i = 0; i < n; i++) { double y = glp_get_col_prim(lp, i+1); soln[i] = y; // printf("y%d = %g\n", i, y); } double t = glp_get_col_prim(lp, n+1); soln[n] = t; // printf("t = %g\n", glp_get_col_prim(lp, n+1)); } for (i = 1; i <= N; i++) { double slack = glp_get_row_prim(lp, i); active_constr[i-1] = fabs(slack) < tol ? 1 : 0; // printf("constr%d %d\n", i, active_constr[i-1]); } glp_delete_prob(lp); // glp_free_env(); free(ia); free(ja); free(ar); }
double c_glp_get_row_prim(glp_prob *lp, int i){ return glp_get_row_prim(lp, i); }