Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: superblock
Type: Package
Title: Analyses of potential superblock
Version: 0.1.0.113
Version: 0.1.0.117
Authors@R:
person(given = "Mark",
family = "Padgham",
Expand Down
7 changes: 7 additions & 0 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,10 @@
rcpp_park_search <- function (graph, edge_map_in, edge_map_rev_in, prop_full, start_edge, ntrials) {
.Call (`_superblock_rcpp_park_search`, graph, edge_map_in, edge_map_rev_in, prop_full, start_edge, ntrials)
}

#' rcpp_park_fill
#'
#' @noRd
rcpp_park_fill <- function (graph, edge_map_in, edge_map_rev_in, prop_full, ntrials) {
.Call (`_superblock_rcpp_park_fill`, graph, edge_map_in, edge_map_rev_in, prop_full, ntrials)
}
2 changes: 1 addition & 1 deletion codemeta.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"codeRepository": "https://github.com/UrbanAnalyst/superblock-ms",
"issueTracker": "https://github.com/UrbanAnalyst/superblock-ms/issues",
"license": "https://spdx.org/licenses/GPL-3.0",
"version": "0.1.0.113",
"version": "0.1.0.117",
"programmingLanguage": {
"@type": "ComputerLanguage",
"name": "R",
Expand Down
16 changes: 16 additions & 0 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,25 @@ BEGIN_RCPP
return rcpp_result_gen;
END_RCPP
}
// rcpp_park_fill
Rcpp::NumericVector rcpp_park_fill(const Rcpp::DataFrame graph, const Rcpp::List edge_map_in, const Rcpp::List edge_map_rev_in, const double prop_full, const int ntrials);
RcppExport SEXP _superblock_rcpp_park_fill(SEXP graphSEXP, SEXP edge_map_inSEXP, SEXP edge_map_rev_inSEXP, SEXP prop_fullSEXP, SEXP ntrialsSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< const Rcpp::DataFrame >::type graph(graphSEXP);
Rcpp::traits::input_parameter< const Rcpp::List >::type edge_map_in(edge_map_inSEXP);
Rcpp::traits::input_parameter< const Rcpp::List >::type edge_map_rev_in(edge_map_rev_inSEXP);
Rcpp::traits::input_parameter< const double >::type prop_full(prop_fullSEXP);
Rcpp::traits::input_parameter< const int >::type ntrials(ntrialsSEXP);
rcpp_result_gen = Rcpp::wrap(rcpp_park_fill(graph, edge_map_in, edge_map_rev_in, prop_full, ntrials));
return rcpp_result_gen;
END_RCPP
}

static const R_CallMethodDef CallEntries[] = {
{"_superblock_rcpp_park_search", (DL_FUNC) &_superblock_rcpp_park_search, 6},
{"_superblock_rcpp_park_fill", (DL_FUNC) &_superblock_rcpp_park_fill, 5},
{NULL, NULL, 0}
};

Expand Down
78 changes: 73 additions & 5 deletions src/park_search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ void parksearch::makeEdgeMaps (
}
}

std::vector <size_t> parksearch::randomOrder (int ntotal, size_t n) {
std::vector <size_t> parksearch::randomOrder (const int ntotal, const size_t n) {

std::vector <double> xrand = Rcpp::as<std::vector<double>> (Rcpp::runif (ntotal));

Expand All @@ -40,7 +40,9 @@ std::vector <size_t> parksearch::randomOrder (int ntotal, size_t n) {
return res;
}

std::vector <double> parksearch::fillParkingSpaces (std::vector <int> num_spaces, double prop_full) {
std::vector <double> parksearch::fillParkingSpaces (
const std::vector <int> &num_spaces,
const double prop_full) {

const size_t n = num_spaces.size();
std::vector <double> p_empty (n, 0.0);
Expand Down Expand Up @@ -81,9 +83,9 @@ std::vector <double> parksearch::fillParkingSpaces (std::vector <int> num_spaces
std::vector<double> parksearch::oneParkSearch (
const parksearch::EdgeMapType &edgeMap,
const parksearch::EdgeMapType &edgeMapRev,
std::vector <double> &dist,
std::vector <double> &d_to_empty,
std::vector <double> &p_empty,
const std::vector <double> &dist,
const std::vector <double> &d_to_empty,
const std::vector <double> &p_empty,
const size_t nedges,
const size_t start_edge
) {
Expand Down Expand Up @@ -213,3 +215,69 @@ Rcpp::DataFrame rcpp_park_search (const Rcpp::DataFrame graph,

return res;
}

//' rcpp_park_fill
//'
//' @noRd
// [[Rcpp::export]]
Rcpp::NumericVector rcpp_park_fill (const Rcpp::DataFrame graph,
const Rcpp::List edge_map_in,
const Rcpp::List edge_map_rev_in,
const double prop_full,
const int ntrials)
{

Rcpp::RNGScope scope;

parksearch::EdgeMapType edgeMap, edgeMapRev;
parksearch::makeEdgeMaps (edge_map_in, edge_map_rev_in, edgeMap, edgeMapRev);

std::vector <double> dist = graph ["d"];
std::vector <int> num_spaces = graph ["np"];
size_t nedges = static_cast <size_t> (graph.nrow ());

int ntotal = 0;
for (auto n: num_spaces) {
ntotal += n;
}
int n_full = floor (ntotal * prop_full);

Rcpp::NumericVector d (ntrials, 0.0);

for (size_t n = 0; n < static_cast<size_t>(ntrials); n++) {

double prop_i = prop_full;

// Incrementally fill spaces and add all distances:
for (int i = n_full; i < ntotal; i++) {

std::vector <double> p_empty = parksearch::fillParkingSpaces (num_spaces, prop_i);

std::vector <double> d_to_empty(nedges, 0.0);
parksearch::fill_d_to_empty(num_spaces, dist, d_to_empty, prop_i);

// oneParkSearch assumes start_edge is 1-based R index:
int start_edge = Rcpp::as<int>(Rcpp::runif(1, 0, nedges)) + 1L;
bool found = false;
int nattempts = 0;
std::vector<double> res;
while (!found && nattempts < 100) {
res = parksearch::oneParkSearch (
edgeMap, edgeMapRev, dist, d_to_empty, p_empty, nedges,
static_cast<size_t>(start_edge)
);
found = res[2] > 0;
nattempts++;
}

if (res[2] > 0) {
d[n] += res[2];
}

prop_i = static_cast<double>(i) / static_cast<double>(ntotal);
}

}

return d;
}
18 changes: 13 additions & 5 deletions src/park_search.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ void makeEdgeMaps (
EdgeMapType &edgeMap,
EdgeMapType &edgeMapRev);

std::vector <size_t> randomOrder (int ntotal, size_t n);
std::vector <size_t> randomOrder (const int ntotal, const size_t n);

std::vector <double> fillParkingSpaces (std::vector <int> num_spaces, double prop_full);
std::vector <double> fillParkingSpaces (
const std::vector <int> &num_spaces,
const double prop_full);

void fill_d_to_empty (
const std::vector <int> &num_spaces,
Expand All @@ -37,9 +39,9 @@ void fill_d_to_empty (
std::vector<double> oneParkSearch (
const parksearch::EdgeMapType &edgeMap,
const parksearch::EdgeMapType &edgeMapRev,
std::vector <double> &dist,
std::vector <double> &d_to_empty,
std::vector <double> &p_empty,
const std::vector <double> &dist,
const std::vector <double> &d_to_empty,
const std::vector <double> &p_empty,
const size_t nedges,
const size_t start_edge
);
Expand All @@ -52,3 +54,9 @@ Rcpp::DataFrame rcpp_park_search (const Rcpp::DataFrame graph,
const double prop_full,
const int start_edge,
const int ntrials);

Rcpp::NumericVector rcpp_park_fill (const Rcpp::DataFrame graph,
const Rcpp::List edge_map_in,
const Rcpp::List edge_map_rev_in,
const double prop_full,
const int ntrials);