forked from google/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
107 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#I "./lib" | ||
#load "Google.OrTools.FSharp.fsx" | ||
|
||
open Google.OrTools.Graph | ||
open Google.OrTools.FSharp | ||
|
||
printfn "Max Flow Problem" | ||
let numNodes = 6; | ||
let numArcs = 9; | ||
let tails = [0; 0; 0; 0; 1; 2; 3; 3; 4] | ||
let heads = [1; 2; 3; 4; 3; 4; 4; 5; 5] | ||
let capacities = [5L; 8L; 5L; 3L; 4L; 5L; 6L; 6L; 4L] | ||
let expectedFlows = [4; 4; 2; 0; 4; 4; 0; 6; 4] | ||
let expectedTotalFlow = 10; | ||
let maxFlow = new MaxFlow() | ||
|
||
for i=0 to (numArcs-1) do | ||
let arc = maxFlow.AddArcWithCapacity(tails.[i], heads.[i], capacities.[i]) | ||
if (arc <> i) then | ||
failwith "Internal error" | ||
|
||
let source = 0; | ||
let sink = numNodes - 1; | ||
printfn "Solving max flow with %i nodes, and %i arcs, source=%i, sink=%i " numNodes numArcs source sink | ||
let solveStatus = maxFlow.Solve(source, sink) | ||
|
||
match solveStatus with | ||
| MaximumFlow.Optimal -> | ||
let totalFlow = maxFlow.OptimalFlow(); | ||
printfn "total computed flow %i, expected = %i" totalFlow expectedTotalFlow | ||
|
||
for i=1 to numArcs do | ||
printfn "Arc %i (%i -> %i), capacity=%i, computed=%i, expected=%i" i (maxFlow.Head(i-1)) (maxFlow.Tail(i-1)) (maxFlow.Capacity(i-1)) (maxFlow.Flow(i-1)) (expectedFlows.[i-1]) | ||
| _ -> | ||
printfn "Solving the max flow problem failed. Solver status: %i" solveStatus | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#I "./lib" | ||
#load "Google.OrTools.FSharp.fsx" | ||
|
||
open Google.OrTools.Graph | ||
open Google.OrTools.FSharp | ||
|
||
printfn "Min Cost Flow Problem" | ||
let numSources = 4 | ||
let numTargets = 4 | ||
let costs = array2D [ | ||
[90L; 75L; 75L; 80L]; | ||
[35L; 85L; 55L; 65L]; | ||
[125L; 95L; 90L; 105L]; | ||
[45L; 110L; 95L; 115L] | ||
] | ||
|
||
let expectedCost = 275 | ||
let minCostFlow = new MinCostFlow() | ||
|
||
for source=0 to (numSources-1) do | ||
for target=0 to (numTargets-1) do | ||
minCostFlow.AddArcWithCapacityAndUnitCost(source, numSources + target, 1L, costs.[source, target]) |> ignore | ||
|
||
for source=0 to (numSources-1) do | ||
minCostFlow.SetNodeSupply(source, 1L) | ||
|
||
for target=0 to (numTargets-1) do | ||
minCostFlow.SetNodeSupply(numSources + target, -1L); | ||
|
||
printfn "Solving min cost flow with %i sources, and %i targets." numSources numTargets | ||
let solveStatus = minCostFlow.Solve(); | ||
|
||
match solveStatus with | ||
| MinimumCostFlow.Optimal -> | ||
printfn "Total computed flow cost = %i, expected = %i" (minCostFlow.OptimalCost()) expectedCost | ||
| _ -> | ||
printfn "Solving the min cost flow problem failed. Solver status: %i" solveStatus | ||
|