Files
ortools-clone/examples/cpp/min_cost_flow.cc

83 lines
3.0 KiB
C++
Raw Normal View History

2025-01-10 11:35:44 +01:00
// Copyright 2010-2025 Google LLC
// 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.
#include "ortools/graph/min_cost_flow.h"
2020-09-23 11:45:03 +02:00
2025-02-25 16:03:40 +01:00
#include <cstdlib>
#include <utility>
#include <vector>
2025-02-25 16:03:40 +01:00
#include "absl/base/log_severity.h"
#include "absl/log/globals.h"
2025-03-04 21:10:09 +01:00
#include "absl/log/log.h"
2023-01-31 20:46:43 +01:00
#include "ortools/base/init_google.h"
namespace operations_research {
2018-11-22 13:15:13 +01:00
struct Arc {
std::pair<SimpleMinCostFlow::NodeIndex, SimpleMinCostFlow::NodeIndex> nodes;
SimpleMinCostFlow::FlowQuantity capacity;
SimpleMinCostFlow::FlowQuantity unit_cost;
2018-11-22 13:15:13 +01:00
};
2018-11-22 13:15:13 +01:00
void SolveMinCostFlow() {
// Define supply of each node.
const std::vector<
std::pair<SimpleMinCostFlow::NodeIndex, SimpleMinCostFlow::FlowQuantity> >
supplies = {{0, 20}, {1, 0}, {2, 0}, {3, -5}, {4, -15}};
2018-11-22 13:15:13 +01:00
// Define 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-22 23:36:58 +02:00
const std::vector<Arc> arcs = {
{{0, 1}, 15, 4}, {{0, 2}, 8, 4}, {{1, 2}, 20, 2},
{{1, 3}, 4, 2}, {{1, 4}, 10, 6}, {{2, 3}, 15, 1},
{{2, 4}, 4, 3}, {{3, 4}, 20, 2}, {{4, 2}, 5, 3}};
2024-12-29 19:21:42 +01:00
SimpleMinCostFlow min_cost_flow;
2020-10-29 14:25:39 +01:00
for (const auto& it : arcs) {
2024-12-29 19:21:42 +01:00
min_cost_flow.AddArcWithCapacityAndUnitCost(it.nodes.first, it.nodes.second,
it.capacity, it.unit_cost);
2018-11-22 13:15:13 +01:00
}
2020-10-29 14:25:39 +01:00
for (const auto& it : supplies) {
2018-11-22 13:15:13 +01:00
min_cost_flow.SetNodeSupply(it.first, it.second);
}
2024-12-29 19:21:42 +01:00
LOG(INFO) << "Solving min cost flow with: " << min_cost_flow.NumNodes()
<< " nodes, and " << min_cost_flow.NumArcs() << " arcs.";
2018-11-22 13:15:13 +01:00
// Find the maximum flow between node 0 and node 4.
2024-12-29 19:21:42 +01:00
const auto status = min_cost_flow.Solve();
if (status != SimpleMinCostFlow::OPTIMAL) {
2018-11-22 13:15:13 +01:00
LOG(FATAL) << "Solving the max flow is not optimal!";
}
SimpleMinCostFlow::FlowQuantity total_flow_cost = min_cost_flow.OptimalCost();
2018-11-22 13:15:13 +01:00
LOG(INFO) << "Minimum cost flow: " << total_flow_cost;
LOG(INFO) << "";
LOG(INFO) << "Arc : Flow / Capacity / Cost";
for (int i = 0; i < arcs.size(); ++i) {
2024-12-29 19:21:42 +01:00
LOG(INFO) << min_cost_flow.Tail(i) << " -> " << min_cost_flow.Head(i)
<< ": " << min_cost_flow.Flow(i) << " / "
<< min_cost_flow.Capacity(i) << " / "
<< min_cost_flow.UnitCost(i);
}
2018-11-22 13:15:13 +01:00
}
2020-10-22 23:36:58 +02:00
} // namespace operations_research
2020-10-29 14:25:39 +01:00
int main(int argc, char** argv) {
2025-02-25 16:03:40 +01:00
absl::SetStderrThreshold(absl::LogSeverityAtLeast::kInfo);
InitGoogle(argv[0], &argc, &argv, true);
operations_research::SolveMinCostFlow();
2018-11-07 09:52:37 +01:00
return EXIT_SUCCESS;
}