Java Reference

Java Reference

FlowTest.java
Go to the documentation of this file.
1 // Copyright 2010-2022 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 
14 package com.google.ortools.graph;
15 
16 import static org.junit.jupiter.api.Assertions.assertEquals;
17 import static org.junit.jupiter.api.Assertions.assertNotNull;
18 
19 import com.google.ortools.Loader;
20 import org.junit.jupiter.api.BeforeEach;
21 import org.junit.jupiter.api.Test;
22 
24 public final class FlowTest {
25  @BeforeEach
26  public void setUp() {
28  }
29 
30  @Test
31  public void testMinCostFlow() {
32  final int numSources = 4;
33  final int numTargets = 4;
34  final int[][] costs = {
35  {90, 75, 75, 80}, {35, 85, 55, 65}, {125, 95, 90, 105}, {45, 110, 95, 115}};
36  final int expectedCost = 275;
37  final MinCostFlow minCostFlow = new MinCostFlow();
38  assertNotNull(minCostFlow);
39  for (int source = 0; source < numSources; ++source) {
40  for (int target = 0; target < numTargets; ++target) {
41  minCostFlow.addArcWithCapacityAndUnitCost(
42  source, numSources + target, 1, costs[source][target]);
43  }
44  }
45 
46  for (int source = 0; source < numSources; ++source) {
47  minCostFlow.setNodeSupply(source, 1);
48  }
49  for (int target = 0; target < numTargets; ++target) {
50  minCostFlow.setNodeSupply(numSources + target, -1);
51  }
52  final MinCostFlowBase.Status solveStatus = minCostFlow.solve();
53  assertEquals(solveStatus, MinCostFlow.Status.OPTIMAL);
54  final long totalFlowCost = minCostFlow.getOptimalCost();
55  assertEquals(expectedCost, totalFlowCost);
56  }
57 
58  @Test
59  public void testMaxFlow() {
60  final int numNodes = 6;
61  final int numArcs = 9;
62  final int[] tails = {0, 0, 0, 0, 1, 2, 3, 3, 4};
63  final int[] heads = {1, 2, 3, 4, 3, 4, 4, 5, 5};
64  final int[] capacities = {5, 8, 2, 0, 4, 5, 6, 0, 4};
65  final int[] expectedFlows = {4, 4, 2, 0, 4, 4, 0, 6, 4};
66  final int expectedTotalFlow = 10;
67  final MaxFlow maxFlow = new MaxFlow();
68  assertNotNull(maxFlow);
69  for (int i = 0; i < numArcs; ++i) {
70  maxFlow.addArcWithCapacity(tails[i], heads[i], capacities[i]);
71  }
72  maxFlow.setArcCapacity(7, 6);
73  final MaxFlow.Status solveStatus = maxFlow.solve(/*source=*/0, /*sink=*/numNodes - 1);
74  assertEquals(solveStatus, MaxFlow.Status.OPTIMAL);
75  final long totalFlow = maxFlow.getOptimalFlow();
76  assertEquals(expectedTotalFlow, totalFlow);
77  for (int i = 0; i < numArcs; ++i) {
78  assertEquals(maxFlow.getFlow(i), expectedFlows[i]);
79  }
80  }
81 }
Load native libraries needed for using ortools-java.
Definition: Loader.java:33
static synchronized void loadNativeLibraries()
Definition: Loader.java:104
Test the Min/Max Flow solver java interface.
Definition: FlowTest.java:24