|
13 | 13 | // limitations under the License. |
14 | 14 |
|
15 | 15 | #include <absl/log/initialize.h> |
| 16 | +#include <absl/status/status.h> |
16 | 17 |
|
17 | 18 | #include <cstdlib> |
18 | 19 | #include <random> |
19 | 20 |
|
20 | 21 | #include "ortools/algorithms/bin_packing.h" |
21 | 22 | #include "ortools/base/init_google.h" |
| 23 | +#include "ortools/set_cover/base_types.h" |
22 | 24 | #include "ortools/set_cover/set_cover_cft.h" |
23 | 25 |
|
24 | 26 | using namespace operations_research; |
25 | 27 | ABSL_FLAG(std::string, instance, "", "BPP instance int RAIL format."); |
26 | 28 | ABSL_FLAG(int, bins, 1000, "Number of bins to generate."); |
27 | 29 |
|
| 30 | +template <typename Iterable> |
| 31 | +std::string Stringify(const Iterable& col) { |
| 32 | + std::string result; |
| 33 | + for (auto i : col) { |
| 34 | + absl::StrAppend(&result, " ", i); |
| 35 | + } |
| 36 | + return result; |
| 37 | +} |
| 38 | + |
| 39 | +bool operator==(const SparseColumn& lhs, const std::vector<BaseInt>& rhs) { |
| 40 | + if (lhs.size() != rhs.size()) return false; |
| 41 | + auto lit = lhs.begin(); |
| 42 | + auto rit = rhs.begin(); |
| 43 | + while (lit != lhs.end() && rit != rhs.end()) { |
| 44 | + if (static_cast<BaseInt>(*lit) != *rit) { |
| 45 | + return false; |
| 46 | + } |
| 47 | + ++lit; |
| 48 | + ++rit; |
| 49 | + } |
| 50 | + return true; |
| 51 | +} |
| 52 | + |
| 53 | +void RunTest(const ElementCostVector& weights, const ElementCostVector& profits, |
| 54 | + const std::vector<BaseInt>& expected) { |
| 55 | + ExpKnap knap_solver; |
| 56 | + |
| 57 | + for (ElementIndex i; i < ElementIndex(weights.size()); ++i) { |
| 58 | + std::cout << "Item " << i << " -- profit: " << profits[i] |
| 59 | + << " weight: " << weights[i] |
| 60 | + << " efficiency: " << profits[i] / weights[i] << "\n"; |
| 61 | + } |
| 62 | + |
| 63 | + knap_solver.InitSolver(profits, weights, 6, 100000000); |
| 64 | + knap_solver.Heuristic(); |
| 65 | + std::cout << "Heur solution cost " << knap_solver.best_cost() << " -- " |
| 66 | + << Stringify(knap_solver.collected_bins()[0]) << "\n"; |
| 67 | + |
| 68 | + knap_solver.EleBranch(); |
| 69 | + std::cout << "B&b solution cost " << knap_solver.best_cost() << " -- " |
| 70 | + << Stringify(knap_solver.collected_bins()[0]) << "\n"; |
| 71 | + |
| 72 | + const auto& result = knap_solver.collected_bins()[0]; |
| 73 | + if (!(result == expected)) { |
| 74 | + std::cout << "Error: expected " << Stringify(expected) << " but got " |
| 75 | + << Stringify(result) << "\n"; |
| 76 | + } |
| 77 | + std::cout << std::endl; |
| 78 | +} |
| 79 | + |
| 80 | +void KnapsackTest() { |
| 81 | + std::cout << "Testing knapsack\n"; |
| 82 | + ExpKnap knap_solver; |
| 83 | + ElementCostVector ws = {1, 2, 3, 4, 5}; |
| 84 | + RunTest(ws, {10, 20, 30, 40, 51}, {0, 4}); |
| 85 | + RunTest(ws, {10, 20, 30, 41, 50}, {1, 3}); |
| 86 | + RunTest(ws, {10, 20, 31, 40, 50}, {0, 1, 2}); |
| 87 | + RunTest(ws, {10, 21, 30, 41, 50}, {1, 3}); |
| 88 | + RunTest(ws, {11, 21, 30, 40, 50}, {0, 1, 2}); |
| 89 | + RunTest(ws, {11, 20, 31, 40, 50}, {0, 1, 2}); |
| 90 | + RunTest(ws, {11, 20, 30, 41, 50}, {0, 4}); |
| 91 | + RunTest(ws, {11, 20, 30, 40, 51}, {0, 4}); |
| 92 | + RunTest(ws, {11, 21, 31, 40, 50}, {0, 1, 2}); |
| 93 | + RunTest({4.1, 2, 2, 2}, {8.5, 3, 3, 3}, {1, 2, 3}); |
| 94 | +} |
| 95 | + |
28 | 96 | int main(int argc, char** argv) { |
29 | 97 | InitGoogle(argv[0], &argc, &argv, true); |
30 | 98 |
|
| 99 | + // KnapsackTest(); |
| 100 | + // return 0; |
| 101 | + |
31 | 102 | BinPackingModel model = ReadBpp(absl::GetFlag(FLAGS_instance)); |
32 | 103 |
|
33 | 104 | // Quick run with a minimal set of bins |
34 | 105 | BinPackingSetCoverModel scp_model = GenerateInitialBins(model); |
35 | 106 | scp::PrimalDualState best_result = scp::RunCftHeuristic(scp_model); |
36 | 107 |
|
37 | | - // Run the CFT again with more bins to get a better solution |
38 | | - std::mt19937 rnd(0); |
39 | | - AddRandomizedBins(model, absl::GetFlag(FLAGS_bins), scp_model, rnd); |
40 | | - scp::PrimalDualState result = |
41 | | - scp::RunCftHeuristic(scp_model, best_result.solution); |
42 | | - if (result.solution.cost() < best_result.solution.cost()) { |
43 | | - best_result = result; |
| 108 | + if (absl::GetFlag(FLAGS_bins) > 0) { |
| 109 | + // Run the CFT again with more bins to get a better solution |
| 110 | + std::mt19937 rnd(0); |
| 111 | + AddRandomizedBins(model, absl::GetFlag(FLAGS_bins), scp_model, rnd); |
| 112 | + scp::PrimalDualState result = |
| 113 | + scp::RunCftHeuristic(scp_model, best_result.solution); |
| 114 | + if (result.solution.cost() < best_result.solution.cost()) { |
| 115 | + best_result = result; |
| 116 | + } |
44 | 117 | } |
45 | 118 |
|
46 | 119 | auto [solution, dual] = best_result; |
|
0 commit comments