OR-Tools  9.6
routing_index_manager.h
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 #ifndef OR_TOOLS_CONSTRAINT_SOLVER_ROUTING_INDEX_MANAGER_H_
15 #define OR_TOOLS_CONSTRAINT_SOLVER_ROUTING_INDEX_MANAGER_H_
16 
17 #include <utility>
18 #include <vector>
19 
21 #include "ortools/base/logging.h"
24 
25 namespace operations_research {
26 
50  public:
51  typedef RoutingNodeIndex NodeIndex;
52  static const int64_t kUnassigned;
53 
60  const std::vector<NodeIndex>& starts,
61  const std::vector<NodeIndex>& ends);
63  int num_nodes, int num_vehicles,
64  const std::vector<std::pair<NodeIndex, NodeIndex> >& starts_ends);
66 
67  // Returns the number of nodes in the manager.
68  int num_nodes() const { return num_nodes_; }
69  // Returns the number of vehicles in the manager.
70  int num_vehicles() const { return num_vehicles_; }
71  // Returns the number of indices mapped to nodes.
72  int num_indices() const { return index_to_node_.size(); }
73  // Returns start and end indices of the given vehicle.
74  int64_t GetStartIndex(int vehicle) const {
75  return vehicle_to_start_[vehicle];
76  }
77  int64_t GetEndIndex(int vehicle) const { return vehicle_to_end_[vehicle]; }
78  // Returns the index of a node. A node can correspond to multiple indices if
79  // it's a start or end node. As of 03/2020, kUnassigned will be returned for
80  // all end nodes. If a node appears more than once as a start node, the index
81  // of the first node in the list of start nodes is returned.
82  int64_t NodeToIndex(NodeIndex node) const {
83  DCHECK_GE(node.value(), 0);
84  DCHECK_LT(node.value(), node_to_index_.size());
85  return node_to_index_[node];
86  }
87  // Same as NodeToIndex but for a given vector of nodes.
88  std::vector<int64_t> NodesToIndices(
89  const std::vector<NodeIndex>& nodes) const;
90  // Returns the node corresponding to an index. A node may appear more than
91  // once if it is used as the start or the end node of multiple vehicles.
92  NodeIndex IndexToNode(int64_t index) const {
93  DCHECK_GE(index, 0);
94  DCHECK_LT(index, index_to_node_.size());
95  return index_to_node_[index];
96  }
97  // Same as IndexToNode but for a given vector of indices.
98  std::vector<NodeIndex> IndicesToNodes(
99  const std::vector<int64_t>& indices) const;
100  // TODO(user) Add unit tests for NodesToIndices and IndicesToNodes.
101  // TODO(user): Remove when removal of NodeIndex from RoutingModel is
103  int num_unique_depots() const { return num_unique_depots_; }
104  std::vector<NodeIndex> GetIndexToNodeMap() const { return index_to_node_; }
106  return node_to_index_;
107  }
108 
109  private:
110  void Initialize(
111  int num_nodes, int num_vehicles,
112  const std::vector<std::pair<NodeIndex, NodeIndex> >& starts_ends);
113 
114  std::vector<NodeIndex> index_to_node_;
116  std::vector<int64_t> vehicle_to_start_;
117  std::vector<int64_t> vehicle_to_end_;
118  int num_nodes_;
119  int num_vehicles_;
120  int num_unique_depots_;
121 };
122 
123 } // namespace operations_research
124 
125 #endif // OR_TOOLS_CONSTRAINT_SOLVER_ROUTING_INDEX_MANAGER_H_
size_type size() const
Manager for any NodeIndex <-> variable index conversion.
std::vector< NodeIndex > IndicesToNodes(const std::vector< int64_t > &indices) const
std::vector< NodeIndex > GetIndexToNodeMap() const
RoutingIndexManager(int num_nodes, int num_vehicles, const std::vector< std::pair< NodeIndex, NodeIndex > > &starts_ends)
std::vector< int64_t > NodesToIndices(const std::vector< NodeIndex > &nodes) const
absl::StrongVector< NodeIndex, int64_t > GetNodeToIndexMap() 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
int index
Collection of objects used to extend the Constraint Solver library.
int nodes