From a67c4bf84d47836113ac639b94b50e85e911b447 Mon Sep 17 00:00:00 2001 From: Daniel Patterson Date: Tue, 30 Oct 2018 15:09:19 -0700 Subject: [PATCH] Calculating durations is unavoidable due to tie-breaking minimums, but we can avoid accumulating distances if they're not requested. --- include/engine/routing_algorithms.hpp | 12 ++-- .../routing_algorithms/many_to_many.hpp | 3 +- src/engine/plugins/table.cpp | 2 +- src/engine/plugins/trip.cpp | 5 +- .../routing_algorithms/many_to_many_ch.cpp | 13 ++-- .../routing_algorithms/many_to_many_mld.cpp | 59 +++++++++++++------ 6 files changed, 58 insertions(+), 36 deletions(-) diff --git a/include/engine/routing_algorithms.hpp b/include/engine/routing_algorithms.hpp index aec19f753..5d4a8a956 100644 --- a/include/engine/routing_algorithms.hpp +++ b/include/engine/routing_algorithms.hpp @@ -34,8 +34,7 @@ class RoutingAlgorithmsInterface ManyToManySearch(const std::vector &phantom_nodes, const std::vector &source_indices, const std::vector &target_indices, - const bool calculate_distance, - const bool calculate_duration) const = 0; + const bool calculate_distance) const = 0; virtual routing_algorithms::SubMatchingList MapMatching(const routing_algorithms::CandidateLists &candidates_list, @@ -88,8 +87,7 @@ template class RoutingAlgorithms final : public RoutingAlgo ManyToManySearch(const std::vector &phantom_nodes, const std::vector &source_indices, const std::vector &target_indices, - const bool calculate_distance, - const bool calculate_duration) const final override; + const bool calculate_distance) const final override; routing_algorithms::SubMatchingList MapMatching(const routing_algorithms::CandidateLists &candidates_list, @@ -198,8 +196,7 @@ std::pair, std::vector> RoutingAlgorithms::ManyToManySearch(const std::vector &phantom_nodes, const std::vector &_source_indices, const std::vector &_target_indices, - const bool calculate_distance, - const bool calculate_duration) const + const bool calculate_distance) const { BOOST_ASSERT(!phantom_nodes.empty()); @@ -222,8 +219,7 @@ RoutingAlgorithms::ManyToManySearch(const std::vector &p phantom_nodes, std::move(source_indices), std::move(target_indices), - calculate_distance, - calculate_duration); + calculate_distance); } template diff --git a/include/engine/routing_algorithms/many_to_many.hpp b/include/engine/routing_algorithms/many_to_many.hpp index 23c8902df..ddd861f06 100644 --- a/include/engine/routing_algorithms/many_to_many.hpp +++ b/include/engine/routing_algorithms/many_to_many.hpp @@ -97,8 +97,7 @@ manyToManySearch(SearchEngineData &engine_working_data, const std::vector &phantom_nodes, const std::vector &source_indices, const std::vector &target_indices, - const bool calculate_distance, - const bool calculate_duration); + const bool calculate_distance); } // namespace routing_algorithms } // namespace engine diff --git a/src/engine/plugins/table.cpp b/src/engine/plugins/table.cpp index e10b5461a..3d89bc25a 100644 --- a/src/engine/plugins/table.cpp +++ b/src/engine/plugins/table.cpp @@ -86,7 +86,7 @@ Status TablePlugin::HandleRequest(const RoutingAlgorithmsInterface &algorithms, bool request_duration = params.annotations & api::TableParameters::AnnotationsType::Duration; auto result_tables_pair = algorithms.ManyToManySearch( - snapped_phantoms, params.sources, params.destinations, request_distance, request_duration); + snapped_phantoms, params.sources, params.destinations, request_distance); if ((request_duration && result_tables_pair.first.empty()) || (request_distance && result_tables_pair.second.empty())) diff --git a/src/engine/plugins/trip.cpp b/src/engine/plugins/trip.cpp index 8914c3e48..f402a0c06 100644 --- a/src/engine/plugins/trip.cpp +++ b/src/engine/plugins/trip.cpp @@ -217,10 +217,7 @@ Status TripPlugin::HandleRequest(const RoutingAlgorithmsInterface &algorithms, // compute the duration table of all phantom nodes auto result_duration_table = util::DistTableWrapper( - algorithms - .ManyToManySearch( - snapped_phantoms, {}, {}, /*requestDistance*/ false, /*requestDuration*/ true) - .first, + algorithms.ManyToManySearch(snapped_phantoms, {}, {}, /*requestDistance*/ false).first, number_of_locations); if (result_duration_table.size() == 0) diff --git a/src/engine/routing_algorithms/many_to_many_ch.cpp b/src/engine/routing_algorithms/many_to_many_ch.cpp index 1b8e90ca6..f9fa8e509 100644 --- a/src/engine/routing_algorithms/many_to_many_ch.cpp +++ b/src/engine/routing_algorithms/many_to_many_ch.cpp @@ -120,8 +120,13 @@ void forwardRoutingStep(const DataFacade &facade, const auto target_distance = current_bucket.distance; auto ¤t_weight = weights_table[row_index * number_of_targets + column_index]; + + EdgeDistance nulldistance = 0; + auto ¤t_duration = durations_table[row_index * number_of_targets + column_index]; - auto ¤t_distance = distances_table[row_index * number_of_targets + column_index]; + auto ¤t_distance = + distances_table.empty() ? nulldistance + : distances_table[row_index * number_of_targets + column_index]; // Check if new weight is better auto new_weight = source_weight + target_weight; @@ -180,8 +185,7 @@ manyToManySearch(SearchEngineData &engine_working_data, const std::vector &phantom_nodes, const std::vector &source_indices, const std::vector &target_indices, - const bool /* calculate_distance */, - const bool /* calculate_duration */) + const bool calculate_distance) { const auto number_of_sources = source_indices.size(); const auto number_of_targets = target_indices.size(); @@ -189,7 +193,8 @@ manyToManySearch(SearchEngineData &engine_working_data, std::vector weights_table(number_of_entries, INVALID_EDGE_WEIGHT); std::vector durations_table(number_of_entries, MAXIMAL_EDGE_DURATION); - std::vector distances_table(number_of_entries, MAXIMAL_EDGE_DISTANCE); + std::vector distances_table(calculate_distance ? number_of_entries : 0, + MAXIMAL_EDGE_DISTANCE); std::vector middle_nodes_table(number_of_entries, SPECIAL_NODEID); std::vector search_space_with_buckets; diff --git a/src/engine/routing_algorithms/many_to_many_mld.cpp b/src/engine/routing_algorithms/many_to_many_mld.cpp index b8521df9f..55544c4bb 100644 --- a/src/engine/routing_algorithms/many_to_many_mld.cpp +++ b/src/engine/routing_algorithms/many_to_many_mld.cpp @@ -193,11 +193,13 @@ oneToManySearch(SearchEngineData &engine_working_data, const DataFacade &facade, const std::vector &phantom_nodes, std::size_t phantom_index, - const std::vector &phantom_indices) + const std::vector &phantom_indices, + const bool calculate_distance) { std::vector weights(phantom_indices.size(), INVALID_EDGE_WEIGHT); std::vector durations(phantom_indices.size(), MAXIMAL_EDGE_DURATION); - std::vector distances_table(phantom_indices.size(), MAXIMAL_EDGE_DISTANCE); + std::vector distances_table(calculate_distance ? phantom_indices.size() : 0, + MAXIMAL_EDGE_DISTANCE); std::vector middle_nodes_table(phantom_indices.size(), SPECIAL_NODEID); // Collect destination (source) nodes into a map @@ -268,12 +270,16 @@ oneToManySearch(SearchEngineData &engine_working_data, const auto path_duration = duration + target_duration; const auto path_distance = distance + target_distance; + EdgeDistance nulldistance = 0; + auto ¤t_distance = + distances_table.empty() ? nulldistance : distances_table[index]; + if (std::tie(path_weight, path_duration, path_distance) < - std::tie(weights[index], durations[index], distances_table[index])) + std::tie(weights[index], durations[index], current_distance)) { weights[index] = path_weight; durations[index] = path_duration; - distances_table[index] = path_distance; + current_distance = path_distance; middle_nodes_table[index] = node; } @@ -434,7 +440,9 @@ void forwardRoutingStep(const DataFacade &facade, : row_idx + column_idx * number_of_sources; auto ¤t_weight = weights_table[location]; auto ¤t_duration = durations_table[location]; - auto ¤t_distance = distances_table[location]; + + EdgeDistance nulldistance = 0; + auto ¤t_distance = distances_table.empty() ? nulldistance : distances_table[location]; // Check if new weight is better auto new_weight = source_weight + target_weight; @@ -529,7 +537,8 @@ manyToManySearch(SearchEngineData &engine_working_data, const DataFacade &facade, const std::vector &phantom_nodes, const std::vector &source_indices, - const std::vector &target_indices) + const std::vector &target_indices, + const bool calculate_distance) { const auto number_of_sources = source_indices.size(); const auto number_of_targets = target_indices.size(); @@ -537,7 +546,8 @@ manyToManySearch(SearchEngineData &engine_working_data, std::vector weights_table(number_of_entries, INVALID_EDGE_WEIGHT); std::vector durations_table(number_of_entries, MAXIMAL_EDGE_DURATION); - std::vector distances_table(number_of_entries, INVALID_EDGE_DISTANCE); + std::vector distances_table(calculate_distance ? number_of_entries : 0, + INVALID_EDGE_DISTANCE); std::vector middle_nodes_table(number_of_entries, SPECIAL_NODEID); std::vector search_space_with_buckets; @@ -626,29 +636,44 @@ manyToManySearch(SearchEngineData &engine_working_data, const std::vector &phantom_nodes, const std::vector &source_indices, const std::vector &target_indices, - const bool /* calculate_distance */, - const bool /* calculate_duration */) + const bool calculate_distance) { if (source_indices.size() == 1) { // TODO: check if target_indices.size() == 1 and do a bi-directional search - return mld::oneToManySearch( - engine_working_data, facade, phantom_nodes, source_indices.front(), target_indices); + return mld::oneToManySearch(engine_working_data, + facade, + phantom_nodes, + source_indices.front(), + target_indices, + calculate_distance); } if (target_indices.size() == 1) { - return mld::oneToManySearch( - engine_working_data, facade, phantom_nodes, target_indices.front(), source_indices); + return mld::oneToManySearch(engine_working_data, + facade, + phantom_nodes, + target_indices.front(), + source_indices, + calculate_distance); } if (target_indices.size() < source_indices.size()) { - return mld::manyToManySearch( - engine_working_data, facade, phantom_nodes, target_indices, source_indices); + return mld::manyToManySearch(engine_working_data, + facade, + phantom_nodes, + target_indices, + source_indices, + calculate_distance); } - return mld::manyToManySearch( - engine_working_data, facade, phantom_nodes, source_indices, target_indices); + return mld::manyToManySearch(engine_working_data, + facade, + phantom_nodes, + source_indices, + target_indices, + calculate_distance); } } // namespace routing_algorithms