Skip to content

Commit

Permalink
revamp non C++ MPSolver export methods
Browse files Browse the repository at this point in the history
  • Loading branch information
lperron committed Oct 8, 2024
1 parent 12ce2c7 commit 5384913
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 33 deletions.
6 changes: 3 additions & 3 deletions ortools/java/com/google/ortools/Loader.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
16 changes: 5 additions & 11 deletions ortools/linear_solver/java/LinearSolverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -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();
}

Expand Down Expand Up @@ -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());
}

Expand Down
18 changes: 9 additions & 9 deletions ortools/linear_solver/java/linear_solver.i
Original file line number Diff line number Diff line change
Expand Up @@ -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("");
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion ortools/linear_solver/linear_solver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1154,7 +1154,7 @@ void MPSolver::SolveLazyMutableRequest(LazyMutableCopy<MPModelRequest> 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);

Expand Down
19 changes: 10 additions & 9 deletions ortools/linear_solver/python/linear_solver.i
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 5384913

Please sign in to comment.