Skip to content

Commit

Permalink
Split routing_base into CH and non-CH parts
Browse files Browse the repository at this point in the history
  • Loading branch information
oxidase committed Mar 10, 2017
1 parent 0dc155b commit fcdb3b2
Show file tree
Hide file tree
Showing 8 changed files with 746 additions and 740 deletions.
597 changes: 117 additions & 480 deletions include/engine/routing_algorithms/routing_base.hpp

Large diffs are not rendered by default.

475 changes: 475 additions & 0 deletions include/engine/routing_algorithms/routing_base_ch.hpp

Large diffs are not rendered by default.

204 changes: 88 additions & 116 deletions src/engine/routing_algorithms/alternative_path.cpp

Large diffs are not rendered by default.

48 changes: 7 additions & 41 deletions src/engine/routing_algorithms/direct_shortest_path.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "engine/routing_algorithms/direct_shortest_path.hpp"

#include "engine/routing_algorithms/routing_base.hpp"
#include "engine/routing_algorithms/routing_base_ch.hpp"

namespace osrm
{
Expand All @@ -9,44 +10,8 @@ namespace engine
namespace routing_algorithms
{

namespace
namespace ch
{
void insertInHeaps(SearchEngineData::QueryHeap &forward_heap,
SearchEngineData::QueryHeap &reverse_heap,
const PhantomNodes &nodes)
{
const auto &source_phantom = nodes.source_phantom;
const auto &target_phantom = nodes.target_phantom;
BOOST_ASSERT(source_phantom.IsValid());
BOOST_ASSERT(target_phantom.IsValid());

if (source_phantom.forward_segment_id.enabled)
{
forward_heap.Insert(source_phantom.forward_segment_id.id,
-source_phantom.GetForwardWeightPlusOffset(),
source_phantom.forward_segment_id.id);
}
if (source_phantom.reverse_segment_id.enabled)
{
forward_heap.Insert(source_phantom.reverse_segment_id.id,
-source_phantom.GetReverseWeightPlusOffset(),
source_phantom.reverse_segment_id.id);
}

if (target_phantom.forward_segment_id.enabled)
{
reverse_heap.Insert(target_phantom.forward_segment_id.id,
target_phantom.GetForwardWeightPlusOffset(),
target_phantom.forward_segment_id.id);
}

if (target_phantom.reverse_segment_id.enabled)
{
reverse_heap.Insert(target_phantom.reverse_segment_id.id,
target_phantom.GetReverseWeightPlusOffset(),
target_phantom.reverse_segment_id.id);
}
}

template <typename AlgorithmT>
InternalRouteResult
Expand Down Expand Up @@ -82,7 +47,6 @@ extractRoute(const datafacade::ContiguousInternalMemoryDataFacade<AlgorithmT> &f

return raw_route_data;
}
}

/// This is a striped down version of the general shortest path algorithm.
/// The general algorithm always computes two queries for each leg. This is only
Expand All @@ -109,7 +73,7 @@ InternalRouteResult directShortestPathSearchImpl(

int weight = INVALID_EDGE_WEIGHT;
std::vector<NodeID> packed_leg;
insertInHeaps(forward_heap, reverse_heap, phantom_nodes);
insertNodesInHeaps(forward_heap, reverse_heap, phantom_nodes);

search(facade,
forward_heap,
Expand All @@ -124,20 +88,22 @@ InternalRouteResult directShortestPathSearchImpl(
return extractRoute(facade, weight, packed_leg, phantom_nodes);
}

} // namespace ch

InternalRouteResult directShortestPathSearch(
SearchEngineData &engine_working_data,
const datafacade::ContiguousInternalMemoryDataFacade<algorithm::CoreCH> &facade,
const PhantomNodes &phantom_nodes)
{
return directShortestPathSearchImpl(engine_working_data, facade, phantom_nodes);
return ch::directShortestPathSearchImpl(engine_working_data, facade, phantom_nodes);
}

InternalRouteResult directShortestPathSearch(
SearchEngineData &engine_working_data,
const datafacade::ContiguousInternalMemoryDataFacade<algorithm::CH> &facade,
const PhantomNodes &phantom_nodes)
{
return directShortestPathSearchImpl(engine_working_data, facade, phantom_nodes);
return ch::directShortestPathSearchImpl(engine_working_data, facade, phantom_nodes);
}

} // namespace routing_algorithms
Expand Down
42 changes: 9 additions & 33 deletions src/engine/routing_algorithms/many_to_many.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "engine/routing_algorithms/many_to_many.hpp"
#include "engine/routing_algorithms/routing_base.hpp"
#include "engine/routing_algorithms/routing_base_ch.hpp"

#include <boost/assert.hpp>

Expand Down Expand Up @@ -101,14 +101,14 @@ void forwardRoutingStep(const datafacade::ContiguousInternalMemoryDataFacade<alg
const EdgeWeight new_weight = source_weight + target_weight;
if (new_weight < 0)
{
const EdgeWeight loop_weight = getLoopWeight<false>(facade, node);
const EdgeWeight loop_weight = ch::getLoopWeight<false>(facade, node);
const EdgeWeight new_weight_with_loop = new_weight + loop_weight;
if (loop_weight != INVALID_EDGE_WEIGHT && new_weight_with_loop >= 0)
{
current_weight = std::min(current_weight, new_weight_with_loop);
current_duration = std::min(current_duration,
source_duration + target_duration +
getLoopWeight<true>(facade, node));
ch::getLoopWeight<true>(facade, node));
}
}
else if (new_weight < current_weight)
Expand All @@ -118,7 +118,7 @@ void forwardRoutingStep(const datafacade::ContiguousInternalMemoryDataFacade<alg
}
}
}
if (stallAtNode<FORWARD_DIRECTION>(facade, node, source_weight, query_heap))
if (ch::stallAtNode<FORWARD_DIRECTION>(facade, node, source_weight, query_heap))
{
return;
}
Expand All @@ -139,7 +139,7 @@ void backwardRoutingStep(
// store settled nodes in search space bucket
search_space_with_buckets[node].emplace_back(column_idx, target_weight, target_duration);

if (stallAtNode<REVERSE_DIRECTION>(facade, node, target_weight, query_heap))
if (ch::stallAtNode<REVERSE_DIRECTION>(facade, node, target_weight, query_heap))
{
return;
}
Expand Down Expand Up @@ -172,21 +172,9 @@ manyToManySearch(SearchEngineData &engine_working_data,

unsigned column_idx = 0;
const auto search_target_phantom = [&](const PhantomNode &phantom) {
// clear heap and insert target nodes
query_heap.Clear();
// insert target(s) at weight 0

if (phantom.forward_segment_id.enabled)
{
query_heap.Insert(phantom.forward_segment_id.id,
phantom.GetForwardWeightPlusOffset(),
{phantom.forward_segment_id.id, phantom.GetForwardDuration()});
}
if (phantom.reverse_segment_id.enabled)
{
query_heap.Insert(phantom.reverse_segment_id.id,
phantom.GetReverseWeightPlusOffset(),
{phantom.reverse_segment_id.id, phantom.GetReverseDuration()});
}
insertNodesInHeap<REVERSE_DIRECTION>(query_heap, phantom);

// explore search space
while (!query_heap.Empty())
Expand All @@ -199,21 +187,9 @@ manyToManySearch(SearchEngineData &engine_working_data,
// for each source do forward search
unsigned row_idx = 0;
const auto search_source_phantom = [&](const PhantomNode &phantom) {
// clear heap and insert source nodes
query_heap.Clear();
// insert target(s) at weight 0

if (phantom.forward_segment_id.enabled)
{
query_heap.Insert(phantom.forward_segment_id.id,
-phantom.GetForwardWeightPlusOffset(),
{phantom.forward_segment_id.id, -phantom.GetForwardDuration()});
}
if (phantom.reverse_segment_id.enabled)
{
query_heap.Insert(phantom.reverse_segment_id.id,
-phantom.GetReverseWeightPlusOffset(),
{phantom.reverse_segment_id.id, -phantom.GetReverseDuration()});
}
insertNodesInHeap<FORWARD_DIRECTION>(query_heap, phantom);

// explore search space
while (!query_heap.Empty())
Expand Down
18 changes: 9 additions & 9 deletions src/engine/routing_algorithms/map_matching.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "engine/routing_algorithms/map_matching.hpp"
#include "engine/routing_algorithms/routing_base.hpp"
#include "engine/routing_algorithms/routing_base_ch.hpp"

#include "engine/map_matching/hidden_markov_model.hpp"
#include "engine/map_matching/matching_confidence.hpp"
Expand Down Expand Up @@ -210,14 +210,14 @@ mapMatchingImpl(SearchEngineData &engine_working_data,
}

double network_distance =
getNetworkDistance(facade,
forward_heap,
reverse_heap,
forward_core_heap,
reverse_core_heap,
prev_unbroken_timestamps_list[s].phantom_node,
current_timestamps_list[s_prime].phantom_node,
duration_upper_bound);
ch::getNetworkDistance(facade,
forward_heap,
reverse_heap,
forward_core_heap,
reverse_core_heap,
prev_unbroken_timestamps_list[s].phantom_node,
current_timestamps_list[s_prime].phantom_node,
duration_upper_bound);

// get distance diff between loc1/2 and locs/s_prime
const auto d_t = std::abs(network_distance - haversine_distance);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#include "engine/routing_algorithms/routing_base.hpp"
#include "engine/routing_algorithms/routing_base_ch.hpp"

namespace osrm
{
namespace engine
{
namespace routing_algorithms
{
namespace ch
{

/**
* Unpacks a single edge (NodeID->NodeID) from the CH graph down to it's original non-shortcut
Expand Down Expand Up @@ -411,31 +413,7 @@ getNetworkDistance(const datafacade::ContiguousInternalMemoryDataFacade<algorith
forward_core_heap.Clear();
reverse_core_heap.Clear();

if (source_phantom.forward_segment_id.enabled)
{
forward_heap.Insert(source_phantom.forward_segment_id.id,
-source_phantom.GetForwardWeightPlusOffset(),
source_phantom.forward_segment_id.id);
}
if (source_phantom.reverse_segment_id.enabled)
{
forward_heap.Insert(source_phantom.reverse_segment_id.id,
-source_phantom.GetReverseWeightPlusOffset(),
source_phantom.reverse_segment_id.id);
}

if (target_phantom.forward_segment_id.enabled)
{
reverse_heap.Insert(target_phantom.forward_segment_id.id,
target_phantom.GetForwardWeightPlusOffset(),
target_phantom.forward_segment_id.id);
}
if (target_phantom.reverse_segment_id.enabled)
{
reverse_heap.Insert(target_phantom.reverse_segment_id.id,
target_phantom.GetReverseWeightPlusOffset(),
target_phantom.reverse_segment_id.id);
}
insertNodesInHeaps(forward_heap, reverse_heap, {source_phantom, target_phantom});

EdgeWeight weight = INVALID_EDGE_WEIGHT;
std::vector<NodeID> packed_path;
Expand Down Expand Up @@ -517,6 +495,7 @@ getNetworkDistance(const datafacade::ContiguousInternalMemoryDataFacade<algorith
return getPathDistance(facade, packed_path, source_phantom, target_phantom);
}

} // namespace ch
} // namespace routing_algorithms
} // namespace engine
} // namespace osrm
71 changes: 36 additions & 35 deletions src/engine/routing_algorithms/shortest_path.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "engine/routing_algorithms/shortest_path.hpp"
#include "engine/routing_algorithms/routing_base.hpp"
#include "engine/routing_algorithms/routing_base_ch.hpp"

#include <boost/assert.hpp>
#include <boost/optional.hpp>
Expand Down Expand Up @@ -71,23 +71,24 @@ void searchWithUTurn(const datafacade::ContiguousInternalMemoryDataFacade<Algori
auto is_oneway_source = !(search_from_forward_node && search_from_reverse_node);
auto is_oneway_target = !(search_to_forward_node && search_to_reverse_node);
// we only enable loops here if we can't search from forward to backward node
auto needs_loop_forwad = is_oneway_source && needsLoopForward(source_phantom, target_phantom);
auto needs_loop_forwad =
is_oneway_source && ch::needsLoopForward(source_phantom, target_phantom);
auto needs_loop_backwards =
is_oneway_target && needsLoopBackwards(source_phantom, target_phantom);
is_oneway_target && ch::needsLoopBackwards(source_phantom, target_phantom);

forward_core_heap.Clear();
reverse_core_heap.Clear();
BOOST_ASSERT(forward_core_heap.Size() == 0);
BOOST_ASSERT(reverse_core_heap.Size() == 0);
routing_algorithms::search(facade,
forward_heap,
reverse_heap,
forward_core_heap,
reverse_core_heap,
new_total_weight,
leg_packed_path,
needs_loop_forwad,
needs_loop_backwards);
ch::search(facade,
forward_heap,
reverse_heap,
forward_core_heap,
reverse_core_heap,
new_total_weight,
leg_packed_path,
needs_loop_forwad,
needs_loop_backwards);

// if no route is found between two parts of the via-route, the entire route becomes
// invalid. Adding to invalid edge weight sadly doesn't return an invalid edge weight. Here
Expand Down Expand Up @@ -147,15 +148,15 @@ void search(const datafacade::ContiguousInternalMemoryDataFacade<AlgorithmT> &fa
reverse_core_heap.Clear();
BOOST_ASSERT(forward_core_heap.Size() == 0);
BOOST_ASSERT(reverse_core_heap.Size() == 0);
routing_algorithms::search(facade,
forward_heap,
reverse_heap,
forward_core_heap,
reverse_core_heap,
new_total_weight_to_forward,
leg_packed_path_forward,
needsLoopForward(source_phantom, target_phantom),
DO_NOT_FORCE_LOOP);
ch::search(facade,
forward_heap,
reverse_heap,
forward_core_heap,
reverse_core_heap,
new_total_weight_to_forward,
leg_packed_path_forward,
ch::needsLoopForward(source_phantom, target_phantom),
DO_NOT_FORCE_LOOP);
}

if (search_to_reverse_node)
Expand Down Expand Up @@ -185,15 +186,15 @@ void search(const datafacade::ContiguousInternalMemoryDataFacade<AlgorithmT> &fa
reverse_core_heap.Clear();
BOOST_ASSERT(forward_core_heap.Size() == 0);
BOOST_ASSERT(reverse_core_heap.Size() == 0);
routing_algorithms::search(facade,
forward_heap,
reverse_heap,
forward_core_heap,
reverse_core_heap,
new_total_weight_to_reverse,
leg_packed_path_reverse,
DO_NOT_FORCE_LOOP,
needsLoopBackwards(source_phantom, target_phantom));
ch::search(facade,
forward_heap,
reverse_heap,
forward_core_heap,
reverse_core_heap,
new_total_weight_to_reverse,
leg_packed_path_reverse,
DO_NOT_FORCE_LOOP,
ch::needsLoopBackwards(source_phantom, target_phantom));
}
}

Expand All @@ -213,11 +214,11 @@ void unpackLegs(const datafacade::ContiguousInternalMemoryDataFacade<algorithm::
auto leg_begin = total_packed_path.begin() + packed_leg_begin[current_leg];
auto leg_end = total_packed_path.begin() + packed_leg_begin[current_leg + 1];
const auto &unpack_phantom_node_pair = phantom_nodes_vector[current_leg];
unpackPath(facade,
leg_begin,
leg_end,
unpack_phantom_node_pair,
raw_route_data.unpacked_path_segments[current_leg]);
ch::unpackPath(facade,
leg_begin,
leg_end,
unpack_phantom_node_pair,
raw_route_data.unpacked_path_segments[current_leg]);

raw_route_data.source_traversed_in_reverse.push_back(
(*leg_begin != phantom_nodes_vector[current_leg].source_phantom.forward_segment_id.id));
Expand Down

0 comments on commit fcdb3b2

Please sign in to comment.