如何调整功能

时间:2016-08-07 14:18:43

标签: c++ c++11 visual-c++ c++14

我试图在C ++中使用Nelder-Mead算法来最小化一个函数。我不明白为什么这段代码不能编译。

#include <iostream>
#include "/home/user/CppNumericalSolvers/include/cppoptlib/meta.h"
#include "/home/user/CppNumericalSolvers/include/cppoptlib/problem.h"
#include "/home/user/CppNumericalSolvers/include/cppoptlib/solver/neldermeadsolver.h"    

template<typename Ta>
class AB : public cppoptlib::Problem<Ta> {
  public:
    AB(int aa) : a(aa){}      
    using typename cppoptlib::Problem<Ta>::TVector;
    //int a = 250;
    Ta value(const TVector &x) {
        return   (1 - x[0]) * (1 - x[0]) + a * (x[1] - x[0] * x[0]) * (x[1] - x[0] * x[0]);
}    
};   

void test(){

 typedef AB<double> TAB;
    TAB f(250);
    // choose a starting point
    Eigen::VectorXd x(2); x << -1, 2;   

    // choose a solver
    cppoptlib::NelderMeadSolver<TAB> solver;
    // and minimize the function
    solver.minimize(f, x);
    // print argmin
    std::cout << "argmin      " << x.transpose() << std::endl;
    std::cout << "f in argmin " << f(x) << std::endl;    
}    

int main(int argc, char const *argv[]) {

    test();       

    return 0;
}

当我编译时,我收到了这个:

essai.cpp: In constructor ‘AB<Ta>::AB(int)’:
essai.cpp:10:18: error: class ‘AB<Ta>’ does not have any field named ‘a’
     AB(int aa) : a(aa){}   
                  ^
essai.cpp: In member function ‘Ta AB<Ta>::value(const typename cppoptlib::Problem<Ta>::TVector&)’:
essai.cpp:14:44: error: ‘a’ was not declared in this scope
         return   (1 - x[0]) * (1 - x[0]) + a * (x[1] - x[0] * x[0]) * (x[1] - x[0] * x[0]);
                                            ^

如果你有一些想法会对我有所帮助,因为现在我不知道如何做到这一点!

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

这是创建构造函数的代码,用于在函数test

中将给定值输入到AB的对象
The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.