首页 > 解决方案 > 如何传递一个类方法?

问题描述

我正在尝试将此库用于某些工作。在他们网站上给出的示例中,他们使用运算符来定义梯度计算。我想使用方法,即getGradient,而不是运算符。我尝试了几种方法,包括 std::bind()、&Rosenbrock::getGradient。它们都不能正常工作。知道如何做到这一点吗?我不需要完整的答案,只需提示就足够了。

#include <Eigen/Core>
#include <iostream>
#include <LBFGS.h>

using Eigen::VectorXd;
using namespace LBFGSpp;

class Rosenbrock
{
private:
    int n;
public:
    Rosenbrock(int n_) : n(n_) {}
    double operator()(const VectorXd& x, VectorXd& grad);
    double getGradient(const VectorXd& x, VectorXd& grad);
};

double Rosenbrock::operator()(const VectorXd& x, VectorXd& grad){
    double fx = 0.0;
    for(int i = 0; i < n; i += 2)
    {
        double t1 = 1.0 - x[i];
        double t2 = 10 * (x[i + 1] - x[i] * x[i]);
        grad[i + 1] = 20 * t2;
        grad[i]     = -2.0 * (x[i] * grad[i + 1] + t1);
        fx += t1 * t1 + t2 * t2;
    }
    return fx;
}

double Rosenbrock::getGradient(const VectorXd& x, VectorXd& grad){
    double fx = 0.0;
    for(int i = 0; i < n; i += 2)
    {
        double t1 = 1.0 - x[i];
        double t2 = 10 * (x[i + 1] - x[i] * x[i]);
        grad[i + 1] = 20 * t2;
        grad[i]     = -2.0 * (x[i] * grad[i + 1] + t1);
        fx += t1 * t1 + t2 * t2;
    }
    return fx;
}


int main(int argc, char** argv){
 
    const int n = 10;
    // Set up parameters
    LBFGSParam<double> param;
    param.epsilon = 1e-6;
    param.max_iterations = 100;

    // Create solver and function object
    LBFGSSolver<double> solver(param);
    Rosenbrock fun(n);

    // Initial guess
    VectorXd x = VectorXd::Zero(n);
    double fx;
    //int niter = solver.minimize(fun, x, fx);
    
    int niter = solver.minimize(std::bind(Rosenbrock::getGradient, fun, _1, _2), x, fx);
    // I want to do something similar to this 
    std::cout << niter << " iterations" << std::endl;
    std::cout << "x = \n" << x.transpose() << std::endl;
    std::cout << "f(x) = " << fx << std::endl;

    return 0;

}

标签: c++bindingfunction-pointers

解决方案


关于什么:

struct Bind
{
   Rosenbrock & impl;
   template <typename X, typename Y> // Template here because I'm lazy writing the full type
   double operator () (X x, Y y) { return impl.getGradient(x, y); }

   Bind(Rosenbrock & impl) : impl(impl) {}
};

// Then use Bind with your solver:
Bind b(fun);
int niter = solver.minimize(b);


// Example with a template (replace X, Y by the argument signature of the method you are binding)
template <typename T, double (T::*Func)(X, Y)>
struct Bind
{
   T & impl;
   double operator()(X x, Y y) { return (impl.*Func)(x, y); }
   Bind(T & ref) : impl(ref) {}
};

// Using like
Bind<Rosenbrock, &Rosenbrock::getGradient> b(fun);

上面的 Bind 类可以是一个模板。它可以是一个 lambda。您只是将 operator() 重定向到 binder 的 operator() 中的方法。


推荐阅读