Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
IDA_Star.H
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
96# ifndef IDA_STAR_H
97# define IDA_STAR_H
98
99# include <limits>
100# include <cmath>
101# include <type_traits>
102# include <tpl_graph_utils.H>
103# include <ah-errors.H>
104# include <AStar.H>
105
106namespace Aleph
107{
108
122template <class GT, class Distance = Dft_Dist<GT>>
124{
126
128 typename GT::Node * to) const
129 {
130 auto & f = from->get_info();
131 auto & t = to->get_info();
132 auto dx = std::abs(f.x - t.x);
133 auto dy = std::abs(f.y - t.y);
134 return static_cast<Distance_Type>(dx > dy ? dx : dy);
135 }
136};
137
169template <class GT,
170 class Distance = Dft_Dist<GT>,
172 template <typename, class> class Itor = Node_Arc_Iterator,
173 class SA = Dft_Show_Arc<GT>>
175{
176public:
178 using Node = typename GT::Node;
179 using Arc = typename GT::Arc;
180
181 static_assert(std::is_arithmetic_v<Distance_Type>,
182 "IDA* requires arithmetic distance type");
183
184private:
188
189 static constexpr Distance_Type Inf =
190 std::numeric_limits<Distance_Type>::max();
191
193 {
194 bool found;
196 };
197
203 SearchResult search(const GT & g, Node * curr, Node * end,
205 Path<GT> & path)
206 {
207 const Distance_Type h = heuristic(curr, end);
209 << "IDA*: heuristic must be non-negative, got " << h;
211 std::numeric_limits<Distance_Type>::max() - h)
212 << "IDA*: overflow computing g_cost + heuristic";
213 const Distance_Type f = g_cost + h;
214
215 if (f > threshold)
216 return {false, f};
217
218 if (curr == end)
219 return {true, g_cost};
220
221 struct Find_Path_Guard
222 {
223 Node * node = nullptr;
224
225 explicit Find_Path_Guard(Node * p) noexcept : node(p)
226 {
227 NODE_BITS(node).set_bit(Find_Path, true);
228 }
229
230 ~Find_Path_Guard() noexcept
231 {
232 NODE_BITS(node).set_bit(Find_Path, false);
233 }
234 };
235
237
239
240 for (Itor<GT, SA> it(curr, sa); it.has_curr(); it.next())
241 {
242 auto arc = it.get_current_arc();
243 auto tgt = g.get_connected_node(arc, curr);
244
245 if (IS_NODE_VISITED(tgt, Find_Path))
246 continue;
247
248 auto w = distance(arc);
250 << "IDA*: negative weight " << w << " not allowed";
251 ah_overflow_error_if(g_cost > std::numeric_limits<Distance_Type>::max() - w)
252 << "IDA*: overflow computing g_cost + w";
253
254 path.append(arc);
255
256 auto res = search(g, tgt, end, g_cost + w, threshold, path);
257
258 if (res.found)
259 return res;
260
261 if (res.value < min_threshold)
262 min_threshold = res.value;
263
264 path.remove_last_node();
265 }
266
267 return {false, min_threshold};
268 }
269
270public:
279 SA _sa = SA())
280 : sa(_sa), distance(dist), heuristic(h)
281 {
282 // empty
283 }
284
304 Distance_Type find_path(const GT & g, Node * start, Node * end,
305 Path<GT> & path)
306 {
307 ah_domain_error_if(start == nullptr) << "start node cannot be null";
308 ah_domain_error_if(end == nullptr) << "end node cannot be null";
309 ah_domain_error_if(g.get_num_nodes() == 0) << "graph is empty";
310
311 path.empty();
312
313 if (start == end)
314 {
315 path.set_graph(g, start);
316 return Distance_Type(0);
317 }
318
319 Distance_Type threshold = heuristic(start, end);
320
321 // Initialize path with start node
322 path.set_graph(g, start);
323
324 while (true)
325 {
327
328 auto res = search(g, start, end, Distance_Type(0), threshold, path);
329
330 if (res.found)
331 {
333 return res.value;
334 }
335
336 if (res.value == Inf)
337 {
339 path.empty();
340 return Inf;
341 }
342
343 threshold = res.value;
344
345 // Reset path for next iteration
346 path.empty();
347 path.set_graph(g, start);
348 }
349 }
350
359 Distance_Type operator()(const GT & g, Node * start, Node * end,
360 Path<GT> & path)
361 {
362 return find_path(g, start, end, path);
363 }
364};
365
366} // end namespace Aleph
367
368# endif // IDA_STAR_H
A* shortest path algorithm.
Exception handling system with formatted messages for Aleph-w.
#define ah_overflow_error_if(C)
Throws std::overflow_error if condition holds.
Definition ah-errors.H:463
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
Definition ah-errors.H:522
List_Graph< Graph_Node< Node_Info >, Graph_Arc< Arc_Info > > GT
long double h
Definition btreepic.C:154
long double w
Definition btreepic.C:153
Default distance accessor for arc weights.
IDA* algorithm for memory-efficient shortest path search.
Definition IDA_Star.H:175
typename Distance::Distance_Type Distance_Type
Definition IDA_Star.H:177
IDA_Star(Distance dist=Distance(), Heuristic h=Heuristic(), SA _sa=SA())
Constructor.
Definition IDA_Star.H:277
Distance_Type operator()(const GT &g, Node *start, Node *end, Path< GT > &path)
Finds shortest path (operator interface).
Definition IDA_Star.H:359
typename GT::Arc Arc
Definition IDA_Star.H:179
Heuristic heuristic
Definition IDA_Star.H:187
Distance_Type find_path(const GT &g, Node *start, Node *end, Path< GT > &path)
Finds the shortest path from start to end using IDA*.
Definition IDA_Star.H:304
static constexpr Distance_Type Inf
Definition IDA_Star.H:189
typename GT::Node Node
Definition IDA_Star.H:178
SearchResult search(const GT &g, Node *curr, Node *end, Distance_Type g_cost, Distance_Type threshold, Path< GT > &path)
Recursive DFS bounded by threshold.
Definition IDA_Star.H:203
Distance distance
Definition IDA_Star.H:186
Path on a graph.
Definition tpl_graph.H:2669
void empty()
Clean the path: all the nodes and arc are removed.
Definition tpl_graph.H:2819
void set_graph(const GT &__g, Node *start_node=nullptr)
Set the graph of the path.
Definition tpl_graph.H:2797
void append(Arc *arc)
Append an arc to the path.
Definition tpl_graph.H:2872
Node * remove_last_node()
Remove the last node of path.
Definition tpl_graph.H:3177
NodeInfo & get_info() noexcept
Return a modifiable reference to the data contained in the node.
Definition graph-dry.H:494
void reset_bit_nodes(int bit) const noexcept
Reset bit to zero for all the nodes of graph.
Definition graph-dry.H:1046
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Definition graph-dry.H:695
Node * get_connected_node(Arc *arc, Node *node) const noexcept
Return the adjacent node to node through arc.
Definition graph-dry.H:778
#define IS_NODE_VISITED(p, bit)
Determine whether the control bit is set or not to one.
#define NODE_BITS(p)
Get the control bits of a node.
@ Find_Path
Definition aleph-graph.H:76
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
Chebyshev (L-infinity) distance heuristic for 8-connected grids.
Definition IDA_Star.H:124
Distance_Type operator()(typename GT::Node *from, typename GT::Node *to) const
Definition IDA_Star.H:127
typename Distance::Distance_Type Distance_Type
Definition IDA_Star.H:125
Default filter for filtered iterators on arcs.
Definition tpl_graph.H:1000
Filtered iterator of adjacent arcs of a node.
Definition tpl_graph.H:1119
Default heuristic for A* (zero heuristic, degrades to Dijkstra).
Definition AStar.H:77
Distance accessor.
Utility algorithms and operations for graphs.