Google OR-Tools v9.15
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
routing_index_manager.h
Go to the documentation of this file.
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#ifndef ORTOOLS_CONSTRAINT_SOLVER_ROUTING_INDEX_MANAGER_H_
15#define ORTOOLS_CONSTRAINT_SOLVER_ROUTING_INDEX_MANAGER_H_
16
17#include <cstdint>
18#include <utility>
19#include <vector>
20
21#include "absl/log/check.h"
22#include "absl/types/span.h"
26
27namespace operations_research {
28
54 public:
55 typedef RoutingNodeIndex NodeIndex;
56 static const int64_t kUnassigned;
75 const std::vector<NodeIndex>& starts,
76 const std::vector<NodeIndex>& ends);
85 int num_nodes, int num_vehicles,
86 const std::vector<std::pair<NodeIndex, NodeIndex> >& starts_ends);
87
88 // Returns the number of nodes in the manager.
89 int num_nodes() const { return num_nodes_; }
90 // Returns the number of vehicles in the manager.
91 int num_vehicles() const { return num_vehicles_; }
92 // Returns the number of indices mapped to nodes.
93 int num_indices() const { return index_to_node_.size(); }
94 // Returns start and end indices of the given vehicle.
95 int64_t GetStartIndex(int vehicle) const {
96 return vehicle_to_start_[vehicle];
97 }
98 int64_t GetEndIndex(int vehicle) const { return vehicle_to_end_[vehicle]; }
99 // Returns the index of a node. A node can correspond to multiple indices if
100 // it's a start or end node. As of 03/2020, kUnassigned will be returned for
101 // all end nodes. If a node appears more than once as a start node, the index
102 // of the first node in the list of start nodes is returned.
103 int64_t NodeToIndex(NodeIndex node) const {
104 DCHECK_GE(node.value(), 0);
105 DCHECK_LT(node.value(), node_to_index_.size());
106 return node_to_index_[node];
108 // Same as NodeToIndex but for a given vector of nodes.
109 std::vector<int64_t> NodesToIndices(
110 const std::vector<NodeIndex>& nodes) const;
111 // Returns the node corresponding to an index. A node may appear more than
112 // once if it is used as the start or the end node of multiple vehicles.
113 NodeIndex IndexToNode(int64_t index) const {
114 DCHECK_GE(index, 0);
115 DCHECK_LT(index, index_to_node_.size());
116 return index_to_node_[index];
118 // Same as IndexToNode but for a given vector of indices.
119 std::vector<NodeIndex> IndicesToNodes(
120 absl::Span<const int64_t> indices) const;
121 // TODO(user) Add unit tests for NodesToIndices and IndicesToNodes.
122 // TODO(user): Remove when removal of NodeIndex from RoutingModel is
123 // complete.
124 int num_unique_depots() const { return num_unique_depots_; }
125 std::vector<NodeIndex> GetIndexToNodeMap() const { return index_to_node_; }
126
127 private:
128 void Initialize(
130 const std::vector<std::pair<NodeIndex, NodeIndex> >& starts_ends);
131
132 std::vector<NodeIndex> index_to_node_;
134 std::vector<int64_t> vehicle_to_start_;
135 std::vector<int64_t> vehicle_to_end_;
136 int num_nodes_;
137 int num_vehicles_;
138 int num_unique_depots_;
139};
140
141} // namespace operations_research
142
143#endif // ORTOOLS_CONSTRAINT_SOLVER_ROUTING_INDEX_MANAGER_H_
#define OR_DLL
Definition base_export.h:27
Manager for any NodeIndex <-> variable index conversion.
RoutingIndexManager(int num_nodes, int num_vehicles, NodeIndex depot)
Creates a NodeIndex to variable index mapping for a problem containing 'num_nodes',...
OR-Tools root namespace.
BlossomGraph::NodeIndex NodeIndex