diff --git a/RoutingAlgorithms/AlternativePathRouting.h b/RoutingAlgorithms/AlternativePathRouting.h index 8162b8e8e..e9ba6fee7 100644 --- a/RoutingAlgorithms/AlternativePathRouting.h +++ b/RoutingAlgorithms/AlternativePathRouting.h @@ -38,9 +38,9 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include -const double VIAPATH_ALPHA = 0.15; -const double VIAPATH_EPSILON = 0.10; //alternative at most 15% longer -const double VIAPATH_GAMMA = 0.75; //alternative shares at most 75% with the shortest. +const double VIAPATH_ALPHA = 0.10; +const double VIAPATH_EPSILON = 0.15; // alternative at most 15% longer +const double VIAPATH_GAMMA = 0.75; // alternative shares at most 75% with the shortest. template class AlternativeRouting : private BasicRoutingInterface { @@ -50,10 +50,14 @@ class AlternativeRouting : private BasicRoutingInterface { typedef std::pair SearchSpaceEdge; struct RankedCandidateNode { - RankedCandidateNode(const NodeID n, const int l, const int s) : - node(n), - length(l), - sharing(s) + RankedCandidateNode( + const NodeID node, + const int length, + const int sharing + ) : + node(node), + length(length), + sharing(sharing) { } NodeID node; @@ -255,7 +259,7 @@ public: const int approximated_length = forward_heap1.GetKey(node) + reverse_heap1.GetKey(node); const bool length_passes = (approximated_length < upper_bound_to_shortest_path_distance*(1+VIAPATH_EPSILON)); const bool sharing_passes = (approximated_sharing <= upper_bound_to_shortest_path_distance*VIAPATH_GAMMA); - const bool stretch_passes = (approximated_length - approximated_sharing) < ((1.+VIAPATH_EPSILON)*(upper_bound_to_shortest_path_distance-approximated_sharing)); + const bool stretch_passes = (approximated_length - approximated_sharing) < ((1.+VIAPATH_ALPHA)*(upper_bound_to_shortest_path_distance-approximated_sharing)); if( length_passes && sharing_passes && stretch_passes ) { preselected_node_list.push_back(node); @@ -279,7 +283,7 @@ public: int length_of_via_path = 0, sharing_of_via_path = 0; ComputeLengthAndSharingOfViaPath(node, &length_of_via_path, &sharing_of_via_path, forward_offset+reverse_offset, packed_shortest_path); const int maximum_allowed_sharing = upper_bound_to_shortest_path_distance*VIAPATH_GAMMA; - if( sharing_of_via_path <= maximum_allowed_sharing ) { + if( sharing_of_via_path <= maximum_allowed_sharing && length_of_via_path <= upper_bound_to_shortest_path_distance*(1+VIAPATH_EPSILON)) { ranked_candidates_list.push_back( RankedCandidateNode( node, @@ -317,22 +321,16 @@ public: // int start_offset = ( packed_shortest_path.front() == phantom_node_pair.startPhantom.forward_node_id ? 1 : -1 )*phantom_node_pair.startPhantom.fwd_segment_position; // SimpleLogger().Write(logDEBUG) << "unpacking from index " << phantom_node_pair.startPhantom.fwd_segment_position; - SimpleLogger().Write(logDEBUG) << "phantom_node_pair.startPhantom.forward_node_id: " << phantom_node_pair.startPhantom.forward_node_id; - SimpleLogger().Write(logDEBUG) << "phantom_node_pair.startPhantom.reverse_node_id: " << phantom_node_pair.startPhantom.reverse_node_id; - SimpleLogger().Write(logDEBUG) << "phantom_node_pair.targetPhantom.packed_geometry_id: " << phantom_node_pair.targetPhantom.packed_geometry_id; + // SimpleLogger().Write(logDEBUG) << "phantom_node_pair.startPhantom.forward_node_id: " << phantom_node_pair.startPhantom.forward_node_id; + // SimpleLogger().Write(logDEBUG) << "phantom_node_pair.startPhantom.reverse_node_id: " << phantom_node_pair.startPhantom.reverse_node_id; + // SimpleLogger().Write(logDEBUG) << "phantom_node_pair.targetPhantom.packed_geometry_id: " << phantom_node_pair.targetPhantom.packed_geometry_id; // SimpleLogger().Write(logDEBUG) << "packed_shortest_path.back(): " << packed_shortest_path.back(); super::UnpackPath( // -- packed input packed_shortest_path, // -- start of route - phantom_node_pair.startPhantom.packed_geometry_id, - phantom_node_pair.startPhantom.fwd_segment_position, - (packed_shortest_path.front() != phantom_node_pair.startPhantom.forward_node_id), - // -- end of route - phantom_node_pair.targetPhantom.packed_geometry_id, - phantom_node_pair.targetPhantom.fwd_segment_position, - (packed_shortest_path.back() != phantom_node_pair.targetPhantom.forward_node_id), + phantom_node_pair, // -- unpacked output raw_route_data.unpacked_path_segments.front() ); @@ -356,12 +354,7 @@ public: // unpack the alternate path super::UnpackPath( packed_alternate_path, - phantom_node_pair.startPhantom.packed_geometry_id, - phantom_node_pair.startPhantom.fwd_segment_position, - (packed_alternate_path.front() != phantom_node_pair.startPhantom.forward_node_id), - phantom_node_pair.targetPhantom.packed_geometry_id, - phantom_node_pair.targetPhantom.fwd_segment_position, - (packed_alternate_path.back() != phantom_node_pair.targetPhantom.forward_node_id), + phantom_node_pair, raw_route_data.unpacked_alternative ); @@ -406,10 +399,17 @@ private: ); } - inline void ComputeLengthAndSharingOfViaPath(const NodeID via_node, int *real_length_of_via_path, int *sharing_of_via_path, - const int offset, const std::vector & packed_shortest_path) { - //compute and unpack and by exploring search spaces from v and intersecting against queues - //only half-searches have to be done at this stage + //TODO: reorder parameters + // compute and unpack and by exploring search spaces + // from v and intersecting against queues. only half-searches have to be + // done at this stage + inline void ComputeLengthAndSharingOfViaPath( + const NodeID via_node, + int *real_length_of_via_path, + int *sharing_of_via_path, + const int offset, + const std::vector & packed_shortest_path + ) { engine_working_data.InitializeOrClearSecondThreadLocalStorage( super::facade->GetNumberOfNodes() ); @@ -609,6 +609,7 @@ private: // return sharing; // } + //todo: reorder parameters template inline void AlternativeRoutingStep( QueryHeap & forward_heap, @@ -622,7 +623,12 @@ private: const NodeID node = forward_heap.DeleteMin(); const int distance = forward_heap.GetKey(node); const int scaled_distance = (distance-edge_expansion_offset)/(1.+VIAPATH_EPSILON); - if( scaled_distance > *upper_bound_to_shortest_path_distance ){ + // SimpleLogger().Write(logDEBUG) << "ub: " << *upper_bound_to_shortest_path_distance << ", distance: " << distance << ", scaled_distance: " << scaled_distance; + if( + (INVALID_EDGE_WEIGHT != *upper_bound_to_shortest_path_distance) && + (scaled_distance > *upper_bound_to_shortest_path_distance) + ) { + // SimpleLogger().Write(logDEBUG) << "removing nodes from heap"; forward_heap.DeleteAll(); return; } diff --git a/RoutingAlgorithms/BasicRoutingInterface.h b/RoutingAlgorithms/BasicRoutingInterface.h index ba5d60926..63bcd1ac3 100644 --- a/RoutingAlgorithms/BasicRoutingInterface.h +++ b/RoutingAlgorithms/BasicRoutingInterface.h @@ -38,8 +38,6 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include -#include - #include SearchEngineData::SearchEngineHeapPtr SearchEngineData::forwardHeap; @@ -140,14 +138,12 @@ public: //TODO: refactor parameters to only edge ids for start and end inline void UnpackPath( const std::vector & packed_path, - const unsigned packed_geometry_id_of_first_edge, - const int fwd_index_offset, - const bool start_traversed_in_reverse, - const unsigned packed_geometry_id_of_last_edge, - const int rev_index_offset, - const bool target_traversed_in_reverse, + const PhantomNodes & phantom_node_pair, std::vector & unpacked_path ) const { + const bool start_traversed_in_reverse = (packed_path.front() != phantom_node_pair.startPhantom.forward_node_id); + const bool target_traversed_in_reverse = (packed_path.back() != phantom_node_pair.targetPhantom.forward_node_id); + const unsigned packed_path_size = packed_path.size(); std::stack > recursion_stack; @@ -230,7 +226,7 @@ public: std::vector id_vector; facade->GetUncompressedGeometry(facade->GetGeometryIndexForEdgeID(ed.id), id_vector); - const int start_index = ( unpacked_path.empty() ? ( ( start_traversed_in_reverse ) ? id_vector.size() - fwd_index_offset - 1 : fwd_index_offset ) : 0 ); + const int start_index = ( unpacked_path.empty() ? ( ( start_traversed_in_reverse ) ? id_vector.size() - phantom_node_pair.startPhantom.fwd_segment_position - 1 : phantom_node_pair.startPhantom.fwd_segment_position ) : 0 ); const int end_index = id_vector.size(); BOOST_ASSERT( start_index >= 0 ); @@ -254,21 +250,22 @@ public: } } } - if(SPECIAL_EDGEID != packed_geometry_id_of_last_edge) { - SimpleLogger().Write(logDEBUG) << "unpacking last segment " << packed_geometry_id_of_last_edge; + if(SPECIAL_EDGEID != phantom_node_pair.targetPhantom.packed_geometry_id ) { + SimpleLogger().Write(logDEBUG) << "unpacking last segment " << phantom_node_pair.targetPhantom.packed_geometry_id; SimpleLogger().Write(logDEBUG) << "start_traversed_in_reverse: " << (start_traversed_in_reverse ? "y" : "n"); SimpleLogger().Write(logDEBUG) << "target_traversed_in_reverse: " << (target_traversed_in_reverse ? "y" : "n"); - SimpleLogger().Write(logDEBUG) << "fwd_index_offset: " << fwd_index_offset << ", rev_index_offset: " << rev_index_offset; + SimpleLogger().Write(logDEBUG) << "phantom_node_pair.startPhantom.fwd_segment_position: " << phantom_node_pair.startPhantom.fwd_segment_position << ", " << + "phantom_node_pair.targetPhantom.fwd_segment_position: " << phantom_node_pair.targetPhantom.fwd_segment_position; std::vector id_vector; - facade->GetUncompressedGeometry(packed_geometry_id_of_last_edge, id_vector); + facade->GetUncompressedGeometry(phantom_node_pair.targetPhantom.packed_geometry_id, id_vector); if( target_traversed_in_reverse ) { std::reverse(id_vector.begin(), id_vector.end() ); } SimpleLogger().Write(logDEBUG) << "id_vector.size() " << id_vector.size(); - const bool start_and_end_on_same_edge = (packed_geometry_id_of_first_edge == packed_geometry_id_of_last_edge) && unpacked_path.empty(); + const bool start_and_end_on_same_edge = (phantom_node_pair.startPhantom.packed_geometry_id == phantom_node_pair.targetPhantom.packed_geometry_id) && unpacked_path.empty(); - const int start_index = ( start_and_end_on_same_edge ? id_vector.size() - fwd_index_offset : 0 ); - const int end_index = (target_traversed_in_reverse ? id_vector.size() - rev_index_offset : rev_index_offset); + const int start_index = ( start_and_end_on_same_edge ? phantom_node_pair.startPhantom.fwd_segment_position : 0 ); + const int end_index = (target_traversed_in_reverse ? id_vector.size() - phantom_node_pair.targetPhantom.fwd_segment_position : phantom_node_pair.targetPhantom.fwd_segment_position); SimpleLogger().Write(logDEBUG) << "fetching from [" << start_index << "," << end_index << "]"; @@ -283,7 +280,7 @@ public: unpacked_path.push_back( PathData( id_vector[i], - 0, + phantom_node_pair.targetPhantom.name_id, TurnInstructionsClass::NoTurn, 0 ) diff --git a/RoutingAlgorithms/ShortestPathRouting.h b/RoutingAlgorithms/ShortestPathRouting.h index f7719db9b..6225aa644 100644 --- a/RoutingAlgorithms/ShortestPathRouting.h +++ b/RoutingAlgorithms/ShortestPathRouting.h @@ -359,7 +359,7 @@ public: std::swap( packed_legs1, packed_legs2 ); } raw_route_data.unpacked_path_segments.resize( packed_legs1.size() ); - const int start_offset = ( packed_legs1[0].front() == phantom_nodes_vector.front().startPhantom.forward_node_id ? 1 : -1 )*phantom_nodes_vector.front().startPhantom.fwd_segment_position; + // const int start_offset = ( packed_legs1[0].front() == phantom_nodes_vector.front().startPhantom.forward_node_id ? 1 : -1 )*phantom_nodes_vector.front().startPhantom.fwd_segment_position; for(unsigned i = 0; i < packed_legs1.size(); ++i){ BOOST_ASSERT( !phantom_nodes_vector.empty() ); @@ -367,30 +367,27 @@ public: const bool at_end = (packed_legs1.size() == i+1); BOOST_ASSERT(packed_legs1.size() == raw_route_data.unpacked_path_segments.size() ); + PhantomNodes unpack_phantom_node_pair = phantom_nodes_vector[i]; + if (!at_beginning) + { + unpack_phantom_node_pair.startPhantom.packed_geometry_id = SPECIAL_EDGEID; + unpack_phantom_node_pair.startPhantom.fwd_segment_position = 0; + } + + if (!at_end) + { + unpack_phantom_node_pair.targetPhantom.packed_geometry_id = SPECIAL_EDGEID; + unpack_phantom_node_pair.targetPhantom.fwd_segment_position = 0; + } super::UnpackPath( // -- packed input packed_legs1[i], - // -- start of route - ( !at_beginning ? SPECIAL_EDGEID : phantom_nodes_vector.front().startPhantom.packed_geometry_id ), - ( !at_beginning ? 0 : phantom_nodes_vector.front().startPhantom.fwd_segment_position ), - ( !at_beginning ? false : (packed_legs1.front().front() != phantom_nodes_vector.front().startPhantom.forward_node_id) ), - // -- end of route - ( !at_end ? SPECIAL_EDGEID : phantom_nodes_vector.back().targetPhantom.packed_geometry_id ), - ( !at_end ? 0 : phantom_nodes_vector.back().targetPhantom.fwd_segment_position ), - ( !at_end ? false : (packed_legs1.back().back() != phantom_nodes_vector.back().targetPhantom.forward_node_id) ), + // -- start and end of (sub-)route + unpack_phantom_node_pair, // -- unpacked output raw_route_data.unpacked_path_segments[i] ); - - - // // TODO: properly unpack first and last segments - // super::UnpackPath( - // packed_legs1[i], - // SPECIAL_EDGEID, ( at_beginning ? start_offset : 0), false, - // SPECIAL_EDGEID, 0, false, - // raw_route_data.unpacked_path_segments[i] - // ); } raw_route_data.lengthOfShortestPath = std::min(distance1, distance2); }