2025-01-10 11:33:35 +01:00
|
|
|
// Copyright 2010-2025 Google LLC
|
2018-09-26 11:02:04 +02: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-02-21 15:14:38 +01:00
|
|
|
#include <cstdlib>
|
2023-06-21 17:31:06 +02:00
|
|
|
#include <utility>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
2025-02-19 17:00:34 +01:00
|
|
|
#include "absl/log/globals.h"
|
2025-02-27 09:38:35 +01:00
|
|
|
#include "absl/log/log.h"
|
2023-01-31 20:46:43 +01:00
|
|
|
#include "ortools/base/init_google.h"
|
2026-02-20 16:40:19 +01:00
|
|
|
#include "ortools/base/log_severity.h"
|
2024-12-29 19:21:42 +01:00
|
|
|
#include "ortools/graph/generic_max_flow.h"
|
2026-01-20 11:52:16 +01:00
|
|
|
#include "ortools/graph_base/graph.h"
|
2018-09-26 11:02:04 +02:00
|
|
|
|
|
|
|
|
namespace operations_research {
|
2024-12-29 19:21:42 +01:00
|
|
|
|
|
|
|
|
using Graph = ::util::ReverseArcListGraph<>;
|
|
|
|
|
using NodeIndex = Graph::NodeIndex;
|
|
|
|
|
using ArcIndex = Graph::ArcIndex;
|
|
|
|
|
using MaxFlowT = GenericMaxFlow<Graph>;
|
2025-01-20 14:58:09 +01:00
|
|
|
using FlowQuantity = MaxFlowT::FlowSumType;
|
2024-12-29 19:21:42 +01:00
|
|
|
|
2018-11-22 13:15:13 +01:00
|
|
|
void SolveMaxFlow() {
|
|
|
|
|
const int num_nodes = 5;
|
|
|
|
|
// Add each arc
|
|
|
|
|
// Can't use std::tuple<NodeIndex, NodeIndex, FlowQuantity>
|
|
|
|
|
// Initialization list is not working on std:tuple cf. N4387
|
|
|
|
|
// Arc are stored as {{begin_node, end_node}, capacity}
|
2020-10-21 00:21:54 +02:00
|
|
|
std::vector<std::pair<std::pair<NodeIndex, NodeIndex>, FlowQuantity> > arcs =
|
2020-10-22 23:36:58 +02:00
|
|
|
{{{0, 1}, 20}, {{0, 2}, 30}, {{0, 3}, 10}, {{1, 2}, 40}, {{1, 4}, 30},
|
|
|
|
|
{{2, 3}, 10}, {{2, 4}, 20}, {{3, 2}, 5}, {{3, 4}, 20}};
|
2024-12-29 19:21:42 +01:00
|
|
|
Graph graph(num_nodes, arcs.size());
|
|
|
|
|
MaxFlowT max_flow(&graph, 0, num_nodes - 1);
|
2020-10-29 14:25:39 +01:00
|
|
|
for (const auto& it : arcs) {
|
2018-11-22 13:15:13 +01:00
|
|
|
ArcIndex arc = graph.AddArc(it.first.first, it.first.second);
|
|
|
|
|
max_flow.SetArcCapacity(arc, it.second);
|
|
|
|
|
}
|
2018-09-26 11:02:04 +02:00
|
|
|
|
2018-11-22 13:15:13 +01:00
|
|
|
LOG(INFO) << "Solving max flow with: " << graph.num_nodes() << " nodes, and "
|
|
|
|
|
<< graph.num_arcs() << " arcs.";
|
2018-09-26 11:02:04 +02:00
|
|
|
|
2018-11-22 13:15:13 +01:00
|
|
|
// Find the maximum flow between node 0 and node 4.
|
|
|
|
|
max_flow.Solve();
|
2024-12-29 19:21:42 +01:00
|
|
|
if (MaxFlowStatusClass::OPTIMAL != max_flow.status()) {
|
2018-11-22 13:15:13 +01:00
|
|
|
LOG(FATAL) << "Solving the max flow is not optimal!";
|
2018-09-26 11:02:04 +02:00
|
|
|
}
|
2018-11-22 13:15:13 +01:00
|
|
|
FlowQuantity total_flow = max_flow.GetOptimalFlow();
|
|
|
|
|
LOG(INFO) << "Maximum flow: " << total_flow;
|
|
|
|
|
LOG(INFO) << "";
|
|
|
|
|
LOG(INFO) << " Arc : Flow / Capacity";
|
|
|
|
|
for (int i = 0; i < arcs.size(); ++i) {
|
|
|
|
|
LOG(INFO) << graph.Tail(i) << " -> " << graph.Head(i) << ": "
|
|
|
|
|
<< max_flow.Flow(i) << " / " << max_flow.Capacity(i);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-10-22 23:36:58 +02:00
|
|
|
} // namespace operations_research
|
2018-09-26 11:02:04 +02:00
|
|
|
|
2020-10-29 14:25:39 +01:00
|
|
|
int main(int argc, char** argv) {
|
2025-02-19 17:00:34 +01:00
|
|
|
absl::SetStderrThreshold(absl::LogSeverityAtLeast::kInfo);
|
2023-06-21 17:31:06 +02:00
|
|
|
InitGoogle(argv[0], &argc, &argv, true);
|
2018-09-26 11:02:04 +02:00
|
|
|
operations_research::SolveMaxFlow();
|
2018-11-07 09:52:37 +01:00
|
|
|
return EXIT_SUCCESS;
|
2018-09-26 11:02:04 +02:00
|
|
|
}
|