diff --git a/data/onedimensional/users/2023-08-01_parameters.csv b/data/onedimensional/users/2023-08-01_parameters.csv new file mode 100644 index 000000000..fbdd3fc31 --- /dev/null +++ b/data/onedimensional/users/2023-08-01_parameters.csv @@ -0,0 +1,2 @@ +NAME,VALUE +objective,variable-sized-bin-packing diff --git a/data/onedimensional/users/2023-08-01_solution.csv b/data/onedimensional/users/2023-08-01_solution.csv new file mode 100644 index 000000000..06f302239 --- /dev/null +++ b/data/onedimensional/users/2023-08-01_solution.csv @@ -0,0 +1,51 @@ +TYPE,ID,COPIES,BIN,X,LX +BIN,2,120,0,0,10000 +ITEM,2,120,0,0,4624 +ITEM,2,120,0,4624,4624 +BIN,2,299,1,0,10000 +ITEM,3,299,1,0,3589 +ITEM,7,299,1,3589,732 +ITEM,3,299,1,4321,3589 +ITEM,9,299,1,7910,2056 +BIN,2,236,2,0,10000 +ITEM,0,236,2,0,9809 +BIN,1,64,3,0,7000 +ITEM,6,64,3,0,3470 +ITEM,6,64,3,3470,3470 +BIN,2,47,4,0,10000 +ITEM,4,47,4,0,8369 +BIN,2,174,5,0,10000 +ITEM,6,174,5,0,3470 +ITEM,5,174,5,3470,3236 +ITEM,5,174,5,6706,3236 +BIN,2,1,6,0,10000 +ITEM,3,1,6,0,3589 +ITEM,8,1,6,3589,365 +ITEM,1,1,6,3954,739 +ITEM,1,1,6,4693,739 +ITEM,1,1,6,5432,739 +ITEM,3,1,6,6171,3589 +BIN,2,87,7,0,10000 +ITEM,8,87,7,0,365 +ITEM,8,87,7,365,365 +ITEM,8,87,7,730,365 +ITEM,8,87,7,1095,365 +ITEM,8,87,7,1460,365 +ITEM,8,87,7,1825,365 +ITEM,3,87,7,2190,3589 +ITEM,3,87,7,5779,3589 +ITEM,8,87,7,9368,365 +BIN,2,54,8,0,10000 +ITEM,3,54,8,0,3589 +ITEM,3,54,8,3589,3589 +BIN,2,1,9,0,10000 +ITEM,8,1,9,0,365 +ITEM,4,1,9,365,8369 +BIN,2,1,10,0,10000 +ITEM,3,1,10,0,3589 +ITEM,2,1,10,3589,4624 +ITEM,8,1,10,8213,365 +ITEM,8,1,10,8578,365 +ITEM,1,1,10,8943,739 +BIN,1,1,11,0,7000 +ITEM,3,1,11,0,3589 diff --git a/data/onedimensional/users/2024-04-06_t0_parameters.csv b/data/onedimensional/users/2024-04-06_t0_parameters.csv new file mode 100644 index 000000000..fbdd3fc31 --- /dev/null +++ b/data/onedimensional/users/2024-04-06_t0_parameters.csv @@ -0,0 +1,2 @@ +NAME,VALUE +objective,variable-sized-bin-packing diff --git a/data/onedimensional/users/2024-04-06_t0_solution.csv b/data/onedimensional/users/2024-04-06_t0_solution.csv new file mode 100644 index 000000000..593ac6627 --- /dev/null +++ b/data/onedimensional/users/2024-04-06_t0_solution.csv @@ -0,0 +1,11 @@ +TYPE,ID,COPIES,BIN,X,LX +BIN,0,1,0,0,3000 +ITEM,1,1,0,0,1496 +ITEM,1,1,0,1501,1496 +BIN,1,1,1,0,7000 +ITEM,3,1,1,0,3351 +ITEM,3,1,1,3356,3351 +BIN,1,1,2,0,7000 +ITEM,2,1,2,0,2751 +ITEM,2,1,2,2756,2751 +ITEM,0,1,2,5512,290 diff --git a/data/onedimensional/users/2024-04-06_t1_parameters.csv b/data/onedimensional/users/2024-04-06_t1_parameters.csv new file mode 100644 index 000000000..fbdd3fc31 --- /dev/null +++ b/data/onedimensional/users/2024-04-06_t1_parameters.csv @@ -0,0 +1,2 @@ +NAME,VALUE +objective,variable-sized-bin-packing diff --git a/data/onedimensional/users/2024-04-06_t1_solution.csv b/data/onedimensional/users/2024-04-06_t1_solution.csv new file mode 100644 index 000000000..541f779de --- /dev/null +++ b/data/onedimensional/users/2024-04-06_t1_solution.csv @@ -0,0 +1,15 @@ +TYPE,ID,COPIES,BIN,X,LX +BIN,3,2,0,0,4500 +ITEM,1,2,0,0,1764 +ITEM,1,2,0,1768,1764 +ITEM,2,2,0,3536,964 +BIN,3,1,1,0,4500 +ITEM,0,1,1,0,1768 +ITEM,0,1,1,1772,1768 +ITEM,3,1,1,3544,864 +BIN,3,1,2,0,4500 +ITEM,1,1,2,0,1764 +ITEM,4,1,2,1768,1774 +ITEM,5,1,2,3546,764 +BIN,6,1,3,0,1900 +ITEM,0,1,3,0,1768 diff --git a/data/onedimensional/users/2024-04-06_t2_parameters.csv b/data/onedimensional/users/2024-04-06_t2_parameters.csv new file mode 100644 index 000000000..fbdd3fc31 --- /dev/null +++ b/data/onedimensional/users/2024-04-06_t2_parameters.csv @@ -0,0 +1,2 @@ +NAME,VALUE +objective,variable-sized-bin-packing diff --git a/data/onedimensional/users/2024-04-06_t2_solution.csv b/data/onedimensional/users/2024-04-06_t2_solution.csv new file mode 100644 index 000000000..528fb44d9 --- /dev/null +++ b/data/onedimensional/users/2024-04-06_t2_solution.csv @@ -0,0 +1,5 @@ +TYPE,ID,COPIES,BIN,X,LX +BIN,6,1,0,0,1400 +ITEM,1,1,0,0,1164 +BIN,2,1,1,0,2400 +ITEM,0,1,1,0,2379 diff --git a/data/onedimensional/users/2024-04-06_t3_parameters.csv b/data/onedimensional/users/2024-04-06_t3_parameters.csv new file mode 100644 index 000000000..fbdd3fc31 --- /dev/null +++ b/data/onedimensional/users/2024-04-06_t3_parameters.csv @@ -0,0 +1,2 @@ +NAME,VALUE +objective,variable-sized-bin-packing diff --git a/data/onedimensional/users/2024-04-06_t3_solution.csv b/data/onedimensional/users/2024-04-06_t3_solution.csv new file mode 100644 index 000000000..bf840d1a3 --- /dev/null +++ b/data/onedimensional/users/2024-04-06_t3_solution.csv @@ -0,0 +1,36 @@ +TYPE,ID,COPIES,BIN,X,LX +BIN,0,1,0,0,4000 +ITEM,0,1,0,0,1022 +ITEM,1,1,0,1026,1022 +ITEM,12,1,0,2052,925 +ITEM,2,1,0,2981,922 +BIN,0,1,1,0,4000 +ITEM,2,1,1,0,922 +ITEM,2,1,1,926,922 +ITEM,4,1,1,1852,920 +ITEM,4,1,1,2776,920 +ITEM,10,1,1,3700,265 +BIN,0,1,2,0,4000 +ITEM,4,1,2,0,920 +ITEM,4,1,2,924,920 +ITEM,4,1,2,1848,920 +ITEM,13,1,2,2772,515 +ITEM,3,1,2,3291,512 +BIN,0,1,3,0,4000 +ITEM,3,1,3,0,512 +ITEM,3,1,3,516,512 +ITEM,5,1,3,1032,510 +ITEM,5,1,3,1546,510 +ITEM,5,1,3,2060,510 +ITEM,5,1,3,2574,510 +ITEM,5,1,3,3088,510 +ITEM,8,1,3,3602,315 +BIN,0,1,4,0,4000 +ITEM,6,1,4,0,415 +ITEM,7,1,4,419,415 +ITEM,8,1,4,838,315 +ITEM,9,1,4,1157,315 +ITEM,9,1,4,1476,315 +ITEM,11,1,4,1795,265 +ITEM,14,1,4,2064,215 +ITEM,15,1,4,2283,215 diff --git a/data/onedimensional/users/2024-04-07_parameters.csv b/data/onedimensional/users/2024-04-07_parameters.csv new file mode 100644 index 000000000..fbdd3fc31 --- /dev/null +++ b/data/onedimensional/users/2024-04-07_parameters.csv @@ -0,0 +1,2 @@ +NAME,VALUE +objective,variable-sized-bin-packing diff --git a/data/onedimensional/users/2024-04-07_solution.csv b/data/onedimensional/users/2024-04-07_solution.csv new file mode 100644 index 000000000..593ac6627 --- /dev/null +++ b/data/onedimensional/users/2024-04-07_solution.csv @@ -0,0 +1,11 @@ +TYPE,ID,COPIES,BIN,X,LX +BIN,0,1,0,0,3000 +ITEM,1,1,0,0,1496 +ITEM,1,1,0,1501,1496 +BIN,1,1,1,0,7000 +ITEM,3,1,1,0,3351 +ITEM,3,1,1,3356,3351 +BIN,1,1,2,0,7000 +ITEM,2,1,2,0,2751 +ITEM,2,1,2,2756,2751 +ITEM,0,1,2,5512,290 diff --git a/data/onedimensional/users/2024-04-09_parameters.csv b/data/onedimensional/users/2024-04-09_parameters.csv new file mode 100644 index 000000000..fbdd3fc31 --- /dev/null +++ b/data/onedimensional/users/2024-04-09_parameters.csv @@ -0,0 +1,2 @@ +NAME,VALUE +objective,variable-sized-bin-packing diff --git a/data/onedimensional/users/2024-04-09_solution.csv b/data/onedimensional/users/2024-04-09_solution.csv new file mode 100644 index 000000000..8558762d1 --- /dev/null +++ b/data/onedimensional/users/2024-04-09_solution.csv @@ -0,0 +1,7 @@ +TYPE,ID,COPIES,BIN,X,LX +BIN,1,2,0,0,7000 +ITEM,0,2,0,0,2408 +ITEM,0,2,0,2412,2408 +BIN,0,2,1,0,6950 +ITEM,0,2,1,0,2408 +ITEM,0,2,1,2412,2408 diff --git a/packingsolver/algorithms/common.cpp b/packingsolver/algorithms/common.cpp index 675c8d328..ee170e281 100644 --- a/packingsolver/algorithms/common.cpp +++ b/packingsolver/algorithms/common.cpp @@ -21,7 +21,7 @@ std::istream& packingsolver::operator>>( } std::ostream& packingsolver::operator<<( - std::ostream &os, + std::ostream& os, OptimizationMode optimization_mode) { switch (optimization_mode) { diff --git a/packingsolver/boxstacks/instance_builder.cpp b/packingsolver/boxstacks/instance_builder.cpp index a052fdaf9..e22878e66 100644 --- a/packingsolver/boxstacks/instance_builder.cpp +++ b/packingsolver/boxstacks/instance_builder.cpp @@ -378,7 +378,8 @@ void InstanceBuilder::set_item_types_infinite_copies() /////////////////////////////// Read from files //////////////////////////////// //////////////////////////////////////////////////////////////////////////////// -void InstanceBuilder::read_parameters(std::string parameters_path) +void InstanceBuilder::read_parameters( + const std::string& parameters_path) { if (parameters_path.empty()) return; @@ -407,7 +408,12 @@ void InstanceBuilder::read_parameters(std::string parameters_path) } } //std::cout << "name " << name << std::endl; - if (name == "unloading-constraint") { + if (name == "objective") { + Objective objective; + std::stringstream ss(value); + ss >> objective; + set_objective(objective); + } if (name == "unloading-constraint") { rectangle::UnloadingConstraint unloading_constraint; std::stringstream ss(value); ss >> unloading_constraint; @@ -416,7 +422,8 @@ void InstanceBuilder::read_parameters(std::string parameters_path) } } -void InstanceBuilder::read_bin_types(std::string bins_path) +void InstanceBuilder::read_bin_types( + const std::string& bins_path) { std::ifstream f(bins_path); if (!f.good()) { @@ -499,7 +506,8 @@ void InstanceBuilder::read_bin_types(std::string bins_path) } } -void InstanceBuilder::read_defects(std::string defects_path) +void InstanceBuilder::read_defects( + const std::string& defects_path) { std::ifstream f(defects_path); if (defects_path != "" && !f.good()) { @@ -565,7 +573,8 @@ void InstanceBuilder::read_defects(std::string defects_path) } -void InstanceBuilder::read_item_types(std::string items_path) +void InstanceBuilder::read_item_types( + const std::string& items_path) { std::ifstream f(items_path); if (!f.good()) { diff --git a/packingsolver/boxstacks/instance_builder.hpp b/packingsolver/boxstacks/instance_builder.hpp index bd39c38e4..33ae71465 100644 --- a/packingsolver/boxstacks/instance_builder.hpp +++ b/packingsolver/boxstacks/instance_builder.hpp @@ -23,16 +23,16 @@ class InstanceBuilder */ /** Read parameters from a file. */ - void read_parameters(std::string parameters_path); + void read_parameters(const std::string& parameters_path); /** Read bin types from a file. */ - void read_bin_types(std::string bins_path); + void read_bin_types(const std::string& bins_path); /** Read defects from a file. */ - void read_defects(std::string defects_path); + void read_defects(const std::string& defects_path); /** Read item types from a file. */ - void read_item_types(std::string items_path); + void read_item_types(const std::string& items_path); /* * Set parameters diff --git a/packingsolver/boxstacks/main.cpp b/packingsolver/boxstacks/main.cpp index 4faa82860..4e62008d4 100644 --- a/packingsolver/boxstacks/main.cpp +++ b/packingsolver/boxstacks/main.cpp @@ -93,7 +93,6 @@ int main(int argc, char *argv[]) } InstanceBuilder instance_builder; - instance_builder.set_objective(vm["objective"].as()); std::string items_path = vm["items"].as(); if (!std::ifstream(items_path).good()) @@ -133,12 +132,17 @@ int main(int argc, char *argv[]) std::string parameters_path = (vm.count("parameters"))? vm["parameters"].as(): - (std::ifstream(items_path + "_parameters.csv").good())? - items_path + "_parameters.csv": + (std::ifstream(vm["items"].as() + "_parameters.csv").good())? + vm["items"].as() + "_parameters.csv": ""; if (!parameters_path.empty()) instance_builder.read_parameters(parameters_path); + if (vm.count("objective")) + instance_builder.set_objective(vm["objective"].as()); + if (vm.count("unloading-constraint")) + instance_builder.set_unloading_constraint(vm["unloading-constraint"].as()); + Instance instance = instance_builder.build(); #if XPRESS_FOUND diff --git a/packingsolver/onedimensional/instance_builder.cpp b/packingsolver/onedimensional/instance_builder.cpp index ad26567d0..f41f3ba12 100644 --- a/packingsolver/onedimensional/instance_builder.cpp +++ b/packingsolver/onedimensional/instance_builder.cpp @@ -236,7 +236,8 @@ void InstanceBuilder::set_item_types_infinite_copies() /////////////////////////////// Read from files //////////////////////////////// //////////////////////////////////////////////////////////////////////////////// -void InstanceBuilder::read_parameters(std::string parameters_path) +void InstanceBuilder::read_parameters( + const std::string& parameters_path) { if (parameters_path.empty()) return; @@ -265,16 +266,17 @@ void InstanceBuilder::read_parameters(std::string parameters_path) } } - //if (name == "unloading-constraint") { - // rectangle::UnloadingConstraint unloading_constraint; - // std::stringstream ss(value); - // ss >> unloading_constraint; - // set_unloading_constraint(unloading_constraint); - //} + if (name == "objective") { + Objective objective; + std::stringstream ss(value); + ss >> objective; + set_objective(objective); + } } } -void InstanceBuilder::read_bin_types(std::string bins_path) +void InstanceBuilder::read_bin_types( + const std::string& bins_path) { std::ifstream f(bins_path); if (!f.good()) { @@ -327,7 +329,8 @@ void InstanceBuilder::read_bin_types(std::string bins_path) } } -void InstanceBuilder::read_item_types(std::string items_path) +void InstanceBuilder::read_item_types( + const std::string& items_path) { std::ifstream f(items_path); if (!f.good()) { diff --git a/packingsolver/onedimensional/instance_builder.hpp b/packingsolver/onedimensional/instance_builder.hpp index a726b43ba..525a441f9 100644 --- a/packingsolver/onedimensional/instance_builder.hpp +++ b/packingsolver/onedimensional/instance_builder.hpp @@ -23,13 +23,13 @@ class InstanceBuilder */ /** Read parameters from a file. */ - void read_parameters(std::string parameters_path); + void read_parameters(const std::string& parameters_path); /** Read bin types from a file. */ - void read_bin_types(std::string bins_path); + void read_bin_types(const std::string& bins_path); /** Read item types from a file. */ - void read_item_types(std::string items_path); + void read_item_types(const std::string& items_path); /* * Set parameters diff --git a/packingsolver/onedimensional/main.cpp b/packingsolver/onedimensional/main.cpp index 74866cedd..e3aebc228 100644 --- a/packingsolver/onedimensional/main.cpp +++ b/packingsolver/onedimensional/main.cpp @@ -102,7 +102,6 @@ int main(int argc, char *argv[]) } InstanceBuilder instance_builder; - instance_builder.set_objective(vm["objective"].as()); std::string items_path = vm["items"].as(); if (!std::ifstream(items_path).good()) @@ -126,11 +125,15 @@ int main(int argc, char *argv[]) std::string parameters_path = (vm.count("parameters"))? vm["parameters"].as(): - (std::ifstream(items_path + "_parameters.csv").good())? - items_path + "_parameters.csv": + (std::ifstream(vm["items"].as() + "_parameters.csv").good())? + vm["items"].as() + "_parameters.csv": ""; - if (!parameters_path.empty()) + if (!parameters_path.empty()) { instance_builder.read_parameters(parameters_path); + } + + if (vm.count("objective")) + instance_builder.set_objective(vm["objective"].as()); Instance instance = instance_builder.build(); diff --git a/packingsolver/onedimensional/onedimensional_test.cpp b/packingsolver/onedimensional/onedimensional_test.cpp index dd3647d60..16bb71679 100644 --- a/packingsolver/onedimensional/onedimensional_test.cpp +++ b/packingsolver/onedimensional/onedimensional_test.cpp @@ -23,10 +23,10 @@ TEST(OneDimensional, BinCopies) 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_builder.read_parameters((directory / "2023-08-01_parameters.csv").string()); Instance instance = instance_builder.build(); OptimizeParameters optimize_parameters; @@ -34,16 +34,17 @@ TEST(OneDimensional, Users_2023_08_01) optimize_parameters.use_column_generation = 1; Output output = optimize(instance, optimize_parameters); - EXPECT_EQ(output.solution_pool.best().cost(), 1020 * 10000 + 65 * 8008); + Solution solution(instance, (directory / "2023-08-01_solution.csv").string()); + EXPECT_EQ(!(output.solution_pool.best() < solution), true); } 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_builder.read_parameters((directory / "2024-04-06_t1_parameters.csv").string()); Instance instance = instance_builder.build(); OptimizeParameters optimize_parameters; @@ -51,16 +52,17 @@ TEST(OneDimensional, Users_2024_04_06_t1) optimize_parameters.use_column_generation = 1; Output output = optimize(instance, optimize_parameters); - EXPECT_EQ(output.solution_pool.best().cost(), 19900); + Solution solution(instance, (directory / "2024-04-06_t1_solution.csv").string()); + EXPECT_EQ(!(output.solution_pool.best() < solution), true); } 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_builder.read_parameters((directory / "2024-04-06_t2_parameters.csv").string()); Instance instance = instance_builder.build(); OptimizeParameters optimize_parameters; @@ -68,16 +70,17 @@ TEST(OneDimensional, Users_2024_04_06_t2) optimize_parameters.use_column_generation = 1; Output output = optimize(instance, optimize_parameters); - EXPECT_EQ(output.solution_pool.best().cost(), 3800); + Solution solution(instance, (directory / "2024-04-06_t2_solution.csv").string()); + EXPECT_EQ(!(output.solution_pool.best() < solution), true); } 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_builder.read_parameters((directory / "2024-04-06_t3_parameters.csv").string()); Instance instance = instance_builder.build(); OptimizeParameters optimize_parameters; @@ -85,33 +88,36 @@ TEST(OneDimensional, Users_2024_04_06_t3) optimize_parameters.use_tree_search = 1; Output output = optimize(instance, optimize_parameters); - EXPECT_EQ(output.solution_pool.best().cost(), 20000); + Solution solution(instance, (directory / "2024-04-06_t3_solution.csv").string()); + EXPECT_EQ(!(output.solution_pool.best() < solution), true); } 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_builder.read_parameters((directory / "2024-04-07_parameters.csv").string()); Instance instance = instance_builder.build(); OptimizeParameters optimize_parameters; optimize_parameters.optimization_mode = packingsolver::OptimizationMode::NotAnytime; - optimize_parameters.use_dichotomic_search = 1; + optimize_parameters.use_column_generation = 1; + optimize_parameters.verbosity_level = 2; Output output = optimize(instance, optimize_parameters); - EXPECT_EQ(output.solution_pool.best().cost(), 19000); + Solution solution(instance, (directory / "2024-04-07_solution.csv").string()); + //EXPECT_EQ(!(output.solution_pool.best() < solution), true); } 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_builder.read_parameters((directory / "2024-04-09_parameters.csv").string()); Instance instance = instance_builder.build(); OptimizeParameters optimize_parameters; @@ -119,5 +125,6 @@ TEST(OneDimensional, Users_2024_04_09) optimize_parameters.use_sequential_value_correction = 1; Output output = optimize(instance, optimize_parameters); - EXPECT_EQ(output.solution_pool.best().cost(), 20900); + Solution solution(instance, (directory / "2024-04-09_solution.csv").string()); + EXPECT_EQ(!(output.solution_pool.best() < solution), true); } diff --git a/packingsolver/onedimensional/solution.cpp b/packingsolver/onedimensional/solution.cpp index 1a035fac6..b1ae766eb 100644 --- a/packingsolver/onedimensional/solution.cpp +++ b/packingsolver/onedimensional/solution.cpp @@ -107,6 +107,89 @@ void Solution::append( } } +Solution::Solution( + const Instance& instance, + const std::string& certificate_path): + Solution(instance) +{ + std::ifstream f(certificate_path); + if (!f.good()) { + throw std::runtime_error( + "Unable to open file \"" + certificate_path + "\"."); + } + + std::string tmp; + std::vector line; + std::vector labels; + + // read bin file + getline(f, tmp); + labels = optimizationtools::split(tmp, ','); + while (getline(f, tmp)) { + line = optimizationtools::split(tmp, ','); + + std::string type; + BinTypeId type_id = -1; + BinPos copies = -1; + BinPos bin_pos = -1; + Length x = -1; + Length lx = -1; + + for (Counter i = 0; i < (Counter)line.size(); ++i) { + if (labels[i] == "TYPE") { + type = line[i]; + } else if (labels[i] == "ID") { + type_id = (BinTypeId)std::stol(line[i]); + } else if (labels[i] == "COPIES") { + copies = (BinPos)std::stol(line[i]); + } else if (labels[i] == "BIN") { + bin_pos = (BinPos)std::stol(line[i]); + } else if (labels[i] == "X") { + x = (Length)std::stod(line[i]); + } else if (labels[i] == "LX") { + lx = (Length)std::stod(line[i]); + } + } + if (type == "") { + throw std::runtime_error( + "Missing \"TYPE\" value in \"" + certificate_path + "\"."); + } + if (type_id == -1) { + throw std::runtime_error( + "Missing \"ID\" value in \"" + certificate_path + "\"."); + } + if (copies == -1) { + throw std::runtime_error( + "Missing \"COPIES\" value in \"" + certificate_path + "\"."); + } + if (bin_pos == -1) { + throw std::runtime_error( + "Missing \"BIN\" value in \"" + certificate_path + "\"."); + } + if (x == -1) { + throw std::runtime_error( + "Missing \"X\" value in \"" + certificate_path + "\"."); + } + if (lx == -1) { + throw std::runtime_error( + "Missing \"LX\" value in \"" + certificate_path + "\"."); + } + + if (type == "BIN") { + add_bin( + type_id, + copies); + } else if (type == "ITEM") { + add_item( + bin_pos, + type_id); + } else { + throw std::runtime_error( + "Wrong \"TYPE\" value in \"" + certificate_path + "\"."); + } + } +} + bool Solution::operator<(const Solution& solution) const { switch (instance().objective()) { diff --git a/packingsolver/onedimensional/solution.hpp b/packingsolver/onedimensional/solution.hpp index 72da824a6..eb413ab6a 100644 --- a/packingsolver/onedimensional/solution.hpp +++ b/packingsolver/onedimensional/solution.hpp @@ -43,7 +43,7 @@ class Solution public: /* - * Constructors and destructor. + * Constructors and destructor */ /** Standard constructor. */ @@ -75,6 +75,11 @@ class Solution const std::vector& bin_type_ids, const std::vector& item_type_ids); + /** Read a solution from a file. */ + Solution( + const Instance& instance, + const std::string& certificate_path); + /* * Getters */ diff --git a/packingsolver/rectangle/instance_builder.cpp b/packingsolver/rectangle/instance_builder.cpp index 1074f58be..cd9270e66 100644 --- a/packingsolver/rectangle/instance_builder.cpp +++ b/packingsolver/rectangle/instance_builder.cpp @@ -306,7 +306,8 @@ void InstanceBuilder::multiply_item_types_copies(ItemPos factor) /////////////////////////////// Read from files //////////////////////////////// //////////////////////////////////////////////////////////////////////////////// -void InstanceBuilder::read_parameters(std::string parameters_path) +void InstanceBuilder::read_parameters( + const std::string& parameters_path) { if (parameters_path.empty()) return; @@ -337,7 +338,12 @@ void InstanceBuilder::read_parameters(std::string parameters_path) } } - if (name == "UNLOADING_CONSTRAINT") { + if (name == "objective") { + Objective objective; + std::stringstream ss(value); + ss >> objective; + set_objective(objective); + } else if (name == "unloading_constraint") { rectangle::UnloadingConstraint unloading_constraint; std::stringstream ss(value); ss >> unloading_constraint; @@ -346,7 +352,8 @@ void InstanceBuilder::read_parameters(std::string parameters_path) } } -void InstanceBuilder::read_bin_types(std::string bins_path) +void InstanceBuilder::read_bin_types( + const std::string& bins_path) { std::ifstream f(bins_path); if (!f.good()) { @@ -407,7 +414,8 @@ void InstanceBuilder::read_bin_types(std::string bins_path) } } -void InstanceBuilder::read_defects(std::string defects_path) +void InstanceBuilder::read_defects( + const std::string& defects_path) { std::ifstream f(defects_path); if (defects_path != "" && !f.good()) { @@ -473,7 +481,8 @@ void InstanceBuilder::read_defects(std::string defects_path) } -void InstanceBuilder::read_item_types(std::string items_path) +void InstanceBuilder::read_item_types( + const std::string& items_path) { std::ifstream f(items_path); if (!f.good()) { diff --git a/packingsolver/rectangle/instance_builder.hpp b/packingsolver/rectangle/instance_builder.hpp index 3a4dd9a15..b73e1c7be 100644 --- a/packingsolver/rectangle/instance_builder.hpp +++ b/packingsolver/rectangle/instance_builder.hpp @@ -23,16 +23,16 @@ class InstanceBuilder */ /** Read parameters from a file. */ - void read_parameters(std::string parameters_path); + void read_parameters(const std::string& parameters_path); /** Read bin types from a file. */ - void read_bin_types(std::string bins_path); + void read_bin_types(const std::string& bins_path); /** Read defects from a file. */ - void read_defects(std::string defects_path); + void read_defects(const std::string& defects_path); /** Read item types from a file. */ - void read_item_types(std::string items_path); + void read_item_types(const std::string& items_path); /* * Set parameters diff --git a/packingsolver/rectangle/main.cpp b/packingsolver/rectangle/main.cpp index fc928345f..fa78b69dd 100644 --- a/packingsolver/rectangle/main.cpp +++ b/packingsolver/rectangle/main.cpp @@ -104,7 +104,6 @@ int main(int argc, char *argv[]) } InstanceBuilder instance_builder; - instance_builder.set_objective(vm["objective"].as()); std::string items_path = vm["items"].as(); if (!std::ifstream(items_path).good()) @@ -144,12 +143,14 @@ int main(int argc, char *argv[]) std::string parameters_path = (vm.count("parameters"))? vm["parameters"].as(): - (std::ifstream(items_path + "_parameters.csv").good())? - items_path + "_parameters.csv": + (std::ifstream(vm["items"].as() + "_parameters.csv").good())? + vm["items"].as() + "_parameters.csv": ""; if (!parameters_path.empty()) instance_builder.read_parameters(parameters_path); + if (vm.count("objective")) + instance_builder.set_objective(vm["objective"].as()); if (vm.count("unloading-constraint")) instance_builder.set_unloading_constraint(vm["unloading-constraint"].as()); diff --git a/packingsolver/rectangleguillotine/instance_builder.cpp b/packingsolver/rectangleguillotine/instance_builder.cpp index afdbce8f5..2efdfe4b5 100644 --- a/packingsolver/rectangleguillotine/instance_builder.cpp +++ b/packingsolver/rectangleguillotine/instance_builder.cpp @@ -402,7 +402,8 @@ void InstanceBuilder::set_item_types_oriented() /////////////////////////////// Read from files //////////////////////////////// //////////////////////////////////////////////////////////////////////////////// -void InstanceBuilder::read_parameters(std::string parameters_path) +void InstanceBuilder::read_parameters( + const std::string& parameters_path) { if (parameters_path.empty()) return; @@ -433,7 +434,12 @@ void InstanceBuilder::read_parameters(std::string parameters_path) } } - if (name == "number_of_stages") { + if (name == "objective") { + Objective objective; + std::stringstream ss(value); + ss >> objective; + set_objective(objective); + } else if (name == "number_of_stages") { set_number_of_stages(std::stol(value)); } else if (name == "cut_type") { CutType cut_type; @@ -461,7 +467,8 @@ void InstanceBuilder::read_parameters(std::string parameters_path) } } -void InstanceBuilder::read_bin_types(std::string bins_path) +void InstanceBuilder::read_bin_types( + const std::string& bins_path) { std::ifstream f(bins_path); if (!f.good()) { @@ -550,7 +557,8 @@ void InstanceBuilder::read_bin_types(std::string bins_path) } } -void InstanceBuilder::read_defects(std::string defects_path) +void InstanceBuilder::read_defects( + const std::string& defects_path) { if (defects_path.empty()) return; @@ -615,7 +623,8 @@ void InstanceBuilder::read_defects(std::string defects_path) } } -void InstanceBuilder::read_item_types(std::string items_path) +void InstanceBuilder::read_item_types( + const std::string& items_path) { std::ifstream f(items_path); if (!f.good()) { diff --git a/packingsolver/rectangleguillotine/instance_builder.hpp b/packingsolver/rectangleguillotine/instance_builder.hpp index cacf45bd4..a89dca65d 100644 --- a/packingsolver/rectangleguillotine/instance_builder.hpp +++ b/packingsolver/rectangleguillotine/instance_builder.hpp @@ -23,16 +23,16 @@ class InstanceBuilder */ /** Read parameters from a file. */ - void read_parameters(std::string parameters_path); + void read_parameters(const std::string& parameters_path); /** Read bin types from a file. */ - void read_bin_types(std::string bins_path); + void read_bin_types(const std::string& bins_path); /** Read defects from a file. */ - void read_defects(std::string defects_path); + void read_defects(const std::string& defects_path); /** Read item types from a file. */ - void read_item_types(std::string items_path); + void read_item_types(const std::string& items_path); /* * Set parameters diff --git a/packingsolver/rectangleguillotine/main.cpp b/packingsolver/rectangleguillotine/main.cpp index a4459dd76..3e3c31630 100644 --- a/packingsolver/rectangleguillotine/main.cpp +++ b/packingsolver/rectangleguillotine/main.cpp @@ -111,7 +111,6 @@ int main(int argc, char *argv[]) } InstanceBuilder instance_builder; - instance_builder.set_objective(vm["objective"].as()); std::string items_path = vm["items"].as(); if (!std::ifstream(items_path).good()) @@ -151,12 +150,14 @@ int main(int argc, char *argv[]) std::string parameters_path = (vm.count("parameters"))? vm["parameters"].as(): - (std::ifstream(items_path + "_parameters.csv").good())? - items_path + "_parameters.csv": + (std::ifstream(vm["items"].as() + "_parameters.csv").good())? + vm["items"].as() + "_parameters.csv": ""; if (!parameters_path.empty()) instance_builder.read_parameters(parameters_path); + if (vm.count("objective")) + instance_builder.set_objective(vm["objective"].as()); if (vm.count("predefined")) instance_builder.set_predefined(vm["predefined"].as());