Initial Skeleton for Recursive Bisection via Inertial Flow
This commit is contained in:
committed by
Patrick Niklaus
parent
07221f5a48
commit
d56db500d3
@@ -0,0 +1,65 @@
|
||||
#include "partition/graph_view.hpp"
|
||||
#include <iterator>
|
||||
|
||||
namespace osrm
|
||||
{
|
||||
namespace partition
|
||||
{
|
||||
|
||||
HasSamePartitionID::HasSamePartitionID(const RecursiveBisectionState::BisectionID bisection_id_,
|
||||
const BisectionGraph &bisection_graph_,
|
||||
const RecursiveBisectionState &recursive_bisection_state_)
|
||||
: bisection_id(bisection_id_), bisection_graph(bisection_graph_),
|
||||
recursive_bisection_state(recursive_bisection_state_)
|
||||
{
|
||||
}
|
||||
|
||||
bool HasSamePartitionID::operator()(const EdgeID eid) const
|
||||
{
|
||||
return recursive_bisection_state.GetBisectionID(bisection_graph.GetTarget(eid)) == bisection_id;
|
||||
}
|
||||
|
||||
GraphView::GraphView(const BisectionGraph &bisection_graph_,
|
||||
const RecursiveBisectionState &bisection_state_,
|
||||
const RecursiveBisectionState::IDIterator begin_,
|
||||
const RecursiveBisectionState::IDIterator end_)
|
||||
: bisection_graph(bisection_graph_), bisection_state(bisection_state_), begin(begin_), end(end_)
|
||||
{
|
||||
// print graph
|
||||
std::cout << "Graph\n";
|
||||
for( auto itr = begin_; itr != end_; ++itr )
|
||||
{
|
||||
std::cout << "Node: " << *itr << std::endl;
|
||||
for( auto eitr = EdgeBegin(*itr); eitr != EdgeEnd(*itr); ++eitr )
|
||||
{
|
||||
std::cout << "\t" << *eitr << " -> " << bisection_graph.GetTarget(*eitr) << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
RecursiveBisectionState::IDIterator GraphView::Begin() const { return begin; }
|
||||
|
||||
RecursiveBisectionState::IDIterator GraphView::End() const { return end; }
|
||||
|
||||
std::size_t GraphView::NumberOfNodes() const { return std::distance(begin, end); }
|
||||
|
||||
GraphView::EdgeIterator GraphView::EdgeBegin(const NodeID nid) const
|
||||
{
|
||||
return boost::make_filter_iterator(
|
||||
HasSamePartitionID(bisection_state.GetBisectionID(nid),
|
||||
bisection_graph,
|
||||
bisection_state),
|
||||
EdgeIDIterator(bisection_graph.BeginEdges(nid)),
|
||||
EdgeIDIterator(bisection_graph.EndEdges(nid)));
|
||||
}
|
||||
|
||||
GraphView::EdgeIterator GraphView::EdgeEnd(const NodeID nid) const
|
||||
{
|
||||
return boost::make_filter_iterator(
|
||||
HasSamePartitionID(bisection_state.GetBisectionID(nid), bisection_graph, bisection_state),
|
||||
EdgeIDIterator(bisection_graph.EndEdges(nid)),
|
||||
EdgeIDIterator(bisection_graph.EndEdges(nid)));
|
||||
}
|
||||
|
||||
} // namespace partition
|
||||
} // namespace osrm
|
||||
@@ -0,0 +1,22 @@
|
||||
#include "partition/inertial_flow.hpp"
|
||||
|
||||
namespace osrm
|
||||
{
|
||||
namespace partition
|
||||
{
|
||||
|
||||
InertialFlow::InertialFlow(const GraphView &view_) : view(view_) {}
|
||||
|
||||
std::vector<bool> InertialFlow::ComputePartition(const double balance, const double source_sink_rate)
|
||||
{
|
||||
std::vector<bool> partition(view.NumberOfNodes());
|
||||
std::size_t i = 0;
|
||||
for( auto itr = partition.begin(); itr != partition.end(); ++itr )
|
||||
{
|
||||
*itr = (i++ % 2) != 0;
|
||||
}
|
||||
return partition;
|
||||
}
|
||||
|
||||
} // namespace partition
|
||||
} // namespace osrm
|
||||
@@ -0,0 +1,78 @@
|
||||
#include "partition/partitioner.hpp"
|
||||
#include "partition/bisection_graph.hpp"
|
||||
|
||||
#include "partition/recursive_bisection.hpp"
|
||||
|
||||
// TODO remove after testing
|
||||
#include "util/coordinate.hpp"
|
||||
|
||||
namespace osrm
|
||||
{
|
||||
namespace partition
|
||||
{
|
||||
|
||||
int Partitioner::Run(const PartitionConfig &config)
|
||||
{
|
||||
struct TestEdge
|
||||
{
|
||||
NodeID source;
|
||||
NodeID target;
|
||||
};
|
||||
|
||||
std::vector<TestEdge> input_edges;
|
||||
|
||||
// 0 - 1 - 2 - 3
|
||||
// | \ | |
|
||||
// 4 - 5 - 6 - 7
|
||||
|
||||
input_edges.push_back({0, 1});
|
||||
input_edges.push_back({0, 4});
|
||||
input_edges.push_back({0, 5});
|
||||
|
||||
input_edges.push_back({1, 0});
|
||||
input_edges.push_back({1, 2});
|
||||
|
||||
input_edges.push_back({2, 1});
|
||||
input_edges.push_back({2, 3});
|
||||
input_edges.push_back({2, 6});
|
||||
|
||||
input_edges.push_back({3, 2});
|
||||
input_edges.push_back({3, 7});
|
||||
|
||||
input_edges.push_back({4, 0});
|
||||
input_edges.push_back({4, 5});
|
||||
|
||||
input_edges.push_back({5, 0});
|
||||
input_edges.push_back({5, 4});
|
||||
input_edges.push_back({5, 6});
|
||||
|
||||
input_edges.push_back({6, 2});
|
||||
input_edges.push_back({6, 5});
|
||||
input_edges.push_back({6, 7});
|
||||
|
||||
input_edges.push_back({7, 3});
|
||||
input_edges.push_back({7, 6});
|
||||
|
||||
input_edges = groupBySource(std::move(input_edges));
|
||||
|
||||
std::vector<util::Coordinate> coordinates;
|
||||
|
||||
for (std::size_t i = 0; i < 8; ++i)
|
||||
{
|
||||
coordinates.push_back(
|
||||
{util::FloatLongitude{(i % 4) / 4.0}, util::FloatLatitude{(double)(i / 4)}});
|
||||
}
|
||||
|
||||
// do the partitioning
|
||||
std::vector<BisectionNode> nodes = computeNodes(coordinates, input_edges);
|
||||
std::vector<BisectionEdge> edges = adaptToBisectionEdge(input_edges);
|
||||
|
||||
BisectionGraph graph(nodes, edges);
|
||||
|
||||
RecursiveBisection recursive_bisection(1024, 1.1, 0.25, graph);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
} // namespace partition
|
||||
} // namespace osrm
|
||||
@@ -0,0 +1,38 @@
|
||||
#include "partition/recursive_bisection.hpp"
|
||||
#include "partition/inertial_flow.hpp"
|
||||
|
||||
#include "partition/graph_view.hpp"
|
||||
#include "partition/recursive_bisection_state.hpp"
|
||||
|
||||
namespace osrm
|
||||
{
|
||||
namespace partition
|
||||
{
|
||||
|
||||
RecursiveBisection::RecursiveBisection(std::size_t maximum_cell_size,
|
||||
double balance,
|
||||
double boundary_factor,
|
||||
const BisectionGraph &bisection_graph_)
|
||||
: bisection_graph(bisection_graph_), internal_state(bisection_graph_)
|
||||
{
|
||||
GraphView view(bisection_graph, internal_state, internal_state.Begin(), internal_state.End());
|
||||
InertialFlow flow(view);
|
||||
const auto partition = flow.ComputePartition(balance, boundary_factor);
|
||||
const auto center = internal_state.ApplyBisection(view.Begin(), view.End(), partition);
|
||||
{
|
||||
auto state = internal_state;
|
||||
}
|
||||
|
||||
GraphView recursive_view_lhs(bisection_graph, internal_state, view.Begin(), center);
|
||||
InertialFlow flow_lhs(recursive_view_lhs);
|
||||
const auto partition_lhs = flow_lhs.ComputePartition(balance,boundary_factor);
|
||||
internal_state.ApplyBisection(recursive_view_lhs.Begin(),recursive_view_lhs.End(),partition_lhs);
|
||||
|
||||
GraphView recursive_view_rhs(bisection_graph, internal_state, center, view.End());
|
||||
InertialFlow flow_rhs(recursive_view_rhs);
|
||||
const auto partition_rhs = flow_rhs.ComputePartition(balance,boundary_factor);
|
||||
internal_state.ApplyBisection(recursive_view_rhs.Begin(),recursive_view_rhs.End(),partition_rhs);
|
||||
}
|
||||
|
||||
} // namespace partition
|
||||
} // namespace osrm
|
||||
@@ -0,0 +1,69 @@
|
||||
#include "partition/recursive_bisection_state.hpp"
|
||||
|
||||
#include <numeric>
|
||||
|
||||
//TODO remove
|
||||
#include <iostream>
|
||||
#include <bitset>
|
||||
|
||||
namespace osrm
|
||||
{
|
||||
namespace partition
|
||||
{
|
||||
|
||||
RecursiveBisectionState::RecursiveBisectionState(const BisectionGraph &bisection_graph_)
|
||||
: bisection_graph(bisection_graph_)
|
||||
{
|
||||
id_array.resize(bisection_graph.GetNumberOfNodes());
|
||||
std::iota(id_array.begin(), id_array.end(), 0);
|
||||
bisection_ids.resize(bisection_graph.GetNumberOfNodes(), 0);
|
||||
}
|
||||
|
||||
RecursiveBisectionState::~RecursiveBisectionState()
|
||||
{
|
||||
std::cout << "Internal Result\n";
|
||||
std::cout << "IDArray:";
|
||||
for( auto id : id_array )
|
||||
std::cout << " " << id;
|
||||
std::cout << std::endl;
|
||||
|
||||
std::cout << "BisectionIDs:";
|
||||
for( auto id : bisection_ids)
|
||||
std::cout << " " << (std::bitset<4>(id));
|
||||
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
const RecursiveBisectionState::IDIterator RecursiveBisectionState::Begin() const
|
||||
{
|
||||
return id_array.begin();
|
||||
}
|
||||
|
||||
const RecursiveBisectionState::IDIterator RecursiveBisectionState::End() const
|
||||
{
|
||||
return id_array.end();
|
||||
}
|
||||
|
||||
RecursiveBisectionState::BisectionID RecursiveBisectionState::GetBisectionID(const NodeID nid) const
|
||||
{
|
||||
return bisection_ids[nid];
|
||||
}
|
||||
|
||||
RecursiveBisectionState::IDIterator RecursiveBisectionState::ApplyBisection(
|
||||
const IDIterator begin, const IDIterator end, const std::vector<bool> &partition)
|
||||
{
|
||||
// augment the partition ids
|
||||
for (auto itr = begin; itr != end; ++itr)
|
||||
{
|
||||
bisection_ids[*itr] <<= 1;
|
||||
bisection_ids[*itr] |= partition[std::distance(begin, itr)];
|
||||
}
|
||||
|
||||
// keep items with `0` as partition id to the left, move other to the right
|
||||
return std::stable_partition(id_array.begin() + std::distance(id_array.cbegin(), begin),
|
||||
id_array.begin() + std::distance(id_array.cbegin(), end),
|
||||
[this](const auto nid) { return 0 == (bisection_ids[nid] & 1); });
|
||||
}
|
||||
|
||||
} // namespace partition
|
||||
} // namespace osrm
|
||||
@@ -42,7 +42,7 @@ struct TarjanEdgeData
|
||||
};
|
||||
|
||||
using TarjanGraph = util::StaticGraph<TarjanEdgeData>;
|
||||
using TarjanEdge = TarjanGraph::InputEdge;
|
||||
using TarjanEdge = util::static_graph_details::SortableEdgeWithData<TarjanEdgeData>;
|
||||
|
||||
std::size_t loadGraph(const std::string &path,
|
||||
std::vector<extractor::QueryNode> &coordinate_list,
|
||||
|
||||
@@ -0,0 +1,145 @@
|
||||
#include "partition/partitioner.hpp"
|
||||
#include "partition/partition_config.hpp"
|
||||
|
||||
#include "util/log.hpp"
|
||||
#include "util/version.hpp"
|
||||
|
||||
#include <tbb/task_scheduler_init.h>
|
||||
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <boost/program_options.hpp>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace osrm;
|
||||
|
||||
enum class return_code : unsigned
|
||||
{
|
||||
ok,
|
||||
fail,
|
||||
exit
|
||||
};
|
||||
|
||||
return_code parseArguments(int argc, char *argv[], partition::PartitionConfig &partition_config)
|
||||
{
|
||||
// declare a group of options that will be allowed only on command line
|
||||
boost::program_options::options_description generic_options("Options");
|
||||
generic_options.add_options()("version,v", "Show version")("help,h", "Show this help message");
|
||||
|
||||
// declare a group of options that will be allowed both on command line
|
||||
boost::program_options::options_description config_options("Configuration");
|
||||
config_options.add_options()(
|
||||
"threads,t",
|
||||
boost::program_options::value<unsigned int>(&partition_config.requested_num_threads)
|
||||
->default_value(tbb::task_scheduler_init::default_num_threads()),
|
||||
"Number of threads to use");
|
||||
|
||||
// hidden options, will be allowed on command line, but will not be
|
||||
// shown to the user
|
||||
boost::program_options::options_description hidden_options("Hidden options");
|
||||
hidden_options.add_options()(
|
||||
"input,i",
|
||||
boost::program_options::value<boost::filesystem::path>(&partition_config.base_path),
|
||||
"Input file in .osrm format");
|
||||
|
||||
// positional option
|
||||
boost::program_options::positional_options_description positional_options;
|
||||
positional_options.add("input", 1);
|
||||
|
||||
// combine above options for parsing
|
||||
boost::program_options::options_description cmdline_options;
|
||||
cmdline_options.add(generic_options).add(config_options).add(hidden_options);
|
||||
|
||||
const auto *executable = argv[0];
|
||||
boost::program_options::options_description visible_options(
|
||||
boost::filesystem::path(executable).filename().string() + " <input.osrm> [options]");
|
||||
visible_options.add(generic_options).add(config_options);
|
||||
|
||||
// parse command line options
|
||||
boost::program_options::variables_map option_variables;
|
||||
try
|
||||
{
|
||||
boost::program_options::store(boost::program_options::command_line_parser(argc, argv)
|
||||
.options(cmdline_options)
|
||||
.positional(positional_options)
|
||||
.run(),
|
||||
option_variables);
|
||||
}
|
||||
catch (const boost::program_options::error &e)
|
||||
{
|
||||
util::Log(logERROR) << e.what();
|
||||
return return_code::fail;
|
||||
}
|
||||
|
||||
if (option_variables.count("version"))
|
||||
{
|
||||
std::cout << OSRM_VERSION << std::endl;
|
||||
return return_code::exit;
|
||||
}
|
||||
|
||||
if (option_variables.count("help"))
|
||||
{
|
||||
std::cout << visible_options;
|
||||
return return_code::exit;
|
||||
}
|
||||
|
||||
boost::program_options::notify(option_variables);
|
||||
|
||||
if (!option_variables.count("input"))
|
||||
{
|
||||
std::cout << visible_options;
|
||||
return return_code::exit;
|
||||
}
|
||||
|
||||
return return_code::ok;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) try
|
||||
{
|
||||
util::LogPolicy::GetInstance().Unmute();
|
||||
partition::PartitionConfig partition_config;
|
||||
|
||||
const auto result = parseArguments(argc, argv, partition_config);
|
||||
|
||||
if (return_code::fail == result)
|
||||
{
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if (return_code::exit == result)
|
||||
{
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
// set the default in/output names
|
||||
partition_config.UseDefaults();
|
||||
|
||||
if (1 > partition_config.requested_num_threads)
|
||||
{
|
||||
util::Log(logERROR) << "Number of threads must be 1 or larger";
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if (!boost::filesystem::is_regular_file(partition_config.base_path))
|
||||
{
|
||||
util::Log(logERROR) << "Input file " << partition_config.base_path.string()
|
||||
<< " not found!";
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
auto exitcode = partition::Partitioner().Run(partition_config);
|
||||
return exitcode;
|
||||
}
|
||||
catch (const std::bad_alloc &e)
|
||||
{
|
||||
util::Log(logERROR) << "[exception] " << e.what();
|
||||
util::Log(logERROR) << "Please provide more memory or consider using a larger swapfile";
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
#ifdef _WIN32
|
||||
catch (const std::exception &e)
|
||||
{
|
||||
util::Log(logERROR) << "[exception] " << e.what() << std::endl;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user