Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Model file export #4305

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
48 changes: 48 additions & 0 deletions ortools/base/gzipfile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,51 @@ File* GZipFileReader(const absl::string_view name, File* file,
LOG(INFO) << "not implemented";
return nullptr;
}

absl::Status WriteToGzipFile(const absl::string_view filename,
const absl::string_view contents) {
// 1 for fastest compression.
gzFile gz_file = gzopen(filename.data(), "w1");
if (gz_file == Z_NULL) {
return {absl::StatusCode::kInternal, "Unable to open file"};
}

// have to write in chunks, otherwise fails to compress big files.
constexpr size_t chunk = 1024 * 1024; // 1 MB chunk size.
size_t remaining = contents.size();
const char* chunk_start_point = contents.data();

while (remaining > 0) {
size_t current_chunk = std::min(chunk, remaining);
size_t written = gzwrite(gz_file, chunk_start_point, current_chunk);
if (written != current_chunk) {
int err_no = 0;
absl::string_view error_message = gzerror(gz_file, &err_no);
gzclose(gz_file);
return {
absl::StatusCode::kInternal,
absl::StrCat(
"Error while writing chunk to compressed file:",
error_message
)
};
}

chunk_start_point += current_chunk;
remaining -= current_chunk;
}

if (const int status = gzclose(gz_file); status != Z_OK) {
int err_no;
absl::string_view error_message = gzerror(gz_file, &err_no);
return {
absl::StatusCode::kInternal,
absl::StrCat(
"Error while writing chunk to compressed file:",
error_message
)
};
}

return absl::OkStatus();
}
6 changes: 6 additions & 0 deletions ortools/base/gzipfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#ifndef OR_TOOLS_BASE_GZIPFILE_H_
#define OR_TOOLS_BASE_GZIPFILE_H_

#include "absl/status/status.h"
#include "absl/strings/string_view.h"
#include "ortools/base/basictypes.h" // for Ownership enum
#include "zlib.h" // for Z_DEFAULT_COMPRESSION
Expand Down Expand Up @@ -65,4 +66,9 @@ inline File* GZipFileReader(absl::string_view name, File* compressed_file,
AppendedStreams::kConcatenateStreams);
}

// Write contents into a gzip compressed file with the lowest compression level
// which is the fastest.
absl::Status WriteToGzipFile(absl::string_view filename,
absl::string_view contents);

#endif // OR_TOOLS_BASE_GZIPFILE_H_
1 change: 1 addition & 0 deletions ortools/linear_solver/csharp/linear_solver.i
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ CONVERT_VECTOR(operations_research::MPVariable, MPVariable)
// Extend code.
%unignore operations_research::MPSolver::ExportModelAsLpFormat(bool);
%unignore operations_research::MPSolver::ExportModelAsMpsFormat(bool, bool);
%unignore operations_research::MPSolver::ExportModelToMpsFile;
%unignore operations_research::MPSolver::SetHint(
const std::vector<operations_research::MPVariable*>&,
const std::vector<double>&);
Expand Down
14 changes: 14 additions & 0 deletions ortools/linear_solver/linear_solver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1868,6 +1868,20 @@ bool MPSolver::ExportModelAsMpsFormat(bool fixed_format, bool obfuscate,
return status_or.ok();
}

bool MPSolver::ExportModelToMpsFile(const std::string& filename,
bool fixed_format,
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();
}

void MPSolver::SetHint(std::vector<std::pair<const MPVariable*, double>> hint) {
for (const auto& var_value_pair : hint) {
CHECK(OwnsVariable(var_value_pair.first))
Expand Down
5 changes: 4 additions & 1 deletion ortools/linear_solver/linear_solver.h
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,10 @@ class MPSolver {
bool ExportModelAsLpFormat(bool obfuscate, std::string* model_str) const;
bool ExportModelAsMpsFormat(bool fixed_format, bool obfuscate,
std::string* model_str) const;

bool ExportModelToMpsFile(const std::string& filename,
bool fixed_format,
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