Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
model_exporter_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// See kUsageStr below, and ./model_exporter.h.
15
16#include <cstdio>
17#include <string>
18
19#include "absl/log/check.h"
20#include "absl/strings/match.h"
21#include "google/protobuf/descriptor.h"
22#include "google/protobuf/io/zero_copy_stream_impl_lite.h"
23#include "google/protobuf/message.h"
24#include "google/protobuf/text_format.h"
26#include "ortools/base/file.h"
29#include "ortools/linear_solver/linear_solver.pb.h"
32
33ABSL_FLAG(std::string, input, "", "REQUIRED: Input file name");
34ABSL_FLAG(std::string, output, "", "REQUIRED: Output file name");
35ABSL_FLAG(bool, input_is_mp_model_request, false,
36 "Whether the input is a MPModelRequest proto.");
37ABSL_FLAG(bool, obfuscate, false,
38 "Whether variable and constaint names should be obfuscated.");
39
40static const char kUsageStr[] =
41 "Convert an operations_research::MPModelProto or"
42 " operations_research::MPModelRequest file (containing a single"
43 " proto, in ascii or wire format, possibly gzipped)"
44 " into a .lp or .mps file.";
45
46int main(int argc, char** argv) {
47 InitGoogle(kUsageStr, &argc, &argv, /*remove_flags=*/true);
48 std::string input_ascii_pb;
49 CHECK(!absl::GetFlag(FLAGS_input).empty() &&
50 !absl::GetFlag(FLAGS_output).empty())
51 << "--input and --output are required.";
52 operations_research::MPModelProto model_proto;
53 if (absl::GetFlag(FLAGS_input_is_mp_model_request)) {
54 operations_research::MPModelRequest request_proto;
55 CHECK_OK(operations_research::ReadFileToProto(absl::GetFlag(FLAGS_input),
56 &request_proto));
57 model_proto.Swap(request_proto.mutable_model());
58 } else {
59 CHECK_OK(operations_research::ReadFileToProto(absl::GetFlag(FLAGS_input),
60 &model_proto));
61 }
62 std::string output_contents;
64 options.obfuscate = absl::GetFlag(FLAGS_obfuscate);
65 if (absl::EndsWith(absl::GetFlag(FLAGS_output), ".lp")) {
66 output_contents = ExportModelAsLpFormat(model_proto, options).value();
67 } else if (absl::EndsWith(absl::GetFlag(FLAGS_output), ".mps")) {
68 output_contents = ExportModelAsMpsFormat(model_proto, options).value();
69 } else if (absl::EndsWith(absl::GetFlag(FLAGS_output), ".pb.txt")) {
70 google::protobuf::io::StringOutputStream stream(&output_contents);
71 CHECK(google::protobuf::TextFormat::PrintToString(model_proto,
72 &output_contents));
73 } else {
74 LOG(FATAL) << "Unsupported extension: " << absl::GetFlag(FLAGS_output)
75 << " (try: .lp, .mps or .pb.txt)";
76 }
77 CHECK_OK(file::SetContents(absl::GetFlag(FLAGS_output), output_contents,
79 fprintf(stderr, "Wrote '%s' successfully\n",
80 absl::GetFlag(FLAGS_output).c_str());
81}
void InitGoogle(const char *usage, int *argc, char ***argv, bool deprecated)
Definition init_google.h:34
int main(int argc, char **argv)
static const char kUsageStr[]
ABSL_FLAG(std::string, input, "", "REQUIRED: Input file name")
See kUsageStr below, and ./model_exporter.h.
absl::Status SetContents(absl::string_view filename, absl::string_view contents, Options options)
Definition file.cc:242
Options Defaults()
Definition file.h:109
absl::Status ReadFileToProto(absl::string_view filename, google::protobuf::Message *proto, bool allow_partial)
Definition file_util.cc:53
static int input(yyscan_t yyscanner)
bool obfuscate
Obfuscates variable and constraint names.