Merge pull request #583 from acco32/fsharp
Add constraint programming examples
This commit is contained in:
63
examples/fsharp/diet.fsx
Normal file
63
examples/fsharp/diet.fsx
Normal file
@@ -0,0 +1,63 @@
|
||||
(*
|
||||
|
||||
Copyright 2012 Hakan Kjellerstrand
|
||||
|
||||
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"
|
||||
|
||||
open Google.OrTools.ConstraintSolver
|
||||
|
||||
|
||||
let solver = new Solver("Diet");
|
||||
|
||||
let price = [|50; 20; 30; 80|] // in cents
|
||||
|
||||
// requirements for each nutrition type
|
||||
let limits = [|500; 6; 10; 8|]
|
||||
let products = [|"A"; "B"; "C"; "D"|]
|
||||
|
||||
// nutritions for each product
|
||||
let calories = [|400L; 200L; 150L; 500L|]
|
||||
let chocolate = [|3L; 2L; 0L; 0L|]
|
||||
let sugar = [|2L; 2L; 4L; 4L|]
|
||||
let fat = [|2L; 4L; 1L; 5L|]
|
||||
|
||||
let x = solver.MakeIntVarArray(products.Length, 0L, 100L, "x")
|
||||
let cost = x.ScalProd(price).Var()
|
||||
|
||||
solver.Add( x.ScalProd(calories).solver().MakeGreaterOrEqual(x.ScalProd(calories), limits.[0]) )
|
||||
solver.Add( x.ScalProd(chocolate).solver().MakeGreaterOrEqual(x.ScalProd(chocolate), limits.[1]))
|
||||
solver.Add( x.ScalProd(sugar).solver().MakeGreaterOrEqual(x.ScalProd(sugar), limits.[2]))
|
||||
solver.Add( x.ScalProd(fat).solver().MakeGreaterOrEqual(x.ScalProd(fat), limits.[3]))
|
||||
|
||||
let obj = cost.Minimize(1)
|
||||
let db = solver.MakePhase(new IntVarVector(x), Solver.CHOOSE_PATH, Solver.ASSIGN_MIN_VALUE)
|
||||
|
||||
solver.NewSearch(db, obj)
|
||||
while (solver.NextSolution()) do
|
||||
printfn "cost: %d" (cost.Value())
|
||||
printfn "Products: "
|
||||
for i=0 to (products.Length-1) do
|
||||
printfn "%s: %d" products.[i] (x.[i].Value())
|
||||
|
||||
|
||||
printfn "\nSolutions: %d" (solver.Solutions())
|
||||
printfn "WallTime: %d ms" (solver.WallTime())
|
||||
printfn "Failures: %d" (solver.Failures())
|
||||
printfn "Branches: %d " (solver.Branches())
|
||||
|
||||
solver.EndSearch();
|
||||
32
examples/fsharp/rabbit-pheasant.fsx
Normal file
32
examples/fsharp/rabbit-pheasant.fsx
Normal file
@@ -0,0 +1,32 @@
|
||||
|
||||
#I "../../bin"
|
||||
#r "Google.OrTools.dll"
|
||||
|
||||
open Google.OrTools.ConstraintSolver
|
||||
|
||||
let solver = new Solver("pheasant")
|
||||
|
||||
// Variables
|
||||
let p = solver.MakeIntVar(0L, 20L, "pheasant")
|
||||
let r = solver.MakeIntVar(0L, 20L, "rabbit")
|
||||
|
||||
// Constraints
|
||||
let legs = solver.MakeSum(solver.MakeProd(p, 2L), solver.MakeProd(r, 4L))
|
||||
let heads = solver.MakeSum(p, r)
|
||||
|
||||
let constraintLegs = solver.MakeEquality(legs, 56)
|
||||
let constraintHeads = solver.MakeEquality(heads, 20)
|
||||
|
||||
solver.Add(constraintLegs)
|
||||
solver.Add(constraintHeads)
|
||||
|
||||
let vars = new IntVarVector([|p; r|])
|
||||
let db = solver.MakePhase(vars, Solver.CHOOSE_FIRST_UNBOUND, Solver.ASSIGN_MIN_VALUE)
|
||||
|
||||
// Solve
|
||||
solver.NewSearch(db)
|
||||
|
||||
while (solver.NextSolution()) do
|
||||
printfn "rabbits: %d, pheasants: %d" (r.Value()) (p.Value())
|
||||
|
||||
solver.EndSearch()
|
||||
Reference in New Issue
Block a user