Skip to content

Commit

Permalink
Adding Network examples
Browse files Browse the repository at this point in the history
  • Loading branch information
acco32 committed Dec 22, 2017
1 parent c8fde82 commit 53860de
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 1 deletion.
2 changes: 1 addition & 1 deletion examples/fsharp/equality.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

open System
open Google.OrTools.FSharp
open Google.OrTools.LinearSolver

let opts = SolverOpts.Default
.Name("Equality Constraints")
Expand All @@ -36,3 +35,4 @@ let opts = SolverOpts.Default
.Algorithm(LP CLP)

let slvr = opts |> lpSolve |> SolverSummary
slvr |> ignore
31 changes: 31 additions & 0 deletions examples/fsharp/lib/Google.OrTools.FSharp.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,37 @@ type LinearSolverAlgorithm =
| LP of LinearProgramming
| IP of IntegerProgramming

/// Max Flow Solver Result
module MaximumFlow =
let (|Optimal|IntOverflow|BadInput|BadResult|) status =
match status with
| 0 ->
Optimal
| 1 ->
IntOverflow
| 2 ->
BadInput
| 3 ->
BadResult

/// Minimum Cost Flow Result
module MinimumCostFlow =
let (|NotSolved|Optimal|Feasible|Infeasible|Unbalanced|BadResult|BadCostRange|) status =
match status with
| 0 ->
NotSolved
| 1 ->
Optimal
| 2 ->
Feasible
| 3 ->
Infeasible
| 4 ->
Unbalanced
| 5 ->
BadResult
| 6 ->
BadCostRange

type SolverOpts = {
/// Name of the solver
Expand Down
37 changes: 37 additions & 0 deletions examples/fsharp/network-max-flow.fsx
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


38 changes: 38 additions & 0 deletions examples/fsharp/network-min-cost-flow.fsx
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

0 comments on commit 53860de

Please sign in to comment.