PBeFS
Parallel Best-First Search
Loading...
Searching...
No Matches
VacuumWorld.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*--------------------------- File VacuumWorld.h ---------------------------*/
3/*--------------------------------------------------------------------------*/
16/*--------------------------------------------------------------------------*/
17/*------------------------------ DEFINITIONS -------------------------------*/
18/*--------------------------------------------------------------------------*/
19
20#ifndef __PBeFS_VacuumWorld
21 #define __PBeFS_VacuumWorld
22 /* self-identification: #endif at the end of the file */
23
24/*--------------------------------------------------------------------------*/
25/*-------------------------------- INCLUDES --------------------------------*/
26/*--------------------------------------------------------------------------*/
27
28#include <bit>
29
30#include <cstddef>
31
32#include <cstdint>
33
34#include <stdexcept>
35
36#include "PBeFS/astar/Domain.h"
37
38/*--------------------------------------------------------------------------*/
39/*------------------------------- NAMESPACE --------------------------------*/
40/*--------------------------------------------------------------------------*/
41
43namespace PBeFS_di_unipi_it
44{
45
46/*--------------------------------------------------------------------------*/
47/*--------------------------- CLASS VacuumWorld ----------------------------*/
48/*--------------------------------------------------------------------------*/
50
67
69
70/*------------------------ PUBLIC PART OF THE CLASS ------------------------*/
71
72 public:
73
74/*------------------------------ PUBLIC TYPES ------------------------------*/
75
76 using state_type = std::uint64_t;
77 using cost_type = int;
78
79/*------------------------------ CONSTRUCTOR -------------------------------*/
80
83
84 VacuumWorld( int n , int start , std::uint32_t dirty )
85 : f_n( n ) , f_start( start ) , f_dirty( dirty ) {
86 if( ( n < 1 ) || ( n > 32 ) || ( start < 0 ) || ( start >= n ) )
87 throw( std::invalid_argument( "VacuumWorld::VacuumWorld: bad sizes" ) );
88 if( ( n < 32 ) && ( dirty >> n ) )
89 throw( std::invalid_argument( "VacuumWorld::VacuumWorld: dirt out of "
90 "the row" ) );
91 }
92
93/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
95
96 [[nodiscard]] static int robot( std::uint64_t s ) noexcept {
97 return( int( s >> 32 ) );
98 }
99
101
102 [[nodiscard]] static std::uint32_t dirt( std::uint64_t s ) noexcept {
103 return( std::uint32_t( s ) );
104 }
105
106/*------------------------- METHODS OF THE DOMAIN --------------------------*/
107
108 [[nodiscard]] std::uint64_t initial_state( void ) const noexcept {
109 return( pack( f_start , f_dirty ) );
110 }
111
112 [[nodiscard]] bool is_goal( std::uint64_t s ) const noexcept {
113 return( dirt( s ) == 0 );
114 }
115
116 [[nodiscard]] std::uint64_t hash( std::uint64_t s ) const noexcept {
117 return( ADS::splitmix64( s ) );
118 }
119
120 [[nodiscard]] int heuristic( std::uint64_t s ) const noexcept {
121 return( std::popcount( dirt( s ) ) );
122 }
123
124/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
126
127 template< class F >
128 void for_each_successor( std::uint64_t s , int h , F && f ) const {
129 const int r = robot( s );
130 const std::uint32_t d = dirt( s );
131 if( d & ( std::uint32_t( 1 ) << r ) ) // Suck
132 f( pack( r , d & ~( std::uint32_t( 1 ) << r ) ) , 1 , h - 1 );
133 if( r > 0 ) // Left
134 f( pack( r - 1 , d ) , 1 , h );
135 if( r + 1 < f_n ) // Right
136 f( pack( r + 1 , d ) , 1 , h );
137 }
138
139/*---------------------- FEATURES FOR ZOBRIST HASHING ----------------------*/
141
142 [[nodiscard]] std::size_t num_features( void ) const noexcept {
143 return( 2 * std::size_t( f_n ) );
144 }
145
147
148 template< class F >
149 void for_each_feature( std::uint64_t s , F && f ) const {
150 for( std::uint32_t d = dirt( s ) ; d ; d &= d - 1 )
151 f( std::size_t( std::countr_zero( d ) ) );
152 f( std::size_t( f_n + robot( s ) ) );
153 }
154
156
157 [[nodiscard]] std::size_t num_abstract_features( void ) const noexcept {
158 return( std::size_t( f_n ) + 1 );
159 }
160
162
163 [[nodiscard]] std::size_t abstract_feature( std::size_t i ) const noexcept
164 {
165 return( i < std::size_t( f_n ) ? i : std::size_t( f_n ) );
166 }
167
168/*--------------------------- STATE ABSTRACTIONS ---------------------------*/
170
171 [[nodiscard]] std::uint64_t abstract_state( std::uint64_t s ) const noexcept
172 {
173 return( dirt( s ) );
174 }
175
177
178 [[nodiscard]] std::size_t num_nblocks( void ) const noexcept {
179 return( std::size_t( f_n ) );
180 }
181
183
184 [[nodiscard]] std::size_t nblock( std::uint64_t s ) const noexcept {
185 return( std::size_t( robot( s ) ) );
186 }
187
189
190 template< class F >
191 void for_each_nblock_successor( std::size_t b , F && f ) const {
192 if( b > 0 )
193 f( b - 1 );
194 if( b + 1 < std::size_t( f_n ) )
195 f( b + 1 );
196 }
197
198/*---------------------- OTHER HEURISTICS AND HASHES -----------------------*/
200
201 [[nodiscard]] int pairwise_heuristic( std::uint64_t s , std::uint64_t t )
202 const noexcept {
203 return( std::popcount( dirt( s ) & ~dirt( t ) ) );
204 }
205
207
208 [[nodiscard]] std::uint64_t perfect_hash( std::uint64_t s ) const noexcept {
209 return( s );
210 }
211
212/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
214
215 [[nodiscard]] int size( void ) const noexcept { return( f_n ); }
216
217/*----------------------- PRIVATE PART OF THE CLASS ------------------------*/
218
219 private:
220
221 // the state with the robot in the cell r and the dirty cells d
222 static std::uint64_t pack( int r , std::uint32_t d ) noexcept {
223 return( ( std::uint64_t( r ) << 32 ) | d );
224 }
225
226 int f_n;
227 int f_start;
228 std::uint32_t f_dirty;
229
230 }; // end( class( VacuumWorld ) )
231
232/*--------------------------------------------------------------------------*/
233
234} // end( namespace PBeFS_di_unipi_it )
235
236/*--------------------------------------------------------------------------*/
237
238#endif /* __PBeFS_VacuumWorld */
239
240/*--------------------------------------------------------------------------*/
241/*------------------------- End File VacuumWorld.h -------------------------*/
242/*--------------------------------------------------------------------------*/
Header file for the concepts a search problem has to satisfy in order to be searched by the algorithm...
std::size_t num_nblocks(void) const noexcept
the number of nblocks: the cells
Definition VacuumWorld.h:178
std::uint64_t abstract_state(std::uint64_t s) const noexcept
the abstract state of AHDA*: the dirty cells
Definition VacuumWorld.h:171
std::size_t num_abstract_features(void) const noexcept
the number of abstract features: the dirty cells and the robot
Definition VacuumWorld.h:157
std::size_t abstract_feature(std::size_t i) const noexcept
the abstract feature of a feature: the dirty cell, or n for the robot
Definition VacuumWorld.h:163
bool is_goal(std::uint64_t s) const noexcept
Definition VacuumWorld.h:112
static int robot(std::uint64_t s) noexcept
the cell of the robot in the state s
Definition VacuumWorld.h:96
VacuumWorld(int n, int start, std::uint32_t dirty)
constructor of the row of n cells, with the robot in the cell start and the dirty cells given by the ...
Definition VacuumWorld.h:84
std::uint64_t hash(std::uint64_t s) const noexcept
Definition VacuumWorld.h:116
void for_each_feature(std::uint64_t s, F &&f) const
calls f( i ) for each dirty cell i and f( n + r ) for the robot in r
Definition VacuumWorld.h:149
int pairwise_heuristic(std::uint64_t s, std::uint64_t t) const noexcept
the heuristic of PA*SE: the cells dirty in s and clean in t
Definition VacuumWorld.h:201
std::uint64_t state_type
the packed state
Definition VacuumWorld.h:76
static std::uint32_t dirt(std::uint64_t s) noexcept
the dirty cells in the state s
Definition VacuumWorld.h:102
void for_each_nblock_successor(std::size_t b, F &&f) const
calls f( b' ) for each cell b' next to the cell b
Definition VacuumWorld.h:191
int cost_type
the cost type
Definition VacuumWorld.h:77
std::uint64_t perfect_hash(std::uint64_t s) const noexcept
the perfect hash: the packed state
Definition VacuumWorld.h:208
std::size_t num_features(void) const noexcept
the number of features: the dirty cells and the cells of the robot
Definition VacuumWorld.h:142
int size(void) const noexcept
the number of cells
Definition VacuumWorld.h:215
void for_each_successor(std::uint64_t s, int h, F &&f) const
calls f( t , 1 , h( t ) ) for Suck, Left and Right, if possible
Definition VacuumWorld.h:128
std::size_t nblock(std::uint64_t s) const noexcept
the nblock of PBNF: the cell of the robot
Definition VacuumWorld.h:184
std::uint64_t initial_state(void) const noexcept
Definition VacuumWorld.h:108
int heuristic(std::uint64_t s) const noexcept
Definition VacuumWorld.h:120
namespace for the Parallel Best-First Search library
Definition AStar.h:83