From 5912937a67753d450b662e6948147d3963550879 Mon Sep 17 00:00:00 2001 From: Laurent Perron Date: Mon, 7 Oct 2024 15:23:51 +0200 Subject: [PATCH] Add WriteModelToMpsFile to MPSolver in Java/Python/.NET --- ortools/linear_solver/csharp/linear_solver.i | 11 +++++++++++ ortools/linear_solver/java/linear_solver.i | 12 ++++++++++++ ortools/linear_solver/python/linear_solver.i | 9 +++++++++ 3 files changed, 32 insertions(+) diff --git a/ortools/linear_solver/csharp/linear_solver.i b/ortools/linear_solver/csharp/linear_solver.i index 23e425b840d..58d621e0107 100644 --- a/ortools/linear_solver/csharp/linear_solver.i +++ b/ortools/linear_solver/csharp/linear_solver.i @@ -166,6 +166,8 @@ 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::WriteModelToMpsFile( + const std::string& filename, bool, bool); %unignore operations_research::MPSolver::SetHint( const std::vector&, const std::vector&); @@ -187,6 +189,15 @@ CONVERT_VECTOR(operations_research::MPVariable, MPVariable) return ExportModelAsMpsFormat(model, options).value_or(""); } + bool WriteModelToMpsFile(const std::string& filename, bool fixed_format, + bool obfuscated) { + operations_research::MPModelExportOptions options; + options.obfuscate = obfuscated; + operations_research::MPModelProto model; + $self->ExportModelToProto(&model); + return WriteModelToMpsFile(filename, model, options).ok(); + } + void SetHint(const std::vector& variables, const std::vector& values) { if (variables.size() != values.size()) { diff --git a/ortools/linear_solver/java/linear_solver.i b/ortools/linear_solver/java/linear_solver.i index 855fa43291d..0192b201880 100644 --- a/ortools/linear_solver/java/linear_solver.i +++ b/ortools/linear_solver/java/linear_solver.i @@ -207,6 +207,18 @@ PROTO2_RETURN( return ExportModelAsMpsFormat(model, options).value_or(""); } + /** + * Write the model to file in MPS format. + */ + bool writeModelToMpsFile(const std::string& filename, bool fixed_format, + bool obfuscated) { + operations_research::MPModelExportOptions options; + options.obfuscate = obfuscated; + operations_research::MPModelProto model; + $self->ExportModelToProto(&model); + return WriteModelToMpsFile(filename, model, options).ok(); + } + /** * Sets a hint for solution. * diff --git a/ortools/linear_solver/python/linear_solver.i b/ortools/linear_solver/python/linear_solver.i index c73d11807c2..e1d1c384260 100644 --- a/ortools/linear_solver/python/linear_solver.i +++ b/ortools/linear_solver/python/linear_solver.i @@ -143,6 +143,15 @@ from ortools.linear_solver.python.linear_solver_natural_api import VariableExpr return ExportModelAsMpsFormat(model, options).value_or(""); } + bool WriteModelToMpsFile(const std::string& filename, bool fixed_format, + bool obfuscated) { + operations_research::MPModelExportOptions options; + options.obfuscate = obfuscated; + operations_research::MPModelProto model; + $self->ExportModelToProto(&model); + return WriteModelToMpsFile(filename, model, options).ok(); + } + /// Set a hint for solution. /// /// If a feasible or almost-feasible solution to the problem is already known,