Google OR-Tools v9.15
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
vrp_with_time_limit.cc

Simple VRP example.

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// [START program]
15// [START import]
16#include <algorithm>
17#include <cstdint>
18#include <cstdlib>
19#include <sstream>
20
21#include "google/protobuf/duration.pb.h"
28// [END import]
29
30namespace operations_research {
35// [START solution_printer]
36void PrintSolution(const RoutingIndexManager& manager,
37 const RoutingModel& routing, const Assignment& solution) {
38 int64_t max_route_distance = 0;
39 for (int vehicle_id = 0; vehicle_id < manager.num_vehicles(); ++vehicle_id) {
40 if (!routing.IsVehicleUsed(solution, vehicle_id)) {
41 continue;
42 }
43 int64_t index = routing.Start(vehicle_id);
44 LOG(INFO) << "Route for Vehicle " << vehicle_id << ":";
45 int64_t route_distance = 0;
46 std::stringstream route;
47 while (!routing.IsEnd(index)) {
48 route << manager.IndexToNode(index).value() << " -> ";
49 const int64_t previous_index = index;
50 index = solution.Value(routing.NextVar(index));
51 route_distance += const_cast<RoutingModel&>(routing).GetArcCostForVehicle(
52 previous_index, index, int64_t{vehicle_id});
53 }
54 LOG(INFO) << route.str() << manager.IndexToNode(index).value();
55 LOG(INFO) << "Distance of the route: " << route_distance << "m";
56 max_route_distance = std::max(route_distance, max_route_distance);
57 }
58 LOG(INFO) << "Maximum of the route distances: " << max_route_distance << "m";
59 LOG(INFO) << "";
60 LOG(INFO) << "Problem solved in " << routing.solver()->wall_time() << "ms";
61}
62// [END solution_printer]
63
64void VrpGlobalSpan() {
65 // Instantiate the data problem.
66 // [START data]
67 const int num_locations = 20;
68 const int num_vehicles = 5;
69 const RoutingIndexManager::NodeIndex depot{0};
70 // [END data]
71
72 // Create Routing Index Manager
73 // [START index_manager]
74 RoutingIndexManager manager(num_locations, num_vehicles, depot);
75 // [END index_manager]
76
77 // Create Routing Model.
78 // [START routing_model]
79 RoutingModel routing(manager);
80 // [END routing_model]
81
82 // Create and register a transit callback.
83 // [START transit_callback]
84 const int transit_callback_index = routing.RegisterTransitCallback(
85 [](int64_t from_index, int64_t to_index) -> int64_t {
86 (void)from_index;
87 (void)to_index;
88 return 1;
89 });
90 // [END transit_callback]
91
92 // Define cost of each arc.
93 // [START arc_cost]
94 routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index);
95 // [END arc_cost]
96
97 // Add Distance constraint.
98 // [START distance_constraint]
99 routing.AddDimension(transit_callback_index,
100 /*slack_max=*/0,
101 /*capacity=*/3000,
102 /*fix_start_cumul_to_zero=*/true, "Distance");
103 const RoutingDimension& distance_dimension =
104 routing.GetDimensionOrDie("Distance");
105 const_cast<RoutingDimension&>(distance_dimension)
106 .SetGlobalSpanCostCoefficient(100);
107 // [END distance_constraint]
108
109 // Setting first solution heuristic.
110 // [START parameters]
111 RoutingSearchParameters search_parameters = DefaultRoutingSearchParameters();
112 search_parameters.set_first_solution_strategy(
114 search_parameters.set_local_search_metaheuristic(
116 search_parameters.set_log_search(true);
117 search_parameters.mutable_time_limit()->set_seconds(5);
118 // [END parameters]
119
120 // Solve the problem.
121 // [START solve]
122 const Assignment* solution = routing.SolveWithParameters(search_parameters);
123 // [END solve]
124
125 // Print solution on console.
126 // [START print_solution]
127 PrintSolution(manager, routing, *solution);
128 // [END print_solution]
129}
130} // namespace operations_research
131
132int main(int /*argc*/, char* /*argv*/[]) {
133 operations_research::VrpGlobalSpan();
134 return EXIT_SUCCESS;
135}
136// [END program]
int main(int argc, char **argv)
Definition fz.cc:218
OR-Tools root namespace.
Select next search node to expand Select next item_i to add this new search node to the search Generate a new search node where item_i is not in the knapsack Check validity of this new partial solution(using propagators) - If valid
RoutingSearchParameters DefaultRoutingSearchParameters()
The vehicle routing library lets one model and solve generic vehicle routing problems ranging from th...