From bb3ec00b0b40afc4e966c421f7ff8dc13bee2493 Mon Sep 17 00:00:00 2001 From: acco32 Date: Thu, 28 Sep 2017 22:21:20 -0700 Subject: [PATCH] Handle Integer Program. Compare to Linear Program. --- examples/fsharp/README.md | 7 ++- examples/fsharp/equality-inequality.fsx | 41 +++++++++++++ examples/fsharp/integer-linear-program.fsx | 61 +++++++++++++++++++ examples/fsharp/lib/Google.OrTools.FSharp.fsx | 7 ++- 4 files changed, 114 insertions(+), 2 deletions(-) create mode 100644 examples/fsharp/equality-inequality.fsx create mode 100644 examples/fsharp/integer-linear-program.fsx diff --git a/examples/fsharp/README.md b/examples/fsharp/README.md index c70c9d1554..4a19b71b29 100644 --- a/examples/fsharp/README.md +++ b/examples/fsharp/README.md @@ -2,8 +2,13 @@ Examples from the or-tools library utilizing F# +## SolverOptions and lpSolve +This function and parameter object are a wrapper around the standard or-tools functions. It is designed +to enter the Linear/Integer program as matrices & vectors. Two input formats are allowed: Canonical Form; Standard Form. -# Execution +*__ALL Matrices & Vectors are entered as columns__* + +## Execution Be sure to compile the or-tools before executing following ```shell fsharpc --target:exe --out:bin/.exe --platform:anycpu --lib:bin -r:Google.OrTools.dll examples/fsharp/.fsx diff --git a/examples/fsharp/equality-inequality.fsx b/examples/fsharp/equality-inequality.fsx new file mode 100644 index 0000000000..e64bb387a0 --- /dev/null +++ b/examples/fsharp/equality-inequality.fsx @@ -0,0 +1,41 @@ +(* + Minimize: -3 * x[0] + 1 * x[1] + 1 * x[2] + + Subject to: 1 * x[0] - 2 * x[1] + 1 * x[2] <= 11 + -4 * x[0] + 1 * x[1] + 2 * x[2] >= 3 + -2 * x[0] + 0 * x[1] - 1 * x[2] = -1 + + And x[0] >= 0, x[1] >= 0, x[2] >= 0 + + + Answer: + Objective: -2.000000 + var[0] : 4.000000 + var[1] : 1.000000 + var[2] : 9.000000 + var[3] : 0.000000 + var[4] : 0.000000 + + Note: When entering the matrix, it is reduced to standard form with appropriate slack variables. + +*) + +#I "./lib" +#load "Google.OrTools.FSharp.fsx" + +open System +open Google.OrTools.FSharp +open Google.OrTools.LinearSolver + +let opts = SolverOpts.Default + .Name("Equality/Inequality Constraints") + .Goal(Minimize) + .Objective([-3.0;1.0;1.0;0.0;0.0]) + .MatrixEq([[1.0;-4.0;-2.0]; [-2.0;1.0;0.0]; [1.0;2.0;1.0]; [1.0;0.0;0.0]; [0.0;-1.0;0.0]]) + .VectorEq([11.0; 3.0; 1.0]) + .Lower([0.0; 0.0; 0.0; 0.0; 0.0]) + .Upper([Double.PositiveInfinity; Double.PositiveInfinity; Double.PositiveInfinity; Double.PositiveInfinity; Double.PositiveInfinity]) + +let slvrCLP = opts.Algorithm(LP CLP) |> lpSolve |> SolverSummary +let slvrGLOP = opts.Algorithm(LP GLOP) |> lpSolve |> SolverSummary + diff --git a/examples/fsharp/integer-linear-program.fsx b/examples/fsharp/integer-linear-program.fsx new file mode 100644 index 0000000000..ed1d03a313 --- /dev/null +++ b/examples/fsharp/integer-linear-program.fsx @@ -0,0 +1,61 @@ +(* + Minimize: 6 * x[0] + 3 * x[1] + 1 * x[2] + 2 * x[3] + + Subject to: 1 * x[0] + 1 * x[1] + 1 * x[2] + 1 * x[3] <= 8 + 2 * x[0] + 1 * x[1] + 3 * x[2] + 0 * x[3] <= 12 + 0 * x[0] + 5 * x[1] + 1 * x[2] + 3 * x[3] <= 6 + + And x[0] <= 1, x[1] <= 1, x[2] <= 4, x[3] <= 2 + + + Answer: + Integer Program Solution + + Problem solved in 51 milliseconds + Iterations: 1 + + Objective: 11.000000 + var[0] : 1.000000 + var[1] : 0.000000 + var[2] : 3.000000 + var[3] : 1.000000 + Advanced usage: + Problem solved in 0 branch-and-bound nodes + + + Linear Program Solution + + Problem solved in 2 milliseconds + Iterations: 2 + + Objective: 11.111111 + var[0] : 1.000000 + var[1] : 0.000000 + var[2] : 3.333333 + var[3] : 0.888889 +*) + +#I "./lib" +#load "Google.OrTools.FSharp.fsx" + +open System +open Google.OrTools.FSharp +open Google.OrTools.LinearSolver + +let opts = SolverOpts.Default + .Goal(Maximize) + .Objective([6.;3.;1.;2.]) + .MatrixIneq([[1.;2.;0.]; [1.;1.;5.]; [1.;3.;1.]; [1.;0.;3.]]) + .VectorIneq([8.; 12.; 6.]) + .Lower([0.; 0.; 0.; 0.]) + .Upper([1.; 1.; 4.; 2.]) + + +printfn "\n\nInteger Program Solution" +let slvrIP = opts.Name("IP Solution").Algorithm(IP CBC) |> lpSolve |> SolverSummary +printfn "Advanced usage:" +printfn "Problem solved in %i branch-and-bound nodes" (slvrIP.Nodes()) + +printfn "\n\nLinear Program Solution" +let slvrLP = opts.Name("LP Solution").Algorithm(LP CLP) |> lpSolve |> SolverSummary + diff --git a/examples/fsharp/lib/Google.OrTools.FSharp.fsx b/examples/fsharp/lib/Google.OrTools.FSharp.fsx index 8075338c72..0132d80374 100644 --- a/examples/fsharp/lib/Google.OrTools.FSharp.fsx +++ b/examples/fsharp/lib/Google.OrTools.FSharp.fsx @@ -142,7 +142,12 @@ module FSharp = | _ -> () // Variables - let vars = [ for i in 0 .. (solverOptions.LowerBound.Length-1) -> solver.MakeNumVar(solverOptions.LowerBound.[i], solverOptions.UpperBound.[i], (sprintf "var[%i]" i ) ) ] + let vars = + match solverOptions.SolverAlgorithm with + | LP lp -> + [ for i in 0 .. (solverOptions.LowerBound.Length-1) -> solver.MakeNumVar(solverOptions.LowerBound.[i], solverOptions.UpperBound.[i], (sprintf "var[%i]" i ) ) ] + | IP ip -> + [ for i in 0 .. (solverOptions.LowerBound.Length-1) -> solver.MakeIntVar(solverOptions.LowerBound.[i], solverOptions.UpperBound.[i], (sprintf "var[%i]" i ) ) ] // Constraints let cols = [ for i in 0 .. (solverOptions.LowerBound.Length-1) -> i ] // generate column index selectors