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
7 changes: 7 additions & 0 deletions include/packingsolver/box/post_process.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ GroupIdenticalBinsOutput group_identical_bins(const Solution& solution);
* 'optimize' and 'parameters' (its output and its post-process being
* disabled).
*
* Each time the re-optimization finds a better solution of the bin, the
* solution with that bin replaced is passed to
* 'parameters.new_solution_callback', in an output of the instance of
* 'solution'. It isn't compared with 'solution': its leftovers may be worse.
*
* Return the solution with that bin replaced by the re-optimized one, or the
* solution itself if the bin is empty or if the re-optimization didn't pack
* all the items of the bin.
Expand All @@ -42,6 +47,8 @@ Solution reoptimize_bin_leftovers(
* leftover value is greater, or if its leftover value is the same and its
* staircase leftover value is greater (see 'Solution::leftovers_better').
*
* 'parameters.new_solution_callback' isn't called.
*
* With the algorithms selected as for any instance, a bin with many items
* found quickly is re-optimized quickly too.
*
Expand Down
7 changes: 7 additions & 0 deletions include/packingsolver/boxstacks/post_process.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ GroupIdenticalBinsOutput group_identical_bins(const Solution& solution);
* 'optimize' and 'parameters' (its output and its post-process being
* disabled).
*
* Each time the re-optimization finds a better solution of the bin, the
* solution with that bin replaced is passed to
* 'parameters.new_solution_callback', in an output of the instance of
* 'solution'. It isn't compared with 'solution': its leftovers may be worse.
*
* Return the solution with that bin replaced by the re-optimized one, or the
* solution itself if the bin is empty or if the re-optimization didn't pack
* all the items of the bin.
Expand All @@ -42,6 +47,8 @@ Solution reoptimize_bin_leftovers(
* leftover value is greater, or if its leftover value is the same and its
* staircase leftover value is greater (see 'Solution::leftovers_better').
*
* 'parameters.new_solution_callback' isn't called.
*
* With the algorithms selected as for any instance, a bin with many items
* found quickly is re-optimized quickly too.
*
Expand Down
7 changes: 7 additions & 0 deletions include/packingsolver/irregular/post_process.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ AnchorOutput anchor(
* 'optimize' and 'parameters' (its output and its post-process being
* disabled).
*
* Each time the re-optimization finds a better solution of the bin, the
* solution with that bin replaced is passed to
* 'parameters.new_solution_callback', in an output of the instance of
* 'solution'. It isn't compared with 'solution': its leftovers may be worse.
*
* Return the solution with that bin replaced by the re-optimized one, or the
* solution itself if the bin is empty or if the re-optimization didn't pack
* all the items of the bin.
Expand All @@ -76,6 +81,8 @@ Solution reoptimize_bin_leftovers(
* leftover value is greater, or if its leftover value is the same and its
* staircase leftover value is greater (see 'Solution::leftovers_better').
*
* 'parameters.new_solution_callback' isn't called.
*
* With the algorithms selected as for any instance, a bin with many items
* found quickly, for example by the tree search periodic packing, is
* re-optimized quickly too.
Expand Down
7 changes: 7 additions & 0 deletions include/packingsolver/rectangle/post_process.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ GroupIdenticalBinsOutput group_identical_bins(const Solution& solution);
* 'optimize' and 'parameters' (its output and its post-process being
* disabled).
*
* Each time the re-optimization finds a better solution of the bin, the
* solution with that bin replaced is passed to
* 'parameters.new_solution_callback', in an output of the instance of
* 'solution'. It isn't compared with 'solution': its leftovers may be worse.
*
* Return the solution with that bin replaced by the re-optimized one, or the
* solution itself if the bin is empty or if the re-optimization didn't pack
* all the items of the bin.
Expand All @@ -42,6 +47,8 @@ Solution reoptimize_bin_leftovers(
* leftover value is greater, or if its leftover value is the same and its
* staircase leftover value is greater (see 'Solution::leftovers_better').
*
* 'parameters.new_solution_callback' isn't called.
*
* With the algorithms selected as for any instance, a bin with many items
* found quickly is re-optimized quickly too.
*
Expand Down
3 changes: 3 additions & 0 deletions src/box/optimize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,9 @@ void optimize_other_bins_post_process(
return;
}
OptimizeParameters post_process_parameters = parameters;
// The solutions of the post-process are reported through the algorithm
// formatter.
post_process_parameters.new_solution_callback = [](const packingsolver::box::Output&) { };
// For the objectives which don't value the leftovers, the algorithm
// formatter ends as soon as the best solution is proven optimal, which
// doesn't mean that its leftovers can't be improved: only the time limit
Expand Down
62 changes: 42 additions & 20 deletions src/box/post_process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,48 @@ Solution packingsolver::box::reoptimize_bin_leftovers(
// Build instance.
Instance bin_instance = bin_instance_builder.build();

// Retrieve a solution: the bins of 'solution', the bin at 'bin_pos' being
// replaced by a solution of the sub-instance.
auto retrieve_solution = [&instance, &solution, &bin, bin_pos, &bin_to_orig](
const Solution& bin_solution)
{
Solution new_solution(instance);
for (BinPos pos = 0;
pos < solution.number_of_different_bins();
++pos) {
if (pos == bin_pos) {
new_solution.append_bin(
bin_solution,
0,
bin.copies,
{bin.bin_type_id},
bin_to_orig);
} else {
new_solution.append_bin(solution, pos, solution.bin(pos).copies);
}
}
return new_solution;
};

// Solve instance.
OptimizeParameters bin_parameters = parameters;
bin_parameters.messages_to_stdout = false;
bin_parameters.messages_path = "";
bin_parameters.messages_streams.clear();
bin_parameters.write_json_output = false;
bin_parameters.new_solution_callback = [](const packingsolver::box::Output&) { };
// The solutions found during the re-optimization, reported as solutions
// of the instance.
bin_parameters.new_solution_callback = [&parameters, &instance, &retrieve_solution](
const packingsolver::box::Output& bin_output)
{
const Solution& bin_solution = bin_output.solution_pool.best();
if (!bin_solution.feasible() || !bin_solution.full())
return;
packingsolver::box::Output output(instance);
output.solution_pool.add(retrieve_solution(bin_solution));
output.time = bin_output.time;
parameters.new_solution_callback(output);
};
bin_parameters.verbosity_level = 0;
// The sub-instance has a single bin: its post-process would re-optimize
// this same bin again.
Expand All @@ -71,24 +106,7 @@ Solution packingsolver::box::reoptimize_bin_leftovers(
if (!bin_solution.feasible() || !bin_solution.full())
return solution;

// Retrieve solution: the bins of 'solution', the bin at 'bin_pos' being
// replaced by the re-optimized one.
Solution new_solution(instance);
for (BinPos pos = 0;
pos < solution.number_of_different_bins();
++pos) {
if (pos == bin_pos) {
new_solution.append_bin(
bin_solution,
0,
bin.copies,
{bin.bin_type_id},
bin_to_orig);
} else {
new_solution.append_bin(solution, pos, solution.bin(pos).copies);
}
}
return new_solution;
return retrieve_solution(bin_solution);
}

namespace
Expand All @@ -103,6 +121,10 @@ Solution reoptimize_bins_leftovers(
BinPos number_of_bins,
const OptimizeParameters& parameters)
{
// The solutions found while re-optimizing a bin are not reported: they
// may be worse than the solution with the bins re-optimized before.
OptimizeParameters bin_parameters = parameters;
bin_parameters.new_solution_callback = [](const packingsolver::box::Output&) { };
Solution new_solution = solution;
for (BinPos bin_pos = 0;
bin_pos < number_of_bins;
Expand All @@ -112,7 +134,7 @@ Solution reoptimize_bins_leftovers(
Solution bin_solution = box::reoptimize_bin_leftovers(
new_solution,
bin_pos,
parameters);
bin_parameters);
// Only the bin at 'bin_pos' differs: the comparison is on its leftover
// value, then on its staircase leftover value.
if (!(bin_solution < new_solution)
Expand Down
14 changes: 10 additions & 4 deletions src/box/solution_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,28 @@ void SolutionBuilder::add_item(
void SolutionBuilder::read(
const std::string& certificate_path)
{
const Instance& instance = this->solution_.instance();

std::ifstream file(certificate_path);
if (!file.good()) {
throw std::runtime_error(
FUNC_SIGNATURE + ": "
"unable to open file \"" + certificate_path + "\".");
}

read(file);
}

void SolutionBuilder::read(
std::istream& certificate_stream)
{
const Instance& instance = this->solution_.instance();

std::string tmp;
std::vector<std::string> line;
std::vector<std::string> labels;

getline(file, tmp);
getline(certificate_stream, tmp);
labels = optimizationtools::split(tmp, ',');
while (getline(file, tmp)) {
while (getline(certificate_stream, tmp)) {
line = optimizationtools::split(tmp, ',');

std::string type = "";
Expand Down
7 changes: 6 additions & 1 deletion src/box/solution_builder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include "packingsolver/box/solution.hpp"

#include <istream>

namespace packingsolver
{
namespace box
Expand All @@ -15,9 +17,12 @@ class SolutionBuilder
/** Constructor. */
SolutionBuilder(const Instance& instance): solution_(instance) { }

/** Read a solution from a file. */
/** Read a solution from a certificate file. */
void read(const std::string& certificate_path);

/** Read a solution from a certificate stream. */
void read(std::istream& certificate_stream);

/** Add a bin at the end of the solution. */
BinPos add_bin(
BinTypeId bin_type_id,
Expand Down
3 changes: 3 additions & 0 deletions src/boxstacks/optimize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,9 @@ void optimize_other_bins_post_process(
return;
}
OptimizeParameters post_process_parameters = parameters;
// The solutions of the post-process are reported through the algorithm
// formatter.
post_process_parameters.new_solution_callback = [](const packingsolver::boxstacks::Output&) { };
// For the objectives which don't value the leftovers, the algorithm
// formatter ends as soon as the best solution is proven optimal, which
// doesn't mean that its leftovers can't be improved: only the time limit
Expand Down
62 changes: 42 additions & 20 deletions src/boxstacks/post_process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,48 @@ Solution packingsolver::boxstacks::reoptimize_bin_leftovers(
// Build instance.
Instance bin_instance = bin_instance_builder.build();

// Retrieve a solution: the bins of 'solution', the bin at 'bin_pos' being
// replaced by a solution of the sub-instance.
auto retrieve_solution = [&instance, &solution, &bin, bin_pos, &bin_to_orig](
const Solution& bin_solution)
{
Solution new_solution(instance);
for (BinPos pos = 0;
pos < solution.number_of_different_bins();
++pos) {
if (pos == bin_pos) {
new_solution.append_bin(
bin_solution,
0,
bin.copies,
{bin.bin_type_id},
bin_to_orig);
} else {
new_solution.append_bin(solution, pos, solution.bin(pos).copies);
}
}
return new_solution;
};

// Solve instance.
OptimizeParameters bin_parameters = parameters;
bin_parameters.messages_to_stdout = false;
bin_parameters.messages_path = "";
bin_parameters.messages_streams.clear();
bin_parameters.write_json_output = false;
bin_parameters.new_solution_callback = [](const packingsolver::boxstacks::Output&) { };
// The solutions found during the re-optimization, reported as solutions
// of the instance.
bin_parameters.new_solution_callback = [&parameters, &instance, &retrieve_solution](
const packingsolver::boxstacks::Output& bin_output)
{
const Solution& bin_solution = bin_output.solution_pool.best();
if (!bin_solution.feasible() || !bin_solution.full())
return;
packingsolver::boxstacks::Output output(instance);
output.solution_pool.add(retrieve_solution(bin_solution));
output.time = bin_output.time;
parameters.new_solution_callback(output);
};
bin_parameters.verbosity_level = 0;
// The sub-instance has a single bin: its post-process would re-optimize
// this same bin again.
Expand All @@ -72,24 +107,7 @@ Solution packingsolver::boxstacks::reoptimize_bin_leftovers(
if (!bin_solution.feasible() || !bin_solution.full())
return solution;

// Retrieve solution: the bins of 'solution', the bin at 'bin_pos' being
// replaced by the re-optimized one.
Solution new_solution(instance);
for (BinPos pos = 0;
pos < solution.number_of_different_bins();
++pos) {
if (pos == bin_pos) {
new_solution.append_bin(
bin_solution,
0,
bin.copies,
{bin.bin_type_id},
bin_to_orig);
} else {
new_solution.append_bin(solution, pos, solution.bin(pos).copies);
}
}
return new_solution;
return retrieve_solution(bin_solution);
}

namespace
Expand All @@ -104,6 +122,10 @@ Solution reoptimize_bins_leftovers(
BinPos number_of_bins,
const OptimizeParameters& parameters)
{
// The solutions found while re-optimizing a bin are not reported: they
// may be worse than the solution with the bins re-optimized before.
OptimizeParameters bin_parameters = parameters;
bin_parameters.new_solution_callback = [](const packingsolver::boxstacks::Output&) { };
Solution new_solution = solution;
for (BinPos bin_pos = 0;
bin_pos < number_of_bins;
Expand All @@ -113,7 +135,7 @@ Solution reoptimize_bins_leftovers(
Solution bin_solution = boxstacks::reoptimize_bin_leftovers(
new_solution,
bin_pos,
parameters);
bin_parameters);
// Only the bin at 'bin_pos' differs: the comparison is on its leftover
// value, then on its staircase leftover value.
if (!(bin_solution < new_solution)
Expand Down
10 changes: 8 additions & 2 deletions src/boxstacks/solution_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,19 @@ void SolutionBuilder::read(
"unable to open file \"" + certificate_path + "\".");
}

read(file);
}

void SolutionBuilder::read(
std::istream& certificate_stream)
{
std::string tmp;
std::vector<std::string> line;
std::vector<std::string> labels;

getline(file, tmp);
getline(certificate_stream, tmp);
labels = optimizationtools::split(tmp, ',');
while (getline(file, tmp)) {
while (getline(certificate_stream, tmp)) {
line = optimizationtools::split(tmp, ',');

std::string type = "";
Expand Down
7 changes: 6 additions & 1 deletion src/boxstacks/solution_builder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include "packingsolver/boxstacks/solution.hpp"

#include <istream>

namespace packingsolver
{
namespace boxstacks
Expand All @@ -15,9 +17,12 @@ class SolutionBuilder
/** Constructor. */
SolutionBuilder(const Instance& instance): solution_(instance) { }

/** Read a solution from a file. */
/** Read a solution from a certificate file. */
void read(const std::string& certificate_path);

/** Read a solution from a certificate stream. */
void read(std::istream& certificate_stream);

/** Add a bin at the end of the solution. */
BinPos add_bin(
BinTypeId bin_type_id,
Expand Down
Loading
Loading