Skip to content

Commit

Permalink
Merge pull request #313 from squarege/yige-week-5
Browse files Browse the repository at this point in the history
GSoC 2023: Yige Huang Week 5
  • Loading branch information
squarege authored Jul 1, 2023
2 parents 919ffab + abb4497 commit 986d439
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 111 deletions.
4 changes: 3 additions & 1 deletion include/drivers/driving_distance/withPoints_dd_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
using Point_on_edge_t = struct Point_on_edge_t;
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 Point_on_edge_t Point_on_edge_t;
typedef struct Edge_t Edge_t;
typedef struct Path_rt Path_rt;
typedef struct MST_rt MST_rt;
#endif

#ifdef __cplusplus
Expand All @@ -68,7 +70,7 @@ extern "C" {

bool, bool, bool,

Path_rt**, size_t*,
MST_rt**, size_t*,
char**, char**, char **);


Expand Down
154 changes: 90 additions & 64 deletions include/driving_distance/withPointsDD.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,34 +52,33 @@ class ShortestPath_tree{


public:
std::deque<MST_rt> generate(
G &graph,
std::deque<Path> paths,
std::vector<int64_t> start_vids);
std::deque<MST_rt> get_depths(
G&,
std::deque<Path>,
bool);


private:
/* Functions */
void clear() {
m_roots.clear();
}

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

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

void get_edges_from_paths(
const G& graph,
const std::deque<Path> paths);
void get_edges_from_path(
const G&,
const Path);


private:
/* Member */
std::vector<int64_t> m_roots;
bool m_details;

struct InSpanning {
std::set<E> edges;
Expand All @@ -92,7 +91,7 @@ class ShortestPath_tree{
template <class G>
template <typename T>
std::deque<MST_rt>
ShortestPath_tree<G>::get_results(// TODO: can be simplified
ShortestPath_tree<G>::get_results(
T order,
int64_t p_root,
const G &graph) {
Expand All @@ -110,6 +109,7 @@ ShortestPath_tree<G>::get_results(// TODO: can be simplified
}

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;
Expand All @@ -122,84 +122,110 @@ ShortestPath_tree<G>::get_results(// TODO: can be simplified
0.0,
0.0 });
}

agg_cost[v] = agg_cost[u] + graph[edge].cost;
depth[v] = depth[u] == -1? 1 : depth[u] + 1;

results.push_back({
root,
0,
graph[v].id,
graph[edge].id,
graph[edge].cost,
agg_cost[v]
});
if (!m_details && graph[v].id < 0) depth[v] = depth[u];
if (m_details || 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) {
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;
for (const auto root : m_roots) {
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});
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_paths(
ShortestPath_tree<G>::get_edges_from_path(
const G& graph,
const std::deque<Path> paths){

// TODO:
// Extract the corresponding edges from paths and store them in m_spanning_tree
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>::generate(
ShortestPath_tree<G>::get_depths(
G &graph,
std::deque<Path> paths,
std::vector<int64_t> start_vids) {
bool details) {
m_details = details;
std::deque<MST_rt> results;

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

return this->dfs_order(graph);
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;
}


Expand Down
1 change: 1 addition & 0 deletions sql/driving_distance/_withPointsDD.sql
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ CREATE FUNCTION _pgr_v4withPointsDD(
equicost BOOLEAN DEFAULT false,

OUT seq INTEGER,
OUT depth BIGINT,
OUT start_vid BIGINT,
OUT node BIGINT,
OUT edge BIGINT,
Expand Down
4 changes: 4 additions & 0 deletions sql/driving_distance/withPointsDD.sql
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ CREATE FUNCTION pgr_withPointsDD(
details BOOLEAN DEFAULT false,

OUT seq INTEGER,
OUT depth BIGINT,
OUT start_vid BIGINT,
OUT node BIGINT,
OUT edge BIGINT,
Expand Down Expand Up @@ -68,6 +69,7 @@ CREATE FUNCTION pgr_withPointsDD(
equicost BOOLEAN DEFAULT false,

OUT seq INTEGER,
OUT depth BIGINT,
OUT start_vid BIGINT,
OUT node BIGINT,
OUT edge BIGINT,
Expand Down Expand Up @@ -140,6 +142,7 @@ RETURNS SETOF RECORD AS
$BODY$
BEGIN
RAISE WARNING 'pgr_withpointsdd(text,text,bigint,double precision,boolean,character,boolean) is been deprecated';
RETURN QUERY
SELECT seq, node, edge, cost, agg_cost
FROM _pgr_withPointsDD(_pgr_get_statement($1), _pgr_get_statement($2), ARRAY[$3]::BIGINT[], $4, $5, $6, $7, false);
END;
Expand Down Expand Up @@ -171,6 +174,7 @@ RETURNS SETOF RECORD AS
$BODY$
BEGIN
RAISE WARNING 'pgr_withpointsdd(text,text,anyarray,double precision,boolean,character,boolean,boolean) is been deprecated';
RETURN QUERY
SELECT *
FROM _pgr_withPointsDD(_pgr_get_statement($1), _pgr_get_statement($2), $3, $4, $5, $6, $7, $8);
END;
Expand Down
31 changes: 15 additions & 16 deletions src/driving_distance/v4withPointsDD.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,11 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#include <stdbool.h>
#include "c_common/postgres_connection.h"

#include "c_types/path_rt.h"
#include "c_common/debug_macro.h"
#include "c_common/e_report.h"
#include "c_common/time_msg.h"

#include "c_common/pgdata_getters.h"
#include "c_types/mst_rt.h"

#include "drivers/withPoints/get_new_queries.h"
#include "drivers/driving_distance/withPoints_dd_driver.h"
Expand All @@ -58,9 +57,8 @@ void process(
bool details,
bool equicost,

Path_rt **result_tuples,
MST_rt **result_tuples,
size_t *result_count) {

pgr_SPI_connect();
char* log_msg = NULL;
char* notice_msg = NULL;
Expand Down Expand Up @@ -154,10 +152,10 @@ void process(
PGDLLEXPORT Datum
_pgr_v4withpointsdd(PG_FUNCTION_ARGS) {
FuncCallContext *funcctx;
TupleDesc tuple_desc;
TupleDesc tuple_desc;

/**********************************************************************/
Path_rt *result_tuples = 0;
MST_rt *result_tuples = NULL;
size_t result_count = 0;
/**********************************************************************/

Expand All @@ -181,7 +179,7 @@ _pgr_v4withpointsdd(PG_FUNCTION_ARGS) {
// equicost BOOLEAN -- DEFAULT false,


PGR_DBG("Calling driving_many_to_dist_driver");
PGR_DBG("Calling withPoints_dd_driver");
process(
text_to_cstring(PG_GETARG_TEXT_P(0)),
text_to_cstring(PG_GETARG_TEXT_P(1)),
Expand Down Expand Up @@ -214,16 +212,16 @@ _pgr_v4withpointsdd(PG_FUNCTION_ARGS) {
funcctx = SRF_PERCALL_SETUP();

tuple_desc = funcctx->tuple_desc;
result_tuples = (Path_rt*) funcctx->user_fctx;
result_tuples = (MST_rt*) funcctx->user_fctx;

if (funcctx->call_cntr < funcctx->max_calls) {
HeapTuple tuple;
Datum result;
Datum *values;
bool* nulls;
Datum *values;
bool* nulls;

/**********************************************************************/
size_t numb = 6;
size_t numb = 7;
values = palloc(numb * sizeof(Datum));
nulls = palloc(numb * sizeof(bool));

Expand All @@ -233,11 +231,12 @@ _pgr_v4withpointsdd(PG_FUNCTION_ARGS) {
}

values[0] = Int32GetDatum(funcctx->call_cntr + 1);
values[1] = Int64GetDatum(result_tuples[funcctx->call_cntr].start_id);
values[2] = Int64GetDatum(result_tuples[funcctx->call_cntr].node);
values[3] = Int64GetDatum(result_tuples[funcctx->call_cntr].edge);
values[4] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost);
values[5] = Float8GetDatum(result_tuples[funcctx->call_cntr].agg_cost);
values[1] = Int64GetDatum(result_tuples[funcctx->call_cntr].depth);
values[2] = Int64GetDatum(result_tuples[funcctx->call_cntr].from_v);
values[3] = Int64GetDatum(result_tuples[funcctx->call_cntr].node);
values[4] = Int64GetDatum(result_tuples[funcctx->call_cntr].edge);
values[5] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost);
values[6] = Float8GetDatum(result_tuples[funcctx->call_cntr].agg_cost);

/**********************************************************************/

Expand Down
Loading

0 comments on commit 986d439

Please sign in to comment.