Skip to content

Commit

Permalink
bdAstar code simplification (#2522)
Browse files Browse the repository at this point in the history
* [bdAstar] code simplification of bdAstar
  • Loading branch information
cvvergara authored Jun 6, 2023
1 parent 81bcd0c commit 816991b
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 228 deletions.
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ pgRouting 3.6.0 Release Notes
simplification.
* `2521 <https://github.com/pgRouting/pgrouting/pull/2521>` Dijkstra code
simplification.
* `2522 <https://github.com/pgRouting/pgrouting/pull/2522>` bdAstar code
simplification.

**Documentation**

Expand Down
2 changes: 2 additions & 0 deletions doc/src/release_notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ pgRouting 3.6.0 Release Notes
simplification.
* `2521 <https://github.com/pgRouting/pgrouting/pull/2521>` Dijkstra code
simplification.
* `2522 <https://github.com/pgRouting/pgrouting/pull/2522>` bdAstar code
simplification.

.. rubric:: Documentation

Expand Down
57 changes: 43 additions & 14 deletions include/bdAstar/pgr_bdAstar.hpp → include/bdAstar/bdAstar.hpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
/*PGR-GNU*****************************************************************
File: pgr_bdAstar.hpp
Generated with Template by:
Copyright (c) 2015 pgRouting developers
Mail: project@pgrouting.org
Function's developer:
Copyright (c) 2015 Celia Virginia Vergara Castillo
Mail:
Mail: vicky at erosion.dev
------
Expand All @@ -32,25 +31,22 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#pragma once


#include "cpp_common/pgr_bidirectional.hpp"

#include <string>
#include <queue>
#include <utility>
#include <vector>
#include <limits>
#include <functional>
#include <map>
#include <set>
#include <deque>

#include "cpp_common/pgr_bidirectional.hpp"
#include "cpp_common/basePath_SSEC.hpp"

namespace pgrouting {

namespace bidirectional {

template < typename G >
template <typename G>
class Pgr_bdAstar : public Pgr_bidirectional<G> {
typedef typename Pgr_bidirectional<G>::V V;
typedef typename Pgr_bidirectional<G>::E E;
typedef typename Pgr_bidirectional<G>::Cost_Vertex_pair Cost_Vertex_pair;
using V = typename Pgr_bidirectional<G>::V;
using E = typename Pgr_bidirectional<G>::E;
using Cost_Vertex_pair = typename Pgr_bidirectional<G>::Cost_Vertex_pair;

using Pgr_bidirectional<G>::graph;
using Pgr_bidirectional<G>::m_log;
Expand Down Expand Up @@ -192,6 +188,39 @@ class Pgr_bdAstar : public Pgr_bidirectional<G> {
};

} // namespace bidirectional


namespace algorithms {

template <class G>
std::deque<Path> bdastar(
G &graph,
const std::map<int64_t, std::set<int64_t>> &combinations,
int heuristic,
double factor,
double epsilon,
bool only_cost) {
std::deque<Path> paths;
pgrouting::bidirectional::Pgr_bdAstar<G> fn_bdAstar(graph);

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_bdAstar.clear();

paths.push_back(fn_bdAstar.pgr_bdAstar(
graph.get_V(c.first), graph.get_V(destination),
heuristic, factor, epsilon, only_cost));
}
}

return paths;
}

} // namespace algorithms
} // namespace pgrouting

#endif // INCLUDE_BDASTAR_PGR_BDASTAR_HPP_
2 changes: 1 addition & 1 deletion include/drivers/bdAstar/bdAstar_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ typedef struct II_t_rt II_t_rt;
extern "C" {
#endif

void do_pgr_bdAstar(
void pgr_do_bdAstar(
Edge_xy_t *data_edges,
size_t total_edges,

Expand Down
49 changes: 14 additions & 35 deletions src/bdAstar/bdAstar.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Mail: project@pgrouting.org
Function's developer:
Copyright (c) 2015 Celia Virginia Vergara Castillo
Mail:
Mail:vicky at erosion.dev
------
Expand All @@ -34,13 +34,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#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_common/check_parameters.h"



#include "drivers/astar/astar_driver.h"
#include "drivers/bdAstar/bdAstar_driver.h"

PGDLLEXPORT Datum _pgr_bdastar(PG_FUNCTION_ARGS);
Expand Down Expand Up @@ -73,6 +69,9 @@ process(char* edges_sql,
int64_t* end_vidsArr = NULL;
size_t size_end_vidsArr = 0;

Edge_xy_t *edges = NULL;
size_t total_edges = 0;

II_t_rt *combinations = NULL;
size_t total_combinations = 0;

Expand All @@ -86,12 +85,8 @@ process(char* edges_sql,
throw_error(err_msg, combinations_sql);
}

Edge_xy_t *edges = NULL;
size_t total_edges = 0;

pgr_get_edges_xy(edges_sql, &edges, &total_edges, true, &err_msg);
throw_error(err_msg, edges_sql);
PGR_DBG("Total %ld edges in query:", total_edges);

if (total_edges == 0) {
PGR_DBG("No edges found");
Expand All @@ -101,36 +96,34 @@ process(char* edges_sql,
return;
}

PGR_DBG("Starting processing");
clock_t start_t = clock();
do_pgr_bdAstar(
pgr_do_bdAstar(
edges, total_edges,

combinations, total_combinations,

start_vidsArr, size_start_vidsArr,
end_vidsArr, size_end_vidsArr,

directed,
heuristic,
factor,
epsilon,
only_cost,

result_tuples,
result_count,
result_tuples, result_count,
&log_msg,
&notice_msg,
&err_msg);

if (only_cost) {
time_msg("pgr_bdAstarCost()", start_t, clock());
time_msg("pgr_bdAstarCost", start_t, clock());
} else {
time_msg("pgr_bdAstar()", start_t, clock());
time_msg("pgr_bdAstar", start_t, clock());
}

if (err_msg && (*result_tuples)) {
pfree(*result_tuples);
(*result_count) = 0;
(*result_tuples) = NULL;
(*result_count) = 0;
}

pgr_global_report(log_msg, notice_msg, err_msg);
Expand All @@ -139,6 +132,8 @@ process(char* edges_sql,
if (notice_msg) pfree(notice_msg);
if (err_msg) pfree(err_msg);
if (edges) pfree(edges);
if (start_vidsArr) pfree(start_vidsArr);
if (end_vidsArr) pfree(end_vidsArr);

pgr_SPI_finish();
}
Expand All @@ -148,7 +143,7 @@ _pgr_bdastar(PG_FUNCTION_ARGS) {
FuncCallContext *funcctx;
TupleDesc tuple_desc;

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

if (SRF_IS_FIRSTCALL()) {
Expand All @@ -174,7 +169,6 @@ _pgr_bdastar(PG_FUNCTION_ARGS) {
PG_GETARG_BOOL(7),
&result_tuples,
&result_count);

} else if (PG_NARGS() == 7) {
/*
* combinations
Expand All @@ -184,7 +178,6 @@ _pgr_bdastar(PG_FUNCTION_ARGS) {
text_to_cstring(PG_GETARG_TEXT_P(1)),
NULL,
NULL,

PG_GETARG_BOOL(2),
PG_GETARG_INT32(3),
PG_GETARG_FLOAT8(4),
Expand All @@ -195,10 +188,7 @@ _pgr_bdastar(PG_FUNCTION_ARGS) {
}




funcctx->max_calls = result_count;

funcctx->user_fctx = result_tuples;
if (get_call_result_type(fcinfo, NULL, &tuple_desc)
!= TYPEFUNC_COMPOSITE)
Expand All @@ -223,20 +213,10 @@ _pgr_bdastar(PG_FUNCTION_ARGS) {
size_t call_cntr = funcctx->call_cntr;


/**********************************************************************
OUT seq INTEGER,
OUT path_seq INTEGER,
OUT node BIGINT,
OUT edge BIGINT,
OUT cost FLOAT,
OUT agg_cost FLOAT
*********************************************************************/

size_t numb = 8;
values = palloc(numb * sizeof(Datum));
nulls = palloc(numb * sizeof(bool));


size_t i;
for (i = 0; i < numb; ++i) {
nulls[i] = false;
Expand All @@ -259,4 +239,3 @@ _pgr_bdastar(PG_FUNCTION_ARGS) {
SRF_RETURN_DONE(funcctx);
}
}

Loading

0 comments on commit 816991b

Please sign in to comment.