c++ - 如何传递一个类方法?
问题描述
我正在尝试将此库用于某些工作。在他们网站上给出的示例中,他们使用运算符来定义梯度计算。我想使用方法,即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;
}
解决方案
关于什么:
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() 中的方法。
推荐阅读
- python-3.x - 批量标准化在 tensorflow 2.0 中没有梯度?
- typescript - 类型不匹配?“...缺少类型...中的以下属性”
- html - 每次修改时都需要制作一个新的css版本
- swift - 如何使用不是主线程的线程等到类静态 Bool 变量为真?
- haskell - 规范化 TypeNats 不等式
- javascript - 如何使 HTML 元素仅在鼠标悬停时跟随光标,否则不跟随?
- javascript - 有没有办法可以在两者之间延迟:this.bullets.push(this.ship.shoot());
- php - laravel 5如何将数据插入3-4个模型中的多对一关系
- powershell - 使用 PowerShell 每 5 分钟创建一个文件的备份副本
- java - Mysql运行代码,但只创建了第一个表