Skip to content

Commit d74962d

Browse files
committed
Updated bin_packing_cft.cc example
1 parent a225bba commit d74962d

1 file changed

Lines changed: 28 additions & 4 deletions

File tree

ortools/algorithms/samples/bin_packing_cft.cc

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
1+
2+
// Copyright 2025 Francesco Cavaliere
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
115
#include <absl/log/initialize.h>
216

317
#include <cstdlib>
18+
#include <random>
419

520
#include "ortools/algorithms/bin_packing.h"
621
#include "ortools/base/init_google.h"
@@ -15,11 +30,20 @@ int main(int argc, char** argv) {
1530

1631
BinPackingModel model = ReadBpp(absl::GetFlag(FLAGS_instance));
1732

18-
BinPackingSetCoverModel scp_model =
19-
GenerateBins(model, absl::GetFlag(FLAGS_bins));
33+
// Quick run with a minimal set of bins
34+
BinPackingSetCoverModel scp_model = GenerateInitialBins(model);
35+
scp::PrimalDualState best_result = scp::RunCftHeuristic(scp_model);
36+
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;
44+
}
2045

21-
scp::PrimalDualState result = scp::RunCftHeuristic(scp_model);
22-
auto [solution, dual] = result;
46+
auto [solution, dual] = best_result;
2347
if (solution.subsets().empty()) {
2448
std::cerr << "Error: failed to find any solution\n";
2549
} else {

0 commit comments

Comments
 (0)