From 5b325e293b8bc0e7e05a67de203b70424c5d72fe Mon Sep 17 00:00:00 2001 From: "omer.candan" Date: Wed, 21 Aug 2024 11:56:51 +0300 Subject: [PATCH] add option to compress while exporting models to files --- ortools/linear_solver/linear_solver.cc | 4 +++- ortools/linear_solver/linear_solver.h | 3 ++- ortools/linear_solver/model_exporter.cc | 6 ++++++ ortools/linear_solver/model_exporter.h | 6 ++++++ 4 files changed, 17 insertions(+), 2 deletions(-) diff --git a/ortools/linear_solver/linear_solver.cc b/ortools/linear_solver/linear_solver.cc index 3008656945..768e6a3702 100644 --- a/ortools/linear_solver/linear_solver.cc +++ b/ortools/linear_solver/linear_solver.cc @@ -1870,11 +1870,13 @@ bool MPSolver::ExportModelAsMpsFormat(bool fixed_format, bool obfuscate, bool MPSolver::ExportModelToMpsFile(const std::string& filename, bool fixed_format, - bool obfuscate) const { + bool obfuscate, + bool use_gzip_compression) const { MPModelProto proto; ExportModelToProto(&proto); MPModelExportOptions options; options.obfuscate = obfuscate; + options.use_gzip_compression = use_gzip_compression; const auto status_or = operations_research::WriteModelToMpsFile(filename, proto, options); return status_or.ok(); diff --git a/ortools/linear_solver/linear_solver.h b/ortools/linear_solver/linear_solver.h index 48549e8587..1923815eac 100644 --- a/ortools/linear_solver/linear_solver.h +++ b/ortools/linear_solver/linear_solver.h @@ -671,7 +671,8 @@ class MPSolver { std::string* model_str) const; bool ExportModelToMpsFile(const std::string& filename, bool fixed_format, - bool obfuscate) const; + bool obfuscate, + bool use_gzip_compression) const; /** * Sets the number of threads to use by the underlying solver. * diff --git a/ortools/linear_solver/model_exporter.cc b/ortools/linear_solver/model_exporter.cc index 51d75611d6..53d0e72b0e 100644 --- a/ortools/linear_solver/model_exporter.cc +++ b/ortools/linear_solver/model_exporter.cc @@ -31,6 +31,7 @@ #include "absl/strings/str_format.h" #include "absl/strings/string_view.h" #include "ortools/base/helpers.h" +#include "ortools/base/gzipfile.h" #include "ortools/base/logging.h" #include "ortools/base/options.h" #include "ortools/base/status_macros.h" @@ -253,6 +254,11 @@ absl::Status WriteModelToMpsFile(absl::string_view filename, const MPModelExportOptions& options) { ASSIGN_OR_RETURN(std::string mps_data, ExportModelAsMpsFormat(model, options)); + + if (options.use_gzip_compression) { + return WriteToGzipFile(filename, mps_data); + } + return file::SetContents(filename, mps_data, file::Defaults()); } diff --git a/ortools/linear_solver/model_exporter.h b/ortools/linear_solver/model_exporter.h index ddf7b35040..21ae915c43 100644 --- a/ortools/linear_solver/model_exporter.h +++ b/ortools/linear_solver/model_exporter.h @@ -41,6 +41,12 @@ struct MPModelExportOptions { * was chosen so that SCIP can read the files. */ int max_line_length = 10000; + + /** + * For .mps files only. Decides whether to use gzip compression when exporting + * models to files. + */ + bool use_gzip_compression = false; }; /**