我目前正在编写用于类分配的模拟退火算法('解决'背包问题),并希望在Rcpp中进行编码(我必须使用R,并且Rcpp更快)。
Rcpp一直在给我以下错误
invalid static_cast from type 'Rcpp::Vector<13, Rcpp::PreserveStorage>' to type 'int'
指的是Rcpp内部caster.h
过去几个小时我一直在谷歌上搜索,但没有结果,我不知道问题可能在哪里。有没有人有任何想法?感谢。
#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
答案 0 :(得分:1)
这些作业:
tempVector = objValue[curObj];
tempVector = objWeight[curObj];
...
objWeight
和objValue
为NumericVector
,您可以将NumericVector::operator[]
次返回的内容分配给另一个NumericVector
。我想那不是NumericVector
而是单个元素,这就是你的代码无法编译的原因。
我不知道Rcpp,但我确定你想做其他一些动作(比如附加元素?)。