PBeFS
Parallel Best-First Search
Loading...
Searching...
No Matches
IDAStar.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*----------------------------- File IDAStar.h -----------------------------*/
3/*--------------------------------------------------------------------------*/
48/*--------------------------------------------------------------------------*/
49/*------------------------------ DEFINITIONS -------------------------------*/
50/*--------------------------------------------------------------------------*/
51
52#ifndef __PBeFS_IDAStar
53 #define __PBeFS_IDAStar
54 /* self-identification: #endif at the end of the file */
55
56/*--------------------------------------------------------------------------*/
57/*-------------------------------- INCLUDES --------------------------------*/
58/*--------------------------------------------------------------------------*/
59
60#include <algorithm>
61
62#include <cstddef>
63
64#include <cstdint>
65
66#include <utility>
67
68#include <vector>
69
70#include "PBeFS/astar/Domain.h"
71
72#include "PBeFS/astar/Node.h"
73
75
77
78/*--------------------------------------------------------------------------*/
79/*------------------------------- NAMESPACE --------------------------------*/
80/*--------------------------------------------------------------------------*/
81
83namespace PBeFS_di_unipi_it
84{
85
86/*--------------------------------------------------------------------------*/
87/*----------------------------- CLASS IDAStar ------------------------------*/
88/*--------------------------------------------------------------------------*/
90
96
97template< Domain D >
98class IDAStar {
99
100/*------------------------ PUBLIC PART OF THE CLASS ------------------------*/
101
102 public:
103
104/*------------------------------ PUBLIC TYPES ------------------------------*/
105
109
110/*------------------------------ CONSTRUCTOR -------------------------------*/
111
113
114 explicit IDAStar( const D & domain , const SearchOptions & options = {} )
115 : f_domain( domain ) , f_options( options ) {}
116
117/*-------------------------------- SOLVING ---------------------------------*/
119
120 [[nodiscard]] SolutionT solve( void ) const
121 {
122 Stopwatch clock;
123 Run r( f_domain , f_options.node_limit );
124 const State s0 = f_domain.initial_state();
125 const Cost h0 = f_domain.heuristic( s0 );
126 r.path.push_back( s0 );
127 Cost bound = h0;
128 SolutionT sol;
129 for( ; ; ) {
130 r.next = infinite_cost< Cost >();
131 if( r.dfs( s0 , Cost( 0 ) , h0 , no_op , bound ) ) {
133 sol.cost = r.cost;
134 sol.path = r.path;
135 break;
136 }
137 if( r.stop ) {
139 break;
140 }
141 if( r.next == infinite_cost< Cost >() ) {
143 break;
144 }
145 bound = r.next;
146 }
147 r.st.expanded_by = { r.st.expanded };
148 r.st.stored_by = { r.st.stored };
149 r.st.seconds = clock.seconds();
150 sol.stats = std::move( r.st );
151 return( sol );
152 }
153
154/*----------------------- PRIVATE PART OF THE CLASS ------------------------*/
155
156 private:
157
159 struct Run {
160
161 const D & d;
162 std::size_t limit;
163 std::vector< State > path;
164 Cost next{};
165 Cost cost{};
166 bool stop = false;
167 SearchStats st;
168
169 Run( const D & domain , std::size_t node_limit )
170 : d( domain ) , limit( node_limit ) {}
171
172 // the depth-first search below u, of cost g and heuristic h with
173 // g + h within the bound, generated by the operator op; true if a goal is
174 // found, the path then ending at it
175 bool dfs( const State & u , Cost g , Cost h , std::uint8_t op ,
176 Cost bound ) {
177 if( d.is_goal( u ) ) {
178 cost = g;
179 return( true );
180 }
181 if( limit && ( st.expanded >= limit ) ) {
182 stop = true;
183 return( false );
184 }
185 ++st.expanded;
186 bool found = false;
187 const auto child = [ & ]( const State & v , Cost c , Cost hv ,
188 std::uint8_t opv ) {
189 ++st.generated;
190 if( g + c + hv > bound ) { // cut: a candidate bound
191 next = std::min( next , g + c + hv );
192 return;
193 }
194 path.push_back( v );
195 st.stored = std::max< std::uint64_t >( st.stored , path.size() );
196 if( dfs( v , g + c , hv , opv , bound ) )
197 found = true;
198 else
199 path.pop_back();
200 };
201 if constexpr( OperatorDomain< D > )
202 d.for_each_successor( u , h , op , [ & ]( const State & v , Cost c ,
203 Cost hv , std::uint8_t opv ) {
204 if( ! found && ! stop )
205 child( v , c , hv , opv );
206 } );
207 else
208 d.for_each_successor( u , h , [ & ]( const State & v , Cost c ,
209 Cost hv ) {
210 if( found || stop )
211 return;
212 if( ( path.size() >= 2 ) && ( v == path[ path.size() - 2 ] ) ) {
213 ++st.generated; // back to the parent
214 return;
215 }
216 child( v , c , hv , no_op );
217 } );
218 return( found );
219 }
220
221 }; // end( struct( Run ) )
222
223/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
224
225 const D & f_domain;
226 SearchOptions f_options;
227
228 }; // end( class( IDAStar ) )
229
230/*--------------------------------------------------------------------------*/
231/*------------------------------- FUNCTIONS --------------------------------*/
232/*--------------------------------------------------------------------------*/
234
235template< Domain D >
237 const D & domain , const SearchOptions & options = {} )
238{
239 return( IDAStar< D >( domain , options ).solve() );
240 }
241
242/*--------------------------------------------------------------------------*/
243
244} // end( namespace PBeFS_di_unipi_it )
245
246/*--------------------------------------------------------------------------*/
247
248#endif /* __PBeFS_IDAStar */
249
250/*--------------------------------------------------------------------------*/
251/*--------------------------- End File IDAStar.h ---------------------------*/
252/*--------------------------------------------------------------------------*/
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 Node, a node of the search graph, for the class template NodePool,...
Header file for the small helpers shared by the searches: a stopwatch, the placement of the workers o...
Header file for the outcome of a search: the class template Solution, the statistics SearchStats coll...
IDAStar(const D &domain, const SearchOptions &options={})
constructor, taking the Domain (which must outlive the IDAStar)
Definition IDAStar.h:114
state_t< D > State
the state type
Definition IDAStar.h:106
Solution< State, Cost > SolutionT
the solution type
Definition IDAStar.h:108
cost_t< D > Cost
the cost type
Definition IDAStar.h:107
SolutionT solve(void) const
searches the Domain, returning the Solution
Definition IDAStar.h:120
a wall-clock stopwatch, started at construction
Definition SearchUtils.h:175
double seconds(void) const
the seconds elapsed since the construction
Definition SearchUtils.h:182
namespace for the Parallel Best-First Search library
Definition AStar.h:83
typename D::cost_type cost_t
the cost type of a Domain
Definition Domain.h:245
Solution< state_t< D >, cost_t< D > > idastar(const D &domain, const SearchOptions &options={})
searches a Domain with IDA*
Definition IDAStar.h:236
@ unsolvable
OPEN emptied without reaching a goal.
Definition Solution.h:48
@ solved
an optimal path has been found
Definition Solution.h:47
@ node_limit
the limit on the stored nodes was reached first
Definition Solution.h:49
constexpr Cost infinite_cost(void) noexcept
the largest value of a cost type, used as "no incumbent yet"
Definition Node.h:100
typename D::state_type state_t
the state type of a Domain
Definition Domain.h:241
constexpr std::uint8_t no_op
the operator of the root, which no state was generated by
Definition Domain.h:205
the options shared by all the searches
Definition Solution.h:119
std::vector< std::uint64_t > expanded_by
expansions of each worker
Definition Solution.h:75
std::uint64_t stored
distinct nodes stored at the end
Definition Solution.h:73
double seconds
wall-clock time of the search
Definition Solution.h:74
std::vector< std::uint64_t > stored_by
nodes stored by each worker
Definition Solution.h:76
std::uint64_t expanded
expansions, re-expansions included
Definition Solution.h:66
a path from the initial state to a goal, and its cost
Definition Solution.h:100
std::vector< State > path
the states along the path
Definition Solution.h:104
Cost cost
the cost of the path
Definition Solution.h:103
SearchStats stats
the counters of the search
Definition Solution.h:105
Status status
how the search ended
Definition Solution.h:102