Skip to content

Commit

Permalink
Merge pull request #18 from fontanf/feature/improve-svc
Browse files Browse the repository at this point in the history
Feature/improve svc
  • Loading branch information
fontanf authored Apr 20, 2024
2 parents c3c97ef + afcaeab commit e913734
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 23 deletions.
48 changes: 30 additions & 18 deletions packingsolver/algorithms/sequential_value_correction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,36 +142,45 @@ SequentialValueCorrectionOutput<Instance, Solution> sequential_value_correction(
if (solution.number_of_bins() == instance.number_of_bins())
break;

std::vector<ItemTypeId> kp2orig;
for (ItemTypeId item_type_id = 0;
item_type_id < instance.number_of_item_types();
++item_type_id) {
ItemPos copies = instance.item_type(item_type_id).copies
- solution.item_copies(item_type_id);
if (copies > 0)
kp2orig.push_back(item_type_id);
}

double largest_item_space = 0;
for (ItemTypeId item_type_id: kp2orig) {
if (largest_item_space < instance.item_type(item_type_id).space())
largest_item_space = instance.item_type(item_type_id).space();
}
//std::cout << "largest_item_space " << largest_item_space << std::endl;

// Find bin types to try.
std::vector<BinTypeId> bin_type_ids;
if (instance.objective() == Objective::VariableSizedBinPacking) {
for (BinTypeId bin_type_id = 0;
bin_type_id < instance.number_of_bin_types();
++bin_type_id) {
if (solution.bin_copies(bin_type_id)
< instance.bin_type(bin_type_id).copies) {
bin_type_ids.push_back(bin_type_id);
}
const auto& bin_type = instance.bin_type(bin_type_id);
if (solution.bin_copies(bin_type_id) == bin_type.copies)
continue;
if (bin_type.space() < largest_item_space)
continue;
bin_type_ids.push_back(bin_type_id);
}
} else {
bin_type_ids.push_back(instance.bin_type_id(solution.number_of_bins()));
}

std::vector<ItemTypeId> kp2orig;
for (ItemTypeId item_type_id = 0;
item_type_id < instance.number_of_item_types();
++item_type_id) {
ItemPos copies = instance.item_type(item_type_id).copies
- solution.item_copies(item_type_id);
if (copies > 0)
kp2orig.push_back(item_type_id);
}

for (BinTypeId bin_type_id: bin_type_ids) {
const auto& bin_type = instance.bin_type(bin_type_id);

if (instance.objective() == Objective::VariableSizedBinPacking
&& solution.number_of_items() > 0) {
&& solutions_cur[bin_type_id].number_of_items() > 0) {
// Check if previous solution is still valid.
bool valid = true;
for (ItemTypeId item_type_id = 0;
Expand Down Expand Up @@ -230,7 +239,7 @@ SequentialValueCorrectionOutput<Instance, Solution> sequential_value_correction(
double ratio = solutions_cur[bin_type_id].profit() / bin_type.cost;
//std::cout << "bin_type_id " << bin_type_id
// << " cost " << bin_type.cost
// << " profit " << kp_solution_pool.best().profit()
// << " profit " << solutions_cur[bin_type_id].profit()
// << " ratio " << ratio
// << std::endl;
if (ratio_best < ratio) {
Expand Down Expand Up @@ -283,8 +292,9 @@ SequentialValueCorrectionOutput<Instance, Solution> sequential_value_correction(
// << " ratio " << ratio
// << " waste " << waste;
item_type_adjusted_space[item_type_id]
+= (double)copies * (double)instance.item_type(item_type_id).space()
+ ratio * waste;
+= number_of_copies
* ((double)copies * (double)instance.item_type(item_type_id).space()
+ ratio * waste);
//std::cout << " adjusted_space " << item_type_adjusted_space[item_type_id] << std::endl;
}

Expand Down Expand Up @@ -312,6 +322,7 @@ SequentialValueCorrectionOutput<Instance, Solution> sequential_value_correction(
// << " new " << solution.number_of_bins()
// << std::endl;
//}
//std::cout << solution.cost() << std::endl;

// Update best solution.
std::stringstream ss;
Expand All @@ -335,6 +346,7 @@ SequentialValueCorrectionOutput<Instance, Solution> sequential_value_correction(
++item_type_id) {
const auto& item_type = instance.item_type(item_type_id);
//std::cout << "item_type_id " << item_type_id
// << " adjusted_space " << item_type_adjusted_space[item_type_id]
// << " profit " << profits[item_type_id];
Profit profit_new = 0.0;
if (instance.objective() == Objective::Knapsack) {
Expand Down
4 changes: 2 additions & 2 deletions packingsolver/onedimensional/onedimensional_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,12 @@ TEST(OneDimensional, Users_2024_04_07)

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

Solution solution(instance, (directory / "2024-04-07_solution.csv").string());
//EXPECT_EQ(!(output.solution_pool.best() < solution), true);
EXPECT_EQ(!(output.solution_pool.best() < solution), true);
}

TEST(OneDimensional, Users_2024_04_09)
Expand Down
7 changes: 6 additions & 1 deletion packingsolver/onedimensional/optimize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,12 @@ const packingsolver::onedimensional::Output packingsolver::onedimensional::optim
if (instance.number_of_bin_types() == 1) {
use_tree_search = true;
} else {
use_dichotomic_search = true;
if (mean_number_of_items_in_bins
> parameters.many_items_in_bins_threshold) {
use_dichotomic_search = true;
} else {
use_sequential_value_correction = true;
}
}
}
use_column_generation = true;
Expand Down
7 changes: 6 additions & 1 deletion packingsolver/rectangle/optimize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,12 @@ const packingsolver::rectangle::Output packingsolver::rectangle::optimize(
if (instance.number_of_bin_types() == 1) {
use_tree_search = true;
} else {
use_dichotomic_search = true;
if (mean_number_of_items_in_bins
> parameters.many_items_in_bins_threshold) {
use_dichotomic_search = true;
} else {
use_sequential_value_correction = true;
}
}
}
use_column_generation = true;
Expand Down
7 changes: 6 additions & 1 deletion packingsolver/rectangleguillotine/optimize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,12 @@ const packingsolver::rectangleguillotine::Output packingsolver::rectangleguillot
if (instance.number_of_bin_types() == 1) {
use_tree_search = true;
} else {
use_dichotomic_search = true;
if (mean_number_of_items_in_bins
> parameters.many_items_in_bins_threshold) {
use_dichotomic_search = true;
} else {
use_sequential_value_correction = true;
}
}
}
use_column_generation = true;
Expand Down

0 comments on commit e913734

Please sign in to comment.