ILNumerics:优化函数时返回意外的最小值

时间:2018-10-08 04:20:24

标签: optimization ilnumerics

我正在试用ILNumerics(发布时的最新版本),但是在运行Optimization.fmin优化器时得到的最小值是错误的。

最小值应为f(-5.218)= -3.342,搜索范围应为-10 <= x <= 10。

private void Compute()
    {
        //var function = new Func<double, double>(input =>
        //{
        //    var output = Math.Pow(input, 3) / Math.Exp(Math.Pow(input, 0.8));
        //    return output;
        //});

        Array<double> bounds = new double[] {-10.0, 10.0};
        Array<double> start = new double[] {1};
        Array<double> y = Optimization.fmin(myfunc, start, lowerBound: bounds[0], upperBound: bounds[1]);

        Console.WriteLine("The minimum is found at");
        Console.WriteLine("{0}", y);
        Console.WriteLine("The value is");
        Console.WriteLine("{0}", myfunc(y));

        Console.ReadLine();

        return;


    }

    static RetArray<double> myfunc(InArray<double> x)
    {
        var input = x.GetValue(0);
        var output = Math.Pow(input, 3) / Math.Exp(Math.Pow(input, 0.8));
        return output;
    }

1 个答案:

答案 0 :(得分:1)

您的目标函数在负位置返回NaN。您可以轻松检查以下内容:

首先,将目标范围内的样本值相加(-10 ... 10),然后计算目标函数的结果。

Array<double> bounds = new double[] { -10.0, 10.0 };
Array<double> start = new double[] { 1 };
Array<double> y = Optimization.fmin(myfunc, start, lowerBound: bounds[0], upperBound: bounds[1]);

Array<double> range = linspace(-10.0, 10, 100); 
Array<double> YinRange = apply(range, range, (a,b) => (double)myfunc(a));

Console.WriteLine("The minimum is found at");
Console.WriteLine("{0}", y);
Console.WriteLine("The value is");
Console.WriteLine("{0}", myfunc(y));

现在,可以使用ArrayVisualizer在调试会话期间可视化实际值:

ObjectiveFunction in ArrayVizualizer

fmin大致返回了0,这似乎是合理的,因为它试图忽略NaN值。根据此图0实际上是最小的最小的

另请参阅:MSDN on Math.Pow