suppression de l’état global :anarchy:

This commit is contained in:
papush! 2019-10-22 20:15:34 +02:00
parent 9cb04f58bd
commit 1d363077da
3 changed files with 32 additions and 31 deletions

View File

@ -1,29 +1,28 @@
#include "master.h" #include "master.h"
#include <stdio.h> /* C input/output */ #include <stdio.h>
#include <stdlib.h> /* C standard library */ #include <stdlib.h>
#include <glpk.h> /* GNU GLPK linear/mixed integer solver */ #include <glpk.h>
#include <string.h> #include <string.h>
#include "sub.h" #include "sub.h"
int col_num = 1; static void add_column(glp_prob *lp, float coef, int *num_col) {
int num_col_sp = 1;
static void add_column(glp_prob *lp, float coef) {
glp_add_cols(lp, 1); glp_add_cols(lp, 1);
char name[10] = ""; char name[10] = "";
snprintf(name, sizeof name, "rouleau%d", col_num); snprintf(name, sizeof name, "rouleau%d", *num_col);
glp_set_col_name(lp, col_num, name); glp_set_col_name(lp, *num_col, name);
glp_set_col_bnds(lp, col_num, GLP_LO, 0.0, 0.0); glp_set_col_bnds(lp, *num_col, GLP_LO, 0.0, 0.0);
glp_set_obj_coef(lp, col_num, coef); glp_set_obj_coef(lp, *num_col, coef);
col_num++; (*num_col)++;
} }
int perfect_rolls() { int perfect_rolls() {
int num_col = 1;
/* declare variables */ /* declare variables */
glp_prob *lp; glp_prob *lp;
int ia[1 + 1000] = {0}, ja[1 + 1000] = {0}; int ia[1 + 1000] = {0}, ja[1 + 1000] = {0};
@ -41,7 +40,7 @@ int perfect_rolls() {
snprintf(name, sizeof name, "taille%d", i); snprintf(name, sizeof name, "taille%d", i);
glp_set_row_name(lp, 1, name); glp_set_row_name(lp, 1, name);
glp_set_row_bnds(lp, 1, GLP_LO, bounds[i-1], 0.0); glp_set_row_bnds(lp, 1, GLP_LO, bounds[i-1], 0.0);
add_column(lp, 1.0); add_column(lp, 1.0, &num_col);
ia[i] = i; ia[i] = i;
ja[i] = i; ja[i] = i;
ar[i] = 1; ar[i] = 1;
@ -61,7 +60,7 @@ int perfect_rolls() {
/* printf("\n"); */ /* printf("\n"); */
glp_load_matrix(lp, nb_coeffs, ia, ja, ar); glp_load_matrix(lp, nb_coeffs, ia, ja, ar);
printf("col_num=%d\n", col_num); printf("Colonne %d\n", num_col);
/* solve problem */ /* solve problem */
glp_simplex(lp, &params); glp_simplex(lp, &params);
@ -70,27 +69,30 @@ int perfect_rolls() {
y3 = glp_get_row_dual(lp, 3); y3 = glp_get_row_dual(lp, 3);
y4 = glp_get_row_dual(lp, 4); y4 = glp_get_row_dual(lp, 4);
column_generator(y1, y2, y3, y4, a, num_col_sp); puts("Résolution du sous-problème.");
column_generator(y1, y2, y3, y4, a);
if (a[0] > 1) { if (a[0] > 1) {
fputs("Après résolution du sous-problème, on ajoute : ", stdout); printf(" Valeur de lobjectif : %d\n", a[0]);
fputs(" On ajoute : ", stdout);
int i = 1; int i = 1;
for (i = 1; i < 4; i++) { for (i = 1; i < 4; i++) {
printf("%d, ", a[i]); printf("%d, ", a[i]);
} }
printf("%d\n", a[i]); printf("%d\n", a[i]);
add_column(lp, 1.0); add_column(lp, 1.0, &num_col);
for (int i = 1; i < 5; i++) { for (int i = 1; i < 5; i++) {
/* a[col_num, i] = a[i] */ /* a[num_col, i] = a[i] */
nb_coeffs++; nb_coeffs++;
ia[nb_coeffs] = i; ia[nb_coeffs] = i;
ja[nb_coeffs] = col_num-1; ja[nb_coeffs] = num_col-1;
ar[nb_coeffs] = a[i]; ar[nb_coeffs] = a[i];
} }
} }
else { else {
break; break;
} }
puts("--------------------------------------------------------------------------------");
} }
/* recover and display results */ /* recover and display results */

View File

@ -4,18 +4,18 @@
#include <glpk.h> #include <glpk.h>
static void add_column_sp(glp_prob *lp, int num_col_sp) { static void add_column_sp(glp_prob *lp, int *num_col_sp) {
glp_add_cols(lp, 1); glp_add_cols(lp, 1);
char name[10] = ""; char name[10] = "";
snprintf(name, sizeof name, "rouleau%d", num_col_sp); snprintf(name, sizeof name, "rouleau%d", *num_col_sp);
glp_set_col_name(lp, num_col_sp, name); glp_set_col_name(lp, *num_col_sp, name);
glp_set_col_bnds(lp, num_col_sp, GLP_LO, 0.0, 0.0); glp_set_col_bnds(lp, *num_col_sp, GLP_LO, 0.0, 0.0);
glp_set_obj_coef(lp, num_col_sp, 1.0); glp_set_obj_coef(lp, *num_col_sp, 1.0);
num_col_sp++; (*num_col_sp)++;
} }
void column_generator(int y1, int y2, int y3, int y4, int a[static 4], int num_col_sp) { void column_generator(int y1, int y2, int y3, int y4, int a[static 4]) {
glp_prob *sous_prob; glp_prob *sous_prob;
int ia[1 + 1000]; int ia[1 + 1000];
int ja[1 + 1000]; int ja[1 + 1000];
@ -32,11 +32,11 @@ void column_generator(int y1, int y2, int y3, int y4, int a[static 4], int num_c
glp_set_row_name(sous_prob, 1, "taille0"); glp_set_row_name(sous_prob, 1, "taille0");
glp_set_row_bnds(sous_prob, 1, GLP_UP, 0.0, 100.0); glp_set_row_bnds(sous_prob, 1, GLP_UP, 0.0, 100.0);
num_col_sp = 1; int num_col_sp = 1;
const double coeffs[] = {45.0, 36.0, 31.0, 14.0}; const double coeffs[] = {45.0, 36.0, 31.0, 14.0};
for (int i = 1; i < 5; i++) { for (int i = 1; i < 5; i++) {
add_column_sp(sous_prob, num_col_sp); add_column_sp(sous_prob, &num_col_sp);
glp_set_col_kind(sous_prob, i, GLP_IV); glp_set_col_kind(sous_prob, i, GLP_IV);
ia[i] = 1; ia[i] = 1;
ja[i] = i; ja[i] = i;
@ -61,7 +61,6 @@ void column_generator(int y1, int y2, int y3, int y4, int a[static 4], int num_c
a[3] = glp_mip_col_val(sous_prob, 3); a[3] = glp_mip_col_val(sous_prob, 3);
a[4] = glp_mip_col_val(sous_prob, 4); a[4] = glp_mip_col_val(sous_prob, 4);
printf("a1 = %d; a2 = %d; a3 = %d; a4 = %d\n", a[1], a[2], a[3], a[4]);
/* housekeeping */ /* housekeeping */
glp_delete_prob(sous_prob); glp_delete_prob(sous_prob);
//glp_intopt(glp_prob *mip, const glp_iocp *parm); //glp_intopt(glp_prob *mip, const glp_iocp *parm);

View File

@ -2,7 +2,7 @@
#define SUB_H #define SUB_H
void column_generator(int y1, int y2, int y3, int y4, int a[static 4], int num_col_sp); void column_generator(int y1, int y2, int y3, int y4, int a[static 4]);
#endif #endif