22#include "absl/algorithm/container.h"
23#include "absl/container/flat_hash_map.h"
24#include "absl/status/status.h"
25#include "absl/status/statusor.h"
26#include "absl/strings/match.h"
27#include "absl/strings/str_cat.h"
28#include "absl/strings/str_join.h"
29#include "absl/strings/string_view.h"
30#include "absl/types/span.h"
35#include "ortools/linear_solver/linear_solver.pb.h"
40#include "ortools/math_opt/model.pb.h"
41#include "ortools/math_opt/model_parameters.pb.h"
47 FileFormat::kMathOptBinary,
48 FileFormat::kMathOptText,
49 FileFormat::kLinearSolverBinary,
50 FileFormat::kLinearSolverText,
54 return absl::MakeConstSpan(kValues);
59 case FileFormat::kMathOptBinary:
61 case FileFormat::kMathOptText:
63 case FileFormat::kLinearSolverBinary:
64 return "linear_solver";
65 case FileFormat::kLinearSolverText:
66 return "linear_solver_txt";
67 case FileFormat::kMPS:
80 std::string*
const error) {
88 absl::StrAppend(error,
"not a known value");
94 {
".pb", FileFormat::kMathOptBinary},
95 {
".proto", FileFormat::kMathOptBinary},
96 {
".pb.txt", FileFormat::kMathOptText},
97 {
".pbtxt", FileFormat::kMathOptText},
98 {
".textproto", FileFormat::kMathOptText},
99 {
".mps", FileFormat::kMPS},
100 {
".mps.gz", FileFormat::kMPS},
101 {
".lp", FileFormat::kLP},
106 const absl::flat_hash_map<absl::string_view, FileFormat> extensions =
111 using ExtensionPair = std::pair<absl::string_view, FileFormat>;
112 std::vector<ExtensionPair> sorted_extensions(extensions.begin(),
114 absl::c_sort(sorted_extensions,
115 [](
const ExtensionPair& lhs,
const ExtensionPair& rhs) {
119 for (
const auto& [extension, format] : sorted_extensions) {
120 if (absl::EndsWith(file_path, extension)) {
128 const std::optional<FileFormat> format_flag_value,
129 const absl::string_view file_path) {
130 if (format_flag_value.has_value()) {
131 return *format_flag_value;
138constexpr absl::string_view kListLinePrefix =
"* ";
139constexpr absl::string_view kSubListLinePrefix =
" - ";
148 absl::StrAppend(&list,
"\n", kListLinePrefix,
"<unset>",
149 ": to guess the format from the file extension:");
152 absl::flat_hash_map<FileFormat, std::vector<absl::string_view>>
155 format_extensions[format].push_back(extension);
157 for (
auto& [format, extensions] : format_extensions) {
158 absl::c_sort(extensions);
166 const std::vector<absl::string_view>& extensions =
167 format_extensions[format];
168 if (extensions.empty()) {
172 absl::StrAppend(&list,
"\n", kSubListLinePrefix,
173 absl::StrJoin(extensions,
", "),
": ",
180 const absl::flat_hash_map<FileFormat, absl::string_view> format_help = {
181 {FileFormat::kMathOptBinary,
"for a MathOpt ModelProto in binary"},
182 {FileFormat::kMathOptText,
"when the proto is in text"},
183 {FileFormat::kLinearSolverBinary,
184 "for a LinearSolver MPModelProto in binary"},
185 {FileFormat::kLinearSolverText,
"when the proto is in text"},
186 {FileFormat::kMPS,
"for MPS file (which can be GZiped)"},
187 {FileFormat::kLP,
" for LP file"},
191 absl::StrAppend(&list,
"\n", kListLinePrefix,
AbslUnparseFlag(format),
": ",
192 format_help.at(format));
197absl::StatusOr<std::pair<ModelProto, std::optional<SolutionHintProto>>>
200 case FileFormat::kMathOptBinary: {
203 return std::make_pair(std::move(
model), std::nullopt);
205 case FileFormat::kMathOptText: {
208 return std::make_pair(std::move(
model), std::nullopt);
210 case FileFormat::kLinearSolverBinary:
211 case FileFormat::kLinearSolverText: {
213 const MPModelProto linear_solver_model,
214 format == FileFormat::kLinearSolverBinary
220 std::optional<SolutionHintProto>
hint,
222 return std::make_pair(std::move(
model), std::move(
hint));
224 case FileFormat::kMPS: {
226 return std::make_pair(std::move(
model), std::nullopt);
228 case FileFormat::kLP: {
232 return std::make_pair(std::move(
model), std::nullopt);
238 const ModelProto& model_proto,
239 const std::optional<SolutionHintProto>& hint_proto,
242 case FileFormat::kMathOptBinary:
244 case FileFormat::kMathOptText:
246 case FileFormat::kLinearSolverBinary:
247 case FileFormat::kLinearSolverText: {
250 if (hint_proto.has_value()) {
251 LOG(WARNING) <<
"support for converting a MathOpt hint to MPModelProto "
252 "is not yet supported thus the hint has been lost";
254 return format == FileFormat::kLinearSolverBinary
260 case FileFormat::kMPS: {
265 case FileFormat::kLP: {
#define ASSIGN_OR_RETURN(lhs, rexpr)
std::optional< ModelSolveParameters::SolutionHint > hint
absl::Status SetTextProto(absl::string_view filename, const google::protobuf::Message &proto, Options options)
absl::StatusOr< std::string > GetContents(absl::string_view path, Options options)
absl::Status SetBinaryProto(absl::string_view filename, const google::protobuf::Message &proto, Options options)
absl::Status SetContents(absl::string_view filename, absl::string_view contents, Options options)
absl::Status GetTextProto(absl::string_view filename, google::protobuf::Message *proto, Options options)
absl::Status GetBinaryProto(const absl::string_view filename, google::protobuf::Message *proto, Options options)
An object oriented wrapper for quadratic constraints in ModelStorage.
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()
std::ostream & operator<<(std::ostream &ostr, const IndicatorConstraint &constraint)
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)
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()