fsdiet.fs 1.95 KB
Newer Older
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
(*
  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.
*)

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]))

41
let obj = cost.Minimize(int64(1))
Valentin Platzgummer's avatar
Valentin Platzgummer committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
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();