From b4750b17f874280140c7297e3a1a66b28ae8089a Mon Sep 17 00:00:00 2001 From: Kajari Ghosh Date: Sun, 1 Apr 2018 22:53:27 -0400 Subject: [PATCH] distance --- .../contiguous_internalmem_datafacade.hpp | 4 +- .../routing_algorithms/routing_base.hpp | 16 +++++++ .../routing_algorithms/routing_base_ch.hpp | 46 ++++++++++++++----- include/engine/unpacking_cache.hpp | 17 ++++--- include/util/typedefs.hpp | 2 + .../routing_algorithms/many_to_many_ch.cpp | 15 +++++- 6 files changed, 79 insertions(+), 21 deletions(-) diff --git a/include/engine/datafacade/contiguous_internalmem_datafacade.hpp b/include/engine/datafacade/contiguous_internalmem_datafacade.hpp index 83c97df2c71..d3d166f6292 100644 --- a/include/engine/datafacade/contiguous_internalmem_datafacade.hpp +++ b/include/engine/datafacade/contiguous_internalmem_datafacade.hpp @@ -566,7 +566,9 @@ class ContiguousInternalMemoryDataFacadeBase : public BaseDataFacade // node and edge information access util::Coordinate GetCoordinateOfNode(const NodeID id) const override final { - return m_coordinate_list[id]; + return m_coordinate_list.at(id); // get default + // return m_coordinate_list[id]; causes socket to hang up when we pass in garbage node_is + // (turn_id) } OSMNodeID GetOSMNodeIDOfNode(const NodeID id) const override final diff --git a/include/engine/routing_algorithms/routing_base.hpp b/include/engine/routing_algorithms/routing_base.hpp index 6a0d578a910..fed087f416a 100644 --- a/include/engine/routing_algorithms/routing_base.hpp +++ b/include/engine/routing_algorithms/routing_base.hpp @@ -424,6 +424,22 @@ EdgeDuration computeEdgeDuration(const FacadeT &facade, NodeID node_id, NodeID t return total_duration; } +template +EdgeDuration computeEdgeDistance(const FacadeT &facade, NodeID node_id_1, NodeID node_id_2) +{ + // how do we get the second node_id? + // node_id_2 is not the node id that we want + const auto coordinate_1 = facade.GetCoordinateOfNode(node_id_1); + const auto coordinate_2 = facade.GetCoordinateOfNode(node_id_2); + const auto osm_id_1 = facade.GetOSMNodeIDOfNode(node_id_1); + const auto osm_id_2 = facade.GetOSMNodeIDOfNode(node_id_2); + std::cout << "coordinate_1: " << coordinate_1.lon << ", " << coordinate_1.lat << std::endl; + std::cout << "coordinate_2: " << coordinate_2.lon << ", " << coordinate_2.lat << std::endl; + std::cout << "osm_id_1: " << osm_id_1 << std::endl; + std::cout << "osm_id_2: " << osm_id_2 << std::endl; + return util::coordinate_calculation::haversineDistance(coordinate_1, coordinate_2); +} + } // namespace routing_algorithms } // namespace engine } // namespace osrm diff --git a/include/engine/routing_algorithms/routing_base_ch.hpp b/include/engine/routing_algorithms/routing_base_ch.hpp index dd1d31eed55..73764b8a6ae 100644 --- a/include/engine/routing_algorithms/routing_base_ch.hpp +++ b/include/engine/routing_algorithms/routing_base_ch.hpp @@ -289,19 +289,21 @@ void unpackPath(const DataFacade &facade, } } +using PathAnnotation = std::pair; template -EdgeDuration calculateEBGNodeAnnotations(const DataFacade &facade, - BidirectionalIterator packed_path_begin, - BidirectionalIterator packed_path_end, - UnpackingCache &unpacking_cache) +PathAnnotation calculateEBGNodeAnnotations(const DataFacade &facade, + BidirectionalIterator packed_path_begin, + BidirectionalIterator packed_path_end, + UnpackingCache &unpacking_cache) { // make sure we have at least something to unpack if (packed_path_begin == packed_path_end || std::distance(packed_path_begin, packed_path_end) <= 1) - return 0; + return std::make_pair(0, 0); std::stack> recursion_stack; std::stack duration_stack; + std::stack distance_stack; // We have to push the path in reverse order onto the stack because it's LIFO. for (auto current = std::prev(packed_path_end); current != packed_path_begin; @@ -326,9 +328,10 @@ EdgeDuration calculateEBGNodeAnnotations(const DataFacade &facade, if (unpacking_cache.IsEdgeInCache(std::make_tuple( std::get<0>(edge), std::get<1>(edge), facade.GetExcludeIndex()))) { - EdgeDuration duration = unpacking_cache.GetDuration(std::make_tuple( + PathAnnotation annotation = unpacking_cache.GetAnnotation(std::make_tuple( std::get<0>(edge), std::get<1>(edge), facade.GetExcludeIndex())); - duration_stack.emplace(duration); + duration_stack.emplace(annotation.first); + distance_stack.emplace(annotation.second); } else { @@ -369,14 +372,17 @@ EdgeDuration calculateEBGNodeAnnotations(const DataFacade &facade, } else { - auto temp = std::make_tuple( + auto new_edge = std::make_tuple( std::get<0>(edge), std::get<1>(edge), facade.GetExcludeIndex()); // compute the duration here and put it onto the duration stack using method // similar to annotatePath but smaller EdgeDuration duration = computeEdgeDuration(facade, std::get<0>(edge), data.turn_id); + EdgeDistance distance = + computeEdgeDistance(facade, std::get<0>(edge), std::get<1>(edge)); duration_stack.emplace(duration); - unpacking_cache.AddEdge(temp, duration); + distance_stack.emplace(distance); + unpacking_cache.AddEdge(new_edge, std::make_pair(duration, distance)); } } } @@ -391,9 +397,19 @@ EdgeDuration calculateEBGNodeAnnotations(const DataFacade &facade, duration_stack.pop(); EdgeDuration duration = edge1 + edge2; duration_stack.emplace(duration); + + BOOST_ASSERT_MSG(distance_stack.size() >= 2, + "There are not enough (at least 2) values on the distance stack"); + EdgeDistance distance1 = distance_stack.top(); + distance_stack.pop(); + EdgeDistance distance2 = distance_stack.top(); + distance_stack.pop(); + EdgeDistance distance = distance1 + distance2; + distance_stack.emplace(distance); + unpacking_cache.AddEdge( std::make_tuple(std::get<0>(edge), std::get<1>(edge), facade.GetExcludeIndex()), - duration); + std::make_pair(duration, distance)); } } @@ -403,7 +419,15 @@ EdgeDuration calculateEBGNodeAnnotations(const DataFacade &facade, total_duration += duration_stack.top(); duration_stack.pop(); } - return total_duration; + + EdgeDistance total_distance = 0; + while (!distance_stack.empty()) + { + total_distance += distance_stack.top(); + distance_stack.pop(); + } + + return std::make_pair(total_duration, total_distance); } template diff --git a/include/engine/unpacking_cache.hpp b/include/engine/unpacking_cache.hpp index a91ff8f7e6f..b3b7084fa55 100644 --- a/include/engine/unpacking_cache.hpp +++ b/include/engine/unpacking_cache.hpp @@ -10,14 +10,16 @@ namespace osrm { namespace engine { +using PathAnnotation = std::pair; class UnpackingCache { private: - boost::compute::detail::lru_cache, EdgeDuration> cache; + boost::compute::detail::lru_cache, PathAnnotation> + cache; unsigned current_data_timestamp = 0; public: - UnpackingCache(unsigned timestamp) : cache(200), current_data_timestamp(timestamp){}; + UnpackingCache(unsigned timestamp) : cache(16000000), current_data_timestamp(timestamp){}; void Clear(unsigned new_data_timestamp) { @@ -33,15 +35,16 @@ class UnpackingCache return cache.contains(edge); } - void AddEdge(std::tuple edge, EdgeDuration duration) + void AddEdge(std::tuple edge, PathAnnotation annotation) { - cache.insert(edge, duration); + cache.insert(edge, annotation); } - EdgeDuration GetDuration(std::tuple edge) + PathAnnotation GetAnnotation(std::tuple edge) { - boost::optional duration = cache.get(edge); - return *duration ? *duration : MAXIMAL_EDGE_DURATION; + boost::optional annotation = cache.get(edge); + return annotation ? *annotation + : std::make_pair(MAXIMAL_EDGE_DURATION, MAXIMAL_EDGE_DISTANCE); } }; } // engine diff --git a/include/util/typedefs.hpp b/include/util/typedefs.hpp index 9b8ecc82bbc..7ffba8bf6cc 100644 --- a/include/util/typedefs.hpp +++ b/include/util/typedefs.hpp @@ -75,6 +75,7 @@ using NameID = std::uint32_t; using AnnotationID = std::uint32_t; using EdgeWeight = std::int32_t; using EdgeDuration = std::int32_t; +using EdgeDistance = double; using SegmentWeight = std::uint32_t; using SegmentDuration = std::uint32_t; using TurnPenalty = std::int16_t; // turn penalty in 100ms units @@ -113,6 +114,7 @@ static const SegmentDuration MAX_SEGMENT_DURATION = INVALID_SEGMENT_DURATION - 1 static const EdgeWeight INVALID_EDGE_WEIGHT = std::numeric_limits::max(); static const EdgeDuration MAXIMAL_EDGE_DURATION = std::numeric_limits::max(); static const TurnPenalty INVALID_TURN_PENALTY = std::numeric_limits::max(); +static const EdgeDistance MAXIMAL_EDGE_DISTANCE = std::numeric_limits::max(); // FIXME the bitfields we use require a reduced maximal duration, this should be kept consistent // within the code base. For now we have to ensure that we don't case 30 bit to -1 and break any diff --git a/src/engine/routing_algorithms/many_to_many_ch.cpp b/src/engine/routing_algorithms/many_to_many_ch.cpp index 7acf47aa29b..b4657217ecf 100644 --- a/src/engine/routing_algorithms/many_to_many_ch.cpp +++ b/src/engine/routing_algorithms/many_to_many_ch.cpp @@ -220,6 +220,7 @@ std::vector manyToManySearch(SearchEngineData &engi std::vector weights_table(number_of_entries, INVALID_EDGE_WEIGHT); std::vector durations_table(number_of_entries, MAXIMAL_EDGE_DURATION); + std::vector distance_table(number_of_entries, MAXIMAL_EDGE_DISTANCE); std::vector middle_nodes_table(number_of_entries, SPECIAL_NODEID); std::vector search_space_with_buckets; @@ -289,7 +290,10 @@ std::vector manyToManySearch(SearchEngineData &engi if (middle_node_id == SPECIAL_NODEID) // takes care of one-ways { - durations_table[row_idx * number_of_targets + column_idx] = MAXIMAL_EDGE_DURATION; + durations_table[row_idx * number_of_targets + column_idx] = + MAXIMAL_EDGE_DURATION; // should this be invalid edge duration? what is the + // difference between maximal and invalid? + durations_table[row_idx * number_of_targets + column_idx] = MAXIMAL_EDGE_DISTANCE; continue; } @@ -317,12 +321,15 @@ std::vector manyToManySearch(SearchEngineData &engi } if (!packed_leg.empty()) { - durations_table[row_idx * number_of_targets + column_idx] = + auto annotation = ch::calculateEBGNodeAnnotations(facade, packed_leg.begin(), packed_leg.end(), *engine_working_data.unpacking_cache.get()); + durations_table[row_idx * number_of_targets + column_idx] = annotation.first; + distance_table[row_idx * number_of_targets + column_idx] = annotation.second; + // check the direction of travel to figure out how to calculate the offset to/from // the source/target if (source_phantom.forward_segment_id.id == packed_leg.front()) @@ -364,6 +371,10 @@ std::vector manyToManySearch(SearchEngineData &engi packed_leg.clear(); } } + std::cout << "distance table: "; + for (auto it = distance_table.begin(); it != distance_table.end(); it++) + std::cout << *it << ", "; + std::cout << std::endl; return durations_table; }