有人能给我看一个在F#中使用NelderMeadSolver类的示例代码吗?
例如,我想最小化以下函数:F(X,Y)
F =(X-1)^ 2 +(y-1)^ 2其中0 <0。 X&lt; 2,0 <&lt; Y&lt; 2答案显然是X = 1,Y = 1
我找到了一个C#的例子:
http://msdn.microsoft.com/en-us/library/hh404040(v=VS.93).aspx
如果有人能给我简单的F#代码以最小化上述功能,我将非常感激。谢谢。
答案 0 :(得分:4)
我之前从未使用过Solver Foundation,但这里是C# example in MSDN的简单翻译(适合您的优化功能):
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)
您应该能够添加Solver Foundation的参考资料并构建程序。如果您使用F#Interactive中的代码,请记住通过引用其确切路径来添加Solver Foundation的dll文件。