Skip to content

Commit

Permalink
Add solution file reader for onedimensional
Browse files Browse the repository at this point in the history
  • Loading branch information
fontanf committed Apr 20, 2024
1 parent 7114006 commit edfa661
Show file tree
Hide file tree
Showing 17 changed files with 259 additions and 14 deletions.
2 changes: 2 additions & 0 deletions data/onedimensional/users/2023-08-01_parameters.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
NAME,VALUE
objective,variable-sized-bin-packing
51 changes: 51 additions & 0 deletions data/onedimensional/users/2023-08-01_solution.csv
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions data/onedimensional/users/2024-04-06_t0_parameters.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
NAME,VALUE
objective,variable-sized-bin-packing
11 changes: 11 additions & 0 deletions data/onedimensional/users/2024-04-06_t0_solution.csv
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions data/onedimensional/users/2024-04-06_t1_parameters.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
NAME,VALUE
objective,variable-sized-bin-packing
15 changes: 15 additions & 0 deletions data/onedimensional/users/2024-04-06_t1_solution.csv
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions data/onedimensional/users/2024-04-06_t2_parameters.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
NAME,VALUE
objective,variable-sized-bin-packing
5 changes: 5 additions & 0 deletions data/onedimensional/users/2024-04-06_t2_solution.csv
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions data/onedimensional/users/2024-04-06_t3_parameters.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
NAME,VALUE
objective,variable-sized-bin-packing
36 changes: 36 additions & 0 deletions data/onedimensional/users/2024-04-06_t3_solution.csv
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions data/onedimensional/users/2024-04-07_parameters.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
NAME,VALUE
objective,variable-sized-bin-packing
11 changes: 11 additions & 0 deletions data/onedimensional/users/2024-04-07_solution.csv
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions data/onedimensional/users/2024-04-09_parameters.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
NAME,VALUE
objective,variable-sized-bin-packing
7 changes: 7 additions & 0 deletions data/onedimensional/users/2024-04-09_solution.csv
Original file line number Diff line number Diff line change
@@ -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
33 changes: 20 additions & 13 deletions packingsolver/onedimensional/onedimensional_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,101 +23,108 @@ 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;
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);
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;
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);
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;
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);
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;
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);
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;
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);
Solution solution(instance, (directory / "2024-04-09_solution.csv").string());
EXPECT_EQ(!(output.solution_pool.best() < solution), true);
}
83 changes: 83 additions & 0 deletions packingsolver/onedimensional/solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> line;
std::vector<std::string> 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()) {
Expand Down
7 changes: 6 additions & 1 deletion packingsolver/onedimensional/solution.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Solution
public:

/*
* Constructors and destructor.
* Constructors and destructor
*/

/** Standard constructor. */
Expand Down Expand Up @@ -75,6 +75,11 @@ class Solution
const std::vector<BinTypeId>& bin_type_ids,
const std::vector<ItemTypeId>& item_type_ids);

/** Read a solution from a file. */
Solution(
const Instance& instance,
const std::string& certificate_path);

/*
* Getters
*/
Expand Down

0 comments on commit edfa661

Please sign in to comment.