diff --git a/packingsolver/algorithms/sequential_value_correction.hpp b/packingsolver/algorithms/sequential_value_correction.hpp index 3ca0c326a..9f63ccafe 100644 --- a/packingsolver/algorithms/sequential_value_correction.hpp +++ b/packingsolver/algorithms/sequential_value_correction.hpp @@ -142,36 +142,45 @@ SequentialValueCorrectionOutput sequential_value_correction( if (solution.number_of_bins() == instance.number_of_bins()) break; + std::vector 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 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 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; @@ -230,7 +239,7 @@ SequentialValueCorrectionOutput 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) { @@ -283,8 +292,9 @@ SequentialValueCorrectionOutput 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; } @@ -312,6 +322,7 @@ SequentialValueCorrectionOutput sequential_value_correction( // << " new " << solution.number_of_bins() // << std::endl; //} + //std::cout << solution.cost() << std::endl; // Update best solution. std::stringstream ss; @@ -335,6 +346,7 @@ SequentialValueCorrectionOutput 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) { diff --git a/packingsolver/onedimensional/onedimensional_test.cpp b/packingsolver/onedimensional/onedimensional_test.cpp index 16bb71679..c6bbebc2a 100644 --- a/packingsolver/onedimensional/onedimensional_test.cpp +++ b/packingsolver/onedimensional/onedimensional_test.cpp @@ -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) diff --git a/packingsolver/onedimensional/optimize.cpp b/packingsolver/onedimensional/optimize.cpp index d88d05b1b..82336d4f5 100644 --- a/packingsolver/onedimensional/optimize.cpp +++ b/packingsolver/onedimensional/optimize.cpp @@ -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; diff --git a/packingsolver/rectangle/optimize.cpp b/packingsolver/rectangle/optimize.cpp index 40830c0c0..4247316a9 100644 --- a/packingsolver/rectangle/optimize.cpp +++ b/packingsolver/rectangle/optimize.cpp @@ -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; diff --git a/packingsolver/rectangleguillotine/optimize.cpp b/packingsolver/rectangleguillotine/optimize.cpp index c15dcffdc..64501c1b9 100644 --- a/packingsolver/rectangleguillotine/optimize.cpp +++ b/packingsolver/rectangleguillotine/optimize.cpp @@ -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;