glp_prob * montarModeloInicial() { glp_prob *lp; lp = glp_create_prob();//cria problema glp_add_cols(lp, nEstradas); // cria uma variavel por estrada for(int est = 1; est <= nEstradas; ++est) { // para cada estrada glp_set_col_bnds(lp, est, GLP_DB, 0.0, 1.0); // estrada entre 0 e 1 glp_set_col_kind(lp, est, GLP_BV); // estrada binaria } glp_set_obj_dir(lp, GLP_MIN); //MIN for(int est=1; est <= nEstradas; ++est) { // para cada estrada glp_set_obj_coef(lp, est, estradas[est].dist); // custo da estrada eh coeficiente da objetivo } for (int cid = 1; cid <= nCidades; ++cid) { //para cada cidade int indCol[123]; double val[123]; int nCoef = 0; for (int est = 1; est < nEstradas; ++est) { //para cada estrada Estrada estrada = estradas[est]; if (estrada.ori == cid || estrada.dest == cid) { //se cidade toca estrada est indCol[nCoef + 1] = est; // a est-esima estrada val[nCoef + 1] = 1.0; // com coeficiente 1 na linha nCoef++; //incremente numero de coeficiente } } glp_add_rows(lp, 1); glp_set_mat_row(lp, cid, nCoef, indCol, val); // adiciona coeficientes da linha glp_set_row_bnds(lp, cid, GLP_DB, 2.0, 2.0); // restringe linha = 2. } }
/** * Build a model with one column * @param forceError force error if bit 0 = 1 */ void buildModel(int forceError) { glp_prob *lp; /* create problem */ lp = glp_create_prob(); if (forceError & 1) { /* add -1 column * this will cause an error. */ glp_add_cols(lp, -1); } else { /* add 1 column */ glp_add_cols(lp, 1); } /* delete problem */ glp_delete_prob(lp); }
void NUMlinprog_addVariable (NUMlinprog me, double lowerBound, double upperBound, double coeff) { glp_add_cols (my linearProgram, 1); glp_set_col_bnds (my linearProgram, ++ my numberOfVariables, lowerBound == NUMundefined ? ( upperBound == NUMundefined ? GLP_FR : GLP_UP ) : upperBound == NUMundefined ? GLP_LO : lowerBound == upperBound ? GLP_FX : GLP_DB, lowerBound, upperBound); glp_set_obj_coef (my linearProgram, my ivar, coeff); }
/** create the initial glp problem, define the rows and columns **/ static void create_glp(void) {int i; P=glp_create_prob(); glp_add_cols(P,cols); glp_add_rows(P,rows); /* set the objective to all zero */ for(i=0;i<=cols;i++)glp_set_obj_coef(P,i,0.0); glp_set_obj_dir(P,GLP_MIN); /* minimize */ }
int make_variable(double coef) { int col = glp_add_cols(ip_, 1); glp_set_col_bnds(ip_, col, GLP_DB, 0, 1); glp_set_col_kind(ip_, col, GLP_BV); glp_set_obj_coef(ip_, col, coef); return col; }
int make_variable(double coef, int lo, int hi) { int col = glp_add_cols(ip_, 1); glp_set_col_bnds(ip_, col, GLP_DB, lo, hi); glp_set_col_kind(ip_, col, GLP_IV); glp_set_obj_coef(ip_, col, coef); return col; }
int glp_asnprob_lp(glp_prob *P, int form, glp_graph *G, int names, int v_set, int a_cost) { glp_vertex *v; glp_arc *a; int i, j, ret, ind[1+2]; double cost, val[1+2]; if (!(form == GLP_ASN_MIN || form == GLP_ASN_MAX || form == GLP_ASN_MMP)) xerror("glp_asnprob_lp: form = %d; invalid parameter\n", form); if (!(names == GLP_ON || names == GLP_OFF)) xerror("glp_asnprob_lp: names = %d; invalid parameter\n", names); if (v_set >= 0 && v_set > G->v_size - (int)sizeof(int)) xerror("glp_asnprob_lp: v_set = %d; invalid offset\n", v_set); if (a_cost >= 0 && a_cost > G->a_size - (int)sizeof(double)) xerror("glp_asnprob_lp: a_cost = %d; invalid offset\n", a_cost); ret = glp_check_asnprob(G, v_set); if (ret != 0) goto done; glp_erase_prob(P); if (names) glp_set_prob_name(P, G->name); glp_set_obj_dir(P, form == GLP_ASN_MIN ? GLP_MIN : GLP_MAX); if (G->nv > 0) glp_add_rows(P, G->nv); for (i = 1; i <= G->nv; i++) { v = G->v[i]; if (names) glp_set_row_name(P, i, v->name); glp_set_row_bnds(P, i, form == GLP_ASN_MMP ? GLP_UP : GLP_FX, 1.0, 1.0); } if (G->na > 0) glp_add_cols(P, G->na); for (i = 1, j = 0; i <= G->nv; i++) { v = G->v[i]; for (a = v->out; a != NULL; a = a->t_next) { j++; if (names) { char name[50+1]; sprintf(name, "x[%d,%d]", a->tail->i, a->head->i); xassert(strlen(name) < sizeof(name)); glp_set_col_name(P, j, name); } ind[1] = a->tail->i, val[1] = +1.0; ind[2] = a->head->i, val[2] = +1.0; glp_set_mat_col(P, j, 2, ind, val); glp_set_col_bnds(P, j, GLP_DB, 0.0, 1.0); if (a_cost >= 0) memcpy(&cost, (char *)a->data + a_cost, sizeof(double)); else cost = 1.0; glp_set_obj_coef(P, j, cost); } } xassert(j == G->na); done: return ret; }
int main(void) { glp_prob *mip = glp_create_prob(); glp_set_prob_name(mip, "sample"); glp_set_obj_dir(mip, GLP_MAX); // 拘束条件 // 具体的な関数は後で glp_add_rows(mip, 3); // 拘束条件の数 glp_set_row_name(mip, 1, "c1"); glp_set_row_bnds(mip, 1, GLP_DB, 0.0, 20.0); glp_set_row_name(mip, 2, "c2"); glp_set_row_bnds(mip, 2, GLP_DB, 0.0, 30.0); glp_set_row_name(mip, 3, "c3"); glp_set_row_bnds(mip, 3, GLP_FX, 0.0, 0); // 変数 // 変数そのものにかかる拘束は、拘束条件ではなくてこちらで管理 glp_add_cols(mip, 4); // 変数の数 glp_set_col_name(mip, 1, "x1"); glp_set_col_bnds(mip, 1, GLP_DB, 0.0, 40.0); glp_set_obj_coef(mip, 1, 1.0); glp_set_col_name(mip, 2, "x2"); glp_set_col_bnds(mip, 2, GLP_LO, 0.0, 0.0); glp_set_obj_coef(mip, 2, 2.0); glp_set_col_name(mip, 3, "x3"); glp_set_col_bnds(mip, 3, GLP_LO, 0.0, 0.0); glp_set_obj_coef(mip, 3, 3.0); glp_set_col_kind(mip, 3, GLP_IV); // 整数値としての宣言 glp_set_col_name(mip, 4, "x4"); glp_set_col_bnds(mip, 4, GLP_DB, 2.0, 3.0); glp_set_obj_coef(mip, 4, 1.0); glp_set_col_kind(mip, 4, GLP_IV); // 整数値としての宣言 int ia[1+9], ja[1+9]; double ar[1+9]; ia[1]=1,ja[1]=1,ar[1]=-1; // a[1,1] = -1 ia[2]=1,ja[2]=2,ar[2]=1; // a[1,2] = 1 ia[3]=1,ja[3]=3,ar[3]=1; // a[1,3] = 1 ia[4]=1,ja[4]=4,ar[4]=10; // a[1,4] = 10 ia[5]=2,ja[5]=1,ar[5]=1; // a[2,1] = 1 ia[6]=2,ja[6]=2,ar[6]=-3; // a[2,2] = -3 ia[7]=2,ja[7]=3,ar[7]=1; // a[2,3] = 1 ia[8]=3,ja[8]=2,ar[8]=1; // a[3,2] = 1 ia[9]=3,ja[9]=4,ar[9]=-3.5; // a[3,4] = -3.5 glp_load_matrix(mip, 9, ia, ja, ar); glp_iocp parm; glp_init_iocp(&parm); parm.presolve = GLP_ON; int err = glp_intopt(mip, &parm); double z = glp_mip_obj_val(mip); double x1 = glp_mip_col_val(mip, 1); double x2 = glp_mip_col_val(mip, 2); double x3 = glp_mip_col_val(mip, 3); double x4 = glp_mip_col_val(mip, 4); printf("\nz = %g; x1 = %g; x2 = %g; x3 = %g, x4 = %g\n", z, x1, x2, x3, x4); // z = 122.5; x1 = 40; x2 = 10.5; x3 = 19.5, x4 = 3 glp_delete_prob(mip); return 0; }
void NADA() { glp_prob *lp; lp = glp_create_prob(); glp_add_cols(lp, 3); glp_set_col_bnds(lp, 1, GLP_LO, 0.0, 3.0); // 0 <= x1 glp_set_col_bnds(lp, 2, GLP_LO, 0.0, 2.0); // 0 <= x2 glp_set_col_bnds(lp, 3, GLP_LO, 0.0, 0.0); // 0 <= x3 glp_set_obj_dir(lp, GLP_MAX); //max glp_set_obj_coef(lp, 1, -3.0); // -3x1 glp_set_obj_coef(lp, 2, 4.0); // +4x2 glp_set_obj_coef(lp, 3, 11.0); // +11x3 // max -3x1 + 4x2 + 11 x3. int indCol[123]; double val[123]; glp_add_rows(lp, 1); indCol[1] = 1; val[1] = 10; // 10*x1 indCol[2] = 2; val[2] = 3; // 3*x2 glp_set_row_bnds(lp, 1, GLP_UP, 0.0, 15.0);// <=15 glp_set_mat_row(lp, 1, 2, indCol, val);// 10 x1 + 3 x2 <= 15 glp_add_rows(lp, 1); indCol[1] = 3; val[1] = 9; // 9*x3 indCol[2] = 1; val[2] = 7; // 7*x1 glp_set_row_bnds(lp, 2, GLP_UP, 0.0, 38.0);// <=38 glp_set_mat_row(lp, 2, 2, indCol, val);// 7x1+9x2<=38 glp_add_rows(lp, 1); indCol[1] = 3; val[1] = 15; // 15*x3 glp_set_row_bnds(lp, 3, GLP_LO, 0.0, 25.0);// >=25 glp_set_mat_row(lp, 3, 1, indCol, val);// 15x3 >=25 glp_set_col_kind(lp, 1, GLP_IV);// X1 EH INTEIRO glp_set_col_kind(lp, 2, GLP_IV);// X2 EH INTEIRO glp_set_col_kind(lp, 3, GLP_IV);// X3 EH INTEIRO glp_intopt(lp, NULL); // acha solucao com restricao de integralidade // glp_simplex(lp, NULL); // printf("Solucao Otima: %.3f\n", glp_get_obj_val(lp)); // printf("X1: %.3f\n", glp_get_col_prim(lp, 1)); // printf("X2: %.3f\n", glp_get_col_prim(lp, 2)); // printf("X3: %.3f\n", glp_get_col_prim(lp, 3)); printf("Solucao Otima: %.3f\n", glp_mip_obj_val(lp)); printf("X1: %.3f\n", glp_mip_col_val(lp, 1)); printf("X2: %.3f\n", glp_mip_col_val(lp, 2)); printf("X3: %.3f\n", glp_mip_col_val(lp, 3)); // for (int est = 1; est <= nEstradas; ++est) { // glp_set_col_bnds(lp, est, GLP_LO, 0.0, 0.0); // } }
int main(void) { glp_prob *lp; int ia[1+1000], ja[1+1000]; double ar[1+1000], z, x1, x2, x3; s1: lp = glp_create_prob(); s2: glp_set_prob_name(lp, "sample"); s3: glp_set_obj_dir(lp, GLP_MAX); s4: glp_add_rows(lp, 3); s5: glp_set_row_name(lp, 1, "p"); s6: glp_set_row_bnds(lp, 1, GLP_UP, 0.0, 100.0); s7: glp_set_row_name(lp, 2, "q"); s8: glp_set_row_bnds(lp, 2, GLP_UP, 0.0, 600.0); s9: glp_set_row_name(lp, 3, "r"); s10: glp_set_row_bnds(lp, 3, GLP_UP, 0.0, 300.0); s11: glp_add_cols(lp, 3); s12: glp_set_col_name(lp, 1, "x1"); s13: glp_set_col_bnds(lp, 1, GLP_LO, 0.0, 0.0); s14: glp_set_obj_coef(lp, 1, 10.0); s15: glp_set_col_name(lp, 2, "x2"); s16: glp_set_col_bnds(lp, 2, GLP_LO, 0.0, 0.0); s17: glp_set_obj_coef(lp, 2, 6.0); s18: glp_set_col_name(lp, 3, "x3"); s19: glp_set_col_bnds(lp, 3, GLP_LO, 0.0, 0.0); s20: glp_set_obj_coef(lp, 3, 4.0); s21: ia[1] = 1, ja[1] = 1, ar[1] = 1.0; /* a[1,1] = 1 */ s22: ia[2] = 1, ja[2] = 2, ar[2] = 1.0; /* a[1,2] = 1 */ s23: ia[3] = 1, ja[3] = 3, ar[3] = 1.0; /* a[1,3] = 1 */ s24: ia[4] = 2, ja[4] = 1, ar[4] = 10.0; /* a[2,1] = 10 */ s25: ia[5] = 3, ja[5] = 1, ar[5] = 2.0; /* a[3,1] = 2 */ s26: ia[6] = 2, ja[6] = 2, ar[6] = 4.0; /* a[2,2] = 4 */ s27: ia[7] = 3, ja[7] = 2, ar[7] = 2.0; /* a[3,2] = 2 */ s28: ia[8] = 2, ja[8] = 3, ar[8] = 5.0; /* a[2,3] = 5 */ s29: ia[9] = 3, ja[9] = 3, ar[9] = 6.0; /* a[3,3] = 6 */ s30: glp_load_matrix(lp, 9, ia, ja, ar); s31: glp_simplex(lp, NULL); s32: z = glp_get_obj_val(lp); s33: x1 = glp_get_col_prim(lp, 1); s34: x2 = glp_get_col_prim(lp, 2); s35: x3 = glp_get_col_prim(lp, 3); s36: printf("\nz = %g; x1 = %g; x2 = %g; x3 = %g\n", z, x1, x2, x3); s37: glp_delete_prob(lp); return 0; }
void B2GlpkHasher::add_trace_vars(const B2TraceCoeffs &trace_coeffs) { const B2HashMap<B2Trace, unsigned int> &trace_vals = _trace_set.trace_vals(); glp_add_cols(_lp, trace_vals.size()); for(B2HashMap<B2Trace, unsigned int>::const_iterator trace_it = trace_vals.begin(); trace_it != trace_vals.end(); ++trace_it) { B2Trace trace = trace_it->first; unsigned int trace_var_idx = trace_it->second; glp_set_col_kind(_lp, trace_var_idx, GLP_BV); glp_set_col_name(_lp, trace_var_idx, B2TraceName(trace).name()); glp_set_col_bnds(_lp, trace_var_idx, GLP_DB, 0.0, 1.0); glp_set_obj_coef(_lp, trace_var_idx, trace_coeffs[trace]); }; };
void glpk_wrapper::init_problem() { // use minimzation by default glp_set_obj_dir(lp, GLP_MIN); // create as many col as dimension in b glp_add_cols(lp, domain.size()); // name the variables (helps debugging) if (DREAL_LOG_INFO_IS_ON) { for (unsigned int i = 0; i < domain.size(); i++) { glp_set_col_name(lp, i+1, domain.get_name(i).c_str()); } } // set_domain(domain); }
/** * Add columns for all addresses * * @param cls GAS_MLP_Handle * @param key Hashcode * @param value ATS_Address * * @return GNUNET_OK to continue */ static int create_columns_it (void *cls, const GNUNET_HashCode * key, void *value) { struct GAS_MLP_Handle *mlp = cls; struct ATS_Address *address = value; struct MLP_information *mlpi; unsigned int col; char *name; GNUNET_assert (address->mlp_information != NULL); mlpi = address->mlp_information; /* Add bandwidth column */ col = glp_add_cols (mlp->prob, 2); mlpi->c_b = col; mlpi->c_n = col + 1; GNUNET_asprintf (&name, "b_%s_%s", GNUNET_i2s (&address->peer), address->plugin); glp_set_col_name (mlp->prob, mlpi->c_b , name); GNUNET_free (name); /* Lower bound == 0 */ glp_set_col_bnds (mlp->prob, mlpi->c_b , GLP_LO, 0.0, 0.0); /* Continuous value*/ glp_set_col_kind (mlp->prob, mlpi->c_b , GLP_CV); /* Objective function coefficient == 0 */ glp_set_obj_coef (mlp->prob, mlpi->c_b , 0); /* Add usage column */ GNUNET_asprintf (&name, "n_%s_%s", GNUNET_i2s (&address->peer), address->plugin); glp_set_col_name (mlp->prob, mlpi->c_n, name); GNUNET_free (name); /* Limit value : 0 <= value <= 1 */ glp_set_col_bnds (mlp->prob, mlpi->c_n, GLP_DB, 0.0, 1.0); /* Integer value*/ glp_set_col_kind (mlp->prob, mlpi->c_n, GLP_IV); /* Objective function coefficient == 0 */ glp_set_obj_coef (mlp->prob, mlpi->c_n, 0); return GNUNET_OK; }
static int find_col(struct csa *csa, char *name) { /* find column by its symbolic name */ int j; j = glp_find_col(csa->P, name); if (j == 0) { /* not found; create new column */ j = glp_add_cols(csa->P, 1); glp_set_col_name(csa->P, j, name); /* enlarge working arrays, if necessary */ if (csa->n_max < j) { int n_max = csa->n_max; int *ind = csa->ind; double *val = csa->val; char *flag = csa->flag; double *lb = csa->lb; double *ub = csa->ub; csa->n_max += csa->n_max; csa->ind = xcalloc(1+csa->n_max, sizeof(int)); memcpy(&csa->ind[1], &ind[1], n_max * sizeof(int)); xfree(ind); csa->val = xcalloc(1+csa->n_max, sizeof(double)); memcpy(&csa->val[1], &val[1], n_max * sizeof(double)); xfree(val); csa->flag = xcalloc(1+csa->n_max, sizeof(char)); memset(&csa->flag[1], 0, csa->n_max * sizeof(char)); memcpy(&csa->flag[1], &flag[1], n_max * sizeof(char)); xfree(flag); csa->lb = xcalloc(1+csa->n_max, sizeof(double)); memcpy(&csa->lb[1], &lb[1], n_max * sizeof(double)); xfree(lb); csa->ub = xcalloc(1+csa->n_max, sizeof(double)); memcpy(&csa->ub[1], &ub[1], n_max * sizeof(double)); xfree(ub); } csa->lb[j] = +DBL_MAX, csa->ub[j] = -DBL_MAX; } return j; }
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; }
void Add_obj() { int i; char num[5]; sig = (float*)malloc(n * sizeof(float)); for(i = 0; i < n; i++) sig[i] = 1.0; stdev = sqrt(1.0 / (2*CODERATE*pow(10,SNR/10))); for(i = 0; i < n; i++) sig[i] += GeneGauss(0,stdev); ///////////////////////////////////////////////////////// // assign the sig to cplex as the r // here just print them /* glpk_data = fopen("//home//why/document//lp//lp.txt","a+"); fprintf(glpk_data,"minimize f : %f*f1",sig[0]); for(int i =1; i < n; i ++) fprintf(glpk_data,"+%f*f%d" , sig[i],i+1); fprintf(glpk_data,";\n"); fclose(glpk_data); */ ///////////////////////////////////////////////////////// glp_add_cols(lp, n); glp_set_obj_dir(lp, GLP_MIN); for(i = 1; i <= n; i ++) { sprintf(num, "%d", i); glp_set_col_name(lp, i, num); glp_set_col_bnds(lp, i, GLP_DB, 0.0, 1.0); glp_set_obj_coef(lp, i, sig[i-1]); } free(sig); }
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; }
/* Restriction bci = ai - bi + xi = Ti and z += ai*Ei + bi*Li */ void addBasicRestriction(glp_prob * Prob, int plane) { int cardinal, constr[4], i = plane, cardRow; double cValues[4]; char buf[AUXSIZE]; cardinal = glp_add_cols(Prob, 3); sprintf(buf,"a%i",i); glp_set_col_name(Prob, cardinal, buf); glp_set_col_bnds(Prob, cardinal, GLP_LO, 0, 0); glp_set_obj_coef(Prob, cardinal, planes[i].costE); sprintf(buf,"b%i",i); glp_set_col_name(Prob, cardinal+1, buf); glp_set_col_bnds(Prob, cardinal+1, GLP_LO, 0, 0); glp_set_obj_coef(Prob, cardinal+1, planes[i].costL); sprintf(buf,"x%i",i); glp_set_col_name(Prob, cardinal+2, buf); if( planes[i].earliest == planes[i].latest ) glp_set_col_bnds(Prob, cardinal+2, GLP_FX, planes[i].earliest, 0); else glp_set_col_bnds(Prob, cardinal+2, GLP_DB, planes[i].earliest, planes[i].latest); cardRow = glp_add_rows(Prob, 1); sprintf(buf,"bc%i",i); glp_set_row_name(Prob, cardRow, buf); glp_set_row_bnds(Prob, cardRow, GLP_FX, planes[i].ideal, 0); constr[3] = 1 + (constr[2] = 1 + (constr[1] = cardinal)); cValues[3] = cValues[1] = 1; cValues[2] = -1; glp_set_mat_row(Prob, cardRow, 3, constr, cValues); }
/** * Create the MLP problem * * @param mlp the MLP handle * @param addresses the hashmap containing all adresses * @return GNUNET_OK or GNUNET_SYSERR */ static int mlp_create_problem (struct GAS_MLP_Handle *mlp, struct GNUNET_CONTAINER_MultiHashMap * addresses) { int res = GNUNET_OK; int col; int c; char *name; GNUNET_assert (mlp->prob == NULL); /* create the glpk problem */ mlp->prob = glp_create_prob (); /* Set a problem name */ glp_set_prob_name (mlp->prob, "gnunet ats bandwidth distribution"); /* Set optimization direction to maximize */ glp_set_obj_dir (mlp->prob, GLP_MAX); /* Adding invariant columns */ /* Diversity d column */ col = glp_add_cols (mlp->prob, 1); mlp->c_d = col; /* Column name */ glp_set_col_name (mlp->prob, col, "d"); /* Column objective function coefficient */ glp_set_obj_coef (mlp->prob, col, mlp->co_D); /* Column lower bound = 0.0 */ glp_set_col_bnds (mlp->prob, col, GLP_LO, 0.0, 0.0); /* Utilization u column */ col = glp_add_cols (mlp->prob, 1); mlp->c_u = col; /* Column name */ glp_set_col_name (mlp->prob, col, "u"); /* Column objective function coefficient */ glp_set_obj_coef (mlp->prob, col, mlp->co_U); /* Column lower bound = 0.0 */ glp_set_col_bnds (mlp->prob, col, GLP_LO, 0.0, 0.0); #if ENABLE_C9 /* Relativity r column */ col = glp_add_cols (mlp->prob, 1); mlp->c_r = col; /* Column name */ glp_set_col_name (mlp->prob, col, "r"); /* Column objective function coefficient */ glp_set_obj_coef (mlp->prob, col, mlp->co_R); /* Column lower bound = 0.0 */ glp_set_col_bnds (mlp->prob, col, GLP_LO, 0.0, 0.0); #endif /* Quality metric columns */ col = glp_add_cols(mlp->prob, mlp->m_q); for (c = 0; c < mlp->m_q; c++) { mlp->c_q[c] = col + c; GNUNET_asprintf (&name, "q_%u", mlp->q[c]); glp_set_col_name (mlp->prob, col + c, name); /* Column lower bound = 0.0 */ glp_set_col_bnds (mlp->prob, col + c, GLP_LO, 0.0, 0.0); GNUNET_free (name); /* Coefficient == Qm */ glp_set_obj_coef (mlp->prob, col + c, mlp->co_Q[c]); } /* Add columns for addresses */ GNUNET_CONTAINER_multihashmap_iterate (addresses, create_columns_it, mlp); /* Add constraints */ mlp_add_constraints_all_addresses (mlp, addresses); /* Load the matrix */ glp_load_matrix(mlp->prob, (mlp->ci-1), mlp->ia, mlp->ja, mlp->ar); return res; }
int lpx_add_cols(LPX *lp, int ncs) { /* add new columns to problem object */ return glp_add_cols(lp, ncs); }
int main() { // Variáveis auxiliares int i, j, constraintNumber, *constraintIndices; double *constraintCoefficients; // Aloca os vetores utilizados para criar as restrições do problema // *********************************************************************************************** // ATENÇÃO ===> É importante dizer que estes vetores serão utilizados da posição 1 em diante // Ou seja, no GLPK você aloca uma posição a mais e descarta a posição 0 dos vetores. // *********************************************************************************************** constraintIndices = (int*)malloc((n+1)*sizeof(int)); constraintCoefficients = (double*)malloc((n+1)*sizeof(double)); // Cria um modelo com nenhuma variável e nenhuma restrição glp_prob *model = glp_create_prob(); // Define o sentido da otimização que, para este problema, é minimização glp_set_obj_dir(model, GLP_MIN); // Cria as variáveis (colunas) no modelo // Para este problema são necessárias n*n variáveis // Estas n*n variáveis são definidas pelo GLPK através dos indices que vão de 1 até n*n (x1, x2, ..., x(n*n)) // Portanto, neste momento é importante determinar qual variável no GLPK (índice) representará qual variável x[i,j] // Para tanto, fazemos o mapeamento das variáveis x[i,j] utilizando a fórmula (i-1)*n + j // Isto é, a variável x[i,j] será representada pela variável de índice (i-1)*n + j no modelo do GLPK // Note que é imprescindível que cada índice (variável do GLPK) seja associado a no máximo uma variável x[i,j] // Caso contrário, uma variável do GLPK pode representar duas variáveis x[i,j] diferentes que assumem valores distintos nas soluções ótimas // Neste caso, o modelo estará incorreto glp_add_cols(model, n*n); // Ajuste dos tipos, limitantes e coeficientes da função objetivo das variáveis do modelo for (i = 1; i <= n; i++) for (j = 1; j <= n; j++) { // Define o tipo da variável como sendo binária (em outros modelos poderia ser contínua (GLP_CV) ou inteira (GLP_IV)) glp_set_col_kind(model, (i-1)*n + j, GLP_BV); // Define o limitante inferior (0) e superior (1) da variável // Consultem no manual as outras forma para definir apenas o limitante inferior ou superior glp_set_col_bnds(model, (i-1)*n + j, GLP_DB, 0.0, 1.0); // Define o coeficiente da variável na função objetivo glp_set_obj_coef(model, (i-1)*n + j, c[i-1][j-1]); } // Cria no modelo 2n restrições (linhas) nulas (com os coeficientes e limitantes zerados) // Ou seja, neste momento é criada uma matriz de zeros que correspondem aos coeficientes das restrições do modelo // O próximo passo será modificar esta matriz de tal forma que ela represente as restrições do problema descrito glp_add_rows(model, 2*n); // Esta variável define qual das restrições (qual linha da matriz de coeficientes) estamos modificando // ************************************************************************************************************ // ATENÇÃO: perceba que as restrições (linhas), assim como as variáveis (colunas), são indexadas a partir de 1. // ************************************************************************************************************ constraintNumber = 1; // Preenchimento das restrições limitando a soma das linhas: // sum{j in 1..n} w[i,j]*x[i,j] <= u[i] para i in 1..n for (i = 1; i <= n; i++) { // Define o limite superior (RHS) da restrição glp_set_row_bnds(model, constraintNumber, GLP_UP, 0.0, u[i-1]); for (j = 1; j <= n; j++) { // Ajusta o índice da variável que será informado à rotina do GLPK constraintIndices[j] = (i-1)*n + j; // Ajusta o coeficiente da variável cujo índice foi definido na linha anterior para ser informado ao GLPK // ****************************************************************************************************** // ATENÇÃO: perceba que na matriz w os índices e colunas são indexados a partir de ZERO ! // ****************************************************************************************************** constraintCoefficients[j] = w[i-1][j-1]; } // Passa ao GLPK a restrição que acabou de ser definida nos vetores constraintIndices e constraintCoefficients glp_set_mat_row(model, constraintNumber, n, constraintIndices, constraintCoefficients); // atualiza o indice da próxima restrição a ser inserida constraintNumber++; } // Preenchimento das restrições limitando a soma das colunas: // sum{i in 1..n} w[i,j]*x[i,j] >= l[i] para j in 1..n for (j = 1; j <= n; j++) { // Define o limite inferior (RHS) da restrição glp_set_row_bnds(model, constraintNumber, GLP_LO, l[j-1], 0.0); for (i = 1; i <= n; i++) { // Ajusta o índice da variável que será informado a rotina do GLPK constraintIndices[i] = (i-1)*n + j; // Ajusta o coeficiente da variável cujo índice foi definido na linha anterior para ser informado ao GLPK constraintCoefficients[i] = w[i-1][j-1]; } // Passa ao GLPK a restrição que acabou de ser definida nos vetores constraintIndices e constraintCoefficients glp_set_mat_row(model, constraintNumber, n, constraintIndices, constraintCoefficients); // atualiza o indice da próxima restrição a ser inserida constraintNumber++; } // Define os parâmetros que serão passados ao resolvedor glp_iocp param; glp_init_iocp(¶m); // Ativa o presolver param.presolve = GLP_ON; // Resolve o modelo int status = glp_intopt(model, ¶m); // Verifica se houve algum erro durante a otimização if (status) { printf("Ocorreu um erro durante o processo de otimizacao.\n"); } else { // Verifica se o método encontrou uma solução status = glp_mip_status(model); if ((status == GLP_OPT) || (status == GLP_FEAS)) { // Imprime a solução encontrada if (status == GLP_OPT) printf("Solucao otima encontrada!\n"); else printf("A solucao encontrada pode nao ser otima!\n"); printf("Custo da solucao: %f\n", glp_mip_obj_val(model)); for (i = 1; i <= n; i++) { for (j = 1; j <= n; j++) printf("%f ", glp_mip_col_val(model, (i-1)*n + j)); printf("\n"); } } else { printf("Nenhuma solucao foi encontrada!\n"); } } // Desaloca os vetores free(constraintIndices); free(constraintCoefficients); return 0; }
void glp_copy_prob(glp_prob *dest, glp_prob *prob, int names) { glp_tree *tree = dest->tree; glp_bfcp bfcp; int i, j, len, *ind; double *val; if (tree != NULL && tree->reason != 0) xerror("glp_copy_prob: operation not allowed\n"); if (dest == prob) xerror("glp_copy_prob: copying problem object to itself not al" "lowed\n"); if (!(names == GLP_ON || names == GLP_OFF)) xerror("glp_copy_prob: names = %d; invalid parameter\n", names); glp_erase_prob(dest); if (names && prob->name != NULL) glp_set_prob_name(dest, prob->name); if (names && prob->obj != NULL) glp_set_obj_name(dest, prob->obj); dest->dir = prob->dir; dest->c0 = prob->c0; if (prob->m > 0) glp_add_rows(dest, prob->m); if (prob->n > 0) glp_add_cols(dest, prob->n); glp_get_bfcp(prob, &bfcp); glp_set_bfcp(dest, &bfcp); dest->pbs_stat = prob->pbs_stat; dest->dbs_stat = prob->dbs_stat; dest->obj_val = prob->obj_val; dest->some = prob->some; dest->ipt_stat = prob->ipt_stat; dest->ipt_obj = prob->ipt_obj; dest->mip_stat = prob->mip_stat; dest->mip_obj = prob->mip_obj; for (i = 1; i <= prob->m; i++) { GLPROW *to = dest->row[i]; GLPROW *from = prob->row[i]; if (names && from->name != NULL) glp_set_row_name(dest, i, from->name); to->type = from->type; to->lb = from->lb; to->ub = from->ub; to->rii = from->rii; to->stat = from->stat; to->prim = from->prim; to->dual = from->dual; to->pval = from->pval; to->dval = from->dval; to->mipx = from->mipx; } ind = xcalloc(1+prob->m, sizeof(int)); val = xcalloc(1+prob->m, sizeof(double)); for (j = 1; j <= prob->n; j++) { GLPCOL *to = dest->col[j]; GLPCOL *from = prob->col[j]; if (names && from->name != NULL) glp_set_col_name(dest, j, from->name); to->kind = from->kind; to->type = from->type; to->lb = from->lb; to->ub = from->ub; to->coef = from->coef; len = glp_get_mat_col(prob, j, ind, val); glp_set_mat_col(dest, j, len, ind, val); to->sjj = from->sjj; to->stat = from->stat; to->prim = from->prim; to->dual = from->dual; to->pval = from->pval; to->dval = from->dval; to->mipx = from->mipx; } xfree(ind); xfree(val); return; }
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; }
int max_flow_lp(int nn, int ne, const int beg[/*1+ne*/], const int end[/*1+ne*/], const int cap[/*1+ne*/], int s, int t, int x[/*1+ne*/]) { glp_prob *lp; glp_smcp smcp; int i, k, nz, flow, *rn, *cn; double temp, *aa; /* create LP problem instance */ lp = glp_create_prob(); /* create LP rows; i-th row is the conservation condition of the * flow at i-th node, i = 1, ..., nn */ glp_add_rows(lp, nn); for (i = 1; i <= nn; i++) glp_set_row_bnds(lp, i, GLP_FX, 0.0, 0.0); /* create LP columns; k-th column is the elementary flow thru * k-th edge, k = 1, ..., ne; the last column with the number * ne+1 is the total flow through the network, which goes along * a dummy feedback edge from the sink to the source */ glp_add_cols(lp, ne+1); for (k = 1; k <= ne; k++) { xassert(cap[k] > 0); glp_set_col_bnds(lp, k, GLP_DB, -cap[k], +cap[k]); } glp_set_col_bnds(lp, ne+1, GLP_FR, 0.0, 0.0); /* build the constraint matrix; structurally this matrix is the * incidence matrix of the network, so each its column (including * the last column for the dummy edge) has exactly two non-zero * entries */ rn = xalloc(1+2*(ne+1), sizeof(int)); cn = xalloc(1+2*(ne+1), sizeof(int)); aa = xalloc(1+2*(ne+1), sizeof(double)); nz = 0; for (k = 1; k <= ne; k++) { /* x[k] > 0 means the elementary flow thru k-th edge goes from * node beg[k] to node end[k] */ nz++, rn[nz] = beg[k], cn[nz] = k, aa[nz] = -1.0; nz++, rn[nz] = end[k], cn[nz] = k, aa[nz] = +1.0; } /* total flow thru the network goes from the sink to the source * along the dummy feedback edge */ nz++, rn[nz] = t, cn[nz] = ne+1, aa[nz] = -1.0; nz++, rn[nz] = s, cn[nz] = ne+1, aa[nz] = +1.0; /* check the number of non-zero entries */ xassert(nz == 2*(ne+1)); /* load the constraint matrix into the LP problem object */ glp_load_matrix(lp, nz, rn, cn, aa); xfree(rn); xfree(cn); xfree(aa); /* objective function is the total flow through the network to * be maximized */ glp_set_obj_dir(lp, GLP_MAX); glp_set_obj_coef(lp, ne + 1, 1.0); /* solve LP instance with the (primal) simplex method */ glp_term_out(0); glp_adv_basis(lp, 0); glp_term_out(1); glp_init_smcp(&smcp); smcp.msg_lev = GLP_MSG_ON; smcp.out_dly = 5000; xassert(glp_simplex(lp, &smcp) == 0); xassert(glp_get_status(lp) == GLP_OPT); /* obtain optimal elementary flows thru edges of the network */ /* (note that the constraint matrix is unimodular and the data * are integral, so all elementary flows in basic solution should * also be integral) */ for (k = 1; k <= ne; k++) { temp = glp_get_col_prim(lp, k); x[k] = (int)floor(temp + .5); xassert(fabs(x[k] - temp) <= 1e-6); } /* obtain the maximum flow thru the original network which is the * flow thru the dummy feedback edge */ temp = glp_get_col_prim(lp, ne+1); flow = (int)floor(temp + .5); xassert(fabs(flow - temp) <= 1e-6); /* delete LP problem instance */ glp_delete_prob(lp); /* return to the calling program */ return flow; }
void glp_maxflow_lp(glp_prob *lp, glp_graph *G, int names, int s, int t, int a_cap) { glp_vertex *v; glp_arc *a; int i, j, type, ind[1+2]; double cap, val[1+2]; if (!(names == GLP_ON || names == GLP_OFF)) xerror("glp_maxflow_lp: names = %d; invalid parameter\n", names); if (!(1 <= s && s <= G->nv)) xerror("glp_maxflow_lp: s = %d; source node number out of rang" "e\n", s); if (!(1 <= t && t <= G->nv)) xerror("glp_maxflow_lp: t = %d: sink node number out of range " "\n", t); if (s == t) xerror("glp_maxflow_lp: s = t = %d; source and sink nodes must" " be distinct\n", s); if (a_cap >= 0 && a_cap > G->a_size - (int)sizeof(double)) xerror("glp_maxflow_lp: a_cap = %d; invalid offset\n", a_cap); glp_erase_prob(lp); if (names) glp_set_prob_name(lp, G->name); glp_set_obj_dir(lp, GLP_MAX); glp_add_rows(lp, G->nv); for (i = 1; i <= G->nv; i++) { v = G->v[i]; if (names) glp_set_row_name(lp, i, v->name); if (i == s) type = GLP_LO; else if (i == t) type = GLP_UP; else type = GLP_FX; glp_set_row_bnds(lp, i, type, 0.0, 0.0); } if (G->na > 0) glp_add_cols(lp, G->na); for (i = 1, j = 0; i <= G->nv; i++) { v = G->v[i]; for (a = v->out; a != NULL; a = a->t_next) { j++; if (names) { char name[50+1]; sprintf(name, "x[%d,%d]", a->tail->i, a->head->i); xassert(strlen(name) < sizeof(name)); glp_set_col_name(lp, j, name); } if (a->tail->i != a->head->i) { ind[1] = a->tail->i, val[1] = +1.0; ind[2] = a->head->i, val[2] = -1.0; glp_set_mat_col(lp, j, 2, ind, val); } if (a_cap >= 0) memcpy(&cap, (char *)a->data + a_cap, sizeof(double)); else cap = 1.0; if (cap == DBL_MAX) type = GLP_LO; else if (cap != 0.0) type = GLP_DB; else type = GLP_FX; glp_set_col_bnds(lp, j, type, 0.0, cap); if (a->tail->i == s) glp_set_obj_coef(lp, j, +1.0); else if (a->head->i == s) glp_set_obj_coef(lp, j, -1.0); } } xassert(j == G->na); return; }
void glp_mincost_lp(glp_prob *lp, glp_graph *G, int names, int v_rhs, int a_low, int a_cap, int a_cost) { glp_vertex *v; glp_arc *a; int i, j, type, ind[1+2]; double rhs, low, cap, cost, val[1+2]; if (!(names == GLP_ON || names == GLP_OFF)) xerror("glp_mincost_lp: names = %d; invalid parameter\n", names); if (v_rhs >= 0 && v_rhs > G->v_size - (int)sizeof(double)) xerror("glp_mincost_lp: v_rhs = %d; invalid offset\n", v_rhs); if (a_low >= 0 && a_low > G->a_size - (int)sizeof(double)) xerror("glp_mincost_lp: a_low = %d; invalid offset\n", a_low); if (a_cap >= 0 && a_cap > G->a_size - (int)sizeof(double)) xerror("glp_mincost_lp: a_cap = %d; invalid offset\n", a_cap); if (a_cost >= 0 && a_cost > G->a_size - (int)sizeof(double)) xerror("glp_mincost_lp: a_cost = %d; invalid offset\n", a_cost) ; glp_erase_prob(lp); if (names) glp_set_prob_name(lp, G->name); if (G->nv > 0) glp_add_rows(lp, G->nv); for (i = 1; i <= G->nv; i++) { v = G->v[i]; if (names) glp_set_row_name(lp, i, v->name); if (v_rhs >= 0) memcpy(&rhs, (char *)v->data + v_rhs, sizeof(double)); else rhs = 0.0; glp_set_row_bnds(lp, i, GLP_FX, rhs, rhs); } if (G->na > 0) glp_add_cols(lp, G->na); for (i = 1, j = 0; i <= G->nv; i++) { v = G->v[i]; for (a = v->out; a != NULL; a = a->t_next) { j++; if (names) { char name[50+1]; sprintf(name, "x[%d,%d]", a->tail->i, a->head->i); xassert(strlen(name) < sizeof(name)); glp_set_col_name(lp, j, name); } if (a->tail->i != a->head->i) { ind[1] = a->tail->i, val[1] = +1.0; ind[2] = a->head->i, val[2] = -1.0; glp_set_mat_col(lp, j, 2, ind, val); } if (a_low >= 0) memcpy(&low, (char *)a->data + a_low, sizeof(double)); else low = 0.0; if (a_cap >= 0) memcpy(&cap, (char *)a->data + a_cap, sizeof(double)); else cap = 1.0; if (cap == DBL_MAX) type = GLP_LO; else if (low != cap) type = GLP_DB; else type = GLP_FX; glp_set_col_bnds(lp, j, type, low, cap); if (a_cost >= 0) memcpy(&cost, (char *)a->data + a_cost, sizeof(double)); else cost = 0.0; glp_set_obj_coef(lp, j, cost); } } xassert(j == G->na); return; }
int main(int argc, char *argv[]) { /* Structures de données propres à GLPK */ glp_prob *prob; // Déclaration d'un pointeur sur le problème int ia[1 + NBCREUX]; int ja[1 + NBCREUX]; double ar[1 + NBCREUX]; // Déclaration des 3 tableaux servant à définir la partie creuse de la matrice des contraintes /* Variables récupérant les résultats de la résolution du problème (fonction objectif et valeur des variables) */ int i, j; double z; double x[NBVAR]; // Autres variables int * p = (int*)malloc(n * sizeof(int)); p[1] = 34; p[2] = 6; p[3] = 8; p[4] = 17; p[5] = 16; p[6] = 5; p[7] = 13; p[8] = 21; p[9] = 25; p[10] = 31; p[11] = 14; p[12] = 13; p[13] = 33; p[14] = 9; p[15] = 25; p[16] = 25; /* Transfert de ces données dans les structures utilisées par la bibliothèque GLPK */ prob = glp_create_prob(); /* allocation mémoire pour le problème */ glp_set_prob_name(prob, "wagons"); /* affectation d'un nom */ glp_set_obj_dir(prob, GLP_MIN); /* Il s'agit d'un problème de minimisation */ /* Déclaration du nombre de contraintes (nombre de lignes de la matrice des contraintes) */ glp_add_rows(prob, NBCONTR); /* On commence par préciser les bornes sur les contraintes, les indices commencent à 1 (!) dans GLPK */ /* Premier ensemble de contraintes ( c = 1 ) */ for(i = 1; i <= n; i++) { glp_set_row_bnds(prob, i, GLP_FX, 1.0, 1.0); } /* Second ensembles de contraintes (c <= 0 ) */ for(i = n + 1; i <= NBCONTR; i++) { glp_set_row_bnds(prob, i, GLP_UP, 0.0, 0.0); } /* Déclaration du nombre de variables */ glp_add_cols(prob, NBVAR); /* On précise le type des variables, les indices commencent à 1 également pour les variables! */ for(i = 1; i <= NBVAR - 1; i++) { glp_set_col_bnds(prob, i, GLP_DB, 0.0, 1.0); glp_set_col_kind(prob, i, GLP_BV); /* les variables sont binaires */ } glp_set_col_bnds(prob, NBVAR, GLP_LO, 0.0, 0.0); /* La dernière variables est continue (par défaut) non négative */ /* Définition des coefficients des variables dans la fonction objectif */ for(i = 1;i <= n*m;i++) { glp_set_obj_coef(prob,i,0.0); // Tous les coûts sont à 0 (sauf le dernier) } /* Dernier coût (qui vaut 1) */ glp_set_obj_coef(prob,n*m + 1,1.0); /* Définition des coefficients non-nuls dans la matrice des contraintes, autrement dit les coefficients de la matrice creuse */ int pos = 1; for(i = 1; i <= n; i++) { for(j = 1; j <= m; j++) { // Première moitié de la matrice ja[pos] = (i - 1)*m + j; ia[pos] = i; ar[pos] = 1; pos++; // Deuxième moitié de la matrice ja[pos] = (i - 1)*m + j; ia[pos] = n + j; ar[pos] = p[i]; pos++; } } // ajout des -1 dans la dernière colonne for(i = n + 1; i <= n + m; i++) { ja[pos] = n*m + 1; ia[pos] = i; ar[pos] = -1; pos++; } /* Chargement de la matrice dans le problème */ glp_load_matrix(prob,NBCREUX,ia,ja,ar); /* Ecriture de la modélisation dans un fichier */ glp_write_lp(prob,NULL,"wagons.lp"); /* Résolution, puis lecture des résultats */ glp_simplex(prob,NULL); glp_intopt(prob,NULL); /* Résolution */ z = glp_mip_obj_val(prob); /* Récupération de la valeur optimale. Dans le cas d'un problème en variables continues, l'appel est différent : z = glp_get_obj_val(prob); */ for(i = 0;i < NBVAR; i++) x[i] = glp_mip_col_val(prob,i+1); /* Récupération de la valeur des variables, Appel différent dans le cas d'un problème en variables continues : for(i = 0;i < p.nbvar;i++) x[i] = glp_get_col_prim(prob,i+1); */ printf("z = %lf\n",z); for(i = 0;i < NBVAR;i++) printf("x%c = %d, ",'B'+i,(int)(x[i] + 0.5)); /* un cast est ajouté, x[i] pourrait être égal à 0.99999... */ puts(""); /* Libération de la mémoire */ glp_delete_prob(prob); free(p); return 0; }
int c_simplex_sparse(int m, int n, DMAT(c), DMAT(b), DVEC(s)) { glp_prob *lp; lp = glp_create_prob(); glp_set_obj_dir(lp, GLP_MAX); int i,j,k; int tot = cr - n; glp_add_rows(lp, m); glp_add_cols(lp, n); //printf("%d %d\n",m,n); // the first n values for (k=1;k<=n;k++) { glp_set_obj_coef(lp, k, AT(c, k-1, 2)); //printf("%d %f\n",k,AT(c, k-1, 2)); } int * ia = malloc((1+tot)*sizeof(int)); int * ja = malloc((1+tot)*sizeof(int)); double * ar = malloc((1+tot)*sizeof(double)); for (k=1; k<= tot; k++) { ia[k] = rint(AT(c,k-1+n,0)); ja[k] = rint(AT(c,k-1+n,1)); ar[k] = AT(c,k-1+n,2); //printf("%d %d %f\n",ia[k],ja[k],ar[k]); } glp_load_matrix(lp, tot, ia, ja, ar); int t; for (i=1;i<=m;i++) { switch((int)rint(AT(b,i-1,0))) { case 0: { t = GLP_FR; break; } case 1: { t = GLP_LO; break; } case 2: { t = GLP_UP; break; } case 3: { t = GLP_DB; break; } default: { t = GLP_FX; break; } } glp_set_row_bnds(lp, i, t , AT(b,i-1,1), AT(b,i-1,2)); } for (j=1;j<=n;j++) { switch((int)rint(AT(b,m+j-1,0))) { case 0: { t = GLP_FR; break; } case 1: { t = GLP_LO; break; } case 2: { t = GLP_UP; break; } case 3: { t = GLP_DB; break; } default: { t = GLP_FX; break; } } glp_set_col_bnds(lp, j, t , AT(b,m+j-1,1), AT(b,m+j-1,2)); } glp_term_out(0); glp_simplex(lp, NULL); sp[0] = glp_get_status(lp); sp[1] = glp_get_obj_val(lp); for (k=1; k<=n; k++) { sp[k+1] = glp_get_col_prim(lp, k); } glp_delete_prob(lp); free(ia); free(ja); free(ar); return 0; }
int main(int argc, char *argv[]) { /* structures de données propres à GLPK */ glp_prob *prob; // déclaration d'un pointeur sur le problème int ia[1 + NBCREUX]; int ja[1 + NBCREUX]; double ar[1 + NBCREUX]; // déclaration des 3 tableaux servant à définir la partie creuse de la matrice des contraintes int p[N+1]; p[1] = 34; p[2] = 6; p[3] = 8; p[4] = 17; p[5] = 16; p[6] = 5; p[7] = 13; p[8] = 21; p[9] = 25; p[10] = 31; p[11] = 14; p[12] = 13; p[13] = 33; p[14] = 9; p[15] = 25; p[16] = 25; /* variables récupérant les résultats de la résolution du problème (fonction objectif et valeur des variables) */ int i,j,pos; double z; double x[NBVAR]; /* Les déclarations suivantes sont optionnelles, leur but est de donner des noms aux variables et aux contraintes. Cela permet de lire plus facilement le modèle saisi si on en demande un affichage à GLPK, ce qui est souvent utile pour détecter une erreur! */ char nomcontr[NBCONTR][8]; /* ici, les contraintes seront nommées "caisse1", "caisse2",... */ char numero[NBCONTR][3]; /* pour un nombre à deux chiffres */ char nomvar[NBVAR][3]; /* "xA", "xB", ... */ /* Création d'un problème (initialement vide) */ prob = glp_create_prob(); /* allocation mémoire pour le problème */ glp_set_prob_name(prob, "wagons"); /* affectation d'un nom (on pourrait mettre NULL) */ glp_set_obj_dir(prob, GLP_MIN); /* Il s'agit d'un problème de minimisation, on utiliserait la constante GLP_MAX dans le cas contraire */ /* Déclaration du nombre de contraintes (nombre de lignes de la matrice des contraintes) : NBCONTR */ glp_add_rows(prob, NBCONTR); /* On commence par préciser les bornes sur les constrainte, les indices des contraintes commencent à 1 (!) dans GLPK */ for(i = 1;i <= N;i++) { /* partie optionnelle : donner un nom aux contraintes */ strcpy(nomcontr[i-1], "caisse"); sprintf(numero[i-1], "%d", i); strcat(nomcontr[i-1], numero[i-1]); /* Les contraintes sont nommés "salle1", "salle2"... */ glp_set_row_name(prob, i, nomcontr[i-1]); /* Affectation du nom à la contrainte i */ /* partie indispensable : les bornes sur les contraintes */ glp_set_row_bnds(prob, i, GLP_FX, 1.0, 1.0); } for(i = N+1;i <= NBCONTR;i++) { /* partie optionnelle : donner un nom aux contraintes */ strcpy(nomcontr[i-1], "chaMax"); sprintf(numero[i-1], "%d", i); strcat(nomcontr[i-1], numero[i-1]); /* Les contraintes sont nommés "chargemax", "chargemax2"... */ glp_set_row_name(prob, i, nomcontr[i-1]); /* Affectation du nom à la contrainte i */ // il doit manquer un bout ici glp_set_row_bnds(prob, i, GLP_UP, 0.0, 0.0); //<=0 // on met cmax a gauche car c'est une variable // il aura le coeff -1 dans la mat creuse } /* Déclaration du nombre de variables : NBVAR */ glp_add_cols(prob, NBVAR); /* On précise le type des variables, les indices commencent à 1 également pour les variables! */ for(i = 1;i <= NBVAR;i++) { if(i==NBVAR){ sprintf(nomvar[i-1],"Cm"); glp_set_col_name(prob, i , nomvar[i-1]); glp_set_col_bnds(prob, i, GLP_LO, 0.0, 0.0); }else{ /* partie optionnelle : donner un nom aux variables */ sprintf(nomvar[i-1],"x%d",i-1); glp_set_col_name(prob, i , nomvar[i-1]); /* Les variables sont nommées "xA", "xB"... afin de respecter les noms de variables de l'exercice 2.2 */ /* partie obligatoire : bornes éventuelles sur les variables, et type */ glp_set_col_bnds(prob, i, GLP_DB, 0.0, 1.0); /* bornes sur les variables, comme sur les contraintes */ glp_set_col_kind(prob, i, GLP_BV); /* les variables sont par défaut continues, nous précisons ici qu'elles sont binaires avec la constante GLP_BV, on utiliserait GLP_IV pour des variables entières */ } } /* définition des coefficients des variables dans la fonction objectif */ for(i = 1;i <= N*M;i++) glp_set_obj_coef(prob,i,0.0); // Tous les coûts sont ici à 0! Mais on doit specifier quand meme glp_set_obj_coef(prob,N*M+1,1.0); // 1 fois cmax /* Définition des coefficients non-nuls dans la matrice des contraintes, autrement dit les coefficients de la matrice creuse */ /* Les indices commencent également à 1 ! */ // pour i de 1 a n //pour i de 1 a m /* xij intervient dans la ligne i avec un coeff 1 et dans la ligne n+j avec un coeff pi ia -> i et n+j ar -> 1 et pi ja -> xij -> (i-1)*m+j */ pos = 1; for(i=1; i<=N; i++){ for(j=1; j<=M; j++){ ia[pos] = i; ja[pos] = (i-1)*M+j; ar[pos] = 1; pos++; ia[pos] = N+j; ja[pos] = (i-1)*M+j; ar[pos] = p[i]; pos++; } } //Cmax a -1 !!! for(i=N+1; i<=N+M;i++){ ia[pos] = i; ja[pos] = N*M+1; ar[pos] = -1; pos++; } /* chargement de la matrice dans le problème */ glp_load_matrix(prob,NBCREUX,ia,ja,ar); /* Optionnel : écriture de la modélisation dans un fichier (TRES utile pour debugger!) */ glp_write_lp(prob,NULL,"wagons.lp"); /* Résolution, puis lecture des résultats */ glp_simplex(prob,NULL); glp_intopt(prob,NULL); /* Résolution */ z = glp_mip_obj_val(prob); /* Récupération de la valeur optimale. Dans le cas d'un problème en variables continues, l'appel est différent : z = glp_get_obj_val(prob); */ for(i = 0;i < NBVAR; i++) x[i] = glp_mip_col_val(prob,i+1); /* Récupération de la valeur des variables, Appel différent dans le cas d'un problème en variables continues : for(i = 0;i < p.nbvar;i++) x[i] = glp_get_col_prim(prob,i+1); */ printf("z = %lf\n",z); for(i = 0;i < NBVAR;i++) printf("x%d = %d, ",i,(int)(x[i] + 0.5)); /* un cast est ajouté, x[i] pourrait être égal à 0.99999... */ puts(""); /* libération mémoire */ glp_delete_prob(prob); /* J'adore qu'un plan se déroule sans accroc! */ return 0; }
void npp_build_prob(NPP *npp, glp_prob *prob) { /* build resultant (preprocessed) problem */ NPPROW *row; NPPCOL *col; NPPAIJ *aij; int i, j, type, len, *ind; double dir, *val; glp_erase_prob(prob); glp_set_prob_name(prob, npp->name); glp_set_obj_name(prob, npp->obj); glp_set_obj_dir(prob, npp->orig_dir); if (npp->orig_dir == GLP_MIN) dir = +1.0; else if (npp->orig_dir == GLP_MAX) dir = -1.0; else xassert(npp != npp); glp_set_obj_coef(prob, 0, dir * npp->c0); /* build rows */ for (row = npp->r_head; row != NULL; row = row->next) { row->temp = i = glp_add_rows(prob, 1); glp_set_row_name(prob, i, row->name); if (row->lb == -DBL_MAX && row->ub == +DBL_MAX) type = GLP_FR; else if (row->ub == +DBL_MAX) type = GLP_LO; else if (row->lb == -DBL_MAX) type = GLP_UP; else if (row->lb != row->ub) type = GLP_DB; else type = GLP_FX; glp_set_row_bnds(prob, i, type, row->lb, row->ub); } /* build columns and the constraint matrix */ ind = xcalloc(1+prob->m, sizeof(int)); val = xcalloc(1+prob->m, sizeof(double)); for (col = npp->c_head; col != NULL; col = col->next) { j = glp_add_cols(prob, 1); glp_set_col_name(prob, j, col->name); #if 0 glp_set_col_kind(prob, j, col->kind); #else glp_set_col_kind(prob, j, col->is_int ? GLP_IV : GLP_CV); #endif if (col->lb == -DBL_MAX && col->ub == +DBL_MAX) type = GLP_FR; else if (col->ub == +DBL_MAX) type = GLP_LO; else if (col->lb == -DBL_MAX) type = GLP_UP; else if (col->lb != col->ub) type = GLP_DB; else type = GLP_FX; glp_set_col_bnds(prob, j, type, col->lb, col->ub); glp_set_obj_coef(prob, j, dir * col->coef); len = 0; for (aij = col->ptr; aij != NULL; aij = aij->c_next) { len++; ind[len] = aij->row->temp; val[len] = aij->val; } glp_set_mat_col(prob, j, len, ind, val); } xfree(ind); xfree(val); /* resultant problem has been built */ npp->m = prob->m; npp->n = prob->n; npp->nnz = prob->nnz; npp->row_ref = xcalloc(1+npp->m, sizeof(int)); npp->col_ref = xcalloc(1+npp->n, sizeof(int)); for (row = npp->r_head, i = 0; row != NULL; row = row->next) npp->row_ref[++i] = row->i; for (col = npp->c_head, j = 0; col != NULL; col = col->next) npp->col_ref[++j] = col->j; /* transformed problem segment is no longer needed */ dmp_delete_pool(npp->pool), npp->pool = NULL; npp->name = npp->obj = NULL; npp->c0 = 0.0; npp->r_head = npp->r_tail = NULL; npp->c_head = npp->c_tail = NULL; return; }