Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ida_star_example.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3 Data structures & Algorithms
4 version 2.0.0b
5 https://github.com/lrleon/Aleph-w
6*/
7
8/*
9 Aleph_w
10
11 Data structures & Algorithms
12 version 2.0.0b
13 https://github.com/lrleon/Aleph-w
14
15 This file is part of Aleph-w library
16
17 Copyright (c) 2002-2026 Leandro Rabindranath Leon
18
19 Permission is hereby granted, free of charge, to any person obtaining a copy
20 of this software and associated documentation files (the "Software"), to deal
21 in the Software without restriction, including without limitation the rights
22 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
23 copies of the Software, and to permit persons to whom the Software is
24 furnished to do so, subject to the following conditions:
25
26 The above copyright notice and this permission notice shall be included in all
27 copies or substantial portions of the Software.
28
29 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
32 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
33 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
34 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
35 SOFTWARE.
36*/
37
38
44# include <iostream>
45# include <iomanip>
46# include <algorithm>
47# include <limits>
48# include <aleph.H>
49# include <tpl_graph.H>
50# include <tpl_dynMapTree.H>
51# include <IDA_Star.H>
52
53using namespace Aleph;
54using namespace std;
55
56struct Coord {
57 int x, y;
58 bool operator==(const Coord & other) const {
59 return x == other.x and y == other.y;
60 }
61};
62
64using Arc_Info = int;
66
67template <class GT>
68struct ManhattanH {
70 int operator()(typename GT::Node * from, typename GT::Node * to) const {
71 auto & f = from->get_info();
72 auto & t = to->get_info();
73 return abs(f.x - t.x) + abs(f.y - t.y);
74 }
75};
76
77int main() {
78 cout << "=== IDA* (Iterative Deepening A*) Example ===" << endl << endl;
79
80 int grid_size = 5;
81
82 // Use DynList<DynList<bool>> for obstacles as requested
84 for (int i = 0; i < grid_size; ++i)
85 {
86 DynList<bool> row;
87 for (int j = 0; j < grid_size; ++j)
88 row.append(false);
89 obstacles.append(row);
90 }
91
92 auto set_obstacle = [&](int y, int x, bool val) {
93 auto & row = obstacles.nth(y);
94 row.nth(x) = val;
95 };
96
97 auto get_obstacle = [&](int y, int x) -> bool {
98 return obstacles.nth(y).nth(x);
99 };
100
101 for (int i = 1; i <= 3; ++i) {
102 set_obstacle(1, i, true);
103 set_obstacle(3, i, true);
104 }
105 set_obstacle(2, 1, true);
106 set_obstacle(2, 3, true);
107
108 cout << "Grid-based pathfinding (5x5):" << endl;
109 cout << "S = Start, G = Goal, # = Obstacle, . = Free" << endl << endl;
110
111 for (int y = 0; y < grid_size; ++y) {
112 for (int x = 0; x < grid_size; ++x) {
113 if (x == 0 and y == 0)
114 cout << "S ";
115 else if (x == grid_size - 1 and y == grid_size - 1)
116 cout << "G ";
117 else if (get_obstacle(y, x))
118 cout << "# ";
119 else
120 cout << ". ";
121 }
122 cout << endl;
123 }
124
125 cout << endl;
126
127 GT g;
129
130 for (int y = 0; y < grid_size; ++y) {
131 for (int x = 0; x < grid_size; ++x) {
132 if (not get_obstacle(y, x)) {
133 auto node = g.insert_node(Coord{x, y});
134 nodes.insert(make_pair(x, y), node);
135 }
136 }
137 }
138
139 for (int y = 0; y < grid_size; ++y) {
140 for (int x = 0; x < grid_size; ++x) {
141 if (get_obstacle(y, x))
142 continue;
143
144 int dx[] = {0, 0, 1, -1};
145 int dy[] = {1, -1, 0, 0};
146
147 for (int d = 0; d < 4; ++d) {
148 int nx = x + dx[d];
149 int ny = y + dy[d];
150
153 auto src = nodes.find(make_pair(x, y));
154 auto tgt = nodes.find(make_pair(nx, ny));
155 g.insert_arc(src, tgt, 1);
156 }
157 }
158 }
159 }
160
161 auto start = nodes.find(make_pair(0, 0));
162 auto goal = nodes.find(make_pair(grid_size - 1, grid_size - 1));
163
164 cout << "Running IDA* with Manhattan distance heuristic:" << endl;
165
167 Path<GT> path(g);
168
169 auto cost = ida(g, start, goal, path);
170
171 if (cost == numeric_limits<int>::max()) {
172 cout << "No path found!" << endl;
173 } else {
174 cout << "Path found! Cost: " << cost << endl << endl;
175
176 cout << "Path coordinates: ";
177 int count = 0;
178 path.for_each_node([&count](auto * n) {
179 auto & c = n->get_info();
180 cout << "(" << c.x << "," << c.y << ") ";
181 count++;
182 });
183 cout << endl << "Path length: " << (count - 1) << " moves" << endl;
184 }
185
186 cout << endl << "=== Performance ===" << endl
187 << "Time: O(b^d)" << endl
188 << "Space: O(d) [linear in depth]" << endl;
189
190 return 0;
191}
IDA* (Iterative Deepening A*) shortest path algorithm.
Core header for the Aleph-w library.
string Node_Info
Dynamic singly linked list with functional programming support.
Definition htlist.H:1155
T & append(const T &item)
Definition htlist.H:1271
Generic key-value map implemented on top of a binary search tree.
IDA* algorithm for memory-efficient shortest path search.
Definition IDA_Star.H:175
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Definition tpl_graph.H:524
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Definition tpl_graph.H:604
Path on a graph.
Definition tpl_graph.H:2669
void for_each_node(Operation op=Operation()) const
Execute an operation on each node of path.
Definition tpl_graph.H:3338
NodeInfo & get_info() noexcept
Return a modifiable reference to the data contained in the node.
Definition graph-dry.H:494
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_abs_function > > abs(const __gmp_expr< T, U > &expr)
Definition gmpfrxx.h:4051
DynArray< Graph::Node * > nodes
Definition graphpic.C:406
int main()
static mpfr_t y
Definition mpfr_mul_d.c:3
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
and
Check uniqueness with explicit hash + equality functors.
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.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Definition ahAlgo.H:127
STL namespace.
Arc of graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:222
bool operator==(const Coord &other) const
int operator()(typename GT::Node *from, typename GT::Node *to) const
Dynamic key-value map based on balanced binary search trees.
Generic graph and digraph implementations.