|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import cudf |
| 5 | +import numpy as np |
| 6 | + |
| 7 | +from cuopt import routing |
| 8 | + |
| 9 | + |
| 10 | +def create_tsp_cost_matrix(n_locations): |
| 11 | + """Creates a simple symmetric cost matrix for TSP.""" |
| 12 | + cost_matrix = np.zeros((n_locations, n_locations), dtype=np.float32) |
| 13 | + for i in range(n_locations): |
| 14 | + for j in range(n_locations): |
| 15 | + cost_matrix[i, j] = abs(i - j) |
| 16 | + return cudf.DataFrame(cost_matrix) |
| 17 | + |
| 18 | + |
| 19 | +def test_batch_solve_varying_sizes(): |
| 20 | + """Test batch solving TSPs of varying sizes.""" |
| 21 | + tsp_sizes = [5, 8, 10, 6, 7, 9] |
| 22 | + |
| 23 | + # Create data models for each TSP |
| 24 | + data_models = [] |
| 25 | + for n_locations in tsp_sizes: |
| 26 | + cost_matrix = create_tsp_cost_matrix(n_locations) |
| 27 | + dm = routing.DataModel(n_locations, 1) |
| 28 | + dm.add_cost_matrix(cost_matrix) |
| 29 | + data_models.append(dm) |
| 30 | + |
| 31 | + # Configure solver settings |
| 32 | + settings = routing.SolverSettings() |
| 33 | + settings.set_time_limit(5.0) |
| 34 | + |
| 35 | + # Call batch solve |
| 36 | + solutions, solve_time = routing.BatchSolve(data_models, settings) |
| 37 | + |
| 38 | + # Verify results |
| 39 | + assert len(solutions) == len(tsp_sizes) |
| 40 | + for i, solution in enumerate(solutions): |
| 41 | + assert solution.get_status() == routing.SolutionStatus.SUCCESS, ( |
| 42 | + f"TSP {i} (size {tsp_sizes[i]}) failed" |
| 43 | + ) |
| 44 | + assert solution.get_vehicle_count() == 1, ( |
| 45 | + f"TSP {i} (size {tsp_sizes[i]}) used multiple vehicles" |
| 46 | + ) |
| 47 | + |
| 48 | + # Verify solve time is reasonable |
| 49 | + assert solve_time > 0.0, "Solve time should be positive" |
| 50 | + |
| 51 | + |
| 52 | +def test_batch_solve_same_size(): |
| 53 | + """Test batch solving multiple TSPs of the same size.""" |
| 54 | + n_problems = 10 |
| 55 | + n_locations = 6 |
| 56 | + |
| 57 | + # Create data models |
| 58 | + data_models = [] |
| 59 | + for _ in range(n_problems): |
| 60 | + cost_matrix = create_tsp_cost_matrix(n_locations) |
| 61 | + dm = routing.DataModel(n_locations, 1) |
| 62 | + dm.add_cost_matrix(cost_matrix) |
| 63 | + data_models.append(dm) |
| 64 | + |
| 65 | + # Configure solver settings |
| 66 | + settings = routing.SolverSettings() |
| 67 | + settings.set_time_limit(2.0) |
| 68 | + |
| 69 | + # Call batch solve |
| 70 | + solutions, solve_time = routing.BatchSolve(data_models, settings) |
| 71 | + |
| 72 | + # Verify all solutions succeeded |
| 73 | + assert len(solutions) == n_problems |
| 74 | + for i, solution in enumerate(solutions): |
| 75 | + assert solution.get_status() == routing.SolutionStatus.SUCCESS, ( |
| 76 | + f"TSP {i} failed" |
| 77 | + ) |
| 78 | + |
| 79 | + |
| 80 | +def test_batch_solve_single_problem(): |
| 81 | + """Test batch solve with a single problem.""" |
| 82 | + n_locations = 5 |
| 83 | + |
| 84 | + cost_matrix = create_tsp_cost_matrix(n_locations) |
| 85 | + dm = routing.DataModel(n_locations, 1) |
| 86 | + dm.add_cost_matrix(cost_matrix) |
| 87 | + |
| 88 | + settings = routing.SolverSettings() |
| 89 | + settings.set_time_limit(2.0) |
| 90 | + |
| 91 | + solutions, solve_time = routing.BatchSolve([dm], settings) |
| 92 | + |
| 93 | + assert len(solutions) == 1 |
| 94 | + assert solutions[0].get_status() == routing.SolutionStatus.SUCCESS |
| 95 | + |
| 96 | + |
| 97 | +def test_batch_solve_default_settings(): |
| 98 | + """Test batch solve with default solver settings.""" |
| 99 | + tsp_sizes = [5, 6, 7] |
| 100 | + |
| 101 | + data_models = [] |
| 102 | + for n_locations in tsp_sizes: |
| 103 | + cost_matrix = create_tsp_cost_matrix(n_locations) |
| 104 | + dm = routing.DataModel(n_locations, 1) |
| 105 | + dm.add_cost_matrix(cost_matrix) |
| 106 | + data_models.append(dm) |
| 107 | + |
| 108 | + # Call batch solve without explicit settings |
| 109 | + solutions, solve_time = routing.BatchSolve(data_models) |
| 110 | + |
| 111 | + assert len(solutions) == len(tsp_sizes) |
| 112 | + for solution in solutions: |
| 113 | + assert solution.get_status() == routing.SolutionStatus.SUCCESS |
0 commit comments