#include "master.h" #include #include #include #include #include "sub.h" static void add_column(glp_prob *lp, float coef, int *num_col) { glp_add_cols(lp, 1); char name[10] = ""; snprintf(name, sizeof name, "rouleau%d", *num_col); glp_set_col_name(lp, *num_col, name); glp_set_col_bnds(lp, *num_col, GLP_LO, 0.0, 0.0); glp_set_obj_coef(lp, *num_col, coef); (*num_col)++; } int perfect_rolls() { int num_col = 1; /* declare variables */ glp_prob *lp; int ia[1 + 1000] = {0}, ja[1 + 1000] = {0}; double ar[1 + 1000] = {0}, z, x1, x2, x3, x4; /* create problem */ lp = glp_create_prob(); glp_set_prob_name(lp, "rouleaux-parfaits"); glp_set_obj_dir(lp, GLP_MIN); /* fill problem */ glp_add_rows(lp, 4); char name[8] = ""; const double bounds[4] = {97, 610, 395, 211}; for (int i = 1; i < 5; i++) { snprintf(name, sizeof name, "taille%d", i); glp_set_row_name(lp, 1, name); glp_set_row_bnds(lp, 1, GLP_LO, bounds[i-1], 0.0); add_column(lp, 1.0, &num_col); ia[i] = i; ja[i] = i; ar[i] = 1; } int nb_coeffs = 4; double y1, y2, y3, y4; int a[5] = {0}; glp_smcp params = {0}; glp_init_smcp(¶ms); params.msg_lev = GLP_MSG_OFF; while (1) { /* printf("nb_coeffs=%d\n", nb_coeffs); */ /* for (int i = 0; i < nb_coeffs + 4; i++) { */ /* printf("ar[%d]=%g\n", i, ar[i]); */ /* } */ /* printf("\n"); */ glp_load_matrix(lp, nb_coeffs, ia, ja, ar); printf("Colonne %d\n", num_col); /* solve problem */ glp_simplex(lp, ¶ms); y1 = glp_get_row_dual(lp, 1); y2 = glp_get_row_dual(lp, 2); y3 = glp_get_row_dual(lp, 3); y4 = glp_get_row_dual(lp, 4); puts("Résolution du sous-problème."); column_generator(y1, y2, y3, y4, a); if (a[0] > 1) { printf(" Valeur de l’objectif : %d\n", a[0]); fputs(" On ajoute : ", stdout); int i = 1; for (i = 1; i < 4; i++) { printf("%d, ", a[i]); } printf("%d\n", a[i]); add_column(lp, 1.0, &num_col); for (int i = 1; i < 5; i++) { /* a[num_col, i] = a[i] */ nb_coeffs++; ia[nb_coeffs] = i; ja[nb_coeffs] = num_col-1; ar[nb_coeffs] = a[i]; } } else { break; } puts("--------------------------------------------------------------------------------"); } /* recover and display results */ z = glp_get_obj_val(lp); x1 = glp_get_col_prim(lp, 1); x2 = glp_get_col_prim(lp, 2); x3 = glp_get_col_prim(lp, 3); x4 = glp_get_col_prim(lp, 4); printf("z = %g; x1 = %g; x2 = %g; x3 = %g; x4 = %g\n", z, x1, x2, x3, x4); /* housekeeping */ glp_delete_prob(lp); glp_free_env(); return z; }