2017-03-06 14:16:24 -05:00
|
|
|
#ifndef OSRM_ENGINE_ROUTING_BASE_MLD_HPP
|
|
|
|
#define OSRM_ENGINE_ROUTING_BASE_MLD_HPP
|
|
|
|
|
|
|
|
#include "engine/algorithm.hpp"
|
|
|
|
#include "engine/datafacade/contiguous_internalmem_datafacade.hpp"
|
|
|
|
#include "engine/routing_algorithms/routing_base.hpp"
|
|
|
|
#include "engine/search_engine_data.hpp"
|
|
|
|
|
|
|
|
#include "util/typedefs.hpp"
|
|
|
|
|
|
|
|
#include <boost/assert.hpp>
|
|
|
|
|
|
|
|
namespace osrm
|
|
|
|
{
|
|
|
|
namespace engine
|
|
|
|
{
|
|
|
|
namespace routing_algorithms
|
|
|
|
{
|
|
|
|
namespace mld
|
|
|
|
{
|
|
|
|
|
2017-03-15 18:16:02 -04:00
|
|
|
namespace
|
|
|
|
{
|
2017-03-16 02:52:17 -04:00
|
|
|
// Unrestricted search (Args is const PhantomNodes &):
|
|
|
|
// * use partition.GetQueryLevel to find the node query level based on source and target phantoms
|
2017-03-15 18:16:02 -04:00
|
|
|
// * allow to traverse all cells
|
2017-03-16 02:52:17 -04:00
|
|
|
LevelID getNodeQureyLevel(const partition::MultiLevelPartitionView &partition,
|
|
|
|
NodeID node,
|
|
|
|
const PhantomNodes &phantom_nodes)
|
|
|
|
{
|
|
|
|
auto level = [&partition, node](const SegmentID &source, const SegmentID &target) {
|
|
|
|
if (source.enabled && target.enabled)
|
|
|
|
return partition.GetQueryLevel(source.id, target.id, node);
|
|
|
|
return INVALID_LEVEL_ID;
|
|
|
|
};
|
|
|
|
return std::min(std::min(level(phantom_nodes.source_phantom.forward_segment_id,
|
|
|
|
phantom_nodes.target_phantom.forward_segment_id),
|
|
|
|
level(phantom_nodes.source_phantom.forward_segment_id,
|
|
|
|
phantom_nodes.target_phantom.reverse_segment_id)),
|
|
|
|
std::min(level(phantom_nodes.source_phantom.reverse_segment_id,
|
|
|
|
phantom_nodes.target_phantom.forward_segment_id),
|
|
|
|
level(phantom_nodes.source_phantom.reverse_segment_id,
|
|
|
|
phantom_nodes.target_phantom.reverse_segment_id)));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool checkParentCellRestriction(CellID, const PhantomNodes &) { return true; }
|
2017-03-15 18:16:02 -04:00
|
|
|
|
|
|
|
// Restricted search (Args is LevelID, CellID):
|
|
|
|
// * use the fixed level for queries
|
|
|
|
// * check if the node cell is the same as the specified parent onr
|
2017-03-16 02:52:17 -04:00
|
|
|
LevelID getNodeQureyLevel(const partition::MultiLevelPartitionView &, NodeID, LevelID level, CellID)
|
|
|
|
{
|
|
|
|
return level;
|
|
|
|
}
|
|
|
|
|
2017-03-15 18:16:02 -04:00
|
|
|
bool checkParentCellRestriction(CellID cell, LevelID, CellID parent) { return cell == parent; }
|
|
|
|
}
|
|
|
|
|
|
|
|
template <bool DIRECTION, typename... Args>
|
2017-03-31 06:52:04 -04:00
|
|
|
void routingStep(const datafacade::ContiguousInternalMemoryDataFacade<Algorithm> &facade,
|
2017-04-03 13:15:58 -04:00
|
|
|
SearchEngineData<Algorithm>::QueryHeap &forward_heap,
|
|
|
|
SearchEngineData<Algorithm>::QueryHeap &reverse_heap,
|
2017-03-06 14:16:24 -05:00
|
|
|
NodeID &middle_node,
|
2017-03-15 18:16:02 -04:00
|
|
|
EdgeWeight &path_upper_bound,
|
|
|
|
Args... args)
|
2017-03-06 14:16:24 -05:00
|
|
|
{
|
2017-03-15 08:33:43 -04:00
|
|
|
const auto &partition = facade.GetMultiLevelPartition();
|
|
|
|
const auto &cells = facade.GetCellStorage();
|
|
|
|
|
2017-03-06 14:16:24 -05:00
|
|
|
const auto node = forward_heap.DeleteMin();
|
|
|
|
const auto weight = forward_heap.GetKey(node);
|
|
|
|
|
2017-03-14 11:04:15 -04:00
|
|
|
// Upper bound for the path source -> target with
|
|
|
|
// weight(source -> node) = weight weight(to -> target) ≤ reverse_weight
|
|
|
|
// 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
|
|
|
|
if (reverse_heap.WasInserted(node))
|
|
|
|
{
|
|
|
|
auto reverse_weight = reverse_heap.GetKey(node);
|
|
|
|
auto path_weight = weight + reverse_weight;
|
|
|
|
if (path_weight >= 0 && path_weight < path_upper_bound)
|
2017-03-06 14:16:24 -05:00
|
|
|
{
|
2017-03-14 11:04:15 -04:00
|
|
|
middle_node = node;
|
|
|
|
path_upper_bound = path_weight;
|
2017-03-06 14:16:24 -05:00
|
|
|
}
|
2017-03-14 11:04:15 -04:00
|
|
|
}
|
2017-03-06 14:16:24 -05:00
|
|
|
|
2017-03-16 02:52:17 -04:00
|
|
|
const auto level = getNodeQureyLevel(partition, node, args...);
|
2017-03-06 14:16:24 -05:00
|
|
|
|
2017-03-15 09:36:41 -04:00
|
|
|
if (level >= 1 && !forward_heap.GetData(node).from_clique_arc)
|
2017-03-06 14:16:24 -05:00
|
|
|
{
|
|
|
|
if (DIRECTION == FORWARD_DIRECTION)
|
|
|
|
{
|
|
|
|
// Shortcuts in forward direction
|
|
|
|
const auto &cell = cells.GetCell(level, partition.GetCell(level, node));
|
|
|
|
auto destination = cell.GetDestinationNodes().begin();
|
|
|
|
for (auto shortcut_weight : cell.GetOutWeight(node))
|
|
|
|
{
|
|
|
|
BOOST_ASSERT(destination != cell.GetDestinationNodes().end());
|
|
|
|
const NodeID to = *destination;
|
|
|
|
if (shortcut_weight != INVALID_EDGE_WEIGHT && node != to)
|
|
|
|
{
|
|
|
|
const EdgeWeight to_weight = weight + shortcut_weight;
|
|
|
|
if (!forward_heap.WasInserted(to))
|
|
|
|
{
|
2017-03-15 09:36:41 -04:00
|
|
|
forward_heap.Insert(to, to_weight, {node, true});
|
2017-03-06 14:16:24 -05:00
|
|
|
}
|
|
|
|
else if (to_weight < forward_heap.GetKey(to))
|
|
|
|
{
|
2017-03-15 09:36:41 -04:00
|
|
|
forward_heap.GetData(to) = {node, true};
|
2017-03-06 14:16:24 -05:00
|
|
|
forward_heap.DecreaseKey(to, to_weight);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
++destination;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Shortcuts in backward direction
|
|
|
|
const auto &cell = cells.GetCell(level, partition.GetCell(level, node));
|
|
|
|
auto source = cell.GetSourceNodes().begin();
|
|
|
|
for (auto shortcut_weight : cell.GetInWeight(node))
|
|
|
|
{
|
|
|
|
BOOST_ASSERT(source != cell.GetSourceNodes().end());
|
|
|
|
const NodeID to = *source;
|
|
|
|
if (shortcut_weight != INVALID_EDGE_WEIGHT && node != to)
|
|
|
|
{
|
|
|
|
const EdgeWeight to_weight = weight + shortcut_weight;
|
|
|
|
if (!forward_heap.WasInserted(to))
|
|
|
|
{
|
2017-03-15 09:36:41 -04:00
|
|
|
forward_heap.Insert(to, to_weight, {node, true});
|
2017-03-06 14:16:24 -05:00
|
|
|
}
|
|
|
|
else if (to_weight < forward_heap.GetKey(to))
|
|
|
|
{
|
2017-03-15 09:36:41 -04:00
|
|
|
forward_heap.GetData(to) = {node, true};
|
2017-03-06 14:16:24 -05:00
|
|
|
forward_heap.DecreaseKey(to, to_weight);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
++source;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Boundary edges
|
2017-03-06 22:59:28 -05:00
|
|
|
for (const auto edge : facade.GetBorderEdgeRange(level, node))
|
2017-03-06 14:16:24 -05:00
|
|
|
{
|
|
|
|
const auto &edge_data = facade.GetEdgeData(edge);
|
|
|
|
if (DIRECTION == FORWARD_DIRECTION ? edge_data.forward : edge_data.backward)
|
|
|
|
{
|
|
|
|
const NodeID to = facade.GetTarget(edge);
|
|
|
|
|
2017-03-06 22:59:28 -05:00
|
|
|
if (checkParentCellRestriction(partition.GetCell(level + 1, to), args...))
|
2017-03-06 14:16:24 -05:00
|
|
|
{
|
|
|
|
BOOST_ASSERT_MSG(edge_data.weight > 0, "edge_weight invalid");
|
|
|
|
const EdgeWeight to_weight = weight + edge_data.weight;
|
|
|
|
|
|
|
|
if (!forward_heap.WasInserted(to))
|
|
|
|
{
|
2017-03-15 09:36:41 -04:00
|
|
|
forward_heap.Insert(to, to_weight, {node, false});
|
2017-03-06 14:16:24 -05:00
|
|
|
}
|
|
|
|
else if (to_weight < forward_heap.GetKey(to))
|
|
|
|
{
|
2017-03-15 09:36:41 -04:00
|
|
|
forward_heap.GetData(to) = {node, false};
|
2017-03-06 14:16:24 -05:00
|
|
|
forward_heap.DecreaseKey(to, to_weight);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-15 18:16:02 -04:00
|
|
|
template <typename... Args>
|
2017-03-16 02:52:17 -04:00
|
|
|
std::tuple<EdgeWeight, NodeID, NodeID, std::vector<EdgeID>>
|
2017-03-31 06:52:04 -04:00
|
|
|
search(const datafacade::ContiguousInternalMemoryDataFacade<Algorithm> &facade,
|
2017-04-03 13:15:58 -04:00
|
|
|
SearchEngineData<Algorithm>::QueryHeap &forward_heap,
|
|
|
|
SearchEngineData<Algorithm>::QueryHeap &reverse_heap,
|
2017-03-16 02:52:17 -04:00
|
|
|
Args... args)
|
2017-03-06 14:16:24 -05:00
|
|
|
{
|
2017-03-14 11:04:15 -04:00
|
|
|
|
2017-03-15 08:33:43 -04:00
|
|
|
const auto &partition = facade.GetMultiLevelPartition();
|
|
|
|
|
2017-03-14 11:04:15 -04:00
|
|
|
BOOST_ASSERT(!forward_heap.Empty() && forward_heap.MinKey() < INVALID_EDGE_WEIGHT);
|
|
|
|
BOOST_ASSERT(!reverse_heap.Empty() && reverse_heap.MinKey() < INVALID_EDGE_WEIGHT);
|
|
|
|
|
2017-03-06 14:16:24 -05:00
|
|
|
// run two-Target Dijkstra routing step.
|
|
|
|
NodeID middle = SPECIAL_NODEID;
|
|
|
|
EdgeWeight weight = INVALID_EDGE_WEIGHT;
|
2017-03-14 11:04:15 -04:00
|
|
|
EdgeWeight forward_heap_min = forward_heap.MinKey();
|
|
|
|
EdgeWeight reverse_heap_min = reverse_heap.MinKey();
|
|
|
|
while (forward_heap.Size() + reverse_heap.Size() > 0 &&
|
|
|
|
forward_heap_min + reverse_heap_min < weight)
|
2017-03-06 14:16:24 -05:00
|
|
|
{
|
2017-03-14 11:04:15 -04:00
|
|
|
if (!forward_heap.Empty())
|
2017-03-06 14:16:24 -05:00
|
|
|
{
|
2017-03-14 11:04:15 -04:00
|
|
|
routingStep<FORWARD_DIRECTION>(
|
2017-03-15 18:16:02 -04:00
|
|
|
facade, forward_heap, reverse_heap, middle, weight, args...);
|
2017-03-14 11:04:15 -04:00
|
|
|
if (!forward_heap.Empty())
|
|
|
|
forward_heap_min = forward_heap.MinKey();
|
2017-03-06 14:16:24 -05:00
|
|
|
}
|
2017-03-14 11:04:15 -04:00
|
|
|
if (!reverse_heap.Empty())
|
2017-03-06 14:16:24 -05:00
|
|
|
{
|
2017-03-14 11:04:15 -04:00
|
|
|
routingStep<REVERSE_DIRECTION>(
|
2017-03-15 18:16:02 -04:00
|
|
|
facade, reverse_heap, forward_heap, middle, weight, args...);
|
2017-03-14 11:04:15 -04:00
|
|
|
if (!reverse_heap.Empty())
|
|
|
|
reverse_heap_min = reverse_heap.MinKey();
|
2017-03-06 14:16:24 -05:00
|
|
|
}
|
2017-03-14 11:04:15 -04:00
|
|
|
};
|
2017-03-06 14:16:24 -05:00
|
|
|
|
|
|
|
// No path found for both target nodes?
|
|
|
|
if (weight == INVALID_EDGE_WEIGHT || SPECIAL_NODEID == middle)
|
|
|
|
{
|
|
|
|
return std::make_tuple(
|
|
|
|
INVALID_EDGE_WEIGHT, SPECIAL_NODEID, SPECIAL_NODEID, std::vector<EdgeID>());
|
|
|
|
}
|
|
|
|
|
2017-03-15 08:33:43 -04:00
|
|
|
// Get packed path as edges {from node ID, to node ID, edge ID}
|
2017-03-15 09:36:41 -04:00
|
|
|
std::vector<std::tuple<NodeID, NodeID, bool>> packed_path;
|
2017-03-06 14:16:24 -05:00
|
|
|
NodeID current_node = middle, parent_node = forward_heap.GetData(middle).parent;
|
|
|
|
while (parent_node != current_node)
|
|
|
|
{
|
|
|
|
const auto &data = forward_heap.GetData(current_node);
|
2017-03-15 09:36:41 -04:00
|
|
|
packed_path.push_back(std::make_tuple(parent_node, current_node, data.from_clique_arc));
|
2017-03-06 14:16:24 -05:00
|
|
|
current_node = parent_node;
|
|
|
|
parent_node = forward_heap.GetData(parent_node).parent;
|
|
|
|
}
|
|
|
|
std::reverse(std::begin(packed_path), std::end(packed_path));
|
|
|
|
const NodeID source_node = current_node;
|
|
|
|
|
|
|
|
current_node = middle, parent_node = reverse_heap.GetData(middle).parent;
|
|
|
|
while (parent_node != current_node)
|
|
|
|
{
|
|
|
|
const auto &data = reverse_heap.GetData(current_node);
|
2017-03-15 09:36:41 -04:00
|
|
|
packed_path.push_back(std::make_tuple(current_node, parent_node, data.from_clique_arc));
|
2017-03-06 14:16:24 -05:00
|
|
|
current_node = parent_node;
|
|
|
|
parent_node = reverse_heap.GetData(parent_node).parent;
|
|
|
|
}
|
|
|
|
const NodeID target_node = current_node;
|
|
|
|
|
|
|
|
// Unpack path
|
|
|
|
std::vector<EdgeID> unpacked_path;
|
|
|
|
unpacked_path.reserve(packed_path.size());
|
2017-03-15 18:16:02 -04:00
|
|
|
for (auto const &packed_edge : packed_path)
|
2017-03-06 14:16:24 -05:00
|
|
|
{
|
|
|
|
NodeID source, target;
|
2017-03-15 09:36:41 -04:00
|
|
|
bool overlay_edge;
|
|
|
|
std::tie(source, target, overlay_edge) = packed_edge;
|
|
|
|
if (!overlay_edge)
|
2017-03-06 14:16:24 -05:00
|
|
|
{ // a base graph edge
|
2017-03-15 09:36:41 -04:00
|
|
|
unpacked_path.push_back(facade.FindEdge(source, target));
|
2017-03-06 14:16:24 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{ // an overlay graph edge
|
2017-03-16 02:52:17 -04:00
|
|
|
LevelID level = getNodeQureyLevel(partition, source, args...);
|
2017-03-06 14:16:24 -05:00
|
|
|
CellID parent_cell_id = partition.GetCell(level, source);
|
|
|
|
BOOST_ASSERT(parent_cell_id == partition.GetCell(level, target));
|
|
|
|
|
2017-03-15 08:33:43 -04:00
|
|
|
LevelID sublevel = level - 1;
|
|
|
|
|
2017-03-06 14:16:24 -05:00
|
|
|
// Here heaps can be reused, let's go deeper!
|
|
|
|
forward_heap.Clear();
|
|
|
|
reverse_heap.Clear();
|
2017-03-15 08:33:43 -04:00
|
|
|
forward_heap.Insert(source, 0, {source});
|
|
|
|
reverse_heap.Insert(target, 0, {target});
|
2017-03-06 14:16:24 -05:00
|
|
|
|
|
|
|
// TODO: when structured bindings will be allowed change to
|
|
|
|
// auto [subpath_weight, subpath_source, subpath_target, subpath] = ...
|
|
|
|
EdgeWeight subpath_weight;
|
|
|
|
NodeID subpath_source, subpath_target;
|
|
|
|
std::vector<EdgeID> subpath;
|
2017-03-15 18:16:02 -04:00
|
|
|
std::tie(subpath_weight, subpath_source, subpath_target, subpath) =
|
|
|
|
search(facade, forward_heap, reverse_heap, sublevel, parent_cell_id);
|
2017-03-06 14:16:24 -05:00
|
|
|
BOOST_ASSERT(!subpath.empty());
|
|
|
|
BOOST_ASSERT(subpath_source == source);
|
|
|
|
BOOST_ASSERT(subpath_target == target);
|
|
|
|
unpacked_path.insert(unpacked_path.end(), subpath.begin(), subpath.end());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::make_tuple(weight, source_node, target_node, std::move(unpacked_path));
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mld
|
|
|
|
} // namespace routing_algorithms
|
|
|
|
} // namespace engine
|
|
|
|
} // namespace osrm
|
|
|
|
|
|
|
|
#endif // OSRM_ENGINE_ROUTING_BASE_MLD_HPP
|