gpt4 book ai didi

c++ - Rcpp 错误 : invalid static_cast from type 'Rcpp::Vector<13, Rcpp::PreserveStorage>' to type 'int'

转载 作者:行者123 更新时间:2023-11-30 05:34:08 30 4
gpt4 key购买 nike

我目前正在为类作业编写模拟退火算法(“解决”背包问题),并想在 Rcpp 中完成(我必须使用 R,而 Rcpp 更快)。

Rcpp 一直给我以下错误

invalid static_cast from type 'Rcpp::Vector<13, Rcpp::PreserveStorage>' to type 'int'

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

过去几个小时我一直在谷歌上搜索,但无济于事,而且我不知道问题出在哪里。有人有什么想法吗?谢谢。

#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);
}

编译错误:

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

最佳答案

就是这些作业:

tempVector = objValue[curObj];
tempVector = objWeight[curObj];
...

objWeightobjValueNumericVector 并且您正在分配任何 NumericVector::operator[] 返回到另一个 NumericVector。我想那不是 NumericVector 而是单个元素,这就是您的代码无法编译的原因。

我不知道 Rcpp,但我确定您想执行一些其他操作(例如附加元素?)。

关于c++ - Rcpp 错误 : invalid static_cast from type 'Rcpp::Vector<13, Rcpp::PreserveStorage>' to type 'int' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34376453/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com