Skip to content

Commit ac98faf

Browse files
committed
math_opt: add java wrapper
* add ortools/java/com/google/ortools/mathopt/ * add ortools/java/com/google/ortools/mathopt/testing
1 parent e4105ff commit ac98faf

98 files changed

Lines changed: 22433 additions & 11 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

cmake/java.cmake

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,19 @@ file(GLOB_RECURSE proto_java_files RELATIVE ${PROJECT_SOURCE_DIR}
108108
"ortools/sat/sat_parameters.proto"
109109
"ortools/util/*.proto"
110110
)
111-
if(USE_PDLP)
111+
list(REMOVE_ITEM proto_java_files "ortools/constraint_solver/assignment.proto")
112+
list(REMOVE_ITEM proto_java_files "ortools/util/testdata/wrappers_test_message.proto")
113+
if(BUILD_MATH_OPT)
114+
file(GLOB_RECURSE mathopt_proto_java_files RELATIVE ${PROJECT_SOURCE_DIR}
115+
"ortools/math_opt/*.proto"
116+
"ortools/math_opt/solvers/*.proto"
117+
)
118+
list(APPEND proto_java_files ${mathopt_proto_java_files})
119+
endif()
120+
if(USE_PDLP OR BUILD_MATH_OPT)
112121
file(GLOB_RECURSE pdlp_proto_java_files RELATIVE ${PROJECT_SOURCE_DIR} "ortools/pdlp/*.proto")
113122
list(APPEND proto_java_files ${pdlp_proto_java_files})
114123
endif()
115-
list(REMOVE_ITEM proto_java_files "ortools/constraint_solver/assignment.proto")
116-
list(REMOVE_ITEM proto_java_files "ortools/util/testdata/wrappers_test_message.proto")
117124
foreach(PROTO_FILE IN LISTS proto_java_files)
118125
#message(STATUS "protoc proto(java): ${PROTO_FILE}")
119126
get_filename_component(PROTO_DIR ${PROTO_FILE} DIRECTORY)
@@ -273,6 +280,9 @@ foreach(SUBPROJECT IN ITEMS
273280
endforeach()
274281
# from ortools/linear_solver/java
275282
target_link_libraries(jni${JAVA_ARTIFACT} PRIVATE jnimodelbuilder)
283+
add_subdirectory(ortools/math_opt/core/java)
284+
target_link_libraries(jni${JAVA_ARTIFACT} PRIVATE mathopt_java_jni_helper)
285+
target_link_libraries(jni${JAVA_ARTIFACT} PRIVATE jnimathopt)
276286
add_subdirectory(ortools/sat/java)
277287
target_link_libraries(jni${JAVA_ARTIFACT} PRIVATE jnisat)
278288
target_link_libraries(jni${JAVA_ARTIFACT} PRIVATE jni_cp_model_proto)

ortools/java/com/google/ortools/BUILD.bazel

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ cc_binary(
2828
"//ortools/graph/java:graph_cc",
2929
"//ortools/init/java:init_cc",
3030
"//ortools/linear_solver/java:modelbuilder_cc",
31+
"//ortools/math_opt/core/java:solver_swig_cc",
3132
"//ortools/sat/java:cp_model_proto_cc",
3233
"//ortools/sat/java:sat_cc",
3334
"//ortools/util/java:java_swig_solve_interrupter_swig_cc",
@@ -49,6 +50,7 @@ cc_binary(
4950
"//ortools/graph/java:graph_cc",
5051
"//ortools/init/java:init_cc",
5152
"//ortools/linear_solver/java:modelbuilder_cc",
53+
"//ortools/math_opt/core/java:solver_swig_cc",
5254
"//ortools/sat/java:cp_model_proto_cc",
5355
"//ortools/sat/java:sat_cc",
5456
"//ortools/util/java:java_swig_solve_interrupter_swig_cc",
@@ -71,6 +73,7 @@ cc_binary(
7173
"//ortools/graph/java:graph_cc",
7274
"//ortools/init/java:init_cc",
7375
"//ortools/linear_solver/java:modelbuilder_cc",
76+
"//ortools/math_opt/core/java:solver_swig_cc",
7477
"//ortools/sat/java:cp_model_proto_cc",
7578
"//ortools/sat/java:sat_cc",
7679
"//ortools/util/java:java_swig_solve_interrupter_swig_cc",
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright 2010-2025 Google LLC
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
package com.google.ortools.mathopt;
15+
16+
import com.google.common.base.Preconditions;
17+
18+
/**
19+
* Additional objectives for optimization models with multiple hierarchical objectives.
20+
*
21+
* <p>Unlike the primary objective, auxiliary objectives have an id and can be deleted. Users who do
22+
* not need these features (most users) can program against the parent class {@link Objective}
23+
* instead, allowing more uniform treatment of the primary and auxiliary objectives.
24+
*/
25+
public final class AuxiliaryObjective extends Objective implements ModelElement {
26+
private final long id;
27+
private boolean deleted = false;
28+
29+
AuxiliaryObjective(
30+
long priority, String name, long id, OnChangeListener listener, ModelId modelId) {
31+
super(priority, name, listener, modelId);
32+
Preconditions.checkArgument(id >= 0, "id should be nonnegative, was %s", id);
33+
this.id = id;
34+
}
35+
36+
@Override
37+
public long getId() {
38+
return id;
39+
}
40+
41+
@Override
42+
public boolean isDeleted() {
43+
return deleted;
44+
}
45+
46+
@Override
47+
public String toString() {
48+
String label = "Objective " + id;
49+
if (getName().isEmpty()) {
50+
return label;
51+
} else {
52+
return getName() + " (" + label + ")";
53+
}
54+
}
55+
56+
/**
57+
* To be called when this objective is deleted from the model, ensures future modifications result
58+
* in an error. It is an error to call this on the primary objective.
59+
*/
60+
void markDeleted() {
61+
deleted = true;
62+
}
63+
}

0 commit comments

Comments
 (0)