ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
GridGraph.h File Reference

The graph of a grid map, beyond the book [see Graph.h and Grid.h]: the cells of a \( \mathit{rows} \times \mathit{cols} \) map, the cell \( ( r , c ) \) the node \( u = r \cdot \mathit{cols} + c \), each cell free or blocked, and the moves to the four sides of a cell or also to its four corners. More...

#include "ADS/Contract.h"
#include "ADS/Modification.h"
#include "ADS/Portable.h"
#include "ADS/graph/Graph.h"
#include "ADS/graph/Grid.h"
#include <cstddef>
#include <istream>
#include <optional>
#include <ranges>
#include <type_traits>
#include <vector>
#include "ADS/graph/impl/GridGraph.tpp"

Go to the source code of this file.

Classes

class  ADS::GridGraph< W, C, Policy >
 the implicit graph of a grid map, the cells of cost \( C \) and the weights of the moves of type \( W \), whose changes of the costs are told through the subject policy Policy [see Modification.h] More...

Namespaces

namespace  ADS

Enumerations

enum class  ADS::GridMoves { ADS::GridMoves::FOUR , ADS::GridMoves::EIGHT }
 the moves of a GridGraph More...
enum class  ADS::Corners { ADS::Corners::CUT , ADS::Corners::NO_SQUEEZE , ADS::Corners::NO_CUT }
 when a move to a corner of a cell is allowed, the corner free More...
enum class  ADS::MoveCost { ADS::MoveCost::ENTERED , ADS::MoveCost::LEFT }
 which cell of a move of a GridGraph weighs it More...

Functions

template<class W, class C, class P>
std::vector< int > ADS::gridDistances (const GridGraph< W, C, P > &g, const std::vector< int > &S)
 the distance in moves of each cell of the grid \( g \) from the nearest of the nodes \( S \), INF if none reaches it or if the cell is blocked; \( D[ u ] \) for the cell \( u \)
template<class W = int>
W ADS::manhattan (GridCell a, GridCell b, W straight=W(1))
 the distance of Manhattan between the cells \( a \) and \( b \), \( \mathit{straight} \cdot ( | \Delta r | + | \Delta c | ) \), the least cost of a path of moves to the sides of cost \( \mathit{straight} \)
template<class W>
W ADS::octile (GridCell a, GridCell b, W straight, W diagonal)
 the octile distance between the cells \( a \) and \( b \), \( \mathit{straight} \cdot ( M - m ) + \mathit{diagonal} \cdot m \) with \( M \) and \( m \) the larger and the smaller of \( | \Delta r | \) and \( | \Delta c | \), the least cost of a path of moves to the sides and to the corners
template<class W = int>
W ADS::chebyshev (GridCell a, GridCell b, W straight=W(1))
 the distance of Chebyshev between the cells \( a \) and \( b \), \( \mathit{straight} \cdot \max\{ | \Delta r | , | \Delta c | \} \), the octile distance when a move to a corner costs as one to a side (the king)
template<class W, class C, class P>
W ADS::gridHeuristic (const GridGraph< W, C, P > &g, int u, int t)
 a lower bound on the cost of a path from \( u \) to \( t \) in \( g \), consistent, for the A* of Hart, Nilsson and Raphael: the least cost \( \gamma_{\mathrm{min}} \) of a cell times manhattan() for four moves and times octile() for eight, the latter with \( \min\{ \mathit{diagonal} , 2 \, \mathit{straight} \} \) as the cost of a corner; the same bound under both values of MoveCost, since a move weighs at least \( \gamma_{\mathrm{min}} \) times its unit cost whichever of its two cells pays (see Graphs on grid maps)
template<class W = double, class C = unsigned char>
GridGraph< W, C > ADS::readGridMap (std::istream &in, GridMoves moves=GridMoves::EIGHT, Corners corners=Corners::NO_CUT)
 the grid of a map in the format of the benchmarks of Sturtevant: the lines type octile, height \( h \), width \( w \) and map, then \( h \) lines of \( w \) characters, of which the dot, G and S are free cells of cost 1 and the others (O, T, W and the at sign) are blocked

Detailed description

The graph of a grid map, beyond the book [see Graph.h and Grid.h]: the cells of a \( \mathit{rows} \times \mathit{cols} \) map, the cell \( ( r , c ) \) the node \( u = r \cdot \mathit{cols} + c \), each cell free or blocked, and the moves to the four sides of a cell or also to its four corners.

The map is one flat vector of the costs of the cells, or none at all when every cell is free and costs 1; a cell is blocked when its cost is not positive. The adjacency is computed, never stored: the neighbors of \( u \) come from the row and the column of \( u \), one division, and from at most 8 reads of the vector, so that the graph takes \( s \, \mathit{rows} \, \mathit{cols} \) bytes (\( s \) the size of a cost, and 0 bytes without the vector) against the \( 4 ( n + 1 ) + ( 4 + s' ) a \) of a StaticGraph of the same grid with \( a \le 8 n \) arcs (\( s' \) the size of a weight). This is the representation of the programs of best-first search on grid maps (see Graphs on grid maps): the maps of the benchmarks of Sturtevant are read as such vectors [see readGridMap()], and the jump point search of Harabor and Grastien needs exactly what the vector gives, i.e., whether a cell is free in \( O( 1 ) \), with no list of adjacency.

A move from the cell \( u \) into the cell \( v \) costs, as MoveCost chooses, \( \mathit{cost}( v ) \) (the cell entered, the default) or \( \mathit{cost}( u ) \) (the cell left) times the cost of the move on a map of unit costs: \( \mathit{straight} \) for a side, \( \mathit{diagonal} \) for a corner, by default 1 and \( \sqrt{2} \) (the octile costs, which the benchmarks use). A move to a corner may cut it, i.e., pass by a blocked side: Corners says whether it may, as in the cutting of corners allowed by the first jump point search, whether at least one of the two sides must be free, or whether both must, as in the benchmarks of Sturtevant since 2012. The graph is oriented when two free cells have different costs, since under either model one of \( ( u , v ) \) and \( ( v , u ) \) is weighed by the cost of \( u \) and the other by that of \( v \); with one cost for all the free cells it is unoriented, the two moves of a pair costing the same, as the rules on the corners are symmetric.

The nodes are the free cells: hasNode( u ) is whether \( u \) is a cell and free, n() counts the free cells and bound() is \( \mathit{rows} \cdot \mathit{cols} \), so that the visits of Visit.h and the shortest paths of ShortestPaths.h run on the grid unchanged, with vectors of bound() places. forEachAdj(), forEachEdge() and forEachMove() (which also tells whether a move is to a corner) call their function on the neighbors in increasing order of name, as the rows of a StaticGraph are sorted, and allocate nothing; adj() and V() build the vectors of the concept Graph. gridDistances() is the visit in breadth of Grid.h from several sources on such a grid, the distances in a vector by node, and manhattan(), octile() and chebyshev() are the lower bounds on the cost between two cells that the A* of Hart, Nilsson and Raphael takes as heuristics, gridHeuristic() the one of the moves and of the costs of a grid.

setCost( u , c ) changes the cost of the cell \( u \), blocking it when \( c \) is not positive and freeing it otherwise, and tells the change, if the cost is not already \( c \), as the event Changed< Value , int , C > of Modification.h (the type CellChanged, with the cell and its two costs), through the subject policy Policy: NoObservers by default, with which the change compiles to the code of the grid alone, and Static< O... >, Dynamic<> or Both< ... > otherwise, as for ObservedGraph [see Observable.h], a batch() sending the changes of one cell coalesced into the first cost and the last one. forEachAffected( u , from , to , f ) calls \( f \) on the heads of the moves that such a change may create, remove or weigh differently, which depend on the model of the costs and on whether the cell changes between free and blocked: the cell itself with ENTERED and its free neighbors with LEFT, when it stays free, and the cell with all its free neighbors (those of the four or the eight moves, since the cell is a side of the corners that the moves between two of its neighbors cut), when it is blocked or freed; nothing when it stays blocked. An incremental search on the grid (LPA*, D* Lite) thus recomputes the right-hand sides of at most nine cells per change, with no rebuilding of the grid, and the union of the heads of the events of a batch, read after it, holds every head whose arcs the batch changed (see Graphs on grid maps). A grid with no vector of the costs takes one at its first setCost() of a cost other than 1, of \( \mathit{rows} \cdot \mathit{cols} \) places; minCost() and directed() follow the changes by the number of the free cells of the least cost, in \( O( 1 ) \) time, and in time linear in the cells when the last cell of the least cost leaves it.

The contracts [see Contract.h]: the grid has rows, cols >= 0 and at most \( 2^{31} - 1 \) cells, so that the name of each cell is an int and no sum in the moves overflows; the vector of the costs has a place per cell, or none; the costs of the moves are positive, and a diagonal one is given for an integral type of the weights; the operations on a node need a free cell, and those on a cell a cell of the grid. The invariant is that n() counts the free cells and the cells of the least cost. The operations are defined in GridGraph.tpp.

See also
N. R. Sturtevant, Benchmarks for grid-based pathfinding, IEEE Transactions on Computational Intelligence and AI in Games 4(2), 144-148, 2012, doi:10.1109/TCIAIG.2012.2197681
D. Harabor, A. Grastien, Online graph pruning for pathfinding on grid maps, Proceedings of the AAAI Conference on Artificial Intelligence 25(1), 1114-1119, 2011, doi:10.1609/aaai.v25i1.7994
D. Harabor, A. Grastien, Improving jump point search, Proceedings of the International Conference on Automated Planning and Scheduling 24, 128-135, 2014, doi:10.1609/icaps.v24i1.13633
P. Yap, Grid-based path-finding, Advances in Artificial Intelligence, Lecture Notes in Computer Science 2338, 44-55, 2002, doi:10.1007/3-540-47922-8_4
P. E. Hart, N. J. Nilsson, B. Raphael, A formal basis for the heuristic determination of minimum cost paths, IEEE Transactions on Systems Science and Cybernetics 4(2), 100-107, 1968, doi:10.1109/TSSC.1968.300136
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa