2025-01-10 11:35:44 +01:00
|
|
|
// Copyright 2010-2025 Google LLC
|
2018-10-31 16:18:18 +01:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
//
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
//
|
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
2025-11-05 11:34:49 +01:00
|
|
|
#ifndef ORTOOLS_CONSTRAINT_SOLVER_ROUTING_INDEX_MANAGER_H_
|
|
|
|
|
#define ORTOOLS_CONSTRAINT_SOLVER_ROUTING_INDEX_MANAGER_H_
|
2018-10-31 16:18:18 +01:00
|
|
|
|
2024-11-18 11:32:13 +01:00
|
|
|
#include <cstdint>
|
2021-04-01 12:13:35 +02:00
|
|
|
#include <utility>
|
2018-10-31 16:18:18 +01:00
|
|
|
#include <vector>
|
|
|
|
|
|
2024-01-19 17:57:04 +01:00
|
|
|
#include "absl/log/check.h"
|
2024-11-18 11:32:13 +01:00
|
|
|
#include "absl/types/span.h"
|
2025-07-23 15:07:49 +02:00
|
|
|
#include "ortools/base/base_export.h"
|
2020-11-19 00:17:26 +01:00
|
|
|
#include "ortools/base/strong_vector.h"
|
2018-10-31 16:18:18 +01:00
|
|
|
#include "ortools/constraint_solver/routing_types.h"
|
2025-12-09 22:50:14 +01:00
|
|
|
|
2018-10-31 16:18:18 +01:00
|
|
|
namespace operations_research {
|
|
|
|
|
|
2025-11-07 18:31:07 +01:00
|
|
|
/// @brief Manager for any NodeIndex <-> variable index conversion.
|
|
|
|
|
/// @details The routing solver uses variable indices internally and through
|
|
|
|
|
/// its API.
|
|
|
|
|
/// These variable indices are tricky to manage directly because one Node can
|
|
|
|
|
/// correspond to a multitude of variables, depending on the number of times
|
|
|
|
|
/// they appear in the model, and if they're used as start and/or end points.
|
|
|
|
|
/// This class aims to simplify variable index usage, allowing users to use
|
|
|
|
|
/// NodeIndex instead.
|
2019-07-05 00:25:55 +02:00
|
|
|
///
|
|
|
|
|
/// Usage:
|
2019-07-05 09:32:02 +02:00
|
|
|
/// \code{.cpp}
|
2019-07-05 00:25:55 +02:00
|
|
|
/// auto starts_ends = ...; /// These are NodeIndex.
|
|
|
|
|
/// RoutingIndexManager manager(10, 4, starts_ends); // 10 nodes, 4 vehicles.
|
|
|
|
|
/// RoutingModel model(manager);
|
2019-07-05 09:32:02 +02:00
|
|
|
/// \endcode
|
2019-07-05 00:25:55 +02:00
|
|
|
///
|
2019-07-05 09:32:02 +02:00
|
|
|
/// Then, use 'manager.NodeToIndex(node)' whenever model requires a variable
|
2019-07-05 00:25:55 +02:00
|
|
|
/// index.
|
2020-08-10 10:55:34 +02:00
|
|
|
///
|
|
|
|
|
/// Note: the mapping between node indices and variables indices is subject to
|
|
|
|
|
/// change so no assumption should be made on it. The only guarantee is that
|
|
|
|
|
/// indices range between 0 and n-1, where n = number of vehicles * 2 (for start
|
|
|
|
|
/// and end nodes) + number of non-start or end nodes.
|
|
|
|
|
///
|
2025-01-27 02:01:03 -08:00
|
|
|
class OR_DLL RoutingIndexManager {
|
2020-10-22 23:36:58 +02:00
|
|
|
public:
|
2018-10-31 16:18:18 +01:00
|
|
|
typedef RoutingNodeIndex NodeIndex;
|
2021-04-01 12:13:35 +02:00
|
|
|
static const int64_t kUnassigned;
|
2018-10-31 16:18:18 +01:00
|
|
|
|
2025-11-07 18:31:07 +01:00
|
|
|
/// @brief Creates a NodeIndex to variable index mapping for a problem
|
|
|
|
|
/// containing 'num_nodes', 'num_vehicles' and the given starts and ends for
|
|
|
|
|
/// each vehicle. If used, any start/end arrays have to have exactly
|
|
|
|
|
/// 'num_vehicles' elements.
|
|
|
|
|
/// @param num_nodes Number of nodes in the problem.
|
|
|
|
|
/// @param num_vehicles Number of vehicles in the problem.
|
|
|
|
|
/// @param depot @ref GetStartIndex "start" and @ref GetEndIndex "end"
|
|
|
|
|
/// NodeIndex for all vehicles.
|
2018-10-31 16:18:18 +01:00
|
|
|
RoutingIndexManager(int num_nodes, int num_vehicles, NodeIndex depot);
|
2025-11-07 18:31:07 +01:00
|
|
|
/// @brief Creates a NodeIndex to variable index mapping.
|
|
|
|
|
/// @param num_nodes Number of nodes in the problem.
|
|
|
|
|
/// @param num_vehicles Number of vehicles in the problem.
|
|
|
|
|
/// @param starts Array containing the start NodeIndex for each vehicle.
|
|
|
|
|
/// @param ends Array containing the end NodeIndex for each vehicle.
|
|
|
|
|
/// @note @b starts and @b ends arrays must have @b exactly @ref num_vehicles
|
|
|
|
|
/// elements.
|
2018-10-31 16:18:18 +01:00
|
|
|
RoutingIndexManager(int num_nodes, int num_vehicles,
|
2020-10-29 14:25:39 +01:00
|
|
|
const std::vector<NodeIndex>& starts,
|
|
|
|
|
const std::vector<NodeIndex>& ends);
|
2025-11-07 18:31:07 +01:00
|
|
|
/// @brief Creates a NodeIndex to variable index mapping.
|
|
|
|
|
/// @param num_nodes Number of nodes in the problem.
|
|
|
|
|
/// @param num_vehicles Number of vehicles in the problem.
|
|
|
|
|
/// @param starts_ends Array containing a pair [start,end] NodeIndex for each
|
|
|
|
|
/// vehicle.
|
|
|
|
|
/// @note @b starts_ends arrays must have @b exactly @ref num_vehicles
|
|
|
|
|
/// elements.
|
2018-10-31 16:18:18 +01:00
|
|
|
RoutingIndexManager(
|
|
|
|
|
int num_nodes, int num_vehicles,
|
2020-10-29 14:25:39 +01:00
|
|
|
const std::vector<std::pair<NodeIndex, NodeIndex> >& starts_ends);
|
2018-10-31 16:18:18 +01:00
|
|
|
|
2020-08-10 10:55:34 +02:00
|
|
|
// Returns the number of nodes in the manager.
|
2018-10-31 16:18:18 +01:00
|
|
|
int num_nodes() const { return num_nodes_; }
|
2020-08-10 10:55:34 +02:00
|
|
|
// Returns the number of vehicles in the manager.
|
2018-10-31 16:18:18 +01:00
|
|
|
int num_vehicles() const { return num_vehicles_; }
|
2020-08-10 10:55:34 +02:00
|
|
|
// Returns the number of indices mapped to nodes.
|
2018-10-31 16:18:18 +01:00
|
|
|
int num_indices() const { return index_to_node_.size(); }
|
2020-08-10 10:55:34 +02:00
|
|
|
// Returns start and end indices of the given vehicle.
|
2021-04-11 11:29:32 +02:00
|
|
|
int64_t GetStartIndex(int vehicle) const {
|
|
|
|
|
return vehicle_to_start_[vehicle];
|
|
|
|
|
}
|
2021-04-01 12:13:35 +02:00
|
|
|
int64_t GetEndIndex(int vehicle) const { return vehicle_to_end_[vehicle]; }
|
2020-08-10 10:55:34 +02:00
|
|
|
// Returns the index of a node. A node can correspond to multiple indices if
|
|
|
|
|
// it's a start or end node. As of 03/2020, kUnassigned will be returned for
|
|
|
|
|
// all end nodes. If a node appears more than once as a start node, the index
|
|
|
|
|
// of the first node in the list of start nodes is returned.
|
2021-04-01 12:13:35 +02:00
|
|
|
int64_t NodeToIndex(NodeIndex node) const {
|
2018-10-31 16:18:18 +01:00
|
|
|
DCHECK_GE(node.value(), 0);
|
|
|
|
|
DCHECK_LT(node.value(), node_to_index_.size());
|
|
|
|
|
return node_to_index_[node];
|
|
|
|
|
}
|
2020-08-10 10:55:34 +02:00
|
|
|
// Same as NodeToIndex but for a given vector of nodes.
|
2021-04-11 11:29:32 +02:00
|
|
|
std::vector<int64_t> NodesToIndices(
|
|
|
|
|
const std::vector<NodeIndex>& nodes) const;
|
2020-08-10 10:55:34 +02:00
|
|
|
// Returns the node corresponding to an index. A node may appear more than
|
|
|
|
|
// once if it is used as the start or the end node of multiple vehicles.
|
2021-04-01 12:13:35 +02:00
|
|
|
NodeIndex IndexToNode(int64_t index) const {
|
2018-10-31 16:18:18 +01:00
|
|
|
DCHECK_GE(index, 0);
|
|
|
|
|
DCHECK_LT(index, index_to_node_.size());
|
|
|
|
|
return index_to_node_[index];
|
|
|
|
|
}
|
2020-08-10 10:55:34 +02:00
|
|
|
// Same as IndexToNode but for a given vector of indices.
|
2020-10-22 23:36:58 +02:00
|
|
|
std::vector<NodeIndex> IndicesToNodes(
|
2024-11-18 11:32:13 +01:00
|
|
|
absl::Span<const int64_t> indices) const;
|
2020-08-10 10:55:34 +02:00
|
|
|
// TODO(user) Add unit tests for NodesToIndices and IndicesToNodes.
|
2018-10-31 16:18:18 +01:00
|
|
|
// TODO(user): Remove when removal of NodeIndex from RoutingModel is
|
2025-11-07 18:31:07 +01:00
|
|
|
// complete.
|
2018-10-31 16:18:18 +01:00
|
|
|
int num_unique_depots() const { return num_unique_depots_; }
|
|
|
|
|
std::vector<NodeIndex> GetIndexToNodeMap() const { return index_to_node_; }
|
|
|
|
|
|
2020-10-22 23:36:58 +02:00
|
|
|
private:
|
2018-10-31 16:18:18 +01:00
|
|
|
void Initialize(
|
|
|
|
|
int num_nodes, int num_vehicles,
|
2020-10-29 14:25:39 +01:00
|
|
|
const std::vector<std::pair<NodeIndex, NodeIndex> >& starts_ends);
|
2018-10-31 16:18:18 +01:00
|
|
|
|
|
|
|
|
std::vector<NodeIndex> index_to_node_;
|
2024-07-12 13:56:11 +02:00
|
|
|
util_intops::StrongVector<NodeIndex, int64_t> node_to_index_;
|
2021-04-01 12:13:35 +02:00
|
|
|
std::vector<int64_t> vehicle_to_start_;
|
|
|
|
|
std::vector<int64_t> vehicle_to_end_;
|
2018-10-31 16:18:18 +01:00
|
|
|
int num_nodes_;
|
|
|
|
|
int num_vehicles_;
|
|
|
|
|
int num_unique_depots_;
|
|
|
|
|
};
|
|
|
|
|
|
2020-10-22 23:36:58 +02:00
|
|
|
} // namespace operations_research
|
2018-10-31 16:18:18 +01:00
|
|
|
|
2025-11-05 11:34:49 +01:00
|
|
|
#endif // ORTOOLS_CONSTRAINT_SOLVER_ROUTING_INDEX_MANAGER_H_
|