Skip to content

Commit

Permalink
Merge pull request #42 from naviqore/feature/remove-comments
Browse files Browse the repository at this point in the history
Feature/remove comments
  • Loading branch information
Brunner246 authored Sep 26, 2024
2 parents 1159d79 + 40fcc35 commit c2fd032
Show file tree
Hide file tree
Showing 8 changed files with 4 additions and 348 deletions.
4 changes: 0 additions & 4 deletions src/gtfsRaptorConfig/src/agencySubsetWriter/readGtfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,3 @@ def load_gtfs_data(directory: str):
route.trips.append(trip)

return gtfs_data_dict

# if __name__ == '__main__':
# gtfs_data = load_gtfs_data('C:\\Users\\MichaelBrunner\\source\\master-thesis\\raptorxx\\test\\test_gtfsRaptorConfig\\test-data\\Verkehrsbetriebe_der_Stadt_St_Gallen')
# print(gtfs_data)
64 changes: 3 additions & 61 deletions src/gtfsRaptorConfig/src/agencySubsetWriter/writeGtfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


def quote(value):
return f'"{value}"' # if value else '""'
return f'"{value}"'


def write_gtfs_files(output_directory: str, agency_id: str, gtfs_data: Dict):
Expand Down Expand Up @@ -41,14 +41,6 @@ def write_gtfs_files(output_directory: str, agency_id: str, gtfs_data: Dict):
transfer.from_stop_id == stop.stop_id}
logger.info(f"Found {len(transfers)} transfers for agency {agency_id}")

# calendars = [calendar for trip in trips for calendar in gtfs_data['calendars'] if
# calendar.service_id == trip.service_id]
# logger.info(f"Found {len(calendars)} calendars for agency {agency_id}")
#
# calendar_dates = [calendar_date for calendar in calendars for calendar_date in gtfs_data['calendar_dates'] if
# calendar_date.service_id == calendar.service_id]
# logger.info(f"Found {len(calendar_dates)} calendar dates for agency {agency_id}")

os.makedirs(output_directory, exist_ok=True)

#
Expand Down Expand Up @@ -80,15 +72,6 @@ def write_csv(file_name, header, rows):
("transfers.txt", ["from_stop_id", "to_stop_id", "transfer_type", "min_transfer_time"],
[[transfer.from_stop_id, transfer.to_stop_id, transfer.transfer_type, transfer.min_transfer_time] for transfer
in transfers]),

# ("calendar.txt",
# ["service_id", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday", "start_date",
# "end_date"],
# [[cal.service_id, cal.monday, cal.tuesday, cal.wednesday, cal.thursday, cal.friday, cal.saturday, cal.sunday,
# cal.start_date, cal.end_date] for cal in calendars]),
#
# ("calendar_dates.txt", ["service_id", "date", "exception_type"],
# [[cd.service_id, cd.date, cd.exception_type] for cd in calendar_dates])
]

logger.info(f"Writing GTFS files to {output_directory}")
Expand All @@ -100,51 +83,10 @@ def write_csv(file_name, header, rows):


if __name__ == '__main__':
directory = 'C:\\Users\\MichaelBrunner\\Downloads\\gtfs_fp2024_2024-05-27'
directory = 'SomeDrive:\\...\\...\\...\\gtfs_fp2024_2024-05-27'
gtfs_data = load_gtfs_data(directory)

output_directory = 'C:\\Users\\MichaelBrunner\\Downloads\\filtered_gtfs'
output_directory = 'SomeDrive:\\...\\...\\...\\filtered_gtfs'
agency_id = "885" # "Verkehrsbetriebe der Stadt St.Gallen"
write_gtfs_files(output_directory, agency_id, gtfs_data)

# stop_times = list(filter(lambda stop_time_item: stop_time_item.trip_id in [trip.trip_id for trip in trips], gtfs_data['stop_times']))
# stops_set = set(filter(lambda stop_item: stop_item.stop_id in [stop_time.stop_id for stop_time in stop_times], gtfs_data['stops']))
# transfers_set = set(filter(lambda transfer_item: transfer_item.from_stop_id in [stop.stop_id for stop in stops_set], gtfs_data['transfers']))
# calendars = list(filter(lambda calendar_item: calendar_item.service_id in [trip.service_id for trip in trips], gtfs_data['calendars']))
# calendar_dates = list(filter(lambda calendar_date_item: calendar_date_item.service_id in [calendar.service_id for calendar in calendars], gtfs_data['calendar_dates']))
#
#
#
# stop_times_dict = {st.trip_id: [] for st in gtfs_data['stop_times']}
# for st in gtfs_data['stop_times']:
# stop_times_dict[st.trip_id].append(st)
# stops_dict = {stop.stop_id: stop for stop in gtfs_data['stops']}
# transfers_dict = {stop.stop_id: [] for stop in gtfs_data['stops']}
# for transfer in gtfs_data['transfers']:
# transfers_dict[transfer.from_stop_id].append(transfer)
# calendars_dict = {cal.service_id: cal for cal in gtfs_data['calendars']}
# calendar_dates_dict = {cal.service_id: [] for cal in gtfs_data['calendars']}
# for cd in gtfs_data['calendar_dates']:
# calendar_dates_dict[cd.service_id].append(cd)
#
# for route in routes_to_write:
# logger.info(f"Processing route {route.route_id}")
# for trip in gtfs_data['trips']:
# if trip.route_id == route.route_id:
# trips.append(trip)
# stop_times.extend(stop_times_dict[trip.trip_id])
#
# for stop_time in stop_times_dict[trip.trip_id]:
# stop = stops_dict.get(stop_time.stop_id)
# if stop:
# stops_set.add(stop)
#
# for stop in stops_set:
# transfers_set.update(transfers_dict[stop.stop_id])
#
# calendar = calendars_dict.get(trip.service_id)
# if calendar:
# calendars.append(calendar)
# calendar_dates.extend(calendar_dates_dict[trip.service_id])
#
# os.makedirs(output_directory, exist_ok=True)
2 changes: 0 additions & 2 deletions src/raptor/include/config/QueryConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ namespace raptor::config {
public:
QueryConfig();

// QueryConfig(int maxWalkingDuration, int minTransferDuration, int maxTransferNumber, int maxTravelTime);

explicit QueryConfig(const WalkingConfig& walkingConfig, const TransferConfig& transferConfig, const TravelConfig& travelConfig);

[[nodiscard]] auto getMaximumWalkingDuration() const -> types::raptorInt;
Expand Down
264 changes: 0 additions & 264 deletions src/raptor/src/utils/RaptorRouterBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,267 +249,3 @@ namespace raptor {
}

} // namespace raptor


/*
* //
// Created by MichaelBrunner on 20/07/2024.
//
#include "RaptorRouterBuilder.h"
#include "LoggerFactory.h"
#include "RouteBuilder.h"
#include "helperFunctions.h"
#include <algorithm>
#include <execution>
#include <stdexcept>
#include <iostream>
#include <numeric>
#include <ranges>
#include <format>
#include <mutex>
namespace raptor {
RaptorRouterBuilder::RaptorRouterBuilder(const int defaultSameStopTransferTime)
: defaultSameStopTransferTime(defaultSameStopTransferTime)
{
}
RaptorRouterBuilder& RaptorRouterBuilder::addStop(const std::string& id)
{
if (stops.contains(id)) {
throw std::invalid_argument("Stop " + id + " already exists");
}
#if LOGGER
getConsoleLogger(LoggerName::RAPTOR)->info(std::format("Adding stop: id={}", id));
#endif
stops[id] = static_cast<types::raptorIdx>(stops.size());
stopRoutes[id] = std::unordered_set<std::string>();
return *this;
}
RaptorRouterBuilder& RaptorRouterBuilder::addRoute(const std::string& routeId, const std::vector<std::string>& stopIds)
{
if (routeBuilders.contains(routeId)) {
throw std::invalid_argument("Route " + routeId + " already exists");
}
for (const auto& stopId : stopIds) {
if (!stops.contains(stopId)) {
throw std::invalid_argument("Stop " + stopId + " does not exist");
}
stopRoutes[stopId].insert(routeId);
}
routeBuilders[routeId] = std::make_shared<RouteBuilder>(routeId, stopIds);
routeStopSize += static_cast<int>(stopIds.size());
return *this;
}
RaptorRouterBuilder& RaptorRouterBuilder::addTrip(const std::string& tripId, const std::string& routeId)
{
getRouteBuilder(routeId)->addTrip(tripId);
return *this;
}
RaptorRouterBuilder& RaptorRouterBuilder::addStopTime(const std::string& routeId, const std::string& tripId, const int position, const std::string& stopId, types::raptorInt arrival, types::raptorInt departure)
{
const auto stopTime = StopTime{arrival, departure};
getRouteBuilder(routeId)->addStopTime(tripId, position, stopId, stopTime);
stopTimeSize++;
return *this;
}
RaptorRouterBuilder& RaptorRouterBuilder::addTransfer(const std::string& sourceStopId, const std::string& targetStopId, int duration)
{
#if LOGGER
getConsoleLogger(LoggerName::RAPTOR)->info(std::format("Adding transfer: sourceStopId={} targetStopId={} duration={}", sourceStopId, targetStopId, duration));
#endif
if (!stops.contains(sourceStopId)) {
throw std::invalid_argument("Source stop " + sourceStopId + " does not exist");
}
if (!stops.contains(targetStopId)) {
throw std::invalid_argument("Target stop " + targetStopId + " does not exist");
}
if (sourceStopId == targetStopId) {
sameStopTransfers[sourceStopId] = duration;
return *this;
}
transfers[sourceStopId].emplace_back(stops[targetStopId], duration);
transferSize++;
return *this;
}
std::shared_ptr<RaptorData> RaptorRouterBuilder::build()
{
#if LOGGER
getConsoleLogger(LoggerName::RAPTOR)->info(std::format("Initialize Raptor with {} stops, {} routes, {} route stops, {} stop times, {} transfers", stops.size(), routeBuilders.size(), routeStopSize, stopTimeSize, transferSize));
#endif
const auto routeContainers = buildAndSortRouteContainers();
auto lookup = buildLookup(routeContainers);
auto stopContext = buildStopContext(lookup);
auto routeTraversal = buildRouteTraversal(routeContainers);
return std::make_shared<RaptorData>(lookup, stopContext, routeTraversal);
}
std::vector<RouteContainer> RaptorRouterBuilder::buildAndSortRouteContainers() const
{
std::vector<RouteContainer> containers;
containers.reserve(routeBuilders.size());
for (const auto& routeBuilder : routeBuilders | std::views::values) {
containers.push_back(routeBuilder->build());
}
std::ranges::sort(containers, [](const RouteContainer& lhs, const RouteContainer& rhs) {
if (lhs.trips().empty()) {
return false;
}
if (rhs.trips().empty()) {
return true;
}
return lhs.trips().begin()->second[0].arrival < rhs.trips().begin()->second[0].arrival;
});
return containers;
}
StopRoutesIndexLookup RaptorRouterBuilder::buildLookup(const std::vector<RouteContainer>& routeContainers) const
{
#if LOGGER
getConsoleLogger(LoggerName::RAPTOR)->info(std::format("Building lookup with {} stops and {} routes", stops.size(), routeContainers.size()));
#endif
std::unordered_map<std::string, types::raptorIdx> routes;
for (auto i = 0; i < routeContainers.size(); ++i) {
routes[routeContainers[i].id()] = i;
}
return {stops, routes};
}
StopContext RaptorRouterBuilder::buildStopContext(const StopRoutesIndexLookup& lookup)
{
#if LOGGER
getConsoleLogger(LoggerName::RAPTOR)->info(std::format("Building stop context with {} stops and {} transfers", stops.size(), transferSize));
#endif
std::vector<Stop> stopArr(stops.size());
auto stopRouteSizeView = stopRoutes
| std::views::values
| std::views::transform([](const std::unordered_set<std::string>& routes) { return static_cast<int>(routes.size()); });
const auto stopRouteSize = std::accumulate(stopRouteSizeView.begin(), stopRouteSizeView.end(), 0);
std::vector<types::raptorIdx> stopRouteArr(stopRouteSize);
std::vector<Transfer> transferArr(transferSize);
int transferIdx = 0;
int stopRouteIdx = 0;
for (const auto& [stopId, stopIdx] : stops) {
const auto& currentStopRoutes = stopRoutes[stopId];
if (currentStopRoutes.empty()) {
throw std::logic_error("Stop " + stopId + " has no routes");
}
auto currentTransfers = transfers.find(stopId);
const auto numberOfTransfers = currentTransfers == transfers.end() ? 0 : currentTransfers->second.size();
const auto sameStopTransferTime = sameStopTransfers.contains(stopId) ? sameStopTransfers[stopId] : defaultSameStopTransferTime;
stopArr[stopIdx] = Stop(stopId, stopRouteIdx, static_cast<int>(currentStopRoutes.size()), sameStopTransferTime, static_cast<int>(numberOfTransfers) == 0 ? NO_INDEX : transferIdx, static_cast<int>(numberOfTransfers));
if (currentTransfers != transfers.end()) {
for (const auto& transfer : currentTransfers->second) {
transferArr[transferIdx++] = transfer;
}
}
for (const auto& routeId : currentStopRoutes) {
stopRouteArr[stopRouteIdx++] = lookup.routes.at(routeId);
}
}
return {transferArr, stopArr, stopRouteArr};
}
RouteTraversal RaptorRouterBuilder::buildRouteTraversal(const std::vector<RouteContainer>& routeContainers) const
{
#if LOGGER
getConsoleLogger(LoggerName::RAPTOR)->info(std::format("Building route traversal with {} routes, {} route stops, {} stop times", routeContainers.size(), routeStopSize, stopTimeSize));
#endif
std::vector<Route> routeArr;
routeArr.reserve(routeContainers.size());
std::vector<RouteStop> routeStopArr;
routeStopArr.reserve(static_cast<types::raptorIdx>(routeStopSize));
std::vector<StopTime> stopTimeArr;
stopTimeArr.reserve(static_cast<types::raptorIdx>(stopTimeSize));
types::raptorIdx routeStopCnt{0};
types::raptorIdx stopTimeCnt{0};
for (size_t routeIdx = 0; routeIdx < routeContainers.size(); ++routeIdx) {
const auto& routeContainer = routeContainers[routeIdx];
const auto& stopSequence = routeContainer.stopSequence();
const auto& trips = routeContainer.trips();
const auto numberOfStops = stopSequence.size();
const auto numberOfTrips = trips.size();
std::vector<std::string> tripIdsArray;
tripIdsArray.reserve(routeContainer.trips().size());
for (const auto& trip : routeContainer.trips()) {
tripIdsArray.push_back(trip.first);
}
// routeArr[routeIdx] = Route(routeContainer.id(), routeStopCnt, static_cast<int>(numberOfStops), stopTimeCnt, static_cast<int>(numberOfTrips), tripIdsArray);
routeArr.emplace_back(routeContainer.id(), routeStopCnt, static_cast<int>(numberOfStops), stopTimeCnt, static_cast<int>(numberOfTrips), tripIdsArray);
for (int position = 0; position < numberOfStops; ++position) {
const auto stopIdxIt = stops.find(routeContainer.stopSequence().at(position));
if (stopIdxIt == stops.end()) {
throw std::invalid_argument("Stop " + routeContainer.stopSequence().at(position) + " does not exist");
}
// routeStopArr[routeStopCnt++] = RouteStop(stopIdxIt->second, static_cast<types::raptorIdx>(routeIdx));
routeStopArr.emplace_back(stopIdxIt->second, static_cast<types::raptorIdx>(routeIdx));
routeStopCnt++;
}
for (const auto& stopTimes : routeContainer.trips() | std::views::values) {
for (const auto& stopTime : stopTimes) {
stopTimeArr[stopTimeCnt++] = stopTime;
}
}
}
return {routeStopArr, stopTimeArr, routeArr};
}
RouteBuilder* RaptorRouterBuilder::getRouteBuilder(const std::string& routeId)
{
const auto it = routeBuilders.find(routeId);
if (it == routeBuilders.end()) {
throw std::invalid_argument("Route " + routeId + " does not exist");
}
return it->second.get();
}
} // namespace raptor
*/
Loading

0 comments on commit c2fd032

Please sign in to comment.