diff --git a/examples/fsharp/diet.fsx b/examples/fsharp/diet.fsx new file mode 100644 index 0000000000..399b45f829 --- /dev/null +++ b/examples/fsharp/diet.fsx @@ -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(); diff --git a/examples/fsharp/rabbit-pheasant.fsx b/examples/fsharp/rabbit-pheasant.fsx new file mode 100644 index 0000000000..9048247c2d --- /dev/null +++ b/examples/fsharp/rabbit-pheasant.fsx @@ -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() \ No newline at end of file