Luca,

> First, thanks to all. Jeff you're right. I misspelled my problem. The problem
> is:
> 
> minimize { sum[from i=1 to 96] of {c1*x1(i)+c2*x2+c3*x3(i)} }
> with this constraints:
>  x1(i)+x2+x3(i)=c4(i)
>  x1(i)+x3(i)=c5
>  x1(i)>=5
> 0<=x3(i)<=100
> 0<=x2<=1 binary
> 
> where x1, x3 and c4 are vectors of 96 elements. c1,c2,c3,c5 are
> numbers.
> 
> Sorry for the mistake. I wish someone would write the problem in a form
> similar to that described above. I need to learn how to write a minimization
> problem with glpk with the language c.

See the attach.

Raniere
/* provaglpk.c */

/* Written in C by Raniere Silva <r.gaia...@gmail.com> */

#include <stdio.h>
#include <stdlib.h>
#include <glpk.h>

int main(void){
    int n = 96, i, ir, j, jc;

    double c1=1.0;
    double c2=1.0;
    double c3=1.0;
    double c5=1.0;

    double c4[97];
    for(j = 1; j <= n; j++){
        c4[j] = 1.0;
    }

    glp_prob *lp;
    lp = glp_create_prob();
    glp_set_prob_name(lp, "provaglpk");
    glp_set_obj_dir(lp, GLP_MIN);
    
    char col_name[10];
    for(j = 1; j <= n; j++){
        jc = glp_add_cols(lp, 1);
        sprintf(col_name, "x1-%d", j);
        glp_set_col_name(lp, jc, col_name);
        glp_set_col_bnds(lp, jc, GLP_UP, 0.0, 5.0);  // Column Bound
        glp_set_obj_coef(lp, jc, c1);  // Objective Coeficient
    }
    jc = glp_add_cols(lp, 1);
    glp_set_col_name(lp, jc, "x2");
    glp_set_col_kind(lp, jc, GLP_BV);  // Binary variable
    glp_set_obj_coef(lp, jc, c2);  // Objective Coeficient
    for(j = 1; j <= n; j++){
        jc = glp_add_cols(lp, 1);
        sprintf(col_name, "x3-%d", j);
        glp_set_col_name(lp, jc, col_name);
        glp_set_col_bnds(lp, jc, GLP_DB, 0.0, 100.0);  // Column Bound
        glp_set_obj_coef(lp, jc, c3);  // Objective Coeficient
    }

    char row_name[10];
    int *ind;
    ind = (int *) malloc((n + 1) * sizeof(int));
    double *val;
    val = (double *) malloc((n + 1) * sizeof(double));
    for(i = 1; i <= n; i++){
        ir = glp_add_rows(lp, 1);
        sprintf(row_name, "c4-%d", i);
        glp_set_row_name(lp, ir, row_name);
        sprintf(col_name, "x1-%d", i);
        ind[1] = glp_find_col(lp, col_name);
        val[1] = 1.0;
        ind[2] = glp_find_col(lp, "x2");
        val[2] = 1.0;
        sprintf(col_name, "x3-%d", i);
        ind[3] = glp_find_col(lp, col_name);
        val[3] = 1.0;
        glp_set_mat_row(lp, ir, 3, ind, val);
        glp_set_row_bnds(lp, ir, GLP_FX, c4[i], c4[i]);
    }
    for(i = 1; i <= n; i++){
        ir = glp_add_rows(lp, 1);
        sprintf(row_name, "c5-%d", i);
        glp_set_row_name(lp, ir, row_name);
        sprintf(col_name, "x1-%d", i);
        ind[1] = glp_find_col(lp, col_name);
        val[1] = 1.0;
        sprintf(col_name, "x3-%d", i);
        ind[2] = glp_find_col(lp, col_name);
        val[2] = 1.0;
        glp_set_mat_row(lp, ir, 2, ind, val);
        glp_set_row_bnds(lp, ir, GLP_FX, c5, c5);
    }

    glp_simplex(lp, NULL);
    double z = glp_get_obj_val(lp);
    double x1[97], x3[97];
    for(i = 1; i <= n; i++){
        sprintf(col_name, "x1-%d", i);
        x1[i] = glp_get_col_prim(lp, glp_find_col(lp, col_name));
        sprintf(col_name, "x3-%d", i);
        x3[i] = glp_get_col_prim(lp, glp_find_col(lp, col_name));
    }
    double x2 = glp_get_col_prim(lp, glp_find_col(lp, "x2"));
    printf("\nz = %g\n", z);
    glp_delete_prob(lp);
    return 0;
}

/* eof */
_______________________________________________
Help-glpk mailing list
Help-glpk@gnu.org
https://lists.gnu.org/mailman/listinfo/help-glpk

Reply via email to