12#include <robin_hood.h>
19#include <boost/graph/dijkstra_shortest_paths.hpp>
20#include <boost/graph/breadth_first_search.hpp>
21#include <boost/graph/dijkstra_shortest_paths_no_color_map.hpp>
22#include <boost/exception/exception.hpp>
23#include <boost/math/special_functions/fpclassify.hpp>
81 const std::vector<size_t>& pred,
82 const std::vector<float>& distances
91 int current_node = end;
92 if (pred[current_node] == current_node)
return Path{};
94 float last_cost = distances[current_node];
97 while (current_node != start) {
101 if (p.
size() > pred.size())
102 throw std::exception(
"Path included more nodes than contaiend in the graph!");
105 int next_node = pred[current_node];
108 float current_cost = distances[next_node];
111 float de_facto_cost = last_cost - current_cost;
114 p.
AddNode(next_node, de_facto_cost);
117 last_cost = current_cost;
118 current_node = next_node;
149 int n = num_vertices(g);
153 auto pm = boost::predecessor_map(&dist_pred.
predecessor[0]);
157 dijkstra_shortest_paths_no_color_map(
178 vector<Path>
FindPaths(
BoostGraph * bg,
const vector<int> & start_points,
const vector<int> & end_points)
182 vector<Path> paths(start_points.size());
185 robin_hood::unordered_map<int, DistPred> dpm;
186 for (
int start_point : start_points)
187 if (dpm.count(start_point) == 0)
191 for (
int i = 0; i < start_points.size(); i++) {
192 int start = start_points[i];
193 int end = end_points[i];
194 const auto& dist_pred = dpm[start];
203 const std::vector<int>& start_points,
204 const std::vector<int>& end_points,
211 robin_hood::unordered_map<int, DistPred> dpm;
214 int core_count = std::thread::hardware_concurrency();
215 int cores_to_use = std::min(core_count-1,
static_cast<int>(start_points.size()));
216 omp_set_num_threads(cores_to_use);
219 std::vector<int> start_copy = start_points;
220 std::sort(std::execution::par_unseq, start_copy.begin(),start_copy.end());
223 std::vector<int> unique_starts;
224 std::unique_copy(start_copy.begin(), start_copy.end(), std::back_inserter(unique_starts));
228 for (
auto uc : unique_starts)
229 dpm.emplace(std::pair<int, DistPred>{uc,
DistPred()});
232 #pragma omp parallel for schedule(dynamic) if (unique_starts.size() > cores_to_use && cores_to_use > 4)
233 for (
int i = 0; i < unique_starts.size(); i++) {
234 int start_point = unique_starts[i];
239#pragma omp parallel for schedule(dynamic) if (cores_to_use > 4)
240 for (
int i = 0; i < start_points.size(); i++) {
243 int start = start_points[i];
244 int end = end_points[i];
247 const auto& dist_pred = dpm[start];
256 out_sizes[i] = out_paths[i]->
size();
260 if (out_sizes[i] == 0) {
261 delete (out_paths[i]);
262 out_paths[i] =
nullptr;
269 const auto & g = bg.
g;
272 const int num_nodes = bg.
p.size();
276 #pragma omp parallel for schedule(dynamic)
277 for (
int row = 0; row < num_nodes; row++) {
284 auto pm = boost::predecessor_map(pred_row_start);
291 dijkstra_shortest_paths_no_color_map(
301 for (
int i = 0; i < num_nodes; i++)
303 float & dist_element = dist_row_start[i];
304 int & pred_element = pred_row_start[i];
309 if (dist_element == std::numeric_limits<float>::max()) {
322 return std::unique_ptr<BoostGraph, BoostGraphDeleter>(
new BoostGraph(g, cost_type));
330 size_t node_count = bg->
p.size();
331 size_t max_path = node_count * node_count;
333 std::vector<int> start_points(max_path);
334 std::vector<int> end_points(max_path);
340 for (
int i = 0; i < node_count; i++) {
341 for (
int k = 0; k < node_count; k++) {
342 start_points[curr_id++] = i;
350 for (
int i = 0; i < node_count; i++) {
351 for (
int k = 0; k < node_count; k++) {
352 end_points[curr_id++] = k;
Contains definitions for the BoostGraph class.
Contains definition for the BoostGraphDeleter structure.
Contains definitions for the Path structure.
Contains standard fundamental data structures for representing space used throughout DHARTAPI.
Algorithms to find the shortest path between nodes in a HF::SpatialStructures::Graph.
vector< Path > FindPaths(BoostGraph *bg, const vector< int > &start_points, const vector< int > &end_points)
Find a path from every id in start_ids to the matching end node in end_ids.
void InsertPathsIntoArray(const BoostGraph *bg, const std::vector< int > &start_points, const std::vector< int > &end_points, HF::SpatialStructures::Path **out_paths, HF::SpatialStructures::PathMember **out_path_members, int *out_sizes)
A special version of FindPaths optimized for the C_Interface.
Path FindPath(BoostGraph *bg, int start_id, int end_id)
Find a path between points A and B using Dijkstra's Shortest Path algorithm.
boost::graph_traits< graph_t >::vertex_descriptor vertex_descriptor
Quick alias to shorten the typename of vertex descriptors for our graph_t type. /summary>
void InsertAllToAllPathsIntoArray(BoostGraph *bg, Path **out_paths, PathMember **out_path_members, int *out_sizes)
A special version of FindPaths optimized for the C_Interface, such that all paths possible from each ...
boost::compressed_sparse_row_graph< boost::directedS, vertex_data, Edge_Cost > graph_t
Type of graph held by the BoostGraph.
Path ConstructShortestPathFromPred(int start, int end, const std::vector< size_t > &pred, const std::vector< float > &distances)
Construct the shortest path from start to end using the given predecessor and distance vectors.
DistPred BuildDistanceAndPredecessor(const graph_t &g, int id)
Build a row of the distance and predecessor matrices for the node at id.
DistanceAndPredecessor GenerateDistanceAndPred(const BoostGraph &bg)
Generate the distance and predecessor matricies for a specific boost graph.
std::unique_ptr< BoostGraph, BoostGraphDeleter > CreateBoostGraph(const Graph &g, const std::string &cost_type)
Create a new boost graph from a HF::SpatialStructures:Graph.
float weight
Cost of traversing this edge.
A graph usable with the BoostGraphLibrary.
graph_t g
The underlying graph in boost.
std::vector< vertex_descriptor > p
Vertex array preallocated to the number of nodes in the graph.
A single row of a distance and predecessor matrix.
std::vector< float > distance
Distance array.
DistPred()
Construct a new instance of DistPred with empty distance and predecessor arrays.
DistPred(int n)
Allocate enough space for n elements.
std::vector< vertex_descriptor > predecessor
Predecessor array.
void operator()(BoostGraph *bg) const
Delete a boost graph.
Holds and maintains a distance and predecessor matrix.
int * GetRowOfPred(int i)
Get a pointer to the beginning of the ith row of the predecessor array.
float * GetRowOfDist(int i)
Get a pointer to the beginning of the ith row of the distance array.
A Graph of nodes connected by edges that supports both integers and HF::SpatialStructures::Node.
The ID of a node, and the cost cost to the node after it.
A collection of nodes that form a path.
int size() const
Determine how many nodes are in this path.
void AddNode(int node, float cost)
Add a new node to the path.
PathMember * GetPMPointer()
Get a pointer to the path's underlying path members vector.
void Reverse()
Reverse the direction of this path.