From 61696270eeebda7eeb5217f04f2ebe23f1293fd6 Mon Sep 17 00:00:00 2001 From: Laurent Perron Date: Fri, 2 Aug 2024 10:55:25 -0700 Subject: [PATCH] improve WriteMpsToFile --- ortools/linear_solver/BUILD.bazel | 1 + ortools/linear_solver/model_exporter.cc | 20 +++++--------------- ortools/linear_solver/model_exporter.h | 21 +-------------------- 3 files changed, 7 insertions(+), 35 deletions(-) diff --git a/ortools/linear_solver/BUILD.bazel b/ortools/linear_solver/BUILD.bazel index 4d1b63d88e..618e1921a6 100644 --- a/ortools/linear_solver/BUILD.bazel +++ b/ortools/linear_solver/BUILD.bazel @@ -379,6 +379,7 @@ cc_library( "//ortools/base:file", "//ortools/base:hash", "//ortools/base:map_util", + "//ortools/base:status_macros", "//ortools/util:fp_utils", "@com_google_absl//absl/container:flat_hash_set", "@com_google_absl//absl/status", diff --git a/ortools/linear_solver/model_exporter.cc b/ortools/linear_solver/model_exporter.cc index 6a83c82c4c..51d75611d6 100644 --- a/ortools/linear_solver/model_exporter.cc +++ b/ortools/linear_solver/model_exporter.cc @@ -33,9 +33,10 @@ #include "ortools/base/helpers.h" #include "ortools/base/logging.h" #include "ortools/base/options.h" +#include "ortools/base/status_macros.h" #include "ortools/linear_solver/linear_solver.pb.h" -ABSL_FLAG(bool, lp_log_invalid_name, false, "DEPRECATED."); +ABSL_RETIRED_FLAG(bool, lp_log_invalid_name, false, "DEPRECATED."); namespace operations_research { namespace { @@ -250,16 +251,9 @@ absl::StatusOr ExportModelAsMpsFormat( absl::Status WriteModelToMpsFile(absl::string_view filename, const MPModelProto& model, const MPModelExportOptions& options) { - if (model.general_constraint_size() > 0) { - return absl::InvalidArgumentError("General constraints are not supported."); - } - - MPModelProtoExporter exporter(model); - std::string mps_model; - if (!exporter.ExportModelAsMpsFormat(options, &mps_model)) { - return absl::InvalidArgumentError("Unable to export model."); - } - return file::SetContents(filename, mps_model, file::Defaults()); + ASSIGN_OR_RETURN(std::string mps_data, + ExportModelAsMpsFormat(model, options)); + return file::SetContents(filename, mps_data, file::Defaults()); } namespace { @@ -478,10 +472,6 @@ void UpdateMaxSize(double new_number, int* size) { } // namespace void MPModelProtoExporter::Setup() { - if (absl::GetFlag(FLAGS_lp_log_invalid_name)) { - LOG(WARNING) << "The \"lp_log_invalid_name\" flag is deprecated. Use " - "MPModelProtoExportOptions instead."; - } num_binary_variables_ = 0; num_integer_variables_ = 0; for (const MPVariableProto& var : proto_.variable()) { diff --git a/ortools/linear_solver/model_exporter.h b/ortools/linear_solver/model_exporter.h index 6123b0b35e..ddf7b35040 100644 --- a/ortools/linear_solver/model_exporter.h +++ b/ortools/linear_solver/model_exporter.h @@ -101,26 +101,7 @@ absl::StatusOr ExportModelAsMpsFormat( * Write the current model (variables, constraints, objective) to a file in MPS * file format, using the "free" MPS format. * - * Returns false if some error has occurred during execution. Models with - * maximization objectives trigger an error, because MPS can encode only - * minimization problems. - * - * The validity of names is automatically checked. If a variable name or a - * constraint name is invalid or non-existent, a new valid name is - * automatically generated. - * - * Name validity and obfuscation works exactly as in ExportModelAsLpFormat(). - * - * For more information about the MPS format: - * http://en.wikipedia.org/wiki/MPS_(format) - * A close-to-original description coming from OSL: - * http://tinyurl.com/mps-format-by-osl - * A recent description from CPLEX: - * http://tinyurl.com/mps-format-by-cplex - * CPLEX extensions: - * http://tinyurl.com/mps-extensions-by-cplex - * Gurobi's description: - * http://www.gurobi.com/documentation/5.1/reference-manual/node869 + * See ExportModelAsMpsFormat(). */ absl::Status WriteModelToMpsFile( absl::string_view filename, const MPModelProto& model,