PBeFS
Parallel Best-First Search
Loading...
Searching...
No Matches
ExplicitGraph.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*-------------------------- File ExplicitGraph.h --------------------------*/
3/*--------------------------------------------------------------------------*/
20/*--------------------------------------------------------------------------*/
21/*------------------------------ DEFINITIONS -------------------------------*/
22/*--------------------------------------------------------------------------*/
23
24#ifndef __PBeFS_ExplicitGraph
25 #define __PBeFS_ExplicitGraph
26 /* self-identification: #endif at the end of the file */
27
28/*--------------------------------------------------------------------------*/
29/*-------------------------------- INCLUDES --------------------------------*/
30/*--------------------------------------------------------------------------*/
31
32#include <cstddef>
33
34#include <cstdint>
35
36#include <stdexcept>
37
38#include <utility>
39
40#include <vector>
41
42#include "PBeFS/astar/Domain.h"
43
45
46/*--------------------------------------------------------------------------*/
47/*------------------------------- NAMESPACE --------------------------------*/
48/*--------------------------------------------------------------------------*/
49
51namespace PBeFS_di_unipi_it
52{
53
54#if defined( PBEFS_STRUCTURES_STD )
55
56/*--------------------------------------------------------------------------*/
57/*------------------------ CLASS ExplicitAdjacency -------------------------*/
58/*--------------------------------------------------------------------------*/
60
62
63template< class Cost >
65
66/*------------------------ PUBLIC PART OF THE CLASS ------------------------*/
67
68 public:
69
71
72 explicit ExplicitAdjacency( std::size_t n ) : v_adj( n ) {}
73
75
76 void add( std::uint32_t u , std::uint32_t v , Cost c ) {
77 v_adj[ u ].emplace_back( v , c );
78 }
79
81
82 template< class F >
83 void for_each( std::uint32_t u , F && f ) const {
84 for( const auto & [ v , c ] : v_adj[ u ] )
85 f( v , c );
86 }
87
89
90 [[nodiscard]] std::size_t size( void ) const noexcept {
91 return( v_adj.size() );
92 }
93
94/*----------------------- PRIVATE PART OF THE CLASS ------------------------*/
95
96 private:
97
98 std::vector< std::vector< std::pair< std::uint32_t , Cost > > > v_adj;
100
101 }; // end( class( ExplicitAdjacency ) )
102
103#endif /* PBEFS_STRUCTURES_STD */
104
105/*--------------------------------------------------------------------------*/
106
107} // end( namespace PBeFS_di_unipi_it )
108
109/*--------------------------------------------------------------------------*/
110/*------------------ THE ADJACENCY OF THE OTHER STRUCTURES -----------------*/
111/*--------------------------------------------------------------------------*/
112
113#if defined( PBEFS_STRUCTURES_BOOST )
114 #include "PBeFS/astar/structures/boost/ExplicitAdjacency.h"
115#elif defined( PBEFS_STRUCTURES_ADS )
117#endif
118
119/*--------------------------------------------------------------------------*/
120
122namespace PBeFS_di_unipi_it
123{
124
125/*--------------------------------------------------------------------------*/
126/*-------------------------- CLASS ExplicitGraph ---------------------------*/
127/*--------------------------------------------------------------------------*/
129
136
137template< class Cost = int >
139
140/*------------------------ PUBLIC PART OF THE CLASS ------------------------*/
141
142 public:
143
144/*------------------------------ PUBLIC TYPES ------------------------------*/
145
146 using state_type = std::uint32_t;
147 using cost_type = Cost;
148
149/*--------------------- CONSTRUCTOR AND INITIALIZATION ---------------------*/
150
152
153 ExplicitGraph( std::size_t n , state_type s , state_type t )
154 : f_source( s ) , f_target( t ) , f_adj( n ) , v_h( n , Cost( 0 ) ) {
155 if( ( s >= n ) || ( t >= n ) )
156 throw( std::invalid_argument( "ExplicitGraph::ExplicitGraph: source or "
157 "target out of range" ) );
158 }
159
160/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
162
163 void add_edge( state_type u , state_type v , Cost c ,
164 bool directed = false ) {
165 if( ( u >= f_adj.size() ) || ( v >= f_adj.size() ) || ( c < 0 ) )
166 throw( std::invalid_argument( "ExplicitGraph::add_edge: bad arc" ) );
167 f_adj.add( u , v , c );
168 if( ! directed )
169 f_adj.add( v , u , c );
170 }
171
172/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
174
175 void set_heuristic( state_type u , Cost h ) { v_h.at( u ) = h; }
176
177/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
179
181 if( s >= f_adj.size() )
182 throw( std::invalid_argument( "ExplicitGraph::set_source: bad node" ) );
183 f_source = s;
184 }
185
186/*------------------------- METHODS OF THE DOMAIN --------------------------*/
187
188 [[nodiscard]] state_type initial_state( void ) const noexcept {
189 return( f_source );
190 }
191
192 [[nodiscard]] bool is_goal( state_type s ) const noexcept {
193 return( s == f_target );
194 }
195
196 [[nodiscard]] std::uint64_t hash( state_type s ) const noexcept {
197 return( ADS::splitmix64( s ) );
198 }
199
200 [[nodiscard]] Cost heuristic( state_type s ) const noexcept {
201 return( v_h[ s ] );
202 }
203
204 template< class F >
205 void for_each_successor( state_type s , Cost , F && f ) const {
206 f_adj.for_each( s , [ & ]( state_type v , Cost c ) {
207 f( v , c , v_h[ v ] );
208 } );
209 }
210
211/*---------------------- FEATURES FOR ZOBRIST HASHING ----------------------*/
212
213 [[nodiscard]] std::size_t num_features( void ) const noexcept {
214 return( f_adj.size() );
215 }
216
217 template< class F >
218 void for_each_feature( state_type s , F && f ) const {
219 f( std::size_t( s ) );
220 }
221
222/*------------------------------ OTHER HASHES ------------------------------*/
224
225 [[nodiscard]] std::uint64_t perfect_hash( state_type s ) const noexcept {
226 return( s );
227 }
228
229/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
231
232 [[nodiscard]] std::size_t size( void ) const noexcept {
233 return( f_adj.size() );
234 }
235
236/*----------------------- PRIVATE PART OF THE CLASS ------------------------*/
237
238 private:
239
240 state_type f_source;
241 state_type f_target;
243 std::vector< Cost > v_h;
244
245 }; // end( class( ExplicitGraph ) )
246
247/*--------------------------------------------------------------------------*/
248
249} // end( namespace PBeFS_di_unipi_it )
250
251/*--------------------------------------------------------------------------*/
252
253#endif /* __PBeFS_ExplicitGraph */
254
255/*--------------------------------------------------------------------------*/
256/*------------------------ End File ExplicitGraph.h ------------------------*/
257/*--------------------------------------------------------------------------*/
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 ExplicitAdjacency of the set of data structures of ADS [see Struct...
Header file for the choice of the set of data structures of A*: the arena of the nodes (NodePool),...
the arcs of a weighted graph, on the GraphBuilder and ArrayGraph of ADS
Definition ExplicitAdjacency.h:63
void for_each(std::uint32_t u, F &&f) const
calls f( v , c ) for each arc ( u , v ) of cost c
Definition ExplicitAdjacency.h:104
ExplicitAdjacency(std::size_t n)
constructor of n nodes and no arc
Definition ExplicitAdjacency.h:71
void add(std::uint32_t u, std::uint32_t v, Cost c)
adds the arc ( u , v ) of cost c
Definition ExplicitAdjacency.h:94
std::size_t size(void) const noexcept
the number of nodes
Definition ExplicitAdjacency.h:114
std::uint64_t perfect_hash(state_type s) const noexcept
the perfect hash: the node
Definition ExplicitGraph.h:225
void for_each_feature(state_type s, F &&f) const
Definition ExplicitGraph.h:218
state_type initial_state(void) const noexcept
Definition ExplicitGraph.h:188
void for_each_successor(state_type s, Cost, F &&f) const
Definition ExplicitGraph.h:205
std::uint64_t hash(state_type s) const noexcept
Definition ExplicitGraph.h:196
void set_source(state_type s)
sets the source of the search
Definition ExplicitGraph.h:180
bool is_goal(state_type s) const noexcept
Definition ExplicitGraph.h:192
void add_edge(state_type u, state_type v, Cost c, bool directed=false)
adds the arc ( u , v ) of cost c, and ( v , u ) too unless directed
Definition ExplicitGraph.h:163
std::size_t size(void) const noexcept
the number of nodes
Definition ExplicitGraph.h:232
std::size_t num_features(void) const noexcept
Definition ExplicitGraph.h:213
void set_heuristic(state_type u, Cost h)
sets the heuristic value of the node u
Definition ExplicitGraph.h:175
Cost heuristic(state_type s) const noexcept
Definition ExplicitGraph.h:200
Cost cost_type
the cost type
Definition ExplicitGraph.h:147
std::uint32_t state_type
a node
Definition ExplicitGraph.h:146
ExplicitGraph(std::size_t n, state_type s, state_type t)
constructor of a graph with n nodes and no arc, searched from s to t
Definition ExplicitGraph.h:153
namespace for the Parallel Best-First Search library
Definition AStar.h:83