Skip to content

Commit 2f48d12

Browse files
committed
simplify routing pybind11 wrapping
1 parent bf2ea23 commit 2f48d12

39 files changed

+362
-393
lines changed

ortools/routing/docs/ROUTING.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ int main(int argc, char* argv[]) {
103103
"""Vehicle Routing example."""
104104
105105
106+
from ortools.routing import enums_pb2
106107
from ortools.routing.python import routing
107108
108109
@@ -137,7 +138,7 @@ def main() -> None:
137138
# Setting first solution heuristic.
138139
search_parameters = routing.default_routing_search_parameters()
139140
search_parameters.first_solution_strategy = (
140-
routing.FirstSolutionStrategy.PATH_CHEAPEST_ARC
141+
enums_pb2.FirstSolutionStrategy.PATH_CHEAPEST_ARC
141142
) # pylint: disable=no-member
142143
143144
# Solve the problem.

ortools/routing/python/BUILD.bazel

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
load("@pip_deps//:requirements.bzl", "requirement")
1717
load("@pybind11_bazel//:build_defs.bzl", "pybind_extension")
18-
load("@rules_cc//cc:cc_binary.bzl", "cc_binary")
1918
load("@rules_cc//cc:cc_library.bzl", "cc_library")
2019
load("@rules_python//python:py_test.bzl", "py_test")
2120

@@ -28,45 +27,12 @@ cc_library(
2827
],
2928
)
3029

31-
cc_binary(
32-
name = "gen_proto_builder_pybind11",
33-
srcs = ["gen_proto_builder_pybind11.cc"],
34-
deps = [
35-
"//ortools/base",
36-
"//ortools/constraint_solver:search_stats_cc_proto",
37-
"//ortools/constraint_solver:solver_parameters_cc_proto",
38-
"//ortools/routing:enums_cc_proto",
39-
"//ortools/routing:heuristic_parameters_cc_proto",
40-
"//ortools/routing:ils_cc_proto",
41-
"//ortools/routing:parameters_cc_proto",
42-
"//ortools/util/python:wrappers",
43-
"@abseil-cpp//absl/flags:parse",
44-
"@abseil-cpp//absl/flags:usage",
45-
"@abseil-cpp//absl/log:die_if_null",
46-
"@abseil-cpp//absl/log:initialize",
47-
"@abseil-cpp//absl/strings:str_format",
48-
],
49-
)
50-
51-
genrule(
52-
name = "run_gen_proto_builder_pybind11",
53-
outs = ["proto_builder_pybind11.h"],
54-
cmd = "$(location :gen_proto_builder_pybind11) > $@",
55-
tools = [":gen_proto_builder_pybind11"],
56-
)
57-
58-
cc_library(
59-
name = "proto_builder_pybind11",
60-
hdrs = ["proto_builder_pybind11.h"],
61-
)
62-
6330
pybind_extension(
6431
name = "routing",
6532
srcs = ["routing.cc"],
6633
visibility = ["//visibility:public"],
6734
deps = [
6835
":doc",
69-
":proto_builder_pybind11",
7036
"//ortools/constraint_solver:cp",
7137
"//ortools/constraint_solver:search_stats_cc_proto",
7238
"//ortools/constraint_solver:solver_parameters_cc_proto",
@@ -87,6 +53,7 @@ pybind_extension(
8753
"@abseil-cpp//absl/container:flat_hash_set",
8854
"@protobuf//:duration_cc_proto",
8955
"@pybind11_abseil//pybind11_abseil:absl_casters",
56+
"@pybind11_protobuf//pybind11_protobuf:native_proto_caster",
9057
],
9158
)
9259

@@ -98,6 +65,7 @@ py_test(
9865
":routing",
9966
requirement("absl-py"),
10067
"//ortools/constraint_solver/python:constraint_solver",
68+
"//ortools/constraint_solver:search_stats_py_pb2",
10169
"//ortools/routing:enums_py_pb2",
10270
"//ortools/routing:parameters_py_pb2",
10371
],
@@ -111,6 +79,7 @@ py_test(
11179
":routing",
11280
requirement("absl-py"),
11381
"//ortools/routing:enums_py_pb2",
82+
"//ortools/routing:parameters_py_pb2", # keep
11483
],
11584
)
11685

@@ -132,6 +101,7 @@ py_test(
132101
":routing",
133102
requirement("absl-py"),
134103
"//ortools/routing:enums_py_pb2",
104+
"//ortools/routing:parameters_py_pb2", # keep
135105
"//ortools/util:optional_boolean_py_pb2",
136106
"//ortools/util/python:sorted_interval_list",
137107
],

ortools/routing/python/CMakeLists.txt

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -11,44 +11,8 @@
1111
# See the License for the specific language governing permissions and
1212
# limitations under the License.
1313

14-
# routing_gen_proto_builder_pybind11 code generator.
15-
add_executable(routing_gen_proto_builder_pybind11)
16-
target_sources(routing_gen_proto_builder_pybind11 PRIVATE "gen_proto_builder_pybind11.cc")
17-
target_include_directories(routing_gen_proto_builder_pybind11 PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
18-
target_compile_features(routing_gen_proto_builder_pybind11 PRIVATE cxx_std_17)
19-
target_link_libraries(routing_gen_proto_builder_pybind11 PRIVATE
20-
absl::flags_commandlineflag
21-
absl::flags_parse
22-
absl::flags_usage
23-
absl::die_if_null
24-
absl::str_format
25-
protobuf::libprotobuf
26-
${PROJECT_NAMESPACE}::ortools_proto
27-
${PROJECT_NAMESPACE}::routing_proto
28-
${PROJECT_NAMESPACE}::util_python_wrappers)
29-
30-
include(GNUInstallDirs)
31-
if(APPLE)
32-
set_target_properties(routing_gen_proto_builder_pybind11 PROPERTIES INSTALL_RPATH
33-
"@loader_path/../${CMAKE_INSTALL_LIBDIR};@loader_path")
34-
elseif(UNIX)
35-
cmake_path(RELATIVE_PATH CMAKE_INSTALL_FULL_LIBDIR
36-
BASE_DIRECTORY ${CMAKE_INSTALL_FULL_BINDIR}
37-
OUTPUT_VARIABLE libdir_relative_path)
38-
set_target_properties(routing_gen_proto_builder_pybind11 PROPERTIES
39-
INSTALL_RPATH "$ORIGIN/${libdir_relative_path}")
40-
endif()
41-
42-
install(TARGETS routing_gen_proto_builder_pybind11)
43-
44-
add_custom_command(
45-
OUTPUT proto_builder_pybind11.h
46-
COMMAND routing_gen_proto_builder_pybind11 > proto_builder_pybind11.h
47-
COMMENT "Generate C++ proto_builder_pybind11.h"
48-
VERBATIM)
49-
5014
# routing
51-
pybind11_add_module(routing_pybind11 MODULE routing.cc proto_builder_pybind11.h)
15+
pybind11_add_module(routing_pybind11 MODULE routing.cc)
5216
set_target_properties(routing_pybind11 PROPERTIES
5317
LIBRARY_OUTPUT_NAME "routing")
5418

@@ -65,6 +29,7 @@ endif()
6529
target_link_libraries(routing_pybind11 PRIVATE
6630
${PROJECT_NAMESPACE}::ortools
6731
pybind11_abseil::absl_casters
32+
pybind11_native_proto_caster
6833
)
6934
add_library(${PROJECT_NAMESPACE}::routing_pybind11 ALIAS routing_pybind11)
7035

ortools/routing/python/dimension_test.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@
1818

1919
from absl.testing import absltest
2020

21+
from ortools.routing import enums_pb2
2122
from ortools.routing.python import routing
2223

23-
FirstSolutionStrategy = routing.FirstSolutionStrategy
24-
RoutingSearchStatus = routing.RoutingSearchStatus
24+
FirstSolutionStrategy = enums_pb2.FirstSolutionStrategy
25+
RoutingSearchStatus = enums_pb2.RoutingSearchStatus
2526

2627

2728
def distance(node_i: int, node_j: int) -> int:

ortools/routing/python/gen_proto_builder_pybind11.cc

Lines changed: 0 additions & 63 deletions
This file was deleted.

ortools/routing/python/pywraprouting_test.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
from absl.testing import absltest
2020
from ortools.constraint_solver import pywrapcp
2121
from ortools.routing import enums_pb2
22-
from ortools.routing import parameters_pb2
2322
from ortools.routing import pywraprouting
2423
from ortools.util import optional_boolean_pb2
2524

ortools/routing/python/routing.cc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#include "pybind11/pybind11.h"
4646
#include "pybind11/stl.h"
4747
#include "pybind11_abseil/absl_casters.h"
48+
#include "pybind11_protobuf/native_proto_caster.h"
4849

4950
namespace py = ::pybind11;
5051

@@ -59,14 +60,12 @@ using ::operations_research::routing::RoutingModelParameters;
5960
using ::operations_research::routing::RoutingSearchParameters;
6061

6162
PYBIND11_MODULE(routing, m) {
63+
pybind11_protobuf::ImportNativeProtoCasters();
64+
6265
pybind11::module::import(
6366
"ortools.constraint_solver.python.constraint_solver");
6467
pybind11::module::import("ortools.util.python.sorted_interval_list");
6568

66-
#define IMPORT_PROTO_WRAPPER_CODE
67-
#include "ortools/routing/python/proto_builder_pybind11.h"
68-
#undef IMPORT_PROTO_WRAPPER_CODE
69-
7069
m.def("default_routing_model_parameters", &DefaultRoutingModelParameters,
7170
DOC(operations_research, routing, DefaultRoutingModelParameters));
7271

ortools/routing/python/routing_test.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,16 @@
1717
import functools
1818

1919
from absl.testing import absltest
20+
from google.protobuf import text_format
2021

2122
from ortools.constraint_solver.python import constraint_solver
23+
from ortools.routing import enums_pb2
24+
from ortools.routing import parameters_pb2
2225
from ortools.routing.python import routing
2326

24-
FirstSolutionStrategy = routing.FirstSolutionStrategy
25-
RoutingSearchStatus = routing.RoutingSearchStatus
26-
RoutingSearchParameters = routing.RoutingSearchParameters
27+
FirstSolutionStrategy = enums_pb2.FirstSolutionStrategy
28+
RoutingSearchStatus = enums_pb2.RoutingSearchStatus
29+
RoutingSearchParameters = parameters_pb2.RoutingSearchParameters
2730

2831

2932
def distance(node_i: int, node_j: int) -> int:
@@ -354,9 +357,10 @@ def test_disjunction_penalty_tsp(self) -> None:
354357
def test_routing_model_parameters(self) -> None:
355358
# Create routing model with parameters
356359
parameters = routing.default_routing_model_parameters()
357-
parameters.solver_parameters.parse_text_format(
358-
str(constraint_solver.Solver.default_solver_parameters())
359-
)
360+
# text_format.Parse(
361+
# str(constraint_solver.Solver.default_solver_parameters()),
362+
# parameters.solver_parameters,
363+
# )
360364
parameters.solver_parameters.trace_propagation = True
361365
manager = routing.IndexManager(10, 1, 0)
362366
self.assertIsNotNone(manager)

0 commit comments

Comments
 (0)