Skip to content

Commit

Permalink
distance
Browse files Browse the repository at this point in the history
  • Loading branch information
ghoshkaj committed Apr 2, 2018
1 parent 0659fe2 commit b4750b1
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions include/engine/routing_algorithms/routing_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,22 @@ EdgeDuration computeEdgeDuration(const FacadeT &facade, NodeID node_id, NodeID t
return total_duration;
}

template <typename FacadeT>
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
Expand Down
46 changes: 35 additions & 11 deletions include/engine/routing_algorithms/routing_base_ch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,19 +289,21 @@ void unpackPath(const DataFacade<Algorithm> &facade,
}
}

using PathAnnotation = std::pair<EdgeDuration, EdgeDistance>;
template <typename BidirectionalIterator>
EdgeDuration calculateEBGNodeAnnotations(const DataFacade<Algorithm> &facade,
BidirectionalIterator packed_path_begin,
BidirectionalIterator packed_path_end,
UnpackingCache &unpacking_cache)
PathAnnotation calculateEBGNodeAnnotations(const DataFacade<Algorithm> &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<std::tuple<NodeID, NodeID, bool>> recursion_stack;
std::stack<EdgeDuration> duration_stack;
std::stack<EdgeDistance> 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;
Expand All @@ -326,9 +328,10 @@ EdgeDuration calculateEBGNodeAnnotations(const DataFacade<Algorithm> &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
{
Expand Down Expand Up @@ -369,14 +372,17 @@ EdgeDuration calculateEBGNodeAnnotations(const DataFacade<Algorithm> &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));
}
}
}
Expand All @@ -391,9 +397,19 @@ EdgeDuration calculateEBGNodeAnnotations(const DataFacade<Algorithm> &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));
}
}

Expand All @@ -403,7 +419,15 @@ EdgeDuration calculateEBGNodeAnnotations(const DataFacade<Algorithm> &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 <typename RandomIter, typename FacadeT>
Expand Down
17 changes: 10 additions & 7 deletions include/engine/unpacking_cache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ namespace osrm
{
namespace engine
{
using PathAnnotation = std::pair<EdgeDuration, EdgeDistance>;
class UnpackingCache
{
private:
boost::compute::detail::lru_cache<std::tuple<NodeID, NodeID, std::size_t>, EdgeDuration> cache;
boost::compute::detail::lru_cache<std::tuple<NodeID, NodeID, std::size_t>, 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)
{
Expand All @@ -33,15 +35,16 @@ class UnpackingCache
return cache.contains(edge);
}

void AddEdge(std::tuple<NodeID, NodeID, std::size_t> edge, EdgeDuration duration)
void AddEdge(std::tuple<NodeID, NodeID, std::size_t> edge, PathAnnotation annotation)
{
cache.insert(edge, duration);
cache.insert(edge, annotation);
}

EdgeDuration GetDuration(std::tuple<NodeID, NodeID, std::size_t> edge)
PathAnnotation GetAnnotation(std::tuple<NodeID, NodeID, std::size_t> edge)
{
boost::optional<EdgeDuration> duration = cache.get(edge);
return *duration ? *duration : MAXIMAL_EDGE_DURATION;
boost::optional<PathAnnotation> annotation = cache.get(edge);
return annotation ? *annotation
: std::make_pair(MAXIMAL_EDGE_DURATION, MAXIMAL_EDGE_DISTANCE);
}
};
} // engine
Expand Down
2 changes: 2 additions & 0 deletions include/util/typedefs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -113,6 +114,7 @@ static const SegmentDuration MAX_SEGMENT_DURATION = INVALID_SEGMENT_DURATION - 1
static const EdgeWeight INVALID_EDGE_WEIGHT = std::numeric_limits<EdgeWeight>::max();
static const EdgeDuration MAXIMAL_EDGE_DURATION = std::numeric_limits<EdgeDuration>::max();
static const TurnPenalty INVALID_TURN_PENALTY = std::numeric_limits<TurnPenalty>::max();
static const EdgeDistance MAXIMAL_EDGE_DISTANCE = std::numeric_limits<EdgeDistance>::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
Expand Down
15 changes: 13 additions & 2 deletions src/engine/routing_algorithms/many_to_many_ch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ std::vector<EdgeDuration> manyToManySearch(SearchEngineData<ch::Algorithm> &engi

std::vector<EdgeWeight> weights_table(number_of_entries, INVALID_EDGE_WEIGHT);
std::vector<EdgeDuration> durations_table(number_of_entries, MAXIMAL_EDGE_DURATION);
std::vector<EdgeDistance> distance_table(number_of_entries, MAXIMAL_EDGE_DISTANCE);
std::vector<NodeID> middle_nodes_table(number_of_entries, SPECIAL_NODEID);

std::vector<NodeBucket> search_space_with_buckets;
Expand Down Expand Up @@ -289,7 +290,10 @@ std::vector<EdgeDuration> manyToManySearch(SearchEngineData<ch::Algorithm> &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;
}

Expand Down Expand Up @@ -317,12 +321,15 @@ std::vector<EdgeDuration> manyToManySearch(SearchEngineData<ch::Algorithm> &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())
Expand Down Expand Up @@ -364,6 +371,10 @@ std::vector<EdgeDuration> manyToManySearch(SearchEngineData<ch::Algorithm> &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;
}
Expand Down

0 comments on commit b4750b1

Please sign in to comment.