Handle Integer Program. Compare to Linear Program.

This commit is contained in:
acco32
2017-09-28 22:21:20 -07:00
parent 3435a23fed
commit bb3ec00b0b
4 changed files with 114 additions and 2 deletions

View File

@@ -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/<example_file>.exe --platform:anycpu --lib:bin -r:Google.OrTools.dll examples/fsharp/<example_file>.fsx

View File

@@ -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

View File

@@ -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

View File

@@ -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