OR-Tools  9.6
cvrptw.cc
Go to the documentation of this file.
1 // Copyright 2010-2022 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 //
15 // Capacitated Vehicle Routing Problem with Time Windows (and optional orders).
16 // A description of the problem can be found here:
17 // http://en.wikipedia.org/wiki/Vehicle_routing_problem.
18 // The variant which is tackled by this model includes a capacity dimension,
19 // time windows and optional orders, with a penalty cost if orders are not
20 // performed. For the sake of simplicity, orders are randomly located and
21 // distances are computed using the Manhattan distance. Distances are assumed
22 // to be in meters and times in seconds.
23 
24 #include <cstdint>
25 #include <random>
26 #include <vector>
27 
28 #include "absl/random/random.h"
29 #include "google/protobuf/text_format.h"
33 #include "ortools/base/logging.h"
37 #include "ortools/constraint_solver/routing_parameters.pb.h"
39 
48 using operations_research::RoutingNodeIndex;
49 using operations_research::RoutingSearchParameters;
51 
52 ABSL_FLAG(int, vrp_orders, 100, "Number of nodes in the problem");
53 ABSL_FLAG(int, vrp_vehicles, 20, "Number of vehicles in the problem");
54 ABSL_FLAG(bool, vrp_use_deterministic_random_seed, false,
55  "Use deterministic random seeds");
56 ABSL_FLAG(bool, vrp_use_same_vehicle_costs, false,
57  "Use same vehicle costs in the routing model");
58 ABSL_FLAG(std::string, routing_search_parameters, "",
59  "Text proto RoutingSearchParameters (possibly partial) that will "
60  "override the DefaultRoutingSearchParameters()");
61 
62 const char* kTime = "Time";
63 const char* kCapacity = "Capacity";
64 const int64_t kMaxNodesPerGroup = 10;
65 const int64_t kSameVehicleCost = 1000;
66 
67 int main(int argc, char** argv) {
68  InitGoogle(argv[0], &argc, &argv, true);
69  CHECK_LT(0, absl::GetFlag(FLAGS_vrp_orders))
70  << "Specify an instance size greater than 0.";
71  CHECK_LT(0, absl::GetFlag(FLAGS_vrp_vehicles))
72  << "Specify a non-null vehicle fleet size.";
73  // VRP of size absl::GetFlag(FLAGS_vrp_size).
74  // Nodes are indexed from 0 to absl::GetFlag(FLAGS_vrp_orders), the starts and
75  // ends of the routes are at node 0.
76  const RoutingIndexManager::NodeIndex kDepot(0);
77  RoutingIndexManager manager(absl::GetFlag(FLAGS_vrp_orders) + 1,
78  absl::GetFlag(FLAGS_vrp_vehicles), kDepot);
79  RoutingModel routing(manager);
80 
81  // Setting up locations.
82  const int64_t kXMax = 100000;
83  const int64_t kYMax = 100000;
84  const int64_t kSpeed = 10;
85  LocationContainer locations(
86  kSpeed, absl::GetFlag(FLAGS_vrp_use_deterministic_random_seed));
87  for (int location = 0; location <= absl::GetFlag(FLAGS_vrp_orders);
88  ++location) {
89  locations.AddRandomLocation(kXMax, kYMax);
90  }
91 
92  // Setting the cost function.
93  const int vehicle_cost = routing.RegisterTransitCallback(
94  [&locations, &manager](int64_t i, int64_t j) {
95  return locations.ManhattanDistance(manager.IndexToNode(i),
96  manager.IndexToNode(j));
97  });
98  routing.SetArcCostEvaluatorOfAllVehicles(vehicle_cost);
99 
100  // Adding capacity dimension constraints.
101  const int64_t kVehicleCapacity = 40;
102  const int64_t kNullCapacitySlack = 0;
103  RandomDemand demand(manager.num_nodes(), kDepot,
104  absl::GetFlag(FLAGS_vrp_use_deterministic_random_seed));
105  demand.Initialize();
106  routing.AddDimension(routing.RegisterTransitCallback(
107  [&demand, &manager](int64_t i, int64_t j) {
108  return demand.Demand(manager.IndexToNode(i),
109  manager.IndexToNode(j));
110  }),
111  kNullCapacitySlack, kVehicleCapacity,
112  /*fix_start_cumul_to_zero=*/true, kCapacity);
113 
114  // Adding time dimension constraints.
115  const int64_t kTimePerDemandUnit = 300;
116  const int64_t kHorizon = 24 * 3600;
118  kTimePerDemandUnit,
119  [&demand](RoutingNodeIndex i, RoutingNodeIndex j) {
120  return demand.Demand(i, j);
121  },
122  [&locations](RoutingNodeIndex i, RoutingNodeIndex j) {
123  return locations.ManhattanTime(i, j);
124  });
125  routing.AddDimension(
126  routing.RegisterTransitCallback([&time, &manager](int64_t i, int64_t j) {
127  return time.Compute(manager.IndexToNode(i), manager.IndexToNode(j));
128  }),
129  kHorizon, kHorizon, /*fix_start_cumul_to_zero=*/true, kTime);
130  const RoutingDimension& time_dimension = routing.GetDimensionOrDie(kTime);
131 
132  // Adding time windows.
133  std::mt19937 randomizer(
134  GetSeed(absl::GetFlag(FLAGS_vrp_use_deterministic_random_seed)));
135  const int64_t kTWDuration = 5 * 3600;
136  for (int order = 1; order < manager.num_nodes(); ++order) {
137  const int64_t start =
138  absl::Uniform<int32_t>(randomizer, 0, kHorizon - kTWDuration);
139  time_dimension.CumulVar(order)->SetRange(start, start + kTWDuration);
140  }
141 
142  // Adding penalty costs to allow skipping orders.
143  const int64_t kPenalty = 10000000;
144  const RoutingIndexManager::NodeIndex kFirstNodeAfterDepot(1);
145  for (RoutingIndexManager::NodeIndex order = kFirstNodeAfterDepot;
146  order < manager.num_nodes(); ++order) {
147  std::vector<int64_t> orders(1, manager.NodeToIndex(order));
148  routing.AddDisjunction(orders, kPenalty);
149  }
150 
151  // Adding same vehicle constraint costs for consecutive nodes.
152  if (absl::GetFlag(FLAGS_vrp_use_same_vehicle_costs)) {
153  std::vector<int64_t> group;
154  for (RoutingIndexManager::NodeIndex order = kFirstNodeAfterDepot;
155  order < manager.num_nodes(); ++order) {
156  group.push_back(manager.NodeToIndex(order));
157  if (group.size() == kMaxNodesPerGroup) {
159  group.clear();
160  }
161  }
162  if (!group.empty()) {
164  }
165  }
166 
167  // Solve, returns a solution if any (owned by RoutingModel).
168  RoutingSearchParameters parameters = DefaultRoutingSearchParameters();
169  CHECK(google::protobuf::TextFormat::MergeFromString(
170  absl::GetFlag(FLAGS_routing_search_parameters), &parameters));
171  const Assignment* solution = routing.SolveWithParameters(parameters);
172  if (solution != nullptr) {
173  DisplayPlan(manager, routing, *solution,
174  absl::GetFlag(FLAGS_vrp_use_same_vehicle_costs),
176  routing.GetDimensionOrDie(kCapacity),
177  routing.GetDimensionOrDie(kTime));
178  } else {
179  LOG(INFO) << "No solution found.";
180  }
181  return EXIT_SUCCESS;
182 }
An Assignment is a variable -> domains mapping, used to report solutions to the user.
virtual void SetRange(int64_t l, int64_t u)
This method sets both the min and the max of the expression.
int64_t ManhattanTime(RoutingIndexManager::NodeIndex from, RoutingIndexManager::NodeIndex to) const
Definition: cvrptw_lib.cc:72
void AddRandomLocation(int64_t x_max, int64_t y_max)
Definition: cvrptw_lib.cc:49
int64_t ManhattanDistance(RoutingIndexManager::NodeIndex from, RoutingIndexManager::NodeIndex to) const
Definition: cvrptw_lib.cc:62
Dimensions represent quantities accumulated at nodes along the routes.
Definition: routing.h:2750
IntVar * CumulVar(int64_t index) const
Get the cumul, transit and slack variables for the given node (given as int64_t var index).
Definition: routing.h:2769
Manager for any NodeIndex <-> variable index conversion.
NodeIndex IndexToNode(int64_t index) const
int64_t NodeToIndex(NodeIndex node) const
void AddSoftSameVehicleConstraint(const std::vector< int64_t > &indices, int64_t cost)
Adds a soft constraint to force a set of variable indices to be on the same vehicle.
Definition: routing.cc:2269
DisjunctionIndex AddDisjunction(const std::vector< int64_t > &indices, int64_t penalty=kNoPenalty, int64_t max_cardinality=1)
Adds a disjunction constraint on the indices: exactly 'max_cardinality' of the indices are active.
Definition: routing.cc:2179
const Assignment * SolveWithParameters(const RoutingSearchParameters &search_parameters, std::vector< const Assignment * > *solutions=nullptr)
Solves the current routing model with the given parameters.
Definition: routing.cc:3311
int RegisterTransitCallback(TransitCallback2 callback)
Definition: routing.cc:1301
void SetArcCostEvaluatorOfAllVehicles(int evaluator_index)
Sets the cost function of the model such that the cost of a segment of a route between node 'from' an...
Definition: routing.cc:1783
bool AddDimension(int evaluator_index, int64_t slack_max, int64_t capacity, bool fix_start_cumul_to_zero, const std::string &name)
Model creation.
Definition: routing.cc:1358
const RoutingDimension & GetDimensionOrDie(const std::string &dimension_name) const
Returns a dimension from its name. Dies if the dimension does not exist.
Definition: routing.cc:1685
SatParameters parameters
int main(int argc, char **argv)
Definition: cvrptw.cc:67
const char * kCapacity
Definition: cvrptw.cc:63
const char * kTime
Definition: cvrptw.cc:62
const int64_t kMaxNodesPerGroup
Definition: cvrptw.cc:64
const int64_t kSameVehicleCost
Definition: cvrptw.cc:65
ABSL_FLAG(int, vrp_orders, 100, "Number of nodes in the problem")
void InitGoogle(const char *usage, int *argc, char ***argv, bool deprecated)
Definition: init_google.h:34
void DisplayPlan(const RoutingIndexManager &manager, const RoutingModel &routing, const operations_research::Assignment &plan, bool use_same_vehicle_costs, int64_t max_nodes_per_group, int64_t same_vehicle_cost, const operations_research::RoutingDimension &capacity_dimension, const operations_research::RoutingDimension &time_dimension)
Definition: cvrptw_lib.cc:160
int32_t GetSeed(bool deterministic)
Definition: cvrptw_lib.cc:35
RoutingSearchParameters DefaultRoutingSearchParameters()
int64_t demand
Definition: resource.cc:126
int64_t time
Definition: resource.cc:1694
int64_t start