correctly handle trips with no stop times, fixes #46

This commit is contained in:
Patrick Brosi 2023-08-21 13:56:46 +02:00
parent 45f508bf34
commit 33a92077df
3 changed files with 41 additions and 20 deletions

View file

@ -776,6 +776,7 @@ EdgeCandMap ShapeBuilder::getECM(
double avgChildT = 0; double avgChildT = 0;
if (chldTrNd.trips) avgChildT = chldTrNd.accTime / chldTrNd.trips; if (chldTrNd.trips) avgChildT = chldTrNd.accTime / chldTrNd.trips;
double timeDiff = avgChildT - avgT; double timeDiff = avgChildT - avgT;
if (timeDiff < 0) timeDiff = 0; if (timeDiff < 0) timeDiff = 0;
@ -812,7 +813,7 @@ double ShapeBuilder::timePen(int candTime, int schedTime) const {
int diff = abs(candTime - schedTime); int diff = abs(candTime - schedTime);
double cNorm = (diff) / standarddev; double cNorm = diff / standarddev;
return cNorm * cNorm; return cNorm * cNorm;
} }

View file

@ -5,6 +5,7 @@
#include <map> #include <map>
#include <string> #include <string>
#include <vector> #include <vector>
#include "TripTrie.h" #include "TripTrie.h"
#include "ad/cppgtfs/gtfs/Feed.h" #include "ad/cppgtfs/gtfs/Feed.h"
#include "pfaedle/gtfs/Feed.h" #include "pfaedle/gtfs/Feed.h"
@ -32,11 +33,14 @@ bool TripTrie<TRIP>::addTrip(TRIP* trip, const RoutingAttrs& rAttrs,
// _____________________________________________________________________________ // _____________________________________________________________________________
template <typename TRIP> template <typename TRIP>
bool TripTrie<TRIP>::add(TRIP* trip, const RoutingAttrs& rAttrs, bool TripTrie<TRIP>::add(TRIP* trip, const RoutingAttrs& rAttrs, bool timeEx) {
bool timeEx) {
if (trip->getStopTimes().size() == 0) return false; if (trip->getStopTimes().size() == 0) return false;
int startSecs = trip->getStopTimes().front().getDepartureTime().seconds(); int startSecs = 0;
if (!trip->getStopTimes().front().getDepartureTime().empty()) {
startSecs = trip->getStopTimes().front().getDepartureTime().seconds();
}
size_t curNdId = 0; size_t curNdId = 0;
for (size_t stId = 0; stId < trip->getStopTimes().size(); stId++) { for (size_t stId = 0; stId < trip->getStopTimes().size(); stId++) {
@ -48,7 +52,11 @@ bool TripTrie<TRIP>::add(TRIP* trip, const RoutingAttrs& rAttrs,
st.getStop()->getLng()); st.getStop()->getLng());
if (stId > 0) { if (stId > 0) {
int arrTime = st.getArrivalTime().seconds() - startSecs; int arrTime = startSecs;
if (!st.getArrivalTime().empty()) {
arrTime = st.getArrivalTime().seconds() - startSecs;
}
size_t arrChild = size_t arrChild =
getMatchChild(curNdId, name, platform, pos, arrTime, timeEx); getMatchChild(curNdId, name, platform, pos, arrTime, timeEx);
@ -66,7 +74,11 @@ bool TripTrie<TRIP>::add(TRIP* trip, const RoutingAttrs& rAttrs,
} }
if (stId < trip->getStopTimes().size() - 1) { if (stId < trip->getStopTimes().size() - 1) {
int depTime = st.getDepartureTime().seconds() - startSecs; int depTime = startSecs;
if (!st.getDepartureTime().empty()) {
depTime = st.getDepartureTime().seconds() - startSecs;
}
size_t depChild = size_t depChild =
getMatchChild(curNdId, name, platform, pos, depTime, timeEx); getMatchChild(curNdId, name, platform, pos, depTime, timeEx);
@ -109,7 +121,11 @@ size_t TripTrie<TRIP>::get(TRIP* trip, bool timeEx) {
st.getStop()->getLng()); st.getStop()->getLng());
if (stId > 0) { if (stId > 0) {
int arrTime = st.getArrivalTime().seconds() - startSecs; int arrTime = startSecs;
if (!st.getArrivalTime().empty()) {
arrTime = st.getArrivalTime().seconds() - startSecs;
}
size_t arrChild = size_t arrChild =
getMatchChild(curNdId, name, platform, pos, arrTime, timeEx); getMatchChild(curNdId, name, platform, pos, arrTime, timeEx);
@ -122,7 +138,11 @@ size_t TripTrie<TRIP>::get(TRIP* trip, bool timeEx) {
} }
if (stId < trip->getStopTimes().size() - 1) { if (stId < trip->getStopTimes().size() - 1) {
int depTime = st.getDepartureTime().seconds() - startSecs; int depTime = startSecs;
if (!st.getDepartureTime().empty()) {
depTime = st.getDepartureTime().seconds() - startSecs;
}
size_t depChild = size_t depChild =
getMatchChild(curNdId, name, platform, pos, depTime, timeEx); getMatchChild(curNdId, name, platform, pos, depTime, timeEx);
@ -141,8 +161,8 @@ size_t TripTrie<TRIP>::get(TRIP* trip, bool timeEx) {
// _____________________________________________________________________________ // _____________________________________________________________________________
template <typename TRIP> template <typename TRIP>
size_t TripTrie<TRIP>::insert(const ad::cppgtfs::gtfs::Stop* stop, size_t TripTrie<TRIP>::insert(const ad::cppgtfs::gtfs::Stop* stop,
const RoutingAttrs& rAttrs, const POINT& pos, int time, const RoutingAttrs& rAttrs, const POINT& pos,
bool arr, size_t parent) { int time, bool arr, size_t parent) {
_nds.emplace_back(TripTrieNd{stop, _nds.emplace_back(TripTrieNd{stop,
stop->getName(), stop->getName(),
stop->getPlatformCode(), stop->getPlatformCode(),
@ -168,9 +188,10 @@ const std::vector<pfaedle::router::TripTrieNd>& TripTrie<TRIP>::getNds() const {
// _____________________________________________________________________________ // _____________________________________________________________________________
template <typename TRIP> template <typename TRIP>
size_t TripTrie<TRIP>::getMatchChild(size_t parentNid, const std::string& stopName, size_t TripTrie<TRIP>::getMatchChild(size_t parentNid,
const std::string& platform, POINT pos, int time, const std::string& stopName,
bool timeEx) const { const std::string& platform, POINT pos,
int time, bool timeEx) const {
for (size_t child : _nds[parentNid].childs) { for (size_t child : _nds[parentNid].childs) {
// TODO(patrick): use similarity classification here? // TODO(patrick): use similarity classification here?
if (_nds[child].stopName == stopName && _nds[child].platform == platform && if (_nds[child].stopName == stopName && _nds[child].platform == platform &&
@ -215,8 +236,7 @@ void TripTrie<TRIP>::toDot(std::ostream& os, const std::string& rootName,
// _____________________________________________________________________________ // _____________________________________________________________________________
template <typename TRIP> template <typename TRIP>
const std::map<size_t, std::vector<TRIP*>>& const std::map<size_t, std::vector<TRIP*>>& TripTrie<TRIP>::getNdTrips() const {
TripTrie<TRIP>::getNdTrips() const {
return _ndTrips; return _ndTrips;
} }

View file

@ -271,21 +271,21 @@ inline std::string getHomeDir() {
} }
// _____________________________________________________________________________ // _____________________________________________________________________________
inline char* readableSize(double size, char* buf) { inline char* readableSize(double size, size_t n, char* buf) {
int i = 0; int i = 0;
const char* units[] = {"B", "kB", "MB", "GB", "TB", "PB"}; const char* units[] = {"B", "kB", "MB", "GB", "TB", "PB"};
while (size > 1024 && i < 5) { while (size > 1024 && i < 5) {
size /= 1024; size /= 1024;
i++; i++;
} }
sprintf(buf, "%.*f %s", i, size, units[i]); snprintf(buf, n, "%.*f %s", i, size, units[i]);
return buf; return buf;
} }
// _____________________________________________________________________________ // _____________________________________________________________________________
inline std::string readableSize(double size) { inline std::string readableSize(double size) {
char buffer[30]; char buffer[30];
return readableSize(size, buffer); return readableSize(size, 30, buffer);
} }
// _____________________________________________________________________________ // _____________________________________________________________________________