pgr_aStarCostMatrix
¶
pgr_aStarCostMatrix
- Calculates the a cost matrix using pgr_aStar.
Availability
- Version 3.0.0
- Official function
- Version 2.4.0
- New proposed function
Description¶
The main characteristics are:
- Using internaly the pgr_aStar algorithm
- Returns a cost matrix.
- No ordering is performed
- let v and u are nodes on the graph:
- when there is no path from v to u:
- no corresponding row is returned
- cost from v to u is \(\inf\)
- when \(v = u\) then
- no corresponding row is returned
- cost from v to u is \(0\)
- when there is no path from v to u:
- When the graph is undirected the cost matrix is symmetric
Signatures¶
Summary
pgr_aStarCostMatrix(Edges SQL, start vids [, directed] [, heuristic] [, factor] [, epsilon]) RETURNS SET OF (start_vid, end_vid, agg_cost)
Example: | Symmetric cost matrix for vertices \(\{5, 6, 10, 15\}\) on an undirected graph using heuristic \(2\) |
---|
SELECT * FROM pgr_aStarCostMatrix(
'SELECT id, source, target, cost, reverse_cost, x1, y1, x2, y2 FROM edges',
(SELECT array_agg(id) FROM vertices WHERE id IN (5, 6, 10, 15)),
directed => false, heuristic => 2);
start_vid | end_vid | agg_cost
-----------+---------+----------
5 | 6 | 1
5 | 10 | 2
5 | 15 | 3
6 | 5 | 1
6 | 10 | 1
6 | 15 | 2
10 | 5 | 2
10 | 6 | 1
10 | 15 | 1
15 | 5 | 3
15 | 6 | 2
15 | 10 | 1
(12 rows)
Parameters¶
Column | Type | Description |
---|---|---|
Edges SQL | TEXT |
Edges SQL as described below |
start vids | ARRAY[BIGINT] |
Array of identifiers of starting vertices. |
Optional parameters¶
Column | Type | Default | Description |
---|---|---|---|
directed |
BOOLEAN |
true |
|
aStar optional Parameters¶
Parameter | Type | Default | Description |
---|---|---|---|
heuristic |
INTEGER |
5 | Heuristic number. Current valid values 0~5.
|
factor |
FLOAT |
1 |
For units manipulation. \(factor > 0\). |
epsilon |
FLOAT |
1 |
For less restricted results. \(epsilon >= 1\). |
See heuristics available and factor handling.
Inner Queries¶
Edges SQL¶
Parameter | Type | Default | Description |
---|---|---|---|
id |
ANY-INTEGER | Identifier of the edge. | |
source |
ANY-INTEGER | Identifier of the first end point vertex of the edge. | |
target |
ANY-INTEGER | Identifier of the second end point vertex of the edge. | |
cost |
ANY-NUMERICAL | Weight of the edge (
|
|
reverse_cost |
ANY-NUMERICAL | -1 | Weight of the edge (
|
x1 |
ANY-NUMERICAL | X coordinate of source vertex. |
|
y1 |
ANY-NUMERICAL | Y coordinate of source vertex. |
|
x2 |
ANY-NUMERICAL | X coordinate of target vertex. |
|
y2 |
ANY-NUMERICAL | Y coordinate of target vertex. |
Where:
ANY-INTEGER: | SMALLINT , INTEGER , BIGINT |
---|---|
ANY-NUMERICAL: | SMALLINT , INTEGER , BIGINT , REAL , FLOAT |
Result Columns¶
Set of (start_vid, end_vid, agg_cost)
Column | Type | Description |
---|---|---|
start_vid |
BIGINT |
Identifier of the starting vertex. |
end_vid |
BIGINT |
Identifier of the ending vertex. |
agg_cost |
FLOAT |
Aggregate cost from start_vid to end_vid . |
Additional Examples¶
Example: | Use with pgr_TSP |
---|
SELECT * FROM pgr_TSP(
$$
SELECT * FROM pgr_aStarCostMatrix(
'SELECT id, source, target, cost, reverse_cost, x1, y1, x2, y2 FROM edges',
(SELECT array_agg(id) FROM vertices WHERE id IN (5, 6, 10, 15)),
directed=> false, heuristic => 2)
$$,
randomize => false
);
NOTICE: pgr_TSP no longer solving with simulated annaeling
HINT: Ignoring annaeling parameters
seq | node | cost | agg_cost
-----+------+------+----------
1 | 5 | 0 | 0
2 | 6 | 1 | 1
3 | 10 | 1 | 2
4 | 15 | 1 | 3
5 | 5 | 3 | 6
(5 rows)
See Also¶
- A* - Family of functions
- Cost Matrix - Category
- Traveling Sales Person - Family of functions
- Sample Data
Indices and tables