Skip to content

Commit 7cb1c36

Browse files
committed
Added small test to bin_packing example
1 parent 5529e2a commit 7cb1c36

1 file changed

Lines changed: 80 additions & 7 deletions

File tree

ortools/algorithms/samples/bin_packing_cft.cc

Lines changed: 80 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,107 @@
1313
// limitations under the License.
1414

1515
#include <absl/log/initialize.h>
16+
#include <absl/status/status.h>
1617

1718
#include <cstdlib>
1819
#include <random>
1920

2021
#include "ortools/algorithms/bin_packing.h"
2122
#include "ortools/base/init_google.h"
23+
#include "ortools/set_cover/base_types.h"
2224
#include "ortools/set_cover/set_cover_cft.h"
2325

2426
using namespace operations_research;
2527
ABSL_FLAG(std::string, instance, "", "BPP instance int RAIL format.");
2628
ABSL_FLAG(int, bins, 1000, "Number of bins to generate.");
2729

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+
2896
int main(int argc, char** argv) {
2997
InitGoogle(argv[0], &argc, &argv, true);
3098

99+
// KnapsackTest();
100+
// return 0;
101+
31102
BinPackingModel model = ReadBpp(absl::GetFlag(FLAGS_instance));
32103

33104
// Quick run with a minimal set of bins
34105
BinPackingSetCoverModel scp_model = GenerateInitialBins(model);
35106
scp::PrimalDualState best_result = scp::RunCftHeuristic(scp_model);
36107

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+
}
44117
}
45118

46119
auto [solution, dual] = best_result;

0 commit comments

Comments
 (0)