19#include "absl/container/flat_hash_set.h"
31 num_vehicles, {depot, depot})) {}
34 const std::vector<NodeIndex>& starts,
35 const std::vector<NodeIndex>& ends) {
38 std::vector<std::pair<NodeIndex, NodeIndex>> starts_ends(
num_vehicles);
40 starts_ends[v] = {starts[v], ends[v]};
46 int num_nodes,
int num_vehicles,
47 const std::vector<std::pair<NodeIndex, NodeIndex>>& starts_ends) {
51void RoutingIndexManager::Initialize(
52 int num_nodes,
int num_vehicles,
53 const std::vector<std::pair<NodeIndex, NodeIndex>>& starts_ends) {
58 CHECK_EQ(num_vehicles_, starts_ends.size());
59 absl::flat_hash_set<NodeIndex> starts;
60 absl::flat_hash_set<NodeIndex> ends;
61 absl::flat_hash_set<NodeIndex> unique_depots;
62 for (
const std::pair<NodeIndex, NodeIndex>& start_end : starts_ends) {
71 unique_depots.insert(
start);
72 unique_depots.insert(
end);
74 num_unique_depots_ = unique_depots.size();
75 const int size = num_nodes_ + num_vehicles_ - num_unique_depots_;
77 index_to_node_.resize(size + num_vehicles_);
79 vehicle_to_start_.resize(num_vehicles_);
80 vehicle_to_end_.resize(num_vehicles_);
82 for (
NodeIndex i = kZeroNode; i < num_nodes_; ++i) {
83 if (starts.contains(i) || !ends.contains(i)) {
84 index_to_node_[
index] = i;
85 node_to_index_[i] =
index;
89 absl::flat_hash_set<NodeIndex> seen_starts;
90 for (
int i = 0; i < num_vehicles_; ++i) {
92 if (!seen_starts.contains(
start)) {
93 seen_starts.insert(
start);
94 const int64_t start_index = node_to_index_[
start];
95 vehicle_to_start_[i] = start_index;
98 vehicle_to_start_[i] =
index;
103 for (
int i = 0; i < num_vehicles_; ++i) {
106 vehicle_to_end_[i] =
index;
112 VLOG(1) <<
"Number of nodes: " << num_nodes_;
113 VLOG(1) <<
"Number of vehicles: " << num_vehicles_;
115 VLOG(2) <<
"Variable index " <<
index <<
" -> Node index "
116 << index_to_node_[
index];
118 for (
NodeIndex node = kZeroNode; node < node_to_index_.
size(); ++node) {
119 VLOG(2) <<
"Node index " << node <<
" -> Variable index "
120 << node_to_index_[node];
125 const std::vector<NodeIndex>&
nodes)
const {
126 std::vector<int64_t> indices;
127 indices.reserve(
nodes.size());
131 indices.push_back(
index);
137 const std::vector<int64_t>& indices)
const {
138 std::vector<NodeIndex>
nodes;
139 nodes.reserve(indices.size());
140 for (
const int64_t
index : indices) {
#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.
std::vector< NodeIndex > IndicesToNodes(const std::vector< int64_t > &indices) const
static const int64_t kUnassigned
std::vector< int64_t > NodesToIndices(const std::vector< NodeIndex > &nodes) 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_t index) const
int64_t NodeToIndex(NodeIndex node) const
RoutingNodeIndex NodeIndex
Collection of objects used to extend the Constraint Solver library.
std::optional< int64_t > end