From 5384913c34f131fc8f52a3bc22be3da444ca3f8f Mon Sep 17 00:00:00 2001 From: Laurent Perron Date: Tue, 8 Oct 2024 16:03:18 +0200 Subject: [PATCH] revamp non C++ MPSolver export methods --- ortools/java/com/google/ortools/Loader.java | 6 +++--- .../linear_solver/java/LinearSolverTest.java | 16 +++++----------- ortools/linear_solver/java/linear_solver.i | 18 +++++++++--------- ortools/linear_solver/linear_solver.cc | 2 +- ortools/linear_solver/python/linear_solver.i | 19 ++++++++++--------- 5 files changed, 28 insertions(+), 33 deletions(-) diff --git a/ortools/java/com/google/ortools/Loader.java b/ortools/java/com/google/ortools/Loader.java index 4c01a9c4c82..0c2dbadd768 100644 --- a/ortools/java/com/google/ortools/Loader.java +++ b/ortools/java/com/google/ortools/Loader.java @@ -119,9 +119,9 @@ public static synchronized void loadNativeLibraries() { Path tempPath = unpackNativeResources(resourceURI); // Load the native library System.load(tempPath.resolve(RESOURCE_PATH) - .resolve(System.mapLibraryName("jniortools")) - .toAbsolutePath() - .toString()); + .resolve(System.mapLibraryName("jniortools")) + .toAbsolutePath() + .toString()); loaded = true; } catch (IOException e) { throw new RuntimeException(e); diff --git a/ortools/linear_solver/java/LinearSolverTest.java b/ortools/linear_solver/java/LinearSolverTest.java index 83c6f826e00..435fe6b8329 100644 --- a/ortools/linear_solver/java/LinearSolverTest.java +++ b/ortools/linear_solver/java/LinearSolverTest.java @@ -520,15 +520,9 @@ public void testModelExport() { final MPConstraint c0 = solver.makeConstraint(-infinity, 100.0); c0.setCoefficient(x1, 5); - final MPModelExportOptions obfuscate = new MPModelExportOptions(); - obfuscate.setObfuscate(true); - String out = solver.exportModelAsLpFormat(); + String out = solver.exportModelAsLpFormat(/* obfuscate= */ true); assertThat(out).isNotEmpty(); - out = solver.exportModelAsLpFormat(obfuscate); - assertThat(out).isNotEmpty(); - out = solver.exportModelAsMpsFormat(); - assertThat(out).isNotEmpty(); - out = solver.exportModelAsMpsFormat(obfuscate); + out = solver.exportModelAsMpsFormat(/* fixed_format= */ true, /* obfuscate= */ true); assertThat(out).isNotEmpty(); } @@ -543,9 +537,9 @@ public void testMPSolver_wrongModelExport() { assertNotNull(solver); // Test that forbidden names are renamed. solver.makeBoolVar("<-%$#!&~-+ ⌂"); // Some illegal name. - String out = solver.exportModelAsLpFormat(); + String out = solver.exportModelAsLpFormat(/* obfuscate= */ false); assertThat(out).isNotEmpty(); - out = solver.exportModelAsMpsFormat(); + out = solver.exportModelAsMpsFormat(/* fixed_format= */ true, /* obfuscate= */ true); assertThat(out).isNotEmpty(); } @@ -611,7 +605,7 @@ public void testMPSolver_issue132() { System.out.println("Number of constraints = " + solver.numConstraints()); solver.enableOutput(); - System.out.println(solver.exportModelAsLpFormat()); + System.out.println(solver.exportModelAsLpFormat(/* obfuscate= */ false)); System.out.println(solver.solve()); } diff --git a/ortools/linear_solver/java/linear_solver.i b/ortools/linear_solver/java/linear_solver.i index 0192b201880..2df3c50df6e 100644 --- a/ortools/linear_solver/java/linear_solver.i +++ b/ortools/linear_solver/java/linear_solver.i @@ -188,9 +188,9 @@ PROTO2_RETURN( /** * Export the loaded model in LP format. */ - std::string exportModelAsLpFormat( - const operations_research::MPModelExportOptions& options = - operations_research::MPModelExportOptions()) { + std::string exportModelAsLpFormat(bool obfuscate) { + operations_research::MPModelExportOptions options; + options.obfuscate = obfuscate; operations_research::MPModelProto model; $self->ExportModelToProto(&model); return ExportModelAsLpFormat(model, options).value_or(""); @@ -199,21 +199,21 @@ PROTO2_RETURN( /** * Export the loaded model in MPS format. */ - std::string exportModelAsMpsFormat( - const operations_research::MPModelExportOptions& options = - operations_research::MPModelExportOptions()) { + std::string exportModelAsMpsFormat(bool fixed_format, bool obfuscate) { + operations_research::MPModelExportOptions options; + options.obfuscate = obfuscate; operations_research::MPModelProto model; $self->ExportModelToProto(&model); return ExportModelAsMpsFormat(model, options).value_or(""); } /** - * Write the model to file in MPS format. + * Write the loaded model to file in MPS format. */ bool writeModelToMpsFile(const std::string& filename, bool fixed_format, - bool obfuscated) { + bool obfuscate) { operations_research::MPModelExportOptions options; - options.obfuscate = obfuscated; + options.obfuscate = obfuscate; operations_research::MPModelProto model; $self->ExportModelToProto(&model); return WriteModelToMpsFile(filename, model, options).ok(); diff --git a/ortools/linear_solver/linear_solver.cc b/ortools/linear_solver/linear_solver.cc index b8dd07a5703..77de096ce99 100644 --- a/ortools/linear_solver/linear_solver.cc +++ b/ortools/linear_solver/linear_solver.cc @@ -1154,7 +1154,7 @@ void MPSolver::SolveLazyMutableRequest(LazyMutableCopy request, // not arbitrary, as we want to maintain any custom thread options set by // the user. They shouldn't matter for polling, but for solving we might // e.g. use a larger stack. - ThreadPool thread_pool("SolverThread", /*num_threads=*/1); + ThreadPool thread_pool(/*num_threads=*/1); thread_pool.StartWorkers(); thread_pool.Schedule(polling_func); diff --git a/ortools/linear_solver/python/linear_solver.i b/ortools/linear_solver/python/linear_solver.i index e1d1c384260..7a5bf5ef839 100644 --- a/ortools/linear_solver/python/linear_solver.i +++ b/ortools/linear_solver/python/linear_solver.i @@ -127,26 +127,26 @@ from ortools.linear_solver.python.linear_solver_natural_api import VariableExpr return status.ok(); } - std::string ExportModelAsLpFormat(bool obfuscated) { + std::string ExportModelAsLpFormat(bool obfuscate) { operations_research::MPModelExportOptions options; - options.obfuscate = obfuscated; + options.obfuscate = obfuscate; operations_research::MPModelProto model; $self->ExportModelToProto(&model); return ExportModelAsLpFormat(model, options).value_or(""); } - std::string ExportModelAsMpsFormat(bool fixed_format, bool obfuscated) { + std::string ExportModelAsMpsFormat(bool fixed_format, bool obfuscate) { operations_research::MPModelExportOptions options; - options.obfuscate = obfuscated; + options.obfuscate = obfuscate; operations_research::MPModelProto model; $self->ExportModelToProto(&model); return ExportModelAsMpsFormat(model, options).value_or(""); } - bool WriteModelToMpsFile(const std::string& filename, bool fixed_format, - bool obfuscated) { + bool WriteModelToMpsFile(const std::string& filename, bool fixed_format, + bool obfuscate) { operations_research::MPModelExportOptions options; - options.obfuscate = obfuscated; + options.obfuscate = obfuscate; operations_research::MPModelProto model; $self->ExportModelToProto(&model); return WriteModelToMpsFile(filename, model, options).ok(); @@ -374,8 +374,9 @@ PY_CONVERT(MPVariable); %rename (LookupVariable) operations_research::MPSolver::LookupVariableOrNull; %unignore operations_research::MPSolver::SetSolverSpecificParametersAsString; %unignore operations_research::MPSolver::NextSolution; -// ExportModelAsLpFormat() is also visible: it's overridden by an %extend, above. -// ExportModelAsMpsFormat() is also visible: it's overridden by an %extend, above. +%unignore operations_research::MPSolver::ExportModelAsLpFormat; +%unignore operations_research::MPSolver::ExportModelAsMpsFormat; +%unignore operations_research::MPSolver::WriteModelToMpsFile; %unignore operations_research::MPSolver::Write; // Expose very advanced parts of the MPSolver API. For expert users only.