Programming for Robotics with Arduino C++ 3, Challenge 3 solution (a seed is a bug report) Idea: because the simulator has its own random generator, a seed names exactly one run. Run 1000 seeds on the roughest setting (20 mm of noise, 30 % dropouts), count the runs that ended in the NO_RANGE fault, and find the fault-free run that ended nearest the wall (the most negative error). Then run that seed again, and the next seed, and compare a fingerprint (a 32-bit hash of the final true position and heading); do the same for the first faulting seed. CODE: // x3.cpp -- challenge 3: use the seed as a bug report. Run 1000 seeds on a hard setting, find the worst one, and reproduce it exactly (host build) #include #include #include "harness.h" struct Result { double err_mm; DriveFault fault; uint32_t fingerprint; }; static Result run_seed(uint32_t seed) { RobotParams rp; SensorParams sp; sp.sigma_m = 0.020; sp.dropout = 0.30; sp.poll_us = 100.0; // the roughest setting of section 5 Arena arena; SimHW world(rp, sp, arena, seed); Outcome o = run_robot(world, default_config(), 12000); Result r; r.err_mm = (o.true_range - 0.25) * 1000.0; r.fault = o.fault; double bits[3] = {o.x, o.y, o.th}; // a fingerprint of the final truth, to compare two runs of the same seed const unsigned char* p = (const unsigned char*)bits; uint32_t h = 2166136261u; for (size_t i = 0; i < sizeof bits; ++i) { h ^= p[i]; h *= 16777619u; } r.fingerprint = h; return r; } int main() { uint32_t worst_seed = 0, fault_seed = 0; double worst = 1e9; int faults = 0, runs = 0; for (uint32_t seed = 1; seed <= 1000; ++seed) { Result r = run_seed(seed); ++runs; if (r.fault != FAULT_NONE) { ++faults; if (!fault_seed) fault_seed = seed; continue; } if (r.err_mm < worst) { worst = r.err_mm; worst_seed = seed; } } std::printf("1000 seeds, sigma 20 mm, dropouts 30 %%: %d runs ended in a NO_RANGE fault (first: seed %u)\n", faults, (unsigned)fault_seed); std::printf("the run that got closest to the wall without a fault: seed %u, %+.1f mm from the stop line\n", (unsigned)worst_seed, worst); Result a = run_seed(worst_seed), b = run_seed(worst_seed), c = run_seed(worst_seed + 1); std::printf("run seed %u twice: fingerprints %08x and %08x (%s); seed %u for comparison: %08x\n", (unsigned)worst_seed, (unsigned)a.fingerprint, (unsigned)b.fingerprint, a.fingerprint == b.fingerprint ? "identical" : "DIFFERENT", (unsigned)(worst_seed + 1), (unsigned)c.fingerprint); Result f1 = run_seed(fault_seed), f2 = run_seed(fault_seed); std::printf("the first faulting seed, %u, run twice: fault %d and %d, ends %+.1f mm from the stop line both times (%s)\n", (unsigned)fault_seed, (int)f1.fault, (int)f2.fault, f1.err_mm, f1.fingerprint == f2.fingerprint ? "identical" : "DIFFERENT"); (void)runs; return 0; } END CODE OUTPUT: 1000 seeds, sigma 20 mm, dropouts 30 %: 243 runs ended in a NO_RANGE fault (first: seed 1) the run that got closest to the wall without a fault: seed 302, -24.1 mm from the stop line run seed 302 twice: fingerprints 87a1a9cc and 87a1a9cc (identical); seed 303 for comparison: 3793e977 the first faulting seed, 1, run twice: fault 1 and 1, ends +760.0 mm from the stop line both times (identical) END OUTPUT Reading the output. 243 of the 1000 runs (24.3 %, in line with 44 of 200 in section 6) ended in the fault, and the first of them was seed 1. The fault-free run that went nearest the wall is seed 302, 24.1 mm past the stop line; running seed 302 again gives the identical fingerprint (87a1a9cc twice) and seed 303 gives another (3793e977). Seed 1 faults the second time too and ends 760.0 mm short of the stop line both times: the robot stopped, as designed, but the job was not done. So a report such as 'seed 302 overshoots by 24 mm at sigma 20 mm and 30 % dropouts' is enough for someone else to reproduce the run exactly, and the same is true of the failure. Check on method: identical fingerprints are shown on this machine and this compiler; section 9 showed that a float world, or possibly another processor or compiler, can change the last digits, so a bug report has to say which build of which code it was found with, as well as the seed and the settings. 1000 seeds is a sample, and the true worst case of the setting may be worse than seed 302.