diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..708882d --- /dev/null +++ b/Makefile @@ -0,0 +1,29 @@ +CC := gcc -Wall -Wextra -Wpedantic -Werror -Iinclude -g + +# LIBS := +# LDFLAGS := $(LDFLAGS) $(shell pkg-config --libs $(LIBS)) +# CFLAGS := $(CFLAGS) $(shell pkg-config --cflags $(LIBS)) + +LDFLAGS := -lglpk + +BUILD_DIR ?= build + +OUT := $(notdir $(shell pwd)) + +SRC := $(wildcard src/*.c) +OBJS := $(patsubst src/%.c,$(BUILD_DIR)/%.o,$(SRC)) +DEPS := $(wildcard $(BUILD_DIR)/*.d) + + +$(BUILD_DIR)/$(OUT): $(OBJS) + $(CC) $(LDFLAGS) -o $@ $^ + + +-include $(DEPS) +$(BUILD_DIR)/%.o: src/%.c + $(CC) $(CFLAGS) -MP -MD $< -c -o $@ + + +clean: + -rm -f $(BUILD_DIR)/*.o + -rm -f $(BUILD_DIR)/*.d diff --git a/makefile b/makefile deleted file mode 100644 index c322fb5..0000000 --- a/makefile +++ /dev/null @@ -1,8 +0,0 @@ -CC=gcc -OPT=-Wall -std=c11 -g -LIB=-lglpk - -all: test - -test: test.c test.h - $(CC) $< $(OPT) $(LIB) -o $@ diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..bcb45ff --- /dev/null +++ b/src/main.c @@ -0,0 +1,7 @@ +#include "master.h" + + +int main(void) { + perfect_rolls(); + return 0; +} diff --git a/test.c b/src/master.c old mode 100755 new mode 100644 similarity index 53% rename from test.c rename to src/master.c index eec76a3..7c49bd4 --- a/test.c +++ b/src/master.c @@ -1,17 +1,18 @@ -/* short.c */ +#include "master.h" #include /* C input/output */ #include /* C standard library */ #include /* GNU GLPK linear/mixed integer solver */ #include -#include "test.h" + +#include "sub.h" int col_num = 1; int num_col_sp = 1; -void add_column(glp_prob *lp, float coef) { +static void add_column(glp_prob *lp, float coef) { glp_add_cols(lp, 1); char name[10] = ""; snprintf(name, sizeof name, "rouleau%d", col_num); @@ -22,63 +23,6 @@ void add_column(glp_prob *lp, float coef) { col_num++; } -void add_column_sp(glp_prob *lp) { - glp_add_cols(lp, 1); - char name[10] = ""; - snprintf(name, sizeof name, "rouleau%d", num_col_sp); - 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_obj_coef(lp, num_col_sp, 1.0); - num_col_sp++; -} - -void column_generator(int y1, int y2, int y3, int y4, int a[static 4]) { - glp_prob *sous_prob; - int ia[1 + 1000]; - int ja[1 + 1000]; - double ar[1 + 1000]; - /* double z, x1, x2, x3, x4; */ - - /* create problem */ - sous_prob = glp_create_prob(); - glp_set_prob_name(sous_prob, "rouleaux-parfaits"); - glp_set_obj_dir(sous_prob, GLP_MAX); - - /* fill problem */ - glp_add_rows(sous_prob, 4); - glp_set_row_name(sous_prob, 1, "taille0"); - glp_set_row_bnds(sous_prob, 1, GLP_UP, 0.0, 100.0); - - num_col_sp = 1; - - const double coeffs[] = {45.0, 36.0, 31.0, 14.0}; - for (int i = 1; i < 5; i++) { - add_column_sp(sous_prob); - glp_set_col_kind(sous_prob, i, GLP_IV); - ia[i] = 1; - ja[i] = i; - ar[i] = coeffs[i]; - } - glp_load_matrix(sous_prob, 4, ia, ja, ar); - - /* solve problem */ - glp_simplex(sous_prob, NULL); - glp_intopt(sous_prob, NULL); - - a[0] = glp_get_obj_val(sous_prob); - a[1] = glp_mip_col_val(sous_prob, 1); - a[2] = glp_mip_col_val(sous_prob, 2); - a[3] = glp_mip_col_val(sous_prob, 3); - 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 */ - glp_delete_prob(sous_prob); - glp_free_env(); - //glp_intopt(glp_prob *mip, const glp_iocp *parm); - -} - int perfect_rolls() { /* declare variables */ glp_prob *lp; @@ -107,11 +51,11 @@ int perfect_rolls() { double y1, y2, y3, y4; int a[4] = {0}; 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"); + /* 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("col_num=%d\n", col_num); @@ -125,11 +69,11 @@ int perfect_rolls() { y3 = glp_get_row_dual(tmp_prob, 3); y4 = glp_get_row_dual(tmp_prob, 4); - column_generator(y1, y2, y3, y4, a); + column_generator(y1, y2, y3, y4, a, num_col_sp); if (a[0] > 1) { - for (int i = 1; i <= 4; i++) { - printf("a[%d]=%d\n", i, a[i]); - } + /* for (int i = 1; i <= 4; i++) { */ + /* printf("a[%d]=%d\n", i, a[i]); */ + /* } */ add_column(lp, 1.0); for (int i = 1; i < 5; i++) { /* a[col_num, i] = a[i] */ @@ -158,8 +102,3 @@ int perfect_rolls() { return z; } - -int main(void) { - perfect_rolls(); - return 0; -} diff --git a/src/master.h b/src/master.h new file mode 100644 index 0000000..34310dd --- /dev/null +++ b/src/master.h @@ -0,0 +1,8 @@ +#ifndef MASTER_H +#define MASTER_H + + +int perfect_rolls(); + + +#endif diff --git a/src/sub.c b/src/sub.c new file mode 100644 index 0000000..fef9d22 --- /dev/null +++ b/src/sub.c @@ -0,0 +1,62 @@ +#include "sub.h" + +#include +#include + + +static void add_column_sp(glp_prob *lp, int num_col_sp) { + glp_add_cols(lp, 1); + char name[10] = ""; + snprintf(name, sizeof name, "rouleau%d", num_col_sp); + 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_obj_coef(lp, num_col_sp, 1.0); + num_col_sp++; +} + + +void column_generator(int y1, int y2, int y3, int y4, int a[static 4], int num_col_sp) { + glp_prob *sous_prob; + int ia[1 + 1000]; + int ja[1 + 1000]; + double ar[1 + 1000]; + /* double z, x1, x2, x3, x4; */ + + /* create problem */ + sous_prob = glp_create_prob(); + glp_set_prob_name(sous_prob, "rouleaux-parfaits"); + glp_set_obj_dir(sous_prob, GLP_MAX); + + /* fill problem */ + glp_add_rows(sous_prob, 4); + glp_set_row_name(sous_prob, 1, "taille0"); + glp_set_row_bnds(sous_prob, 1, GLP_UP, 0.0, 100.0); + + num_col_sp = 1; + + const double coeffs[] = {45.0, 36.0, 31.0, 14.0}; + for (int i = 1; i < 5; i++) { + add_column_sp(sous_prob, num_col_sp); + glp_set_col_kind(sous_prob, i, GLP_IV); + ia[i] = 1; + ja[i] = i; + ar[i] = coeffs[i]; + } + glp_load_matrix(sous_prob, 4, ia, ja, ar); + + /* solve problem */ + glp_simplex(sous_prob, NULL); + glp_intopt(sous_prob, NULL); + + a[0] = glp_get_obj_val(sous_prob); + a[1] = glp_mip_col_val(sous_prob, 1); + a[2] = glp_mip_col_val(sous_prob, 2); + a[3] = glp_mip_col_val(sous_prob, 3); + 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 */ + glp_delete_prob(sous_prob); + //glp_intopt(glp_prob *mip, const glp_iocp *parm); + +} diff --git a/src/sub.h b/src/sub.h new file mode 100644 index 0000000..5b315c3 --- /dev/null +++ b/src/sub.h @@ -0,0 +1,8 @@ +#ifndef SUB_H +#define SUB_H + + +void column_generator(int y1, int y2, int y3, int y4, int a[static 4], int num_col_sp); + + +#endif