diff --git a/connection_scan_algorithm/include/calculator.hpp b/connection_scan_algorithm/include/calculator.hpp index a70c19db..821c4fbc 100644 --- a/connection_scan_algorithm/include/calculator.hpp +++ b/connection_scan_algorithm/include/calculator.hpp @@ -126,6 +126,7 @@ namespace TrRouting std::vector> forwardJourneys; // index = stop index, tuple: final enter connection, final exit connection, final footpath, final exit trip index, transfer travel time, is same stop transfer (first, second, third and fourth values = -1 for access and egress journeys) std::vector> forwardEgressJourneys; // index = stop index, tuple: final enter connection, final exit connection, final footpath, final exit trip index, transfer travel time, is same stop transfer (first, second, third and fourth values = -1 for access and egress journeys) std::vector> reverseJourneys; // index = stop index, tuple: final enter connection, final exit connection, final footpath, final exit trip index, transfer travel time, is same stop transfer (first, second, third and fourth values = -1 for access and egress journeys) + std::vector> reverseAccessJourneys; // index = stop index, tuple: final enter connection, final exit connection, final footpath, final exit trip index, transfer travel time, is same stop transfer (first, second, third and fourth values = -1 for access and egress journeys) int maxTimeValue; int minAccessTravelTime; int maxEgressTravelTime; diff --git a/connection_scan_algorithm/src/forward_calculation.cpp b/connection_scan_algorithm/src/forward_calculation.cpp index 03e11a3f..a1efd64b 100644 --- a/connection_scan_algorithm/src/forward_calculation.cpp +++ b/connection_scan_algorithm/src/forward_calculation.cpp @@ -90,14 +90,16 @@ namespace TrRouting { footpathStopArrivalIndex = std::get<1>(footpaths[footpathIndex]); footpathTravelTime = std::get<2>(footpaths[footpathIndex]); - - if (footpathTravelTime <= params.maxTransferWalkingTravelTimeSeconds && footpathTravelTime + params.minWaitingTimeSeconds + connectionArrivalTime < stopsTentativeTime[footpathStopArrivalIndex]) + if (footpathTravelTime <= params.maxTransferWalkingTravelTimeSeconds) { - stopsTentativeTime[footpathStopArrivalIndex] = footpathTravelTime + connectionArrivalTime + params.minWaitingTimeSeconds; - forwardJourneys[footpathStopArrivalIndex] = std::make_tuple(tripsEnterConnection[tripIndex], i, footpathIndex, tripIndex, footpathTravelTime, (stopArrivalIndex == footpathStopArrivalIndex ? 1 : -1)); - if (stopArrivalIndex == footpathStopArrivalIndex) + if (footpathTravelTime + params.minWaitingTimeSeconds + connectionArrivalTime < stopsTentativeTime[footpathStopArrivalIndex]) { - forwardEgressJourneys[footpathStopArrivalIndex] = forwardJourneys[footpathStopArrivalIndex]; + stopsTentativeTime[footpathStopArrivalIndex] = footpathTravelTime + connectionArrivalTime + params.minWaitingTimeSeconds; + forwardJourneys[footpathStopArrivalIndex] = std::make_tuple(tripsEnterConnection[tripIndex], i, footpathIndex, tripIndex, footpathTravelTime, (stopArrivalIndex == footpathStopArrivalIndex ? 1 : -1)); + } + if (stopArrivalIndex == footpathStopArrivalIndex && (std::get<4>(forwardEgressJourneys[footpathStopArrivalIndex]) == -1 || std::get(forwardConnections[std::get<1>(forwardEgressJourneys[footpathStopArrivalIndex])]) > connectionArrivalTime)) + { + forwardEgressJourneys[footpathStopArrivalIndex] = std::make_tuple(tripsEnterConnection[tripIndex], i, footpathIndex, tripIndex, footpathTravelTime, 1); } } footpathIndex++; @@ -128,7 +130,7 @@ namespace TrRouting { egressTravelTime = stopsEgressTravelTime[egressFootpath.first]; egressStopArrivalTime = std::get(forwardConnections[egressExitConnection]) + egressTravelTime; - //std::cerr << egressTravelTime << " - " << egressStopArrivalTime << std::endl; + std::cerr << stops[egressFootpath.first].name << ": " << egressTravelTime << " - " << Toolbox::convertSecondsToFormattedTime(egressStopArrivalTime) << std::endl; if (egressStopArrivalTime >= 0 && egressStopArrivalTime < MAX_INT && egressStopArrivalTime < bestArrivalTime) { bestArrivalTime = egressStopArrivalTime; diff --git a/connection_scan_algorithm/src/preparations.cpp b/connection_scan_algorithm/src/preparations.cpp index 32fa5ecf..37cb03d5 100644 --- a/connection_scan_algorithm/src/preparations.cpp +++ b/connection_scan_algorithm/src/preparations.cpp @@ -83,6 +83,7 @@ namespace TrRouting forwardJourneys = std::vector>(stops.size()); forwardEgressJourneys = std::vector>(stops.size()); reverseJourneys = std::vector>(stops.size()); + reverseAccessJourneys = std::vector>(stops.size()); } diff --git a/connection_scan_algorithm/src/resets.cpp b/connection_scan_algorithm/src/resets.cpp index 554d3d76..764720b8 100644 --- a/connection_scan_algorithm/src/resets.cpp +++ b/connection_scan_algorithm/src/resets.cpp @@ -24,6 +24,7 @@ namespace TrRouting std::fill(forwardJourneys.begin(), forwardJourneys.end(), std::make_tuple(-1,-1,-1,-1,-1,-1)); std::fill(forwardEgressJourneys.begin(), forwardEgressJourneys.end(), std::make_tuple(-1,-1,-1,-1,-1,-1)); std::fill(reverseJourneys.begin(), reverseJourneys.end(), std::make_tuple(-1,-1,-1,-1,-1,-1)); + std::fill(reverseAccessJourneys.begin(), reverseAccessJourneys.end(), std::make_tuple(-1,-1,-1,-1,-1,-1)); accessFootpaths.clear(); egressFootpaths.clear(); @@ -139,7 +140,6 @@ namespace TrRouting { minEgressTravelTime = egressFootpath.second; } - stopsEgressTravelTime[egressFootpath.first] = egressFootpath.second; //stopsD[egressFootpath.first] = egressFootpath.second; //result.json += "origin_stop: " + stops[accessFootpath.first].name + " - " + Toolbox::convertSecondsToFormattedTime(stopsTentativeTime[accessFootpath.first]) + "\n"; //result.json += std::to_string((int)(ceil(egressFootpath.second))) + ","; diff --git a/connection_scan_algorithm/src/reverse_calculation.cpp b/connection_scan_algorithm/src/reverse_calculation.cpp index 01a074ad..21b095a7 100644 --- a/connection_scan_algorithm/src/reverse_calculation.cpp +++ b/connection_scan_algorithm/src/reverse_calculation.cpp @@ -38,7 +38,7 @@ namespace TrRouting for(auto & connection : reverseConnections) { // ignore connections before departure time + minimum access travel time: - if (std::get(connection) <= arrivalTimeSeconds + minEgressTravelTime) + if (std::get(connection) <= arrivalTimeSeconds - minEgressTravelTime) { tripIndex = std::get(connection); @@ -60,7 +60,7 @@ namespace TrRouting if (tripExitConnectionIndex != -1 || stopArrivalTentativeTime >= connectionArrivalTime) { - if (std::get(connection) == 1 && (tripExitConnectionIndex == -1/* || (std::get<0>(reverseJourneys[stopArrivalIndex]) == -1 && std::get<4>(reverseJourneys[stopArrivalIndex]) >= 0 && std::get<4>(reverseJourneys[stopArrivalIndex]) < tripsExitConnectionTransferTravelTime[tripIndex])*/)) + if (std::get(connection) == 1 && (tripExitConnectionIndex == -1 || (std::get<0>(reverseJourneys[stopArrivalIndex]) == -1 && std::get<4>(reverseJourneys[stopArrivalIndex]) >= 0 && std::get<4>(reverseJourneys[stopArrivalIndex]) <= tripsExitConnectionTransferTravelTime[tripIndex]))) // <= to make sure we get the same result as forward calculation, which uses > { tripsExitConnection[tripIndex] = i; tripsExitConnectionTransferTravelTime[tripIndex] = std::get<4>(reverseJourneys[stopArrivalIndex]); @@ -86,10 +86,17 @@ namespace TrRouting footpathStopDepartureIndex = std::get<1>(footpaths[footpathIndex]); footpathTravelTime = std::get<2>(footpaths[footpathIndex]); - if (footpathTravelTime <= params.maxTransferWalkingTravelTimeSeconds && connectionDepartureTime - footpathTravelTime - params.minWaitingTimeSeconds > stopsReverseTentativeTime[footpathStopDepartureIndex]) + if (footpathTravelTime <= params.maxTransferWalkingTravelTimeSeconds) { - stopsReverseTentativeTime[footpathStopDepartureIndex] = connectionDepartureTime - footpathTravelTime - params.minWaitingTimeSeconds; - reverseJourneys[footpathStopDepartureIndex] = std::make_tuple(i, tripsExitConnection[tripIndex], footpathIndex, tripIndex, footpathTravelTime, (stopDepartureIndex == footpathStopDepartureIndex ? 1 : -1)); + if (connectionDepartureTime - footpathTravelTime - params.minWaitingTimeSeconds >= stopsReverseTentativeTime[footpathStopDepartureIndex]) + { + stopsReverseTentativeTime[footpathStopDepartureIndex] = connectionDepartureTime - footpathTravelTime - params.minWaitingTimeSeconds; + reverseJourneys[footpathStopDepartureIndex] = std::make_tuple(i, tripsExitConnection[tripIndex], footpathIndex, tripIndex, footpathTravelTime, (stopDepartureIndex == footpathStopDepartureIndex ? 1 : -1)); + } + if (stopDepartureIndex == footpathStopDepartureIndex && (std::get<4>(reverseAccessJourneys[footpathStopDepartureIndex]) == -1 || std::get(reverseConnections[std::get<1>(reverseAccessJourneys[footpathStopDepartureIndex])]) <= connectionDepartureTime)) + { + reverseAccessJourneys[footpathStopDepartureIndex] = std::make_tuple(i, tripsExitConnection[tripIndex], footpathIndex, tripIndex, footpathTravelTime, 1); + } } footpathIndex++; } @@ -102,17 +109,27 @@ namespace TrRouting i++; } - // find best egress stop: + int accessStopDepartureTime {-1}; + int accessEnterConnection {-1}; + int accessTravelTime {-1}; + // find best access stop: if (!params.returnAllStopsResult) { i = 0; - for (auto & departureTime : stopsReverseTentativeTime) + for (auto & accessFootpath : accessFootpaths) { - if (stopsAccessTravelTime[i] >= 0 && departureTime >= 0 && departureTime - stopsAccessTravelTime[i] > bestDepartureTime) + accessEnterConnection = std::get<0>(reverseAccessJourneys[accessFootpath.first]); + if (accessEnterConnection != -1) { - bestDepartureTime = departureTime - stopsAccessTravelTime[i]; - bestAccessStopIndex = i; - bestAccessTravelTime = stopsAccessTravelTime[i]; + accessTravelTime = stopsAccessTravelTime[accessFootpath.first]; + accessStopDepartureTime = std::get(reverseConnections[accessEnterConnection]) - accessTravelTime - params.minWaitingTimeSeconds; + std::cerr << stops[accessFootpath.first].name << ": " << accessTravelTime << " t: " << trips[std::get(reverseConnections[accessEnterConnection])].id << " - " << Toolbox::convertSecondsToFormattedTime(accessStopDepartureTime) << std::endl; + if (accessStopDepartureTime >= 0 && accessStopDepartureTime < MAX_INT && accessStopDepartureTime > bestDepartureTime) + { + bestDepartureTime = accessStopDepartureTime; + bestAccessStopIndex = accessFootpath.first; + bestAccessTravelTime = accessTravelTime; + } } i++; } diff --git a/connection_scan_algorithm/src/reverse_journey.cpp b/connection_scan_algorithm/src/reverse_journey.cpp index 62a3b90e..b48bc746 100644 --- a/connection_scan_algorithm/src/reverse_journey.cpp +++ b/connection_scan_algorithm/src/reverse_journey.cpp @@ -13,6 +13,9 @@ namespace TrRouting int reachableStopsCount {0}; bool foundRoute {false}; + + //return result; + std::vector resultingStops; if (params.returnAllStopsResult) { @@ -44,7 +47,7 @@ namespace TrRouting Trip journeyStepTrip; Route journeyStepRoute; - int totalInVehicleTime { 0}; int transferArrivalTime {-1}; int firstArrivalTime {-1}; + int totalInVehicleTime { 0}; int transferArrivalTime {-1}; int firstDepartureTime {-1}; int totalWalkingTime { 0}; int transferReadyTime {-1}; int numberOfTransfers {-1}; int totalWaitingTime { 0}; int departureTime {-1}; int boardingSequence {-1}; int totalTransferWalkingTime { 0}; int arrivalTime {-1}; int unboardingSequence {-1}; @@ -59,7 +62,7 @@ namespace TrRouting legs.clear(); journey.clear(); - totalInVehicleTime = 0; transferArrivalTime = -1; firstArrivalTime = -1; + totalInVehicleTime = 0; transferArrivalTime = -1; firstDepartureTime = -1; totalWalkingTime = 0; transferReadyTime = -1; numberOfTransfers = -1; totalWaitingTime = 0; departureTime = -1; boardingSequence = -1; totalTransferWalkingTime = 0; arrivalTime = -1; unboardingSequence = -1; @@ -68,25 +71,29 @@ namespace TrRouting transferTime = -1; egressWalkingTime = -1; waitingTime = -1; accessWaitingTime = -1; - //std::cerr << stops[resultingStopIndex].name << " : " << std::get<0>(forwardJourneys[resultingStopIndex]) << std::endl; + std::cerr << stops[resultingStopIndex].name << " : " << std::get<0>(reverseAccessJourneys[resultingStopIndex]) << " tt: " << std::get<4>(reverseAccessJourneys[resultingStopIndex]) << std::endl; // recreate journey: - resultingStopJourneyStep = reverseJourneys[resultingStopIndex]; + resultingStopJourneyStep = reverseAccessJourneys[resultingStopIndex]; if (resultingStopJourneyStep == emptyJourneyStep) // ignore stops with no route { continue; } - std::cerr << stops[bestEgressStopIndex].name << std::endl; + //std::cerr << stops[bestEgressStopIndex].name << std::endl; i = 0; while ((std::get<0>(resultingStopJourneyStep) != -1 && std::get<1>(resultingStopJourneyStep) != -1)) { + if (journey.size() > 0) + { + std::get<4>(journey[journey.size()-1]) = std::get<4>(resultingStopJourneyStep); + } journey.push_back(resultingStopJourneyStep); - bestEgressStopIndex = std::get(reverseConnections[std::get<0>(resultingStopJourneyStep)]); - std::cerr << stops[std::get(reverseConnections[std::get<0>(resultingStopJourneyStep)])].name << " > " << stops[std::get(reverseConnections[std::get<1>(resultingStopJourneyStep)])].name << std::endl; + bestEgressStopIndex = std::get(reverseConnections[std::get<1>(resultingStopJourneyStep)]); + std::cerr << stops[std::get(reverseConnections[std::get<0>(resultingStopJourneyStep)])].name << " tt: " << std::get<4>(resultingStopJourneyStep) << " > " << stops[std::get(reverseConnections[std::get<1>(resultingStopJourneyStep)])].name << std::endl; resultingStopJourneyStep = reverseJourneys[bestEgressStopIndex]; + i++; } - if (!params.returnAllStopsResult) { json["steps"] = nlohmann::json::array(); @@ -124,10 +131,10 @@ namespace TrRouting numberOfTransfers += 1; legs.push_back(std::make_tuple(journeyStepTrip.id, journeyStepTrip.routeId, journeyStepTrip.routePathId, boardingSequence, unboardingSequence)); - if (i == journeyStepsCount - 1) // last leg + if (i == 1) // first leg { - accessWaitingTime = waitingTime; - firstArrivalTime = arrivalTime; + accessWaitingTime = waitingTime; + firstDepartureTime = departureTime; } else { @@ -179,7 +186,7 @@ namespace TrRouting stepJson["inVehicleTimeMinutes"] = Toolbox::convertSecondsToMinutes(inVehicleTime); json["steps"].push_back(stepJson); } - if (i > 0) // if not the first transit leg + if (i < journeyStepsCount - 2) // if not the last transit leg { totalTransferWalkingTime += transferTime; totalWalkingTime += transferTime; @@ -203,9 +210,9 @@ namespace TrRouting { transferTime = std::get<4>(journeyStep); - if (i == journeyStepsCount - 1) // access + if (i == 0) // access { - transferArrivalTime = departureTimeSeconds + transferTime; + transferArrivalTime = bestDepartureTime + transferTime; transferReadyTime = transferArrivalTime; totalWalkingTime += transferTime; accessWalkingTime = transferTime; @@ -216,9 +223,9 @@ namespace TrRouting stepJson["type"] = "access"; stepJson["travelTimeSeconds"] = transferTime; stepJson["travelTimeMinutes"] = Toolbox::convertSecondsToMinutes(transferTime); - stepJson["departureTime"] = Toolbox::convertSecondsToFormattedTime(departureTimeSeconds); + stepJson["departureTime"] = Toolbox::convertSecondsToFormattedTime(bestDepartureTime); stepJson["arrivalTime"] = Toolbox::convertSecondsToFormattedTime(transferArrivalTime); - stepJson["departureTimeSeconds"] = departureTimeSeconds; + stepJson["departureTimeSeconds"] = bestDepartureTime; stepJson["arrivalTimeSeconds"] = transferArrivalTime; stepJson["readyToBoardAt"] = Toolbox::convertSecondsToFormattedTime(transferReadyTime + params.minWaitingTimeSeconds); json["steps"].push_back(stepJson); @@ -251,14 +258,14 @@ namespace TrRouting if (params.returnAllStopsResult) { - departureTime = stopsTentativeTime[resultingStopIndex]; + arrivalTime = stopsTentativeTime[resultingStopIndex] - params.minWaitingTimeSeconds; if (arrivalTime - departureTimeSeconds <= params.maxTotalTravelTimeSeconds) { reachableStopsCount++; stopJson = {}; stopJson["id"] = stops[resultingStopIndex].id; - stopJson["departureTime"] = Toolbox::convertSecondsToFormattedTime(departureTime); - stopJson["totalTravelTimeSeconds"] = arrivalTimeSeconds - departureTime; + stopJson["arrivalTime"] = Toolbox::convertSecondsToFormattedTime(arrivalTime); + stopJson["totalTravelTimeSeconds"] = arrivalTime - departureTimeSeconds; stopJson["numberOfTransfers"] = numberOfTransfers; json["stops"].push_back(stopJson); } @@ -271,12 +278,12 @@ namespace TrRouting json["status"] = "success"; json["origin"] = {params.origin.longitude, params.origin.latitude}; json["destination"] = {params.destination.longitude, params.destination.latitude}; - json["departureTime"] = Toolbox::convertSecondsToFormattedTime(departureTime); - json["arrivalTime"] = Toolbox::convertSecondsToFormattedTime(arrivalTimeSeconds); - json["departureTimeSeconds"] = departureTime; - json["arrivalTimeSeconds"] = arrivalTimeSeconds; - json["totalTravelTimeMinutes"] = Toolbox::convertSecondsToMinutes(arrivalTimeSeconds - departureTime); - json["totalTravelTimeSeconds"] = arrivalTimeSeconds - departureTime; + json["departureTime"] = Toolbox::convertSecondsToFormattedTime(bestDepartureTime); + json["arrivalTime"] = Toolbox::convertSecondsToFormattedTime(arrivalTime); + json["departureTimeSeconds"] = bestDepartureTime; + json["arrivalTimeSeconds"] = arrivalTime; + json["totalTravelTimeMinutes"] = Toolbox::convertSecondsToMinutes(arrivalTime - bestDepartureTime); + json["totalTravelTimeSeconds"] = arrivalTime - bestDepartureTime; json["totalInVehicleTimeMinutes"] = Toolbox::convertSecondsToMinutes(totalInVehicleTime); json["totalInVehicleTimeSeconds"] = totalInVehicleTime; json["totalNonTransitTravelTimeMinutes"] = Toolbox::convertSecondsToMinutes(totalWalkingTime); @@ -295,12 +302,10 @@ namespace TrRouting json["firstWaitingTimeSeconds"] = accessWaitingTime; json["totalWaitingTimeMinutes"] = Toolbox::convertSecondsToMinutes(totalWaitingTime); json["totalWaitingTimeSeconds"] = totalWaitingTime; - json["minimumWaitingTimeBeforeEachBoardingMinutes"] = Toolbox::convertSecondsToMinutes(params.minWaitingTimeSeconds); - json["minimumWaitingTimeBeforeEachBoardingSeconds"] = params.minWaitingTimeSeconds; - - result.travelTimeSeconds = arrivalTimeSeconds - departureTime; - result.arrivalTimeSeconds = arrivalTimeSeconds; - result.departureTimeSeconds = departureTime; + + result.travelTimeSeconds = arrivalTime - bestDepartureTime; + result.arrivalTimeSeconds = arrivalTime; + result.departureTimeSeconds = bestDepartureTime; result.numberOfTransfers = numberOfTransfers; result.legs = legs; result.status = "success"; diff --git a/include/toolbox.hpp b/include/toolbox.hpp index 9d80b89a..5cb9ce05 100644 --- a/include/toolbox.hpp +++ b/include/toolbox.hpp @@ -24,7 +24,7 @@ namespace TrRouting { std::string formattedTime {""}; int hour {timeInSeconds / 3600}; - int minute {(int)ceil((float)timeInSeconds / 60.0 - (float)(hour * 60))}; + int minute {(int)floor((float)timeInSeconds / 60.0 - (float)(hour * 60))}; formattedTime += (hour <= 9 ? "0" : "") + std::to_string(hour) + ":" + (minute <= 9 ? "0" : "") + std::to_string(minute); return formattedTime; }