From 41af9615cdaf5fa1838deab743b8707e3e873b87 Mon Sep 17 00:00:00 2001 From: xlaussel Date: Mon, 23 Nov 2020 23:00:27 +0100 Subject: [PATCH] Improvements --- .../routing_algorithms/routing_base_ch.hpp | 57 +++++++++---------- .../routing_algorithms/routing_base_mld.hpp | 8 +-- include/util/query_heap.hpp | 4 +- src/contractor/contractor_search.cpp | 2 +- .../alternative_path_ch.cpp | 4 +- .../routing_algorithms/many_to_many_ch.cpp | 25 ++++---- .../routing_algorithms/many_to_many_mld.cpp | 6 +- 7 files changed, 50 insertions(+), 56 deletions(-) diff --git a/include/engine/routing_algorithms/routing_base_ch.hpp b/include/engine/routing_algorithms/routing_base_ch.hpp index dcbe969cb..4ec2cbeb9 100644 --- a/include/engine/routing_algorithms/routing_base_ch.hpp +++ b/include/engine/routing_algorithms/routing_base_ch.hpp @@ -24,11 +24,10 @@ namespace ch // Stalling template bool stallAtNode(const DataFacade &facade, - const NodeID node, - const EdgeWeight weight, + const typename HeapT::HeapNode& heapNode, const HeapT &query_heap) { - for (auto edge : facade.GetAdjacentEdgeRange(node)) + for (auto edge : facade.GetAdjacentEdgeRange(heapNode.node)) { const auto &data = facade.GetEdgeData(edge); if (DIRECTION == REVERSE_DIRECTION ? data.forward : data.backward) @@ -36,10 +35,10 @@ bool stallAtNode(const DataFacade &facade, const NodeID to = facade.GetTarget(edge); const EdgeWeight edge_weight = data.weight; 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->weight + edge_weight < weight) + if (toHeapNode->weight + edge_weight < heapNode.weight) { return true; } @@ -51,11 +50,10 @@ bool stallAtNode(const DataFacade &facade, template void relaxOutgoingEdges(const DataFacade &facade, - const NodeID node, - const EdgeWeight weight, + const SearchEngineData::QueryHeap::HeapNode& heapNode, SearchEngineData::QueryHeap &heap) { - for (const auto edge : facade.GetAdjacentEdgeRange(node)) + for (const auto edge : facade.GetAdjacentEdgeRange(heapNode.node)) { const auto &data = facade.GetEdgeData(edge); if (DIRECTION == FORWARD_DIRECTION ? data.forward : data.backward) @@ -64,21 +62,21 @@ void relaxOutgoingEdges(const DataFacade &facade, const EdgeWeight edge_weight = data.weight; 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 - if (!toData) + if (!toHeapNode) { - heap.Insert(to, to_weight, node); + heap.Insert(to, to_weight, heapNode.node); } // Found a shorter Path -> Update weight - else if (to_weight < toData->weight) + else if (to_weight < toHeapNode->weight) { // new parent - toData->data.parent = node; - toData->weight=to_weight; - heap.DecreaseKey(*toData); + toHeapNode->data.parent = heapNode.node; + toHeapNode->weight=to_weight; + heap.DecreaseKey(*toHeapNode); } } } @@ -125,36 +123,35 @@ void routingStep(const DataFacade &facade, const bool force_loop_forward, const bool force_loop_reverse) { - auto nodeData = forward_heap.DeleteMinGetHeapNode(); - const EdgeWeight weight = nodeData.weight; + auto heapNode = forward_heap.DeleteMinGetHeapNode(); + auto reverseHeapNode = reverse_heap.GetHeapNodeIfWasInserted(heapNode.node); - auto reverseNodeData= reverse_heap.WasInsertedGetHeapNode(nodeData.node); - if (reverseNodeData) + if (reverseHeapNode) { - const EdgeWeight new_weight = reverseNodeData->weight + weight; + const EdgeWeight new_weight = reverseHeapNode->weight + heapNode.weight; if (new_weight < upper_bound) { // if loops are forced, they are so at the source - if ((force_loop_forward && nodeData.data.parent == nodeData.node) || - (force_loop_reverse && reverseNodeData->data.parent == nodeData.node) || + if ((force_loop_forward && heapNode.data.parent == heapNode.node) || + (force_loop_reverse && reverseHeapNode->data.parent == heapNode.node) || // in this case we are looking at a bi-directional way where the source // and target phantom are on the same edge based node new_weight < 0) { // 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); if (DIRECTION == FORWARD_DIRECTION ? data.forward : data.backward) { const NodeID to = facade.GetTarget(edge); - if (to == nodeData.node) + if (to == heapNode.node) { const EdgeWeight edge_weight = data.weight; const EdgeWeight loop_weight = new_weight + edge_weight; if (loop_weight >= 0 && loop_weight < upper_bound) { - middle_node_id = nodeData.node; + middle_node_id = heapNode.node; upper_bound = loop_weight; } } @@ -165,7 +162,7 @@ void routingStep(const DataFacade &facade, { BOOST_ASSERT(new_weight >= 0); - middle_node_id = nodeData.node; + middle_node_id = heapNode.node; upper_bound = new_weight; } } @@ -174,19 +171,19 @@ void routingStep(const DataFacade &facade, // 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 BOOST_ASSERT(min_edge_offset <= 0); - if (weight + min_edge_offset > upper_bound) + if (heapNode.weight + min_edge_offset > upper_bound) { forward_heap.DeleteAll(); return; } // Stalling - if (STALLING && stallAtNode(facade, nodeData.node, weight, forward_heap)) + if (STALLING && stallAtNode(facade, heapNode, forward_heap)) { return; } - relaxOutgoingEdges(facade, nodeData.node, weight, forward_heap); + relaxOutgoingEdges(facade, heapNode, forward_heap); } template diff --git a/include/engine/routing_algorithms/routing_base_mld.hpp b/include/engine/routing_algorithms/routing_base_mld.hpp index b76a4160a..7a51120af 100644 --- a/include/engine/routing_algorithms/routing_base_mld.hpp +++ b/include/engine/routing_algorithms/routing_base_mld.hpp @@ -255,7 +255,7 @@ void relaxOutgoingEdges(const DataFacade &facade, { const EdgeWeight to_weight = weight + shortcut_weight; BOOST_ASSERT(to_weight >= weight); - auto toNodeData= forward_heap.WasInsertedGetHeapNode(to); + auto toNodeData= forward_heap.GetHeapNodeIfWasInserted(to); if (!toNodeData) { forward_heap.Insert(to, to_weight, {heapNode.node, true}); @@ -284,7 +284,7 @@ void relaxOutgoingEdges(const DataFacade &facade, { const EdgeWeight to_weight = weight + shortcut_weight; BOOST_ASSERT(to_weight >= weight); - auto toNodeData= forward_heap.WasInsertedGetHeapNode(to); + auto toNodeData= forward_heap.GetHeapNodeIfWasInserted(to); if (!toNodeData) { forward_heap.Insert(to, to_weight, {heapNode.node, true}); @@ -322,7 +322,7 @@ void relaxOutgoingEdges(const DataFacade &facade, const EdgeWeight to_weight = weight + node_weight + turn_penalty; - auto toNodeData= forward_heap.WasInsertedGetHeapNode(to); + auto toNodeData= forward_heap.GetHeapNodeIfWasInserted(to); if (!toNodeData) { forward_heap.Insert(to, to_weight, {heapNode.node, false}); @@ -358,7 +358,7 @@ void routingStep(const DataFacade &facade, // is weight + reverse_weight // More tighter upper bound requires additional condition reverse_heap.WasRemoved(to) // 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) { auto reverse_weight = reverveNodeData->weight; diff --git a/include/util/query_heap.hpp b/include/util/query_heap.hpp index fec3f0e36..3bf8421c5 100644 --- a/include/util/query_heap.hpp +++ b/include/util/query_heap.hpp @@ -294,7 +294,7 @@ class QueryHeap return inserted_nodes[index].node == node; } - boost::optional WasInsertedGetHeapNode(const NodeID node) + boost::optional GetHeapNodeIfWasInserted(const NodeID node) { const auto index = node_index.peek_index(node); if (index >= static_cast(inserted_nodes.size())) @@ -304,7 +304,7 @@ class QueryHeap return inserted_nodes[index]; } - const boost::optional WasInsertedGetHeapNode(const NodeID node) const + const boost::optional GetHeapNodeIfWasInserted(const NodeID node) const { const auto index = node_index.peek_index(node); if (index >= static_cast(inserted_nodes.size())) diff --git a/src/contractor/contractor_search.cpp b/src/contractor/contractor_search.cpp index cac78cde5..2eaacd848 100644 --- a/src/contractor/contractor_search.cpp +++ b/src/contractor/contractor_search.cpp @@ -32,7 +32,7 @@ void relaxNode(ContractorHeap &heap, } 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 if (!toHeapNode) { diff --git a/src/engine/routing_algorithms/alternative_path_ch.cpp b/src/engine/routing_algorithms/alternative_path_ch.cpp index 158bcba21..78b160ad6 100644 --- a/src/engine/routing_algorithms/alternative_path_ch.cpp +++ b/src/engine/routing_algorithms/alternative_path_ch.cpp @@ -76,7 +76,7 @@ void alternativeRoutingStep(const DataFacade &facade, 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) { search_space_intersection.emplace_back(heapNode.node); @@ -114,7 +114,7 @@ void alternativeRoutingStep(const DataFacade &facade, BOOST_ASSERT(edge_weight > 0); 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 if (!toHeapNode) { diff --git a/src/engine/routing_algorithms/many_to_many_ch.cpp b/src/engine/routing_algorithms/many_to_many_ch.cpp index 7c82b0adf..6ccf6e95d 100644 --- a/src/engine/routing_algorithms/many_to_many_ch.cpp +++ b/src/engine/routing_algorithms/many_to_many_ch.cpp @@ -46,19 +46,16 @@ inline bool addLoopWeight(const DataFacade &facade, template void relaxOutgoingEdges(const DataFacade &facade, - const NodeID node, - const EdgeWeight weight, - const EdgeDuration duration, - const EdgeDistance distance, + const typename SearchEngineData::ManyToManyQueryHeap::HeapNode& heapNode, typename SearchEngineData::ManyToManyQueryHeap &query_heap, const PhantomNode &) { - if (stallAtNode(facade, node, weight, query_heap)) + if (stallAtNode(facade, heapNode, query_heap)) { return; } - for (auto edge : facade.GetAdjacentEdgeRange(node)) + for (auto edge : facade.GetAdjacentEdgeRange(heapNode.node)) { const auto &data = facade.GetEdgeData(edge); if (DIRECTION == FORWARD_DIRECTION ? data.forward : data.backward) @@ -70,21 +67,21 @@ void relaxOutgoingEdges(const DataFacade &facade, const auto edge_distance = data.distance; BOOST_ASSERT_MSG(edge_weight > 0, "edge_weight invalid"); - const auto to_weight = weight + edge_weight; - const auto to_duration = duration + edge_duration; - const auto to_distance = distance + edge_distance; + const auto to_weight = heapNode.weight + edge_weight; + const auto to_duration = heapNode.data.duration + edge_duration; + 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 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 else if (std::tie(to_weight, 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; query_heap.DecreaseKey(*toHeapNode); } @@ -155,7 +152,7 @@ void forwardRoutingStep(const DataFacade &facade, } relaxOutgoingEdges( - facade, heapNode.node, source_weight, source_duration, source_distance, query_heap, phantom_node); + facade, heapNode, query_heap, phantom_node); } void backwardRoutingStep(const DataFacade &facade, @@ -175,7 +172,7 @@ void backwardRoutingStep(const DataFacade &facade, heapNode.node, parent, column_index, target_weight, target_duration, target_distance); relaxOutgoingEdges( - facade, heapNode.node, target_weight, target_duration, target_distance, query_heap, phantom_node); + facade, heapNode, query_heap, phantom_node); } } // namespace ch diff --git a/src/engine/routing_algorithms/many_to_many_mld.cpp b/src/engine/routing_algorithms/many_to_many_mld.cpp index 4b3638750..796d8eb8a 100644 --- a/src/engine/routing_algorithms/many_to_many_mld.cpp +++ b/src/engine/routing_algorithms/many_to_many_mld.cpp @@ -71,7 +71,7 @@ void relaxBorderEdges(const DataFacade &facade, const auto to_distance = distance + node_distance; // 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) { query_heap.Insert(to, to_weight, {node, false, to_duration, to_distance}); @@ -134,7 +134,7 @@ void relaxOutgoingEdges(const DataFacade &facade, const auto to_weight = weight + shortcut_weight; const auto to_duration = duration + shortcut_durations.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) { query_heap.Insert(to, to_weight, {node, true, to_duration, to_distance}); @@ -174,7 +174,7 @@ void relaxOutgoingEdges(const DataFacade &facade, const auto to_weight = weight + shortcut_weight; const auto to_duration = duration + shortcut_durations.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) { query_heap.Insert(to, to_weight, {node, true, to_duration, to_distance});