110 [[nodiscard]]
int width(
void )
const noexcept {
return( f_width ); }
114 [[nodiscard]]
int height(
void )
const noexcept {
return( f_height ); }
119 [[nodiscard]]
int cost(
int x ,
int y )
const noexcept {
120 if( ( x < 0 ) || ( y < 0 ) || ( x >= f_width ) || ( y >= f_height ) )
122 return( v_cost[ std::size_t( y ) * f_width + x ] );
139 [[nodiscard]]
const std::vector< std::uint8_t > &
costs(
void )
const
150 std::vector< std::uint8_t > v_cost;
163#if defined( PBEFS_STRUCTURES_STD ) || defined( PBEFS_STRUCTURES_BOOST )
182 using cost_type = std::conditional_t< Moves == 4 , int , double >;
193 void for_each( std::uint32_t ,
int x ,
int y , F && f )
const {
194 static constexpr int dx[ 8 ] = { 0 , -1 , 1 , 0 , -1 , 1 , -1 , 1 };
195 static constexpr int dy[ 8 ] = { -1 , 0 , 0 , 1 , -1 , -1 , 1 , 1 };
196 const int w = f_map.width();
198 for(
int k = 0 ; k < Moves ; ++k ) {
199 const int nx = x + dx[ k ] , ny = y + dy[ k ];
200 if( ! f_map.cost( nx , ny ) )
203 ( ! f_map.cost( nx , y ) || ! f_map.cost( x , ny ) ) )
205 f( std::uint32_t( ny ) * std::uint32_t( w ) + std::uint32_t( nx ) ,
228#if defined( PBEFS_STRUCTURES_ADS )
258template<
int Moves = 4 >
261 static_assert( ( Moves == 4 ) || ( Moves == 8 ) ,
262 "Grid: the moves are either 4 or 8" );
271 using cost_type = std::conditional_t< Moves == 4 , int , double >;
284 : f_map( map ) , f_adj( map ) , f_w( map.width() ) , f_h( map.height() ) ,
285 f_start(
index( sx , sy ) ) , f_goal(
index( gx , gy ) ) ,
286 f_gx( gx ) , f_gy( gy ) , f_min( map.min_cost() ) ,
288 if( ! map.
cost( sx , sy ) || ! map.
cost( gx , gy ) )
289 throw( std::invalid_argument(
"Grid::Grid: blocked start or goal" ) );
291 throw( std::invalid_argument(
"Grid::Grid: blocks of side <= 0" ) );
292 f_bw = ( f_w + f_block - 1 ) / f_block;
293 f_bh = ( f_h + f_block - 1 ) / f_block;
294 f_nw = ( f_w + f_nside - 1 ) / f_nside;
295 f_nh = ( f_h + f_nside - 1 ) / f_nside;
308 return( s == f_goal );
314 return( ADS::splitmix64( s ) );
321 return( distance(
int( s % f_w ) ,
int( s / f_w ) , f_gx , f_gy ) );
329 const int x = int( s % f_w ) , y = int( s / f_w );
330 f_adj.for_each( s , x , y , [ & ](
state_type t ,
int nx ,
int ny ,
332 f( t , c , distance( nx , ny , f_gx , f_gy ) );
340 return( std::size_t( f_w ) + f_h );
348 f( std::size_t( s % f_w ) );
349 f( std::size_t( f_w ) + s / f_w );
355 return( std::size_t( f_bw ) + f_bh );
362 if( i < std::size_t( f_w ) )
363 return( i / f_block );
364 return( std::size_t( f_bw ) + ( i - f_w ) / f_block );
371 return( std::uint64_t( s / f_w / f_block ) * f_bw + s % f_w / f_block );
377 return( std::size_t( f_nw ) * f_nh );
384 return( std::size_t( s / f_w / f_nside ) * f_nw + s % f_w / f_nside );
391 const int bx = int( b % f_nw ) , by = int( b / f_nw );
392 for(
int dy = -1 ; dy <= 1 ; ++dy )
393 for(
int dx = -1 ; dx <= 1 ; ++dx ) {
394 if( ( ! dx && ! dy ) || ( ( Moves == 4 ) && dx && dy ) )
396 const int nx = bx + dx , ny = by + dy;
397 if( ( nx >= 0 ) && ( ny >= 0 ) && ( nx < f_nw ) && ( ny < f_nh ) )
398 f( std::size_t( ny ) * f_nw + nx );
404 [[nodiscard]]
int nblock_side(
void )
const noexcept {
return( f_nside ); }
408 [[nodiscard]]
int block(
void )
const noexcept {
return( f_block ); }
415 return( distance(
int( s % f_w ) ,
int( s / f_w ) ,
416 int( t % f_w ) ,
int( t / f_w ) ) );
443 [[nodiscard]]
cost_type distance(
int x ,
int y ,
int u ,
int v )
445 const int ax = std::abs( x - u ) , ay = std::abs( y - v );
446 if constexpr( Moves == 4 )
447 return( ( ax + ay ) * f_min );
449 return( ( std::max( ax , ay ) + ( c_sqrt2 - 1 ) * std::min( ax , ay ) )
455 const GridMap & f_map;
456 GridAdjacency< Moves > f_adj;
Header file for the concepts a search problem has to satisfy in order to be searched by the algorithm...
Header file for the class template GridAdjacency of the set of data structures of ADS [see Structures...
Header file for the choice of the set of data structures of A*: the arena of the nodes (NodePool),...
the moves from a cell of a GridMap and their costs, on the GridGraph of ADS
Definition GridAdjacency.h:58
GridAdjacency(const GridMap &map)
constructor, copying the costs of the cells of the map
Definition GridAdjacency.h:69
std::conditional_t< Moves==4, int, double > cost_type
the cost of a move: an integer with 4 moves, a real with 8
Definition GridAdjacency.h:65
void for_each(std::uint32_t s, int x, int y, F &&f) const
calls f( t , x' , y' , c ) for each free cell t = ( x' , y' ) next to the free cell s = ( x ,...
Definition GridAdjacency.h:83
a rectangular map of cells, each with a cost of traversal
Definition Grid.h:73
int height(void) const noexcept
the number of rows
Definition Grid.h:114
static GridMap random(int width, int height, double p, std::uint64_t seed, int max_cost=1)
a random map with blocked cells of probability p
static GridMap read_movingai(std::istream &in)
reads a map in the format of the benchmarks of Sturtevant (2012)
int min_cost(void) const noexcept
the smallest cost of a free cell (1 if there is none)
const std::vector< std::uint8_t > & costs(void) const noexcept
the costs of the cells, row by row
Definition Grid.h:139
int cost(int x, int y) const noexcept
the cost of the cell ( x , y ), 0 if blocked or outside the map
Definition Grid.h:119
GridMap(int width=0, int height=0)
constructor of a map of width by height free cells of cost 1
void set_cost(int x, int y, int c)
sets the cost of the cell ( x , y ) to c, in
int width(void) const noexcept
the number of columns
Definition Grid.h:110
std::size_t num_nblocks(void) const noexcept
the number of nblocks
Definition Grid.h:376
std::uint64_t perfect_hash(state_type s) const noexcept
the perfect hash of HDA*[P]: the cell index
Definition Grid.h:421
void for_each_feature(state_type s, F &&f) const
calls f( x ) and f( W + y ) for the cell of the state s
Definition Grid.h:347
std::uint64_t abstract_state(state_type s) const noexcept
the abstract state of AHDA*: the block of the cell
Definition Grid.h:370
std::size_t nblock(state_type s) const noexcept
the nblock of PBNF: the block of cells, being nblock_side()
Definition Grid.h:383
state_type initial_state(void) const noexcept
the start cell
Definition Grid.h:301
std::size_t num_abstract_features(void) const noexcept
the number of abstract features: the columns and rows of blocks
Definition Grid.h:354
std::size_t abstract_feature(std::size_t i) const noexcept
the abstract feature of a feature: the column or row of its block
Definition Grid.h:360
Grid(const GridMap &map, int sx, int sy, int gx, int gy, int block=100, int nblock_side=20)
constructor, taking the map (which must outlive the Grid) and the cells
Definition Grid.h:282
std::uint64_t hash(state_type s) const noexcept
the hash of a cell: its index, mixed
Definition Grid.h:313
cost_type heuristic(state_type s) const noexcept
the Manhattan (4 moves) or octile (8 moves) distance to the goal
Definition Grid.h:320
bool is_goal(state_type s) const noexcept
true if s is the goal cell
Definition Grid.h:307
void for_each_nblock_successor(std::size_t b, F &&f) const
calls f( b' ) for each nblock b' next to the nblock b
Definition Grid.h:390
std::size_t num_features(void) const noexcept
the number of features: the columns and the rows
Definition Grid.h:339
int nblock_side(void) const noexcept
the side of the nblocks
Definition Grid.h:404
state_type index(int x, int y) const noexcept
the cell index of ( x , y )
Definition Grid.h:428
std::conditional_t< Moves==4, int, double > cost_type
the cost type
Definition Grid.h:271
cost_type pairwise_heuristic(state_type s, state_type t) const noexcept
the heuristic of PA*SE: the distance between two cells
Definition Grid.h:413
int block(void) const noexcept
the side of the blocks of AZH and AHDA*
Definition Grid.h:408
void for_each_successor(state_type s, cost_type, F &&f) const
calls f( t , c , h( t ) ) for each free cell t next to s
Definition Grid.h:328
std::uint32_t state_type
a cell index
Definition Grid.h:270
namespace for the Parallel Best-First Search library
Definition AStar.h:83
constexpr double grid_sqrt2
, the cost of a diagonal move per unit of cost of a cell
Definition Grid.h:159