Skip to content

Commit

Permalink
reverse calculation with all stops results and od trips
Browse files Browse the repository at this point in the history
  • Loading branch information
kaligrafy committed Nov 28, 2017
1 parent cccc974 commit 1974a87
Show file tree
Hide file tree
Showing 8 changed files with 256 additions and 140 deletions.
61 changes: 46 additions & 15 deletions connection_scan_algorithm/src/calculator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,55 @@ namespace TrRouting

std::cerr << "-- forward calculation -- " << algorithmCalculationTime.getDurationMicrosecondsNoStop() - calculationTime << " microseconds\n";
calculationTime = algorithmCalculationTime.getDurationMicrosecondsNoStop();

result = forwardJourney(bestArrivalTime, bestEgressStopIndex, bestEgressTravelTime);

std::cerr << "-- forward journey -- " << algorithmCalculationTime.getDurationMicrosecondsNoStop() - calculationTime << " microseconds\n";
calculationTime = algorithmCalculationTime.getDurationMicrosecondsNoStop();


if (params.returnAllStopsResult)
{
result = forwardJourney(bestArrivalTime, bestEgressStopIndex, bestEgressTravelTime);

std::cerr << "-- forward journey -- " << algorithmCalculationTime.getDurationMicrosecondsNoStop() - calculationTime << " microseconds\n";
calculationTime = algorithmCalculationTime.getDurationMicrosecondsNoStop();
}
else
{

if (bestArrivalTime < MAX_INT)
{
departureTimeSeconds = -1;
arrivalTimeSeconds = bestArrivalTime;

for (auto & egressFootpath : egressFootpaths) // reset stops reverse tentative times with new arrival time:
{
stopsReverseTentativeTime[egressFootpath.first] = arrivalTimeSeconds - egressFootpath.second;
}

std::tie(bestDepartureTime, bestAccessStopIndex, bestAccessTravelTime) = reverseCalculation();

std::cerr << "-- reverse calculation -- " << algorithmCalculationTime.getDurationMicrosecondsNoStop() - calculationTime << " microseconds\n";
calculationTime = algorithmCalculationTime.getDurationMicrosecondsNoStop();

result = reverseJourney(bestDepartureTime, bestAccessStopIndex, bestAccessTravelTime);

std::cerr << "-- reverse journey -- " << algorithmCalculationTime.getDurationMicrosecondsNoStop() - calculationTime << " microseconds\n";
calculationTime = algorithmCalculationTime.getDurationMicrosecondsNoStop();

}
else
{

result = forwardJourney(bestArrivalTime, bestEgressStopIndex, bestEgressTravelTime);

std::cerr << "-- forward journey -- " << algorithmCalculationTime.getDurationMicrosecondsNoStop() - calculationTime << " microseconds\n";
calculationTime = algorithmCalculationTime.getDurationMicrosecondsNoStop();

}

}
}
else if (arrivalTimeSeconds > -1)
{

std::fill(tripsUsable.begin(), tripsUsable.end(), 1); // we need to make all trips usable when not coming from forward result because reverse calculation, by default, checks for usableTrips == 1

std::tie(bestDepartureTime, bestAccessStopIndex, bestAccessTravelTime) = reverseCalculation();

std::cerr << "-- reverse calculation -- " << algorithmCalculationTime.getDurationMicrosecondsNoStop() - calculationTime << " microseconds\n";
Expand All @@ -49,15 +89,6 @@ namespace TrRouting

}

//if (!params.returnAllStopsResult && bestArrivalTime < MAX_INT && bestEgressStopIndex != -1)
//{
// arrivalTimeSeconds = bestArrivalTime;
//
//
// Calculator::reverseCalculation();
//
//}

return result;

}
Expand Down
4 changes: 2 additions & 2 deletions connection_scan_algorithm/src/forward_calculation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ namespace TrRouting
{
egressTravelTime = stopsEgressTravelTime[egressFootpath.first];
egressStopArrivalTime = std::get<connectionIndexes::TIME_ARR>(forwardConnections[egressExitConnection]) + egressTravelTime;
std::cerr << stops[egressFootpath.first].name << ": " << egressTravelTime << " - " << Toolbox::convertSecondsToFormattedTime(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;
Expand All @@ -149,4 +149,4 @@ namespace TrRouting

}

}
}
79 changes: 56 additions & 23 deletions connection_scan_algorithm/src/forward_journey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ namespace TrRouting
std::tuple<int,int,int,int,int,short> emptyJourneyStep {-1,-1,-1,-1,-1,-1};
std::tuple<int,int,int,int,int,short,short,int> journeyStepEnterConnection; // connection tuple: departureStopIndex, arrivalStopIndex, departureTimeSeconds, arrivalTimeSeconds, tripIndex, canBoard, canUnboard, sequenceinTrip
std::tuple<int,int,int,int,int,short,short,int> journeyStepExitConnection;
std::vector<unsigned long long> routeIds;
std::vector<unsigned long long> routeTypeIds;
std::vector<unsigned long long> agencyIds;
std::vector<std::tuple<unsigned long long, unsigned long long, unsigned long long, int, int>> legs; // tuple: tripId, routeId, routePathId, boarding sequence, unboarding sequence
nlohmann::json stepJson = {};
nlohmann::json stopJson = {};
Expand All @@ -58,6 +61,8 @@ namespace TrRouting

legs.clear();
journey.clear();
routeIds.clear();
routeTypeIds.clear();

totalInVehicleTime = 0; transferArrivalTime = -1; firstDepartureTime = -1;
totalWalkingTime = 0; transferReadyTime = -1; minimizedDepartureTime = -1;
Expand Down Expand Up @@ -123,6 +128,9 @@ namespace TrRouting
totalInVehicleTime += inVehicleTime;
totalWaitingTime += waitingTime;
numberOfTransfers += 1;
routeIds.push_back(journeyStepRoute.id);
routeTypeIds.push_back(journeyStepRoute.routeTypeId);
agencyIds.push_back(journeyStepRoute.agencyId);
legs.push_back(std::make_tuple(journeyStepTrip.id, journeyStepTrip.routeId, journeyStepTrip.routePathId, boardingSequence, unboardingSequence));

if (i == 1) // first leg
Expand Down Expand Up @@ -254,16 +262,19 @@ namespace TrRouting

if (params.returnAllStopsResult)
{
arrivalTime = stopsTentativeTime[resultingStopIndex] - params.minWaitingTimeSeconds;
if (arrivalTime - departureTimeSeconds <= params.maxTotalTravelTimeSeconds)
if (std::get<0>(forwardEgressJourneys[resultingStopIndex]) != -1)
{
reachableStopsCount++;
stopJson = {};
stopJson["id"] = stops[resultingStopIndex].id;
stopJson["arrivalTime"] = Toolbox::convertSecondsToFormattedTime(arrivalTime);
stopJson["totalTravelTimeSeconds"] = arrivalTime - departureTimeSeconds;
stopJson["numberOfTransfers"] = numberOfTransfers;
json["stops"].push_back(stopJson);
arrivalTime = std::get<connectionIndexes::TIME_ARR>(forwardConnections[std::get<1>(forwardEgressJourneys[resultingStopIndex])]);
if (arrivalTime - departureTimeSeconds <= params.maxTotalTravelTimeSeconds)
{
reachableStopsCount++;
stopJson = {};
stopJson["id"] = stops[resultingStopIndex].id;
stopJson["arrivalTime"] = Toolbox::convertSecondsToFormattedTime(arrivalTime);
stopJson["totalTravelTimeSeconds"] = arrivalTime - departureTimeSeconds;
stopJson["numberOfTransfers"] = numberOfTransfers;
json["stops"].push_back(stopJson);
}
}
}
else if (!params.returnAllStopsResult)
Expand Down Expand Up @@ -304,26 +315,48 @@ namespace TrRouting
json["minimumWaitingTimeBeforeEachBoardingMinutes"] = Toolbox::convertSecondsToMinutes(params.minWaitingTimeSeconds);
json["minimumWaitingTimeBeforeEachBoardingSeconds"] = params.minWaitingTimeSeconds;

result.travelTimeSeconds = arrivalTime - departureTimeSeconds;
result.arrivalTimeSeconds = arrivalTime;
result.departureTimeSeconds = departureTimeSeconds;
result.numberOfTransfers = numberOfTransfers;
result.legs = legs;
result.status = "success";
result.travelTimeSeconds = arrivalTime - departureTimeSeconds;
result.arrivalTimeSeconds = arrivalTime;
result.departureTimeSeconds = departureTimeSeconds;
result.numberOfTransfers = numberOfTransfers;
result.inVehicleTravelTimeSeconds = totalInVehicleTime;
result.transferTravelTimeSeconds = totalTransferWalkingTime;
result.waitingTimeSeconds = totalWaitingTime;
result.accessTravelTimeSeconds = accessWalkingTime;
result.egressTravelTimeSeconds = egressWalkingTime;
result.transferWaitingTimeSeconds = totalTransferWaitingTime;
result.firstWaitingTimeSeconds = accessWaitingTime;
result.nonTransitTravelTimeSeconds = totalWalkingTime;
result.legs = legs;
result.routeIds = routeIds;
result.routeTypeIds = routeTypeIds;
result.agencyIds = agencyIds;
result.status = "success";

}
}
}
}
else // no route found
{
json["status"] = "no_routing_found";
json["origin"] = { params.origin.longitude, params.origin.latitude };
json["destination"] = { params.destination.longitude, params.destination.latitude };
json["departureTime"] = Toolbox::convertSecondsToFormattedTime(departureTimeSeconds);
json["departureTimeSeconds"] = departureTimeSeconds;
result.status = "no_routing_found";
result.travelTimeSeconds = -1;
json["status"] = "no_routing_found";
json["origin"] = { params.origin.longitude, params.origin.latitude };
json["destination"] = { params.destination.longitude, params.destination.latitude };
json["departureTime"] = Toolbox::convertSecondsToFormattedTime(departureTimeSeconds);
json["departureTimeSeconds"] = departureTimeSeconds;
result.status = "no_routing_found";
result.travelTimeSeconds = -1;
result.arrivalTimeSeconds = -1;
result.departureTimeSeconds = departureTimeSeconds;
result.numberOfTransfers = -1;
result.inVehicleTravelTimeSeconds = -1;
result.transferTravelTimeSeconds = -1;
result.waitingTimeSeconds = -1;
result.accessTravelTimeSeconds = -1;
result.egressTravelTimeSeconds = -1;
result.transferWaitingTimeSeconds = -1;
result.firstWaitingTimeSeconds = -1;
result.nonTransitTravelTimeSeconds = -1;
}

if (params.returnAllStopsResult)
Expand All @@ -337,4 +370,4 @@ namespace TrRouting

}

}
}
8 changes: 4 additions & 4 deletions connection_scan_algorithm/src/resets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace TrRouting
{

calculationTime = algorithmCalculationTime.getDurationMicrosecondsNoStop();

std::fill(stopsTentativeTime.begin(), stopsTentativeTime.end(), MAX_INT);
std::fill(stopsReverseTentativeTime.begin(), stopsReverseTentativeTime.end(), -1);
//std::fill(stopsD.begin(), stopsD.end(), MAX_INT);
Expand All @@ -20,18 +20,18 @@ namespace TrRouting
std::fill(tripsExitConnectionTransferTravelTime.begin(), tripsExitConnectionTransferTravelTime.end(), MAX_INT);
//std::fill(tripsReverseTime.begin(), tripsReverseTime.end(), MAX_INT);
std::fill(tripsEnabled.begin(), tripsEnabled.end(), 1);
std::fill(tripsUsable.begin(), tripsUsable.end(), 0);
std::fill(tripsUsable.begin(), tripsUsable.end(), -1);
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();

departureTimeSeconds = -1;
arrivalTimeSeconds = -1;

if(params.odTrip != NULL)
{
departureTimeSeconds = params.odTrip->departureTimeSeconds;
Expand Down
13 changes: 10 additions & 3 deletions connection_scan_algorithm/src/reverse_calculation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@ namespace TrRouting
// ignore connections before departure time + minimum access travel time:
if (std::get<connectionIndexes::TIME_ARR>(connection) <= arrivalTimeSeconds - minEgressTravelTime)
{

tripIndex = std::get<connectionIndexes::TRIP>(connection);

// enabled trips only here:
if (/*tripsUsable[tripIndex] != -1 && */tripsEnabled[tripIndex] != -1)
if (tripsUsable[tripIndex] == 1 && tripsEnabled[tripIndex] != -1)
{

connectionArrivalTime = std::get<connectionIndexes::TIME_ARR>(connection);

// no need to parse next connections if already reached destination from all egress stops, except if max travel time is set, so we can get a reverse profile in the next loop calculation:
Expand All @@ -56,6 +58,8 @@ namespace TrRouting
stopArrivalIndex = std::get<connectionIndexes::STOP_ARR>(connection);
stopArrivalTentativeTime = stopsReverseTentativeTime[stopArrivalIndex];

//std::cerr << "stopArrivalTentativeTime: " << stopArrivalTentativeTime << " connectionArrivalTime: " << connectionArrivalTime << " tripExitConnectionIndex: " << tripExitConnectionIndex << std::endl;

// reachable connections only here:
if (tripExitConnectionIndex != -1 || stopArrivalTentativeTime >= connectionArrivalTime)
{
Expand Down Expand Up @@ -108,7 +112,10 @@ namespace TrRouting
}
i++;
}

std::cerr << "-- " << reachableConnectionsCount << " reverse connections parsed on " << connectionsCount << std::endl;


int accessStopDepartureTime {-1};
int accessEnterConnection {-1};
int accessTravelTime {-1};
Expand All @@ -123,7 +130,7 @@ namespace TrRouting
{
accessTravelTime = stopsAccessTravelTime[accessFootpath.first];
accessStopDepartureTime = std::get<connectionIndexes::TIME_DEP>(reverseConnections[accessEnterConnection]) - accessTravelTime - params.minWaitingTimeSeconds;
std::cerr << stops[accessFootpath.first].name << ": " << accessTravelTime << " t: " << trips[std::get<connectionIndexes::TRIP>(reverseConnections[accessEnterConnection])].id << " - " << Toolbox::convertSecondsToFormattedTime(accessStopDepartureTime) << std::endl;
//std::cerr << stops[accessFootpath.first].name << ": " << accessTravelTime << " t: " << trips[std::get<connectionIndexes::TRIP>(reverseConnections[accessEnterConnection])].id << " - " << Toolbox::convertSecondsToFormattedTime(accessStopDepartureTime) << std::endl;
if (accessStopDepartureTime >= 0 && accessStopDepartureTime < MAX_INT && accessStopDepartureTime > bestDepartureTime)
{
bestDepartureTime = accessStopDepartureTime;
Expand All @@ -143,4 +150,4 @@ namespace TrRouting

}

}
}
Loading

0 comments on commit 1974a87

Please sign in to comment.