取负一个Func <double [],double =“”> </double [],>

时间:2014-02-10 12:57:48

标签: c# algorithm math func

我有一个数学函数-exp{-(x - 1)²} - exp{-0.5*(y - 2)²},它使用函数及其衍生物传递给BFGS算法

Func<double[], double> f = (x) => 
    Math.Exp(-Math.Pow(x[0] - 1, 2)) + Math.Exp(-0.5 * Math.Pow(x[1] - 2, 2));

Func<double[], double[]> g = (x) => new double[] 
{
    // df/dx = -2 * e^(-(x - 1)²)(x - 1).
    -2 * Math.Exp(-Math.Pow(x[0] - 1, 2)) * (x[0] - 1),

    // df/dy = -e^(-1/2(y - 2)²) * (y - 2).
    -Math.Exp(-0.5 * Math.Pow(x[1] - 2, 2)) * (x[1] - 2)
};

现在,算法已被编码以最小化传递给它的函数,这很有效。但是,我想添加最大化功能的能力。为此,我将仅通过否定函数和派生数来包装最小化代码,并将结果传递给最小化代码。

算法的人。是

public BroydenFletcherGoldfarbShanno(int numberOfVariables, 
    Func<double[], double> function, 
    Func<double[], double[]> gradient, 
    Random random = null)
        : this(numberOfVariables, random)
{
    if (function == null)
        throw new ArgumentNullException("function");
    if (gradient == null)
        throw new ArgumentNullException("gradient");
    this.Function = function;
    this.Gradient = gradient;
}

初始化Function / Gradient。这个类有Minimize方法,现在我添加了

public double Maximize()
{
     // Negate the function.
     Func<double[], double> f;
     f = (x) => -this.Function(x);
     this.Function = f;

     // Negate the derivatives.
     ...
}

我的问题是我怎样才能否定this.Function / this.Gradient个对象?我上面所做的就是StackOverflowException

感谢您的时间。


编辑。功能声明

[TestMethod]
public void LBFGSMaximisationTest()
{
    // Suppose we would like to find the maximum of the function:
    // f(x, y) = exp{-(x - 1)²} + exp{-(y - 2)²/2}.
    // First we need write down the function either as a named
    // method, an anonymous method or as a lambda function.
    Func<double[], double> f = (x) =>
    Math.Exp(-Math.Pow(x[0] - 1, 2)) + Math.Exp(-0.5 * Math.Pow(x[1] - 2, 2));

    // Now, we need to write its gradient, which is just the
    // vector of first partial derivatives del_f / del_x.
    // g(x, y) = { del f / del x, del f / del y }.
    Func<double[], double[]> g = (x) => new double[] 
    {
        // df/dx = -2 * e^(-(x - 1)²)(x - 1).
        -2 * Math.Exp(-Math.Pow(x[0] - 1, 2)) * (x[0] - 1),

        // df/dy = -e^(-1/2(y - 2)²) * (y - 2).
        -Math.Exp(-0.5 * Math.Pow(x[1] - 2, 2)) * (x[1] - 2)
    };

    // Finally, we can create the L-BFGS solver, passing the functions as arguments.
    Random r = new SystemRandomSource(0, true);
    BroydenFletcherGoldfarbShanno lbfgs = new BroydenFletcherGoldfarbShanno(
        numberOfVariables: 2, function: f, gradient: g, random: r);

    // And then minimize the function.
    double maxValue = lbfgs.Maximize();
    ...

1 个答案:

答案 0 :(得分:3)

是的,它会:

f = (x) => -this.Function(x);
this.Function = f;

假设Function在班级是Funct<double[],double>,你基本上是写作:

this.Function = (x) => -this.Function(x);
是的,那会爆炸。这是因为this.Function是通过捕获的范围延迟的。我怀疑你意味着什么

Func<double[], double> oldFunc = this.Function;
this.Function = (x) => -oldFunc(x);

现在我们将旧函数捕获到委托中,并使用捕获的委托而不是递归调用。