Skip to content

Commit

Permalink
Merge pull request #16 from fontanf/bugfix/onedimensional-not-anytime
Browse files Browse the repository at this point in the history
Bugfix/onedimensional not anytime
  • Loading branch information
fontanf authored Apr 19, 2024
2 parents b95d82f + ad93e85 commit a7f09f0
Show file tree
Hide file tree
Showing 26 changed files with 225 additions and 21 deletions.
6 changes: 6 additions & 0 deletions data/onedimensional/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
filegroup(
name = "onedimensional",
srcs = glob(["**/*.csv"]),
visibility = ["//visibility:public"],
)

4 changes: 4 additions & 0 deletions data/onedimensional/users/2023-08-01_bins.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ID,X,COST,COPIES
0,3000,3000,10000
1,7000,8008,10000
2,10000,10000,10000
11 changes: 11 additions & 0 deletions data/onedimensional/users/2023-08-01_items.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
ID,X,COPIES
0,9809,236
1,739,4
2,4624,241
3,3589,884
4,8369,48
5,3236,348
6,3470,302
7,732,299
8,365,613
9,2056,299
3 changes: 3 additions & 0 deletions data/onedimensional/users/2024-04-06_t0_bins.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ID,X,COST,COPIES
0,3000,3000,3000
1,7000,7000,5000
5 changes: 5 additions & 0 deletions data/onedimensional/users/2024-04-06_t0_items.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ID,X,COPIES,NESTING_LENGTH
0,290,1,-5
1,1496,2,-5
2,2751,2,-5
3,3351,2,-5
9 changes: 9 additions & 0 deletions data/onedimensional/users/2024-04-06_t1_bins.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
ID,X,COST,COPIES
0,1400,1400,30000
1,3900,3900,30000
2,900,900,30000
3,4500,4500,30000
4,2900,2900,30000
5,3400,3400,30000
6,1900,1900,30000
7,2400,2400,30000
7 changes: 7 additions & 0 deletions data/onedimensional/users/2024-04-06_t1_items.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
ID,X,COPIES,NESTING_LENGTH
0,1768,3,-4
1,1764,5,-4
2,964,2,-4
3,864,1,-4
4,1774,1,-4
5,764,1,-4
9 changes: 9 additions & 0 deletions data/onedimensional/users/2024-04-06_t2_bins.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
ID,X,COST,COPIES
0,3900,3900,5000
1,1900,1900,5000
2,2400,2400,5000
3,3400,3400,5000
4,4500,4500,5000
5,900,900,5000
6,1400,1400,5000
7,2900,2900,5000
3 changes: 3 additions & 0 deletions data/onedimensional/users/2024-04-06_t2_items.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ID,X,COPIES,NESTING_LENGTH
0,2379,1,-4
1,1164,1,-4
2 changes: 2 additions & 0 deletions data/onedimensional/users/2024-04-06_t3_bins.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ID,X,COST,COPIES
0,4000,4000,32767
17 changes: 17 additions & 0 deletions data/onedimensional/users/2024-04-06_t3_items.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
ID,X,COPIES,NESTING_LENGTH
0,1022,1,-4
1,1022,1,-4
2,922,3,-4
3,512,3,-4
4,920,5,-4
5,510,5,-4
6,415,1,-4
7,415,1,-4
8,315,2,-4
9,315,2,-4
10,265,1,-4
11,265,1,-4
12,925,1,-4
13,515,1,-4
14,215,1,-4
15,215,1,-4
3 changes: 3 additions & 0 deletions data/onedimensional/users/2024-04-07_bins.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ID,X,COST,COPIES
0,3000,3000,3000
1,7000,7000,5000
5 changes: 5 additions & 0 deletions data/onedimensional/users/2024-04-07_items.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ID,X,COPIES,NESTING_LENGTH
0,290,1,-5
1,1496,2,-5
2,2751,2,-5
3,3351,2,-5
3 changes: 3 additions & 0 deletions data/onedimensional/users/2024-04-09_bins.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ID,X,COST,COPIES
0,6950,6950,8
1,7000,3500,2
2 changes: 2 additions & 0 deletions data/onedimensional/users/2024-04-09_items.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ID,X,COPIES,NESTING_LENGTH
0,2408,8,-4
6 changes: 3 additions & 3 deletions packingsolver/algorithms/column_generation.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* Column Generation algorithm
* Column generation algorithm
*
* Algorithm for Variable-sized Bin Packing, Bin Packing and (Multiple)
* Knapsack Problems.
* Algorithm for variable-sized bin packing, bin packing and (multiple)
* knapsack problems.
*
* Input:
* - m bin types with lower bounds lᵢ, upper bounds uᵢ and costs cᵢ (i = 0..m)
Expand Down
18 changes: 10 additions & 8 deletions packingsolver/algorithms/sequential_value_correction.hpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
/**
* Sequential Value Correction algorithm
* Sequential value correction algorithm
*
* Algorithm for Multiple Knapsack, Bin Packing and Variable-sized Bin Packing
* Algorithm for multiple knapsack, bin packing and variable-sized bin packing
* problems.
*
* For Variable-sized Bin Packing, it doesn't handle the minimum number of
* For variable-sized bin packing, it doesn't handle the minimum number of
* copies of each bin type to use.
*
* The algorithm solves a Single Knapsack subproblem for each bin until either
* The algorithm solves a single knapsack subproblem for each bin until either
* all items are packed, or there is no bin left.
* Then, it restarts but increases the profits of the items that where packed
* in bins with high waste, such that they get packed earlier and hopefully
* generate less waste.
*
* For Variable-sized Bin Packing, this algorithm should require less
* For Variable-sized bin packing, this algorithm should require less
* computational effort to find a good solution than the other algorithms for
* this objective.
* For Multiple Knapsck and Bin Packing, it is useful for boxstacks problem
* type since, in this case, Tree Search might not be applied directly to get a
* For multiple knapsck and bin packing, it is useful for boxstacks problem
* type since, in this case, tree search might not be applied directly to get a
* good solution.
*
* Some references from which this implementation is inspired:
Expand Down Expand Up @@ -109,7 +109,9 @@ SequentialValueCorrectionOutput<Instance, Solution> sequential_value_correction(
}

for (output.number_of_iterations = 0;; output.number_of_iterations++) {
//std::cout << "it " << output.number_of_iterations << std::endl;
//std::cout << "it " << output.number_of_iterations
// << " / " << parameters.maximum_number_of_iterations
// << std::endl;

// Check maximum number of iterations.
if (parameters.maximum_number_of_iterations != -1
Expand Down
2 changes: 1 addition & 1 deletion packingsolver/onedimensional/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ cc_test(
"@boost//:filesystem",
],
copts = STDCPP,
#data = ["//data/onedimensional:onedimensional"],
data = ["//data/onedimensional:onedimensional"],
timeout = "short",
)

11 changes: 11 additions & 0 deletions packingsolver/onedimensional/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ int main(int argc, char *argv[])
("sequential-value-correction-subproblem-queue-size,", po::value<NodeId>(), "set sequential value correction subproblem queue size")
("sequential-value-correction-number-of-iterations,", po::value<Counter>(), "set sequential value correction number of iterations")
("column-generation-subproblem-queue-size,", po::value<NodeId>(), "set column generation subproblem queue size")
("not-anytime-tree-search-queue-size,", po::value<Counter>(), "")
("not-anytime-sequential-single-knapsack-subproblem-queue-size,", po::value<Counter>(), "")
("not-anytime-sequential-value-correction-number-of-iterations,", po::value<Counter>(), "")
("not-anytime-dichotomic-search-subproblem-queue-size,", po::value<Counter>(), "")
;
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
Expand Down Expand Up @@ -133,6 +136,8 @@ int main(int argc, char *argv[])

OptimizeParameters parameters;
read_args(parameters, vm);
if (vm.count("optimization-mode"))
parameters.optimization_mode = vm["optimization-mode"].as<OptimizationMode>();

if (vm.count("use-tree-search"))
parameters.use_tree_search = vm["use-tree-search"].as<bool>();
Expand All @@ -149,8 +154,14 @@ int main(int argc, char *argv[])
parameters.sequential_value_correction_subproblem_queue_size = vm["sequential-value-correction-subproblem-queue-size"].as<NodeId>();
if (vm.count("column-generation-subproblem-queue-size"))
parameters.column_generation_subproblem_queue_size = vm["column-generation-subproblem-queue-size"].as<NodeId>();
if (vm.count("not-anytime-tree-search-queue-size"))
parameters.not_anytime_tree_search_queue_size = vm["not-anytime-tree-search-queue-size"].as<Counter>();
if (vm.count("not-anytime-sequential-single-knapsack-subproblem-queue-size"))
parameters.not_anytime_sequential_single_knapsack_subproblem_queue_size = vm["not-anytime-sequential-single-knapsack-subproblem-queue-size"].as<Counter>();
if (vm.count("not-anytime-sequential-value-correction-number-of-iterations"))
parameters.not_anytime_sequential_value_correction_number_of_iterations = vm["not-anytime-sequential-value-correction-number-of-iterations"].as<Counter>();
if (vm.count("not-anytime-dichotomic-search-subproblem-queue-size"))
parameters.not_anytime_dichotomic_search_subproblem_queue_size = vm["not-anytime-dichotomic-search-subproblem-queue-size"].as<Counter>();
const onedimensional::Output output = optimize(instance, parameters);

if (vm.count("certificate"))
Expand Down
102 changes: 102 additions & 0 deletions packingsolver/onedimensional/onedimensional_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,105 @@ TEST(OneDimensional, BinCopies)
EXPECT_EQ(solution.number_of_bins(), 2);
EXPECT_EQ(solution.bin_copies(0), 2);
}

TEST(OneDimensional, Users_2023_08_01)
{
InstanceBuilder instance_builder;
instance_builder.set_objective(packingsolver::Objective::VariableSizedBinPacking);
fs::path directory = fs::path("data") / "onedimensional" / "users";
instance_builder.read_item_types((directory / "2023-08-01_items.csv").string());
instance_builder.read_bin_types((directory / "2023-08-01_bins.csv").string());
Instance instance = instance_builder.build();

OptimizeParameters optimize_parameters;
optimize_parameters.optimization_mode = packingsolver::OptimizationMode::NotAnytime;
optimize_parameters.use_column_generation = 1;
Output output = optimize(instance, optimize_parameters);

EXPECT_EQ(output.solution_pool.best().cost(), 1020 * 10000 + 65 * 8008);
}

TEST(OneDimensional, Users_2024_04_06_t1)
{
InstanceBuilder instance_builder;
instance_builder.set_objective(packingsolver::Objective::VariableSizedBinPacking);
fs::path directory = fs::path("data") / "onedimensional" / "users";
instance_builder.read_item_types((directory / "2024-04-06_t1_items.csv").string());
instance_builder.read_bin_types((directory / "2024-04-06_t1_bins.csv").string());
Instance instance = instance_builder.build();

OptimizeParameters optimize_parameters;
optimize_parameters.optimization_mode = packingsolver::OptimizationMode::NotAnytime;
optimize_parameters.use_column_generation = 1;
Output output = optimize(instance, optimize_parameters);

EXPECT_EQ(output.solution_pool.best().cost(), 19900);
}

TEST(OneDimensional, Users_2024_04_06_t2)
{
InstanceBuilder instance_builder;
instance_builder.set_objective(packingsolver::Objective::VariableSizedBinPacking);
fs::path directory = fs::path("data") / "onedimensional" / "users";
instance_builder.read_item_types((directory / "2024-04-06_t2_items.csv").string());
instance_builder.read_bin_types((directory / "2024-04-06_t2_bins.csv").string());
Instance instance = instance_builder.build();

OptimizeParameters optimize_parameters;
optimize_parameters.optimization_mode = packingsolver::OptimizationMode::NotAnytime;
optimize_parameters.use_column_generation = 1;
Output output = optimize(instance, optimize_parameters);

EXPECT_EQ(output.solution_pool.best().cost(), 3800);
}

TEST(OneDimensional, Users_2024_04_06_t3)
{
InstanceBuilder instance_builder;
instance_builder.set_objective(packingsolver::Objective::VariableSizedBinPacking);
fs::path directory = fs::path("data") / "onedimensional" / "users";
instance_builder.read_item_types((directory / "2024-04-06_t3_items.csv").string());
instance_builder.read_bin_types((directory / "2024-04-06_t3_bins.csv").string());
Instance instance = instance_builder.build();

OptimizeParameters optimize_parameters;
optimize_parameters.optimization_mode = packingsolver::OptimizationMode::NotAnytime;
optimize_parameters.use_tree_search = 1;
Output output = optimize(instance, optimize_parameters);

EXPECT_EQ(output.solution_pool.best().cost(), 20000);
}

TEST(OneDimensional, Users_2024_04_07)
{
InstanceBuilder instance_builder;
instance_builder.set_objective(packingsolver::Objective::VariableSizedBinPacking);
fs::path directory = fs::path("data") / "onedimensional" / "users";
instance_builder.read_item_types((directory / "2024-04-07_items.csv").string());
instance_builder.read_bin_types((directory / "2024-04-07_bins.csv").string());
Instance instance = instance_builder.build();

OptimizeParameters optimize_parameters;
optimize_parameters.optimization_mode = packingsolver::OptimizationMode::NotAnytime;
optimize_parameters.use_dichotomic_search = 1;
Output output = optimize(instance, optimize_parameters);

EXPECT_EQ(output.solution_pool.best().cost(), 19000);
}

TEST(OneDimensional, Users_2024_04_09)
{
InstanceBuilder instance_builder;
instance_builder.set_objective(packingsolver::Objective::VariableSizedBinPacking);
fs::path directory = fs::path("data") / "onedimensional" / "users";
instance_builder.read_item_types((directory / "2024-04-09_items.csv").string());
instance_builder.read_bin_types((directory / "2024-04-09_bins.csv").string());
Instance instance = instance_builder.build();

OptimizeParameters optimize_parameters;
optimize_parameters.optimization_mode = packingsolver::OptimizationMode::NotAnytime;
optimize_parameters.use_sequential_value_correction = 1;
Output output = optimize(instance, optimize_parameters);

EXPECT_EQ(output.solution_pool.best().cost(), 20900);
}
4 changes: 2 additions & 2 deletions packingsolver/onedimensional/optimize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ void optimize_sequential_single_knapsack(
for (Counter queue_size = 1;;) {

if (parameters.optimization_mode != OptimizationMode::Anytime)
queue_size = parameters.not_anytime_dichotomic_search_subproblem_beam_size;
queue_size = parameters.not_anytime_dichotomic_search_subproblem_queue_size;

SequentialValueCorrectionFunction<Instance, Solution> kp_solve
= [&parameters, &queue_size](const Instance& kp_instance)
Expand Down Expand Up @@ -212,7 +212,7 @@ void optimize_dichotomic_search(
for (Counter queue_size = 1;;) {

if (parameters.optimization_mode != OptimizationMode::Anytime)
queue_size = parameters.not_anytime_dichotomic_search_subproblem_beam_size;
queue_size = parameters.not_anytime_dichotomic_search_subproblem_queue_size;

DichotomicSearchFunction<Instance, Solution> bpp_solve
= [&parameters, &queue_size](const Instance& bpp_instance)
Expand Down
2 changes: 1 addition & 1 deletion packingsolver/onedimensional/optimize.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ struct OptimizeParameters: packingsolver::Parameters<Instance, Solution>
* Size of the queue in the bin packing subproblem of the dichotomic search
* algorithm.
*/
NodeId not_anytime_dichotomic_search_subproblem_beam_size = 256;
NodeId not_anytime_dichotomic_search_subproblem_queue_size = 256;
};

const Output optimize(
Expand Down
4 changes: 2 additions & 2 deletions packingsolver/rectangle/optimize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ void optimize_sequential_single_knapsack(
for (Counter queue_size = 1;;) {

if (parameters.optimization_mode != OptimizationMode::Anytime)
queue_size = parameters.not_anytime_dichotomic_search_subproblem_beam_size;
queue_size = parameters.not_anytime_dichotomic_search_subproblem_queue_size;

SequentialValueCorrectionFunction<Instance, Solution> kp_solve
= [&parameters, &queue_size](const Instance& kp_instance)
Expand Down Expand Up @@ -235,7 +235,7 @@ void optimize_dichotomic_search(
for (Counter queue_size = 1;;) {

if (parameters.optimization_mode != OptimizationMode::Anytime)
queue_size = parameters.not_anytime_dichotomic_search_subproblem_beam_size;
queue_size = parameters.not_anytime_dichotomic_search_subproblem_queue_size;

DichotomicSearchFunction<Instance, Solution> bpp_solve
= [&parameters, &queue_size](const Instance& bpp_instance)
Expand Down
2 changes: 1 addition & 1 deletion packingsolver/rectangle/optimize.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ struct OptimizeParameters: packingsolver::Parameters<Instance, Solution>
* Size of the queue in the bin packing subproblem of the dichotomic search
* algorithm.
*/
NodeId not_anytime_dichotomic_search_subproblem_beam_size = 1024;
NodeId not_anytime_dichotomic_search_subproblem_queue_size = 1024;
};

const Output optimize(
Expand Down
4 changes: 2 additions & 2 deletions packingsolver/rectangleguillotine/optimize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ void optimize_sequential_single_knapsack(
for (Counter queue_size = 1;;) {

if (parameters.optimization_mode != OptimizationMode::Anytime)
queue_size = parameters.not_anytime_dichotomic_search_subproblem_beam_size;
queue_size = parameters.not_anytime_dichotomic_search_subproblem_queue_size;

SequentialValueCorrectionFunction<Instance, Solution> kp_solve
= [&parameters, &queue_size](const Instance& kp_instance)
Expand Down Expand Up @@ -221,7 +221,7 @@ void optimize_dichotomic_search(
for (Counter queue_size = 1;;) {

if (parameters.optimization_mode != OptimizationMode::Anytime)
queue_size = parameters.not_anytime_dichotomic_search_subproblem_beam_size;
queue_size = parameters.not_anytime_dichotomic_search_subproblem_queue_size;

DichotomicSearchFunction<Instance, Solution> bpp_solve
= [&parameters, &queue_size](const Instance& bpp_instance)
Expand Down
2 changes: 1 addition & 1 deletion packingsolver/rectangleguillotine/optimize.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ struct OptimizeParameters: packingsolver::Parameters<Instance, Solution>
* Size of the queue in the bin packing subproblem of the dichotomic search
* algorithm.
*/
NodeId not_anytime_dichotomic_search_subproblem_beam_size = 256;
NodeId not_anytime_dichotomic_search_subproblem_queue_size = 256;
};

const Output optimize(
Expand Down

0 comments on commit a7f09f0

Please sign in to comment.