22#include "absl/algorithm/container.h"
23#include "absl/base/optimization.h"
24#include "absl/container/flat_hash_map.h"
25#include "absl/log/log.h"
26#include "absl/status/status.h"
27#include "absl/status/statusor.h"
28#include "absl/strings/match.h"
29#include "absl/strings/str_cat.h"
30#include "absl/strings/str_join.h"
31#include "absl/strings/string_view.h"
32#include "absl/types/span.h"
48 FileFormat::kMathOptBinary,
49 FileFormat::kMathOptText,
50 FileFormat::kLinearSolverBinary,
51 FileFormat::kLinearSolverText,
55 return absl::MakeConstSpan(kValues);
60 case FileFormat::kMathOptBinary:
62 case FileFormat::kMathOptText:
64 case FileFormat::kLinearSolverBinary:
65 return "linear_solver";
66 case FileFormat::kLinearSolverText:
67 return "linear_solver_txt";
68 case FileFormat::kMPS:
82 std::string*
const error) {
90 absl::StrAppend(error,
"not a known value");
96 {
".pb", FileFormat::kMathOptBinary},
97 {
".proto", FileFormat::kMathOptBinary},
98 {
".pb.txt", FileFormat::kMathOptText},
99 {
".pbtxt", FileFormat::kMathOptText},
100 {
".textproto", FileFormat::kMathOptText},
101 {
".mps", FileFormat::kMPS},
102 {
".mps.gz", FileFormat::kMPS},
103 {
".lp", FileFormat::kLP},
108 const absl::flat_hash_map<absl::string_view, FileFormat> extensions =
113 using ExtensionPair = std::pair<absl::string_view, FileFormat>;
114 std::vector<ExtensionPair> sorted_extensions(extensions.begin(),
116 absl::c_sort(sorted_extensions,
117 [](
const ExtensionPair& lhs,
const ExtensionPair& rhs) {
121 for (
const auto& [extension, format] : sorted_extensions) {
122 if (absl::EndsWith(file_path, extension)) {
130 const std::optional<FileFormat> format_flag_value,
131 const absl::string_view file_path) {
132 if (format_flag_value.has_value()) {
133 return *format_flag_value;
140constexpr absl::string_view kListLinePrefix =
"* ";
141constexpr absl::string_view kSubListLinePrefix =
" - ";
150 absl::StrAppend(&list,
"\n", kListLinePrefix,
"<unset>",
151 ": to guess the format from the file extension:");
154 absl::flat_hash_map<FileFormat, std::vector<absl::string_view>>
157 format_extensions[format].push_back(extension);
159 for (
auto& [format, extensions] : format_extensions) {
160 absl::c_sort(extensions);
168 const std::vector<absl::string_view>& extensions =
169 format_extensions[format];
170 if (extensions.empty()) {
174 absl::StrAppend(&list,
"\n", kSubListLinePrefix,
175 absl::StrJoin(extensions,
", "),
": ",
182 const absl::flat_hash_map<FileFormat, absl::string_view> format_help = {
183 {FileFormat::kMathOptBinary,
"for a MathOpt ModelProto in binary"},
184 {FileFormat::kMathOptText,
"when the proto is in text"},
185 {FileFormat::kLinearSolverBinary,
186 "for a LinearSolver MPModelProto in binary"},
187 {FileFormat::kLinearSolverText,
"when the proto is in text"},
188 {FileFormat::kMPS,
"for MPS file (which can be GZiped)"},
189 {FileFormat::kLP,
" for LP file"},
193 absl::StrAppend(&list,
"\n", kListLinePrefix,
AbslUnparseFlag(format),
": ",
194 format_help.at(format));
199absl::StatusOr<std::pair<ModelProto, std::optional<SolutionHintProto>>>
202 case FileFormat::kMathOptBinary: {
205 return std::make_pair(std::move(model), std::nullopt);
207 case FileFormat::kMathOptText: {
210 return std::make_pair(std::move(model), std::nullopt);
212 case FileFormat::kLinearSolverBinary:
213 case FileFormat::kLinearSolverText: {
216 format == FileFormat::kLinearSolverBinary
222 std::optional<SolutionHintProto> hint,
224 return std::make_pair(std::move(model), std::move(hint));
226 case FileFormat::kMPS: {
228 return std::make_pair(std::move(model), std::nullopt);
230 case FileFormat::kLP: {
234 return std::make_pair(std::move(model), std::nullopt);
242 const std::optional<SolutionHintProto>& hint_proto,
245 case FileFormat::kMathOptBinary:
247 case FileFormat::kMathOptText:
249 case FileFormat::kLinearSolverBinary:
250 case FileFormat::kLinearSolverText: {
253 if (hint_proto.has_value()) {
254 LOG(WARNING) <<
"support for converting a MathOpt hint to MPModelProto "
255 "is not yet supported thus the hint has been lost";
257 return format == FileFormat::kLinearSolverBinary
263 case FileFormat::kMPS: {
268 case FileFormat::kLP: {
#define ASSIGN_OR_RETURN(lhs, rexpr)
absl::StatusOr< std::string > GetContents(absl::string_view path, Options options)
absl::Status SetBinaryProto(absl::string_view file_name, const google::protobuf::Message &proto, Options options)
absl::Status SetTextProto(absl::string_view file_name, const google::protobuf::Message &proto, Options options)
absl::Status GetTextProto(absl::string_view file_name, google::protobuf::Message *proto, Options options)
absl::Status GetBinaryProto(const absl::string_view file_name, google::protobuf::Message *proto, Options options)
absl::Status SetContents(absl::string_view file_name, absl::string_view contents, Options options)
std::optional< FileFormat > FormatFromFlagOrFilePath(const std::optional< FileFormat > format_flag_value, const absl::string_view file_path)
absl::StatusOr< std::string > ModelProtoToLp(const ModelProto &model)
bool AbslParseFlag(const absl::string_view text, SolverType *const value, std::string *const error)
absl::StatusOr< std::optional< SolutionHintProto > > MPModelProtoSolutionHintToMathOptHint(const MPModelProto &model)
absl::flat_hash_map< absl::string_view, FileFormat > ExtensionToFileFormat()
absl::StatusOr<::operations_research::MPModelProto > MathOptModelToMPModelProto(const ::operations_research::math_opt::ModelProto &model)
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< ModelProto > ReadMpsFile(const absl::string_view filename)
absl::StatusOr< std::string > ModelProtoToMps(const ModelProto &model)
std::ostream & operator<<(std::ostream &ostr, const SecondOrderConeConstraint &constraint)
absl::StatusOr< std::pair< ModelProto, std::optional< SolutionHintProto > > > ReadModel(const absl::string_view file_path, const FileFormat format)
absl::StatusOr<::operations_research::math_opt::ModelProto > MPModelProtoToMathOptModel(const ::operations_research::MPModelProto &model)
absl::StatusOr< ModelProto > ModelProtoFromLp(const absl::string_view lp_data)
std::optional< FileFormat > FormatFromFilePath(absl::string_view file_path)
std::string AbslUnparseFlag(const SolverType value)
absl::Span< const FileFormat > AllFileFormats()
std::string FormatFlagPossibleValuesList()