// Clean-room contracts for the live bounded stochastic commute route. #include "sc2k/sim/commute.hpp" #include "sc2k/sim/region.hpp" #include #include #include #include "test_harness.hpp" namespace { using namespace sc2k; void straight_city(City& city, int density = 0) { city.xzon.at(64, 64) = 1; for (int column = 65; column <= 72; ++column) { city.xbld.at(64, column) = 0x1D; city.xtrf.at(32, column / 2) = static_cast(density); } city.xzon.at(64, 73) = 3; } void diamond_city(City& city, int upperDensity = 0) { city.xzon.at(64, 64) = 1; city.xbld.at(64, 65) = 0x1D; for (int column = 65; column <= 72; ++column) { city.xbld.at(63, column) = 0x1D; city.xbld.at(65, column) = 0x1D; city.xtrf.at(31, column / 2) = static_cast(upperDensity); } city.xbld.at(64, 72) = 0x1D; city.xzon.at(64, 73) = 3; } int traffic_row_sum(const City& city, int row, int firstColumn, int lastColumn) { int total = 0; for (int column = firstColumn; column <= lastColumn; ++column) total += city.xtrf.at(row / 2, column / 2); return total; } sim::BorderConnection connection(int row, int column, int edgeSlot, int offset, sim::BorderConnectionKind kind) { return {row, column, edgeSlot, offset, kind, sim::connection_layer(kind), sim::connection_footprint(kind)}; } } // namespace int main() { // Independent table checks: integer costs, congestion divisors, and explicit mode categories. CHECK(commute_travel_mode(0) == CommuteTravelMode::Car); CHECK(commute_travel_mode(4) == CommuteTravelMode::Bus); CHECK(commute_travel_mode(10) == CommuteTravelMode::Rail); CHECK(commute_travel_mode(13) == CommuteTravelMode::Subway); CHECK(commute_generalized_step_cost(0, 0x1D, 0, 0) == 4); CHECK(commute_generalized_step_cost(0, 0x1D, 0, 64) == 6); CHECK(commute_generalized_step_cost(0, 0x49, 0, 64) == 4); CHECK(commute_generalized_step_cost(4, 0x1D, 0, 64) == 5); CHECK(commute_generalized_step_cost(4, 0x49, 0, 64) == 3); CHECK(commute_generalized_step_cost(12, 0x2C, 0, 255) == 3); CHECK(commute_generalized_step_cost(13, 0, 1, 255) == 2); CHECK(commute_generalized_step_cost(9, xbld::kBusDepot, 0, 255) == 6); CHECK(commute_funded_step_cost_basis_points(2, 5000) == 40000); CHECK(commute_funded_step_cost_basis_points(2, 10000) == 20000); CHECK(commute_funded_step_cost_basis_points(2, 11852) == 16875); CHECK(!commute_funded_step_cost_basis_points(2, 0)); // Compatibility budget records map to the modes they actually operate. Cars are not a transit // service; buses consume road/bus record 10, rail record 13, and subway record 14. { std::array planned; planned.fill(100); planned[10] = 50; planned[13] = 150; planned[14] = 0; const sim::OperatingFundingSnapshot funding = sim::make_operating_funding_snapshot( [&](int record) { return planned[static_cast(record)]; }, 10000); const CommuteRouteConfig config = funded_commute_route_config(funding); CHECK(config.serviceEfficiency.basis_points(CommuteTravelMode::Car) == 10000); CHECK(config.serviceEfficiency.basis_points(CommuteTravelMode::Bus) == 5000); CHECK(config.serviceEfficiency.basis_points(CommuteTravelMode::Rail) == 11852); CHECK(config.serviceEfficiency.basis_points(CommuteTravelMode::Subway) == 0); } CHECK(commute_access_distance(1) == 5); CHECK(commute_access_distance(3) == 4); CHECK(commute_access_distance(5) == 6); { City city; city.xbld.at(64, 69) = 0x1D; CHECK(has_commute_access(city, 64, 64, 1)); CHECK(!has_commute_access(city, 64, 64, 3)); CHECK(has_commute_access(city, 64, 64, 5)); } // A straight route has a hand-calculated cost: one access unit plus seven road entries at four. { City city; straight_city(city); MsRand rng(7); CommuteRouteProbe probe; CommutePeriodDiagnostics diagnostics; CHECK(commute_route(city, 64, 64, 1, 10, 100, rng, {}, &probe, {}, &diagnostics)); CHECK(probe.stop == CommuteRouteStop::Destination); CHECK(probe.generalizedCost == 29); CHECK(probe.pathLength == 8); CHECK(probe.randomDraws == 1); CHECK(probe.modeSteps[static_cast(CommuteTravelMode::Car)] == 8); CHECK(probe.trafficCellsStamped == 8); CHECK(probe.expandedStates <= 4096); CHECK(diagnostics.attempts == 1 && diagnostics.successes == 1); CHECK(diagnostics.stops[static_cast(CommuteRouteStop::Destination)] == 1); CHECK(diagnostics.modeTrips[static_cast(CommuteTravelMode::Car)] == 1); CHECK(diagnostics.modeSteps[static_cast(CommuteTravelMode::Car)] == 8); CHECK(diagnostics.trafficCellsStamped == 8); } // Building weight controls traffic contribution, not the live trip-cost range. Level-one origins retain // the full base-100 bound, allowing a cost-77 trip which the compatibility tracer limits to 75. { City city; city.xzon.at(64, 64) = 1; for (int column = 65; column <= 84; ++column) city.xbld.at(64, column) = 0x1D; city.xzon.at(64, 85) = 3; MsRand rng(9); CommuteRouteProbe probe; CHECK(commute_route(city, 64, 64, 1, 1, 100, rng, {}, &probe)); CHECK(probe.effectiveMaxCost == 100); CHECK(probe.generalizedCost == 77); } // Congestion is sampled before the accepted path commits and increases generalized cost monotonically. { City clearCity, busyCity; straight_city(clearCity); straight_city(busyCity, 64); MsRand clearRng(11), busyRng(11); CommuteRouteProbe clear, busy; CHECK(commute_route(clearCity, 64, 64, 1, 0, 100, clearRng, {}, &clear)); CHECK(commute_route(busyCity, 64, 64, 1, 0, 100, busyRng, {}, &busy)); CHECK(clear.generalizedCost == 29); CHECK(busy.generalizedCost == 43); CHECK(busy.generalizedCost > clear.generalizedCost); } // Equal-cost alternatives retain stochastic path identity but no longer produce false failures. { std::set paths; for (int seed = 1; seed <= 256; ++seed) { City city; diamond_city(city); MsRand rng(static_cast(seed)); CommuteRouteProbe probe; CHECK(commute_route(city, 64, 64, 1, 0, 100, rng, {}, &probe)); CHECK(probe.stop == CommuteRouteStop::Destination); paths.insert(probe.pathHash); } CHECK(paths.size() >= 2); } // A congested equal-length arm loses to its clear alternative; only the accepted surface path stamps. { City city; diamond_city(city, 160); const int upperBefore = traffic_row_sum(city, 63, 66, 71); const int lowerBefore = traffic_row_sum(city, 65, 66, 71); MsRand rng(19); CommuteRouteProbe probe; CHECK(commute_route(city, 64, 64, 1, 10, 100, rng, {}, &probe)); const int upperDelta = traffic_row_sum(city, 63, 66, 71) - upperBefore; const int lowerDelta = traffic_row_sum(city, 65, 66, 71) - lowerBefore; CHECK(upperDelta == 0); CHECK(lowerDelta > 0); } // The work budget is unconditional (also when no probe is requested) and failed routes never stamp. { City city; straight_city(city); const auto before = city.xtrf.cells; MsRand rng(23); CommuteRouteProbe probe; CommuteRouteConfig config; config.maximumExpandedStates = 1; CHECK(!commute_route(city, 64, 64, 1, 10, 100, rng, {}, &probe, config)); CHECK(probe.stop == CommuteRouteStop::WorkLimit); CHECK(city.xtrf.cells == before); City withoutProbe; straight_city(withoutProbe); MsRand secondRng(23); CHECK(!commute_route(withoutProbe, 64, 64, 1, 10, 100, secondRng, {}, nullptr, config)); } // Invalid configuration and source values reject before consuming RNG or mutating traffic. { City city; straight_city(city); MsRand control(27), invalidRng(27); CommuteRouteProbe probe; CommuteRouteConfig invalid; invalid.version = kCommuteRouteConfigurationVersion + 1; CHECK(!commute_route(city, 64, 64, 1, 10, 100, invalidRng, {}, &probe, invalid)); CHECK(probe.stop == CommuteRouteStop::InvalidConfiguration); CHECK(invalidRng.next() == control.next()); CHECK(!commute_route(city, 64, 64, 0, 10, 100, invalidRng, {}, &probe)); CHECK(probe.stop == CommuteRouteStop::InvalidConfiguration); } // One caller RNG draw supplies all route diversity, independent of topology or optional diagnostics. { City straight, diamond; straight_city(straight); diamond_city(diamond); MsRand first(29), second(29); CommuteRouteProbe probe; CHECK(commute_route(straight, 64, 64, 1, 0, 100, first, {}, &probe)); CHECK(commute_route(diamond, 64, 64, 1, 0, 100, second)); CHECK(first.next() == second.next()); } // Enabling the bounded route-history observer changes neither path choice, traffic, nor RNG state. // It records canonical coordinates for accepted paths and the origin/reason for failures. { City observed, control; straight_city(observed); straight_city(control); MsRand observedRng(30), controlRng(30); CommuteRouteProbe observedProbe, controlProbe; CommuteRouteHistory history; CHECK(commute_route(observed, 64, 64, 1, 10, 100, observedRng, {}, &observedProbe, {}, nullptr, &history)); CHECK(commute_route(control, 64, 64, 1, 10, 100, controlRng, {}, &controlProbe)); CHECK(observed.xtrf.cells == control.xtrf.cells); CHECK(observedProbe.pathHash == controlProbe.pathHash); CHECK(observedRng.next() == controlRng.next()); CHECK(history.accepted().size() == 1 && history.failed().empty()); const CommuteRouteTrace& route = history.accepted().front(); CHECK(route.originRow == 64 && route.originColumn == 64); CHECK(route.terminalRow == 64 && route.terminalColumn == 73); CHECK(route.stop == CommuteRouteStop::Destination); CHECK(route.points.size() == 8); CHECK(route.points.front().row == 64 && route.points.front().column == 65); CHECK(route.points.back().row == 64 && route.points.back().column == 72); City limited; straight_city(limited); MsRand limitedRng(30); CommuteRouteConfig config; config.maximumExpandedStates = 1; CHECK(!commute_route(limited, 64, 64, 1, 0, 100, limitedRng, {}, nullptr, config, nullptr, &history)); CHECK(history.failed().size() == 1); CHECK(history.failed().front().stop == CommuteRouteStop::WorkLimit); CHECK(history.failed().front().points.empty()); } // Sampling retains the lowest stable keys separately for accepted and failed attempts. { CommuteRouteHistory history; for (std::uint32_t key = 600; key > 0; --key) { CommuteRouteTrace accepted; accepted.sampleKey = key; accepted.stop = CommuteRouteStop::Destination; accepted.originRow = static_cast(key % 128); history.consider(std::move(accepted)); CommuteRouteTrace failed; failed.sampleKey = key; failed.stop = CommuteRouteStop::CostLimit; failed.originRow = static_cast(key % 128); history.consider(std::move(failed)); } CHECK(history.accepted().size() == CommuteRouteHistory::kMaximumAcceptedRoutes); CHECK(history.failed().size() == CommuteRouteHistory::kMaximumFailedRoutes); CHECK(history.accepted().front().sampleKey == 1); CHECK(history.accepted().back().sampleKey == CommuteRouteHistory::kMaximumAcceptedRoutes); CHECK(history.failed().front().sampleKey == 1); CHECK(history.failed().back().sampleKey == CommuteRouteHistory::kMaximumFailedRoutes); } // A compatibility marker is no longer authority. Kind-compatible typed connections are required. { City city; city.xzon.at(64, 6) = 1; for (int column = 0; column <= 5; ++column) city.xbld.at(64, column) = 0x1D; city.xtxt.at(64, 0) = 0xFA; MsRand noConnectionRng(31); CommuteRouteProbe noConnection; CHECK(!commute_route(city, 64, 6, 1, 0, 100, noConnectionRng, {}, &noConnection)); const std::array wrong = {connection(64, 0, 3, 64, sim::BorderConnectionKind::Rail)}; MsRand wrongRng(31); CHECK(!commute_route(city, 64, 6, 1, 0, 100, wrongRng, wrong)); const std::array road = {connection(64, 0, 3, 64, sim::BorderConnectionKind::Road)}; MsRand roadRng(31); CommuteRouteProbe roadProbe; CHECK(commute_route(city, 64, 6, 1, 0, 100, roadRng, road, &roadProbe)); CHECK(roadProbe.stop == CommuteRouteStop::BorderConnection); } // Highway and rail exits retain their own connection kinds; connection order cannot change the result. { City highway; highway.xzon.at(64, 10) = 1; highway.xbld.at(64, 9) = 0x1D; highway.xbld.at(64, 8) = xbld::kOnrampFirst; for (int column = 0; column <= 7; ++column) highway.xbld.at(64, column) = xbld::kHighwayLR; const std::array highwayConnections = { connection(64, 0, 3, 64, sim::BorderConnectionKind::Road), connection(64, 0, 3, 64, sim::BorderConnectionKind::Highway)}; MsRand highwayRng(33); CommuteRouteProbe highwayProbe; CHECK(commute_route(highway, 64, 10, 1, 0, 100, highwayRng, highwayConnections, &highwayProbe)); CHECK(highwayProbe.stop == CommuteRouteStop::BorderConnection); City rail; rail.xzon.at(64, 6) = 1; rail.xbld.at(64, 5) = xbld::kRailDepot; for (int column = 0; column <= 4; ++column) rail.xbld.at(64, column) = 0x2C; const std::array roadOnly = { connection(64, 0, 3, 64, sim::BorderConnectionKind::Road)}; MsRand roadOnlyRng(35); CHECK(!commute_route(rail, 64, 6, 1, 0, 100, roadOnlyRng, roadOnly)); const std::array railConnections = { connection(64, 0, 3, 64, sim::BorderConnectionKind::Rail), connection(64, 0, 3, 64, sim::BorderConnectionKind::Road)}; MsRand railRng(35); CommuteRouteProbe railProbe; CHECK(commute_route(rail, 64, 6, 1, 0, 100, railRng, railConnections, &railProbe)); CHECK(railProbe.stop == CommuteRouteStop::BorderConnection); CHECK(railProbe.modeSteps[static_cast(CommuteTravelMode::Rail)] > 0); } // Read-only assessments are stable and leave both the traffic field and caller RNG ownership alone. { City city; diamond_city(city); const auto trafficBefore = city.xtrf.cells; CommuteRouteProbe first, second; CHECK(commute_route_assess(city, 64, 64, 1, 0, 100, {}, &first)); CHECK(commute_route_assess(city, 64, 64, 1, 0, 100, {}, &second)); CHECK(first.pathHash == second.pathHash); CHECK(first.generalizedCost == second.generalizedCost); CHECK(city.xtrf.cells == trafficBefore); } // Bus, rail, and subway use their own line-haul costs and report their accepted path modes. { City bus; bus.xzon.at(64, 64) = 1; bus.xbld.at(64, 65) = xbld::kBusDepot; for (int column = 66; column <= 72; ++column) bus.xbld.at(64, column) = 0x1D; bus.xbld.at(64, 73) = xbld::kBusDepot; bus.xzon.at(64, 74) = 3; MsRand busRng(37); CommuteRouteProbe busProbe; CHECK(commute_route(bus, 64, 64, 1, 0, 100, busRng, {}, &busProbe)); CHECK(busProbe.generalizedCost == 27); CHECK(busProbe.modeSteps[static_cast(CommuteTravelMode::Bus)] > 0); CHECK(busProbe.modeSteps[static_cast(CommuteTravelMode::Car)] == 0); City rail; rail.xzon.at(64, 64) = 1; rail.xbld.at(64, 65) = xbld::kRailDepot; for (int column = 66; column <= 72; ++column) rail.xbld.at(64, column) = 0x2C; rail.xbld.at(64, 73) = xbld::kRailDepot; rail.xzon.at(64, 74) = 3; MsRand railRng(37); CommuteRouteProbe railProbe; CHECK(commute_route(rail, 64, 64, 1, 0, 100, railRng, {}, &railProbe)); CHECK(railProbe.generalizedCost == 20); CHECK(railProbe.modeSteps[static_cast(CommuteTravelMode::Rail)] > 0); CHECK(railProbe.modeSteps[static_cast(CommuteTravelMode::Bus)] == 0); City subway; subway.xzon.at(64, 64) = 1; subway.xbld.at(64, 65) = xbld::kSubwayStation; for (int column = 66; column <= 72; ++column) subway.xund.at(64, column) = 1; subway.xbld.at(64, 73) = xbld::kSubwayStation; subway.xzon.at(64, 74) = 3; MsRand subwayRng(37); CommuteRouteProbe subwayProbe; CHECK(commute_route(subway, 64, 64, 1, 0, 100, subwayRng, {}, &subwayProbe)); CHECK(subwayProbe.generalizedCost == 13); CHECK(subwayProbe.modeSteps[static_cast(CommuteTravelMode::Subway)] > 0); CHECK(subwayProbe.modeSteps[static_cast(CommuteTravelMode::Bus)] == 0); CHECK(subwayProbe.generalizedCost < railProbe.generalizedCost); } // Transit efficiency divides only service cost in basis points. Walking access remains one cost; // 0% stops the mode explicitly, 50% doubles its service portion, and 150% retains the fractional // diminishing-return benefit that integer per-step costs would otherwise erase. { City rail; rail.xzon.at(64, 64) = 1; rail.xbld.at(64, 65) = xbld::kRailDepot; for (int column = 66; column <= 72; ++column) rail.xbld.at(64, column) = 0x2C; rail.xbld.at(64, 73) = xbld::kRailDepot; rail.xzon.at(64, 74) = 3; auto railConfig = [](int efficiency) { CommuteRouteConfig config; config.serviceEfficiency.modeBasisPoints[static_cast( CommuteTravelMode::Rail)] = efficiency; return config; }; City halfCity = rail; MsRand halfRng(41); CommuteRouteProbe half; CHECK(commute_route(halfCity, 64, 64, 1, 0, 39, halfRng, {}, &half, railConfig(5000))); CHECK(half.generalizedCostBasisPoints == 390000); CHECK(half.generalizedCost == 39); City halfLimitedCity = rail; MsRand halfLimitedRng(41); CommuteRouteProbe halfLimited; CHECK(!commute_route(halfLimitedCity, 64, 64, 1, 0, 38, halfLimitedRng, {}, &halfLimited, railConfig(5000))); CHECK(halfLimited.stop == CommuteRouteStop::CostLimit); City overCity = rail; MsRand overRng(41); CommuteRouteProbe over; CHECK(commute_route(overCity, 64, 64, 1, 0, 100, overRng, {}, &over, railConfig(11852))); CHECK(over.generalizedCostBasisPoints == 170312); CHECK(over.generalizedCost == 18); City stoppedCity = rail; MsRand stoppedRng(41); const std::uint32_t stoppedState = stoppedRng.state(); CommuteRouteProbe stopped; CHECK(!commute_route(stoppedCity, 64, 64, 1, 0, 100, stoppedRng, {}, &stopped, railConfig(0))); CHECK(stopped.stop == CommuteRouteStop::ServiceUnavailable); CHECK(stopped.accessCandidates > 0 && stopped.randomDraws == 0); CHECK(stoppedRng.state() == stoppedState); City implicitFull = rail; City explicitFull = rail; MsRand implicitRng(43), explicitRng(43); CommuteRouteProbe implicitProbe, explicitProbe; CHECK(commute_route(implicitFull, 64, 64, 1, 4, 100, implicitRng, {}, &implicitProbe)); CHECK(commute_route(explicitFull, 64, 64, 1, 4, 100, explicitRng, {}, &explicitProbe, railConfig(10000))); CHECK(implicitProbe.pathHash == explicitProbe.pathHash); CHECK(implicitProbe.generalizedCostBasisPoints == explicitProbe.generalizedCostBasisPoints); CHECK(implicitFull.xtrf.cells == explicitFull.xtrf.cells); CHECK(implicitRng.state() == explicitRng.state()); CommuteRouteConfig invalid = railConfig(12001); MsRand invalidRng(45); CommuteRouteProbe invalidProbe; CHECK(!commute_route(rail, 64, 64, 1, 0, 100, invalidRng, {}, &invalidProbe, invalid)); CHECK(invalidProbe.stop == CommuteRouteStop::InvalidConfiguration); } if (failures == 0) { std::puts("commute routing: costs, access, congestion, stochastic bounds, modes, and typed borders passed"); return 0; } std::printf("commute routing: %d failure(s)\n", failures); return 1; }