Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
dag_constrained_shortest_path.cc
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
15
16#include <vector>
17
18#include "absl/log/check.h"
19#include "absl/status/statusor.h"
20#include "absl/types/span.h"
22#include "ortools/graph/graph.h"
24
25namespace operations_research {
26
27namespace {
28
29void ApplyMapping(absl::Span<const int> mapping, std::vector<int>& values) {
30 if (!mapping.empty()) {
31 for (int i = 0; i < values.size(); ++i) {
32 values[i] = mapping[values[i]];
33 }
34 }
35}
36
37} // namespace
38
40 const int num_nodes,
41 absl::Span<const ArcWithLengthAndResources> arcs_with_length_and_resources,
42 int source, int destination, const std::vector<double>& max_resources) {
43 using GraphType = util::StaticGraph<>;
44 using NodeIndex = GraphType::NodeIndex;
45 using ArcIndex = GraphType::ArcIndex;
46
47 const int num_arcs = arcs_with_length_and_resources.size();
48 GraphType graph(num_nodes, num_arcs);
49 std::vector<double> arc_lengths;
50 arc_lengths.reserve(num_arcs);
51 std::vector<std::vector<double>> arc_resources(max_resources.size());
52 for (int i = 0; i < max_resources.size(); ++i) {
53 arc_resources[i].reserve(num_arcs);
54 }
55 for (const auto& arc : arcs_with_length_and_resources) {
56 graph.AddArc(arc.from, arc.to);
57 arc_lengths.push_back(arc.length);
58 for (int i = 0; i < arc.resources.size(); ++i) {
59 arc_resources[i].push_back(arc.resources[i]);
60 }
61 }
62
63 std::vector<ArcIndex> permutation;
64 graph.Build(&permutation);
65 util::Permute(permutation, &arc_lengths);
66 for (int i = 0; i < max_resources.size(); ++i) {
67 util::Permute(permutation, &arc_resources[i]);
68 }
69
70 std::vector<ArcIndex> inverse_permutation =
71 GetInversePermutation(permutation);
72
73 const absl::StatusOr<std::vector<NodeIndex>> topological_order =
75 CHECK_OK(topological_order) << "arcs_with_length form a cycle.";
76
77 std::vector<NodeIndex> sources = {source};
78 std::vector<NodeIndex> destinations = {destination};
80 constrained_shortest_path_on_dag(&graph, &arc_lengths, &arc_resources,
81 *topological_order, sources,
82 destinations, &max_resources);
83
84 PathWithLength path_with_length =
85 constrained_shortest_path_on_dag.RunConstrainedShortestPathOnDag();
86
87 ApplyMapping(inverse_permutation, path_with_length.arc_path);
88
89 return path_with_length;
90}
91
92std::vector<int> GetInversePermutation(absl::Span<const int> permutation) {
93 std::vector<int> inverse_permutation(permutation.size());
94 if (!permutation.empty()) {
95 for (int i = 0; i < permutation.size(); ++i) {
96 inverse_permutation[permutation[i]] = i;
97 }
98 }
99 return inverse_permutation;
100}
101
102} // namespace operations_research
NodeIndexType size() const
Definition graph.h:212
std::vector< NodeIndex > topological_order
std::vector< double > arc_lengths
GraphType graph
int arc
In SWIG mode, we don't want anything besides these top-level includes.
std::vector< int > GetInversePermutation(absl::Span< const int > permutation)
PathWithLength ConstrainedShortestPathsOnDag(const int num_nodes, absl::Span< const ArcWithLengthAndResources > arcs_with_length_and_resources, int source, int destination, const std::vector< double > &max_resources)
absl::StatusOr< std::vector< int > > FastTopologicalSort(const AdjacencyLists &adj)
void Permute(const IntVector &permutation, Array *array_to_permute)
Definition graph.h:758