Programming for Robotics with Arduino C++ 9, Challenge 2 solution (a weighted heuristic) Idea: A* with f = 4 g + wq h for wq = 0, 4, 5, 6, 8, 12 and 16 (the heuristic weighted by 0, 1, 1.25, 1.5, 2, 3 and 4) on the 248 solvable grids of 300 random 32 x 32 grids at 20 % obstacles. The path at wq = 4 is optimal, and every other path is compared with it. CODE: // x2.cpp -- Challenge 2: weighted A*: how much faster, and how much longer a path? (host build) // f = 4 g + wq h with wq = 4 (ordinary A*, optimal), 5, 6, 8, 12 and 16 (the heuristic counted 1.25 to 4 times), and wq = 0 (no heuristic, the same as BFS). // 300 random 32 x 32 grids at 20 % obstacles, 4-connected. Reported, over the grids that have a path: cells expanded, the longest open list, the mean path length, the share of grids on which // the path was LONGER than the optimal one, and the worst excess in steps. #include #include #include "sim.h" #include "grid.h" typedef BitGrid<32, 32> Map; static AStar<32, 32, 1500> as; int main() { const int weights[7] = {0, 4, 5, 6, 8, 12, 16}; double exp_[7] = {0}, steps[7] = {0}; int longer[7] = {0}, worst[7] = {0}; uint16_t maxopen[7] = {0}; int solv = 0; for (int seed = 1; seed <= 300; ++seed) { Map m; Rng rng((uint32_t)seed * 2654435761u + 17u); for (uint8_t y = 0; y < 32; ++y) for (uint8_t x = 0; x < 32; ++x) if (rng.uniform() < 0.20) m.set(x, y, true); m.set(0, 0, false); m.set(31, 31, false); uint16_t p[1024]; int16_t opt = as.plan(m, 0, 0, 31, 31, p, 1024, 4); if (opt < 0) continue; ++solv; for (int i = 0; i < 7; ++i) { int16_t n = as.plan(m, 0, 0, 31, 31, p, 1024, (uint8_t)weights[i]); if (n < 0) { std::printf("weight %d failed on seed %d (%d)\n", weights[i], seed, n); continue; } exp_[i] += as.expanded(); steps[i] += n; if (as.max_open() > maxopen[i]) maxopen[i] = as.max_open(); if (n > opt) { ++longer[i]; if (n - opt > worst[i]) worst[i] = n - opt; } } } std::printf("weighted A*, 32 x 32, 20 %% obstacles, %d solvable grids of 300\n", solv); std::printf(" weight wq/4 cells expanded longest open list mean path (steps) grids with a longer path (%%) worst excess (steps)\n"); for (int i = 0; i < 7; ++i) std::printf(" %11.2f %14.1f %17u %17.2f %28.1f %20d\n", weights[i] / 4.0, exp_[i] / solv, maxopen[i], steps[i] / solv, 100.0 * longer[i] / solv, worst[i]); return 0; } END CODE OUTPUT: weighted A*, 32 x 32, 20 % obstacles, 248 solvable grids of 300 weight wq/4 cells expanded longest open list mean path (steps) grids with a longer path (%) worst excess (steps) 0.00 818.1 44 62.01 0.0 0 1.00 317.9 177 62.01 0.0 0 1.25 96.9 95 63.59 60.5 6 1.50 90.2 91 64.89 79.4 10 2.00 85.7 77 66.64 91.9 14 3.00 83.5 76 68.02 94.8 22 4.00 82.3 76 69.56 96.8 26 END OUTPUT Reading the output. Weight 0 is no heuristic at all: 818 cells expanded, the same as breadth-first search, with the shortest open list (44). Weight 1 (the ordinary A*) expands 318 cells and needs an open list of 177 entries, and its paths are optimal. Weight 1.25 expands 97 cells, 0.30 of the ordinary A*, and 0.12 of BFS, for a mean path of 63.59 steps against 62.01 (2.5 % longer); but the path is longer than the optimal one on 60.5 % of the grids, by up to 6 steps. Larger weights expand a little fewer cells (90, 86, 84, 82) and lengthen the paths more: +4.6 %, +7.5 %, +9.7 %, +12.2 %, with a worst excess of 26 steps at weight 4. Almost the whole saving is in the first quarter-step: the curve is flat after 1.25. The open list shrinks too (177 to 95, then 91, 77, 76, 76), but it never gets near BFS's 44. For a robot that cannot afford a heap of 177 entries the honest answer is not a weighted A*. The heap is only part of A*'s memory: the one-byte-per-cell g array stays (1024 bytes here), and a heap of 95 entries is still 380 bytes against BFS's queue of 44 cells, 88 bytes, which also finds the shortest path. A weighted heuristic buys fewer cells expanded, which is a saving of time, not of memory. So: if RAM is the scarce thing, use BFS; if time is, use weight 1.25 and accept that the path may be a few steps longer. I did not measure time, so that second claim is about cells expanded. Check on method: random grids at one density with start and goal in opposite corners; at other densities or with long walls (mazes) a heuristic can be much worse than here.