Make edge metrics strongly typed (#6421)

This change takes the existing typedefs for weight, duration and
distance, and makes them proper types, using the existing Alias
functionality.

Primarily this is to prevent bugs where the metrics are switched,
but it also adds additional documentation. For example, it now
makes it clear (despite the naming of variables) that most of the
trip algorithm is running on the duration metric.

I've not made any changes to the casts performed between metrics
and numeric types, they now just more explicit.
This commit is contained in:
Michael Bell
2022-10-28 15:16:12 +01:00
committed by GitHub
parent 16685d0de9
commit 5d468f2897
69 changed files with 922 additions and 686 deletions
+15 -12
View File
@@ -74,24 +74,26 @@ unsigned CompressedEdgeContainer::GetZippedPositionForReverseID(const EdgeID edg
return map_iterator->second;
}
SegmentWeight CompressedEdgeContainer::ClipWeight(const SegmentWeight weight)
SegmentWeight CompressedEdgeContainer::ClipWeight(const EdgeWeight weight)
{
if (weight >= INVALID_SEGMENT_WEIGHT)
SegmentWeight seg_weight = alias_cast<SegmentWeight>(weight);
if (seg_weight >= INVALID_SEGMENT_WEIGHT)
{
clipped_weights++;
return MAX_SEGMENT_WEIGHT;
}
return weight;
return seg_weight;
}
SegmentDuration CompressedEdgeContainer::ClipDuration(const SegmentDuration duration)
SegmentDuration CompressedEdgeContainer::ClipDuration(const EdgeDuration duration)
{
if (duration >= INVALID_SEGMENT_DURATION)
SegmentDuration seg_duration = alias_cast<SegmentDuration>(duration);
if (seg_duration >= INVALID_SEGMENT_DURATION)
{
clipped_weights++;
return MAX_SEGMENT_DURATION;
}
return duration;
return seg_duration;
}
// Adds info for a compressed edge to the container. edge_id_2
@@ -119,8 +121,8 @@ void CompressedEdgeContainer::CompressEdge(const EdgeID edge_id_1,
BOOST_ASSERT(SPECIAL_EDGEID != edge_id_2);
BOOST_ASSERT(SPECIAL_NODEID != via_node_id);
BOOST_ASSERT(SPECIAL_NODEID != target_node_id);
BOOST_ASSERT(INVALID_SEGMENT_WEIGHT != weight1);
BOOST_ASSERT(INVALID_SEGMENT_WEIGHT != weight2);
BOOST_ASSERT(INVALID_EDGE_WEIGHT != weight1);
BOOST_ASSERT(INVALID_EDGE_WEIGHT != weight2);
// append list of removed edge_id plus via node to surviving edge id:
// <surv_1, .. , surv_n, via_node_id, rem_1, .. rem_n
@@ -207,13 +209,14 @@ void CompressedEdgeContainer::CompressEdge(const EdgeID edge_id_1,
void CompressedEdgeContainer::AddUncompressedEdge(const EdgeID edge_id,
const NodeID target_node_id,
const SegmentWeight weight,
const SegmentDuration duration)
const EdgeWeight weight,
const EdgeDuration duration)
{
// remove super-trivial geometries
BOOST_ASSERT(SPECIAL_EDGEID != edge_id);
BOOST_ASSERT(SPECIAL_NODEID != target_node_id);
BOOST_ASSERT(INVALID_EDGE_WEIGHT != weight);
BOOST_ASSERT(INVALID_EDGE_DURATION != duration);
// Add via node id. List is created if it does not exist
if (!HasEntryForID(edge_id))
@@ -336,12 +339,12 @@ void CompressedEdgeContainer::PrintStatistics() const
if (clipped_weights > 0)
{
util::Log(logWARNING) << "Clipped " << clipped_weights << " segment weights to "
<< (INVALID_SEGMENT_WEIGHT - 1);
<< MAX_SEGMENT_WEIGHT;
}
if (clipped_durations > 0)
{
util::Log(logWARNING) << "Clipped " << clipped_durations << " segment durations to "
<< (INVALID_SEGMENT_DURATION - 1);
<< MAX_SEGMENT_DURATION;
}
util::Log() << "Geometry successfully removed:"
+14 -12
View File
@@ -94,7 +94,7 @@ void EdgeBasedGraphFactory::GetEdgeBasedNodeWeights(std::vector<EdgeWeight> &out
}
void EdgeBasedGraphFactory::GetEdgeBasedNodeDurations(
std::vector<EdgeWeight> &output_node_durations)
std::vector<EdgeDuration> &output_node_durations)
{
using std::swap; // Koenig swap
swap(m_edge_based_node_durations, output_node_durations);
@@ -147,7 +147,8 @@ NBGToEBG EdgeBasedGraphFactory::InsertEdgeBasedNode(const NodeID node_u, const N
// * in other cases node weights must be masked with 0x7fffffff to clear MSB
if (nbe_to_ebn_mapping[edge_id_1] != SPECIAL_NODEID &&
nbe_to_ebn_mapping[edge_id_2] == SPECIAL_NODEID)
m_edge_based_node_weights[nbe_to_ebn_mapping[edge_id_1]] |= 0x80000000;
m_edge_based_node_weights[nbe_to_ebn_mapping[edge_id_1]] |=
EdgeWeight{static_cast<EdgeWeight::value_type>(0x80000000)};
BOOST_ASSERT(m_compressed_edge_container.HasEntryForID(edge_id_1) ==
m_compressed_edge_container.HasEntryForID(edge_id_2));
@@ -400,7 +401,7 @@ EdgeBasedGraphFactory::GenerateEdgeExpandedNodes(const WayRestrictionMap &way_re
segregated_edges.count(eid) > 0;
const auto ebn_weight = m_edge_based_node_weights[nbe_to_ebn_mapping[eid]];
BOOST_ASSERT((ebn_weight & 0x7fffffff) == edge_data.weight);
BOOST_ASSERT((ebn_weight & EdgeWeight{0x7fffffff}) == edge_data.weight);
m_edge_based_node_weights.push_back(ebn_weight);
m_edge_based_node_durations.push_back(
m_edge_based_node_durations[nbe_to_ebn_mapping[eid]]);
@@ -663,7 +664,7 @@ void EdgeBasedGraphFactory::GenerateEdgeExpandedEdges(
edge_data1.flags.highway_turn_classification,
edge_data1.flags.access_turn_classification,
((double)intersection::findEdgeLength(edge_geometries, node_based_edge_from) /
edge_data1.duration) *
from_alias<double>(edge_data1.duration)) *
36,
edge_data1.flags.road_classification.GetPriority(),
// target info
@@ -675,7 +676,7 @@ void EdgeBasedGraphFactory::GenerateEdgeExpandedEdges(
edge_data2.flags.highway_turn_classification,
edge_data2.flags.access_turn_classification,
((double)intersection::findEdgeLength(edge_geometries, node_based_edge_to) /
edge_data2.duration) *
from_alias<double>(edge_data2.duration)) *
36,
edge_data2.flags.road_classification.GetPriority(),
// connected roads
@@ -686,17 +687,18 @@ void EdgeBasedGraphFactory::GenerateEdgeExpandedEdges(
// turn penalties are limited to [-2^15, 2^15) which roughly translates to 54 minutes
// and fits signed 16bit deci-seconds
auto weight_penalty =
boost::numeric_cast<TurnPenalty>(extracted_turn.weight * weight_multiplier);
auto duration_penalty = boost::numeric_cast<TurnPenalty>(extracted_turn.duration * 10.);
auto weight_penalty = TurnPenalty{boost::numeric_cast<TurnPenalty::value_type>(
extracted_turn.weight * weight_multiplier)};
auto duration_penalty = TurnPenalty{
boost::numeric_cast<TurnPenalty::value_type>(extracted_turn.duration * 10.)};
BOOST_ASSERT(SPECIAL_NODEID != nbe_to_ebn_mapping[node_based_edge_from]);
BOOST_ASSERT(SPECIAL_NODEID != nbe_to_ebn_mapping[node_based_edge_to]);
// auto turn_id = m_edge_based_edge_list.size();
auto weight = boost::numeric_cast<EdgeWeight>(edge_data1.weight + weight_penalty);
auto duration = boost::numeric_cast<EdgeWeight>(edge_data1.duration + duration_penalty);
auto distance = boost::numeric_cast<EdgeDistance>(edge_data1.distance);
auto weight = edge_data1.weight + alias_cast<EdgeWeight>(weight_penalty);
auto duration = edge_data1.duration + alias_cast<EdgeDuration>(duration_penalty);
auto distance = edge_data1.distance;
EdgeBasedEdge edge_based_edge = {edge_based_node_from,
edge_based_node_to,
@@ -860,7 +862,7 @@ void EdgeBasedGraphFactory::GenerateEdgeExpandedEdges(
edge_data.flags.access_turn_classification,
((double)intersection::findEdgeLength(edge_geometries,
connected_edge.eid) /
edge_data.duration) *
from_alias<double>(edge_data.duration)) *
36,
edge_data.flags.road_classification.GetPriority(),
is_incoming,
+7 -7
View File
@@ -712,9 +712,11 @@ void ExtractionContainers::PrepareEdges(ScriptingEnvironment &scripting_environm
scripting_environment.ProcessSegment(segment);
auto &edge = edge_iterator->result;
edge.weight = std::max<EdgeWeight>(1, std::round(segment.weight * weight_multiplier));
edge.duration = std::max<EdgeWeight>(1, std::round(segment.duration * 10.));
edge.distance = static_cast<float>(accurate_distance);
edge.weight = std::max<EdgeWeight>(
{1}, to_alias<EdgeWeight>(std::round(segment.weight * weight_multiplier)));
edge.duration = std::max<EdgeDuration>(
{1}, to_alias<EdgeDuration>(std::round(segment.duration * 10.)));
edge.distance = to_alias<EdgeDistance>(accurate_distance);
// assign new node id
const auto node_id = mapExternalToInternalNodeID(
@@ -779,10 +781,8 @@ void ExtractionContainers::PrepareEdges(ScriptingEnvironment &scripting_environm
NodeID source = all_edges_list[i].result.source;
NodeID target = all_edges_list[i].result.target;
auto min_forward = std::make_pair(std::numeric_limits<EdgeWeight>::max(),
std::numeric_limits<EdgeWeight>::max());
auto min_backward = std::make_pair(std::numeric_limits<EdgeWeight>::max(),
std::numeric_limits<EdgeWeight>::max());
auto min_forward = std::make_pair(MAXIMAL_EDGE_WEIGHT, MAXIMAL_EDGE_DURATION);
auto min_backward = std::make_pair(MAXIMAL_EDGE_WEIGHT, MAXIMAL_EDGE_DURATION);
std::size_t min_forward_idx = std::numeric_limits<std::size_t>::max();
std::size_t min_backward_idx = std::numeric_limits<std::size_t>::max();
+1 -1
View File
@@ -668,7 +668,7 @@ void Extractor::FindComponents(unsigned number_of_edge_based_nodes,
for (const auto &edge : input_edge_list)
{
BOOST_ASSERT_MSG(static_cast<unsigned int>(std::max(edge.data.weight, 1)) > 0,
BOOST_ASSERT_MSG((std::max(edge.data.weight, EdgeWeight{1})) > EdgeWeight{0},
"edge distance < 1");
BOOST_ASSERT(edge.source < number_of_edge_based_nodes);
BOOST_ASSERT(edge.target < number_of_edge_based_nodes);
+8 -8
View File
@@ -425,10 +425,10 @@ void ExtractorCallbacks::ProcessWay(const osmium::Way &input_way, const Extracti
NodeBasedEdgeWithOSM edge = {
OSMNodeID{static_cast<std::uint64_t>(first_node.ref())},
OSMNodeID{static_cast<std::uint64_t>(last_node.ref())},
0, // weight
0, // duration
0, // distance
{}, // geometry id
{0}, // weight
{0}, // duration
{0}, // distance
{}, // geometry id
static_cast<AnnotationID>(annotation_data_id),
{true,
in_backward_direction && !split_edge,
@@ -459,10 +459,10 @@ void ExtractorCallbacks::ProcessWay(const osmium::Way &input_way, const Extracti
NodeBasedEdgeWithOSM edge = {
OSMNodeID{static_cast<std::uint64_t>(first_node.ref())},
OSMNodeID{static_cast<std::uint64_t>(last_node.ref())},
0, // weight
0, // duration
0, // distance
{}, // geometry id
{0}, // weight
{0}, // duration
{0}, // distance
{}, // geometry id
static_cast<AnnotationID>(annotation_data_id),
{false,
true,
+18 -16
View File
@@ -252,16 +252,18 @@ void GraphCompressor::Compress(const std::unordered_set<NodeID> &barrier_nodes,
roads_on_the_left);
scripting_environment.ProcessTurn(extraction_turn);
auto update_direction_penalty =
[&extraction_turn, weight_multiplier](bool signal,
EdgeDuration &duration_penalty,
EdgeWeight &weight_penalty) {
if (signal)
{
duration_penalty = extraction_turn.duration * SECOND_TO_DECISECOND;
weight_penalty = extraction_turn.weight * weight_multiplier;
}
};
auto update_direction_penalty = [&extraction_turn, weight_multiplier](
bool signal,
EdgeDuration &duration_penalty,
EdgeWeight &weight_penalty) {
if (signal)
{
duration_penalty = to_alias<EdgeDuration>(extraction_turn.duration *
SECOND_TO_DECISECOND);
weight_penalty =
to_alias<EdgeWeight>(extraction_turn.weight * weight_multiplier);
}
};
update_direction_penalty(has_forward_signal,
forward_node_duration_penalty,
@@ -277,8 +279,8 @@ void GraphCompressor::Compress(const std::unordered_set<NodeID> &barrier_nodes,
const auto forward_duration1 = fwd_edge_data1.duration;
const auto forward_duration2 = fwd_edge_data2.duration;
BOOST_ASSERT(0 != forward_weight1);
BOOST_ASSERT(0 != forward_weight2);
BOOST_ASSERT(EdgeWeight{0} != forward_weight1);
BOOST_ASSERT(EdgeWeight{0} != forward_weight2);
const auto reverse_weight1 = rev_edge_data1.weight;
const auto reverse_weight2 = rev_edge_data2.weight;
@@ -297,8 +299,8 @@ void GraphCompressor::Compress(const std::unordered_set<NodeID> &barrier_nodes,
BOOST_ASSERT(forward_distance2 == reverse_distance1);
#endif
BOOST_ASSERT(0 != reverse_weight1);
BOOST_ASSERT(0 != reverse_weight2);
BOOST_ASSERT(EdgeWeight{0} != reverse_weight1);
BOOST_ASSERT(EdgeWeight{0} != reverse_weight2);
auto apply_e2_to_e1 = [&graph](EdgeID edge1,
EdgeID edge2,
@@ -346,8 +348,8 @@ void GraphCompressor::Compress(const std::unordered_set<NodeID> &barrier_nodes,
if (weight_penalty == INVALID_EDGE_WEIGHT &&
other_weight_penalty != INVALID_EDGE_WEIGHT)
{
weight_penalty = 0;
duration_penalty = 0;
weight_penalty = {0};
duration_penalty = {0};
}
};
set_dummy_penalty(forward_node_weight_penalty,
+1 -1
View File
@@ -549,7 +549,7 @@ void Sol2ScriptingEnvironment::InitContext(LuaScriptingContext &context)
"precision",
COORDINATE_PRECISION,
"max_turn_weight",
std::numeric_limits<TurnPenalty>::max());
std::numeric_limits<TurnPenalty::value_type>::max());
// call initialize function
sol::function setup_function = function_table.value()["setup"];