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 },
95 bool operator==(
const GridState &)
const =
default;
106 using State = GridState;
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;
162 if (
GRID[
static_cast<size_t>(
nr)][
static_cast<size_t>(
nc)] != 0)
183 return GRID[
static_cast<size_t>(
r)][
static_cast<size_t>(c)] ?
'#' :
'.';
189 <<
", S=start, G=goal, #=obstacle):\n";
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";
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 ("
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.
and
Check uniqueness with explicit hash + equality functors.
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.
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.
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)