Reputation: 53
Could anyone show me a sample code to use NelderMeadSolver class in F#?
For example, I want to minimize the following function: F(X, Y)
F = (X-1)^2 + (y-1)^2 where 0< X < 2 , 0< Y < 2 Answer is obviousely X = 1, Y = 1
I found an example for C#:
http://msdn.microsoft.com/en-us/library/hh404040(v=VS.93).aspx
I would appreciate very much if someone can give me simple F# code to minimize the function above. Thank you.
Upvotes: 3
Views: 980
Reputation: 41290
I've never used Solver Foundation before, but here is a straightforward translation from C# example in MSDN (adapted to your optimization function):
open System
open Microsoft.SolverFoundation.Common
open Microsoft.SolverFoundation.Solvers
let xInitial = [| 0.; 0. |]
let xLower = [| 0.; 0. |]
let xUpper = [| 2.; 2. |]
let sqr x = x * x
let solution =
NelderMeadSolver.Solve(
Func<float [], _>(fun xs -> sqr(xs.[0] - 1.) + sqr(xs.[1] - 1.)),
xInitial, xLower, xUpper)
printfn "%A" solution.Result
printfn "solution = %A" (solution.GetSolutionValue 0)
printfn "x = %A" (solution.GetValue 1)
printfn "y = %A" (solution.GetValue 2)
You should be able to add Solver Foundation's references and build the program. If you use the code in F# Interactive, remember to add Solver Foundation's dll files by referencing their exact paths.
Upvotes: 4