#include "reserve_strata.hpp" #include "evo/core/random.hpp" #include #include #include #include #include #include #include #include #include namespace { using namespace evo::fixture; using evo::experiment::ReserveStrata; // Cost control: retire dead rows while retaining original identities and tie order. // It still invokes the historical scalar stepping rule, including its sort/scratch. struct CompactIndividual { IndividualRun run; std::vector identities; std::uint64_t next_identity{}; explicit CompactIndividual(const IndividualRun& source) : run{source} { next_identity = source.members.size(); for (std::size_t index = 0; index < source.members.size(); ++index) { identities.push_back(index); } compact(); } void compact() { std::size_t written = 0; for (std::size_t index = 0; index < run.members.size(); ++index) { if (run.members[index].alive) { run.members[written] = run.members[index]; identities[written] = identities[index]; ++written; } } run.members.resize(written); identities.resize(written); } void step() { run.step(); while (identities.size() < run.members.size()) { identities.push_back(next_identity++); } compact(); } [[nodiscard]] Census census() const { return run.census(); } [[nodiscard]] std::vector portrait() const { std::vector result; for (std::size_t index = 0; index < run.members.size(); ++index) { const auto& member = run.members[index]; result.push_back({identities[index], member.age, member.energy, member.matter}); } return result; } }; std::vector identified(const IndividualRun& run) { std::vector result; for (std::size_t index = 0; index < run.members.size(); ++index) { const auto& member = run.members[index]; if (member.alive) { result.push_back({index, member.age, member.energy, member.matter}); } } return result; } void matches(const IndividualRun& individual, const ReserveStrata& strata) { insist(individual.census() == strata.census(), "strata census mismatch"); insist(identified(individual) == strata.portrait(), "strata identified portrait mismatch"); insist(individual.members.size() == strata.next_identity(), "strata identity mismatch"); insist(individual.environment == strata.environment(), "strata environment mismatch"); insist(individual.matter_store == strata.matter_store(), "strata matter store mismatch"); insist(individual.detritus_energy == strata.detritus_energy(), "strata energy sink mismatch"); insist(individual.detritus_matter == strata.detritus_matter(), "strata matter sink mismatch"); } void controls() { // Same age, distinct reserves, a compressed range and a partially funded birth // range. Census-only comparison cannot detect paying the wrong parent. auto contested = founding_contested(0, 6); contested.members = {{50, 120, 80}, {50, 120, 80}, {50, 120, 80}, {50, 200, 90}}; ReserveStrata partial{contested}; insist(partial.groups().size() == 2, "strata did not compress identical members"); for (unsigned tick = 0; tick < 80; ++tick) { contested.step(); partial.step(); matches(contested, partial); } insist(partial.census().births > 0, "strata control never reproduced"); auto fragmented = founding_individuals(5); fragmented.members = { {10, 0, 5, false}, {10, 1, 5}, {10, 1, 5}, {10, 0, 5, false}, {10, 200, 5}}; ReserveStrata gaps{fragmented}; fragmented.step(); gaps.step(); matches(fragmented, gaps); insist(gaps.census().living == 1, "strata did not preserve viable outlier"); insist(gaps.portrait().front().identity == 4, "strata compacted away identity"); auto empty = founding_individuals(0); ReserveStrata vacant{empty}; empty.step(); vacant.step(); matches(empty, vacant); const auto source = founding_individuals(100); ReserveStrata whole{source}; ReserveStrata partitioned{source}; whole.advance(401); partitioned.advance(0); partitioned.advance(1); partitioned.advance(63); partitioned.advance(337); insist(whole.portrait() == partitioned.portrait() && whole.census() == partitioned.census() && whole.total_energy() == partitioned.total_energy() && whole.total_matter() == partitioned.total_matter() && whole.environment() == partitioned.environment() && whole.matter_store() == partitioned.matter_store() && whole.next_identity() == partitioned.next_identity(), "strata partition mismatch"); auto invalid = source; invalid.rule.threshold_energy = invalid.rule.investment_energy - 1; bool rejected = false; try { (void)ReserveStrata{invalid}; } catch (const ModelViolation&) { rejected = true; } insist(rejected, "strata admitted unsupported underfunded rule"); rejected = false; try { (void)evo::experiment::checked_product(std::numeric_limits::max(), 2); } catch (const ModelViolation&) { rejected = true; } insist(rejected, "strata product overflow was not rejected"); } IndividualRun seeded(std::uint64_t seed, std::uint64_t supply) { auto run = founding_individuals(0); run.rule.environment_energy = supply; evo::Random random{seed}; for (unsigned block = 0; block < 20; ++block) { // Fixed modulo fixture sampling; no gameplay distribution is selected. const Member member{random.next() % 400, random.next() % 180, random.next() % 150}; for (unsigned index = 0; index < 5; ++index) { run.members.push_back(member); } } return run; } void compare(IndividualRun reference, std::uint64_t seed, std::string_view split, std::uint64_t ticks) { ReserveStrata strata{reference}; CompactIndividual compact{reference}; const auto energy = reference.total_energy(); const auto matter = reference.total_matter(); std::size_t peak_rows = strata.groups().size(); std::uint64_t peak_living = reference.census().living; matches(reference, strata); for (std::uint64_t tick = 0; tick < ticks; ++tick) { reference.step(); strata.step(); compact.step(); matches(reference, strata); insist(compact.census() == reference.census() && compact.portrait() == strata.portrait() && compact.run.total_energy() == energy && compact.run.total_matter() == matter && compact.run.environment == reference.environment && compact.run.matter_store == reference.matter_store && compact.run.detritus_energy == reference.detritus_energy && compact.run.detritus_matter == reference.detritus_matter, "compact cost control diverged"); insist(reference.total_energy() == energy && strata.total_energy() == energy, "strata energy conservation failure"); insist(reference.total_matter() == matter && strata.total_matter() == matter, "strata matter conservation failure"); peak_rows = std::max(peak_rows, strata.groups().size()); peak_living = std::max(peak_living, strata.census().living); } std::cout << "{\"kind\":\"comparison\",\"split\":\"" << split << "\",\"seed\":" << seed << ",\"supply\":" << reference.rule.environment_energy << ",\"ticks\":" << ticks << ",\"identified_trajectory_exact\":true,\"false_extinction\":false," "\"population_gap_ppt\":0,\"energy_gap_ppt\":0,\"matter_gap_ppt\":0," "\"peak_strata\":" << peak_rows << ",\"peak_living\":" << peak_living << ",\"final_living\":" << strata.census().living << "}\n"; } template void timed(Run run, std::uint64_t supply, unsigned sample) { std::size_t peak_rows = 0, peak_payload = 0, peak_capacity = 0; const auto observe = [&] { std::size_t rows = 0, payload = 0, capacity = 0; if constexpr (std::is_same_v) { rows = run.members.size(); payload = rows * sizeof(Member); capacity = run.members.capacity() * sizeof(Member); } else if constexpr (std::is_same_v) { rows = run.run.members.size(); payload = rows * sizeof(Member) + run.identities.size() * sizeof(std::uint64_t); capacity = run.run.members.capacity() * sizeof(Member) + run.identities.capacity() * sizeof(std::uint64_t); } else { rows = run.groups().size(); payload = rows * sizeof(evo::experiment::ReserveStratum); capacity = run.capacity_bytes(); } peak_rows = std::max(peak_rows, rows); peak_payload = std::max(peak_payload, payload); peak_capacity = std::max(peak_capacity, capacity); }; observe(); const auto start = std::chrono::steady_clock::now(); for (unsigned tick = 0; tick < 10'000; ++tick) { run.step(); observe(); } const auto ns = std::chrono::duration_cast( std::chrono::steady_clock::now() - start) .count(); if (sample == 0) { return; } constexpr auto tier = [] { if constexpr (std::is_same_v) { return "historical_individual"; } else if constexpr (std::is_same_v) { return "compact_individual"; } else { return "exact_strata"; } }(); std::cout << "{\"kind\":\"timing\",\"tier\":\"" << tier << "\",\"supply\":" << supply << ",\"sample\":" << sample << ",\"elapsed_ns\":" << ns << ",\"peak_rows\":" << peak_rows << ",\"peak_payload_bytes\":" << peak_payload << ",\"peak_capacity_bytes\":" << peak_capacity << ",\"final_living\":" << run.census().living << "}\n"; } } // namespace int main(int argc, char** argv) try { if (argc > 2 || (argc == 2 && std::string_view{argv[1]} != "--verify")) { std::fputs("usage: evo-reserve-strata [--verify]\n", stderr); return 2; } std::cout << "{\"kind\":\"metadata\",\"candidate\":\"exact-reserve-strata-1\"," "\"reference\":\"resolution-fixture-2\",\"founders\":100," "\"production_qualified\":false,\"compiler\":\"" #if defined(__clang__) << "Clang " << __clang_version__ #elif defined(_MSC_VER) << "MSVC " << _MSC_VER #else << "GCC " << __VERSION__ #endif << "\",\"build\":\"" #if defined(NDEBUG) << "release" #else << "debug" #endif << "\"}\n"; controls(); for (const auto supply : {240U, 300U, 360U}) { Rule rule; rule.environment_energy = supply; compare(founding_individuals(100, rule), 0, "historical", 10'000); if (argc == 1) { for (unsigned sample = 0; sample <= 3; ++sample) { const auto source = founding_individuals(100, rule); if (sample % 2 == 0) { timed(source, supply, sample); timed(CompactIndividual{source}, supply, sample); timed(ReserveStrata{source}, supply, sample); } else { timed(ReserveStrata{source}, supply, sample); timed(CompactIndividual{source}, supply, sample); timed(source, supply, sample); } } } } for (std::uint64_t seed = 0; seed < 16; ++seed) { for (const auto supply : {180U, 240U, 300U, 360U, 480U}) { compare(seeded(seed, supply), seed, seed < 8 ? "development" : "holdout", 1'500); } } return 0; } catch (const evo::fixture::ModelViolation& error) { std::fprintf(stderr, "%s\n", error.what); return 1; } catch (const std::exception& error) { std::fprintf(stderr, "%s\n", error.what()); return 1; } catch (...) { std::fputs("unknown reserve-strata failure\n", stderr); return 1; }