Add tests for avoid flags

This commit is contained in:
Patrick Niklaus 2017-07-28 09:49:49 +00:00 committed by Patrick Niklaus
parent 303a8fae32
commit 84fd38ac9c
6 changed files with 266 additions and 32 deletions

View File

@ -31,8 +31,13 @@ class CellCustomizer
CellCustomizer(const partition::MultiLevelPartition &partition) : partition(partition) {}
template <typename GraphT>
void Customize(
const GraphT &graph, Heap &heap, const partition::CellStorage &cells, CellMetric &metric, LevelID level, CellID id)
void Customize(const GraphT &graph,
Heap &heap,
const partition::CellStorage &cells,
const std::vector<bool> &allowed_nodes,
CellMetric &metric,
LevelID level,
CellID id)
{
auto cell = cells.GetCell(metric, level, id);
auto destinations = cell.GetDestinationNodes();
@ -40,7 +45,19 @@ class CellCustomizer
// for each source do forward search
for (auto source : cell.GetSourceNodes())
{
std::unordered_set<NodeID> destinations_set(destinations.begin(), destinations.end());
if (!allowed_nodes[source])
{
continue;
}
std::unordered_set<NodeID> destinations_set;
for (const auto destination : destinations)
{
if (allowed_nodes[destination])
{
destinations_set.insert(destination);
}
}
heap.Clear();
heap.Insert(source, 0, {false, 0});
@ -52,9 +69,11 @@ class CellCustomizer
const EdgeDuration duration = heap.GetData(node).duration;
if (level == 1)
RelaxNode<true>(graph, cells, metric, heap, level, node, weight, duration);
RelaxNode<true>(
graph, cells, allowed_nodes, metric, heap, level, node, weight, duration);
else
RelaxNode<false>(graph, cells, metric, heap, level, node, weight, duration);
RelaxNode<false>(
graph, cells, allowed_nodes, metric, heap, level, node, weight, duration);
destinations_set.erase(node);
}
@ -80,7 +99,11 @@ class CellCustomizer
}
}
template <typename GraphT> void Customize(const GraphT &graph, const partition::CellStorage &cells, CellMetric &metric)
template <typename GraphT>
void Customize(const GraphT &graph,
const partition::CellStorage &cells,
const std::vector<bool> &allowed_nodes,
CellMetric &metric)
{
Heap heap_exemplar(graph.GetNumberOfNodes());
HeapPtr heaps(heap_exemplar);
@ -92,7 +115,8 @@ class CellCustomizer
auto &heap = heaps.local();
for (auto id = range.begin(), end = range.end(); id != end; ++id)
{
Customize(graph, heap, cells, metric, level, id);
Customize(
graph, heap, cells, allowed_nodes, metric, level, id);
}
});
}
@ -102,6 +126,7 @@ class CellCustomizer
template <bool first_level, typename GraphT>
void RelaxNode(const GraphT &graph,
const partition::CellStorage &cells,
const std::vector<bool> &allowed_nodes,
const CellMetric &metric,
Heap &heap,
LevelID level,
@ -132,6 +157,11 @@ class CellCustomizer
if (subcell_weight != INVALID_EDGE_WEIGHT)
{
const NodeID to = *subcell_destination;
if (!allowed_nodes[to])
{
continue;
}
const EdgeWeight to_weight = weight + subcell_weight;
if (!heap.WasInserted(to))
{
@ -154,6 +184,11 @@ class CellCustomizer
for (auto edge : graph.GetInternalEdgeRange(level, node))
{
const NodeID to = graph.GetTarget(edge);
if (!allowed_nodes[to])
{
continue;
}
const auto &data = graph.GetEdgeData(edge);
if (data.forward &&
(first_level ||

View File

@ -17,7 +17,7 @@ namespace customizer
struct CustomizationConfig final : storage::IOConfig
{
CustomizationConfig()
: IOConfig({".osrm.ebg", ".osrm.partition", ".osrm.cells"},
: IOConfig({".osrm.ebg", ".osrm.partition", ".osrm.cells", ".osrm.ebg_nodes", ".osrm.properties"},
{},
{".osrm.cell_metrics", ".osrm.mldgr"}),
requested_num_threads(0)

View File

@ -11,6 +11,7 @@ namespace extractor
{
using ClassData = std::uint8_t;
constexpr ClassData INAVLID_CLASS_DATA = std::numeric_limits<ClassData>::max();
static const std::uint8_t MAX_CLASS_INDEX = 8 - 1;
static const std::uint8_t MAX_AVOIDABLE_CLASSES = 8;

View File

@ -30,6 +30,7 @@ struct ProfileProperties
use_turn_restrictions(false), left_hand_driving(false), fallback_to_duration(true),
weight_name{"duration"}, call_tagless_node_function(true)
{
std::fill(avoidable_classes.begin(), avoidable_classes.end(), INAVLID_CLASS_DATA);
BOOST_ASSERT(weight_name[MAX_WEIGHT_NAME_LENGTH] == '\0');
}

View File

@ -1,5 +1,7 @@
#include "customizer/customizer.hpp"
#include "extractor/node_data_container.hpp"
#include "customizer/cell_customizer.hpp"
#include "customizer/customizer.hpp"
#include "customizer/edge_based_graph.hpp"
#include "customizer/files.hpp"
@ -85,16 +87,50 @@ auto LoadAndUpdateEdgeExpandedGraph(const CustomizationConfig &config,
auto directed = partition::splitBidirectionalEdges(edge_based_edge_list);
auto tidied =
partition::prepareEdgesForUsageInGraph<StaticEdgeBasedGraphEdge>(std::move(directed));
auto edge_based_graph =
std::make_unique<customizer::MultiLevelEdgeBasedGraph>(mlp, num_nodes, std::move(tidied));
util::Log() << "Loaded edge based graph for mapping partition ids: "
<< edge_based_graph->GetNumberOfEdges() << " edges, "
<< edge_based_graph->GetNumberOfNodes() << " nodes";
auto edge_based_graph = customizer::MultiLevelEdgeBasedGraph(mlp, num_nodes, std::move(tidied));
return edge_based_graph;
}
std::vector<std::vector<bool>>
avoidFlagsToNodeFilter(const MultiLevelEdgeBasedGraph &graph,
const extractor::EdgeBasedNodeDataContainer &node_data,
const extractor::ProfileProperties &properties)
{
std::vector<std::vector<bool>> filters;
for (auto mask : properties.avoidable_classes)
{
if (mask != extractor::INAVLID_CLASS_DATA)
{
std::vector<bool> allowed_nodes(true, graph.GetNumberOfNodes());
for (const auto node : util::irange<NodeID>(0, graph.GetNumberOfNodes()))
{
allowed_nodes[node] = (node_data.GetClassData(node) & mask) == 0;
}
filters.push_back(std::move(allowed_nodes));
}
}
return filters;
}
std::vector<CellMetric> filterToMetrics(const MultiLevelEdgeBasedGraph &graph,
const partition::CellStorage &storage,
const partition::MultiLevelPartition &mlp,
const std::vector<std::vector<bool>> &node_filters)
{
CellCustomizer customizer(mlp);
std::vector<CellMetric> metrics;
for (auto filter : node_filters)
{
auto metric = storage.MakeMetric();
customizer.Customize(graph, storage, filter, metric);
metrics.push_back(std::move(metric));
}
return metrics;
}
int Customizer::Run(const CustomizationConfig &config)
{
TIMER_START(loading_data);
@ -102,31 +138,42 @@ int Customizer::Run(const CustomizationConfig &config)
partition::MultiLevelPartition mlp;
partition::files::readPartition(config.GetPath(".osrm.partition"), mlp);
auto edge_based_graph = LoadAndUpdateEdgeExpandedGraph(config, mlp);
auto graph = LoadAndUpdateEdgeExpandedGraph(config, mlp);
util::Log() << "Loaded edge based graph: " << graph.GetNumberOfEdges() << " edges, "
<< graph.GetNumberOfNodes() << " nodes";
partition::CellStorage storage;
partition::files::readCells(config.GetPath(".osrm.cells"), storage);
TIMER_STOP(loading_data);
extractor::EdgeBasedNodeDataContainer node_data;
extractor::files::readNodeData(config.GetPath(".osrm.ebg_nodes"), node_data);
extractor::ProfileProperties properties;
extractor::files::readProfileProperties(config.GetPath(".osrm.properties"), properties);
util::Log() << "Loading partition data took " << TIMER_SEC(loading_data) << " seconds";
TIMER_START(cell_customize);
CellCustomizer customizer(mlp);
auto metric = storage.MakeMetric();
customizer.Customize(*edge_based_graph, storage, metric);
auto filter = avoidFlagsToNodeFilter(graph, node_data, properties);
auto metrics = filterToMetrics(graph, storage, mlp, filter);
TIMER_STOP(cell_customize);
util::Log() << "Cells customization took " << TIMER_SEC(cell_customize) << " seconds";
TIMER_START(writing_mld_data);
files::writeCellMetrics(config.GetPath(".osrm.cell_metrics"), std::vector<CellMetric>{metric});
files::writeCellMetrics(config.GetPath(".osrm.cell_metrics"), metrics);
TIMER_STOP(writing_mld_data);
util::Log() << "MLD customization writing took " << TIMER_SEC(writing_mld_data) << " seconds";
TIMER_START(writing_graph);
partition::files::writeGraph(config.GetPath(".osrm.mldgr"), *edge_based_graph);
partition::files::writeGraph(config.GetPath(".osrm.mldgr"), graph);
TIMER_STOP(writing_graph);
util::Log() << "Graph writing took " << TIMER_SEC(writing_graph) << " seconds";
CellStorageStatistics(*edge_based_graph, mlp, storage, metric);
for (const auto &metric : metrics)
{
CellStorageStatistics(graph, mlp, storage, metric);
}
return 0;
}

View File

@ -1,11 +1,12 @@
#include "common/range_tools.hpp"
#include <boost/test/unit_test.hpp>
#include "customizer/cell_customizer.hpp"
#include "partition/multi_level_graph.hpp"
#include "partition/multi_level_partition.hpp"
#include "util/static_graph.hpp"
#include <boost/test/unit_test.hpp>
using namespace osrm;
using namespace osrm::customizer;
using namespace osrm::partition;
@ -48,6 +49,9 @@ BOOST_AUTO_TEST_SUITE(cell_customization_tests)
BOOST_AUTO_TEST_CASE(two_level_test)
{
// 0 --- 1
// | |
// 2 --- 3
// node: 0 1 2 3
std::vector<CellID> l1{{0, 0, 1, 1}};
MultiLevelPartition mlp{{l1}, {2}};
@ -57,6 +61,7 @@ BOOST_AUTO_TEST_CASE(two_level_test)
std::vector<MockEdge> edges = {{0, 1, 1}, {0, 2, 1}, {2, 3, 1}, {3, 1, 1}, {3, 2, 1}};
auto graph = makeGraph(mlp, edges);
std::vector<bool> node_filter(true, graph.GetNumberOfNodes());
CellStorage storage(mlp, graph);
auto metric = storage.MakeMetric();
@ -83,8 +88,8 @@ BOOST_AUTO_TEST_CASE(two_level_test)
REQUIRE_SIZE_RANGE(cell_1_1.GetOutWeight(2), 2);
REQUIRE_SIZE_RANGE(cell_1_1.GetInWeight(3), 2);
customizer.Customize(graph, heap, storage, metric, 1, 0);
customizer.Customize(graph, heap, storage, metric, 1, 1);
customizer.Customize(graph, heap, storage, node_filter, metric, 1, 0);
customizer.Customize(graph, heap, storage, node_filter, metric, 1, 1);
// cell 0
// check row source -> destination
@ -136,6 +141,7 @@ BOOST_AUTO_TEST_CASE(four_levels_test)
};
auto graph = makeGraph(mlp, edges);
std::vector<bool> node_filter(true, graph.GetNumberOfNodes());
CellStorage storage(mlp, graph);
auto metric = storage.MakeMetric();
@ -209,13 +215,13 @@ BOOST_AUTO_TEST_CASE(four_levels_test)
CellCustomizer customizer(mlp);
CellCustomizer::Heap heap(graph.GetNumberOfNodes());
customizer.Customize(graph, heap, storage, metric, 1, 0);
customizer.Customize(graph, heap, storage, metric, 1, 1);
customizer.Customize(graph, heap, storage, metric, 1, 2);
customizer.Customize(graph, heap, storage, metric, 1, 3);
customizer.Customize(graph, heap, storage, node_filter, metric, 1, 0);
customizer.Customize(graph, heap, storage, node_filter, metric, 1, 1);
customizer.Customize(graph, heap, storage, node_filter, metric, 1, 2);
customizer.Customize(graph, heap, storage, node_filter, metric, 1, 3);
customizer.Customize(graph, heap, storage, metric, 2, 0);
customizer.Customize(graph, heap, storage, metric, 2, 1);
customizer.Customize(graph, heap, storage, node_filter, metric, 2, 0);
customizer.Customize(graph, heap, storage, node_filter, metric, 2, 1);
// level 1
// cell 0
@ -264,7 +270,7 @@ BOOST_AUTO_TEST_CASE(four_levels_test)
CellStorage storage_rec(mlp, graph);
auto metric_rec = storage_rec.MakeMetric();
customizer.Customize(graph, storage_rec, metric_rec);
customizer.Customize(graph, storage_rec, node_filter, metric_rec);
CHECK_EQUAL_COLLECTIONS(cell_2_1.GetOutWeight(9),
storage_rec.GetCell(metric_rec, 2, 1).GetOutWeight(9));
@ -278,4 +284,148 @@ BOOST_AUTO_TEST_CASE(four_levels_test)
storage_rec.GetCell(metric_rec, 2, 1).GetInWeight(12));
}
BOOST_AUTO_TEST_CASE(avoid_test)
{
// 0 --- 1 --- 5 --- 6
// | / | | |
// 2 ----3 --- 4 --- 7
// \__________/
std::vector<MockEdge> edges = {
{0, 1, 1},
{0, 2, 1},
{1, 0, 1},
{1, 2, 10},
{1, 3, 1},
{1, 5, 1},
{2, 0, 1},
{2, 1, 10},
{2, 3, 1},
{2, 4, 1},
{3, 1, 1},
{3, 2, 1},
{3, 4, 1},
{4, 2, 1},
{4, 3, 1},
{4, 5, 1},
{4, 7, 1},
{5, 1, 1},
{5, 4, 1},
{5, 6, 1},
{6, 5, 1},
{6, 7, 1},
{7, 4, 1},
{7, 6, 1},
};
// node: 0 1 2 3 4 5 6 7
std::vector<CellID> l1{{0, 0, 1, 1, 3, 2, 2, 3}};
std::vector<CellID> l2{{0, 0, 0, 0, 1, 1, 1, 1}};
std::vector<CellID> l3{{0, 0, 0, 0, 0, 0, 0, 0}};
MultiLevelPartition mlp{{l1, l2, l3}, {4, 2, 1}};
BOOST_REQUIRE_EQUAL(mlp.GetNumberOfLevels(), 4);
auto graph = makeGraph(mlp, edges);
// avoid node 0, 3 and 7
std::vector<bool> node_filter = {false, true, true, false, true, true, true, false};
CellCustomizer customizer(mlp);
CellStorage storage(mlp, graph);
auto metric = storage.MakeMetric();
customizer.Customize(graph, storage, node_filter, metric);
auto cell_1_0 = storage.GetCell(metric, 1, 0);
auto cell_1_1 = storage.GetCell(metric, 1, 1);
auto cell_1_2 = storage.GetCell(metric, 1, 2);
auto cell_1_3 = storage.GetCell(metric, 1, 3);
REQUIRE_SIZE_RANGE(cell_1_0.GetSourceNodes(), 2);
REQUIRE_SIZE_RANGE(cell_1_0.GetDestinationNodes(), 2);
REQUIRE_SIZE_RANGE(cell_1_1.GetSourceNodes(), 2);
REQUIRE_SIZE_RANGE(cell_1_1.GetDestinationNodes(), 2);
REQUIRE_SIZE_RANGE(cell_1_2.GetSourceNodes(), 2);
REQUIRE_SIZE_RANGE(cell_1_2.GetDestinationNodes(), 2);
REQUIRE_SIZE_RANGE(cell_1_3.GetSourceNodes(), 2);
REQUIRE_SIZE_RANGE(cell_1_3.GetDestinationNodes(), 2);
CHECK_EQUAL_RANGE(cell_1_0.GetSourceNodes(), 0, 1);
CHECK_EQUAL_RANGE(cell_1_0.GetDestinationNodes(), 0, 1);
CHECK_EQUAL_RANGE(cell_1_1.GetSourceNodes(), 2, 3);
CHECK_EQUAL_RANGE(cell_1_1.GetDestinationNodes(), 2, 3);
CHECK_EQUAL_RANGE(cell_1_2.GetSourceNodes(), 5, 6);
CHECK_EQUAL_RANGE(cell_1_2.GetDestinationNodes(), 5, 6);
CHECK_EQUAL_RANGE(cell_1_3.GetSourceNodes(), 4, 7);
CHECK_EQUAL_RANGE(cell_1_3.GetDestinationNodes(), 4, 7);
REQUIRE_SIZE_RANGE(cell_1_0.GetOutWeight(0), 2);
REQUIRE_SIZE_RANGE(cell_1_0.GetOutWeight(1), 2);
REQUIRE_SIZE_RANGE(cell_1_0.GetInWeight(0), 2);
REQUIRE_SIZE_RANGE(cell_1_0.GetInWeight(1), 2);
REQUIRE_SIZE_RANGE(cell_1_1.GetOutWeight(2), 2);
REQUIRE_SIZE_RANGE(cell_1_1.GetOutWeight(3), 2);
REQUIRE_SIZE_RANGE(cell_1_1.GetInWeight(2), 2);
REQUIRE_SIZE_RANGE(cell_1_1.GetInWeight(3), 2);
REQUIRE_SIZE_RANGE(cell_1_2.GetOutWeight(5), 2);
REQUIRE_SIZE_RANGE(cell_1_2.GetOutWeight(6), 2);
REQUIRE_SIZE_RANGE(cell_1_2.GetInWeight(5), 2);
REQUIRE_SIZE_RANGE(cell_1_2.GetInWeight(6), 2);
REQUIRE_SIZE_RANGE(cell_1_3.GetOutWeight(4), 2);
REQUIRE_SIZE_RANGE(cell_1_3.GetOutWeight(7), 2);
REQUIRE_SIZE_RANGE(cell_1_3.GetInWeight(4), 2);
REQUIRE_SIZE_RANGE(cell_1_3.GetInWeight(7), 2);
CHECK_EQUAL_RANGE(cell_1_0.GetOutWeight(0), INVALID_EDGE_WEIGHT, INVALID_EDGE_WEIGHT);
CHECK_EQUAL_RANGE(cell_1_0.GetOutWeight(1), INVALID_EDGE_WEIGHT, 0);
CHECK_EQUAL_RANGE(cell_1_0.GetInWeight(0), INVALID_EDGE_WEIGHT, INVALID_EDGE_WEIGHT);
CHECK_EQUAL_RANGE(cell_1_0.GetInWeight(1), INVALID_EDGE_WEIGHT, 0);
CHECK_EQUAL_RANGE(cell_1_1.GetOutWeight(2), 0, INVALID_EDGE_WEIGHT);
CHECK_EQUAL_RANGE(cell_1_1.GetOutWeight(3), INVALID_EDGE_WEIGHT, INVALID_EDGE_WEIGHT);
CHECK_EQUAL_RANGE(cell_1_1.GetInWeight(2), 0, INVALID_EDGE_WEIGHT);
CHECK_EQUAL_RANGE(cell_1_1.GetInWeight(3), INVALID_EDGE_WEIGHT, INVALID_EDGE_WEIGHT);
CHECK_EQUAL_RANGE(cell_1_2.GetOutWeight(5), 0, 1);
CHECK_EQUAL_RANGE(cell_1_2.GetOutWeight(6), 1, 0);
CHECK_EQUAL_RANGE(cell_1_2.GetInWeight(5), 0, 1);
CHECK_EQUAL_RANGE(cell_1_2.GetInWeight(6), 1, 0);
CHECK_EQUAL_RANGE(cell_1_3.GetOutWeight(4), 0, INVALID_EDGE_WEIGHT);
CHECK_EQUAL_RANGE(cell_1_3.GetOutWeight(7), INVALID_EDGE_WEIGHT, INVALID_EDGE_WEIGHT);
CHECK_EQUAL_RANGE(cell_1_3.GetInWeight(4), 0, INVALID_EDGE_WEIGHT);
CHECK_EQUAL_RANGE(cell_1_3.GetInWeight(7), INVALID_EDGE_WEIGHT, INVALID_EDGE_WEIGHT);
auto cell_2_0 = storage.GetCell(metric, 2, 0);
auto cell_2_1 = storage.GetCell(metric, 2, 1);
REQUIRE_SIZE_RANGE(cell_2_0.GetSourceNodes(), 3);
REQUIRE_SIZE_RANGE(cell_2_0.GetDestinationNodes(), 3);
REQUIRE_SIZE_RANGE(cell_2_1.GetSourceNodes(), 2);
REQUIRE_SIZE_RANGE(cell_2_1.GetDestinationNodes(), 2);
CHECK_EQUAL_RANGE(cell_2_0.GetSourceNodes(), 1, 2, 3);
CHECK_EQUAL_RANGE(cell_2_0.GetDestinationNodes(), 1, 2, 3);
CHECK_EQUAL_RANGE(cell_2_1.GetSourceNodes(), 4, 5);
CHECK_EQUAL_RANGE(cell_2_1.GetDestinationNodes(), 4, 5);
REQUIRE_SIZE_RANGE(cell_2_0.GetOutWeight(1), 3);
REQUIRE_SIZE_RANGE(cell_2_0.GetOutWeight(2), 3);
REQUIRE_SIZE_RANGE(cell_2_0.GetOutWeight(3), 3);
REQUIRE_SIZE_RANGE(cell_2_0.GetInWeight(1), 3);
REQUIRE_SIZE_RANGE(cell_2_0.GetInWeight(2), 3);
REQUIRE_SIZE_RANGE(cell_2_0.GetInWeight(3), 3);
REQUIRE_SIZE_RANGE(cell_2_1.GetOutWeight(4), 2);
REQUIRE_SIZE_RANGE(cell_2_1.GetOutWeight(5), 2);
REQUIRE_SIZE_RANGE(cell_2_1.GetInWeight(4), 2);
REQUIRE_SIZE_RANGE(cell_2_1.GetInWeight(5), 2);
CHECK_EQUAL_RANGE(cell_2_0.GetOutWeight(1), 0, 10, INVALID_EDGE_WEIGHT);
CHECK_EQUAL_RANGE(cell_2_0.GetOutWeight(2), 10, 0, INVALID_EDGE_WEIGHT);
CHECK_EQUAL_RANGE(cell_2_0.GetOutWeight(3), INVALID_EDGE_WEIGHT, INVALID_EDGE_WEIGHT, INVALID_EDGE_WEIGHT);
CHECK_EQUAL_RANGE(cell_2_0.GetInWeight(1), 0, 10, INVALID_EDGE_WEIGHT);
CHECK_EQUAL_RANGE(cell_2_0.GetInWeight(2), 10, 0, INVALID_EDGE_WEIGHT);
CHECK_EQUAL_RANGE(cell_2_0.GetInWeight(3), INVALID_EDGE_WEIGHT, INVALID_EDGE_WEIGHT, INVALID_EDGE_WEIGHT);
CHECK_EQUAL_RANGE(cell_2_1.GetOutWeight(4), 0, 1);
CHECK_EQUAL_RANGE(cell_2_1.GetOutWeight(5), 1, 0);
CHECK_EQUAL_RANGE(cell_2_1.GetInWeight(4), 0, 1);
CHECK_EQUAL_RANGE(cell_2_1.GetInWeight(5), 1, 0);
}
BOOST_AUTO_TEST_SUITE_END()