首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rcpp错误:从'Rcpp::Vector<13,Rcpp::PreserveStorage>‘类型到'int’类型的无效Rcpp::PreserveStorage>

Rcpp错误:从'Rcpp::Vector<13,Rcpp::PreserveStorage>‘类型到'int’类型的无效Rcpp::PreserveStorage>
EN

Stack Overflow用户
提问于 2015-12-19 23:23:31
回答 1查看 698关注 0票数 2

我目前正在为类分配(“解决”背包问题)编写模拟退火算法,并希望在Rcpp中进行(我必须使用R,而且Rcpp更快)。

Rcpp给了我以下错误

代码语言:javascript
复制
invalid static_cast from type 'Rcpp::Vector<13, Rcpp::PreserveStorage>' to type 'int'

这指的是Rcpp内部caster.h的第30行

在过去的几个小时里,我一直在谷歌搜索,但没有结果,我也不知道问题出在哪里。有人有什么想法吗?谢谢。

代码语言:javascript
复制
#include <RcppArmadilloExtensions/sample.h>
#include <Rcpp.h>
// [[Rcpp::depends(RcppArmadillo)]]
using namespace Rcpp;

// [[Rcpp::export]]
List ska(NumericVector objWeight,
         NumericVector objValue,
         float maxWeight, 
         float tau,
         int N) {
  // m .... weight of objects
  // V .... value of objects
  // M .... maximum weight allowed
  // tau .. starting temperature
  // N .... number of iterations

  RNGScope scope;

  int nObj;
  IntegerVector Obj;

  nObj = objWeight.length();
  Obj = seq_len(nObj);

  IntegerVector curObj = IntegerVector::create(1);
  float curValue;
  float curWeight;

  NumericVector tempVector;
  IntegerVector tempIn;
  IntegerVector tempOut;
  float tempSum;

  tempVector = objValue[curObj];
  curValue = std::accumulate(tempVector.begin(), tempVector.end(), 0.0);

  tempVector = objWeight[curObj];
  curWeight = std::accumulate(tempVector.begin(), tempVector.end(), 0.0);

  IntegerVector bestObj;
  float bestValue;
  float bestWeight;

  bestObj = curObj;
  bestValue = curValue;
  bestWeight = curWeight;

  IntegerVector tempObj;
  float tempValue;
  float tempWeight;

  float testLHS;
  float testRHS;

  LogicalVector subsetOther;
  LogicalVector subsetTemp;

  IntegerVector otherObj;

  for (int i = 0; i < N; i++) {
    tempObj = curObj;

    tempVector = objWeight[tempObj - 1];
    tempSum = std::accumulate(tempVector.begin(), tempVector.end(), 0.0);

    while (tempSum <= maxWeight) {
      // adding random objects until over maxWeight
      subsetOther = !in(tempObj, Obj);
      otherObj = Obj[subsetOther];

      tempIn = RcppArmadillo::sample(otherObj, 1, false);
      tempObj.push_back(tempIn);

      tempVector = objWeight[tempObj - 1];
      tempSum = std::accumulate(tempVector.begin(), tempVector.end(), 0.0);
    }

    while (tempSum > maxWeight) {
      // removing random objects until under maxWeight
      tempOut = RcppArmadillo::sample(tempObj, 1, false);

      subsetTemp = !in(tempOut, tempObj);
      tempObj = tempObj[subsetTemp];

      tempVector = objWeight[tempObj - 1];
      tempSum = std::accumulate(tempVector.begin(), tempVector.end(), 0.0);
    }

    // calculate the values for this iteration
    tempWeight = tempSum;

    tempVector = objValue[tempObj - 1];
    tempValue = std::accumulate(tempVector.begin(), tempVector.end(), 0.0);

    if (tempValue > bestValue) {
      bestObj = tempObj;
      bestValue = tempValue;
      bestWeight = tempWeight;
    }

    // candidate acceptance
    testLHS = R::runif(0,1);
    testRHS = exp((curValue - tempValue) / (tau / i));

    if(testLHS < testRHS) {
      curObj = tempObj;
      curValue = tempValue;
    }
  }

  return List::create(_["Objects"] = bestObj,
                      _["Total_value"] = bestValue,
                      _["Total_weight"] = bestWeight);
}

编译器错误:

代码语言:javascript
复制
D:/R/library/Rcpp/include/Rcpp/internal/caster.h: In function 'TO Rcpp::internal::caster(FROM) [with FROM = Rcpp::Vector<13, Rcpp::PreserveStorage>, TO = int]':
D:/R/library/Rcpp/include/Rcpp/vector/converter.h:34:33:   instantiated from 'static Rcpp::internal::element_converter<RTYPE>::target Rcpp::internal::element_converter<RTYPE>::get(const T&) [with T = Rcpp::Vector<13, Rcpp::PreserveStorage>, int RTYPE = 13, Rcpp::internal::element_converter<RTYPE>::target = int]'
D:/R/library/Rcpp/include/Rcpp/vector/Vector.h:426:9:   instantiated from 'void Rcpp::Vector<RTYPE, StoragePolicy>::push_back(const T&) [with T = Rcpp::Vector<13, Rcpp::PreserveStorage>, int RTYPE = 13, StoragePolicy = Rcpp::PreserveStorage]'
ska.cpp:73:31:   instantiated from here
D:/R/library/Rcpp/include/Rcpp/internal/caster.h:30:29: error: invalid static_cast from type 'Rcpp::Vector<13, Rcpp::PreserveStorage>' to type 'int'
D:/R/library/Rcpp/include/Rcpp/internal/caster.h:31:1: warning: control reaches end of non-void function [-Wreturn-type]
make: *** [ska.o] Error 1
Warning message:
running command 'make -f "D:/R/etc/x64/Makeconf" -f "D:/R/share/make/winshlib.mk" SHLIB_LDFLAGS='$(SHLIB_CXXLDFLAGS)' SHLIB_LD='$(SHLIB_CXXLD)' SHLIB="sourceCpp_61.dll" WIN=64 TCLBIN=64 OBJECTS="ska.o"' had status 2 
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-12-19 23:29:48

就是这些任务:

代码语言:javascript
复制
tempVector = objValue[curObj];
tempVector = objWeight[curObj];
...

objWeightobjValueNumericVector,您要将任何NumericVector::operator[]返回给另一个NumericVector。我想这不是一个NumericVector,而是一个元素,这就是为什么您的代码不编译。

我不知道Rcpp,但我确信您希望执行其他操作(比如追加元素?)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34376453

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档