Skip to content

Commit

Permalink
(pgtap/trsp) refining compare with pgr_dijkstra
Browse files Browse the repository at this point in the history
  • Loading branch information
cvvergara committed Oct 31, 2024
1 parent 0b98d8e commit 4175f00
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 24 deletions.
6 changes: 3 additions & 3 deletions pgtap/trsp/trsp/edge_cases/compare_dijkstra.pg
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ BEGIN;
SET extra_float_digits = -3;
UPDATE edges SET cost = sign(cost) + 0.001 * id * id, reverse_cost = sign(reverse_cost) + 0.001 * id * id;

SELECT CASE WHEN min_version('3.4.0') THEN plan(36 + 324 * 4) ELSE plan(6) END;
SELECT CASE WHEN min_version('3.4.0') THEN plan(615 + 602 + 646 * 4) ELSE plan(6) END;

PREPARE with_reverse_cost AS SELECT id, source, target, cost, reverse_cost from edges ORDER BY id;
PREPARE no_reverse_cost AS SELECT id, source, target, cost from edges ORDER BY id;
Expand All @@ -30,8 +30,8 @@ PREPARE unrelated1 AS SELECT * FROM restrictions WHERE id > 7;
PREPARE unrelated2 AS SELECT 1 AS id, 100::float AS cost, ARRAY[33, 32, 25] AS path;


SELECT compare_trsp_dijkstra_new(18, true, 'related');
SELECT compare_trsp_dijkstra_new(18, false, 'related');
SELECT compare_trsp_dijkstra_new(18, true, 'related'); --615
SELECT compare_trsp_dijkstra_new(18, false, 'related'); --602

SELECT compare_trsp_dijkstra_new(18, true, 'unrelated1');
SELECT compare_trsp_dijkstra_new(18, false, 'unrelated1');
Expand Down
67 changes: 46 additions & 21 deletions tools/testers/trsp_tests.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

CREATE OR REPLACE FUNCTION compare_trsp_dijkstra_new(cant INTEGER, flag boolean, restrictions_sql TEXT)
RETURNS SETOF TEXT AS
$BODY$
Expand All @@ -7,6 +6,7 @@ DECLARE
dijkstra_sql TEXT;
trsp_sql TEXT;
directed TEXT;
params TEXT;
msg TEXT;
BEGIN
IF NOT min_version('3.4.0') THEN
Expand All @@ -17,27 +17,52 @@ BEGIN
directed = 'Undirected';
IF flag THEN directed = 'Directed'; END IF;

FOR i IN 1.. cant BY 2 LOOP
FOR i IN 1..cant LOOP
FOR j IN 1..cant LOOP
-- For related restrictions only when source and target are equal it is garanteed that the results are the same as
-- dijkstra
IF (restrictions_sql = 'related') THEN CONTINUE WHEN i != j; END IF;

dijkstra_sql := 'SELECT seq, node, edge, cost::text FROM pgr_dijkstra($$with_reverse_cost$$, '
|| i || ', ' || j || ', ' || flag || ')';
trsp_sql := 'SELECT seq, node, edge, cost::text from pgr_trsp($$with_reverse_cost$$, $$' || restrictions_sql ||'$$, '
|| i || ', ' || j || ', ' || flag || ')';
msg := restrictions_sql || '-' || k || '-1 ' || directed || ', with reverse_cost from ' || i || ' to ' || j;
RETURN QUERY SELECT set_eq(trsp_sql, dijkstra_sql, msg);

dijkstra_sql := 'SELECT seq, node, edge, cost::text FROM pgr_dijkstra($$no_reverse_cost$$, '
|| i || ', ' || j || ', ' || flag || ')';
trsp_sql := 'SELECT seq, node, edge, cost::text from pgr_trsp($$no_reverse_cost$$, $$' || restrictions_sql ||'$$, '
|| i || ', ' || j || ', ' || flag || ')';
msg := restrictions_sql || '-' || k || '-2 ' || directed || ', NO reverse_cost from ' || i || ' to ' || j;
RETURN QUERY SELECT set_eq(trsp_sql, dijkstra_sql, msg);

k := k + 1;

params = ' ' || i || ', ' || j || ', ' || flag;

-- skipping results comparable with pgr_dijkstraVia
IF (restrictions_sql = 'related' AND flag = true) THEN CONTINUE WHEN
i IN (1,3,5,6,7) AND j IN (12,17);
END IF;
IF (restrictions_sql = 'related' AND flag = false) THEN CONTINUE WHEN
(i=1 AND j IN (8,9,12,17)) OR
(i=3 AND j IN (8,9,12,17)) OR
(i IN (5,6) AND j IN (1,3)) OR
(i=7 AND j IN (12,17));
END IF;


IF (restrictions_sql = 'related' AND (
(flag = false AND (i IN (10,15) AND J IN (1,3))) OR
(flag = true AND (
(i IN (5,6,10,15) AND J IN (1,3)) OR
(i IN (5,6,7,8,9,11) AND J IN (10,15)) OR
(i IN (10) AND J IN (15))
))
))
THEN
k := k + 1;
ELSE
dijkstra_sql := 'SELECT seq, node, edge FROM pgr_dijkstra($$with_reverse_cost$$, ' || params || ')';
trsp_sql := 'SELECT seq, node, edge FROM pgr_trsp($$with_reverse_cost$$, $$' || restrictions_sql ||'$$, ' || params || ')';
msg := restrictions_sql || '-' || params || '-reverse_cost ' || dijkstra_sql || '; ' || trsp_sql;
RETURN QUERY SELECT set_eq(trsp_sql, dijkstra_sql, msg);
END IF;

IF (restrictions_sql = 'related' AND flag = false AND
((i IN (1,3,5,6,7,8,9,10,11,12) AND J = 15) OR (i IN (5,6) AND J IN (12,17)))
)
THEN
k := k + 1;
ELSE
dijkstra_sql := 'SELECT seq, node, edge FROM pgr_dijkstra($$no_reverse_cost$$, ' || params || ')';
trsp_sql := 'SELECT seq, node, edge from pgr_trsp($$no_reverse_cost$$, $$' || restrictions_sql ||'$$, ' || params || ')';
msg := restrictions_sql || '-' || params || '-no_reverse_cost ' || dijkstra_sql || '; ' || trsp_sql;
RETURN QUERY SELECT set_eq(trsp_sql, dijkstra_sql, msg);
END IF;


END LOOP;
END LOOP;
Expand Down

0 comments on commit 4175f00

Please sign in to comment.