From 7a61617e367b25cfd1752c8937882f7ba6f4a340 Mon Sep 17 00:00:00 2001 From: Moritz Kobitzsch Date: Tue, 14 Feb 2017 16:13:33 +0100 Subject: [PATCH] fix exponential in depth cost for annotation --- include/partition/annotated_partition.hpp | 2 +- src/partition/annotated_partition.cpp | 47 ++++++++++++++------- src/partition/partitioner.cpp | 4 ++ src/partition/recursive_bisection_state.cpp | 4 +- 4 files changed, 38 insertions(+), 19 deletions(-) diff --git a/include/partition/annotated_partition.hpp b/include/partition/annotated_partition.hpp index 2c18a413761..4c23821c96c 100644 --- a/include/partition/annotated_partition.hpp +++ b/include/partition/annotated_partition.hpp @@ -125,7 +125,7 @@ class AnnotatedPartition const std::vector &cell_ids) const; std::vector - ComputeCellIDs(const std::vector> &prefixes, + ComputeCellIDs(std::vector> &prefixes, const BisectionGraph &graph, const std::vector &bisection_ids) const; }; diff --git a/src/partition/annotated_partition.cpp b/src/partition/annotated_partition.cpp index 01c62783d9e..2d0c8ba4634 100644 --- a/src/partition/annotated_partition.cpp +++ b/src/partition/annotated_partition.cpp @@ -12,6 +12,8 @@ #include #include +#include "util/timing_util.hpp" + namespace osrm { namespace partition @@ -149,17 +151,18 @@ void AnnotatedPartition::PrintBisection(const std::vector &implicit_tre std::queue id_queue; id_queue.push(0); - const auto add_child = [&id_queue, implicit_tree](const BisectionID prefix, - const std::uint32_t level) { + const auto add_child = [&id_queue, &implicit_tree](const BisectionID prefix, + const std::uint32_t level) { const auto child_range = getChildrenRange(implicit_tree, prefix, level); if (std::distance(child_range.first, child_range.second) > 1) id_queue.push(prefix); }; + std::vector> current_level; for (std::int32_t level = -1; !id_queue.empty(); ++level) { auto level_size = id_queue.size(); - std::vector> current_level; + current_level.clear(); while (level_size--) { const auto prefix = id_queue.front(); @@ -196,7 +199,7 @@ void AnnotatedPartition::SearchLevels(const std::vector &implicit_tree, const auto print_level = [&]() { if (current_level.empty()) return; - + const auto cell_ids = ComputeCellIDs(current_level, graph, bisection_ids); const auto stats = AnalyseLevel(graph, cell_ids); stats.logMachinereadable(std::cout, "dfs-balanced", level, level == -1); @@ -204,11 +207,11 @@ void AnnotatedPartition::SearchLevels(const std::vector &implicit_tree, }; std::size_t max_size = 0.5 * graph.NumberOfNodes(); + std::queue> id_queue; while (!current_level.empty()) { std::size_t total_size = 0; std::size_t count = 0; - std::queue> id_queue; for (auto element : current_level) { // don't relax final cells @@ -230,7 +233,7 @@ void AnnotatedPartition::SearchLevels(const std::vector &implicit_tree, current_level.clear(); - const auto relax = [&id_queue, implicit_tree, avg_size, ¤t_level]( + const auto relax = [&id_queue, &implicit_tree, avg_size, ¤t_level]( const std::pair &element) { const auto size = getCellSize(implicit_tree, element.first, element.second); if (!hasChildren(implicit_tree, element.first, element.second)) @@ -247,8 +250,8 @@ void AnnotatedPartition::SearchLevels(const std::vector &implicit_tree, }; if (get_penalty(size) < - 0.5 * (get_penalty(getCellSize(implicit_tree, left, element.second+1)) + - get_penalty(getCellSize(implicit_tree, right, element.second+1)))) + 0.5 * (get_penalty(getCellSize(implicit_tree, left, element.second + 1)) + + get_penalty(getCellSize(implicit_tree, right, element.second + 1)))) { current_level.push_back(element); } @@ -283,6 +286,7 @@ AnnotatedPartition::AnalyseLevel(const BisectionGraph &graph, std::size_t border_arcs_total = 0; std::size_t contained_nodes = 0; + // only border nodes on the lowest level can be border nodes in general for (const auto &node : graph.Nodes()) { const auto cell_id = cell_ids[node.original_id]; @@ -290,6 +294,7 @@ AnnotatedPartition::AnalyseLevel(const BisectionGraph &graph, continue; ++contained_nodes; + const auto edge_range = graph.Edges(node); const auto border_arcs_at_node = std::count_if( edge_range.begin(), edge_range.end(), [&cell_id, &cell_ids, &graph](const auto &edge) { @@ -334,7 +339,6 @@ AnnotatedPartition::AnalyseLevel(const BisectionGraph &graph, cell_sizes.end(), cell_sizes_vec.begin(), [](const auto &pair) { return pair.second; }); - return {border_nodes_total, border_arcs_total, contained_nodes, @@ -345,13 +349,17 @@ AnnotatedPartition::AnalyseLevel(const BisectionGraph &graph, std::move(cell_sizes_vec)}; } -std::vector AnnotatedPartition::ComputeCellIDs( - const std::vector> &prefixes, - const BisectionGraph &graph, - const std::vector &bisection_ids) const +std::vector +AnnotatedPartition::ComputeCellIDs(std::vector> &prefixes, + const BisectionGraph &graph, + const std::vector &bisection_ids) const { std::vector cell_ids(graph.NumberOfNodes(), INVALID_CELLID); + std::sort(prefixes.begin(), prefixes.end(), [](const auto lhs, const auto rhs) { + return lhs.first < rhs.first; + }); + for (const auto &node : graph.Nodes()) { // find the cell_id of node in the current levels @@ -361,12 +369,19 @@ std::vector AnnotatedPartition::ComputeCellIDs( return masked(id, prefix.second) == prefix.first; }; - const auto prefix = std::find_if(prefixes.begin(), prefixes.end(), is_prefixed_by); + const auto prefix = std::lower_bound(prefixes.begin(), + prefixes.end(), + id, + [&](const auto prefix, const BisectionID id){ + return prefix.first < masked(id, prefix.second); + }); - if (prefix != prefixes.end()) + if( prefix == prefixes.end() ) + continue; + + if (is_prefixed_by(*prefix)) cell_ids[node.original_id] = std::distance(prefixes.begin(), prefix); } - return cell_ids; } diff --git a/src/partition/partitioner.cpp b/src/partition/partitioner.cpp index fe0c11a54f6..4706ce7eba9 100644 --- a/src/partition/partitioner.cpp +++ b/src/partition/partitioner.cpp @@ -19,6 +19,7 @@ #include "util/geojson_debug_logger.hpp" #include "util/geojson_debug_policies.hpp" #include "util/json_container.hpp" +#include "util/timing_util.hpp" namespace osrm { @@ -80,7 +81,10 @@ void LogStatistics(const std::string &filename, std::vector bisec makeBisectionGraph(compressed_node_based_graph.coordinates, adaptToBisectionEdge(std::move(compressed_node_based_graph.edges))); + TIMER_START(annotation); AnnotatedPartition partition(graph, bisection_ids); + TIMER_STOP(annotation); + std::cout << "Annotation took " << TIMER_SEC(annotation) << " seconds" << std::endl; } void LogGeojson(const std::string &filename, std::vector bisection_ids) diff --git a/src/partition/recursive_bisection_state.cpp b/src/partition/recursive_bisection_state.cpp index 6fd1c3ba4a8..d5f2874b3dc 100644 --- a/src/partition/recursive_bisection_state.cpp +++ b/src/partition/recursive_bisection_state.cpp @@ -159,8 +159,8 @@ RecursiveBisectionState::PrePartitionWithSCC(const std::size_t small_component_s const auto component_id = transform_id(nid); const auto itr = ordered_component_ids.find(component_id); BOOST_ASSERT(itr != ordered_component_ids.end()); - const auto consecutive_id = std::distance(ordered_component_ids.begin(), itr); - BOOST_ASSERT(static_cast(consecutive_id) < ordered_component_ids.size()); + BOOST_ASSERT(static_cast(std::distance(ordered_component_ids.begin(), itr)) < + ordered_component_ids.size()); return std::distance(ordered_component_ids.begin(), itr); };