微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何传递类方法?

如何解决如何传递类方法?

我正在尝试将 this library 用于某些工作。在他们网站上给出的示例中,他们使用运算符来定义梯度计算。我想使用方法,即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),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;

}

解决方法

关于:

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() 重定向到绑定器的 operator () 中的方法。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。