-
Notifications
You must be signed in to change notification settings - Fork 215
fix(routing): preserve row-major layout when resizing capacity routes #1621
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mlubin
wants to merge
1
commit into
NVIDIA:main
Choose a base branch
from
mlubin:fix/capacity-route-resize-stride
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+126
−4
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| /* clang-format off */ | ||
| /* | ||
| * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| /* clang-format on */ | ||
|
|
||
| #include <gtest/gtest.h> | ||
| #include <cuopt/routing/solve.hpp> | ||
| #include <utilities/copy_helpers.hpp> | ||
|
|
||
| #include <numeric> | ||
| #include <vector> | ||
|
|
||
| namespace cuopt::routing::test { | ||
|
|
||
| // Covers capacity_route_t::resize preserving the row-major layout of a multi-dimension | ||
| // capacity route: many orders over a few vehicles force a route to grow past its base | ||
| // allocation, and a second capacity dimension exposes the resize. See issue #1618. | ||
| TEST(capacity_route_resize, multi_dim_grow_preserves_second_dimension) | ||
| { | ||
| constexpr int norders = 130; | ||
| constexpr int nvehicles = 3; | ||
| constexpr int nloops = 3; | ||
| constexpr int time_limit = 2; | ||
| constexpr int nlocations = norders + 1; | ||
| constexpr int cap = 1000000; // loose: never binding, so any excess is corruption | ||
|
|
||
| // Locations on a line; cost = |a - b| so consolidating orders onto a route is cheap. | ||
| std::vector<float> cost_matrix(nlocations * nlocations); | ||
| for (int a = 0; a < nlocations; ++a) { | ||
| for (int b = 0; b < nlocations; ++b) { | ||
| cost_matrix[a * nlocations + b] = std::abs(a - b); | ||
| } | ||
| } | ||
| std::vector<int> order_locations(norders); | ||
| std::iota(order_locations.begin(), order_locations.end(), 1); // order i -> location i+1 | ||
| std::vector<int> vehicle_start(nvehicles, 0), vehicle_return(nvehicles, 0); | ||
| std::vector<int> demand_weight(norders, 1); | ||
| std::vector<int> demand_volume(norders, 1); | ||
| std::vector<int> capacity_weight(nvehicles, cap); | ||
| std::vector<int> capacity_volume(nvehicles, cap); | ||
|
|
||
| raft::handle_t handle; | ||
| auto stream = handle.get_stream(); | ||
|
|
||
| auto v_cost_matrix = device_copy(cost_matrix, stream); | ||
| auto v_order_locations = device_copy(order_locations, stream); | ||
| auto v_start = device_copy(vehicle_start, stream); | ||
| auto v_return = device_copy(vehicle_return, stream); | ||
| auto v_demand_weight = device_copy(demand_weight, stream); | ||
| auto v_demand_volume = device_copy(demand_volume, stream); | ||
| auto v_cap_weight = device_copy(capacity_weight, stream); | ||
| auto v_cap_volume = device_copy(capacity_volume, stream); | ||
|
|
||
| data_model_view_t<int, float> data_model(&handle, nlocations, nvehicles, norders); | ||
| data_model.add_cost_matrix(v_cost_matrix.data()); | ||
| data_model.set_order_locations(v_order_locations.data()); | ||
| data_model.set_vehicle_locations(v_start.data(), v_return.data()); | ||
| data_model.add_capacity_dimension("weight", v_demand_weight.data(), v_cap_weight.data()); | ||
| data_model.add_capacity_dimension("volume", v_demand_volume.data(), v_cap_volume.data()); | ||
|
|
||
| solver_settings_t<int, float> settings; | ||
| settings.set_time_limit(time_limit); | ||
|
|
||
| for (int loop = 0; loop < nloops; ++loop) { | ||
| auto routing_solution = solve(data_model, settings); | ||
| handle.sync_stream(); | ||
| ASSERT_EQ(routing_solution.get_status(), solution_status_t::SUCCESS) << "loop " << loop; | ||
|
|
||
| // The in-solver feasibility assert is the primary detector (it aborts during solve on | ||
| // the corrupted dimension). These host-side checks are a secondary guard for release | ||
| // builds where asserts are compiled out: a valid solution serves every order exactly | ||
| // once and respects each configured capacity. | ||
| host_assignment_t<int> host_route(routing_solution); | ||
| std::vector<int> load_weight(nvehicles, 0), load_volume(nvehicles, 0); | ||
| std::vector<int> times_served(norders, 0); | ||
| for (size_t i = 0; i < host_route.route.size(); ++i) { | ||
| auto node_type = static_cast<node_type_t>(host_route.node_types[i]); | ||
| if (node_type == node_type_t::DEPOT || node_type == node_type_t::BREAK) { continue; } | ||
| int truck = host_route.truck_id[i]; | ||
| int order = host_route.route[i]; | ||
| ++times_served[order]; | ||
| load_weight[truck] += demand_weight[order]; | ||
| load_volume[truck] += demand_volume[order]; | ||
| } | ||
| for (int order = 0; order < norders; ++order) { | ||
| ASSERT_EQ(times_served[order], 1) | ||
| << "order " << order << " served " << times_served[order] << " times, loop " << loop; | ||
| } | ||
| for (int v = 0; v < nvehicles; ++v) { | ||
| ASSERT_LE(load_weight[v], capacity_weight[v]) | ||
| << "weight cap on veh " << v << " loop " << loop; | ||
| ASSERT_LE(load_volume[v], capacity_volume[v]) | ||
| << "volume cap on veh " << v << " loop " << loop; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } // namespace cuopt::routing::test | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.