Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
FlowTests.cs
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
14using System;
15using Xunit;
17
19{
20public class FlowTest
21{
22 [Fact]
23 public void testMinCostFlow()
24 {
25 int numSources = 4;
26 int numTargets = 4;
27 int[,] costs = { { 90, 75, 75, 80 }, { 35, 85, 55, 65 }, { 125, 95, 90, 105 }, { 45, 110, 95, 115 } };
28 int expectedCost = 275;
29 MinCostFlow minCostFlow = new MinCostFlow();
30 Assert.NotNull(minCostFlow);
31 for (int source = 0; source < numSources; ++source)
32 {
33 for (int target = 0; target < numTargets; ++target)
34 {
35 minCostFlow.AddArcWithCapacityAndUnitCost(source, numSources + target, 1, costs[source, target]);
36 }
37 }
38
39 for (int source = 0; source < numSources; ++source)
40 {
41 minCostFlow.SetNodeSupply(source, 1);
42 }
43 for (int target = 0; target < numTargets; ++target)
44 {
45 minCostFlow.SetNodeSupply(numSources + target, -1);
46 }
47 MinCostFlowBase.Status solveStatus = minCostFlow.Solve();
48 Assert.Equal(solveStatus, MinCostFlow.Status.OPTIMAL);
49 long totalFlowCost = minCostFlow.OptimalCost();
50 Assert.Equal(expectedCost, totalFlowCost);
51 }
52
53 [Fact]
54 public void testMaxFlow()
55 {
56 int numNodes = 6;
57 int numArcs = 9;
58 int[] tails = { 0, 0, 0, 0, 1, 2, 3, 3, 4 };
59 int[] heads = { 1, 2, 3, 4, 3, 4, 4, 5, 5 };
60 int[] capacities = { 5, 8, 2, 0, 4, 5, 6, 0, 4 };
61 int[] expectedFlows = { 4, 4, 2, 0, 4, 4, 0, 6, 4 };
62 int expectedTotalFlow = 10;
63 MaxFlow maxFlow = new MaxFlow();
64 Assert.NotNull(maxFlow);
65 for (int i = 0; i < numArcs; ++i)
66 {
67 maxFlow.AddArcWithCapacity(tails[i], heads[i], capacities[i]);
68 }
69 maxFlow.SetArcCapacity(7, 6);
70 MaxFlow.Status solveStatus = maxFlow.Solve(/*source=*/0, /*sink=*/numNodes - 1);
71 Assert.Equal(solveStatus, MaxFlow.Status.OPTIMAL);
72
73 long totalFlow = maxFlow.OptimalFlow();
74 Assert.Equal(expectedTotalFlow, totalFlow);
75
76 Assert.Equal(numArcs, maxFlow.NumArcs());
77 for (int i = 0; i < numArcs; ++i)
78 {
79 Assert.Equal(maxFlow.Flow(i), expectedFlows[i]);
80 }
81 }
82}
83} // namespace Google.OrTools.Tests
int AddArcWithCapacity(int tail, int head, long capacity)
Definition MaxFlow.cs:65
void SetArcCapacity(int arc, long capacity)
Definition MaxFlow.cs:110
MaxFlow.Status Solve(int source, int sink)
Definition MaxFlow.cs:95
MinCostFlowBase.Status Solve()
void SetNodeSupply(int node, long supply)
int AddArcWithCapacityAndUnitCost(int tail, int head, long capacity, long unit_cost)