Improvements

This commit is contained in:
xlaussel 2020-11-23 23:00:27 +01:00
parent 13067844ee
commit 41af9615cd
7 changed files with 50 additions and 56 deletions

View File

@ -24,11 +24,10 @@ namespace ch
// Stalling // Stalling
template <bool DIRECTION, typename HeapT> template <bool DIRECTION, typename HeapT>
bool stallAtNode(const DataFacade<Algorithm> &facade, bool stallAtNode(const DataFacade<Algorithm> &facade,
const NodeID node, const typename HeapT::HeapNode& heapNode,
const EdgeWeight weight,
const HeapT &query_heap) const HeapT &query_heap)
{ {
for (auto edge : facade.GetAdjacentEdgeRange(node)) for (auto edge : facade.GetAdjacentEdgeRange(heapNode.node))
{ {
const auto &data = facade.GetEdgeData(edge); const auto &data = facade.GetEdgeData(edge);
if (DIRECTION == REVERSE_DIRECTION ? data.forward : data.backward) if (DIRECTION == REVERSE_DIRECTION ? data.forward : data.backward)
@ -36,10 +35,10 @@ bool stallAtNode(const DataFacade<Algorithm> &facade,
const NodeID to = facade.GetTarget(edge); const NodeID to = facade.GetTarget(edge);
const EdgeWeight edge_weight = data.weight; const EdgeWeight edge_weight = data.weight;
BOOST_ASSERT_MSG(edge_weight > 0, "edge_weight invalid"); BOOST_ASSERT_MSG(edge_weight > 0, "edge_weight invalid");
const auto& toHeapNode= query_heap.WasInsertedGetHeapNode(to); const auto& toHeapNode= query_heap.GetHeapNodeIfWasInserted(to);
if (toHeapNode) if (toHeapNode)
{ {
if (toHeapNode->weight + edge_weight < weight) if (toHeapNode->weight + edge_weight < heapNode.weight)
{ {
return true; return true;
} }
@ -51,11 +50,10 @@ bool stallAtNode(const DataFacade<Algorithm> &facade,
template <bool DIRECTION> template <bool DIRECTION>
void relaxOutgoingEdges(const DataFacade<Algorithm> &facade, void relaxOutgoingEdges(const DataFacade<Algorithm> &facade,
const NodeID node, const SearchEngineData<Algorithm>::QueryHeap::HeapNode& heapNode,
const EdgeWeight weight,
SearchEngineData<Algorithm>::QueryHeap &heap) SearchEngineData<Algorithm>::QueryHeap &heap)
{ {
for (const auto edge : facade.GetAdjacentEdgeRange(node)) for (const auto edge : facade.GetAdjacentEdgeRange(heapNode.node))
{ {
const auto &data = facade.GetEdgeData(edge); const auto &data = facade.GetEdgeData(edge);
if (DIRECTION == FORWARD_DIRECTION ? data.forward : data.backward) if (DIRECTION == FORWARD_DIRECTION ? data.forward : data.backward)
@ -64,21 +62,21 @@ void relaxOutgoingEdges(const DataFacade<Algorithm> &facade,
const EdgeWeight edge_weight = data.weight; const EdgeWeight edge_weight = data.weight;
BOOST_ASSERT_MSG(edge_weight > 0, "edge_weight invalid"); BOOST_ASSERT_MSG(edge_weight > 0, "edge_weight invalid");
const EdgeWeight to_weight = weight + edge_weight; const EdgeWeight to_weight = heapNode.weight + edge_weight;
auto toData= heap.WasInsertedGetHeapNode(to); auto toHeapNode = heap.GetHeapNodeIfWasInserted(to);
// New Node discovered -> Add to Heap + Node Info Storage // New Node discovered -> Add to Heap + Node Info Storage
if (!toData) if (!toHeapNode)
{ {
heap.Insert(to, to_weight, node); heap.Insert(to, to_weight, heapNode.node);
} }
// Found a shorter Path -> Update weight // Found a shorter Path -> Update weight
else if (to_weight < toData->weight) else if (to_weight < toHeapNode->weight)
{ {
// new parent // new parent
toData->data.parent = node; toHeapNode->data.parent = heapNode.node;
toData->weight=to_weight; toHeapNode->weight=to_weight;
heap.DecreaseKey(*toData); heap.DecreaseKey(*toHeapNode);
} }
} }
} }
@ -125,36 +123,35 @@ void routingStep(const DataFacade<Algorithm> &facade,
const bool force_loop_forward, const bool force_loop_forward,
const bool force_loop_reverse) const bool force_loop_reverse)
{ {
auto nodeData = forward_heap.DeleteMinGetHeapNode(); auto heapNode = forward_heap.DeleteMinGetHeapNode();
const EdgeWeight weight = nodeData.weight; auto reverseHeapNode = reverse_heap.GetHeapNodeIfWasInserted(heapNode.node);
auto reverseNodeData= reverse_heap.WasInsertedGetHeapNode(nodeData.node); if (reverseHeapNode)
if (reverseNodeData)
{ {
const EdgeWeight new_weight = reverseNodeData->weight + weight; const EdgeWeight new_weight = reverseHeapNode->weight + heapNode.weight;
if (new_weight < upper_bound) if (new_weight < upper_bound)
{ {
// if loops are forced, they are so at the source // if loops are forced, they are so at the source
if ((force_loop_forward && nodeData.data.parent == nodeData.node) || if ((force_loop_forward && heapNode.data.parent == heapNode.node) ||
(force_loop_reverse && reverseNodeData->data.parent == nodeData.node) || (force_loop_reverse && reverseHeapNode->data.parent == heapNode.node) ||
// in this case we are looking at a bi-directional way where the source // in this case we are looking at a bi-directional way where the source
// and target phantom are on the same edge based node // and target phantom are on the same edge based node
new_weight < 0) new_weight < 0)
{ {
// check whether there is a loop present at the node // check whether there is a loop present at the node
for (const auto edge : facade.GetAdjacentEdgeRange(nodeData.node)) for (const auto edge : facade.GetAdjacentEdgeRange(heapNode.node))
{ {
const auto &data = facade.GetEdgeData(edge); const auto &data = facade.GetEdgeData(edge);
if (DIRECTION == FORWARD_DIRECTION ? data.forward : data.backward) if (DIRECTION == FORWARD_DIRECTION ? data.forward : data.backward)
{ {
const NodeID to = facade.GetTarget(edge); const NodeID to = facade.GetTarget(edge);
if (to == nodeData.node) if (to == heapNode.node)
{ {
const EdgeWeight edge_weight = data.weight; const EdgeWeight edge_weight = data.weight;
const EdgeWeight loop_weight = new_weight + edge_weight; const EdgeWeight loop_weight = new_weight + edge_weight;
if (loop_weight >= 0 && loop_weight < upper_bound) if (loop_weight >= 0 && loop_weight < upper_bound)
{ {
middle_node_id = nodeData.node; middle_node_id = heapNode.node;
upper_bound = loop_weight; upper_bound = loop_weight;
} }
} }
@ -165,7 +162,7 @@ void routingStep(const DataFacade<Algorithm> &facade,
{ {
BOOST_ASSERT(new_weight >= 0); BOOST_ASSERT(new_weight >= 0);
middle_node_id = nodeData.node; middle_node_id = heapNode.node;
upper_bound = new_weight; upper_bound = new_weight;
} }
} }
@ -174,19 +171,19 @@ void routingStep(const DataFacade<Algorithm> &facade,
// make sure we don't terminate too early if we initialize the weight // make sure we don't terminate too early if we initialize the weight
// for the nodes in the forward heap with the forward/reverse offset // for the nodes in the forward heap with the forward/reverse offset
BOOST_ASSERT(min_edge_offset <= 0); BOOST_ASSERT(min_edge_offset <= 0);
if (weight + min_edge_offset > upper_bound) if (heapNode.weight + min_edge_offset > upper_bound)
{ {
forward_heap.DeleteAll(); forward_heap.DeleteAll();
return; return;
} }
// Stalling // Stalling
if (STALLING && stallAtNode<DIRECTION>(facade, nodeData.node, weight, forward_heap)) if (STALLING && stallAtNode<DIRECTION>(facade, heapNode, forward_heap))
{ {
return; return;
} }
relaxOutgoingEdges<DIRECTION>(facade, nodeData.node, weight, forward_heap); relaxOutgoingEdges<DIRECTION>(facade, heapNode, forward_heap);
} }
template <bool UseDuration> template <bool UseDuration>

View File

@ -255,7 +255,7 @@ void relaxOutgoingEdges(const DataFacade<Algorithm> &facade,
{ {
const EdgeWeight to_weight = weight + shortcut_weight; const EdgeWeight to_weight = weight + shortcut_weight;
BOOST_ASSERT(to_weight >= weight); BOOST_ASSERT(to_weight >= weight);
auto toNodeData= forward_heap.WasInsertedGetHeapNode(to); auto toNodeData= forward_heap.GetHeapNodeIfWasInserted(to);
if (!toNodeData) if (!toNodeData)
{ {
forward_heap.Insert(to, to_weight, {heapNode.node, true}); forward_heap.Insert(to, to_weight, {heapNode.node, true});
@ -284,7 +284,7 @@ void relaxOutgoingEdges(const DataFacade<Algorithm> &facade,
{ {
const EdgeWeight to_weight = weight + shortcut_weight; const EdgeWeight to_weight = weight + shortcut_weight;
BOOST_ASSERT(to_weight >= weight); BOOST_ASSERT(to_weight >= weight);
auto toNodeData= forward_heap.WasInsertedGetHeapNode(to); auto toNodeData= forward_heap.GetHeapNodeIfWasInserted(to);
if (!toNodeData) if (!toNodeData)
{ {
forward_heap.Insert(to, to_weight, {heapNode.node, true}); forward_heap.Insert(to, to_weight, {heapNode.node, true});
@ -322,7 +322,7 @@ void relaxOutgoingEdges(const DataFacade<Algorithm> &facade,
const EdgeWeight to_weight = weight + node_weight + turn_penalty; const EdgeWeight to_weight = weight + node_weight + turn_penalty;
auto toNodeData= forward_heap.WasInsertedGetHeapNode(to); auto toNodeData= forward_heap.GetHeapNodeIfWasInserted(to);
if (!toNodeData) if (!toNodeData)
{ {
forward_heap.Insert(to, to_weight, {heapNode.node, false}); forward_heap.Insert(to, to_weight, {heapNode.node, false});
@ -358,7 +358,7 @@ void routingStep(const DataFacade<Algorithm> &facade,
// is weight + reverse_weight // is weight + reverse_weight
// More tighter upper bound requires additional condition reverse_heap.WasRemoved(to) // More tighter upper bound requires additional condition reverse_heap.WasRemoved(to)
// with weight(to -> target) = reverse_weight and all weights ≥ 0 // with weight(to -> target) = reverse_weight and all weights ≥ 0
auto reverveNodeData= reverse_heap.WasInsertedGetHeapNode(heapNode.node); auto reverveNodeData= reverse_heap.GetHeapNodeIfWasInserted(heapNode.node);
if (reverveNodeData) if (reverveNodeData)
{ {
auto reverse_weight = reverveNodeData->weight; auto reverse_weight = reverveNodeData->weight;

View File

@ -294,7 +294,7 @@ class QueryHeap
return inserted_nodes[index].node == node; return inserted_nodes[index].node == node;
} }
boost::optional<HeapNode&> WasInsertedGetHeapNode(const NodeID node) boost::optional<HeapNode&> GetHeapNodeIfWasInserted(const NodeID node)
{ {
const auto index = node_index.peek_index(node); const auto index = node_index.peek_index(node);
if (index >= static_cast<decltype(index)>(inserted_nodes.size())) if (index >= static_cast<decltype(index)>(inserted_nodes.size()))
@ -304,7 +304,7 @@ class QueryHeap
return inserted_nodes[index]; return inserted_nodes[index];
} }
const boost::optional<const HeapNode&> WasInsertedGetHeapNode(const NodeID node) const const boost::optional<const HeapNode&> GetHeapNodeIfWasInserted(const NodeID node) const
{ {
const auto index = node_index.peek_index(node); const auto index = node_index.peek_index(node);
if (index >= static_cast<decltype(index)>(inserted_nodes.size())) if (index >= static_cast<decltype(index)>(inserted_nodes.size()))

View File

@ -32,7 +32,7 @@ void relaxNode(ContractorHeap &heap,
} }
const EdgeWeight to_weight = node_weight + data.weight; const EdgeWeight to_weight = node_weight + data.weight;
const auto& toHeapNode=heap.WasInsertedGetHeapNode(to); const auto& toHeapNode= heap.GetHeapNodeIfWasInserted(to);
// New Node discovered -> Add to Heap + Node Info Storage // New Node discovered -> Add to Heap + Node Info Storage
if (!toHeapNode) if (!toHeapNode)
{ {

View File

@ -76,7 +76,7 @@ void alternativeRoutingStep(const DataFacade<Algorithm> &facade,
search_space.emplace_back(heapNode.data.parent, heapNode.node); search_space.emplace_back(heapNode.data.parent, heapNode.node);
const auto& reverseHeapNode=reverse_heap.WasInsertedGetHeapNode(heapNode.node); const auto& reverseHeapNode= reverse_heap.GetHeapNodeIfWasInserted(heapNode.node);
if (reverseHeapNode) if (reverseHeapNode)
{ {
search_space_intersection.emplace_back(heapNode.node); search_space_intersection.emplace_back(heapNode.node);
@ -114,7 +114,7 @@ void alternativeRoutingStep(const DataFacade<Algorithm> &facade,
BOOST_ASSERT(edge_weight > 0); BOOST_ASSERT(edge_weight > 0);
const EdgeWeight to_weight = weight + edge_weight; const EdgeWeight to_weight = weight + edge_weight;
const auto& toHeapNode=forward_heap.WasInsertedGetHeapNode(to); const auto& toHeapNode= forward_heap.GetHeapNodeIfWasInserted(to);
// New Node discovered -> Add to Heap + Node Info Storage // New Node discovered -> Add to Heap + Node Info Storage
if (!toHeapNode) if (!toHeapNode)
{ {

View File

@ -46,19 +46,16 @@ inline bool addLoopWeight(const DataFacade<ch::Algorithm> &facade,
template <bool DIRECTION> template <bool DIRECTION>
void relaxOutgoingEdges(const DataFacade<Algorithm> &facade, void relaxOutgoingEdges(const DataFacade<Algorithm> &facade,
const NodeID node, const typename SearchEngineData<Algorithm>::ManyToManyQueryHeap::HeapNode& heapNode,
const EdgeWeight weight,
const EdgeDuration duration,
const EdgeDistance distance,
typename SearchEngineData<Algorithm>::ManyToManyQueryHeap &query_heap, typename SearchEngineData<Algorithm>::ManyToManyQueryHeap &query_heap,
const PhantomNode &) const PhantomNode &)
{ {
if (stallAtNode<DIRECTION>(facade, node, weight, query_heap)) if (stallAtNode<DIRECTION>(facade, heapNode, query_heap))
{ {
return; return;
} }
for (auto edge : facade.GetAdjacentEdgeRange(node)) for (auto edge : facade.GetAdjacentEdgeRange(heapNode.node))
{ {
const auto &data = facade.GetEdgeData(edge); const auto &data = facade.GetEdgeData(edge);
if (DIRECTION == FORWARD_DIRECTION ? data.forward : data.backward) if (DIRECTION == FORWARD_DIRECTION ? data.forward : data.backward)
@ -70,21 +67,21 @@ void relaxOutgoingEdges(const DataFacade<Algorithm> &facade,
const auto edge_distance = data.distance; const auto edge_distance = data.distance;
BOOST_ASSERT_MSG(edge_weight > 0, "edge_weight invalid"); BOOST_ASSERT_MSG(edge_weight > 0, "edge_weight invalid");
const auto to_weight = weight + edge_weight; const auto to_weight = heapNode.weight + edge_weight;
const auto to_duration = duration + edge_duration; const auto to_duration = heapNode.data.duration + edge_duration;
const auto to_distance = distance + edge_distance; const auto to_distance = heapNode.data.distance + edge_distance;
const auto& toHeapNode=query_heap.WasInsertedGetHeapNode(to); const auto& toHeapNode= query_heap.GetHeapNodeIfWasInserted(to);
// New Node discovered -> Add to Heap + Node Info Storage // New Node discovered -> Add to Heap + Node Info Storage
if (!toHeapNode) if (!toHeapNode)
{ {
query_heap.Insert(to, to_weight, {node, to_duration, to_distance}); query_heap.Insert(to, to_weight, {heapNode.node, to_duration, to_distance});
} }
// Found a shorter Path -> Update weight and set new parent // Found a shorter Path -> Update weight and set new parent
else if (std::tie(to_weight, to_duration) < else if (std::tie(to_weight, to_duration) <
std::tie(query_heap.GetKey(to), query_heap.GetData(to).duration)) std::tie(query_heap.GetKey(to), query_heap.GetData(to).duration))
{ {
toHeapNode->data = {node, to_duration, to_distance}; toHeapNode->data = {heapNode.node, to_duration, to_distance};
toHeapNode->weight=to_weight; toHeapNode->weight=to_weight;
query_heap.DecreaseKey(*toHeapNode); query_heap.DecreaseKey(*toHeapNode);
} }
@ -155,7 +152,7 @@ void forwardRoutingStep(const DataFacade<Algorithm> &facade,
} }
relaxOutgoingEdges<FORWARD_DIRECTION>( relaxOutgoingEdges<FORWARD_DIRECTION>(
facade, heapNode.node, source_weight, source_duration, source_distance, query_heap, phantom_node); facade, heapNode, query_heap, phantom_node);
} }
void backwardRoutingStep(const DataFacade<Algorithm> &facade, void backwardRoutingStep(const DataFacade<Algorithm> &facade,
@ -175,7 +172,7 @@ void backwardRoutingStep(const DataFacade<Algorithm> &facade,
heapNode.node, parent, column_index, target_weight, target_duration, target_distance); heapNode.node, parent, column_index, target_weight, target_duration, target_distance);
relaxOutgoingEdges<REVERSE_DIRECTION>( relaxOutgoingEdges<REVERSE_DIRECTION>(
facade, heapNode.node, target_weight, target_duration, target_distance, query_heap, phantom_node); facade, heapNode, query_heap, phantom_node);
} }
} // namespace ch } // namespace ch

View File

@ -71,7 +71,7 @@ void relaxBorderEdges(const DataFacade<mld::Algorithm> &facade,
const auto to_distance = distance + node_distance; const auto to_distance = distance + node_distance;
// New Node discovered -> Add to Heap + Node Info Storage // New Node discovered -> Add to Heap + Node Info Storage
const auto& toHeapNode=query_heap.WasInsertedGetHeapNode(to); const auto& toHeapNode= query_heap.GetHeapNodeIfWasInserted(to);
if (!toHeapNode) if (!toHeapNode)
{ {
query_heap.Insert(to, to_weight, {node, false, to_duration, to_distance}); query_heap.Insert(to, to_weight, {node, false, to_duration, to_distance});
@ -134,7 +134,7 @@ void relaxOutgoingEdges(const DataFacade<mld::Algorithm> &facade,
const auto to_weight = weight + shortcut_weight; const auto to_weight = weight + shortcut_weight;
const auto to_duration = duration + shortcut_durations.front(); const auto to_duration = duration + shortcut_durations.front();
const auto to_distance = distance + shortcut_distances.front(); const auto to_distance = distance + shortcut_distances.front();
const auto& toHeapNode=query_heap.WasInsertedGetHeapNode(to); const auto& toHeapNode= query_heap.GetHeapNodeIfWasInserted(to);
if (!toHeapNode) if (!toHeapNode)
{ {
query_heap.Insert(to, to_weight, {node, true, to_duration, to_distance}); query_heap.Insert(to, to_weight, {node, true, to_duration, to_distance});
@ -174,7 +174,7 @@ void relaxOutgoingEdges(const DataFacade<mld::Algorithm> &facade,
const auto to_weight = weight + shortcut_weight; const auto to_weight = weight + shortcut_weight;
const auto to_duration = duration + shortcut_durations.front(); const auto to_duration = duration + shortcut_durations.front();
const auto to_distance = distance + shortcut_distances.front(); const auto to_distance = distance + shortcut_distances.front();
const auto& toHeapNode=query_heap.WasInsertedGetHeapNode(to); const auto& toHeapNode= query_heap.GetHeapNodeIfWasInserted(to);
if (!toHeapNode) if (!toHeapNode)
{ {
query_heap.Insert(to, to_weight, {node, true, to_duration, to_distance}); query_heap.Insert(to, to_weight, {node, true, to_duration, to_distance});