// Validation of the deterministic commute reachability (sim/commute.hpp commute_reachable) — the // flag-gated fix for the stochastic-commute death spiral. It must be deterministic and exhaustive: a job // reachable over the transit graph within budget is ALWAYS found, regardless of luck. #include "sc2k/sim/commute.hpp" #include #include "test_harness.hpp" namespace { using namespace sc2k; // R building at (30,30) with a road run of length `len` east, and a C job past its end (if `job`). City line_city(int len, bool job) { City c; c.xzon.at(30, 30) = 0x01; // R source for (int x = 31; x <= 30 + len; ++x) c.xbld.at(30, x) = 0x1D; if (job) c.xzon.at(30, 31 + len) = 0x03; // a C job just past the road end return c; } } // namespace int main() { using namespace sc2k; // (1) A short road to a job → reachable; deterministic across repeats. { City c = line_city(4, true); const bool a = commute_reachable(c, 30, 30, 1, 100); const bool b = commute_reachable(c, 30, 30, 1, 100); CHECK(a == true); CHECK(a == b); // deterministic } // (2) No transit adjacent → not reachable. { City c; c.xzon.at(30, 30) = 0x01; // a lone R zone, no roads CHECK(commute_reachable(c, 30, 30, 1, 100) == false); } // (3) A road but no job anywhere → not reachable. { City c = line_city(4, false); CHECK(commute_reachable(c, 30, 30, 1, 100) == false); } // (4) Budget bound: a job far down a long road is reachable with a big budget but not a tiny one. { City c = line_city(20, true); // 20-tile road (road step costs 3 → ~60) CHECK(commute_reachable(c, 30, 30, 1, 100) == true); // within budget 100 CHECK(commute_reachable(c, 30, 30, 1, 10) == false); // budget 10 can't reach it } // (5) Exhaustiveness vs the stochastic walk: where a job IS reachable, the deterministic check must // succeed even on seeds where a single random trace might wander off and fail. { City c = line_city(8, true); int traceHits = 0; for (int seed = 1; seed <= 20; ++seed) { MsRand rng(seed); if (commute_trace(c, 30, 30, 1, 0, 100, rng)) ++traceHits; } CHECK(commute_reachable(c, 30, 30, 1, 100) == true); // always true… std::printf(" (the stochastic trace hit %d/20 seeds on the same reachable city)\n", traceHits); } if (failures == 0) std::printf("test_commute_reachable: ALL CHECKS PASSED\n"); else std::printf("test_commute_reachable: %d CHECK(S) FAILED\n", failures); return failures ? 1 : 0; }