From 75ff975920ab512e00a51043d1c43477b4283545 Mon Sep 17 00:00:00 2001 From: acco32 Date: Sun, 10 Sep 2017 22:28:03 -0700 Subject: [PATCH] Add integer programming example. Add types for solver algorithms --- examples/fsharp/Google.OrTools.FSharp.fsx | 53 +++++++++++++++ examples/fsharp/README.md | 8 ++- examples/fsharp/csintegerprogramming.fsx | 68 +++++++++++++++++++ ...programming.fs => cslinearprogramming.fsx} | 34 +++++++--- 4 files changed, 149 insertions(+), 14 deletions(-) create mode 100644 examples/fsharp/Google.OrTools.FSharp.fsx create mode 100644 examples/fsharp/csintegerprogramming.fsx rename examples/fsharp/{cslinearprogramming.fs => cslinearprogramming.fsx} (73%) diff --git a/examples/fsharp/Google.OrTools.FSharp.fsx b/examples/fsharp/Google.OrTools.FSharp.fsx new file mode 100644 index 0000000000..f853149c6a --- /dev/null +++ b/examples/fsharp/Google.OrTools.FSharp.fsx @@ -0,0 +1,53 @@ +namespace Google.OrTools.FSharp + + +type LinearProgramming = + // Linear Programming + | CLP // Recommended default value. + | GLPK + | GLOP + | GUROBI + | CPLEX + override this.ToString() = + match this with + | CLP -> "CLP_LINEAR_PROGRAMMING" + | GLPK -> "GLPK_LINEAR_PROGRAMMING" + | GLOP -> "GLOP_LINEAR_PROGRAMMING" + | GUROBI -> "GUROBI_LINEAR_PROGRAMMING" + | CPLEX -> "CPLEX_LINEAR_PROGRAMMING" + member this.Id = + match this with + | CLP -> 0 + | GLPK -> 1 + | GLOP -> 2 + | GUROBI -> 6 + | CPLEX -> 10 + + +type IntegerProgramming = + // Integer programming problems. + | SCIP // Recommended default value. + | GLPK + | CBC + | GUROBI + | CPLEX + | BOP + override this.ToString() = + match this with + | SCIP -> "SCIP_MIXED_INTEGER_PROGRAMMING" // Recommended default value. + | GLPK -> "GLPK_MIXED_INTEGER_PROGRAMMING" + | CBC -> "CBC_MIXED_INTEGER_PROGRAMMING" + | GUROBI -> "GUROBI_MIXED_INTEGER_PROGRAMMING" + | CPLEX -> "CPLEX_MIXED_INTEGER_PROGRAMMING" + | BOP -> "BOP_INTEGER_PROGRAMMING" + member this.Id = + match this with + | SCIP -> 3 + | GLPK -> 4 + | CBC -> 5 + | GUROBI -> 7 + | CPLEX -> 11 + | BOP -> 12 + + + diff --git a/examples/fsharp/README.md b/examples/fsharp/README.md index e18fb5d665..35225c6d32 100644 --- a/examples/fsharp/README.md +++ b/examples/fsharp/README.md @@ -2,10 +2,12 @@ Examples from the or-tools library utilizing F# + # 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/.fs -DYLD_FALLBACK_LIBRARY_PATH=lib mono64 bin/cslinearprogramming.exe +fsharpc --target:exe --out:bin/.exe --platform:anycpu --lib:bin -r:Google.OrTools.dll examples/fsharp/.fsx -``` +DYLD_FALLBACK_LIBRARY_PATH=lib mono64 bin/.exe + +``` \ No newline at end of file diff --git a/examples/fsharp/csintegerprogramming.fsx b/examples/fsharp/csintegerprogramming.fsx new file mode 100644 index 0000000000..4964dccfb5 --- /dev/null +++ b/examples/fsharp/csintegerprogramming.fsx @@ -0,0 +1,68 @@ +// Copyright 2010-2014 Google +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#I "../../bin" +#r "Google.OrTools.dll" +#load "Google.OrTools.FSharp.fsx" + +open System +open Google.OrTools.FSharp +open Google.OrTools.LinearSolver +open Google.OrTools.ConstraintSolver + +let solver solverType = + let svr = Solver.CreateSolver("IntegerProgramming", solverType.ToString()) + + // x1 and x2 are integer non-negative variables. + let x1 = svr.MakeIntVar(0.0, Double.PositiveInfinity, "x1") + let x2 = svr.MakeIntVar(0.0, Double.PositiveInfinity, "x2") + + // Minimize x1 + 2 * x2. + let objective = svr.Objective() + objective.SetMinimization() + objective.SetCoefficient(x1, 1.0) + objective.SetCoefficient(x2, 2.0) + + // 2 * x2 + 3 * x1 >= 17. + let ct = svr.MakeConstraint(17.0, Double.PositiveInfinity) + ct.SetCoefficient(x1, 3.0) + ct.SetCoefficient(x2, 2.0) + + let resultStatus = svr.Solve() + + // Check that the problem has an optimal solution. + match resultStatus with + | status when status <> Solver.OPTIMAL -> + printfn "The problem does not have an optimal solution!" + exit 0 + | _ -> + printfn "Problem solved in %i milliseconds" (svr.WallTime()) + + // The objective value of the solution. + printfn "Optimal objective value = %f" (objective.Value()) + + // The value of each variable in the solution. + printfn "x1 = %f" (x1.SolutionValue()) + printfn "x2 = %f" (x2.SolutionValue()) + + printfn "Advanced usage:" + printfn "Problem solved in %i branch-and-bound nodes" (svr.Nodes()) + + +// printfn "---- Integer programming example with %A ----" IntegerProgramming.GLPK +// solver IntegerProgramming.GLPK + +printfn "---- Linear programming example with %A ----" IntegerProgramming.CBC +solver IntegerProgramming.CBC + +// printfn "---- Linear programming example with %A ----" IntegerProgramming.SCIP +// solver IntegerProgramming.SCIP diff --git a/examples/fsharp/cslinearprogramming.fs b/examples/fsharp/cslinearprogramming.fsx similarity index 73% rename from examples/fsharp/cslinearprogramming.fs rename to examples/fsharp/cslinearprogramming.fsx index 43dc963ea7..113d9799c9 100644 --- a/examples/fsharp/cslinearprogramming.fs +++ b/examples/fsharp/cslinearprogramming.fsx @@ -1,15 +1,27 @@ +// Copyright 2010-2017 Google +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#I "../../bin" +#r "Google.OrTools.dll" + +#load "Google.OrTools.FSharp.fsx" open System open Google.OrTools.LinearSolver - - -let Glop = "GLOP_LINEAR_PROGRAMMING" -let Glpk = "GLPK_LINEAR_PROGRAMMING" -let Clp = "CLP_LINEAR_PROGRAMMING" - +open Google.OrTools.FSharp let solver solverType = - let svr = Solver.CreateSolver("IntegerProgramming", solverType) + let svr = Solver.CreateSolver("IntegerProgramming", solverType.ToString()) // x1, x2 and x3 are continuous non-negative variables. let x1 = svr.MakeNumVar(0.0, Double.PositiveInfinity, "x1") @@ -77,11 +89,11 @@ let solver solverType = printfn " activity = %f" (activities.[c2.Index()]) -printfn "---- Linear programming example with %s ----" Glop -solver Glop +printfn "---- Linear programming example with %A ----" LinearProgramming.GLOP +solver LinearProgramming.GLOP // printfn "---- Linear programming example with %s ----" Glpk // solver Glpk -printfn "---- Linear programming example with %s ----" Clp -solver Clp +printfn "---- Linear programming example with %A ----" LinearProgramming.CLP +solver LinearProgramming.CLP