Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions include/packingsolver/irregular/algorithm_formatter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ class AlgorithmFormatter
const Solution& solution,
const std::string& s);

/** Update the knapsack bound. */
void update_knapsack_bound(
Profit profit);

/** Update the bin packing bound. */
void update_bin_packing_bound(
BinPos number_of_bins);

/** Method to call at the end of the algorithm. */
void end();

Expand Down
41 changes: 40 additions & 1 deletion src/irregular/algorithm_formatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,11 @@ void AlgorithmFormatter::update_solution(
parameters_.new_solution_callback(output_);

// Check optimality.
if (instance_.objective() == Objective::BinPacking) {
if (instance_.objective() == Objective::Knapsack) {
if (equal(output_.knapsack_bound, output_.solution_pool.best().profit())) {
end_ = true;
}
} else if (instance_.objective() == Objective::BinPacking) {
if (output_.solution_pool.best().full()
&& output_.bin_packing_bound == output_.solution_pool.best().number_of_bins()) {
end_ = true;
Expand All @@ -220,6 +224,41 @@ void AlgorithmFormatter::update_solution(
mutex_.unlock();
}

void AlgorithmFormatter::update_knapsack_bound(
Profit profit)
{
mutex_.lock();
if (profit < output_.knapsack_bound) {
output_.knapsack_bound = profit;
output_.json["IntermediaryOutputs"].push_back(output_.to_json());
parameters_.new_solution_callback(output_);

// Check optimality.
if (equal(output_.knapsack_bound, output_.solution_pool.best().profit())) {
end_ = true;
}
}
mutex_.unlock();
}

void AlgorithmFormatter::update_bin_packing_bound(
BinPos number_of_bins)
{
mutex_.lock();
if (number_of_bins > output_.bin_packing_bound) {
output_.bin_packing_bound = number_of_bins;
output_.json["IntermediaryOutputs"].push_back(output_.to_json());
parameters_.new_solution_callback(output_);

// Check optimality.
if (output_.solution_pool.best().full()
&& output_.bin_packing_bound == output_.solution_pool.best().number_of_bins()) {
end_ = true;
}
}
mutex_.unlock();
}

void AlgorithmFormatter::end()
{
output_.time = parameters_.timer.elapsed_time();
Expand Down
9 changes: 9 additions & 0 deletions src/irregular/optimize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ void optimize_tree_search_worker(
ibs_parameters);

// Check end.
if (algorithm_formatter.end_boolean())
break;
if (parameters.timer.needs_to_end())
break;

Expand Down Expand Up @@ -277,6 +279,8 @@ void optimize_sequential_single_knapsack(
sequential_value_correction<Instance, InstanceBuilder, Solution, AlgorithmFormatter>(instance, kp_solve, svc_parameters);

// Check end.
if (algorithm_formatter.end_boolean())
break;
if (parameters.timer.needs_to_end())
break;

Expand Down Expand Up @@ -380,6 +384,8 @@ void optimize_dichotomic_search(
auto ds_output = dichotomic_search<Instance, InstanceBuilder, Solution, AlgorithmFormatter>(instance, bpp_solve, ds_parameters);

// Check end.
if (algorithm_formatter.end_boolean())
break;
if (parameters.timer.needs_to_end())
break;

Expand Down Expand Up @@ -579,6 +585,9 @@ packingsolver::irregular::Output packingsolver::irregular::optimize(
}
}

if (instance.objective() == Objective::Knapsack)
algorithm_formatter.update_knapsack_bound(instance.item_profit());

int last_algorithm =
(use_column_generation)? 4:
(use_dichotomic_search)? 3:
Expand Down
Loading