F# Microsoft Solver Foundation - NelderMeadSolver 类

发布于 2025-01-07 02:10:49 字数 375 浏览 0 评论 0原文

谁能向我展示在 F# 中使用 NelderMeadSolver 类的示例代码?

例如,我想最小化以下函数: F(X, Y)

F = (X-1)^2 + (y-1)^2 其中 0< X< 2 , 0< Y< 2 答案显然是 X = 1, Y = 1

我找到了 C# 的示例:
http://msdn.microsoft.com/en- us/library/hh404040(v=VS.93).aspx

如果有人能给我简单的 F# 代码来最小化上述函数,我将非常感激。谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南七夏 2025-01-14 02:10:49

我以前从未使用过 Solver Foundation,但这里有一个来自 MSDN 中的 C# 示例(适应您的优化功能):

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 文件的确切路径来添加它们。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文