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 <ah-graph-concepts.H>
100
101# include <limits>
102# include <cmath>
103# include <type_traits>
104# include <tpl_graph_utils.H>
105# include <ah-errors.H>
106# include <AStar.H>
107
108namespace Aleph
109{
110
124template <AlephGraph GT, ArcDistance<GT> Distance = Dft_Dist<GT>>
126{
128
130 typename GT::Node * to) const
131 {
132 auto & f = from->get_info();
133 auto & t = to->get_info();
134 auto dx = std::abs(f.x - t.x);
135 auto dy = std::abs(f.y - t.y);
136 return static_cast<Distance_Type>(dx > dy ? dx : dy);
137 }
138};
139
171template <AlephGraph GT,
174 template <typename, class> class Itor = Node_Arc_Iterator,
177{
178public:
180 using Node = typename GT::Node;
181 using Arc = typename GT::Arc;
182
183 static_assert(std::is_arithmetic_v<Distance_Type>,
184 "IDA* requires arithmetic distance type");
185
186private:
187 SA sa;
190
191 static constexpr Distance_Type Inf =
192 std::numeric_limits<Distance_Type>::max();
193
195 {
196 bool found;
198 };
199
205 SearchResult search(const GT & g, Node * curr, Node * end,
207 Path<GT> & path)
208 {
209 const Distance_Type h = heuristic(curr, end);
211 << "IDA*: heuristic must be non-negative, got " << h;
213 std::numeric_limits<Distance_Type>::max() - h)
214 << "IDA*: overflow computing g_cost + heuristic";
215 const Distance_Type f = g_cost + h;
216
217 if (f > threshold)
218 return {false, f};
219
220 if (curr == end)
221 return {true, g_cost};
222
223 struct Find_Path_Guard
224 {
225 Node * node = nullptr;
226
227 explicit Find_Path_Guard(Node * p) noexcept : node(p)
228 {
229 NODE_BITS(node).set_bit(Find_Path, true);
230 }
231
232 ~Find_Path_Guard() noexcept
233 {
234 NODE_BITS(node).set_bit(Find_Path, false);
235 }
236 };
237
239
241
242 for (Itor<GT, SA> it(curr, sa); it.has_curr(); it.next())
243 {
244 auto arc = it.get_current_arc();
245 auto tgt = g.get_connected_node(arc, curr);
246
247 if (IS_NODE_VISITED(tgt, Find_Path))
248 continue;
249
250 auto w = distance(arc);
252 << "IDA*: negative weight " << w << " not allowed";
253 ah_overflow_error_if(g_cost > std::numeric_limits<Distance_Type>::max() - w)
254 << "IDA*: overflow computing g_cost + w";
255
256 path.append(arc);
257
258 auto res = search(g, tgt, end, g_cost + w, threshold, path);
259
260 if (res.found)
261 return res;
262
263 if (res.value < min_threshold)
264 min_threshold = res.value;
265
266 path.remove_last_node();
267 }
268
269 return {false, min_threshold};
270 }
271
272public:
281 SA _sa = SA())
282 : sa(_sa), distance(dist), heuristic(h)
283 {
284 // empty
285 }
286
306 Distance_Type find_path(const GT & g, Node * start, Node * end,
307 Path<GT> & path)
308 {
309 ah_domain_error_if(start == nullptr) << "start node cannot be null";
310 ah_domain_error_if(end == nullptr) << "end node cannot be null";
311 ah_domain_error_if(g.get_num_nodes() == 0) << "graph is empty";
312
313 path.empty();
314
315 if (start == end)
316 {
317 path.set_graph(g, start);
318 return Distance_Type(0);
319 }
320
321 Distance_Type threshold = heuristic(start, end);
322
323 // Initialize path with start node
324 path.set_graph(g, start);
325
326 while (true)
327 {
329
330 auto res = search(g, start, end, Distance_Type(0), threshold, path);
331
332 if (res.found)
333 {
335 return res.value;
336 }
337
338 if (res.value == Inf)
339 {
341 path.empty();
342 return Inf;
343 }
344
345 threshold = res.value;
346
347 // Reset path for next iteration
348 path.empty();
349 path.set_graph(g, start);
350 }
351 }
352
361 Distance_Type operator()(const GT & g, Node * start, Node * end,
362 Path<GT> & path)
363 {
364 return find_path(g, start, end, path);
365 }
366};
367
368} // end namespace Aleph
369
370# 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:468
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
Definition ah-errors.H:527
C++20 concepts for the protocol shared by graph algorithms.
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:177
typename Distance::Distance_Type Distance_Type
Definition IDA_Star.H:179
IDA_Star(Distance dist=Distance(), Heuristic h=Heuristic(), SA _sa=SA())
Constructor.
Definition IDA_Star.H:279
Distance_Type operator()(const GT &g, Node *start, Node *end, Path< GT > &path)
Finds shortest path (operator interface).
Definition IDA_Star.H:361
typename GT::Arc Arc
Definition IDA_Star.H:181
Heuristic heuristic
Definition IDA_Star.H:189
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:306
static constexpr Distance_Type Inf
Definition IDA_Star.H:191
typename GT::Node Node
Definition IDA_Star.H:180
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:205
Distance distance
Definition IDA_Star.H:188
Path on a graph.
Definition tpl_graph.H:2772
void empty()
Clean the path: all the nodes and arc are removed.
Definition tpl_graph.H:2922
void set_graph(const GT &__g, Node *start_node=nullptr)
Set the graph of the path.
Definition tpl_graph.H:2900
void append(Arc *arc)
Append an arc to the path.
Definition tpl_graph.H:2975
Node * remove_last_node()
Remove the last node of path.
Definition tpl_graph.H:3280
NodeInfo & get_info() noexcept
Return a modifiable reference to the data contained in the node.
Definition graph-dry.H:536
void reset_bit_nodes(int bit) const noexcept
Reset bit to zero for all the nodes of graph.
Definition graph-dry.H:1088
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Definition graph-dry.H:737
Node * get_connected_node(Arc *arc, Node *node) const noexcept
Return the adjacent node to node through arc.
Definition graph-dry.H:820
#define IS_NODE_VISITED(p, bit)
Determine whether the control bit is set or not to one.
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
#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
Chebyshev (L-infinity) distance heuristic for 8-connected grids.
Definition IDA_Star.H:126
Distance_Type operator()(typename GT::Node *from, typename GT::Node *to) const
Definition IDA_Star.H:129
typename Distance::Distance_Type Distance_Type
Definition IDA_Star.H:127
Default filter for filtered iterators on arcs.
Definition tpl_graph.H:1001
Filtered iterator of adjacent arcs of a node.
Definition tpl_graph.H:1120
Default heuristic for A* (zero heuristic, degrades to Dijkstra).
Definition AStar.H:79
Distance accessor.
Utility algorithms and operations for graphs.