Skip to content

Commit

Permalink
Merge pull request #297 from AbhinJ/Abhinj-week-2
Browse files Browse the repository at this point in the history
GSoC 2023 - Abhinav Jain Week 2
  • Loading branch information
AbhinJ committed Jun 12, 2023
2 parents 3315ddf + 08cee7d commit 44a056c
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 43 deletions.
18 changes: 9 additions & 9 deletions include/drivers/yen/withPoints_ksp_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ 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 II_t_rt = struct II_t_rt;
#else
# include <stddef.h>
# include <stdint.h>
Expand All @@ -50,18 +51,17 @@ typedef struct Path_rt Path_rt;
extern "C" {
#endif

// CREATE OR REPLACE FUNCTION pgr_withPointKsp(
// edges_sql TEXT,
// points_sql TEXT,
// start_pid BIGINT,
// end_pid BIGINT,
// directed BOOLEAN DEFAULT true,
int do_pgr_withPointsKsp(
Edge_t *edges, size_t total_edges,
Point_on_edge_t *points, size_t total_points,
Edge_t *edges_of_points, size_t total_edges_of_points,
Edge_t *edges, size_t total_edges,
Point_on_edge_t *points_p, size_t total_points,
Edge_t *edges_of_points, size_t total_edges_of_points,
#if 0
int64_t start_pid,
int64_t end_pid,
#endif
II_t_rt *combinationsArr, size_t total_combinations,
int64_t *start_pidsArr, size_t size_start_pidsArr,
int64_t *end_pidsArr, size_t size_end_pidsArr,
size_t k,
bool directed,
bool heap_paths,
Expand Down
32 changes: 32 additions & 0 deletions include/yen/pgr_ksp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,38 @@ class Pgr_ksp : public Pgr_messages {


} // namespace yen

namespace algorithms {

template <class G>
std::deque<Path> Yen(
G &graph,
const std::map<int64_t, std::set<int64_t>> &combinations,
size_t k,
bool heap_paths){
std::deque<Path> paths;
pgrouting::yen::Pgr_ksp<G> fn_yen;

for (const auto &c : combinations) {
if (!graph.has_vertex(c.first))
continue;

for (const auto &destination : c.second) {
if (!graph.has_vertex(destination))
continue;

fn_yen.clear();
auto result_path = fn_yen.Yen(graph, c.first, destination, k, heap_paths);
paths.insert(paths.end(), result_path.begin(), result_path.end());
}
}

return paths;
}

} // namespace algorithms

} // namespace pgrouting


#endif // INCLUDE_YEN_PGR_KSP_HPP_
92 changes: 69 additions & 23 deletions src/ksp/withPoints_ksp.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ void
process(
char* edges_sql,
char* points_sql,
int64_t start_pid,
int64_t end_pid,
char* combinations_sql,
ArrayType *starts,
ArrayType *ends,

int p_k,

bool directed,
Expand All @@ -74,57 +76,85 @@ process(
}

pgr_SPI_connect();

char* log_msg = NULL;
char* notice_msg = NULL;
char* err_msg = NULL;

Edge_t *edges = NULL;
size_t total_edges = 0;

Point_on_edge_t *points = NULL;
size_t total_points = 0;

Edge_t *edges_of_points = NULL;
size_t total_edges_of_points = 0;

int64_t* start_pidsArr = NULL;
size_t size_start_pidsArr = 0;

int64_t* end_pidsArr = NULL;
size_t size_end_pidsArr = 0;

II_t_rt *combinationsArr = NULL;
size_t total_combinations = 0;

pgr_get_points(points_sql, &points, &total_points, &err_msg);
throw_error(err_msg, points_sql);

char *edges_of_points_query = NULL;
char *edges_no_points_query = NULL;

get_new_queries(
edges_sql, points_sql,
&edges_of_points_query,
&edges_no_points_query);


Edge_t *edges_of_points = NULL;
size_t total_edges_of_points = 0;
pgr_get_edges(edges_of_points_query, &edges_of_points, &total_edges_of_points, true, false, &err_msg);
throw_error(err_msg, edges_of_points_query);

Edge_t *edges = NULL;
size_t total_edges = 0;
pgr_get_edges(edges_no_points_query, &edges, &total_edges, true, false, &err_msg);
throw_error(err_msg, edges_no_points_query);


if (starts && ends) {
start_pidsArr = pgr_get_bigIntArray(&size_start_pidsArr, starts, false, &err_msg);
throw_error(err_msg, "While getting start pids");
end_pidsArr = pgr_get_bigIntArray(&size_end_pidsArr, ends, false, &err_msg);
throw_error(err_msg, "While getting end pids");
} else if (combinations_sql) {
pgr_get_combinations(combinations_sql, &combinationsArr, &total_combinations, &err_msg);
throw_error(err_msg, combinations_sql);
}

PGR_DBG("freeing allocated memory not used anymore");
pfree(edges_of_points_query);
pfree(edges_no_points_query);

if ((total_edges + total_edges_of_points) == 0) {
PGR_DBG("No edges found");
(*result_count) = 0;
(*result_tuples) = NULL;
if (end_pidsArr) pfree(end_pidsArr);
if (start_pidsArr) pfree(start_pidsArr);
if (combinationsArr) pfree(combinationsArr);
pgr_SPI_finish();
return;
}

if (total_combinations == 0 && (size_start_pidsArr== 0 || size_end_pidsArr == 0)) {
if (edges) pfree(edges);
pgr_SPI_finish();
return;
}

PGR_DBG("Starting processing");
clock_t start_t = clock();

do_pgr_withPointsKsp(
edges,
total_edges,
points,
total_points,
edges_of_points,
total_edges_of_points,
start_pid,
end_pid,
edges, total_edges,
points, total_points,
edges_of_points, total_edges_of_points,
combinationsArr, total_combinations,
start_pidsArr, size_start_pidsArr,
end_pidsArr, size_end_pidsArr,

k,

directed,
Expand Down Expand Up @@ -191,19 +221,35 @@ PGDLLEXPORT Datum _pgr_withpointsksp(PG_FUNCTION_ARGS) {

PGR_DBG("Calling process");
PGR_DBG("initial driving side:%s",
text_to_cstring(PG_GETARG_TEXT_P(7)));
process(
text_to_cstring(PG_GETARG_TEXT_P(8)));
if(PG_NARGS() == 9){
process(
text_to_cstring(PG_GETARG_TEXT_P(0)),
text_to_cstring(PG_GETARG_TEXT_P(1)),
PG_GETARG_INT64(2),
PG_GETARG_INT64(3),
NULL,
PG_GETARG_ARRAYTYPE_P(2),
PG_GETARG_ARRAYTYPE_P(3),
PG_GETARG_INT32(4),
PG_GETARG_BOOL(5),
PG_GETARG_BOOL(6),
text_to_cstring(PG_GETARG_TEXT_P(7)),
PG_GETARG_BOOL(8),
&result_tuples,
&result_count);
} else/* (PG_NARGS() == 7) */{
process(
text_to_cstring(PG_GETARG_TEXT_P(0)),
text_to_cstring(PG_GETARG_TEXT_P(1)),
text_to_cstring(PG_GETARG_TEXT_P(2)),
NULL, NULL,
PG_GETARG_INT32(3),
PG_GETARG_BOOL(4),
PG_GETARG_BOOL(5),
text_to_cstring(PG_GETARG_TEXT_P(6)),
PG_GETARG_BOOL(7),
&result_tuples,
&result_count);
}


funcctx->max_calls = result_count;
Expand Down
48 changes: 37 additions & 11 deletions src/ksp/withPoints_ksp_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@ Function's developer:
Copyright (c) 2015 Celia Virginia Vergara Castillo
Mail:
Copyright (c) 2023 Abhinav Jain
Mail: this.abhinav AT gmail.com
------
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
Expand All @@ -34,12 +39,14 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#include <deque>
#include <vector>

#include "yen/pgr_ksp.hpp"

#include "withPoints/pgr_withPoints.hpp"
#include "c_types/ii_t_rt.h"
#include "cpp_common/combinations.h"
#include "cpp_common/pgr_alloc.hpp"
#include "cpp_common/pgr_assert.h"

#include "yen/pgr_ksp.hpp"
#include "withPoints/pgr_withPoints.hpp"

using pgrouting::yen::Pgr_ksp;

// CREATE OR REPLACE FUNCTION pgr_withPointsKSP(
Expand All @@ -55,8 +62,14 @@ do_pgr_withPointsKsp(
Edge_t *edges, size_t total_edges,
Point_on_edge_t *points_p, size_t total_points,
Edge_t *edges_of_points, size_t total_edges_of_points,
#if 0
int64_t start_pid,
int64_t end_pid,
#endif
II_t_rt *combinationsArr, size_t total_combinations,
int64_t *start_pidsArr, size_t size_start_pidsArr,
int64_t *end_pidsArr, size_t size_end_pidsArr,

size_t k,
bool directed,
bool heap_paths,
Expand All @@ -76,12 +89,14 @@ do_pgr_withPointsKsp(
std::ostringstream err;
std::ostringstream notice;
try {
pgassert(total_edges != 0);
pgassert(!(*log_msg));
pgassert(!(*notice_msg));
pgassert(!(*err_msg));
pgassert(!(*return_tuples));
pgassert(*return_count == 0);
pgassert(total_edges != 0);
pgassert(total_combinations != 0 || (size_start_pidsArr != 0 && size_end_pidsArr != 0));


pgrouting::Pg_points_graph pg_graph(
std::vector<Point_on_edge_t>(
Expand All @@ -102,7 +117,7 @@ do_pgr_withPointsKsp(
return -1;
}


#if 0
int64_t start_vid(start_pid);
int64_t end_vid(end_pid);

Expand All @@ -111,10 +126,16 @@ do_pgr_withPointsKsp(
log << "driving_side" << driving_side << "\n";
log << "start_vid" << start_vid << "\n";
log << "end_vid" << end_vid << "\n";
#endif

graphType gType = directed? DIRECTED: UNDIRECTED;

std::deque< Path > paths;

auto combinations = total_combinations?
pgrouting::utilities::get_combinations(combinationsArr, total_combinations)
: pgrouting::utilities::get_combinations(start_pidsArr, size_start_pidsArr, end_pidsArr, size_end_pidsArr);

auto vertices(pgrouting::extract_vertices(edges, total_edges));
vertices = pgrouting::extract_vertices(vertices, pg_graph.new_edges());

Expand All @@ -125,27 +146,32 @@ do_pgr_withPointsKsp(
log << "\n";

if (directed) {
log << "Working with directed Graph\n";
pgrouting::DirectedGraph digraph(vertices, gType);
digraph.insert_edges(edges, total_edges);
digraph.insert_edges(pg_graph.new_edges());

#if 0
log << "graph after inserting edges\n";
log << digraph << "\n";

digraph.insert_edges(pg_graph.new_edges());
log << "graph after inserting new edges\n";
log << digraph << "\n";

Pgr_ksp< pgrouting::DirectedGraph > fn_yen;
paths = fn_yen.Yen(digraph, start_vid, end_vid, k, heap_paths);
#endif

paths = pgrouting::algorithms::Yen(digraph, combinations, k, heap_paths);
// pgassert(true==false);
} else {
#if 0
log << "Working with undirected Graph\n";
#endif
pgrouting::UndirectedGraph undigraph(gType);
undigraph.insert_edges(edges, total_edges);
undigraph.insert_edges(pg_graph.new_edges());

#if 0
Pgr_ksp< pgrouting::UndirectedGraph > fn_yen;
paths = fn_yen.Yen(undigraph, start_vid, end_vid, k, heap_paths);
#endif
paths = pgrouting::algorithms::Yen(undigraph, combinations, k, heap_paths);
}


Expand Down

0 comments on commit 44a056c

Please sign in to comment.