将数据传递给 Rcpp 中的 nlopt?

Passing data to nlopt in Rcpp?

这是一个相当简单的问题,但我还没能在网上找到答案。

希望我最近的尝试,这是最新的编译器输出: 注意:候选函数不可行:没有已知的从 'double (unsigned int, const double *, void *, void )' 到 'nlopt_func'(又名 'double ()(unsigned int, const double *, double *, void *)')的第二个参数

的转换

根据这个错误,我推测我现在正在包装或 'type casting' 正确地包装数据参数以及参数向量。第三个输入梯度之间的差异让我感到困惑。正如我所说的无梯度优化例程。

这是一个带有常量和变量的简单线性回归:

#include "RcppArmadillo.h"

// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::depends(nloptr)]]
//#include <vector>
#include <nloptrAPI.h>
using namespace arma;
using namespace Rcpp;

typedef struct {
  arma::mat data_in;
} *my_func_data;

typedef struct {
  double a, b;
} my_theta;

double myfunc(unsigned n, const double *theta, void *grad, void *data){

  my_func_data &temp = (my_func_data &) data;
  arma::mat data_in = temp->data_in;

  my_theta *theta_temp = (my_theta *) theta;
  double a = theta_temp->a, b = theta_temp->b;

  int Len = arma::size(data_in)[0];
  arma::vec Y1 = data_in(span(0, Len-1), 1);
  arma::vec Y2 = data_in(span(0, Len-1), 2);
  arma::vec res = data_in(span(0, Len-1), 0) - a*Y1 - b*Y2 ;
  return sum(res);
}


// [[Rcpp::export]]
void test_nlopt_c() {

  arma::mat data_in(10,3);
  data_in(span(0,9),0) = arma::regspace(40, 49);
  data_in(span(0,9),1) = arma::ones(10);
  data_in(span(0,9),2) = arma::regspace(10, 19);

  my_func_data &temp = (my_func_data &) data_in;

  double lb[2] = { 0, 0,}; /* lower bounds */
  nlopt_opt opt;
  opt = nlopt_create(NLOPT_LN_NELDERMEAD, 2); /* algorithm and dimensionality */
  nlopt_set_lower_bounds(opt, lb);

  nlopt_set_min_objective(opt, myfunc, &data_in );

  nlopt_set_xtol_rel(opt, 1e-4);
  double minf; /* the minimum objective value, upon return */
  double x[2] = {0.5, 0.5};  /* some initial guess */
  nlopt_result result = nlopt_optimize(opt, x, &minf);
  Rcpp::Rcout << "result:" << result;
    return;
}

想通了,傻答答对了,把'void'改成'double'就好了,不知道为什么。不管怎样,示例代码需要一些改进,但它确实有效。

#include "RcppArmadillo.h"

// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::depends(nloptr)]]
//#include <vector>
#include <nloptrAPI.h>
using namespace arma;
using namespace Rcpp;

typedef struct {
  arma::mat data_in;
} *my_func_data;

typedef struct {
  double a, b;
} my_theta;

double myfunc(unsigned n, const double *theta, double *grad, void *data){

  my_func_data &temp = (my_func_data &) data;
  arma::mat data_in = temp->data_in;

  my_theta *theta_temp = (my_theta *) theta;
  double a = theta_temp->a, b = theta_temp->b;

  int Len = arma::size(data_in)[0];
  arma::vec Y1 = data_in(span(0, Len-1), 1);
  arma::vec Y2 = data_in(span(0, Len-1), 2);
  arma::vec res = data_in(span(0, Len-1), 0) - a*Y1 - b*Y2 ;
  return sum(res);
}


// [[Rcpp::export]]
void test_nlopt_c() {

  arma::mat data_in(10,3);
  data_in(span(0,9),0) = arma::regspace(40, 49);
  data_in(span(0,9),1) = arma::ones(10);
  data_in(span(0,9),2) = arma::regspace(10, 19);

  my_func_data &temp = (my_func_data &) data_in;

  double lb[2] = { 0, 0,}; /* lower bounds */
  nlopt_opt opt;
  opt = nlopt_create(NLOPT_LN_NELDERMEAD, 2); /* algorithm and dimensionality */
  nlopt_set_lower_bounds(opt, lb);

  nlopt_set_min_objective(opt, myfunc, &data_in );

  nlopt_set_xtol_rel(opt, 1e-4);
  double minf; /* the minimum objective value, upon return */
  double x[2] = {0.5, 0.5};  /* some initial guess */
  nlopt_result result = nlopt_optimize(opt, x, &minf);
  Rcpp::Rcout << "result:" << result;
    return;
}