revamp non C++ MPSolver export methods

This commit is contained in:
Laurent Perron
2024-10-08 16:03:18 +02:00
parent 6a54d671d7
commit af9051e8d8
5 changed files with 28 additions and 33 deletions

View File

@@ -119,9 +119,9 @@ public class Loader {
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);

View File

@@ -520,15 +520,9 @@ public final class LinearSolverTest {
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 final class LinearSolverTest {
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 final class LinearSolverTest {
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());
}

View File

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

View File

@@ -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);

View File

@@ -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.