Google OR-Tools v9.11
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-2024 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
20#include "absl/log/check.h"
23#include "ortools/base/types.h"
25
26namespace operations_research {
27
50class RoutingIndexManager {
51 public:
52 typedef RoutingNodeIndex NodeIndex;
53 static const int64_t kUnassigned;
61 const std::vector<NodeIndex>& starts,
62 const std::vector<NodeIndex>& ends);
64 int num_nodes, int num_vehicles,
65 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_; }
105
106 private:
107 void Initialize(
109 const std::vector<std::pair<NodeIndex, NodeIndex> >& starts_ends);
110
111 std::vector<NodeIndex> index_to_node_;
113 std::vector<int64_t> vehicle_to_start_;
114 std::vector<int64_t> vehicle_to_end_;
115 int num_nodes_;
116 int num_vehicles_;
117 int num_unique_depots_;
118};
119
120} // namespace operations_research
121
122#endif // OR_TOOLS_CONSTRAINT_SOLVER_ROUTING_INDEX_MANAGER_H_
std::vector< NodeIndex > GetIndexToNodeMap() const
int num_indices() const
Returns the number of indices mapped to nodes.
std::vector< NodeIndex > IndicesToNodes(const std::vector< int64_t > &indices) const
Same as IndexToNode but for a given vector of indices.
RoutingIndexManager(int num_nodes, int num_vehicles, NodeIndex depot)
int num_nodes() const
Returns the number of nodes in the manager.
int num_vehicles() const
Returns the number of vehicles in the manager.
std::vector< int64_t > NodesToIndices(const std::vector< NodeIndex > &nodes) const
Same as NodeToIndex but for a given vector of nodes.
NodeIndex IndexToNode(int64_t index) const
int64_t GetStartIndex(int vehicle) const
Returns start and end indices of the given vehicle.
int index
In SWIG mode, we don't want anything besides these top-level includes.
int nodes