当前位置: 首页>>代码示例>>C#>>正文


C# QuadraticObjectiveFunction.Function方法代码示例

本文整理汇总了C#中Accord.Math.Optimization.QuadraticObjectiveFunction.Function方法的典型用法代码示例。如果您正苦于以下问题:C# QuadraticObjectiveFunction.Function方法的具体用法?C# QuadraticObjectiveFunction.Function怎么用?C# QuadraticObjectiveFunction.Function使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Accord.Math.Optimization.QuadraticObjectiveFunction的用法示例。


在下文中一共展示了QuadraticObjectiveFunction.Function方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: FunctionTest

        public void FunctionTest()
        {
            double x = 0;
            double y = 0;

            Func<double> expected = () => -2 * x * x + x * y - y * y + 5 * y;
            var actual = new QuadraticObjectiveFunction("-2x² + xy - y² + 5y");

            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    x = (i - 5) / 10.0;
                    y = (j - 5) / 10.0;

                    double a = actual.Function(new[] { x, y });
                    double e = expected();

                    Assert.AreEqual(e, a, 1e-10);
                    Assert.IsFalse(Double.IsNaN(a));
                    Assert.IsFalse(Double.IsNaN(e));
                }
            }
        }
开发者ID:KommuSoft,项目名称:accord_framework,代码行数:24,代码来源:QuadraticObjectiveFunctionTest.cs

示例2: LambdaFunctionTest3

        public void LambdaFunctionTest3()
        {
            double x = 0;

            Func<double> expected = () => x * x + 1;
            var actual = new QuadraticObjectiveFunction(() => x * x + 1);

            for (int i = 0; i < 10; i++)
            {
                x = (i - 5) / 10.0;

                double a = actual.Function(new[] { x });
                double e = expected();

                Assert.AreEqual(e, a, 1e-10);
                Assert.IsFalse(Double.IsNaN(a));
                Assert.IsFalse(Double.IsNaN(e));
            }
        }
开发者ID:accord-net,项目名称:framework,代码行数:19,代码来源:QuadraticObjectiveFunctionTest.cs

示例3: LambdaFunctionTest4

        public void LambdaFunctionTest4()
        {
            double x = 0;
            double y = 0;
            double z = 0;

            Func<double> expected = () => -x * y + y * z;
            var actual = new QuadraticObjectiveFunction(() => -x * y + y * z);

            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    for (int k = 0; k < 10; k++)
                    {
                        x = (i - 5) / 10.0;
                        y = (j - 5) / 10.0;
                        z = (k - 5) / 10.0;

                        double a = actual.Function(new[] { x, y, z });
                        double e = expected();

                        Assert.AreEqual(e, a, 1e-10);
                        Assert.IsFalse(Double.IsNaN(a));
                        Assert.IsFalse(Double.IsNaN(e));
                    }
                }
            }
        }
开发者ID:accord-net,项目名称:framework,代码行数:29,代码来源:QuadraticObjectiveFunctionTest.cs

示例4: GoldfarbIdnani4

        public void GoldfarbIdnani4()
        {
            // https://github.com/accord-net/framework/issues/171

            int n = 21;
            var Q = Matrix.Diagonal(n, 2.0);
            var d = Vector.Create(3132.0, 6264, 15660, 18792, 21924, 6264, 18792, 21924, 9396, 3132, 12528, 6264, 9396, 18792, 21924, 9396, 3132, 3132, 6264, 15660, 18792);

            int m = 44;

            var b = Vector.Create(
                703.999, -704.001,
                1267.999, -1268.001,
                1565.999, -1566.001,
                471.999, -472.001,
                1425.999, -1426.001, 
                -107.001,
                -1164.001, 
                -57.001,
                -311.001,
                -1433.001, 
                -0.001,
                -0.001, 
                -788.001, 
                -472.001, 
                -850.001, 
                -273.001, 
                -0.001, -0.001, -0.001, -0.001,
                -0.001, -0.001, -0.001, -0.001, -0.001, 
                -0.001, -0.001, -0.001, -0.001, -0.001, 
                -0.001, -0.001, -0.001, -0.001, -0.001, 
                -0.001, -0.001, -0.001, -0.001);

            var A = Matrix.Create(m, n, 
                 1.0, 1,  1,  1,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,   0,  0,  0,  0,  0,  0, 
                -1,  -1, -1, -1,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,   0,  0,  0,  0,  0,  0, 
                 0,   0,  0,  0,  1,  1,  1,  1,  0,  0,  0,  0,  0,  0,  0,   0,  0,  0,  0,  0,  0, 
                 0,   0,  0,  0, -1, -1, -1, -1,  0,  0,  0,  0,  0,  0,  0,   0,  0,  0,  0,  0,  0,
                 0,   0,  0,  0,  0,  0,  0,  0,  1,  1,  1,  1,  1,  0,  0,   0,  0,  0,  0,  0,  0,
                 0,   0,  0,  0,  0,  0,  0,  0, -1, -1, -1, -1, -1,  0,  0,   0,  0,  0,  0,  0,  0,
                 0,   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  1,  1,   0,  0,  0,  0,  0,  0,
                 0,   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, -1, -1,   0,  0,  0,  0,  0,  0,
                 0,   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,   1,  1,  1,  1,  1,  1,
                 0,   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  -1, -1, -1, -1, -1, -1,
                 0,   0,  0,  0, -1,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,   0,  0,  0,  0,  0,  0,  
                 0,   0,  0,  0,  0,  0,  0,  0, -1,  0,  0,  0,  0,  0,  0,  -1,  0,  0,  0,  0,  0,
                 0,   0,  0,  0,  0,  0,  0,  0,  0, -1,  0,  0,  0,  0,  0,   0, -1,  0,  0,  0,  0,
                 0,   0,  0,  0,  0, -1,  0,  0,  0,  0,  0,  0,  0,  0,  0,   0,  0,  0,  0,  0,  0,
                 0,   0,  0,  0,  0,  0,  0,  0,  0,  0, -1,  0,  0,  0,  0,   0,  0,  0,  0,  0,  0,
                 0,   0,  0,  0,  0,  0,  0,  0,  0,  0,  0, -1,  0,  0,  0,   0,  0,  0,  0,  0,  0,
                 0,   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, -1,  0,  0,   0,  0,  0,  0,  0,  0,
                -1,   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,   0,  0, -1,  0,  0,  0,
                 0,   0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 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);


            var solver = new GoldfarbIdnani(Q, d, A, b, 0);

            //for (int i = 0; i < solver.ConstraintTolerances.Length; i++)
            //    solver.ConstraintTolerances[i] = 1e-1;

            Assert.IsTrue(solver.Minimize());

            QuadraticObjectiveFunction f = new QuadraticObjectiveFunction(Q, d);
            f.Function(solver.Solution);

            var constraints = LinearConstraintCollection.FromMatrix(A, b, 0);

            double[] violation = constraints.Apply(x => x.GetViolation(solver.Solution));
        }
开发者ID:accord-net,项目名称:framework,代码行数:70,代码来源:GoldfarbIdnaniTest.cs


注:本文中的Accord.Math.Optimization.QuadraticObjectiveFunction.Function方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。