Skip to content

Commit

Permalink
fix exponential in depth cost for annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
Moritz Kobitzsch committed Feb 14, 2017
1 parent 1275332 commit 7a61617
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 19 deletions.
2 changes: 1 addition & 1 deletion include/partition/annotated_partition.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class AnnotatedPartition
const std::vector<std::uint32_t> &cell_ids) const;

std::vector<std::uint32_t>
ComputeCellIDs(const std::vector<std::pair<BisectionID, std::int32_t>> &prefixes,
ComputeCellIDs(std::vector<std::pair<BisectionID, std::int32_t>> &prefixes,
const BisectionGraph &graph,
const std::vector<BisectionID> &bisection_ids) const;
};
Expand Down
47 changes: 31 additions & 16 deletions src/partition/annotated_partition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include <string>
#include <unordered_map>

#include "util/timing_util.hpp"

namespace osrm
{
namespace partition
Expand Down Expand Up @@ -149,17 +151,18 @@ void AnnotatedPartition::PrintBisection(const std::vector<SizedID> &implicit_tre
std::queue<BisectionID> 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<std::pair<BisectionID, std::int32_t>> current_level;
for (std::int32_t level = -1; !id_queue.empty(); ++level)
{
auto level_size = id_queue.size();
std::vector<std::pair<BisectionID, std::int32_t>> current_level;
current_level.clear();
while (level_size--)
{
const auto prefix = id_queue.front();
Expand Down Expand Up @@ -196,19 +199,19 @@ void AnnotatedPartition::SearchLevels(const std::vector<SizedID> &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);
++level;
};

std::size_t max_size = 0.5 * graph.NumberOfNodes();
std::queue<std::pair<BisectionID, std::int32_t>> id_queue;
while (!current_level.empty())
{
std::size_t total_size = 0;
std::size_t count = 0;
std::queue<std::pair<BisectionID, std::int32_t>> id_queue;
for (auto element : current_level)
{
// don't relax final cells
Expand All @@ -230,7 +233,7 @@ void AnnotatedPartition::SearchLevels(const std::vector<SizedID> &implicit_tree,

current_level.clear();

const auto relax = [&id_queue, implicit_tree, avg_size, &current_level](
const auto relax = [&id_queue, &implicit_tree, avg_size, &current_level](
const std::pair<BisectionID, std::uint32_t> &element) {
const auto size = getCellSize(implicit_tree, element.first, element.second);
if (!hasChildren(implicit_tree, element.first, element.second))
Expand All @@ -247,8 +250,8 @@ void AnnotatedPartition::SearchLevels(const std::vector<SizedID> &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);
}
Expand Down Expand Up @@ -283,13 +286,15 @@ 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];
if (cell_id == INVALID_CELLID)
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) {
Expand Down Expand Up @@ -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,
Expand All @@ -345,13 +349,17 @@ AnnotatedPartition::AnalyseLevel(const BisectionGraph &graph,
std::move(cell_sizes_vec)};
}

std::vector<std::uint32_t> AnnotatedPartition::ComputeCellIDs(
const std::vector<std::pair<BisectionID, std::int32_t>> &prefixes,
const BisectionGraph &graph,
const std::vector<BisectionID> &bisection_ids) const
std::vector<std::uint32_t>
AnnotatedPartition::ComputeCellIDs(std::vector<std::pair<BisectionID, std::int32_t>> &prefixes,
const BisectionGraph &graph,
const std::vector<BisectionID> &bisection_ids) const
{
std::vector<std::uint32_t> 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
Expand All @@ -361,12 +369,19 @@ std::vector<std::uint32_t> 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;
}

Expand Down
4 changes: 4 additions & 0 deletions src/partition/partitioner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -80,7 +81,10 @@ void LogStatistics(const std::string &filename, std::vector<std::uint32_t> 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<std::uint32_t> bisection_ids)
Expand Down
4 changes: 2 additions & 2 deletions src/partition/recursive_bisection_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::size_t>(consecutive_id) < ordered_component_ids.size());
BOOST_ASSERT(static_cast<std::size_t>(std::distance(ordered_component_ids.begin(), itr)) <
ordered_component_ids.size());
return std::distance(ordered_component_ids.begin(), itr);
};

Expand Down

0 comments on commit 7a61617

Please sign in to comment.