Google OR-Tools v9.14
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
vector_bin_packing_main.cc
Go to the documentation of this file.
1// Copyright 2010-2025 Google LLC
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14#include <cstddef>
15#include <cstdlib>
16#include <string>
17
18#include "absl/flags/flag.h"
19#include "absl/log/globals.h"
20#include "absl/log/log.h"
21#include "absl/strings/match.h"
22#include "absl/strings/string_view.h"
31
32ABSL_FLAG(std::string, input, "", "Vector Bin Packing (.vpb) data file name.");
33ABSL_FLAG(std::string, params, "",
34 "Parameters in solver specific text format.");
35ABSL_FLAG(std::string, solver, "sat", "Solver to use: sat, scip");
36ABSL_FLAG(double, time_limit, 900.0, "Time limit in seconds");
37ABSL_FLAG(int, threads, 1, "Number of threads");
38ABSL_FLAG(bool, display_proto, false, "Print the input protobuf");
39ABSL_FLAG(int, max_bins, -1,
40 "Maximum number of bins: default = -1 meaning no limits");
41
42namespace operations_research {
43void ParseAndSolve(const std::string& filename, absl::string_view solver,
44 const std::string& params) {
45 std::string problem_name = filename;
46 const size_t found = problem_name.find_last_of("/\\");
47 if (found != std::string::npos) {
48 problem_name = problem_name.substr(found + 1);
49 }
50 if (absl::EndsWith(problem_name, ".vbp")) {
51 // TODO(user): Move naming code to parser.
52 problem_name.resize(problem_name.size() - 4);
53 }
54
56
58 if (!parser.ParseFile(filename)) {
59 LOG(FATAL) << "Cannot read " << filename;
60 }
61 data = parser.problem();
62 data.set_name(problem_name);
63
64 if (data.max_bins() != 0) {
65 LOG(WARNING)
66 << "Ignoring max_bins value. The feasibility problem is not supported.";
67 }
68
69 LOG(INFO) << "Solving vector packing problem '" << data.name() << "' with "
70 << data.item_size() << " item types, and "
71 << data.resource_capacity_size() << " dimensions.";
72 if (absl::GetFlag(FLAGS_display_proto)) {
73 LOG(INFO) << data;
74 }
75
76 // Build optimization model.
78 MPSolver::ParseSolverType(solver, &solver_type);
81 data, solver_type, params, absl::GetFlag(FLAGS_time_limit),
82 absl::GetFlag(FLAGS_threads), absl::GetFlag(FLAGS_max_bins));
83 if (!solution.bins().empty()) {
84 for (int b = 0; b < solution.bins_size(); ++b) {
85 LOG(INFO) << "Bin " << b;
87 solution.bins(b);
88 for (int i = 0; i < bin.item_indices_size(); ++i) {
89 LOG(INFO) << " - item: " << bin.item_indices(i)
90 << ", copies: " << bin.item_copies(i);
91 }
92 }
93 }
94}
95
96} // namespace operations_research
97
98int main(int argc, char** argv) {
99 absl::SetStderrThreshold(absl::LogSeverityAtLeast::kInfo);
100 InitGoogle(argv[0], &argc, &argv, true);
101 if (absl::GetFlag(FLAGS_input).empty()) {
102 LOG(FATAL) << "Please supply a data file with --input=";
103 }
104
105 operations_research::ParseAndSolve(absl::GetFlag(FLAGS_input),
106 absl::GetFlag(FLAGS_solver),
107 absl::GetFlag(FLAGS_params));
108 return EXIT_SUCCESS;
109}
static bool ParseSolverType(absl::string_view solver_id, OptimizationProblemType *type)
static
::operations_research::packing::vbp::VectorBinPackingProblem problem() const
We keep the fully qualified name for SWIG.
bool ParseFile(absl::string_view data_filename)
int item_size() const
repeated .operations_research.packing.vbp.Item item = 4;
int resource_capacity_size() const
repeated int64 resource_capacity = 2;
void InitGoogle(absl::string_view usage, int *argc, char ***argv, bool deprecated)
Definition init_google.h:49
time_limit
Definition solve.cc:22
vbp::VectorBinPackingSolution SolveVectorBinPackingWithArcFlow(const vbp::VectorBinPackingProblem &problem, MPSolver::OptimizationProblemType solver_type, const std::string &mip_params, double time_limit, int num_threads, int max_bins)
In SWIG mode, we don't want anything besides these top-level includes.
Select next search node to expand Select next item_i to add this new search node to the search Generate a new search node where item_i is not in the knapsack Check validity of this new partial solution(using propagators) - If valid
void ParseAndSolve(const std::string &filename, absl::string_view solver, const std::string &params)
static int input(yyscan_t yyscanner)
ABSL_FLAG(std::string, input, "", "Vector Bin Packing (.vpb) data file name.")
int main(int argc, char **argv)