Skip to content

Commit

Permalink
Fix and improve sequential value correction algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
fontanf committed Oct 4, 2024
1 parent 8cc7191 commit b2487bd
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 29 deletions.
12 changes: 12 additions & 0 deletions include/packingsolver/algorithms/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,18 @@ struct Parameters: optimizationtools::Parameters
NewSolutionCallback<Instance, Solution> new_solution_callback = [](const Output<Instance, Solution>&) { };
};

template <typename Instance>
double largest_bin_space(const Instance& instance)
{
double space_max = 0;
for (BinTypeId bin_type_id = 0;
bin_type_id < instance.number_of_bin_types();
++bin_type_id)
if (space_max < instance.bin_type(bin_type_id).space())
space_max = instance.bin_type(bin_type_id).space();
return space_max;
}

template<class F, F f> struct wrapper_impl;
template<class R, class... Args, R(*f)(Args...)>
struct wrapper_impl<R(*)(Args...), f>
Expand Down
12 changes: 0 additions & 12 deletions src/algorithms/dichotomic_search.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,6 @@
namespace packingsolver
{

template <typename Instance>
double largest_bin_space(const Instance& instance)
{
double space_max = 0;
for (BinTypeId bin_type_id = 0;
bin_type_id < instance.number_of_bin_types();
++bin_type_id)
if (space_max < instance.bin_type(bin_type_id).space())
space_max = instance.bin_type(bin_type_id).space();
return space_max;
}

template <typename Instance>
double mean_item_space(const Instance& instance)
{
Expand Down
32 changes: 15 additions & 17 deletions src/algorithms/sequential_value_correction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,11 @@ SequentialValueCorrectionOutput<Instance, Solution> sequential_value_correction(
} else {
profits[item_type_id] = instance.item_type(item_type_id).space();
}
//std::cout << "item_type_id " << item_type_id
// << " profit " << profits[item_type_id]
// << std::endl;
}
auto lbs = largest_bin_space(instance);

for (output.number_of_iterations = 0;; output.number_of_iterations++) {
//std::cout << "it " << output.number_of_iterations
Expand All @@ -121,9 +125,9 @@ SequentialValueCorrectionOutput<Instance, Solution> sequential_value_correction(
// For VBPP objective, we store the solutions found for each bin type
// at each iterations. Thus, at the next iteration, we can check if the
// previously computed ones are still feasible.
std::vector<Solution> solutions_cur(
std::vector<std::pair<Solution, Profit>> solutions_cur(
instance.number_of_bin_types(),
Solution(instance));
{Solution(instance), 0.0});

for (;;) {

Expand All @@ -149,13 +153,6 @@ SequentialValueCorrectionOutput<Instance, Solution> sequential_value_correction(
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) {
Expand All @@ -182,8 +179,6 @@ SequentialValueCorrectionOutput<Instance, Solution> sequential_value_correction(
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);
}
}
Expand All @@ -195,7 +190,7 @@ SequentialValueCorrectionOutput<Instance, Solution> sequential_value_correction(
const auto& bin_type = instance.bin_type(bin_type_id);

if (instance.objective() == Objective::VariableSizedBinPacking
&& solutions_cur[bin_type_id].number_of_items() > 0) {
&& solutions_cur[bin_type_id].first.number_of_items() > 0) {
// Check if previous solution is still valid.
bool valid = true;
for (ItemTypeId item_type_id = 0;
Expand All @@ -205,7 +200,7 @@ SequentialValueCorrectionOutput<Instance, Solution> sequential_value_correction(
= instance.item_type(item_type_id).copies
- solution.item_copies(item_type_id);
if (item_remaining_copies
< solutions_cur[bin_type_id].item_copies(item_type_id)) {
< solutions_cur[bin_type_id].first.item_copies(item_type_id)) {
valid = false;
break;
}
Expand Down Expand Up @@ -239,7 +234,8 @@ SequentialValueCorrectionOutput<Instance, Solution> sequential_value_correction(
Solution solution(instance);
if (kp_solution.number_of_different_bins() > 0)
solution.append(kp_solution, 0, 1, {bin_type_id}, kp2orig);
solutions_cur[bin_type_id] = solution;
solutions_cur[bin_type_id].first = solution;
solutions_cur[bin_type_id].second = kp_solution.profit();
output.all_patterns.push_back(solution);
}

Expand All @@ -251,10 +247,10 @@ SequentialValueCorrectionOutput<Instance, Solution> sequential_value_correction(
const auto& bin_type = instance.bin_type(bin_type_id);

// Update next solution.
double ratio = solutions_cur[bin_type_id].profit() / bin_type.cost;
double ratio = solutions_cur[bin_type_id].second / bin_type.cost;
//std::cout << "bin_type_id " << bin_type_id
// << " cost " << bin_type.cost
// << " profit " << solutions_cur[bin_type_id].profit()
// << " profit " << solutions_cur[bin_type_id].second
// << " ratio " << ratio
// << std::endl;
if (ratio_best < ratio) {
Expand All @@ -266,7 +262,7 @@ SequentialValueCorrectionOutput<Instance, Solution> sequential_value_correction(
// If no item has been packed, stop.
if (bin_type_id_best == -1)
break;
const Solution& kp_solution_best = solutions_cur[bin_type_id_best];
const Solution& kp_solution_best = solutions_cur[bin_type_id_best].first;

// Compute the number of copies of the selected Knapsack solution
// to add.
Expand Down Expand Up @@ -359,6 +355,8 @@ SequentialValueCorrectionOutput<Instance, Solution> sequential_value_correction(
* item_type_adjusted_space[item_type_id]
/ solution.item_copies(item_type_id);
} else {
item_type_adjusted_space[item_type_id]
+= 100 * lbs * (item_type.copies - solution.item_copies(item_type_id));
profit_new
= item_type_adjusted_space[item_type_id]
/ item_type.copies;
Expand Down

0 comments on commit b2487bd

Please sign in to comment.