18 #include "absl/container/flat_hash_set.h"
30 num_vehicles, {depot, depot})) {}
33 const std::vector<NodeIndex>& starts,
34 const std::vector<NodeIndex>& ends) {
37 std::vector<std::pair<NodeIndex, NodeIndex>> starts_ends(
num_vehicles);
39 starts_ends[v] = {starts[v], ends[v]};
45 int num_nodes,
int num_vehicles,
46 const std::vector<std::pair<NodeIndex, NodeIndex>>& starts_ends) {
50 void RoutingIndexManager::Initialize(
51 int num_nodes,
int num_vehicles,
52 const std::vector<std::pair<NodeIndex, NodeIndex>>& starts_ends) {
57 CHECK_EQ(num_vehicles_, starts_ends.size());
58 absl::flat_hash_set<NodeIndex> starts;
59 absl::flat_hash_set<NodeIndex> ends;
60 absl::flat_hash_set<NodeIndex> unique_depots;
61 for (
const std::pair<NodeIndex, NodeIndex>& start_end : starts_ends) {
70 unique_depots.insert(start);
71 unique_depots.insert(end);
73 num_unique_depots_ = unique_depots.size();
74 const int size = num_nodes_ + num_vehicles_ - num_unique_depots_;
76 index_to_node_.resize(size + num_vehicles_);
78 vehicle_to_start_.resize(num_vehicles_);
79 vehicle_to_end_.resize(num_vehicles_);
81 for (
NodeIndex i = kZeroNode; i < num_nodes_; ++i) {
83 index_to_node_[
index] = i;
84 node_to_index_[i] =
index;
88 absl::flat_hash_set<NodeIndex> seen_starts;
89 for (
int i = 0; i < num_vehicles_; ++i) {
90 const NodeIndex start = starts_ends[i].first;
92 seen_starts.insert(start);
93 const int64 start_index = node_to_index_[start];
94 vehicle_to_start_[i] = start_index;
97 vehicle_to_start_[i] =
index;
98 index_to_node_[
index] = start;
102 for (
int i = 0; i < num_vehicles_; ++i) {
104 index_to_node_[
index] = end;
105 vehicle_to_end_[i] =
index;
111 VLOG(1) <<
"Number of nodes: " << num_nodes_;
112 VLOG(1) <<
"Number of vehicles: " << num_vehicles_;
114 VLOG(2) <<
"Variable index " <<
index <<
" -> Node index "
115 << index_to_node_[
index];
117 for (
NodeIndex node = kZeroNode; node < node_to_index_.
size(); ++node) {
118 VLOG(2) <<
"Node index " << node <<
" -> Variable index "
119 << node_to_index_[node];
124 const std::vector<NodeIndex>& nodes)
const {
125 std::vector<int64> indices;
126 indices.reserve(nodes.size());
130 indices.push_back(
index);
136 const std::vector<int64>& indices)
const {
137 std::vector<NodeIndex> nodes;
138 nodes.reserve(indices.size());
#define CHECK_EQ(val1, val2)
#define CHECK_GE(val1, val2)
#define CHECK_NE(val1, val2)
#define CHECK_LE(val1, val2)
#define VLOG(verboselevel)
void resize(size_type new_size)
Manager for any NodeIndex <-> variable index conversion.
static const int64 kUnassigned
int64 NodeToIndex(NodeIndex node) const
RoutingIndexManager(int num_nodes, int num_vehicles, NodeIndex depot)
Creates a NodeIndex to variable index mapping for a problem containing 'num_nodes',...
NodeIndex IndexToNode(int64 index) const
std::vector< int64 > NodesToIndices(const std::vector< NodeIndex > &nodes) const
RoutingNodeIndex NodeIndex
std::vector< NodeIndex > IndicesToNodes(const std::vector< int64 > &indices) const
bool ContainsKey(const Collection &collection, const Key &key)
The vehicle routing library lets one model and solve generic vehicle routing problems ranging from th...