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
| #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
| 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
| 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 languages = Domain.Enum("English", "Spanish", | |
| "Japanese", "Italian", "Norwegian") | |
| let language = Decision(languages, "Language") | |
| person.AddDecision(language) |
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 pfilterRange predicate (i, j) = | |
| let results = ResizeArray(j-i+1) | |
| let monitor = new Object() | |
| Parallel.For( | |
| i, j, new ParallelOptions(), | |
| (fun () -> ResizeArray(j-i+1)), | |
| (fun k _ (localList: ResizeArray<_>) -> | |
| if predicate k then localList.Add(k) | |
| localList), | |
| (fun local -> lock (monitor) (fun () -> results.AddRange(local))) |
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 filterRange predicate (i, j) = | |
| let results = ResizeArray(j-i+1) // reserve quite a lot of space. | |
| for k = i to j do | |
| if predicate k then results.Add(k) | |
| results.ToArray() | |
| let indivisible divisors b = | |
| Array.forall (fun a -> b%a<>0) divisors |
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 primesUnder = function | |
| | n when n<=2 -> [||] | |
| | 3 -> [|2|] | |
| | n -> let ns = n |> float |> sqrt |> ceil |> int | |
| let smallers = primesUnder ns | |
| Array.append smallers (filterRange (indivisible smallers) (ns, n-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 inline mersenne (i: int) = | |
| if i = 2 || (isPrime (bigint i) && lucasLehmer i) then | |
| let p = 1I <<< i | |
| Some (i, (p/2I) * (p-1I)) | |
| else None | |
| let runPerfectsSeq n = | |
| seq {1..n} | |
| |> Seq.choose mersenne | |
| |> Seq.toArray |
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 lucasLehmer p = | |
| let m = (1I <<< p) - 1I | |
| let rec loop i acc = | |
| if i = p-2 then acc | |
| else loop (i+1) ((acc*acc - 2I)%m) | |
| (loop 0 4I) = 0I |