将数据传递到Rcpp中的nlopt?

时间:2018-07-09 07:42:20

标签: r rcpp armadillo nlopt

这是一个非常简单的问题,但是我还不能在网上找到答案。

希望我的最新尝试,这是最新的编译器输出: 注意:候选函数不可行:没有已知的从'double(unsigned int,const double *,void *,void )'到'nlopt_func'(aka'double()(unsigned int,const 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, 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;
}

1 个答案:

答案 0 :(得分:0)

知道了,愚蠢的答案被证明是正确的,只是将'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;
}