void glp_unscale_prob(glp_prob *lp) { int m = glp_get_num_rows(lp); int n = glp_get_num_cols(lp); int i, j; for (i = 1; i <= m; i++) glp_set_rii(lp, i, 1.0); for (j = 1; j <= n; j++) glp_set_sjj(lp, j, 1.0); return; }
int GLPKAddConstraint(LinEquation* InEquation) { if (InEquation->QuadCoeff.size() > 0) { FErrorFile() << "GLPK solver cannot accept quadratic constraints." << endl; FlushErrorFile(); return FAIL; } if (GLPKModel == NULL) { FErrorFile() << "Could not add constraint because GLPK object does not exist." << endl; FlushErrorFile(); return FAIL; } int NumRows = glp_get_num_rows(GLPKModel); if (InEquation->Index >= NumRows) { glp_add_rows(GLPKModel, 1); } if (InEquation->EqualityType == EQUAL) { glp_set_row_bnds(GLPKModel, InEquation->Index+1, GLP_FX, InEquation->RightHandSide, InEquation->RightHandSide); } else if (InEquation->EqualityType == GREATER) { glp_set_row_bnds(GLPKModel, InEquation->Index+1, GLP_LO, InEquation->RightHandSide, InEquation->RightHandSide); } else if (InEquation->EqualityType == LESS) { glp_set_row_bnds(GLPKModel, InEquation->Index+1, GLP_UP, InEquation->RightHandSide, InEquation->RightHandSide); } else { FErrorFile() << "Could not add constraint because the constraint type was not recognized." << endl; FlushErrorFile(); return FAIL; } int NumColumns = glp_get_num_cols(GLPKModel); int* Indecies = new int[int(InEquation->Variables.size())+1]; double* Coeff = new double[int(InEquation->Variables.size())+1]; for (int i=0; i < int(InEquation->Variables.size()); i++) { if (InEquation->Variables[i]->Index < NumColumns) { if (InEquation->Variables[i]->Exclude) { Coeff[i+1] = 0; } else { Coeff[i+1] = InEquation->Coefficient[i]; } Indecies[i+1] = InEquation->Variables[i]->Index+1; } else { FErrorFile() << "Variable index found in constraint is out of the range found in GLPK problem" << endl; FlushErrorFile(); return FAIL; } } glp_set_mat_row(GLPKModel, InEquation->Index+1, int(InEquation->Variables.size()), Indecies, Coeff); delete [] Indecies; delete [] Coeff; return SUCCESS; }
/* Different cases : * - if the created node is root, then father is NULL, the problem version in the node is the one gave as parameter. * - else we copy the problem, and had the constraint "x_{y} = valy" */ void create_node(node* n, glp_prob* prob, node* father, int y, double valy) { n->father = father; n->leftSon = NULL; n->rightSon = NULL; n->check = 0; int i = 0; int ind[] = {0,y}; double val[] = {0,1}; if (n-> father == NULL) { n->prob = prob; } else { n->prob = glp_create_prob(); glp_copy_prob(n->prob, n->father->prob, GLP_ON); i = glp_add_rows(n->prob, 1); glp_set_mat_row(n->prob, i, 1, ind, val); glp_set_row_bnds(n->prob, i, GLP_FX, valy, valy); } glp_smcp parm; glp_init_smcp(&parm); parm.msg_lev = GLP_MSG_OFF; glp_iocp parmip; glp_init_iocp(&parmip); parmip.msg_lev = GLP_MSG_OFF; glp_write_lp(prob, NULL, "ULS.lp"); n->solveFlag = glp_simplex(n->prob, &parm); glp_intopt(n->prob, &parmip); n->z = glp_mip_obj_val(n->prob); n->x = (double *) malloc (glp_get_num_cols(n->prob) * sizeof(double)); for (i = 0; i < glp_get_num_cols(n->prob); ++i) n->x[i] = glp_mip_col_val(n->prob, i+1); }
/* Display node function */ void displayNode(node* n) { int i; if (n->solveFlag == 0 && n->z == 0) { printf("NOT FEASIBLE\n"); } else { printf(" z = %f\n",n->z); printf("Solution :\n"); for (i = 0; i < glp_get_num_cols(n->prob); ++i) printf(" x%d = %f \n", i+1, n->x[i]); } }
/* While there is one unchecked node : * We take the most promising. * If its z value is lower than vUp (constructed solution) : * if this is an integer solution, we keep it * else, * we create its sons with a new constraint. */ node* branchAndBound (glp_prob * prob) { node* root = (node *) malloc (sizeof(node)); create_node(root, prob, NULL, 0, 0); node* res = construction(prob); double vUp = res->z; node* node_ptr = checked(root); while (node_ptr != NULL) { if (node_ptr->z != 0) { if (!(node_ptr->z >= vUp)) { int y = allYinteger(node_ptr->x, glp_get_num_cols(node_ptr->prob)); if ( y == -1) { vUp = node_ptr->z; res = node_ptr; } else { node* left = (node *) malloc (sizeof(node)); node* right = (node *) malloc (sizeof(node)); create_node(left, prob, node_ptr, y, 0); create_node(right, prob, node_ptr, y, 1); node_ptr->leftSon = left; node_ptr->rightSon = right; } } } node_ptr->check = 1; node_ptr = checked(root); } return res; }
vector<int> LinearProblem::ElasticFilter() const { LinearProblem tmp(*this); int realCols = glp_get_num_cols(tmp.lp_); for (int i = realCols; i > 0; --i) { // set old coefs to zero glp_set_obj_coef(tmp.lp_, i, 0); } int elasticCols = glp_get_num_rows(tmp.lp_); glp_add_cols(tmp.lp_, elasticCols); for (int i = 1; i <= elasticCols; ++i) { int indices[MAX_VARS]; double values[MAX_VARS]; int nonZeros = glp_get_mat_row(tmp.lp_, i, indices, values); indices[nonZeros + 1] = realCols + i; values[nonZeros + 1] = 1.0; glp_set_mat_row(tmp.lp_, i, nonZeros + 1, indices, values); glp_set_obj_coef(tmp.lp_, realCols + i, 1); glp_set_col_bnds(tmp.lp_, realCols + i, GLP_UP, 0.0, 0.0); } vector<int> suspects; glp_std_basis(tmp.lp_); int status = tmp.Solve(); while ((status != GLP_INFEAS) && (status != GLP_NOFEAS)) { for (int i = 1; i <= elasticCols; ++i) { if (glp_get_col_prim(tmp.lp_, realCols + i) < 0) { suspects.push_back(i); glp_set_col_bnds(tmp.lp_, realCols + i, GLP_FX, 0.0, 0.0); } } status = tmp.Solve(); } return suspects; }
int glp_mpl_postsolve(glp_tran *tran, glp_prob *prob, int sol) { /* postsolve the model */ int j, m, n, ret; double x; 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 (j = 1; j <= n; j++) { if (sol == GLP_SOL) x = glp_get_col_prim(prob, j); else if (sol == GLP_IPT) x = glp_ipt_col_prim(prob, j); else if (sol == GLP_MIP) x = glp_mip_col_val(prob, j); else xassert(sol != sol); if (fabs(x) < 1e-9) x = 0.0; mpl_put_col_value(tran, j, x); } ret = mpl_postsolve(tran); if (ret == 3) ret = 0; else if (ret == 4) ret = 1; done: return ret; }
int GLPKLoadVariables(MFAVariable* InVariable, bool RelaxIntegerVariables,bool UseTightBounds) { if (GLPKModel == NULL) { FErrorFile() << "Could not add variable because GLPK object does not exist." << endl; FlushErrorFile(); return FAIL; } int NumColumns = glp_get_num_cols(GLPKModel); if (InVariable->Index >= NumColumns) { glp_add_cols(GLPKModel, 1); string Name = GetMFAVariableName(InVariable); char* Temp = new char[Name.length()+1]; strcpy(Temp,Name.data()); glp_set_col_name(GLPKModel,InVariable->Index+1,Temp); } double LowerBound = InVariable->LowerBound; double UpperBound = InVariable->UpperBound; if (UseTightBounds) { LowerBound = InVariable->Min; UpperBound = InVariable->Max; } if (LowerBound != UpperBound) { glp_set_col_bnds(GLPKModel, InVariable->Index+1, GLP_DB, InVariable->LowerBound, InVariable->UpperBound); } else { glp_set_col_bnds(GLPKModel, InVariable->Index+1, GLP_FX, InVariable->LowerBound, InVariable->UpperBound); } if (InVariable->Binary && !RelaxIntegerVariables) { //glp_set_class(GLPKModel, GLP_MIP); There is no equivalent in the new API glp_set_col_kind(GLPKModel, InVariable->Index+1,GLP_IV); } return SUCCESS; }
int GLPKLoadObjective(LinEquation* InEquation, bool Max) { if (InEquation->QuadCoeff.size() > 0) { FErrorFile() << "GLPK solver cannot accept quadratic objectives." << endl; FlushErrorFile(); return FAIL; } if (GLPKModel == NULL) { FErrorFile() << "Could not add objective because GLPK object does not exist." << endl; FlushErrorFile(); return FAIL; } if (!Max) { glp_set_obj_dir(GLPKModel, GLP_MIN); } else { glp_set_obj_dir(GLPKModel, GLP_MAX); } int NumColumns = glp_get_num_cols(GLPKModel); for (int i=0; i < NumColumns; i++) { glp_set_obj_coef(GLPKModel, i+1, 0); } for (int i=0; i < int(InEquation->Variables.size()); i++) { if (NumColumns > InEquation->Variables[i]->Index) { glp_set_obj_coef(GLPKModel, InEquation->Variables[i]->Index+1, InEquation->Coefficient[i]); } else { FErrorFile() << "Variable index specified in objective was out of the range of variables added to the GLPK problem object." << endl; FlushErrorFile(); return FAIL; } } return SUCCESS; }
/* Create from the base problem, an other problem which forces stocks to be 0. * The cronstruted solution stay feasible for the first problem. */ node* construction (glp_prob * prob) { node* res = (node *) malloc (sizeof(node)); glp_prob * constProb = glp_create_prob(); glp_copy_prob(constProb, prob, GLP_ON); int i = glp_add_rows(constProb, 1); int k = glp_add_rows(constProb, 1); int nbj = glp_get_num_cols(prob)/3 -1; int ind[nbj+2]; double val[nbj+2]; int indk[nbj+2]; double valk[nbj+2]; int j; ind[0] = 0; val[0] = 1; ind[1] = 1; val[1] = 1; indk[1] = 2 ; valk[1] = 1; for (j = 1; j <= nbj; ++j) { ind[j] = j * 3 +2; indk[j] = j *3 + 3; val[j] = 1; valk[j] = 1; } glp_set_mat_row(constProb, i, nbj, ind, val); glp_set_row_bnds(constProb, i, GLP_FX, 0, 0); glp_set_mat_row(constProb, k, nbj, indk, valk); glp_set_row_bnds(constProb, k, GLP_FX, nbj, nbj); create_node(res, constProb, NULL, 0, 0); return res; }
static void solve(char* file_name) { ppl_Constraint_System_t ppl_cs; #ifndef NDEBUG ppl_Constraint_System_t ppl_cs_copy; #endif ppl_Generator_t optimum_location; ppl_Linear_Expression_t ppl_le; int dimension, row, num_rows, column, nz, i, j, type; int* coefficient_index; double lb, ub; double* coefficient_value; mpq_t rational_lb, rational_ub; mpq_t* rational_coefficient; mpq_t* objective; ppl_Linear_Expression_t ppl_objective_le; ppl_Coefficient_t optimum_n; ppl_Coefficient_t optimum_d; mpq_t optimum; mpz_t den_lcm; int optimum_found; glp_mpscp glpk_mpscp; glpk_lp = glp_create_prob(); glp_init_mpscp(&glpk_mpscp); if (verbosity == 0) { /* FIXME: find a way to suppress output from glp_read_mps. */ } #ifdef PPL_LPSOL_SUPPORTS_TIMINGS if (print_timings) start_clock(); #endif /* defined(PPL_LPSOL_SUPPORTS_TIMINGS) */ if (glp_read_mps(glpk_lp, GLP_MPS_FILE, &glpk_mpscp, file_name) != 0) fatal("cannot read MPS file `%s'", file_name); #ifdef PPL_LPSOL_SUPPORTS_TIMINGS if (print_timings) { fprintf(stderr, "Time to read the input file: "); print_clock(stderr); fprintf(stderr, " s\n"); start_clock(); } #endif /* defined(PPL_LPSOL_SUPPORTS_TIMINGS) */ glpk_lp_num_int = glp_get_num_int(glpk_lp); if (glpk_lp_num_int > 0 && !no_mip && !use_simplex) fatal("the enumeration solving method can not handle MIP problems"); dimension = glp_get_num_cols(glpk_lp); /* Read variables constrained to be integer. */ if (glpk_lp_num_int > 0 && !no_mip && use_simplex) { if (verbosity >= 4) fprintf(output_file, "Integer variables:\n"); integer_variables = (ppl_dimension_type*) malloc((glpk_lp_num_int + 1)*sizeof(ppl_dimension_type)); for (i = 0, j = 0; i < dimension; ++i) { int col_kind = glp_get_col_kind(glpk_lp, i+1); if (col_kind == GLP_IV || col_kind == GLP_BV) { integer_variables[j] = i; if (verbosity >= 4) { ppl_io_fprint_variable(output_file, i); fprintf(output_file, " "); } ++j; } } } coefficient_index = (int*) malloc((dimension+1)*sizeof(int)); coefficient_value = (double*) malloc((dimension+1)*sizeof(double)); rational_coefficient = (mpq_t*) malloc((dimension+1)*sizeof(mpq_t)); ppl_new_Constraint_System(&ppl_cs); mpq_init(rational_lb); mpq_init(rational_ub); for (i = 1; i <= dimension; ++i) mpq_init(rational_coefficient[i]); mpz_init(den_lcm); if (verbosity >= 4) fprintf(output_file, "\nConstraints:\n"); /* Set up the row (ordinary) constraints. */ num_rows = glp_get_num_rows(glpk_lp); for (row = 1; row <= num_rows; ++row) { /* Initialize the least common multiple computation. */ mpz_set_si(den_lcm, 1); /* Set `nz' to the number of non-zero coefficients. */ nz = glp_get_mat_row(glpk_lp, row, coefficient_index, coefficient_value); for (i = 1; i <= nz; ++i) { set_mpq_t_from_double(rational_coefficient[i], coefficient_value[i]); /* Update den_lcm. */ mpz_lcm(den_lcm, den_lcm, mpq_denref(rational_coefficient[i])); } lb = glp_get_row_lb(glpk_lp, row); ub = glp_get_row_ub(glpk_lp, row); set_mpq_t_from_double(rational_lb, lb); set_mpq_t_from_double(rational_ub, ub); mpz_lcm(den_lcm, den_lcm, mpq_denref(rational_lb)); mpz_lcm(den_lcm, den_lcm, mpq_denref(rational_ub)); ppl_new_Linear_Expression_with_dimension(&ppl_le, dimension); for (i = 1; i <= nz; ++i) { mpz_mul(tmp_z, den_lcm, mpq_numref(rational_coefficient[i])); mpz_divexact(tmp_z, tmp_z, mpq_denref(rational_coefficient[i])); ppl_assign_Coefficient_from_mpz_t(ppl_coeff, tmp_z); ppl_Linear_Expression_add_to_coefficient(ppl_le, coefficient_index[i]-1, ppl_coeff); } type = glp_get_row_type(glpk_lp, row); add_constraints(ppl_le, type, rational_lb, rational_ub, den_lcm, ppl_cs); ppl_delete_Linear_Expression(ppl_le); } free(coefficient_value); for (i = 1; i <= dimension; ++i) mpq_clear(rational_coefficient[i]); free(rational_coefficient); free(coefficient_index); #ifndef NDEBUG ppl_new_Constraint_System_from_Constraint_System(&ppl_cs_copy, ppl_cs); #endif /* FIXME: here we could build the polyhedron and minimize it before adding the variable bounds. */ /* Set up the columns constraints, i.e., variable bounds. */ for (column = 1; column <= dimension; ++column) { lb = glp_get_col_lb(glpk_lp, column); ub = glp_get_col_ub(glpk_lp, column); set_mpq_t_from_double(rational_lb, lb); set_mpq_t_from_double(rational_ub, ub); /* Initialize the least common multiple computation. */ mpz_set_si(den_lcm, 1); mpz_lcm(den_lcm, den_lcm, mpq_denref(rational_lb)); mpz_lcm(den_lcm, den_lcm, mpq_denref(rational_ub)); ppl_new_Linear_Expression_with_dimension(&ppl_le, dimension); ppl_assign_Coefficient_from_mpz_t(ppl_coeff, den_lcm); ppl_Linear_Expression_add_to_coefficient(ppl_le, column-1, ppl_coeff); type = glp_get_col_type(glpk_lp, column); add_constraints(ppl_le, type, rational_lb, rational_ub, den_lcm, ppl_cs); ppl_delete_Linear_Expression(ppl_le); } mpq_clear(rational_ub); mpq_clear(rational_lb); /* Deal with the objective function. */ objective = (mpq_t*) malloc((dimension+1)*sizeof(mpq_t)); /* Initialize the least common multiple computation. */ mpz_set_si(den_lcm, 1); mpq_init(objective[0]); set_mpq_t_from_double(objective[0], glp_get_obj_coef(glpk_lp, 0)); for (i = 1; i <= dimension; ++i) { mpq_init(objective[i]); set_mpq_t_from_double(objective[i], glp_get_obj_coef(glpk_lp, i)); /* Update den_lcm. */ mpz_lcm(den_lcm, den_lcm, mpq_denref(objective[i])); } /* Set the ppl_objective_le to be the objective function. */ ppl_new_Linear_Expression_with_dimension(&ppl_objective_le, dimension); /* Set value for objective function's inhomogeneous term. */ mpz_mul(tmp_z, den_lcm, mpq_numref(objective[0])); mpz_divexact(tmp_z, tmp_z, mpq_denref(objective[0])); ppl_assign_Coefficient_from_mpz_t(ppl_coeff, tmp_z); ppl_Linear_Expression_add_to_inhomogeneous(ppl_objective_le, ppl_coeff); /* Set values for objective function's variable coefficients. */ for (i = 1; i <= dimension; ++i) { mpz_mul(tmp_z, den_lcm, mpq_numref(objective[i])); mpz_divexact(tmp_z, tmp_z, mpq_denref(objective[i])); ppl_assign_Coefficient_from_mpz_t(ppl_coeff, tmp_z); ppl_Linear_Expression_add_to_coefficient(ppl_objective_le, i-1, ppl_coeff); } if (verbosity >= 4) { fprintf(output_file, "Objective function:\n"); if (mpz_cmp_si(den_lcm, 1) != 0) fprintf(output_file, "("); ppl_io_fprint_Linear_Expression(output_file, ppl_objective_le); } for (i = 0; i <= dimension; ++i) mpq_clear(objective[i]); free(objective); if (verbosity >= 4) { if (mpz_cmp_si(den_lcm, 1) != 0) { fprintf(output_file, ")/"); mpz_out_str(output_file, 10, den_lcm); } fprintf(output_file, "\n%s\n", (maximize ? "Maximizing." : "Minimizing.")); } ppl_new_Coefficient(&optimum_n); ppl_new_Coefficient(&optimum_d); ppl_new_Generator_zero_dim_point(&optimum_location); optimum_found = use_simplex ? solve_with_simplex(ppl_cs, ppl_objective_le, optimum_n, optimum_d, optimum_location) : solve_with_generators(ppl_cs, ppl_objective_le, optimum_n, optimum_d, optimum_location); ppl_delete_Linear_Expression(ppl_objective_le); if (glpk_lp_num_int > 0) free(integer_variables); if (optimum_found) { mpq_init(optimum); ppl_Coefficient_to_mpz_t(optimum_n, tmp_z); mpq_set_num(optimum, tmp_z); ppl_Coefficient_to_mpz_t(optimum_d, tmp_z); mpz_mul(tmp_z, tmp_z, den_lcm); mpq_set_den(optimum, tmp_z); if (verbosity == 1) fprintf(output_file, "Optimized problem.\n"); if (verbosity >= 2) fprintf(output_file, "Optimum value: %.10g\n", mpq_get_d(optimum)); if (verbosity >= 3) { fprintf(output_file, "Optimum location:\n"); ppl_Generator_divisor(optimum_location, ppl_coeff); ppl_Coefficient_to_mpz_t(ppl_coeff, tmp_z); for (i = 0; i < dimension; ++i) { mpz_set(mpq_denref(tmp1_q), tmp_z); ppl_Generator_coefficient(optimum_location, i, ppl_coeff); ppl_Coefficient_to_mpz_t(ppl_coeff, mpq_numref(tmp1_q)); ppl_io_fprint_variable(output_file, i); fprintf(output_file, " = %.10g\n", mpq_get_d(tmp1_q)); } } #ifndef NDEBUG { ppl_Polyhedron_t ph; unsigned int relation; ppl_new_C_Polyhedron_recycle_Constraint_System(&ph, ppl_cs_copy); ppl_delete_Constraint_System(ppl_cs_copy); relation = ppl_Polyhedron_relation_with_Generator(ph, optimum_location); ppl_delete_Polyhedron(ph); assert(relation == PPL_POLY_GEN_RELATION_SUBSUMES); } #endif maybe_check_results(PPL_MIP_PROBLEM_STATUS_OPTIMIZED, mpq_get_d(optimum)); mpq_clear(optimum); } ppl_delete_Constraint_System(ppl_cs); ppl_delete_Coefficient(optimum_d); ppl_delete_Coefficient(optimum_n); ppl_delete_Generator(optimum_location); glp_delete_prob(glpk_lp); }
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; }
OptSolutionData* GLPKRunSolver(int ProbType) { OptSolutionData* NewSolution = NULL; int NumVariables = glp_get_num_cols(GLPKModel); int Status = 0; if (ProbType == MILP) { Status = glp_simplex(GLPKModel, NULL); // Use default settings if (Status != 0) { FErrorFile() << "Failed to optimize problem." << endl; FlushErrorFile(); return NULL; } Status = glp_intopt(GLPKModel, NULL); // Use default settings if (Status != 0) { FErrorFile() << "Failed to optimize problem." << endl; FlushErrorFile(); return NULL; } NewSolution = new OptSolutionData; Status = glp_mip_status(GLPKModel); if (Status == GLP_UNDEF || Status == GLP_NOFEAS) { NewSolution->Status = INFEASIBLE; return NewSolution; } else if (Status == GLP_FEAS) { NewSolution->Status = UNBOUNDED; return NewSolution; } else if (Status == GLP_OPT) { NewSolution->Status = SUCCESS; } else { delete NewSolution; FErrorFile() << "Problem status unrecognized." << endl; FlushErrorFile(); return NULL; } NewSolution->Objective = glp_mip_obj_val(GLPKModel); NewSolution->SolutionData.resize(NumVariables); for (int i=0; i < NumVariables; i++) { NewSolution->SolutionData[i] = glp_mip_col_val(GLPKModel, i+1); } } else if (ProbType == LP) { //First we check the basis matrix to ensure it is not singular if (glp_warm_up(GLPKModel) != 0) { glp_adv_basis(GLPKModel, 0); } Status = glp_simplex(GLPKModel, NULL); // Use default settings if (Status == GLP_EBADB) { /* the basis is invalid; build some valid basis */ glp_adv_basis(GLPKModel, 0); Status = glp_simplex(GLPKModel, NULL); // Use default settings } if (Status != 0) { FErrorFile() << "Failed to optimize problem." << endl; FlushErrorFile(); return NULL; } NewSolution = new OptSolutionData; Status = glp_get_status(GLPKModel); if (Status == GLP_INFEAS || Status == GLP_NOFEAS || Status == GLP_UNDEF) { cout << "Model is infeasible" << endl; FErrorFile() << "Model is infeasible" << endl; FlushErrorFile(); NewSolution->Status = INFEASIBLE; return NewSolution; } else if (Status == GLP_FEAS || Status == GLP_UNBND) { cout << "Model is unbounded" << endl; FErrorFile() << "Model is unbounded" << endl; FlushErrorFile(); NewSolution->Status = UNBOUNDED; return NewSolution; } else if (Status == GLP_OPT) { NewSolution->Status = SUCCESS; } else { delete NewSolution; FErrorFile() << "Problem status unrecognized." << endl; FlushErrorFile(); return NULL; } NewSolution->Objective = glp_get_obj_val(GLPKModel); NewSolution->SolutionData.resize(NumVariables); for (int i=0; i < NumVariables; i++) { NewSolution->SolutionData[i] = glp_get_col_prim(GLPKModel, i+1); } } else { FErrorFile() << "Optimization problem type cannot be handled by GLPK solver." << endl; FlushErrorFile(); return NULL; } return NewSolution; }
// retrieve all missing values of LP/MILP void Rglpk_retrieve_MP_from_file (char **file, int *type, int *lp_n_constraints, int *lp_n_objective_vars, double *lp_objective_coefficients, int *lp_constraint_matrix_i, int *lp_constraint_matrix_j, double *lp_constraint_matrix_values, int *lp_direction_of_constraints, double *lp_right_hand_side, double *lp_left_hand_side, int *lp_objective_var_is_integer, int *lp_objective_var_is_binary, int *lp_bounds_type, double *lp_bounds_lower, double *lp_bounds_upper, int *lp_ignore_first_row, int *lp_verbosity, char **lp_constraint_names, char **lp_objective_vars_names ) { extern glp_prob *lp; glp_tran *tran; const char *str; int i, j, lp_column_kind, tmp; int ind_offset, status; // Turn on/off Terminal Output if (*lp_verbosity==1) glp_term_out(GLP_ON); else glp_term_out(GLP_OFF); // create problem object if (lp) glp_delete_prob(lp); lp = glp_create_prob(); // read file -> gets stored as an GLPK problem object 'lp' // which file type do we have? switch (*type){ case 1: // Fixed (ancient) MPS Format, param argument currently NULL status = glp_read_mps(lp, GLP_MPS_DECK, NULL, *file); break; case 2: // Free (modern) MPS format, param argument currently NULL status = glp_read_mps(lp, GLP_MPS_FILE, NULL, *file); break; case 3: // CPLEX LP Format status = glp_read_lp(lp, NULL, *file); break; case 4: // MATHPROG Format (based on lpx_read_model function) tran = glp_mpl_alloc_wksp(); status = glp_mpl_read_model(tran, *file, 0); if (!status) { status = glp_mpl_generate(tran, NULL); if (!status) { glp_mpl_build_prob(tran, lp); } } glp_mpl_free_wksp(tran); break; } // if file read successfully glp_read_* returns zero if ( status != 0 ) { glp_delete_prob(lp); lp = NULL; error("Reading file %c failed.", *file); } if(*lp_verbosity==1) Rprintf("Retrieve column specific data ...\n"); if(glp_get_num_cols(lp) != *lp_n_objective_vars) { glp_delete_prob(lp); lp = NULL; error("The number of columns is not as specified"); } // retrieve column specific data (values, bounds and type) for (i = 0; i < *lp_n_objective_vars; i++) { lp_objective_coefficients[i] = glp_get_obj_coef(lp, i+1); // Note that str must not be freed befor we have returned // from the .C call in R! str = glp_get_col_name(lp, i+1); if (str){ lp_objective_vars_names[i] = (char *) str; } lp_bounds_type[i] = glp_get_col_type(lp, i+1); lp_bounds_lower[i] = glp_get_col_lb (lp, i+1); lp_bounds_upper[i] = glp_get_col_ub (lp, i+1); lp_column_kind = glp_get_col_kind(lp, i+1); // set to TRUE if objective variable is integer or binary switch (lp_column_kind){ case GLP_IV: lp_objective_var_is_integer[i] = 1; break; case GLP_BV: lp_objective_var_is_binary[i] = 1; break; } } ind_offset = 0; if(*lp_verbosity==1) Rprintf("Retrieve row specific data ...\n"); if(glp_get_num_rows(lp) != *lp_n_constraints) { glp_delete_prob(lp); lp = NULL; error("The number of rows is not as specified"); } // retrieve row specific data (right hand side, direction of constraints) for (i = *lp_ignore_first_row; i < *lp_n_constraints; i++) { lp_direction_of_constraints[i] = glp_get_row_type(lp, i+1); str = glp_get_row_name(lp, i + 1); if (str) { lp_constraint_names[i] = (char *) str; } // the right hand side. Note we don't allow for double bounded or // free auxiliary variables if( lp_direction_of_constraints[i] == GLP_LO ) lp_right_hand_side[i] = glp_get_row_lb(lp, i+1); if( lp_direction_of_constraints[i] == GLP_UP ) lp_right_hand_side[i] = glp_get_row_ub(lp, i+1); if( lp_direction_of_constraints[i] == GLP_FX ) lp_right_hand_side[i] = glp_get_row_lb(lp, i+1); if( lp_direction_of_constraints[i] == GLP_DB ){ lp_right_hand_side[i] = glp_get_row_ub(lp, i+1); lp_left_hand_side[i] = glp_get_row_lb(lp, i+1); } tmp = glp_get_mat_row(lp, i+1, &lp_constraint_matrix_j[ind_offset-1], &lp_constraint_matrix_values[ind_offset-1]); if (tmp > 0) for (j = 0; j < tmp; j++) lp_constraint_matrix_i[ind_offset+j] = i+1; ind_offset += tmp; } if(*lp_verbosity==1) Rprintf("Done.\n"); }
static PyObject* LPX_Str(LPXObject *self) { // Returns a string representation of this object. return PyString_FromFormat ("<%s %d-by-%d at %p>", self->ob_type->tp_name, glp_get_num_rows(LP), glp_get_num_cols(LP), self); }
int lpx_get_num_cols(LPX *lp) { /* retrieve number of columns */ return glp_get_num_cols(lp); }
int lpx_write_pb(LPX *lp, const char *fname, int normalized, int binarize) { FILE* fp; int m,n,i,j,k,o,nonfree=0, obj_dir, dbl, *ndx, row_type, emptylhs=0; double coeff, *val, bound, constant/*=0.0*/; char* objconstname = "dummy_one"; char* emptylhsname = "dummy_zero"; /* Variables needed for possible binarization */ /*LPX* tlp;*/ IPP *ipp = NULL; /*tlp=lp;*/ if(binarize) /* Transform integer variables to binary ones */ { ipp = ipp_create_wksp(); ipp_load_orig(ipp, lp); ipp_binarize(ipp); lp = ipp_build_prob(ipp); } fp = fopen(fname, "w"); if(fp!= NULL) { xprintf( "lpx_write_pb: writing problem in %sOPB format to `%s'...\n", (normalized?"normalized ":""), fname); m = glp_get_num_rows(lp); n = glp_get_num_cols(lp); for(i=1;i<=m;i++) { switch(glp_get_row_type(lp,i)) { case GLP_LO: case GLP_UP: case GLP_FX: { nonfree += 1; break; } case GLP_DB: { nonfree += 2; break; } } } constant=glp_get_obj_coef(lp,0); fprintf(fp,"* #variables = %d #constraints = %d\n", n + (constant == 0?1:0), nonfree + (constant == 0?1:0)); /* Objective function */ obj_dir = glp_get_obj_dir(lp); fprintf(fp,"min: "); for(i=1;i<=n;i++) { coeff = glp_get_obj_coef(lp,i); if(coeff != 0.0) { if(obj_dir == GLP_MAX) coeff=-coeff; if(normalized) fprintf(fp, " %d x%d", (int)coeff, i); else fprintf(fp, " %d*%s", (int)coeff, glp_get_col_name(lp,i)); } } if(constant) { if(normalized) fprintf(fp, " %d x%d", (int)constant, n+1); else fprintf(fp, " %d*%s", (int)constant, objconstname); } fprintf(fp,";\n"); if(normalized && !binarize) /* Name substitution */ { fprintf(fp,"* Variable name substitution:\n"); for(j=1;j<=n;j++) { fprintf(fp, "* x%d = %s\n", j, glp_get_col_name(lp,j)); } if(constant) fprintf(fp, "* x%d = %s\n", n+1, objconstname); } ndx = xcalloc(1+n, sizeof(int)); val = xcalloc(1+n, sizeof(double)); /* Constraints */ for(j=1;j<=m;j++) { row_type=glp_get_row_type(lp,j); if(row_type!=GLP_FR) { if(row_type == GLP_DB) { dbl=2; row_type = GLP_UP; } else { dbl=1; } k=glp_get_mat_row(lp, j, ndx, val); for(o=1;o<=dbl;o++) { if(o==2) { row_type = GLP_LO; } if(k==0) /* Empty LHS */ { emptylhs = 1; if(normalized) { fprintf(fp, "0 x%d ", n+2); } else { fprintf(fp, "0*%s ", emptylhsname); } } for(i=1;i<=k;i++) { if(val[i] != 0.0) { if(normalized) { fprintf(fp, "%d x%d ", (row_type==GLP_UP)?(-(int)val[i]):((int)val[i]), ndx[i]); } else { fprintf(fp, "%d*%s ", (int)val[i], glp_get_col_name(lp,ndx[i])); } } } switch(row_type) { case GLP_LO: { fprintf(fp, ">="); bound = glp_get_row_lb(lp,j); break; } case GLP_UP: { if(normalized) { fprintf(fp, ">="); bound = -glp_get_row_ub(lp,j); } else { fprintf(fp, "<="); bound = glp_get_row_ub(lp,j); } break; } case GLP_FX: { fprintf(fp, "="); bound = glp_get_row_lb(lp,j); break; } } fprintf(fp," %d;\n",(int)bound); } } } xfree(ndx); xfree(val); if(constant) { xprintf( "lpx_write_pb: adding constant objective function variable\n"); if(normalized) fprintf(fp, "1 x%d = 1;\n", n+1); else fprintf(fp, "1*%s = 1;\n", objconstname); } if(emptylhs) { xprintf( "lpx_write_pb: adding dummy variable for empty left-hand si" "de constraint\n"); if(normalized) fprintf(fp, "1 x%d = 0;\n", n+2); else fprintf(fp, "1*%s = 0;\n", emptylhsname); } } else { xprintf("Problems opening file for writing: %s\n", fname); return(1); } fflush(fp); if (ferror(fp)) { xprintf("lpx_write_pb: can't write to `%s' - %s\n", fname, strerror(errno)); goto fail; } fclose(fp); if(binarize) { /* delete the resultant problem object */ if (lp != NULL) lpx_delete_prob(lp); /* delete MIP presolver workspace */ if (ipp != NULL) ipp_delete_wksp(ipp); /*lp=tlp;*/ } return 0; fail: if (fp != NULL) fclose(fp); return 1; }
// read in all necessary elements for retrieving the LP/MILP void Rglpk_read_file (char **file, int *type, int *lp_direction_of_optimization, int *lp_n_constraints, int *lp_n_objective_vars, int *lp_n_values_in_constraint_matrix, int *lp_n_integer_vars, int *lp_n_binary_vars, char **lp_prob_name, char **lp_obj_name, int *lp_verbosity) { int status; extern glp_prob *lp; glp_tran *tran; const char *str; // Turn on/off Terminal Output if (*lp_verbosity==1) glp_term_out(GLP_ON); else glp_term_out(GLP_OFF); // create problem object if (lp) glp_delete_prob(lp); lp = glp_create_prob(); // read file -> gets stored as an GLPK problem object 'lp' // which file type do we have? switch (*type){ case 1: // Fixed (ancient) MPS Format, param argument currently NULL status = glp_read_mps(lp, GLP_MPS_DECK, NULL, *file); break; case 2: // Free (modern) MPS format, param argument currently NULL status = glp_read_mps(lp, GLP_MPS_FILE, NULL, *file); break; case 3: // CPLEX LP Format status = glp_read_lp(lp, NULL, *file); break; case 4: // MATHPROG Format (based on lpx_read_model function) tran = glp_mpl_alloc_wksp(); status = glp_mpl_read_model(tran, *file, 0); if (!status) { status = glp_mpl_generate(tran, NULL); if (!status) { glp_mpl_build_prob(tran, lp); } } glp_mpl_free_wksp(tran); break; } // if file read successfully glp_read_* returns zero if ( status != 0 ) { glp_delete_prob(lp); lp = NULL; error("Reading file %s failed", *file); } // retrieve problem name str = glp_get_prob_name(lp); if (str){ *lp_prob_name = (char *) str; } // retrieve name of objective function str = glp_get_obj_name(lp); if (str){ *lp_obj_name = (char *) str; } // retrieve optimization direction flag *lp_direction_of_optimization = glp_get_obj_dir(lp); // retrieve number of constraints *lp_n_constraints = glp_get_num_rows(lp); // retrieve number of objective variables *lp_n_objective_vars = glp_get_num_cols(lp); // retrieve number of non-zero elements in constraint matrix *lp_n_values_in_constraint_matrix = glp_get_num_nz(lp); // retrieve number of integer variables *lp_n_integer_vars = glp_get_num_int(lp); // retrieve number of binary variables *lp_n_binary_vars = glp_get_num_bin(lp); }
int c_glp_get_num_cols (glp_prob *lp){ return glp_get_num_cols (lp); }
int glpk (int sense, int n, int m, double *c, int nz, int *rn, int *cn, double *a, double *b, char *ctype, int *freeLB, double *lb, int *freeUB, double *ub, int *vartype, int isMIP, int lpsolver, int save_pb, char *save_filename, char *filetype, double *xmin, double *fmin, double *status, double *lambda, double *redcosts, double *time, double *mem) { int typx = 0; int method; clock_t t_start = clock(); //Redirect standard output if (glpIntParam[0] > 1) glp_term_hook (glpk_print_hook, NULL); else glp_term_hook (NULL, NULL); //-- Create an empty LP/MILP object LPX *lp = lpx_create_prob (); //-- Set the sense of optimization if (sense == 1) glp_set_obj_dir (lp, GLP_MIN); else glp_set_obj_dir (lp, GLP_MAX); //-- Define the number of unknowns and their domains. glp_add_cols (lp, n); for (int i = 0; i < n; i++) { //-- Define type of the structural variables if (! freeLB[i] && ! freeUB[i]) { if ( lb[i] == ub[i] ) glp_set_col_bnds (lp, i+1, GLP_FX, lb[i], ub[i]); else glp_set_col_bnds (lp, i+1, GLP_DB, lb[i], ub[i]); } else { if (! freeLB[i] && freeUB[i]) glp_set_col_bnds (lp, i+1, GLP_LO, lb[i], ub[i]); else { if (freeLB[i] && ! freeUB[i]) glp_set_col_bnds (lp, i+1, GLP_UP, lb[i], ub[i]); else glp_set_col_bnds (lp, i+1, GLP_FR, lb[i], ub[i]); } } // -- Set the objective coefficient of the corresponding // -- structural variable. No constant term is assumed. glp_set_obj_coef(lp,i+1,c[i]); if (isMIP) glp_set_col_kind (lp, i+1, vartype[i]); } glp_add_rows (lp, m); for (int i = 0; i < m; i++) { /* If the i-th row has no lower bound (types F,U), the corrispondent parameter will be ignored. If the i-th row has no upper bound (types F,L), the corrispondent parameter will be ignored. If the i-th row is of S type, the i-th LB is used, but the i-th UB is ignored. */ switch (ctype[i]) { case 'F': typx = GLP_FR; break; // upper bound case 'U': typx = GLP_UP; break; // lower bound case 'L': typx = GLP_LO; break; // fixed constraint case 'S': typx = GLP_FX; break; // double-bounded variable case 'D': typx = GLP_DB; break; } if ( typx == GLP_DB && -b[i] < b[i]) { glp_set_row_bnds (lp, i+1, typx, -b[i], b[i]); } else if(typx == GLP_DB && -b[i] == b[i]) { glp_set_row_bnds (lp, i+1, GLP_FX, b[i], b[i]); } else { // this should be glp_set_row_bnds (lp, i+1, typx, -b[i], b[i]); glp_set_row_bnds (lp, i+1, typx, b[i], b[i]); } } // Load constraint matrix A glp_load_matrix (lp, nz, rn, cn, a); // Save problem if (save_pb) { if (!strcmp(filetype,"cplex")){ if (glp_write_lp (lp, NULL, save_filename) != 0) { mexErrMsgTxt("glpk: unable to write the problem"); longjmp (mark, -1); } }else{ if (!strcmp(filetype,"fixedmps")){ if (glp_write_mps (lp, GLP_MPS_DECK, NULL, save_filename) != 0) { mexErrMsgTxt("glpk: unable to write the problem"); longjmp (mark, -1); } }else{ if (!strcmp(filetype,"freemps")){ if (glp_write_mps (lp, GLP_MPS_FILE, NULL, save_filename) != 0) { mexErrMsgTxt("glpk: unable to write the problem"); longjmp (mark, -1); } }else{// plain text if (lpx_print_prob (lp, save_filename) != 0) { mexErrMsgTxt("glpk: unable to write the problem"); longjmp (mark, -1); } } } } } //-- scale the problem data (if required) if (! glpIntParam[16] || lpsolver != 1) { switch ( glpIntParam[1] ) { case ( 0 ): glp_scale_prob( lp, GLP_SF_SKIP ); break; case ( 1 ): glp_scale_prob( lp, GLP_SF_GM ); break; case ( 2 ): glp_scale_prob( lp, GLP_SF_EQ ); break; case ( 3 ): glp_scale_prob( lp, GLP_SF_AUTO ); break; case ( 4 ): glp_scale_prob( lp, GLP_SF_2N ); break; default : mexErrMsgTxt("glpk: unrecognized scaling option"); longjmp (mark, -1); } } else { /* do nothing? or unscale? glp_unscale_prob( lp ); */ } //-- build advanced initial basis (if required) if (lpsolver == 1 && ! glpIntParam[16]) glp_adv_basis (lp, 0); glp_smcp sParam; glp_init_smcp(&sParam); //-- set control parameters for simplex/exact method if (lpsolver == 1 || lpsolver == 3){ //remap of control parameters for simplex method sParam.msg_lev=glpIntParam[0]; // message level // simplex method: primal/dual switch ( glpIntParam[2] ) { case 0: sParam.meth=GLP_PRIMAL; break; case 1: sParam.meth=GLP_DUAL; break; case 2: sParam.meth=GLP_DUALP; break; default: mexErrMsgTxt("glpk: unrecognized primal/dual method"); longjmp (mark, -1); } // pricing technique if (glpIntParam[3]==0) sParam.pricing=GLP_PT_STD; else sParam.pricing=GLP_PT_PSE; // ratio test if (glpIntParam[20]==0) sParam.r_test = GLP_RT_STD; else sParam.r_test=GLP_RT_HAR; //tollerances sParam.tol_bnd=glpRealParam[1]; // primal feasible tollerance sParam.tol_dj=glpRealParam[2]; // dual feasible tollerance sParam.tol_piv=glpRealParam[3]; // pivot tollerance sParam.obj_ll=glpRealParam[4]; // lower limit sParam.obj_ul=glpRealParam[5]; // upper limit // iteration limit if (glpIntParam[5]==-1) sParam.it_lim=INT_MAX; else sParam.it_lim=glpIntParam[5]; // time limit if (glpRealParam[6]==-1) sParam.tm_lim=INT_MAX; else sParam.tm_lim=(int) glpRealParam[6]; sParam.out_frq=glpIntParam[7]; // output frequency sParam.out_dly=(int) glpRealParam[7]; // output delay // presolver if (glpIntParam[16]) sParam.presolve=GLP_ON; else sParam.presolve=GLP_OFF; }else{ for(int i = 0; i < NIntP; i++) { // skip assinging ratio test or if ( i == 18 || i == 20) continue; lpx_set_int_parm (lp, IParam[i], glpIntParam[i]); } for (int i = 0; i < NRealP; i++) { lpx_set_real_parm (lp, RParam[i], glpRealParam[i]); } } //set MIP params if MIP.... glp_iocp iParam; glp_init_iocp(&iParam); if ( isMIP ){ method = 'I'; switch (glpIntParam[0]) { //message level case 0: iParam.msg_lev = GLP_MSG_OFF; break; case 1: iParam.msg_lev = GLP_MSG_ERR; break; case 2: iParam.msg_lev = GLP_MSG_ON; break; case 3: iParam.msg_lev = GLP_MSG_ALL; break; default: mexErrMsgTxt("glpk: msg_lev bad param"); } switch (glpIntParam[14]) { //branching param case 0: iParam.br_tech = GLP_BR_FFV; break; case 1: iParam.br_tech = GLP_BR_LFV; break; case 2: iParam.br_tech = GLP_BR_MFV; break; case 3: iParam.br_tech = GLP_BR_DTH; break; default: mexErrMsgTxt("glpk: branch bad param"); } switch (glpIntParam[15]) { //backtracking heuristic case 0: iParam.bt_tech = GLP_BT_DFS; break; case 1: iParam.bt_tech = GLP_BT_BFS; break; case 2: iParam.bt_tech = GLP_BT_BLB; break; case 3: iParam.bt_tech = GLP_BT_BPH; break; default: mexErrMsgTxt("glpk: backtrack bad param"); } if ( glpRealParam[8] > 0.0 && glpRealParam[8] < 1.0 ) iParam.tol_int = glpRealParam[8]; // absolute tolorence else mexErrMsgTxt("glpk: tolint must be between 0 and 1"); iParam.tol_obj = glpRealParam[9]; // relative tolarence iParam.mip_gap = glpRealParam[10]; // realative gap tolerance // set time limit for mip if ( glpRealParam[6] < 0.0 || glpRealParam[6] > 1e6 ) iParam.tm_lim = INT_MAX; else iParam.tm_lim = (int)(1000.0 * glpRealParam[6] ); // Choose Cutsets for mip // shut all cuts off, then start over.... iParam.gmi_cuts = GLP_OFF; iParam.mir_cuts = GLP_OFF; iParam.cov_cuts = GLP_OFF; iParam.clq_cuts = GLP_OFF; switch( glpIntParam[17] ) { case 0: break; case 1: iParam.gmi_cuts = GLP_ON; break; case 2: iParam.mir_cuts = GLP_ON; break; case 3: iParam.cov_cuts = GLP_ON; break; case 4: iParam.clq_cuts = GLP_ON; break; case 5: iParam.clq_cuts = GLP_ON; iParam.gmi_cuts = GLP_ON; iParam.mir_cuts = GLP_ON; iParam.cov_cuts = GLP_ON; iParam.clq_cuts = GLP_ON; break; default: mexErrMsgTxt("glpk: cutset bad param"); } switch( glpIntParam[18] ) { // pre-processing for mip case 0: iParam.pp_tech = GLP_PP_NONE; break; case 1: iParam.pp_tech = GLP_PP_ROOT; break; case 2: iParam.pp_tech = GLP_PP_ALL; break; default: mexErrMsgTxt("glpk: pprocess bad param"); } if (glpIntParam[16]) iParam.presolve=GLP_ON; else iParam.presolve=GLP_OFF; if (glpIntParam[19]) iParam.binarize = GLP_ON; else iParam.binarize = GLP_OFF; } else { /* Choose simplex method ('S') or interior point method ('T') or Exact method ('E') to solve the problem */ switch (lpsolver) { case 1: method = 'S'; break; case 2: method = 'T'; break; case 3: method = 'E'; break; default: mexErrMsgTxt("glpk: lpsolver != lpsolver"); longjmp (mark, -1); } } // now run the problem... int errnum = 0; switch (method) { case 'I': errnum = glp_intopt( lp, &iParam ); errnum += 200; //this is to avoid ambiguity in the return codes. break; case 'S': errnum = glp_simplex(lp, &sParam); errnum += 100; //this is to avoid ambiguity in the return codes. break; case 'T': errnum = glp_interior(lp, NULL ); errnum += 300; //this is to avoid ambiguity in the return codes. break; case 'E': errnum = glp_exact(lp, &sParam); errnum += 100; //this is to avoid ambiguity in the return codes. break; default: /*xassert (method != method); */ mexErrMsgTxt("glpk: method != method"); longjmp (mark, -1); } if (errnum==100 || errnum==200 || errnum==300 || errnum==106 || errnum==107 || errnum==108 || errnum==109 || errnum==209 || errnum==214 || errnum==308) { // Get status and object value if (isMIP) { *status = glp_mip_status (lp); *fmin = glp_mip_obj_val (lp); } else { if (lpsolver == 1 || lpsolver == 3) { *status = glp_get_status (lp); *fmin = glp_get_obj_val (lp); } else { *status = glp_ipt_status (lp); *fmin = glp_ipt_obj_val (lp); } } // Get optimal solution (if exists) if (isMIP) { for (int i = 0; i < n; i++) xmin[i] = glp_mip_col_val (lp, i+1); } else { /* Primal values */ for (int i = 0; i < n; i++) { if (lpsolver == 1 || lpsolver == 3) xmin[i] = glp_get_col_prim (lp, i+1); else xmin[i] = glp_ipt_col_prim (lp, i+1); } /* Dual values */ for (int i = 0; i < m; i++) { if (lpsolver == 1 || lpsolver == 3) lambda[i] = glp_get_row_dual (lp, i+1); else lambda[i] = glp_ipt_row_dual (lp, i+1); } /* Reduced costs */ for (int i = 0; i < glp_get_num_cols (lp); i++) { if (lpsolver == 1 || lpsolver == 3) redcosts[i] = glp_get_col_dual (lp, i+1); else redcosts[i] = glp_ipt_col_dual (lp, i+1); } } *time = (clock () - t_start) / CLOCKS_PER_SEC; size_t tpeak; glp_mem_usage(NULL, NULL, NULL, &tpeak); *mem=((double) tpeak) / (1024); lpx_delete_prob(lp); return 0; } else { // printf("errnum is %d\n", errnum); } lpx_delete_prob(lp); /* this shouldn't be nessiary with glp_deleted_prob, but try it if we have weird behavior again... */ glp_free_env(); *status = errnum; return errnum; }
static void update_quality (struct GAS_MLP_Handle *mlp, struct ATS_Address * address) { GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating quality metrics for peer `%s'\n", GNUNET_i2s (&address->peer)); GNUNET_assert (NULL != address); GNUNET_assert (NULL != address->mlp_information); GNUNET_assert (NULL != address->ats); struct MLP_information *mlpi = address->mlp_information; struct GNUNET_ATS_Information *ats = address->ats; GNUNET_assert (mlpi != NULL); int c; for (c = 0; c < GNUNET_ATS_QualityPropertiesCount; c++) { int index = mlp_lookup_ats(address, mlp->q[c]); if (index == GNUNET_SYSERR) continue; GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating address for peer `%s' value `%s': %f\n", GNUNET_i2s (&address->peer), mlp_ats_to_string(mlp->q[c]), (double) ats[index].value); int i = mlpi->q_avg_i[c]; double * qp = mlpi->q[c]; qp[i] = (double) ats[index].value; int t; for (t = 0; t < MLP_AVERAGING_QUEUE_LENGTH; t++) { GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%s': `%s' queue[%u]: %f\n", GNUNET_i2s (&address->peer), mlp_ats_to_string(mlp->q[c]), t, qp[t]); } if (mlpi->q_avg_i[c] + 1 < (MLP_AVERAGING_QUEUE_LENGTH)) mlpi->q_avg_i[c] ++; else mlpi->q_avg_i[c] = 0; int c2; int c3; double avg = 0.0; switch (mlp->q[c]) { case GNUNET_ATS_QUALITY_NET_DELAY: c3 = 0; for (c2 = 0; c2 < MLP_AVERAGING_QUEUE_LENGTH; c2++) { if (mlpi->q[c][c2] != -1) { double * t2 = mlpi->q[c] ; avg += t2[c2]; c3 ++; } } if ((c3 > 0) && (avg > 0)) /* avg = 1 / ((q[0] + ... + q[l]) /c3) => c3 / avg*/ mlpi->q_averaged[c] = (double) c3 / avg; else mlpi->q_averaged[c] = 0.0; GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%s': `%s' average sum: %f, average: %f, weight: %f\n", GNUNET_i2s (&address->peer), mlp_ats_to_string(mlp->q[c]), avg, avg / (double) c3, mlpi->q_averaged[c]); break; case GNUNET_ATS_QUALITY_NET_DISTANCE: c3 = 0; for (c2 = 0; c2 < MLP_AVERAGING_QUEUE_LENGTH; c2++) { if (mlpi->q[c][c2] != -1) { double * t2 = mlpi->q[c] ; avg += t2[c2]; c3 ++; } } if ((c3 > 0) && (avg > 0)) /* avg = 1 / ((q[0] + ... + q[l]) /c3) => c3 / avg*/ mlpi->q_averaged[c] = (double) c3 / avg; else mlpi->q_averaged[c] = 0.0; GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%s': `%s' average sum: %f, average: %f, weight: %f\n", GNUNET_i2s (&address->peer), mlp_ats_to_string(mlp->q[c]), avg, avg / (double) c3, mlpi->q_averaged[c]); break; default: break; } if ((mlpi->c_b != 0) && (mlpi->r_q[c] != 0)) { /* Get current number of columns */ int found = GNUNET_NO; int cols = glp_get_num_cols(mlp->prob); int *ind = GNUNET_malloc (cols * sizeof (int) + 1); double *val = GNUNET_malloc (cols * sizeof (double) + 1); /* Get the matrix row of quality */ int length = glp_get_mat_row(mlp->prob, mlp->r_q[c], ind, val); GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "cols %i, length %i c_b %i\n", cols, length, mlpi->c_b); int c4; /* Get the index if matrix row of quality */ for (c4 = 1; c4 <= length; c4++ ) { if (mlpi->c_b == ind[c4]) { /* Update the value */ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating quality `%s' column `%s' row `%s' : %f -> %f\n", mlp_ats_to_string(mlp->q[c]), glp_get_col_name (mlp->prob, ind[c4]), glp_get_row_name (mlp->prob, mlp->r_q[c]), val[c4], mlpi->q_averaged[c]); val[c4] = mlpi->q_averaged[c]; found = GNUNET_YES; break; } } if (found == GNUNET_NO) { ind[length+1] = mlpi->c_b; val[length+1] = mlpi->q_averaged[c]; GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%i ind[%i] val[%i]: %i %f\n", length+1, length+1, length+1, mlpi->c_b, mlpi->q_averaged[c]); glp_set_mat_row (mlp->prob, mlpi->r_q[c], length+1, ind, val); } else { /* Get the index if matrix row of quality */ glp_set_mat_row (mlp->prob, mlpi->r_q[c], length, ind, val); } GNUNET_free (ind); GNUNET_free (val); } } }
int glpk (int sense, int n, int m, double *c, int nz, int *rn, int *cn, double *a, double *b, char *ctype, int *freeLB, double *lb, int *freeUB, double *ub, int *vartype, int isMIP, int lpsolver, int save_pb, char *save_filename, char *filetype, double *xmin, double *fmin, double *status, double *lambda, double *redcosts, double *time, double *mem) { int typx = 0; int method; clock_t t_start = clock(); // Obsolete //lib_set_fault_hook (NULL, glpk_fault_hook); //Redirect standard output if (glpIntParam[0] > 1) glp_term_hook (glpk_print_hook, NULL); else glp_term_hook (NULL, NULL); //-- Create an empty LP/MILP object glp_prob *lp = glp_create_prob (); //-- Set the sense of optimization if (sense == 1) glp_set_obj_dir (lp, GLP_MIN); else glp_set_obj_dir (lp, GLP_MAX); //-- Define the number of unknowns and their domains. glp_add_cols (lp, n); for (int i = 0; i < n; i++) { //-- Define type of the structural variables if (! freeLB[i] && ! freeUB[i]) glp_set_col_bnds (lp, i+1, GLP_DB, lb[i], ub[i]); else { if (! freeLB[i] && freeUB[i]) glp_set_col_bnds (lp, i+1, GLP_LO, lb[i], ub[i]); else { if (freeLB[i] && ! freeUB[i]) glp_set_col_bnds (lp, i+1, GLP_UP, lb[i], ub[i]); else glp_set_col_bnds (lp, i+1, GLP_FR, lb[i], ub[i]); } } // -- Set the objective coefficient of the corresponding // -- structural variable. No constant term is assumed. glp_set_obj_coef(lp,i+1,c[i]); if (isMIP) glp_set_col_kind (lp, i+1, vartype[i]); } glp_add_rows (lp, m); for (int i = 0; i < m; i++) { /* If the i-th row has no lower bound (types F,U), the corrispondent parameter will be ignored. If the i-th row has no upper bound (types F,L), the corrispondent parameter will be ignored. If the i-th row is of S type, the i-th LB is used, but the i-th UB is ignored. */ switch (ctype[i]) { case 'F': typx = GLP_FR; break; // upper bound case 'U': typx = GLP_UP; break; // lower bound case 'L': typx = GLP_LO; break; // fixed constraint case 'S': typx = GLP_FX; break; // double-bounded variable case 'D': typx = GLP_DB; break; } glp_set_row_bnds (lp, i+1, typx, b[i], b[i]); } // Load constraint matrix A glp_load_matrix (lp, nz, rn, cn, a); // Save problem if (save_pb) { if (!strcmp(filetype,"cplex")){ if (lpx_write_cpxlp (lp, save_filename) != 0) { mexErrMsgTxt("glpkcc: unable to write the problem"); longjmp (mark, -1); } }else{ if (!strcmp(filetype,"fixedmps")){ if (lpx_write_mps (lp, save_filename) != 0) { mexErrMsgTxt("glpkcc: unable to write the problem"); longjmp (mark, -1); } }else{ if (!strcmp(filetype,"freemps")){ if (lpx_write_freemps (lp, save_filename) != 0) { mexErrMsgTxt("glpkcc: unable to write the problem"); longjmp (mark, -1); } }else{// plain text if (lpx_print_prob (lp, save_filename) != 0) { mexErrMsgTxt("glpkcc: unable to write the problem"); longjmp (mark, -1); } } } } } //-- scale the problem data (if required) if (glpIntParam[1] && (! glpIntParam[16] || lpsolver != 1)) lpx_scale_prob (lp); //-- build advanced initial basis (if required) if (lpsolver == 1 && ! glpIntParam[16]) lpx_adv_basis (lp); glp_smcp sParam; glp_init_smcp(&sParam); //-- set control parameters if (lpsolver==1){ //remap of control parameters for simplex method sParam.msg_lev=glpIntParam[0]; // message level // simplex method: primal/dual if (glpIntParam[2]==0) sParam.meth=GLP_PRIMAL; else sParam.meth=GLP_DUALP; // pricing technique if (glpIntParam[3]==0) sParam.pricing=GLP_PT_STD; else sParam.pricing=GLP_PT_PSE; //sParam.r_test not available sParam.tol_bnd=glpRealParam[1]; // primal feasible tollerance sParam.tol_dj=glpRealParam[2]; // dual feasible tollerance sParam.tol_piv=glpRealParam[3]; // pivot tollerance sParam.obj_ll=glpRealParam[4]; // lower limit sParam.obj_ul=glpRealParam[5]; // upper limit // iteration limit if (glpIntParam[5]==-1) sParam.it_lim=INT_MAX; else sParam.it_lim=glpIntParam[5]; // time limit if (glpRealParam[6]==-1) sParam.tm_lim=INT_MAX; else sParam.tm_lim=(int) glpRealParam[6]; sParam.out_frq=glpIntParam[7]; // output frequency sParam.out_dly=(int) glpRealParam[7]; // output delay // presolver if (glpIntParam[16]) sParam.presolve=GLP_ON; else sParam.presolve=GLP_OFF; }else{ for(int i = 0; i < NIntP; i++) lpx_set_int_parm (lp, IParam[i], glpIntParam[i]); for (int i = 0; i < NRealP; i++) lpx_set_real_parm (lp, RParam[i], glpRealParam[i]); } // Choose simplex method ('S') or interior point method ('T') to solve the problem if (lpsolver == 1) method = 'S'; else method = 'T'; int errnum; switch (method){ case 'S': { if (isMIP){ method = 'I'; errnum = lpx_intopt (lp); } else{ errnum = glp_simplex(lp, &sParam); errnum += 100; //this is to avoid ambiguity in the return codes. } } break; case 'T': errnum = lpx_interior(lp); break; default: xassert (method != method); } /* errnum assumes the following results: errnum = 0 <=> No errors errnum = 1 <=> Iteration limit exceeded. errnum = 2 <=> Numerical problems with basis matrix. */ if (errnum == LPX_E_OK || errnum==100){ // Get status and object value if (isMIP) { *status = glp_mip_status (lp); *fmin = glp_mip_obj_val (lp); } else { if (lpsolver == 1) { *status = glp_get_status (lp); *fmin = glp_get_obj_val (lp); } else { *status = glp_ipt_status (lp); *fmin = glp_ipt_obj_val (lp); } } // Get optimal solution (if exists) if (isMIP) { for (int i = 0; i < n; i++) xmin[i] = glp_mip_col_val (lp, i+1); } else { /* Primal values */ for (int i = 0; i < n; i++) { if (lpsolver == 1) xmin[i] = glp_get_col_prim (lp, i+1); else xmin[i] = glp_ipt_col_prim (lp, i+1); } /* Dual values */ for (int i = 0; i < m; i++) { if (lpsolver == 1) lambda[i] = glp_get_row_dual (lp, i+1); else lambda[i] = glp_ipt_row_dual (lp, i+1); } /* Reduced costs */ for (int i = 0; i < glp_get_num_cols (lp); i++) { if (lpsolver == 1) redcosts[i] = glp_get_col_dual (lp, i+1); else redcosts[i] = glp_ipt_col_dual (lp, i+1); } } *time = (clock () - t_start) / CLOCKS_PER_SEC; glp_ulong tpeak; lib_mem_usage(NULL, NULL, NULL, &tpeak); *mem=(double)(4294967296.0 * tpeak.hi + tpeak.lo) / (1024); glp_delete_prob (lp); return 0; } glp_delete_prob (lp); *status = errnum; return errnum; }