Skip to content

Commit

Permalink
Merge pull request #315 from aryan1010/aryan-week-6
Browse files Browse the repository at this point in the history
GSoC 2023: Aryan Gupta week 6
  • Loading branch information
aryan1010 committed Jul 9, 2023
2 parents 184ebd0 + 0e380a2 commit f2c529e
Show file tree
Hide file tree
Showing 7 changed files with 224 additions and 70 deletions.
52 changes: 0 additions & 52 deletions include/c_types/path_dd_rt.h

This file was deleted.

196 changes: 196 additions & 0 deletions include/dijkstra/v4drivingDist.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#include "cpp_common/pgr_base_graph.hpp"
#include "cpp_common/interruption.h"
#include "visitors/dijkstra_visitors.hpp"
#include <deque>
#include <set>
#include <vector>
#include <utility>

#include <visitors/dfs_visitor_with_root.hpp>
#include "cpp_common/basePath_SSEC.hpp"
#include <visitors/edges_order_dfs_visitor.hpp>
#include <boost/graph/filtered_graph.hpp>

#include"c_types/mst_rt.h"



namespace pgrouting {
Expand Down Expand Up @@ -479,7 +491,191 @@ class Pgr_dijkstra {
std::ostringstream log;
//@}
};
namespace functions {

template <class G>
class ShortestPath_tree{
typedef typename G::V V;
typedef typename G::E E;
typedef typename G::B_G B_G;


public:
std::deque<MST_rt> get_depths(
G&,
std::deque<Path>);


private:
/* Functions */

template <typename T>
std::deque<MST_rt> get_results(
T,
int64_t,
const G&);

std::deque<MST_rt> dfs_order(
const G&,
int64_t);

void get_edges_from_path(
const G&,
const Path);


private:
/* Member */

struct InSpanning {
std::set<E> edges;
bool operator()(E e) const { return edges.count(e); }
void clear() { edges.clear(); }
} m_spanning_tree;
};


template <class G>
template <typename T>
std::deque<MST_rt>
ShortestPath_tree<G>::get_results(
T order,
int64_t p_root,
const G &graph) {
std::deque<MST_rt> results;

std::vector<double> agg_cost(graph.num_vertices(), 0);
std::vector<int64_t> depth(graph.num_vertices(), 0);
int64_t root(p_root);

for (const auto edge : order) {
auto u = graph.source(edge);
auto v = graph.target(edge);
if (depth[u] == 0 && depth[v] != 0) {
std::swap(u, v);
}

if (depth[u] == 0 && depth[v] == 0) {
if (graph[u].id != root) std::swap(u, v);
if (!p_root && graph[u].id > graph[v].id) std::swap(u, v);

root = p_root? p_root: graph[u].id;
depth[u] = -1;
results.push_back({
root,
0,
graph[u].id,
-1,
0.0,
0.0 });
}
agg_cost[v] = agg_cost[u] + graph[edge].cost;
depth[v] = depth[u] == -1? 1 : depth[u] + 1;

if (graph[v].id < 0) depth[v] = depth[u];
if (graph[v].id > 0){
results.push_back({
root,
depth[v],
graph[v].id,
graph[edge].id,
graph[edge].cost,
agg_cost[v]
});
}
}
return results;
}

template <class G>
std::deque<MST_rt>
ShortestPath_tree<G>::dfs_order(const G &graph, int64_t root) {
boost::filtered_graph<B_G, InSpanning, boost::keep_all>
mstGraph(graph.graph, m_spanning_tree, {});

std::deque<MST_rt> results;
std::vector<E> visited_order;

using dfs_visitor = visitors::Dfs_visitor_with_root<V, E>;
if (graph.has_vertex(root)) {
/* abort in case of an interruption occurs (e.g. the query is being cancelled) */
CHECK_FOR_INTERRUPTS();
try {
boost::depth_first_search(
mstGraph,
visitor(dfs_visitor(graph.get_V(root), visited_order))
.root_vertex(graph.get_V(root)));
} catch(found_goals &) {
{}
} catch (boost::exception const& ex) {
(void)ex;
throw;
} catch (std::exception &e) {
(void)e;
throw;
} catch (...) {
throw;
}
auto result = get_results(visited_order, root, graph);
results.insert(results.end(), result.begin(), result.end());
} else {
results.push_back({root, 0, root, -1, 0.0, 0.0});
}

return results;
}


template <class G>
void
ShortestPath_tree<G>::get_edges_from_path(
const G& graph,
const Path path){
// m_spanning_tree.clear();

for (size_t i = 0; i < path.size(); i++){
auto u = graph.get_V(path[i].node);

for (size_t j = i+1; j < path.size(); j++){
auto v = graph.get_V(path[j].node);
double cost = path[j].cost;
auto edge = graph.get_edge(u, v, cost);
if(graph.target(edge) == v
&& path[i].agg_cost+cost == path[j].agg_cost
&& graph[edge].id == path[j].edge){
this->m_spanning_tree.edges.insert(edge);
}
}
}
}

template <class G>
std::deque<MST_rt>
ShortestPath_tree<G>::get_depths(
G &graph,
std::deque<Path> paths) {

std::deque<MST_rt> results;

for (const Path& path : paths) {
get_edges_from_path(graph, path);
int64_t root = path.start_id();
auto result = this->dfs_order(graph, root);

std::sort(result.begin(), result.end(),
[](const MST_rt &l, const MST_rt &r)
{return l.node < r.node;});
std::stable_sort(result.begin(), result.end(),
[](const MST_rt &l, const MST_rt &r)
{return l.agg_cost < r.agg_cost;});

results.insert(results.end(), result.begin(), result.end());
}
return results;
}


}
} // namespace pgrouting


Expand Down
6 changes: 3 additions & 3 deletions include/drivers/driving_distance/v4drivedist_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
# include <cstdint>
# include <cstddef>
using Edge_t = struct Edge_t;
using Path_rt = struct Path_rt;
using MST_rt = struct MST_rt;
#else
# include <stddef.h>
# include <stdint.h>
typedef struct Edge_t Edge_t;
typedef struct Path_rt Path_rt;
typedef struct MST_rt MST_rt;
#endif

#ifdef __cplusplus
Expand All @@ -51,7 +51,7 @@ extern "C" {
double distance,
bool directed,
bool equicost,
Path_rt** return_tuples, size_t* return_count,
MST_rt** return_tuples, size_t* return_count,
char **log_msg,
char **notice_msg,
char **err_msg);
Expand Down
1 change: 1 addition & 0 deletions sql/driving_distance/_drivingDistance.sql
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ CREATE FUNCTION _pgr_v4drivingDistance(
equicost BOOLEAN DEFAULT FALSE,
OUT seq INTEGER,
OUT start_vid BIGINT,
OUT depth BIGINT,
OUT node BIGINT,
OUT edge BIGINT,
OUT cost FLOAT,
Expand Down
2 changes: 2 additions & 0 deletions sql/driving_distance/drivingDistance.sql
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ CREATE FUNCTION pgr_v4drivingDistance(

OUT seq INTEGER,
OUT start_vid BIGINT,
OUT depth BIGINT,
OUT node BIGINT,
OUT edge BIGINT,
OUT cost FLOAT,
Expand All @@ -62,6 +63,7 @@ CREATE FUNCTION pgr_v4drivingDistance(

OUT seq INTEGER,
OUT start_vid BIGINT,
OUT depth BIGINT,
OUT node BIGINT,
OUT edge BIGINT,
OUT cost FLOAT,
Expand Down
16 changes: 11 additions & 5 deletions src/driving_distance/v4drivedist_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

#include "cpp_common/pgr_alloc.hpp"
#include "cpp_common/pgr_assert.h"

#undef WITH_TIME
#ifdef WITH_TIME
#include <ctime>
Expand Down Expand Up @@ -90,7 +89,7 @@ do_pgr_v4driving_many_to_dist(
double distance,
bool directedFlag,
bool equiCostFlag,
Path_rt **return_tuples, size_t *return_count,
MST_rt **return_tuples, size_t *return_count,
char **log_msg,
char **notice_msg,
char **err_msg) {
Expand All @@ -115,6 +114,7 @@ do_pgr_v4driving_many_to_dist(
graphType gType = directedFlag? DIRECTED: UNDIRECTED;

std::deque<Path> paths;
std::deque<MST_rt> results;
std::vector<int64_t> start_vertices(start_vertex, start_vertex + s_len);

auto vertices(pgrouting::extract_vertices(data_edges, total_edges));
Expand Down Expand Up @@ -153,6 +153,8 @@ do_pgr_v4driving_many_to_dist(
#endif
paths = pgr_v4drivingdistance(
digraph, start_vertices, distance, equiCostFlag, log);
pgrouting::functions::ShortestPath_tree<pgrouting::DirectedGraph> spt;
results = spt.get_depths(digraph, paths);
#ifdef WITH_TIME
end_timing(start_t, begin_elapsed, begin, log);
#endif
Expand Down Expand Up @@ -184,12 +186,14 @@ do_pgr_v4driving_many_to_dist(
#endif
paths = pgr_v4drivingdistance(
undigraph, start_vertices, distance, equiCostFlag, log);
pgrouting::functions::ShortestPath_tree<pgrouting::UndirectedGraph> spt;
results = spt.get_depths(undigraph, paths);
#ifdef WITH_TIME
end_timing(start_t, begin_elapsed, begin, log);
#endif
}

size_t count(count_tuples(paths));
size_t count(results.size());


if (count == 0) {
Expand All @@ -198,8 +202,10 @@ do_pgr_v4driving_many_to_dist(
return;
}
*return_tuples = pgr_alloc(count, (*return_tuples));
auto trueCount(collapse_paths(return_tuples, paths));
*return_count = trueCount;
for (size_t i = 0; i < count; i++) {
*((*return_tuples) + i) = results[i];
}
(*return_count) = count;


*log_msg = log.str().empty()?
Expand Down
Loading

0 comments on commit f2c529e

Please sign in to comment.