Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
mathopt_convert.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// Tool to run use MathOpt to convert MIP/LP models formats.
15//
16// Examples:
17// * Convert a text MPModelProto to a MPS file:
18// mathopt_convert \
19// --input_file model.textproto \
20// --input_format linear_solver_txt \
21// --output_file model.mps
22// * Convert a binary ModelProto to a binary MPModelProto:
23// mathopt_convert \
24// --input_file model.pb \
25// --output_file model_linear_solver.pb \
26// --output_format linear_solver
27// * Convert a binary ModelProto to a LP file:
28// mathopt_convert \
29// --input_file model.pb \
30// --output_file model.lp
31// * Anonymize a binary ModelProto:
32// mathopt_convert \
33// --input_file model.pb \
34// --nonames \
35// --output_file anonymous-model.pb
36#include <optional>
37#include <string>
38
39#include "absl/flags/flag.h"
40#include "absl/status/status.h"
41#include "absl/strings/str_cat.h"
42#include "absl/strings/string_view.h"
49
51 std::string, input_file, "",
52 "the file containing the model to solve; use --input_format to specify"
53 " the file format");
55 std::optional<operations_research::math_opt::FileFormat>, input_format,
56 std::nullopt,
57 absl::StrCat(
58 "the format of the --input_file; possible values:",
60
61ABSL_FLAG(std::string, output_file, "",
62 "the file to write to; use --output_format to specify"
63 " the file format");
65 std::optional<operations_research::math_opt::FileFormat>, output_format,
66 std::nullopt,
67 absl::StrCat(
68 "the format of the --output_file; possible values:",
70
71ABSL_FLAG(bool, names, true,
72 "use the names in the input models; ignoring names is useful when "
73 "the input contains duplicates or if the model must be anonymized");
74
76namespace {
77
78// Returns the format to use for the file, or LOG(QFATAL) an error.
79//
80// Either use the format flag value is available, or guess the format based on
81// the file_path's extension.
82FileFormat ParseOptionalFormatFlag(
83 const absl::string_view format_flag_name,
84 const std::optional<FileFormat> format_flag_value,
85 const absl::string_view file_path_flag_name,
86 const absl::string_view file_path_flag_value) {
87 const std::optional<FileFormat> format =
88 FormatFromFlagOrFilePath(format_flag_value, file_path_flag_value);
89 if (format.has_value()) {
90 return *format;
91 }
92 LOG(QFATAL) << "Can't guess the format from the --" << file_path_flag_name
93 << " extension, please use --" << format_flag_name
94 << " to specify the file format explicitly.";
95}
96
97absl::Status Main() {
98 const std::string input_file_path = absl::GetFlag(FLAGS_input_file);
99 if (input_file_path.empty()) {
100 LOG(QFATAL) << "The flag --input_file is mandatory.";
101 }
102 const std::string output_file_path = absl::GetFlag(FLAGS_output_file);
103 if (output_file_path.empty()) {
104 LOG(QFATAL) << "The flag --output_file is mandatory.";
105 }
106
107 const FileFormat input_format = ParseOptionalFormatFlag(
108 /*format_flag_name=*/"input_format", absl::GetFlag(FLAGS_input_format),
109 /*file_path_flag_name=*/"input_file", input_file_path);
110 const FileFormat output_format = ParseOptionalFormatFlag(
111 /*format_flag_name=*/"output_format", absl::GetFlag(FLAGS_output_format),
112 /*file_path_flag_name=*/"output_file", output_file_path);
113
114 // Read the model.
115 OR_ASSIGN_OR_RETURN3((auto [model_proto, optional_hint]),
116 ReadModel(input_file_path, input_format),
117 _ << "failed to read " << input_file_path);
118
119 if (!absl::GetFlag(FLAGS_names)) {
120 RemoveNames(model_proto);
121 }
122
123 // Write the model.
125 WriteModel(output_file_path, model_proto, optional_hint, output_format))
126 << "failed to write " << output_file_path;
127
128 return absl::OkStatus();
129}
130
131} // namespace
132} // namespace operations_research::math_opt
133
134int main(int argc, char* argv[]) {
135 InitGoogle(argv[0], &argc, &argv, /*remove_flags=*/true);
136
137 const absl::Status status = operations_research::math_opt::Main();
138 // We don't use QCHECK_OK() here since the logged message contains more than
139 // the failing status.
140 if (!status.ok()) {
141 LOG(QFATAL) << status;
142 }
143
144 return 0;
145}
#define RETURN_IF_ERROR(expr)
* FileFormat
absl::Status status
Definition g_gurobi.cc:44
void InitGoogle(const char *usage, int *argc, char ***argv, bool deprecated)
Definition init_google.h:34
input_file model textproto input_format linear_solver_txt output_file model mps input_file model pb output_file model_linear_solver pb output_format linear_solver input_file model pb output_file model lp input_file model pb nonames output_file anonymous model pb ABSL_FLAG(std::string, input_file, "", "the file containing the model to solve; use --input_format to specify" " the file format")
int main(int argc, char *argv[])
An object oriented wrapper for quadratic constraints in ModelStorage.
Definition gurobi_isv.cc:28
std::optional< FileFormat > FormatFromFlagOrFilePath(const std::optional< FileFormat > format_flag_value, const absl::string_view file_path)
std::string OptionalFormatFlagPossibleValuesList()
absl::Status WriteModel(const absl::string_view file_path, const ModelProto &model_proto, const std::optional< SolutionHintProto > &hint_proto, const FileFormat format)
absl::StatusOr< std::pair< ModelProto, std::optional< SolutionHintProto > > > ReadModel(const absl::string_view file_path, const FileFormat format)
void RemoveNames(ModelProto &model)
Removes the model, variables and constraints names of the provided model.
#define OR_ASSIGN_OR_RETURN3(lhs, rexpr, error_expression)