Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various improvements and fixes #2

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions res/instance1.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
100
45 97
36 610
31 395
14 211
5600
1380 22
1520 25
1560 12
1710 14
1820 18
1880 18
1930 20
2000 10
2050 12
2100 14
2140 16
2150 18
2200 20
10 changes: 7 additions & 3 deletions src/column.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@
#include <stdlib.h>
#include <string.h>

double *column_create(int column_count, double value, int index) {
double *column_create(int column_count, int count, int index) {
double *column;

column = (double *)malloc(column_count * sizeof(double));
memset(column, 0, column_count * sizeof(double));
column[index] = value;
column[index] = count;

return column;
}
Expand Down Expand Up @@ -80,7 +80,11 @@ double **columns_matrix_compute(order **orders, int order_count, int max_width)
return NULL;
}

columns_matrix[i] = column_create(order_count, max_width / orders[i]->width, i);
/* For each order width, create a column with only that width */
int count = max_width / orders[i]->width;
if (count > orders[i]->demand)
count = orders[i]->demand;
columns_matrix[i] = column_create(order_count, count, i);
}

return columns_matrix;
Expand Down
2 changes: 1 addition & 1 deletion src/column.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

#include <stdio.h>

double *column_create(int column_count, double value, int index);
double *column_create(int column_count, int count, int index);

void column_print(double *columns, int column_count, FILE *fd);

Expand Down
37 changes: 15 additions & 22 deletions src/cutting_stock.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ void cutting_stock_branch_and_cut(order **orders, int order_count, double *dual_
glp_add_cols(lp, col_number);

for (i = 0, col = 1; col <= col_number; col++, i++) {
glp_set_col_bnds(lp, col, GLP_LO, 0.0, 0.0);
glp_set_col_bnds(lp, col, GLP_DB, 0.0, orders[i]->demand);
glp_set_col_kind(lp, col, GLP_IV );
glp_set_obj_coef(lp, col, dual_column[i]);
}
Expand Down Expand Up @@ -135,21 +135,13 @@ double **cutting_stock_compute_best_patterns(order **orders, int order_count, in
columns_matrix = columns_matrix_compute(orders, order_count, max_width);
column_size = order_count;
columns_matrix_number = order_count;
best_patterns = NULL;
temp_best_patterns_number = 0;

while (true) {
dual_column = cutting_stock_compute_dual(orders, order_count, columns_matrix, columns_matrix_number);

new_pattern = (double *)malloc(column_size * sizeof(double));
cutting_stock_branch_and_cut(orders, order_count, dual_column, column_size, max_width, &obj_value, new_pattern);

if (obj_value <= 1.00000999999) {
free((void *)dual_column);
free((void *)new_pattern);
break;
}

/* Add the new pattern */
if (columns_matrix) {
columns_matrix = (double **)realloc(columns_matrix, (columns_matrix_number + 1) * sizeof(double *));
Expand All @@ -160,25 +152,24 @@ double **cutting_stock_compute_best_patterns(order **orders, int order_count, in
columns_matrix_number++;

free((void *)dual_column);

if (obj_value <= 1.00000000001)
break;
}

if (columns_matrix_number == 0) {
columns_matrix_destroy(columns_matrix, columns_matrix_number);
return NULL;
}

best_patterns = (double **)malloc(columns_matrix_number * sizeof(double *));
temp_best_patterns_number = 0;
for (i = 0; i < columns_matrix_number; i++) {
if (best_patterns) {
if (!double_matrix_contains_array(best_patterns, temp_best_patterns_number, columns_matrix[i], column_size)) {
best_patterns = (double **)realloc(best_patterns, (temp_best_patterns_number + 1) * sizeof(double *));
best_patterns[temp_best_patterns_number] = columns_matrix[i];
temp_best_patterns_number++;
}
} else {
best_patterns = (double **)malloc(sizeof(double *));
best_patterns[0] = columns_matrix[i];
temp_best_patterns_number++;
}
if (double_matrix_contains_array(best_patterns, temp_best_patterns_number, columns_matrix[i], column_size))
continue;

best_patterns[temp_best_patterns_number] = columns_matrix[i];
temp_best_patterns_number++;
}

*best_patterns_number = temp_best_patterns_number;
Expand Down Expand Up @@ -208,6 +199,7 @@ double *cutting_stock_compute(double **best_patterns, int best_patterns_number,

for (col = 1; col <= best_patterns_number; col++) {
glp_set_col_bnds(lp, col, GLP_LO, 0.0, 0.0);
glp_set_col_kind(lp, col, GLP_IV);
glp_set_obj_coef(lp, col, 1.0);
}

Expand All @@ -221,12 +213,13 @@ double *cutting_stock_compute(double **best_patterns, int best_patterns_number,
glp_load_matrix(lp, mat->count, mat->ia, mat->ja, mat->ar);

assert(glp_simplex(lp, NULL) == 0);
assert(glp_intopt(lp, NULL) == 0);

*obj_value = glp_get_obj_val(lp);
*obj_value = glp_mip_obj_val(lp);

pattern_demand_repartition = (double *)malloc(best_patterns_number * sizeof(double));
for (i = 0, col = 1; col <= best_patterns_number; col++, i++) {
pattern_demand_repartition[i] = glp_get_col_prim(lp, col);
pattern_demand_repartition[i] = glp_mip_col_val(lp, col);
}

glp_delete_prob(lp);
Expand Down
29 changes: 19 additions & 10 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
#include <assert.h>
#include <time.h>

void compute_problem_from_file(char *file_name) {
int compute_problem_from_file(char *file_name) {
int ret = 0;
order **orders;
int order_count, max_width, best_patterns_number;
double **best_patterns, *pattern_demand_repartition, obj_value;
Expand All @@ -51,24 +52,26 @@ void compute_problem_from_file(char *file_name) {

if (!(is_file_exists(file_name))) {
printf("[ERROR] The specified file '%s' doesn't exists.\n", file_name);
goto clean_up;
return -1;
}

if (!(orders = orders_read_from_file(file_name, &order_count, &max_width))) {
printf("[ERROR] Failed to load orders from the file '%s'.\n", file_name);
goto clean_up;
return -1;
}

begin_clock = clock();

if (!(best_patterns = cutting_stock_compute_best_patterns(orders, order_count, max_width, &best_patterns_number))) {
printf("[ERROR] The computation of the best patterns failed.\n");
goto clean_up;
ret = -1;
goto err_compute_best_patterns;
}

if (!(pattern_demand_repartition = cutting_stock_compute(best_patterns, best_patterns_number, orders, order_count, &obj_value))) {
printf("[ERROR] The resolution of the problem failed.\n");
goto clean_up;
ret = -1;
goto err_compute;
}

end_clock = clock();
Expand All @@ -77,23 +80,29 @@ void compute_problem_from_file(char *file_name) {

cutting_stock_print_solution(best_patterns, best_patterns_number, order_count, pattern_demand_repartition, obj_value, orders, order_count, stdout);

clean_up:
free((void *)pattern_demand_repartition);
free(pattern_demand_repartition);

err_compute:
columns_matrix_destroy(best_patterns, best_patterns_number);
err_compute_best_patterns:
orders_destroy(orders, order_count);
return ret;
}

int main(int argc, char **argv) {
int exit_code = 0;

glp_term_out(GLP_OFF);

if (argc != 2) {
printf("%s <file_name>\n", argv[0]);
return 0;
return 1;
}

compute_problem_from_file(argv[1]);
if (compute_problem_from_file(argv[1]) < 0)
exit_code = 1;

glp_free_env();

return 0;
return exit_code;
}