Skip to content

Commit

Permalink
add option to compress while exporting models to files
Browse files Browse the repository at this point in the history
  • Loading branch information
omer-candan committed Aug 21, 2024
1 parent 8f3e844 commit 5b325e2
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
4 changes: 3 additions & 1 deletion ortools/linear_solver/linear_solver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
3 changes: 2 additions & 1 deletion ortools/linear_solver/linear_solver.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
6 changes: 6 additions & 0 deletions ortools/linear_solver/model_exporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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());
}

Expand Down
6 changes: 6 additions & 0 deletions ortools/linear_solver/model_exporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

/**
Expand Down

0 comments on commit 5b325e2

Please sign in to comment.