Skip to content

Instantly share code, notes, and snippets.

View dungpa's full-sized avatar

Anh-Dung Phan dungpa

View GitHub Profile
@dungpa
dungpa / zebra2.fs
Created June 21, 2012 15:10
Create infix functions for shortcuts
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"),
@dungpa
dungpa / zebra3.fs
Created June 21, 2012 15:16
Add knowledges to the root model
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),
@dungpa
dungpa / zebra4.fs
Created June 21, 2012 15:24
The whole solution for Zebra Puzzle using SolverFoundation
#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")
@dungpa
dungpa / bootyDivision1.fs
Created July 30, 2012 14:11
Backtracking on the binary tree
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]
@dungpa
dungpa / bootyDivision2.fs
Created July 30, 2012 14:14
Branch and bound technique
// 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)
@dungpa
dungpa / bootyDivision3.fs
Created July 30, 2012 14:18
Parallelism bits
// 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
@dungpa
dungpa / mistake1.fs
Created September 13, 2012 19:36
Async mistake 1
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 :-)
@dungpa
dungpa / mistake2.fs
Created September 13, 2012 19:40
A common mistake
let func =
printfn "hi"
1
// vs.
let func() =
printfn "hi"
1
@dungpa
dungpa / mistake3.fs
Created September 13, 2012 19:43
Common mistake
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 []
@dungpa
dungpa / mistake4.fs
Created September 13, 2012 19:47
Common mistake
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 {