使用Microsoft.SolverFoundation(C#)的任意非线性函数或类方法的极值 [英] Extremum for an arbitrary nonlinear function or a class method with Microsoft.SolverFoundation (C#)

查看:106
本文介绍了使用Microsoft.SolverFoundation(C#)的任意非线性函数或类方法的极值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


Given - 任意非线性函数或类
方法,fe:

double someFunction(double x,doulbe y);

和constraints,fe:

xє[0,10]; yє[-5,20];

1。怎么能 (如果
可以)
我们在Microsoft.SolverFoundation(C#)中找到此函数的最大值?

2。我们如何(如果可以)使用Solver Foundation Services图层
(C#)

谢谢! 

 

推荐答案

1。一个简单的例子没有绑定和其他有用的东西,你肯定想在手册中查找; - )

1. A simple example w/o binding and other useful stuff you'd definitely want to look up in the manual ;-)


    public static class SF
    {
        public static void Main()
        {
            IndividualSolvers();
            TheAbstractWay();
            Console.ReadLine();
        }

        private static double MinimizeThat(double[] args)
        {
            return Math.Cos(args[0]) + args[1] * args[1];
        }

        private static void IndividualSolvers()
        {
            var hls = new HybridLocalSearchSolver();
            int x, y;
            hls.AddVariable(out x, 0, 5, false);
            hls.AddVariable(out y, -1, 1, true);
            hls.AddGoal(hls.CreateNaryFunction(MinimizeThat, new[] { x, y }));
            var hlsr = hls.Solve(new HybridLocalSearchParameters());
            Console.WriteLine("HLS: f({0}, {1}) = {2}", hlsr.GetValue(x), hlsr.GetValue(y), hlsr.GetSolutionValue(0));

            var nms = NelderMeadSolver.Solve(MinimizeThat, new[] { 0.0, 0.0 }, new[] { 0.0, -1.0 }, new[] { 5.0, 1.0 });
            Console.WriteLine("NMS: f({0}, {1}) = {2}", nms.GetValue(1), nms.GetValue(2), nms.GetSolutionValue(0));
        }

        private static void TheAbstractWay()
        {
            var ctx = new SolverContext();
            var m = ctx.CreateModel();
            var x = new Decision(Domain.Real, "x");
            var y = new Decision(Domain.Real, "y");
            m.AddDecisions(x, y);
            m.AddConstraints("Bounds", 0 <= x, x <= 5, -1 <= y, y <= 1);
            m.AddGoal("Minimize", GoalKind.Minimize, "Cos[x]+y^2");
            //m.AddGoal("Minimize", GoalKind.Minimize, Model.Cos(x) + y * y);
            var r = ctx.Solve();
            ctx.PropagateDecisions();
            Console.WriteLine("{3}: f({0}, {1}) = {2}", x.GetDouble(), y.GetDouble(), r.Goals.Single().ToDouble(), r.GetReport().SolverType.Name);
        }
    }


这篇关于使用Microsoft.SolverFoundation(C#)的任意非线性函数或类方法的极值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆