Something went wrong. Try again.
[Mirror] Libreopolis is a cross-platform city simulation game fully compatible with SimCity 2000. gitlab.com/conasg/libreopolis
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149// Fixed-fixture ensemble characterization for the stochastic commute walker. These are statistical// contracts and diagnostic invariants, not a promise that any individual random path is canonical.#include "sc2k/sim/commute.hpp"
#include <algorithm>#include <array>#include <cstdio>
#include "test_harness.hpp"
namespace {using namespace sc2k;
struct Ensemble { int successes = 0; int destinations = 0; int borders = 0; int costLimits = 0; int exhausted = 0; int minWork = 1'000'000; int maxWork = 0; int minDepth = 1'000'000; int maxDepth = 0; long traffic = 0;};
template <class Build>Ensemble sample(Build build, int seeds, int weight = 10, int maxCost = 100) { Ensemble e; for (int seed = 1; seed <= seeds; ++seed) { City c; build(c); MsRand rng(static_cast<std::uint32_t>(seed)); CommuteTraceProbe p; const bool reached = commute_trace(c, 64, 64, 1, weight, maxCost, rng, &p); e.successes += reached; e.destinations += p.stop == CommuteTraceStop::Destination; e.borders += p.stop == CommuteTraceStop::BorderMarker; e.costLimits += p.stop == CommuteTraceStop::CostLimit; e.exhausted += p.stop == CommuteTraceStop::SearchExhausted; e.minWork = std::min(e.minWork, p.candidateDirections); e.maxWork = std::max(e.maxWork, p.candidateDirections); e.minDepth = std::min(e.minDepth, p.maxDepth); e.maxDepth = std::max(e.maxDepth, p.maxDepth); for (std::uint8_t value : c.xtrf.cells) e.traffic += value;
CHECK(p.randomDraws >= 1); CHECK(p.candidateDirections >= p.validSteps); CHECK(p.maxDepth >= 1); CHECK(p.finalCost <= p.effectiveMaxCost + 4); CHECK(reached == (p.stop == CommuteTraceStop::Destination || p.stop == CommuteTraceStop::BorderMarker)); } return e;}
void straight_city(City& c) { c.xzon.at(64, 64) = 1; for (int col = 65; col <= 72; ++col) c.xbld.at(64, col) = 0x1D; c.xzon.at(64, 73) = 3;}
void fork_city(City& c) { c.xzon.at(64, 64) = 1; c.xbld.at(64, 65) = 0x1D; for (int col = 66; col <= 68; ++col) c.xbld.at(64, col) = 0x1D; c.xzon.at(64, 69) = 3; for (int row = 63; row >= 50; --row) c.xbld.at(row, 65) = 0x1D;}
void diamond_city(City& c) { c.xzon.at(64, 64) = 1; c.xbld.at(64, 65) = 0x1D; for (int col = 66; col <= 72; ++col) { c.xbld.at(63, col) = 0x1D; c.xbld.at(65, col) = 0x1D; } c.xbld.at(63, 65) = c.xbld.at(65, 65) = 0x1D; c.xbld.at(64, 72) = 0x1D; c.xzon.at(64, 73) = 3;}} // namespace
int main() { constexpr int kSeeds = 512;
// A single unbranched route is invariant across the ensemble and always deposits road traffic. const Ensemble straight = sample(straight_city, kSeeds); CHECK(straight.successes == kSeeds); CHECK(straight.destinations == kSeeds); CHECK(straight.borders == 0 && straight.costLimits == 0 && straight.exhausted == 0); CHECK(straight.minDepth == straight.maxDepth); CHECK(straight.traffic > 0);
// A cheap job branch competing with an over-budget dead branch exposes seed-sensitive work and // the walker's global cost-limit termination: some reachable trips fail after choosing the dead arm. const Ensemble fork = sample(fork_city, kSeeds, 10, 18); CHECK(fork.successes > 0 && fork.successes < kSeeds); CHECK(fork.destinations == fork.successes); CHECK(fork.costLimits == kSeeds - fork.successes); CHECK(fork.minWork < fork.maxWork); CHECK(fork.minDepth < fork.maxDepth);
// Equal feasible arms converge on the same destination, yet the walker can revisit enough of the // diamond to hit its global cost limit. This is a second, less contrived source of false failure. // Aggregate traffic still uses both half-resolution branches across successful samples. const Ensemble diamond = sample(diamond_city, kSeeds); CHECK(diamond.successes > kSeeds / 2 && diamond.successes < 3 * kSeeds / 4); CHECK(diamond.costLimits + diamond.exhausted == kSeeds - diamond.successes); CHECK(diamond.minWork < diamond.maxWork); CHECK(diamond.traffic > straight.traffic);
// The special weight-one trip shortens the budget by exactly 25 percent before walking. { City c; straight_city(c); MsRand rng(1); CommuteTraceProbe p; (void)commute_trace(c, 64, 64, 1, 1, 100, rng, &p); CHECK(p.effectiveMaxCost == 75); }
// Current off-map success is keyed only by the generic compatibility marker. This deliberately // records the false-positive seam that typed border connections will replace in the next phase. { City c; c.xzon.at(64, 64) = 1; for (int col = 63; col >= 0; --col) c.xbld.at(64, col) = 0x1D; c.xtxt.at(64, 0) = 0xFA; MsRand rng(7); CommuteTraceProbe p; CHECK(commute_trace(c, 64, 64, 1, 0, 220, rng, &p)); CHECK(p.stop == CommuteTraceStop::BorderMarker); CHECK(p.trafficCellsStamped == 0); }
std::printf("commute characterization: straight %d/%d, fork %d/%d (%d cost), " "diamond %d/%d; work straight %d-%d fork %d-%d diamond %d-%d\n", straight.successes, kSeeds, fork.successes, kSeeds, fork.costLimits, diamond.successes, kSeeds, straight.minWork, straight.maxWork, fork.minWork, fork.maxWork, diamond.minWork, diamond.maxWork);
if (failures == 0) { std::puts("test_commute_characterization: ALL CHECKS PASSED"); return 0; } std::printf("test_commute_characterization: %d CHECK(S) FAILED\n", failures); return 1;}