0

我最近一直在将一些 R 代码转换为 Rcpp 函数。我正在模拟人们在停车场停车。我有一个功能,可以根据他们进入的大门和停车场的满载情况来选择一个人将停在哪个停车场。

#include <Rcpp.h>
#include <numeric>
#include <chrono>

// [[Rcpp::plugins(cpp11)]]

using namespace Rcpp;
// [[Rcpp::export]]
std::string pickLotcpp(std::string gate,DataFrame dist,DataFrame curr,NumericVector maxDist = 0.005) {
  std::vector<std::string> gates = Rcpp::as<std::vector<std::string> >(dist["inGate"]);
  std::vector<std::string> lots = Rcpp::as<std::vector<std::string> >(dist["lot"]);
  NumericVector d = dist["dist"];

  std::vector<std::string> currLots = Rcpp::as<std::vector<std::string> >(curr["Lot"]);
  NumericVector cap = curr["Cap"];
  NumericVector util = curr["Util"];
  NumericVector percFree = (cap - util)/cap;

  std::vector<std::string> relLot;
  NumericVector relD;
  int n = gates.size();
  for(int i = 0; i < n; i++){
    if(gates[i] == gate){
      if(d[i] <= maxDist[0]){
        relLot.push_back(lots[i]);
        relD.push_back(pow(d[i],-2));
      }
    }
  }

  n = relLot.size();
  int n2 = currLots.size();
  NumericVector relPerc;
  for(int i = 0; i < n; i++){
    for(int j = 0; j < n2; j++){
      if(relLot[i] == currLots[j]){
        relPerc.push_back(percFree[j]);
      }
    }
  }

  relD = relD*relPerc;
  NumericVector csV(relD.size());
  std::partial_sum(relD.begin(), relD.end(), csV.begin());
  NumericVector::iterator mv;
  mv = std::max_element(csV.begin(),csV.end());
  double maxV = *mv;
  unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
  std::mt19937 gen(seed);
  std::uniform_real_distribution<> dis(0, maxV);
  double rv = dis(gen);
  int done = 0;
  int i = 0;
  std::string selGate;
  while(done < 1){
    if(csV[i] >= rv){
      selGate = relLot[i];
      done = 1;
    }
    i++;
  }

  return selGate;
}

在 R 中效果很好:

fakeDist = structure(list(inGate = c("A", "A", "B", "B"), lot = c("Y", "Z", "Y", "Z"), dist = c(0.001, 0.003, 0.003, 0.001)), .Names = c("inGate", "lot", "dist"), row.names = c(NA, 4L), class = c("tbl_df", "tbl", "data.frame"))
fakeStatus = structure(list(Lot = c("Y", "Z"), Cap = c(100, 100), Util = c(0, 0)), .Names = c("Lot", "Cap", "Util"), row.names = c(NA, 2L), class = c("tbl_df", "tbl", "data.frame"))

pickLotcpp("A",fakeDist,fakeStatus)

#> [1] "Y"

现在我正在尝试编写将循环遍历所有门活动并按顺序停放人员的函数。所以我有这个 Rcpp 函数:

// [[Rcpp::export]]
List test(DataFrame records, DataFrame currentLoc, DataFrame dist, 
          DataFrame currentStatus, NumericVector times){
  List out(times.size());
  NumericVector recID = records["ID"];
  NumericVector recTime = records["Time"];
  NumericVector recDir = records["Dir"];
  std::vector<std::string> Gate = Rcpp::as<std::vector<std::string> >(records["Gate"]);
  NumericVector currState = currentLoc["State"];
  std::vector<std::string> currLot = Rcpp::as<std::vector<std::string> >(currentLoc["Lot"]);

  out[0] = pickLotcpp(Gate[0],dist,currentStatus);

  return out;
}

这在同一个文件中,在 pickLotcpp 下。它编译得很好,但是当被调用时会导致 R 崩溃。

fakeData = structure(list(ID = c(1, 2, 3), Time = c(1, 2, 3), Dir = c(1, 1, 1), Gate = c("A", "A", "B")), .Names = c("ID", "Time", "Dir", "Gate"), row.names = c(NA, 3L), class = c("tbl_df", "tbl", "data.frame"))
fakeLoc = structure(list(ID = c(1, 2, 3), State = c(0, 0, 0), Lot = c("", "", "")), .Names = c("ID", "State", "Lot"), row.names = c(NA, 3L), class = c("tbl_df", "tbl", "data.frame"))

a = test(fakeData, fakeLoc, fakeDist, fakeStatus, 10)

我编写了其他 Rcpp 代码,其中函数调用函数并且它们工作正常。我能想到的唯一一件事是,我正在将作为输入的 DataFrame 直接传递给另一个函数,但我找不到任何说我不能的东西。我不是专业的 C++ 程序员——几周前我才开始研究它,这让我很困惑。

我如何通过传递所需的距离和状态数据帧pickLotcpp来调用?test

4

1 回答 1

1

它似乎与 Rcpp 无关。

你能仔细检查一下线路吗

selGate = relLot[i];

relLot可能是空的。

于 2015-11-20T17:24:41.027 回答