Skip to content

Commit f3d2d9f

Browse files
authored
feat(routing): add optional distance matrix inputs to C++ models (#2036)
Adds optional physical-distance matrix inputs to the C++ routing model, independent of cost and transit time. This is the foundation for the distance limits and vehicle-specific tier pricing discussed in #2018; user-facing interfaces and consumers will follow separately. - Adds registration, getters, CPU problem storage and CPU-to-device transfer. - Validates host matrix dimensions, distance values and vehicle-type correspondence and coverage. - Keeps distance inputs out of the solver's internal cost/time buffer and uses explicit indices for time-matrix access and detection. Registering distance does not change scoring or add distance constraints, tier charges or a distance-minimization objective. Models that omit it retain their cost/time behavior. Native consumers should rebuild because the C++ model layouts gain members. Local validation: all 73 tests in `ROUTING_UNIT_TEST` pass, including 13 distance-matrix tests using the public routing API. Pre-commit, clang-format and Vale pass. Authors: - José M Baca (https://github.com/josembd) - Ramakrishna Prabhu (https://github.com/ramakrishnap-nv) Approvers: - Hugo Linsenmaier (https://github.com/hlinsen) - Ramakrishna Prabhu (https://github.com/ramakrishnap-nv) URL: #2036
1 parent 477f843 commit f3d2d9f

11 files changed

Lines changed: 491 additions & 39 deletions

File tree

‎cpp/include/cuopt/routing/cpu_routing_problem.hpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ class cpu_routing_problem_t {
7979

8080
std::vector<cpu_cost_matrix_t> cost_matrices;
8181
std::vector<cpu_cost_matrix_t> transit_time_matrices;
82+
std::vector<cpu_cost_matrix_t> distance_matrices;
8283

8384
std::vector<int32_t> vehicle_start_locations;
8485
std::vector<int32_t> vehicle_return_locations;

‎cpp/include/cuopt/routing/data_model_view.hpp‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,15 @@ class data_model_view_t {
7676
*/
7777
void add_cost_matrix(f_t const* matrix, uint8_t vehicle_type = 0);
7878

79+
/**
80+
* @brief Register an optional physical distance matrix, separate from cost and time.
81+
*
82+
* @param[in] matrix Non-owning device pointer to a square row-major matrix.
83+
* @param[in] vehicle_type Vehicle-type identifier.
84+
* @throws cuopt::logic_error if matrix is null.
85+
*/
86+
void add_distance_matrix(f_t const* matrix, uint8_t vehicle_type = 0);
87+
7988
/**
8089
* @brief Set a transit time matrix for all locations (depot included) at
8190
* once. The time matrix is used to check constraints satisfiability rather
@@ -436,6 +445,9 @@ class data_model_view_t {
436445
*/
437446
f_t const* get_cost_matrix(uint8_t vehicle_type = 0) const noexcept;
438447

448+
/** @brief Return the distance matrix for a vehicle type, or nullptr if not set. */
449+
f_t const* get_distance_matrix(uint8_t vehicle_type = 0) const noexcept;
450+
439451
/**
440452
* @brief Get transit time matrix
441453
* @return Matrix pointer
@@ -448,6 +460,9 @@ class data_model_view_t {
448460
*/
449461
std::unordered_map<uint8_t, f_t const*> get_cost_matrices() const noexcept;
450462

463+
/** @brief Return all registered physical distance matrices. */
464+
std::unordered_map<uint8_t, f_t const*> get_distance_matrices() const noexcept;
465+
451466
/**
452467
* @brief Get all transit time matrices as a map
453468
* @return map of vehicle type to transit time matrix
@@ -661,6 +676,7 @@ class data_model_view_t {
661676
i_t n_requests_{};
662677
raft::device_span<uint8_t const> vehicle_types_;
663678
std::unordered_map<uint8_t, f_t const*> cost_matrices_{};
679+
std::unordered_map<uint8_t, f_t const*> distance_matrices_{};
664680
std::unordered_map<uint8_t, f_t const*> transit_time_matrices_{};
665681
i_t const* order_locations_{nullptr};
666682
i_t const* break_locations_{nullptr};

‎cpp/src/routing/cpu_routing_problem.cu‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#include <cuda/stream>
1616
#include <rmm/device_uvector.hpp>
1717

18+
#include <algorithm>
19+
#include <cmath>
1820
#include <stdexcept>
1921
#include <vector>
2022

@@ -24,6 +26,7 @@ namespace routing {
2426
struct cpu_routing_problem_t::device_data_t {
2527
std::vector<std::unique_ptr<rmm::device_uvector<float>>> cost_matrices;
2628
std::vector<std::unique_ptr<rmm::device_uvector<float>>> transit_time_matrices;
29+
std::vector<std::unique_ptr<rmm::device_uvector<float>>> distance_matrices;
2730

2831
std::unique_ptr<rmm::device_uvector<int32_t>> vehicle_start_locations;
2932
std::unique_ptr<rmm::device_uvector<int32_t>> vehicle_return_locations;
@@ -117,15 +120,25 @@ cpu_routing_problem_t::to_device(raft::handle_t* handle) const
117120

118121
int32_t orders = (num_orders < 0) ? num_locations : num_orders;
119122
data_model_view_t<int, float> view(handle, num_locations, fleet_size, orders);
123+
const auto matrix_size = static_cast<size_t>(num_locations) * num_locations;
120124

121125
for (auto const& cm : cost_matrices) {
126+
if (cm.matrix.size() != matrix_size) {
127+
throw std::invalid_argument(
128+
"cpu_routing_problem_t::to_device: cost matrix size must equal num_locations squared");
129+
}
122130
auto d = copy_vector(cm.matrix, stream);
123131
if (!d) { throw std::invalid_argument("cpu_routing_problem_t::to_device: empty cost matrix"); }
124132
view.add_cost_matrix(d->data(), cm.vehicle_type);
125133
data->cost_matrices.push_back(std::move(d));
126134
}
127135

128136
for (auto const& tm : transit_time_matrices) {
137+
if (tm.matrix.size() != matrix_size) {
138+
throw std::invalid_argument(
139+
"cpu_routing_problem_t::to_device: transit time matrix size must equal num_locations "
140+
"squared");
141+
}
129142
auto d = copy_vector(tm.matrix, stream);
130143
if (!d) {
131144
throw std::invalid_argument("cpu_routing_problem_t::to_device: empty transit time matrix");
@@ -134,6 +147,26 @@ cpu_routing_problem_t::to_device(raft::handle_t* handle) const
134147
data->transit_time_matrices.push_back(std::move(d));
135148
}
136149

150+
for (auto const& dm : distance_matrices) {
151+
if (dm.matrix.size() != matrix_size) {
152+
throw std::invalid_argument(
153+
"cpu_routing_problem_t::to_device: distance matrix size must equal num_locations squared");
154+
}
155+
if (std::any_of(dm.matrix.begin(), dm.matrix.end(), [](float value) {
156+
return std::isnan(value) || value < 0.f;
157+
})) {
158+
throw std::invalid_argument(
159+
"cpu_routing_problem_t::to_device: distance matrix values must be non-negative and not "
160+
"NaN");
161+
}
162+
auto d = copy_vector(dm.matrix, stream);
163+
if (!d) {
164+
throw std::invalid_argument("cpu_routing_problem_t::to_device: empty distance matrix");
165+
}
166+
view.add_distance_matrix(d->data(), dm.vehicle_type);
167+
data->distance_matrices.push_back(std::move(d));
168+
}
169+
137170
if (!vehicle_start_locations.empty() && !vehicle_return_locations.empty()) {
138171
data->vehicle_start_locations = copy_vector(vehicle_start_locations, stream);
139172
data->vehicle_return_locations = copy_vector(vehicle_return_locations, stream);

‎cpp/src/routing/data_model_view.cu‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@ data_model_view_t<i_t, f_t>::data_model_view_t(raft::handle_t* handle_ptr,
7676
"Number of nodes should be lower than 65535");
7777
}
7878

79+
template <typename i_t, typename f_t>
80+
void data_model_view_t<i_t, f_t>::add_distance_matrix(f_t const* matrix, uint8_t vehicle_type)
81+
{
82+
cuopt_expects(matrix != nullptr, error_type_t::ValidationError, "Matrix input cannot be null");
83+
distance_matrices_[vehicle_type] = matrix;
84+
}
85+
7986
template <typename i_t, typename f_t>
8087
void data_model_view_t<i_t, f_t>::add_cost_matrix(f_t const* matrix, uint8_t vehicle_type)
8188
{
@@ -805,6 +812,20 @@ raft::handle_t const* data_model_view_t<i_t, f_t>::get_handle_ptr() const noexce
805812
return handle_ptr_;
806813
}
807814

815+
template <typename i_t, typename f_t>
816+
f_t const* data_model_view_t<i_t, f_t>::get_distance_matrix(uint8_t vehicle_type) const noexcept
817+
{
818+
auto const matrix = distance_matrices_.find(vehicle_type);
819+
return matrix == distance_matrices_.end() ? nullptr : matrix->second;
820+
}
821+
822+
template <typename i_t, typename f_t>
823+
std::unordered_map<uint8_t, f_t const*> data_model_view_t<i_t, f_t>::get_distance_matrices()
824+
const noexcept
825+
{
826+
return distance_matrices_;
827+
}
828+
808829
template class CUOPT_EXPORT data_model_view_t<int, float>;
809830
} // namespace routing
810831
} // namespace cuopt

‎cpp/src/routing/fleet_info.cu‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ void populate_matrices(data_model_view_t<i_t, f_t> const& data_model, d_mdarray_
2626

2727
// Check for consistency of cost matrices
2828
const auto& cost_matrices = data_model.get_cost_matrices();
29+
const auto& distance_matrices = data_model.get_distance_matrices();
2930
const auto& transit_time_matrices = data_model.get_transit_time_matrices();
3031

3132
if (cost_matrices.empty()) { EXE_CUOPT_FAIL("Cost matrix (or matrices) must be specified!"); }
@@ -50,6 +51,29 @@ void populate_matrices(data_model_view_t<i_t, f_t> const& data_model, d_mdarray_
5051
}
5152
}
5253

54+
if (!distance_matrices.empty()) {
55+
for (auto const& [vtype, distance_matrix] : distance_matrices) {
56+
cuopt_expects(cost_matrices.count(vtype) > 0,
57+
error_type_t::ValidationError,
58+
"Distance matrix vehicle types must have corresponding cost matrices");
59+
const size_t matrix_size = static_cast<size_t>(nlocations) * nlocations;
60+
const bool valid =
61+
thrust::all_of(handle_ptr_->get_thrust_policy(),
62+
distance_matrix,
63+
distance_matrix + matrix_size,
64+
[] __device__(f_t value) { return value == value && value >= 0; });
65+
cuopt_expects(valid,
66+
error_type_t::ValidationError,
67+
"Distance matrix values must be non-negative and not NaN");
68+
}
69+
auto const types = get_unique_vehicle_types(data_model.get_vehicle_types(), stream_view_);
70+
for (auto const& [vtype, mapped_type] : types) {
71+
cuopt_expects(distance_matrices.count(vtype) > 0,
72+
error_type_t::ValidationError,
73+
"All vehicle distance matrices should be set");
74+
}
75+
}
76+
5377
auto n_matrix_types = detail::get_cost_matrix_type_dim<i_t, f_t>(data_model);
5478
matrices_ =
5579
detail::create_device_mdarray<f_t>(nlocations, n_vehicle_types, n_matrix_types, stream_view_);

‎cpp/src/routing/fleet_info.hpp‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ class fleet_info_t {
5252

5353
auto constexpr get_num_vehicles() const { return v_earliest_time_.size(); }
5454

55-
constexpr bool has_time_matrix() const { return matrices_.extent[1] > 1; }
55+
constexpr bool has_time_matrix() const
56+
{
57+
return matrices_.time_matrix_index != matrices_.cost_matrix_index;
58+
}
5659

5760
constexpr bool is_homogenous() const { return is_homogenous_; }
5861

@@ -93,6 +96,8 @@ class fleet_info_t {
9396
h.buckets = host_copy(v_buckets_, stream);
9497
h.matrices = detail::create_host_mdarray<f_t>(
9598
matrices_.extent[2], matrices_.extent[0], matrices_.extent[1]);
99+
h.matrices.cost_matrix_index = matrices_.cost_matrix_index;
100+
h.matrices.time_matrix_index = matrices_.time_matrix_index;
96101
raft::copy(h.matrices.buffer.data(), matrices_.buffer.data(), matrices_.buffer.size(), stream);
97102
return h;
98103
}
@@ -178,7 +183,10 @@ class fleet_info_t {
178183

179184
constexpr i_t is_homogenous_fleet() const { return is_homogenous; }
180185

181-
constexpr bool has_time_matrix() const { return matrices.extent[1] > 1; }
186+
constexpr bool has_time_matrix() const
187+
{
188+
return matrices.time_matrix_index != matrices.cost_matrix_index;
189+
}
182190
i_t num_vehicles = 0;
183191
mdarray_view_t<f_t> matrices{};
184192
const i_t* break_offset{nullptr};

‎cpp/src/routing/solver.cu‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* clang-format off */
22
/*
3-
* SPDX-FileCopyrightText: Copyright (c) 2021-2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3+
* SPDX-FileCopyrightText: Copyright (c) 2021-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66
/* clang-format on */
@@ -43,8 +43,8 @@ solver_t<i_t, f_t>::solver_t(data_model_view_t<i_t, f_t> const& data_model,
4343
solver_settings_t<i_t, f_t> const& settings)
4444
: handle_ptr_(data_model.get_handle_ptr()), settings_(settings)
4545
{
46-
auto n_matrix_types = detail::get_cost_matrix_type_dim<i_t, f_t>(data_model);
47-
if (n_matrix_types == 1 && !data_model.get_vehicle_max_times().empty()) {
46+
if (!detail::has_transit_time_matrix<i_t, f_t>(data_model) &&
47+
!data_model.get_vehicle_max_times().empty()) {
4848
cuopt_expects(false,
4949
error_type_t::ValidationError,
5050
"Time matrix should be set in order to use vehicle max time constraints");

0 commit comments

Comments
 (0)