69static constexpr int GRID_ROWS = 6;
70static constexpr int GRID_COLS = 8;
71static constexpr std::array<std::array<int, GRID_COLS>, GRID_ROWS> GRID = {{
72 { 0, 0, 0, 0, 0, 0, 0, 0 },
73 { 0, 1, 1, 0, 1, 1, 0, 0 },
74 { 0, 0, 0, 0, 1, 0, 0, 0 },
75 { 0, 1, 0, 1, 1, 0, 1, 0 },
76 { 0, 1, 0, 0, 0, 0, 1, 0 },
77 { 0, 0, 0, 0, 0, 0, 0, 0 },
81static constexpr int START_ROW = 0;
82static constexpr int START_COL = 0;
83static constexpr int GOAL_ROW = 5;
84static constexpr int GOAL_COL = 7;
95 bool operator==(
const GridState &)
const =
default;
106 using State = GridState;
112 explicit GridDomain(
const int gr = GOAL_ROW,
const int gc = GOAL_COL)
113 : goal_row(gr), goal_col(gc)
118 bool is_goal(
const State &s)
const noexcept
120 return s.row == goal_row
and s.col == goal_col;
123 Distance heuristic(
const State &s)
const noexcept
125 return std::abs(s.row - goal_row) + std::abs(s.col - goal_col);
128 Distance cost(
const State &,
const Move &)
const noexcept
133 void apply(State &s,
const Move &
m)
const noexcept
139 void undo(State &s,
const Move &
m)
const noexcept
145 template <
typename Visitor>
146 bool for_each_successor(
const State &s, Visitor visit)
const
148 static constexpr std::array<Move, 4> dirs = {{
155 for (
const auto &d : dirs)
157 const int nr = s.row + d.dr;
158 const int nc = s.col + d.dc;
160 if (nr < 0 or nr >= GRID_ROWS or nc < 0 or nc >= GRID_COLS)
162 if (GRID[
static_cast<size_t>(nr)][
static_cast<size_t>(nc)] != 0)
177char cell_char(
const int r,
const int c)
179 if (
r == START_ROW
and c == START_COL)
181 if (
r == GOAL_ROW
and c == GOAL_COL)
183 return GRID[
static_cast<size_t>(
r)][
static_cast<size_t>(c)] ?
'#' :
'.';
188 std::cout <<
"Grid (" << GRID_ROWS <<
'x' << GRID_COLS
189 <<
", S=start, G=goal, #=obstacle):\n";
190 for (
int r = 0;
r < GRID_ROWS; ++
r)
193 for (
int c = 0; c < GRID_COLS; ++c)
194 std::cout << cell_char(
r, c);
200 const GridState &start)
202 GridState pos = start;
203 std::cout <<
" (" << pos.row <<
',' << pos.col <<
')';
204 for (
const auto &
m : path)
208 std::cout <<
" -> (" << pos.row <<
',' << pos.col <<
')';
217 std::cout <<
"IDA* grid pathfinding example\n\n";
221 GridDomain domain(GOAL_ROW, GOAL_COL);
222 const GridState
root{START_ROW, START_COL};
224 auto result = Search::ida_star_search(domain,
root);
226 std::cout <<
"Status: ";
227 switch (result.status)
232 default: std::cout <<
"not started\n";
break;
235 std::cout <<
"IDA* iterations: " << result.iterations.size() <<
'\n';
236 std::cout <<
"Total visited states: " << result.stats.visited_states <<
'\n';
237 std::cout <<
"Total expanded states: " << result.stats.expanded_states <<
'\n';
239 for (
size_t i = 0; i < result.iterations.size(); ++i)
241 const auto &it = result.iterations[i];
242 std::cout <<
" Iteration " << i + 1
243 <<
": threshold=" << it.threshold
244 <<
" visited=" << it.visited_states
245 << (it.found_solution ?
" FOUND" :
"") <<
'\n';
248 if (result.found_solution())
250 const auto &sol = result.best_solution.get();
251 std::cout <<
"\nOptimal path cost: " << result.total_cost <<
" steps\n";
252 std::cout <<
"Path:\n";
257 std::cout <<
"\nNo path found from ("
258 << START_ROW <<
',' << START_COL <<
") to ("
259 << GOAL_ROW <<
',' << GOAL_COL <<
")\n";
Umbrella header for the implicit state-space search framework.
void print_path(Path< WeightedDigraph > &path, WeightedDigraph &g)
Simple dynamic array with automatic resizing and functional operations.
__gmp_expr< T, __gmp_binary_expr< __gmp_expr< T, U >, unsigned long int, __gmp_root_function > > root(const __gmp_expr< T, U > &expr, unsigned long int l)
User-facing facade exported by the umbrella search header.
bool operator==(const DynList< T > &l1, const DynList< T > &l2)
Equality operator for DynList.
@ LimitReached
Search stopped because an external hard limit was hit.
@ Exhausted
Search space within the configured bounds was exhausted.
@ StoppedOnSolution
Search stopped because solution handling requested it.
and
Check uniqueness with explicit hash + equality functors.
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)