Google OR-Tools v9.11
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-2024 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/strings/match.h"
20#include "absl/strings/string_view.h"
27#include "ortools/packing/vector_bin_packing.pb.h"
29
30ABSL_FLAG(std::string, input, "", "Vector Bin Packing (.vpb) data file name.");
31ABSL_FLAG(std::string, params, "",
32 "Parameters in solver specific text format.");
33ABSL_FLAG(std::string, solver, "sat", "Solver to use: sat, scip");
34ABSL_FLAG(double, time_limit, 900.0, "Time limit in seconds");
35ABSL_FLAG(int, threads, 1, "Number of threads");
36ABSL_FLAG(bool, display_proto, false, "Print the input protobuf");
37ABSL_FLAG(int, max_bins, -1,
38 "Maximum number of bins: default = -1 meaning no limits");
39
40namespace operations_research {
41void ParseAndSolve(const std::string& filename, absl::string_view solver,
42 const std::string& params) {
43 std::string problem_name = filename;
44 const size_t found = problem_name.find_last_of("/\\");
45 if (found != std::string::npos) {
46 problem_name = problem_name.substr(found + 1);
47 }
48 if (absl::EndsWith(problem_name, ".vbp")) {
49 // TODO(user): Move naming code to parser.
50 problem_name.resize(problem_name.size() - 4);
51 }
52
53 packing::vbp::VectorBinPackingProblem data;
54
56 if (!parser.ParseFile(filename)) {
57 LOG(FATAL) << "Cannot read " << filename;
58 }
59 data = parser.problem();
60 data.set_name(problem_name);
61
62 if (data.max_bins() != 0) {
63 LOG(WARNING)
64 << "Ignoring max_bins value. The feasibility problem is not supported.";
65 }
66
67 LOG(INFO) << "Solving vector packing problem '" << data.name() << "' with "
68 << data.item_size() << " item types, and "
69 << data.resource_capacity_size() << " dimensions.";
70 if (absl::GetFlag(FLAGS_display_proto)) {
71 LOG(INFO) << data;
72 }
73
74 // Build optimization model.
76 MPSolver::ParseSolverType(solver, &solver_type);
77 packing::vbp::VectorBinPackingSolution solution =
79 data, solver_type, params, absl::GetFlag(FLAGS_time_limit),
80 absl::GetFlag(FLAGS_threads), absl::GetFlag(FLAGS_max_bins));
81 if (!solution.bins().empty()) {
82 for (int b = 0; b < solution.bins_size(); ++b) {
83 LOG(INFO) << "Bin " << b;
84 const packing::vbp::VectorBinPackingOneBinInSolution& bin =
85 solution.bins(b);
86 for (int i = 0; i < bin.item_indices_size(); ++i) {
87 LOG(INFO) << " - item: " << bin.item_indices(i)
88 << ", copies: " << bin.item_copies(i);
89 }
90 }
91 }
92}
93
94} // namespace operations_research
95
96int main(int argc, char** argv) {
97 absl::SetFlag(&FLAGS_stderrthreshold, 0);
98 InitGoogle(argv[0], &argc, &argv, true);
99 if (absl::GetFlag(FLAGS_input).empty()) {
100 LOG(FATAL) << "Please supply a data file with --input=";
101 }
102
103 operations_research::ParseAndSolve(absl::GetFlag(FLAGS_input),
104 absl::GetFlag(FLAGS_solver),
105 absl::GetFlag(FLAGS_params));
106 return EXIT_SUCCESS;
107}
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)
int64_t b
Definition table.cc:45
void InitGoogle(const char *usage, int *argc, char ***argv, bool deprecated)
Definition init_google.h:34
time_limit
Definition solve.cc:22
double solution
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.
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)