This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let (==) (x: Decision) (y: string) = Term.op_Equality(x, y) | |
let (==>) x y = Model.Implies(x, y) | |
person.AddConstraints("constraints", | |
language == "English" ==> (color == "Red"), | |
language == "Spanish" ==> (animal == "Dog"), | |
language == "Japanese" ==> (actor == "Painter"), | |
language == "Italian" ==> (beverage == "Tea"), | |
color == "Green" ==> (beverage == "Coffee"), | |
actor == "Sculptor" ==> (animal == "Snails"), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let persons = Array.init 5 (sprintf "person%i" >> person.CreateInstance) | |
let allDifferent (decision: Decision) = Array.init 5 (fun i -> persons.[i].[decision] :> Term) | |
rootModel.AddConstraints("constraints", | |
Model.AllDifferent (allDifferent language), | |
Model.AllDifferent (allDifferent color), | |
Model.AllDifferent (allDifferent actor), | |
Model.AllDifferent (allDifferent animal), | |
Model.AllDifferent (allDifferent beverage), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#r @"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Microsoft.Solver.Foundation.dll" | |
module ZebraPuzzle = | |
open System | |
open Microsoft.SolverFoundation.Services | |
let solve() = | |
let context = SolverContext.GetContext() | |
let rootModel = context.CreateModel() | |
let person = rootModel.CreateSubModel("Person") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
member x.selectBB(index) = | |
totalUnassigned <- totalUnassigned - itemValues.[depth] | |
path.[depth] <- byte(index) | |
totalValues.[index] <- totalValues.[index] + itemValues.[depth] | |
depth <- depth + 1 | |
member x.deselectBB(index) = | |
depth <- depth - 1 | |
path.[depth] <- 0uy | |
totalValues.[index] <- totalValues.[index] - itemValues.[depth] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Using branch and bound method to prune unused branches. | |
member x.divideBB() = | |
if depth >= numItems then | |
if abs(totalValues.[0] - totalValues.[1]) < bestDifference then | |
Array.Copy(totalValues, bestTotalValues, size) | |
bestDifference <- abs(totalValues.[0] - totalValues.[1]) | |
else () | |
elif abs(totalValues.[0] - totalValues.[1]) - totalUnassigned <= bestDifference then | |
for i in 0..1 do | |
x.selectBB(i) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Preassign the divider with some configurations. | |
member x.preassignBB(level, threadId) = | |
for l in 1..level do | |
if threadId &&& (pown 2 l) <> 0 then | |
path.[depth] <- 1uy | |
totalValues.[1] <- totalValues.[1] + itemValues.[depth] | |
totalUnassigned <- totalUnassigned - itemValues.[depth] | |
depth <- depth + 1 | |
else | |
path.[depth] <- 0uy |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let rec loop () = async { | |
do! Async.Sleep(1000) | |
do! loop() } // Wrong! | |
// In order to write tail-recursive calls in F# async workflows, we should use return!: | |
let rec loop () = async { | |
do! Async.Sleep(1000) | |
return! loop() } // Correct :-) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let func = | |
printfn "hi" | |
1 | |
// vs. | |
let func() = | |
printfn "hi" | |
1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let plusOne (s : seq<_>) = | |
let e = s.GetEnumerator() | |
seq { | |
while e.MoveNext() do | |
yield e.Current + 1 | |
} | |
let r = plusOne [1; 2; 3] | |
printfn "%A" r // prints [2; 3; 4] | |
printfn "%A" r // prints [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async { | |
let wc = new WebClient() | |
let html = wc.DownloadString(uri) | |
printfn "%s" html } | |
// To make code in F# async workflow actually asynchronous, | |
// we need to use a primitive operation of type Async<'T> | |
// (implemented usually in the F# library as an extension) and call it using let! or do!: | |
async { |