Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug-fix: invalid contract node #7066

Merged
merged 15 commits into from
Apr 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/contractor/contractor_search.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace osrm::contractor

void search(ContractorHeap &heap,
const ContractorGraph &graph,
const std::vector<bool> &contractable,
const unsigned number_of_targets,
const int node_limit,
const EdgeWeight weight_limit,
Expand Down
2 changes: 1 addition & 1 deletion include/util/xor_fast_hash.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ template <std::size_t MaxNumElements = (1u << 16u)> class XORFastHash
public:
XORFastHash()
{
std::mt19937 generator; // impl. defined but deterministic default seed
std::mt19937 generator(1); // impl. defined but deterministic default seed

std::iota(begin(table1), end(table1), 0u);
std::shuffle(begin(table1), end(table1), generator);
Expand Down
8 changes: 7 additions & 1 deletion src/contractor/contractor_search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace
{
void relaxNode(ContractorHeap &heap,
const ContractorGraph &graph,
const std::vector<bool> &contractable,
const NodeID node,
const EdgeWeight node_weight,
const NodeID forbidden_node)
Expand All @@ -34,6 +35,10 @@ void relaxNode(ContractorHeap &heap,
// New Node discovered -> Add to Heap + Node Info Storage
if (!toHeapNode)
{
if (!contractable[to])
{
continue;
}
heap.Insert(to, to_weight, ContractorHeapData{current_hop, false});
}
// Found a shorter Path -> Update weight
Expand All @@ -49,6 +54,7 @@ void relaxNode(ContractorHeap &heap,

void search(ContractorHeap &heap,
const ContractorGraph &graph,
const std::vector<bool> &contractable,
const unsigned number_of_targets,
const int node_limit,
const EdgeWeight weight_limit,
Expand Down Expand Up @@ -80,7 +86,7 @@ void search(ContractorHeap &heap,
}
}

relaxNode(heap, graph, node, node_weight, forbidden_node);
relaxNode(heap, graph, contractable, node, node_weight, forbidden_node);
}
}
} // namespace osrm::contractor
60 changes: 39 additions & 21 deletions src/contractor/graph_contractor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ void ContractNode(ContractorThreadData *data,
const ContractorGraph &graph,
const NodeID node,
std::vector<EdgeWeight> &node_weights,
const std::vector<bool> &contractable,
ContractionStats *stats = nullptr)
{
auto &heap = data->heap;
Expand Down Expand Up @@ -245,12 +246,24 @@ void ContractNode(ContractorThreadData *data,
if (RUNSIMULATION)
{
const int constexpr SIMULATION_SEARCH_SPACE_SIZE = 1000;
search(heap, graph, number_of_targets, SIMULATION_SEARCH_SPACE_SIZE, max_weight, node);
search(heap,
graph,
contractable,
number_of_targets,
SIMULATION_SEARCH_SPACE_SIZE,
max_weight,
node);
}
else
{
const int constexpr FULL_SEARCH_SPACE_SIZE = 2000;
search(heap, graph, number_of_targets, FULL_SEARCH_SPACE_SIZE, max_weight, node);
search(heap,
graph,
contractable,
number_of_targets,
FULL_SEARCH_SPACE_SIZE,
max_weight,
node);
}
for (auto out_edge : graph.GetAdjacentEdgeRange(node))
{
Expand Down Expand Up @@ -344,18 +357,20 @@ void ContractNode(ContractorThreadData *data,
void ContractNode(ContractorThreadData *data,
const ContractorGraph &graph,
const NodeID node,
std::vector<EdgeWeight> &node_weights)
std::vector<EdgeWeight> &node_weights,
const std::vector<bool> &contractable)
{
ContractNode<false>(data, graph, node, node_weights, nullptr);
ContractNode<false>(data, graph, node, node_weights, contractable, nullptr);
}

ContractionStats SimulateNodeContraction(ContractorThreadData *data,
const ContractorGraph &graph,
const NodeID node,
std::vector<EdgeWeight> &node_weights)
std::vector<EdgeWeight> &node_weights,
const std::vector<bool> &contractable)
{
ContractionStats stats;
ContractNode<true>(data, graph, node, node_weights, &stats);
ContractNode<true>(data, graph, node, node_weights, contractable, &stats);
return stats;
}

Expand Down Expand Up @@ -487,7 +502,8 @@ bool UpdateNodeNeighbours(ContractorNodeData &node_data,
if (node_data.contractable[u])
{
node_data.priorities[u] = EvaluateNodePriority(
SimulateNodeContraction(data, graph, u, node_data.weights), node_data.depths[u]);
SimulateNodeContraction(data, graph, u, node_data.weights, node_data.contractable),
node_data.depths[u]);
}
}
return true;
Expand Down Expand Up @@ -618,19 +634,21 @@ std::vector<bool> contractGraph(ContractorGraph &graph,
{
util::UnbufferedLog log;
log << "initializing node priorities...";
tbb::parallel_for(tbb::blocked_range<std::size_t>(0, remaining_nodes.size(), PQGrainSize),
[&](const auto &range)
{
ContractorThreadData *data = thread_data_list.GetThreadData();
for (auto x = range.begin(), end = range.end(); x != end; ++x)
{
auto node = remaining_nodes[x].id;
BOOST_ASSERT(node_data.contractable[node]);
node_data.priorities[node] = EvaluateNodePriority(
SimulateNodeContraction(data, graph, node, node_data.weights),
node_data.depths[node]);
}
});
tbb::parallel_for(
tbb::blocked_range<std::size_t>(0, remaining_nodes.size(), PQGrainSize),
[&](const auto &range)
{
ContractorThreadData *data = thread_data_list.GetThreadData();
for (auto x = range.begin(), end = range.end(); x != end; ++x)
{
auto node = remaining_nodes[x].id;
BOOST_ASSERT(node_data.contractable[node]);
node_data.priorities[node] = EvaluateNodePriority(
SimulateNodeContraction(
data, graph, node, node_data.weights, node_data.contractable),
node_data.depths[node]);
}
});
log << " ok.";
}

Expand Down Expand Up @@ -688,7 +706,7 @@ std::vector<bool> contractGraph(ContractorGraph &graph,
for (auto position = range.begin(), end = range.end(); position != end; ++position)
{
const NodeID node = remaining_nodes[position].id;
ContractNode(data, graph, node, node_data.weights);
ContractNode(data, graph, node, node_data.weights, node_data.contractable);
}
});

Expand Down
81 changes: 81 additions & 0 deletions unit_tests/contractor/graph_contractor.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include "contractor/graph_contractor.hpp"
#include <contractor/contract_excludable_graph.hpp>

#include "../common/range_tools.hpp"
#include "helper.hpp"

#include <boost/test/unit_test.hpp>

#include <tbb/global_control.h>

using namespace osrm;
Expand All @@ -12,6 +14,85 @@ using namespace osrm::unit_test;

BOOST_AUTO_TEST_SUITE(graph_contractor)

BOOST_AUTO_TEST_CASE(contract_exclude_graph)
{
tbb::global_control scheduler(tbb::global_control::max_allowed_parallelism, 1);
/* Edge 0 is labeled with toll,
* no edge will be contracted
*
* toll
* (0) <--1--< (1)
* v v
* | \
* 1 2
* | \
* > (3) <--2--< (2) <
*/
std::vector edges = {TestEdge{1, 0, 1},
TestEdge{0, 3, 1},

TestEdge{1, 2, 2},
TestEdge{2, 3, 2}};
auto reference_graph = makeGraph(edges);

auto contracted_graph = reference_graph;

QueryGraph query_graph;
std::vector<std::vector<bool>> edge_filters;
std::tie(query_graph, edge_filters) =
contractExcludableGraph(contracted_graph,
{{1}, {1}, {1}, {1}},
{{true, true, true, true}, {false, true, true, true}});
REQUIRE_SIZE_RANGE(query_graph.GetAdjacentEdgeRange(0), 0);
BOOST_CHECK(query_graph.FindEdge(0, 1) == SPECIAL_EDGEID);
BOOST_CHECK(query_graph.FindEdge(0, 3) == SPECIAL_EDGEID);
REQUIRE_SIZE_RANGE(query_graph.GetAdjacentEdgeRange(1), 2);
BOOST_CHECK(query_graph.FindEdge(1, 0) != SPECIAL_EDGEID);
BOOST_CHECK(query_graph.FindEdge(1, 2) != SPECIAL_EDGEID);
REQUIRE_SIZE_RANGE(query_graph.GetAdjacentEdgeRange(2), 0);
BOOST_CHECK(query_graph.FindEdge(2, 1) == SPECIAL_EDGEID);
BOOST_CHECK(query_graph.FindEdge(2, 3) == SPECIAL_EDGEID);
REQUIRE_SIZE_RANGE(query_graph.GetAdjacentEdgeRange(3), 2);
BOOST_CHECK(query_graph.FindEdge(3, 0) != SPECIAL_EDGEID);
BOOST_CHECK(query_graph.FindEdge(3, 2) != SPECIAL_EDGEID);

auto reference_graph2 = makeGraph(edges);

auto contracted_graph2 = reference_graph2;

/* All edges are normal edges,
* edge 2 will be contracted
*
* Deleted edge_based_edges 1 -> 2, 3 -> 2
*
* (0) <--1--< (1)
* v v
* | \
* 1 2
* | \
* > (3) <--2--< (2) <
*/
QueryGraph query_graph2;
std::vector<std::vector<bool>> edge_filters2;
std::tie(query_graph2, edge_filters2) =
contractExcludableGraph(contracted_graph2,
{{1}, {1}, {1}, {1}},
{{true, true, true, true}, {true, true, true, true}});

REQUIRE_SIZE_RANGE(query_graph2.GetAdjacentEdgeRange(0), 0);
BOOST_CHECK(query_graph2.FindEdge(0, 1) == SPECIAL_EDGEID);
BOOST_CHECK(query_graph2.FindEdge(0, 3) == SPECIAL_EDGEID);
REQUIRE_SIZE_RANGE(query_graph2.GetAdjacentEdgeRange(1), 1);
BOOST_CHECK(query_graph2.FindEdge(1, 0) != SPECIAL_EDGEID);
BOOST_CHECK(query_graph2.FindEdge(1, 2) == SPECIAL_EDGEID);
REQUIRE_SIZE_RANGE(query_graph2.GetAdjacentEdgeRange(2), 2);
BOOST_CHECK(query_graph2.FindEdge(2, 1) != SPECIAL_EDGEID);
BOOST_CHECK(query_graph2.FindEdge(2, 3) != SPECIAL_EDGEID);
REQUIRE_SIZE_RANGE(query_graph2.GetAdjacentEdgeRange(3), 1);
BOOST_CHECK(query_graph2.FindEdge(3, 0) != SPECIAL_EDGEID);
BOOST_CHECK(query_graph2.FindEdge(3, 2) == SPECIAL_EDGEID);
}

BOOST_AUTO_TEST_CASE(contract_graph)
{
tbb::global_control scheduler(tbb::global_control::max_allowed_parallelism, 1);
Expand Down
Loading