From c237e4da97109a2fb74dbf107895134fe6e38a3a Mon Sep 17 00:00:00 2001 From: dingo Date: Sat, 26 Sep 2026 09:39:53 -0400 Subject: [PATCH] Measure aggregate cost and fidelity across scarcity scenarios --- .clang-tidy | 2 +- CMakeLists.txt | 8 + README.md | 3 + docs/ROADMAP.md | 6 + docs/STATUS.md | 19 +- docs/adr/0041-aggregate-budget.md | 47 ++ docs/adr/README.md | 2 + docs/biology/ECOLOGY_MODEL.md | 8 + docs/engineering/AGGREGATE_BUDGET.md | 98 +++ docs/engineering/VALIDATION.md | 25 + .../results/resolution-budget-msvc1944.jsonl | 111 ++++ tests/benchmarks/resolution_benchmark.cpp | 160 +++++ tests/equivalence_tests.cpp | 536 +-------------- tests/fixtures/resolution_model.hpp | 617 ++++++++++++++++++ tools/check-format.cmake | 2 +- 15 files changed, 1115 insertions(+), 529 deletions(-) create mode 100644 docs/adr/0041-aggregate-budget.md create mode 100644 docs/engineering/AGGREGATE_BUDGET.md create mode 100644 docs/engineering/results/resolution-budget-msvc1944.jsonl create mode 100644 tests/benchmarks/resolution_benchmark.cpp create mode 100644 tests/fixtures/resolution_model.hpp diff --git a/.clang-tidy b/.clang-tidy index 93915be..ede99fe 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -1,6 +1,6 @@ Checks: '-*,clang-analyzer-*,bugprone-*,performance-*,portability-*,-bugprone-easily-swappable-parameters' WarningsAsErrors: '*' -HeaderFilterRegex: '(include[\\/]evo|src)[\\/].*' +HeaderFilterRegex: '(include[\\/]evo|src|tests[\\/]fixtures)[\\/].*' FormatStyle: file CheckOptions: bugprone-narrowing-conversions.WarnOnIntegerToFloatingPointNarrowingConversion: true diff --git a/CMakeLists.txt b/CMakeLists.txt index cdc775b..0c6916a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -200,6 +200,14 @@ add_executable(evo-benchmark tests/benchmarks/simulation_benchmark.cpp) target_link_libraries(evo-benchmark PRIVATE evo::simulation) evo_owned_target(evo-benchmark) +add_executable(evo-resolution-benchmark tests/benchmarks/resolution_benchmark.cpp) +target_link_libraries(evo-resolution-benchmark PRIVATE evo::ecology) +evo_owned_target(evo-resolution-benchmark) +if(BUILD_TESTING) + add_test(NAME resolution-sensitivity COMMAND evo-resolution-benchmark --verify) + set_tests_properties(resolution-sensitivity PROPERTIES TIMEOUT 120) +endif() + add_executable(evo-spatial-benchmark tests/benchmarks/spatial_benchmark.cpp) target_link_libraries(evo-spatial-benchmark PRIVATE evo::simulation) evo_owned_target(evo-spatial-benchmark) diff --git a/README.md b/README.md index f160bc9..c4ff7ec 100644 --- a/README.md +++ b/README.md @@ -94,6 +94,7 @@ cmake --build --preset release ctest --preset release ./build/release/evo-benchmark ./build/release/evo-spatial-benchmark +./build/release/evo-resolution-benchmark ``` ASan uses UBSan too on supported Linux compilers. TSan has a separate `tsan` @@ -126,3 +127,5 @@ Persistent-world deployment still requires storage, ownership and backup gates. The project license has not been selected. Do not infer an open-source license from a public dependency's license. + +M4.6 reproduction, measurements and limits: [aggregate budget report](docs/engineering/AGGREGATE_BUDGET.md). diff --git a/docs/ROADMAP.md b/docs/ROADMAP.md index 3791ef8..feffb20 100644 --- a/docs/ROADMAP.md +++ b/docs/ROADMAP.md @@ -206,3 +206,9 @@ stays open. No date or production capacity is promised without staffing and benchmark data. Every release records simulation/world/network versions; biological upgrades use production snapshot → clone → migrate → accelerated comparison → approval. + + +M4.6 completed as measurement tooling and a policy review on 2026-09-26; see +[aggregate budget report](engineering/AGGREGATE_BUDGET.md). No automatic tier policy +qualified. M5 may proceed independently; production tier selection needs the +fidelity and target-host follow-ups listed in that report. diff --git a/docs/STATUS.md b/docs/STATUS.md index de8c973..d5cda9a 100644 --- a/docs/STATUS.md +++ b/docs/STATUS.md @@ -1,13 +1,13 @@ # Project status and next steps -Updated 2026-09-23. The repository contains an engineering foundation and a +Updated 2026-09-26. The repository contains an engineering foundation and a deterministic headless proof. It does not yet contain cellular gameplay or a persistent simulation server. The founding vision remains unchanged. ## Completed implementation - Vision, architectural pillars, risks, open-question register, architecture - diagrams, biological contracts, engineering constitution and 40 ADRs. + diagrams, biological contracts, engineering constitution and 41 ADRs. Proposed/deferred choices remain distinct from accepted proof decisions. - M0–M4 task breakdowns with dependencies, acceptance evidence and risks, plus M5–M14 gates in [ROADMAP.md](ROADMAP.md). @@ -61,7 +61,8 @@ for the bounded proof; the general production ECS remains a later measured choic | Priority | Work | Completion evidence | |---|---|---| -| 1 | M4.6 budget and review the aggregate model | CPU and memory against the measured error regimes; the tier policy has to survive a resolution boundary that is not monotone | +| 1 | M5 small sparse voxel prototype | Deterministic generation, modification, snapshot and restore; select scoped storage contract first | +| Research | Improve aggregate fidelity and measure Linux/target-host costs | M4.6 found no qualifying width; see the aggregate budget report | | 2 | Revisit the ADR-0031/0032 reproduction cost model | Reproduction is affordable only at the genome schema's floor; decide whether that is intended before M4 builds cohorts on it | | Delivery gate | Add/update the dev workload when a deployable increment needs it; Argo is ready | Observe Job/pod completion separately from image publication; no World0 changes | @@ -506,3 +507,15 @@ commit. A fifty-tick interval shedding 10,480 against 6,950 held while drawing 13,080 in reproduced it. Arrivals are now the first leg, matching the order the arithmetic was checked in, with a regression covering it. Native Release, analysis, ASan and formatting passed 38/38; Linux CI has not run yet. + + +## M4.6 - aggregate budget review (2026-09-26) +Completed test-only shared model, CPU/retained-memory benchmark, and 15-scenario +scarcity sensitivity report. The partial `tests/fixtures/resolution_model.hpp` +was integrated with existing M4.5 controls, including contested parent allocation. +No production library, simulation format, deployment or gameplay rule changed. +See [the report](engineering/AGGREGATE_BUDGET.md) and ADR-0041. None of the five +widths passes the proposed fidelity gate across supplies 240/300/360; automatic tier +selection remains disabled. The next implementation milestone is M5's sparse voxel +prototype; aggregate-model improvement and Linux/target-host measurement remain +prerequisites for a production tier policy. See VALIDATION.md for checks actually run. diff --git a/docs/adr/0041-aggregate-budget.md b/docs/adr/0041-aggregate-budget.md new file mode 100644 index 0000000..821970a --- /dev/null +++ b/docs/adr/0041-aggregate-budget.md @@ -0,0 +1,47 @@ +# ADR-0041: Measure aggregate cost before enabling tier selection + +## Status +Accepted for M4.6 measurement tooling only. No runtime tier selector or biological +rule is adopted. Uses the breeding-order correction already committed in 55a728a. Historical +M4.5 results are retained; the new run independently checks them. + +## Context +M4.5 detected large resolution errors, and review detected mismatched parent +priority at capacity. Timing those differing models would not justify a policy. + +## Decision +Share one test-only fixture between comparison and benchmark. Both representations +allocate breeding slots oldest-first. A contested-slot control compares individual +portraits, not only totals. Preserve the real collapse/advance/materialize integration. +Measure 100 founders for 10,000 ticks at widths 10/25/50/100/200 and energy supplies +240/300/360 per tick. Supplies are independent experiments, not gameplay constants. +Report population, energy and matter error plus false extinction. Benchmark step +loops with a warmup and three fresh measured repetitions, report steady-clock elapsed +nanoseconds, final census and peak row payload/vector capacity bytes. Timing excludes +setup, conservation checks and conversion; these costs are not production budgets. +No portable wall-time assertion. Compare costs only alongside fidelity results. + +Provisional research gate: no false extinction and final population, energy and +matter errors each <=100 parts per thousand in every tested scarcity scenario. +This is a recommendation for experiments, not an accepted production error budget. +Failure keeps automatic tier selection disabled; do not pick a width merely because +it runs faster. Even passing would require trajectory, mixed-genome and host evidence. + +## Alternatives Considered +Copying the fixture would allow rules to drift. Timing the validation loop measures +assertions/ledger verification instead of model advancement. A runtime automatic +selector is premature before any resolution has demonstrated acceptable fidelity. + +## Benefits +Costs and ecological error refer to the same fixture. Raw results are reproducible. +## Costs +The individual fixture retains dead rows and sorts eligible parents each step; +report that cost rather than extrapolating to a future compact active ECS. +## Risks +One synthetic genome, deterministic founding distribution, no production physics, +network, scheduler, conversion overhead or server hardware measurement. Payload and +capacity bytes exclude allocator metadata, temporary vectors, stack and RSS. +## Consequences +Keep controls and conservation in CTest. Archive native measurements with compiler, +build and hardware metadata. Do not change simulation versions: only a test model +changes. Repeat Linux/target-host measurements before any capacity promise. diff --git a/docs/adr/README.md b/docs/adr/README.md index e46874f..7bd7f02 100644 --- a/docs/adr/README.md +++ b/docs/adr/README.md @@ -39,3 +39,5 @@ transitive license obligations, maintenance/security review, owner and removal boundary. Candidate performance and binary-size comparisons are hypotheses until benchmarked. Primary sources were reviewed during the initial September 2026 design. No unadopted candidate is a link dependency of the proof. + +- [ADR-0041: Aggregate cost and fidelity budget](0041-aggregate-budget.md) - M4.6 measurement only. diff --git a/docs/biology/ECOLOGY_MODEL.md b/docs/biology/ECOLOGY_MODEL.md index 97c5cac..75bcaa0 100644 --- a/docs/biology/ECOLOGY_MODEL.md +++ b/docs/biology/ECOLOGY_MODEL.md @@ -315,3 +315,11 @@ model, one scarce axis and one initial distribution, so the numbers describe thi fixture; interval-length error is unmeasured, because the aggregate run sub-steps at one tick to keep the averaging error attributable; and nothing here is distributed or persistent. + + +## M4.6 budget review +[ADR-0041](../adr/0041-aggregate-budget.md) and the +[measurement report](../engineering/AGGREGATE_BUDGET.md) evaluate cost together with +population, energy and matter errors. No tested width qualifies across the scarcity +sweep. Faster execution alone does not authorize a resolution switch; automatic +selection remains disabled. This fixture is not a production ecological model. diff --git a/docs/engineering/AGGREGATE_BUDGET.md b/docs/engineering/AGGREGATE_BUDGET.md new file mode 100644 index 0000000..1f9f133 --- /dev/null +++ b/docs/engineering/AGGREGATE_BUDGET.md @@ -0,0 +1,98 @@ +# M4.6 aggregate budget and sensitivity report + +## Decision +M4.6 measurement work is complete; automatic tier selection remains **disabled**. +None of the tested resolutions meets the provisional research gate in ADR-0041. +This is a measured rejection of this model as a production tier policy, not a +promise to simulate every organism individually at world scale. + +## Reproduce +```sh +cmake --preset release +cmake --build --preset release +ctest --preset release +./build/release/evo-resolution-benchmark > resolution-budget.jsonl +``` +Windows: `tools/windows-check.ps1 -Preset release -Action check`, then +`build/release/evo-resolution-benchmark.exe`. `--verify` runs conservation and +sensitivity without timings and is registered in CTest. Exceptions fail the run. +The executable rejects other arguments. Use a Release build for cost comparisons. + +## Method and host +Measured 2026-09-26 on Windows, AMD Ryzen 7 9800X3D, 8 cores/16 logical processors, +MSVC 19.44 x64, CMake Release /O2, single simulation thread, no CPU affinity or +frequency lock. The host is a workstation, not the R730xd. Raw data: +[resolution-budget-msvc1944.jsonl](results/resolution-budget-msvc1944.jsonl). +Base checkout: 55a728a plus the M4.6 working changes. Fixture version 2 identifies +the shared configurable model, not a persistent simulation version. + +100 founders, 10,000 ticks, capacity 200; no randomness. Five age widths and energy +supply 240/300/360; only supply varies between scarcity scenarios. Four identical +matter channels remain abundant. A warmup and three fresh measured repetitions +per representation/scenario alternate timing order. Timings include step allocation, +sorting and peak-counter observation, exclude setup/copy, logging, conservation +checks, cohort validation and collapse/advance/materialize transactions. Terminal +censuses are output so the compiler cannot discard the computation. + +Peak payload is vector size times row sizeof; peak capacity includes spare vector +storage. Neither includes allocator metadata, per-step eligible-index scratch, +object/stack bytes or RSS. The individual fixture retains every dead row and scans +it, whereas aggregate empty classes are erased. This inflates its cost relative to +a future compact active ECS. These figures compare these fixtures only. + +## Baseline costs (supply 300) +Median elapsed milliseconds for all 10,000 ticks. Extinct runs still execute the +remaining empty steps, so their low cost must not be treated as useful throughput. + +| Width | Individual ms | Aggregate ms | Peak individual capacity bytes | Peak aggregate capacity bytes | Aggregate survives | +|---|---:|---:|---:|---:|---| +| 10 | 27.107 | 0.250 | 122528 | 360 | yes | +| 25 | 27.256 | 0.054 | 122528 | 160 | no | +| 50 | 29.190 | 0.135 | 122528 | 120 | yes | +| 100 | 27.180 | 0.045 | 122528 | 80 | no | +| 200 | 27.263 | 0.045 | 122528 | 80 | no | + +## Sensitivity +Errors are integer parts per thousand: abs(a-b)/max(a,b), rounded down; zero when +both values are zero. Matter is per channel; all four channels are identical here. +The proposed gate requires no false extinction at any tick and final population, +energy and matter error each <=100 ppt in **every** scenario. This threshold was +written before the run; it is a research recommendation, not a biological law. +Maximum errors over the trajectory are also recorded in the raw data and are often +much worse than the terminal errors. No candidate passes even the terminal gate. + +| Width | Supply | Individual/aggregate survivors | Population error | Energy error | Matter error | False extinction | +|---|---|---|---:|---:|---:|---| +| 10 | 240 | 108/120 | 100 | 127 | 346 | no | +| 10 | 300 | 75/100 | 250 | 24 | 724 | no | +| 10 | 360 | 130/130 | 0 | 34 | 531 | no | +| 25 | 240 | 108/100 | 74 | 272 | 189 | no | +| 25 | 300 | 75/0 | 1000 | 1000 | 1000 | yes | +| 25 | 360 | 130/130 | 0 | 151 | 657 | no | +| 50 | 240 | 108/0 | 1000 | 1000 | 1000 | yes | +| 50 | 300 | 75/120 | 375 | 43 | 35 | no | +| 50 | 360 | 130/180 | 277 | 217 | 589 | no | +| 100 | 240 | 108/0 | 1000 | 1000 | 1000 | yes | +| 100 | 300 | 75/0 | 1000 | 1000 | 1000 | yes | +| 100 | 360 | 130/0 | 1000 | 1000 | 1000 | yes | +| 200 | 240 | 108/0 | 1000 | 1000 | 1000 | yes | +| 200 | 300 | 75/0 | 1000 | 1000 | 1000 | yes | +| 200 | 360 | 130/0 | 1000 | 1000 | 1000 | yes | + +The shared fixture reproduces M4.5's baseline survivor results. Width 10 avoids +extinction but has large matter errors; width 50 fails under reduced energy supply. +Widths 100 and 200 go extinct in every scenario. Conservation passed at every tick +for both representations in all 15 scenarios; that does not imply ecological fidelity. + +## Limits and next steps +This is one genome and one deterministic founding distribution. No ensemble, +spatial interactions, oxygen, predation, network load or production threading is +represented. There is no R730xd capacity estimate or Kubernetes resource request. +CPU measurements are wall time on an uncontrolled host and are reports, not CI gates. + +Before adopting a tier model: preserve within-class reserve variation, test multiple +genomes and founding distributions, measure trajectory/distribution errors, and +compare an individual store with dead rows retired. Measure conversions, catch-up +and complete region workloads on Linux and the target host. Calibrate acceptable +error budgets with biological/playtest requirements. M5 can independently begin its +small sparse-voxel prototype; no terrain dependency is added to this fixture. diff --git a/docs/engineering/VALIDATION.md b/docs/engineering/VALIDATION.md index d304f0e..bac8d2e 100644 --- a/docs/engineering/VALIDATION.md +++ b/docs/engineering/VALIDATION.md @@ -1373,3 +1373,28 @@ order-sensitive stage, and the environment is sized so intake never depletes it, which the fixture asserts rather than assumes. A future stage introducing another order-sensitive decision would need its own control before these numbers could be trusted again. + + +## M4.6 local validation - 2026-09-26 + +Base 55a728a plus working M4.6 changes. Integrated the prior agent's untracked +`tests/fixtures/resolution_model.hpp`; no production library changes or format +version changes. Existing contested-parent and round-trip controls remain active. + +- Native MSVC 19.44 Release configure/build and CTest: 39/39 passed. +- Native analysis (clang-tidy 19, including fixture headers): build and 39/39 passed. + Initial analysis found a throwing error-reporting boundary in the benchmark; + replaced catch-handler stream writes with stdio diagnostics and reran successfully. +- Native AddressSanitizer: build and 39/39 passed. Windows ASan does not cover UBSan. +- clang-format 19 format-check passed, now including tests/*.hpp. Diff whitespace + check passed. +- Full Release benchmark: 15 scenarios, one warmup plus three measured repetitions + per representation. Raw results and host/method caveats are in + [AGGREGATE_BUDGET.md](AGGREGATE_BUDGET.md). All scenarios conserved every channel; + none qualified across all scarcity inputs under the proposed research gate. +- Linux CI, UBSan, target-server profiling and production deployment were not run. + No World0 resources or user source documents were changed. + +The final exception-handler-only adjustment was compiled in analysis; Release and +ASan benchmark targets were then rebuilt and their resolution tests rerun. No +performance or model algorithm changed after the archived timing run. diff --git a/docs/engineering/results/resolution-budget-msvc1944.jsonl b/docs/engineering/results/resolution-budget-msvc1944.jsonl new file mode 100644 index 0000000..fc31e7d --- /dev/null +++ b/docs/engineering/results/resolution-budget-msvc1944.jsonl @@ -0,0 +1,111 @@ +{"kind":"metadata","model":"resolution-fixture-2","founders":100,"ticks":10000,"randomness":"none","warmups":1,"samples":3,"compiler":"MSVC 1944","build":"release"} +{"kind":"sensitivity","supply":240,"width":10,"individual_living":108,"aggregate_living":120,"population_gap_ppt":100,"energy_gap_ppt":127,"matter_gap_ppt":346,"max_population_gap_ppt":725,"max_energy_gap_ppt":650,"max_matter_gap_ppt":932,"false_extinction":false,"research_gate_pass":false} +{"kind":"timing","tier":"aggregate","supply":240,"width":10,"sample":1,"elapsed_ns":146800,"peak_rows":6,"peak_payload_bytes":240,"peak_capacity_bytes":240,"living":120,"energy":6000,"matter_per_channel":134340} +{"kind":"timing","tier":"individual","supply":240,"width":10,"sample":1,"elapsed_ns":25031400,"peak_rows":2471,"peak_payload_bytes":79072,"peak_capacity_bytes":81696,"living":108,"energy":6875,"matter_per_channel":87828} +{"kind":"timing","tier":"individual","supply":240,"width":10,"sample":2,"elapsed_ns":25904400,"peak_rows":2471,"peak_payload_bytes":79072,"peak_capacity_bytes":81696,"living":108,"energy":6875,"matter_per_channel":87828} +{"kind":"timing","tier":"aggregate","supply":240,"width":10,"sample":2,"elapsed_ns":141600,"peak_rows":6,"peak_payload_bytes":240,"peak_capacity_bytes":240,"living":120,"energy":6000,"matter_per_channel":134340} +{"kind":"timing","tier":"aggregate","supply":240,"width":10,"sample":3,"elapsed_ns":140900,"peak_rows":6,"peak_payload_bytes":240,"peak_capacity_bytes":240,"living":120,"energy":6000,"matter_per_channel":134340} +{"kind":"timing","tier":"individual","supply":240,"width":10,"sample":3,"elapsed_ns":25541800,"peak_rows":2471,"peak_payload_bytes":79072,"peak_capacity_bytes":81696,"living":108,"energy":6875,"matter_per_channel":87828} +{"kind":"sensitivity","supply":300,"width":10,"individual_living":75,"aggregate_living":100,"population_gap_ppt":250,"energy_gap_ppt":24,"matter_gap_ppt":724,"max_population_gap_ppt":530,"max_energy_gap_ppt":485,"max_matter_gap_ppt":935,"false_extinction":false,"research_gate_pass":false} +{"kind":"timing","tier":"aggregate","supply":300,"width":10,"sample":1,"elapsed_ns":249600,"peak_rows":7,"peak_payload_bytes":280,"peak_capacity_bytes":360,"living":100,"energy":6120,"matter_per_channel":14360} +{"kind":"timing","tier":"individual","supply":300,"width":10,"sample":1,"elapsed_ns":27547800,"peak_rows":2625,"peak_payload_bytes":84000,"peak_capacity_bytes":122528,"living":75,"energy":6275,"matter_per_channel":52115} +{"kind":"timing","tier":"individual","supply":300,"width":10,"sample":2,"elapsed_ns":26594700,"peak_rows":2625,"peak_payload_bytes":84000,"peak_capacity_bytes":122528,"living":75,"energy":6275,"matter_per_channel":52115} +{"kind":"timing","tier":"aggregate","supply":300,"width":10,"sample":2,"elapsed_ns":252100,"peak_rows":7,"peak_payload_bytes":280,"peak_capacity_bytes":360,"living":100,"energy":6120,"matter_per_channel":14360} +{"kind":"timing","tier":"aggregate","supply":300,"width":10,"sample":3,"elapsed_ns":247300,"peak_rows":7,"peak_payload_bytes":280,"peak_capacity_bytes":360,"living":100,"energy":6120,"matter_per_channel":14360} +{"kind":"timing","tier":"individual","supply":300,"width":10,"sample":3,"elapsed_ns":27106700,"peak_rows":2625,"peak_payload_bytes":84000,"peak_capacity_bytes":122528,"living":75,"energy":6275,"matter_per_channel":52115} +{"kind":"sensitivity","supply":360,"width":10,"individual_living":130,"aggregate_living":130,"population_gap_ppt":0,"energy_gap_ppt":34,"matter_gap_ppt":531,"max_population_gap_ppt":400,"max_energy_gap_ppt":501,"max_matter_gap_ppt":923,"false_extinction":false,"research_gate_pass":false} +{"kind":"timing","tier":"aggregate","supply":360,"width":10,"sample":1,"elapsed_ns":200600,"peak_rows":8,"peak_payload_bytes":320,"peak_capacity_bytes":360,"living":130,"energy":6800,"matter_per_channel":104940} +{"kind":"timing","tier":"individual","supply":360,"width":10,"sample":1,"elapsed_ns":32192200,"peak_rows":3156,"peak_payload_bytes":100992,"peak_capacity_bytes":122528,"living":130,"energy":7040,"matter_per_channel":49113} +{"kind":"timing","tier":"individual","supply":360,"width":10,"sample":2,"elapsed_ns":32369500,"peak_rows":3156,"peak_payload_bytes":100992,"peak_capacity_bytes":122528,"living":130,"energy":7040,"matter_per_channel":49113} +{"kind":"timing","tier":"aggregate","supply":360,"width":10,"sample":2,"elapsed_ns":207300,"peak_rows":8,"peak_payload_bytes":320,"peak_capacity_bytes":360,"living":130,"energy":6800,"matter_per_channel":104940} +{"kind":"timing","tier":"aggregate","supply":360,"width":10,"sample":3,"elapsed_ns":198700,"peak_rows":8,"peak_payload_bytes":320,"peak_capacity_bytes":360,"living":130,"energy":6800,"matter_per_channel":104940} +{"kind":"timing","tier":"individual","supply":360,"width":10,"sample":3,"elapsed_ns":32798600,"peak_rows":3156,"peak_payload_bytes":100992,"peak_capacity_bytes":122528,"living":130,"energy":7040,"matter_per_channel":49113} +{"kind":"policy","width":10,"passes_all_scenarios":false} +{"kind":"sensitivity","supply":240,"width":25,"individual_living":108,"aggregate_living":100,"population_gap_ppt":74,"energy_gap_ppt":272,"matter_gap_ppt":189,"max_population_gap_ppt":580,"max_energy_gap_ppt":718,"max_matter_gap_ppt":923,"false_extinction":false,"research_gate_pass":false} +{"kind":"timing","tier":"aggregate","supply":240,"width":25,"sample":1,"elapsed_ns":126900,"peak_rows":3,"peak_payload_bytes":120,"peak_capacity_bytes":120,"living":100,"energy":5000,"matter_per_channel":71150} +{"kind":"timing","tier":"individual","supply":240,"width":25,"sample":1,"elapsed_ns":24614400,"peak_rows":2471,"peak_payload_bytes":79072,"peak_capacity_bytes":81696,"living":108,"energy":6875,"matter_per_channel":87828} +{"kind":"timing","tier":"individual","supply":240,"width":25,"sample":2,"elapsed_ns":26241400,"peak_rows":2471,"peak_payload_bytes":79072,"peak_capacity_bytes":81696,"living":108,"energy":6875,"matter_per_channel":87828} +{"kind":"timing","tier":"aggregate","supply":240,"width":25,"sample":2,"elapsed_ns":127800,"peak_rows":3,"peak_payload_bytes":120,"peak_capacity_bytes":120,"living":100,"energy":5000,"matter_per_channel":71150} +{"kind":"timing","tier":"aggregate","supply":240,"width":25,"sample":3,"elapsed_ns":126800,"peak_rows":3,"peak_payload_bytes":120,"peak_capacity_bytes":120,"living":100,"energy":5000,"matter_per_channel":71150} +{"kind":"timing","tier":"individual","supply":240,"width":25,"sample":3,"elapsed_ns":25096400,"peak_rows":2471,"peak_payload_bytes":79072,"peak_capacity_bytes":81696,"living":108,"energy":6875,"matter_per_channel":87828} +{"kind":"sensitivity","supply":300,"width":25,"individual_living":75,"aggregate_living":0,"population_gap_ppt":1000,"energy_gap_ppt":1000,"matter_gap_ppt":1000,"max_population_gap_ppt":1000,"max_energy_gap_ppt":1000,"max_matter_gap_ppt":1000,"false_extinction":true,"research_gate_pass":false} +{"kind":"timing","tier":"aggregate","supply":300,"width":25,"sample":1,"elapsed_ns":55100,"peak_rows":4,"peak_payload_bytes":160,"peak_capacity_bytes":160,"living":0,"energy":0,"matter_per_channel":0} +{"kind":"timing","tier":"individual","supply":300,"width":25,"sample":1,"elapsed_ns":27255700,"peak_rows":2625,"peak_payload_bytes":84000,"peak_capacity_bytes":122528,"living":75,"energy":6275,"matter_per_channel":52115} +{"kind":"timing","tier":"individual","supply":300,"width":25,"sample":2,"elapsed_ns":26745600,"peak_rows":2625,"peak_payload_bytes":84000,"peak_capacity_bytes":122528,"living":75,"energy":6275,"matter_per_channel":52115} +{"kind":"timing","tier":"aggregate","supply":300,"width":25,"sample":2,"elapsed_ns":54500,"peak_rows":4,"peak_payload_bytes":160,"peak_capacity_bytes":160,"living":0,"energy":0,"matter_per_channel":0} +{"kind":"timing","tier":"aggregate","supply":300,"width":25,"sample":3,"elapsed_ns":53400,"peak_rows":4,"peak_payload_bytes":160,"peak_capacity_bytes":160,"living":0,"energy":0,"matter_per_channel":0} +{"kind":"timing","tier":"individual","supply":300,"width":25,"sample":3,"elapsed_ns":27301400,"peak_rows":2625,"peak_payload_bytes":84000,"peak_capacity_bytes":122528,"living":75,"energy":6275,"matter_per_channel":52115} +{"kind":"sensitivity","supply":360,"width":25,"individual_living":130,"aggregate_living":130,"population_gap_ppt":0,"energy_gap_ppt":151,"matter_gap_ppt":657,"max_population_gap_ppt":435,"max_energy_gap_ppt":528,"max_matter_gap_ppt":915,"false_extinction":false,"research_gate_pass":false} +{"kind":"timing","tier":"aggregate","supply":360,"width":25,"sample":1,"elapsed_ns":176900,"peak_rows":4,"peak_payload_bytes":160,"peak_capacity_bytes":160,"living":130,"energy":8300,"matter_per_channel":143460} +{"kind":"timing","tier":"individual","supply":360,"width":25,"sample":1,"elapsed_ns":31631700,"peak_rows":3156,"peak_payload_bytes":100992,"peak_capacity_bytes":122528,"living":130,"energy":7040,"matter_per_channel":49113} +{"kind":"timing","tier":"individual","supply":360,"width":25,"sample":2,"elapsed_ns":32229400,"peak_rows":3156,"peak_payload_bytes":100992,"peak_capacity_bytes":122528,"living":130,"energy":7040,"matter_per_channel":49113} +{"kind":"timing","tier":"aggregate","supply":360,"width":25,"sample":2,"elapsed_ns":178000,"peak_rows":4,"peak_payload_bytes":160,"peak_capacity_bytes":160,"living":130,"energy":8300,"matter_per_channel":143460} +{"kind":"timing","tier":"aggregate","supply":360,"width":25,"sample":3,"elapsed_ns":177200,"peak_rows":4,"peak_payload_bytes":160,"peak_capacity_bytes":160,"living":130,"energy":8300,"matter_per_channel":143460} +{"kind":"timing","tier":"individual","supply":360,"width":25,"sample":3,"elapsed_ns":31715900,"peak_rows":3156,"peak_payload_bytes":100992,"peak_capacity_bytes":122528,"living":130,"energy":7040,"matter_per_channel":49113} +{"kind":"policy","width":25,"passes_all_scenarios":false} +{"kind":"sensitivity","supply":240,"width":50,"individual_living":108,"aggregate_living":0,"population_gap_ppt":1000,"energy_gap_ppt":1000,"matter_gap_ppt":1000,"max_population_gap_ppt":1000,"max_energy_gap_ppt":1000,"max_matter_gap_ppt":1000,"false_extinction":true,"research_gate_pass":false} +{"kind":"timing","tier":"aggregate","supply":240,"width":50,"sample":1,"elapsed_ns":42700,"peak_rows":2,"peak_payload_bytes":80,"peak_capacity_bytes":80,"living":0,"energy":0,"matter_per_channel":0} +{"kind":"timing","tier":"individual","supply":240,"width":50,"sample":1,"elapsed_ns":26048600,"peak_rows":2471,"peak_payload_bytes":79072,"peak_capacity_bytes":81696,"living":108,"energy":6875,"matter_per_channel":87828} +{"kind":"timing","tier":"individual","supply":240,"width":50,"sample":2,"elapsed_ns":25978000,"peak_rows":2471,"peak_payload_bytes":79072,"peak_capacity_bytes":81696,"living":108,"energy":6875,"matter_per_channel":87828} +{"kind":"timing","tier":"aggregate","supply":240,"width":50,"sample":2,"elapsed_ns":42500,"peak_rows":2,"peak_payload_bytes":80,"peak_capacity_bytes":80,"living":0,"energy":0,"matter_per_channel":0} +{"kind":"timing","tier":"aggregate","supply":240,"width":50,"sample":3,"elapsed_ns":42500,"peak_rows":2,"peak_payload_bytes":80,"peak_capacity_bytes":80,"living":0,"energy":0,"matter_per_channel":0} +{"kind":"timing","tier":"individual","supply":240,"width":50,"sample":3,"elapsed_ns":24559800,"peak_rows":2471,"peak_payload_bytes":79072,"peak_capacity_bytes":81696,"living":108,"energy":6875,"matter_per_channel":87828} +{"kind":"sensitivity","supply":300,"width":50,"individual_living":75,"aggregate_living":120,"population_gap_ppt":375,"energy_gap_ppt":43,"matter_gap_ppt":35,"max_population_gap_ppt":868,"max_energy_gap_ppt":977,"max_matter_gap_ppt":943,"false_extinction":false,"research_gate_pass":false} +{"kind":"timing","tier":"aggregate","supply":300,"width":50,"sample":1,"elapsed_ns":134500,"peak_rows":3,"peak_payload_bytes":120,"peak_capacity_bytes":120,"living":120,"energy":6000,"matter_per_channel":54060} +{"kind":"timing","tier":"individual","supply":300,"width":50,"sample":1,"elapsed_ns":29189700,"peak_rows":2625,"peak_payload_bytes":84000,"peak_capacity_bytes":122528,"living":75,"energy":6275,"matter_per_channel":52115} +{"kind":"timing","tier":"individual","supply":300,"width":50,"sample":2,"elapsed_ns":27115300,"peak_rows":2625,"peak_payload_bytes":84000,"peak_capacity_bytes":122528,"living":75,"energy":6275,"matter_per_channel":52115} +{"kind":"timing","tier":"aggregate","supply":300,"width":50,"sample":2,"elapsed_ns":153300,"peak_rows":3,"peak_payload_bytes":120,"peak_capacity_bytes":120,"living":120,"energy":6000,"matter_per_channel":54060} +{"kind":"timing","tier":"aggregate","supply":300,"width":50,"sample":3,"elapsed_ns":130600,"peak_rows":3,"peak_payload_bytes":120,"peak_capacity_bytes":120,"living":120,"energy":6000,"matter_per_channel":54060} +{"kind":"timing","tier":"individual","supply":300,"width":50,"sample":3,"elapsed_ns":34029700,"peak_rows":2625,"peak_payload_bytes":84000,"peak_capacity_bytes":122528,"living":75,"energy":6275,"matter_per_channel":52115} +{"kind":"sensitivity","supply":360,"width":50,"individual_living":130,"aggregate_living":180,"population_gap_ppt":277,"energy_gap_ppt":217,"matter_gap_ppt":589,"max_population_gap_ppt":561,"max_energy_gap_ppt":772,"max_matter_gap_ppt":934,"false_extinction":false,"research_gate_pass":false} +{"kind":"timing","tier":"aggregate","supply":360,"width":50,"sample":1,"elapsed_ns":128500,"peak_rows":3,"peak_payload_bytes":120,"peak_capacity_bytes":120,"living":180,"energy":9000,"matter_per_channel":119700} +{"kind":"timing","tier":"individual","supply":360,"width":50,"sample":1,"elapsed_ns":32230800,"peak_rows":3156,"peak_payload_bytes":100992,"peak_capacity_bytes":122528,"living":130,"energy":7040,"matter_per_channel":49113} +{"kind":"timing","tier":"individual","supply":360,"width":50,"sample":2,"elapsed_ns":34034400,"peak_rows":3156,"peak_payload_bytes":100992,"peak_capacity_bytes":122528,"living":130,"energy":7040,"matter_per_channel":49113} +{"kind":"timing","tier":"aggregate","supply":360,"width":50,"sample":2,"elapsed_ns":129300,"peak_rows":3,"peak_payload_bytes":120,"peak_capacity_bytes":120,"living":180,"energy":9000,"matter_per_channel":119700} +{"kind":"timing","tier":"aggregate","supply":360,"width":50,"sample":3,"elapsed_ns":126900,"peak_rows":3,"peak_payload_bytes":120,"peak_capacity_bytes":120,"living":180,"energy":9000,"matter_per_channel":119700} +{"kind":"timing","tier":"individual","supply":360,"width":50,"sample":3,"elapsed_ns":33152200,"peak_rows":3156,"peak_payload_bytes":100992,"peak_capacity_bytes":122528,"living":130,"energy":7040,"matter_per_channel":49113} +{"kind":"policy","width":50,"passes_all_scenarios":false} +{"kind":"sensitivity","supply":240,"width":100,"individual_living":108,"aggregate_living":0,"population_gap_ppt":1000,"energy_gap_ppt":1000,"matter_gap_ppt":1000,"max_population_gap_ppt":1000,"max_energy_gap_ppt":1000,"max_matter_gap_ppt":1000,"false_extinction":true,"research_gate_pass":false} +{"kind":"timing","tier":"aggregate","supply":240,"width":100,"sample":1,"elapsed_ns":41900,"peak_rows":1,"peak_payload_bytes":40,"peak_capacity_bytes":40,"living":0,"energy":0,"matter_per_channel":0} +{"kind":"timing","tier":"individual","supply":240,"width":100,"sample":1,"elapsed_ns":25856900,"peak_rows":2471,"peak_payload_bytes":79072,"peak_capacity_bytes":81696,"living":108,"energy":6875,"matter_per_channel":87828} +{"kind":"timing","tier":"individual","supply":240,"width":100,"sample":2,"elapsed_ns":25132500,"peak_rows":2471,"peak_payload_bytes":79072,"peak_capacity_bytes":81696,"living":108,"energy":6875,"matter_per_channel":87828} +{"kind":"timing","tier":"aggregate","supply":240,"width":100,"sample":2,"elapsed_ns":40700,"peak_rows":1,"peak_payload_bytes":40,"peak_capacity_bytes":40,"living":0,"energy":0,"matter_per_channel":0} +{"kind":"timing","tier":"aggregate","supply":240,"width":100,"sample":3,"elapsed_ns":40300,"peak_rows":1,"peak_payload_bytes":40,"peak_capacity_bytes":40,"living":0,"energy":0,"matter_per_channel":0} +{"kind":"timing","tier":"individual","supply":240,"width":100,"sample":3,"elapsed_ns":25615500,"peak_rows":2471,"peak_payload_bytes":79072,"peak_capacity_bytes":81696,"living":108,"energy":6875,"matter_per_channel":87828} +{"kind":"sensitivity","supply":300,"width":100,"individual_living":75,"aggregate_living":0,"population_gap_ppt":1000,"energy_gap_ppt":1000,"matter_gap_ppt":1000,"max_population_gap_ppt":1000,"max_energy_gap_ppt":1000,"max_matter_gap_ppt":1000,"false_extinction":true,"research_gate_pass":false} +{"kind":"timing","tier":"aggregate","supply":300,"width":100,"sample":1,"elapsed_ns":44800,"peak_rows":2,"peak_payload_bytes":80,"peak_capacity_bytes":80,"living":0,"energy":0,"matter_per_channel":0} +{"kind":"timing","tier":"individual","supply":300,"width":100,"sample":1,"elapsed_ns":28119900,"peak_rows":2625,"peak_payload_bytes":84000,"peak_capacity_bytes":122528,"living":75,"energy":6275,"matter_per_channel":52115} +{"kind":"timing","tier":"individual","supply":300,"width":100,"sample":2,"elapsed_ns":27056100,"peak_rows":2625,"peak_payload_bytes":84000,"peak_capacity_bytes":122528,"living":75,"energy":6275,"matter_per_channel":52115} +{"kind":"timing","tier":"aggregate","supply":300,"width":100,"sample":2,"elapsed_ns":45400,"peak_rows":2,"peak_payload_bytes":80,"peak_capacity_bytes":80,"living":0,"energy":0,"matter_per_channel":0} +{"kind":"timing","tier":"aggregate","supply":300,"width":100,"sample":3,"elapsed_ns":44600,"peak_rows":2,"peak_payload_bytes":80,"peak_capacity_bytes":80,"living":0,"energy":0,"matter_per_channel":0} +{"kind":"timing","tier":"individual","supply":300,"width":100,"sample":3,"elapsed_ns":27179600,"peak_rows":2625,"peak_payload_bytes":84000,"peak_capacity_bytes":122528,"living":75,"energy":6275,"matter_per_channel":52115} +{"kind":"sensitivity","supply":360,"width":100,"individual_living":130,"aggregate_living":0,"population_gap_ppt":1000,"energy_gap_ppt":1000,"matter_gap_ppt":1000,"max_population_gap_ppt":1000,"max_energy_gap_ppt":1000,"max_matter_gap_ppt":1000,"false_extinction":true,"research_gate_pass":false} +{"kind":"timing","tier":"aggregate","supply":360,"width":100,"sample":1,"elapsed_ns":45800,"peak_rows":2,"peak_payload_bytes":80,"peak_capacity_bytes":80,"living":0,"energy":0,"matter_per_channel":0} +{"kind":"timing","tier":"individual","supply":360,"width":100,"sample":1,"elapsed_ns":32033200,"peak_rows":3156,"peak_payload_bytes":100992,"peak_capacity_bytes":122528,"living":130,"energy":7040,"matter_per_channel":49113} +{"kind":"timing","tier":"individual","supply":360,"width":100,"sample":2,"elapsed_ns":32456600,"peak_rows":3156,"peak_payload_bytes":100992,"peak_capacity_bytes":122528,"living":130,"energy":7040,"matter_per_channel":49113} +{"kind":"timing","tier":"aggregate","supply":360,"width":100,"sample":2,"elapsed_ns":45900,"peak_rows":2,"peak_payload_bytes":80,"peak_capacity_bytes":80,"living":0,"energy":0,"matter_per_channel":0} +{"kind":"timing","tier":"aggregate","supply":360,"width":100,"sample":3,"elapsed_ns":44700,"peak_rows":2,"peak_payload_bytes":80,"peak_capacity_bytes":80,"living":0,"energy":0,"matter_per_channel":0} +{"kind":"timing","tier":"individual","supply":360,"width":100,"sample":3,"elapsed_ns":32254100,"peak_rows":3156,"peak_payload_bytes":100992,"peak_capacity_bytes":122528,"living":130,"energy":7040,"matter_per_channel":49113} +{"kind":"policy","width":100,"passes_all_scenarios":false} +{"kind":"sensitivity","supply":240,"width":200,"individual_living":108,"aggregate_living":0,"population_gap_ppt":1000,"energy_gap_ppt":1000,"matter_gap_ppt":1000,"max_population_gap_ppt":1000,"max_energy_gap_ppt":1000,"max_matter_gap_ppt":1000,"false_extinction":true,"research_gate_pass":false} +{"kind":"timing","tier":"aggregate","supply":240,"width":200,"sample":1,"elapsed_ns":40300,"peak_rows":1,"peak_payload_bytes":40,"peak_capacity_bytes":40,"living":0,"energy":0,"matter_per_channel":0} +{"kind":"timing","tier":"individual","supply":240,"width":200,"sample":1,"elapsed_ns":25126700,"peak_rows":2471,"peak_payload_bytes":79072,"peak_capacity_bytes":81696,"living":108,"energy":6875,"matter_per_channel":87828} +{"kind":"timing","tier":"individual","supply":240,"width":200,"sample":2,"elapsed_ns":25820000,"peak_rows":2471,"peak_payload_bytes":79072,"peak_capacity_bytes":81696,"living":108,"energy":6875,"matter_per_channel":87828} +{"kind":"timing","tier":"aggregate","supply":240,"width":200,"sample":2,"elapsed_ns":41200,"peak_rows":1,"peak_payload_bytes":40,"peak_capacity_bytes":40,"living":0,"energy":0,"matter_per_channel":0} +{"kind":"timing","tier":"aggregate","supply":240,"width":200,"sample":3,"elapsed_ns":40800,"peak_rows":1,"peak_payload_bytes":40,"peak_capacity_bytes":40,"living":0,"energy":0,"matter_per_channel":0} +{"kind":"timing","tier":"individual","supply":240,"width":200,"sample":3,"elapsed_ns":24109700,"peak_rows":2471,"peak_payload_bytes":79072,"peak_capacity_bytes":81696,"living":108,"energy":6875,"matter_per_channel":87828} +{"kind":"sensitivity","supply":300,"width":200,"individual_living":75,"aggregate_living":0,"population_gap_ppt":1000,"energy_gap_ppt":1000,"matter_gap_ppt":1000,"max_population_gap_ppt":1000,"max_energy_gap_ppt":1000,"max_matter_gap_ppt":1000,"false_extinction":true,"research_gate_pass":false} +{"kind":"timing","tier":"aggregate","supply":300,"width":200,"sample":1,"elapsed_ns":45300,"peak_rows":2,"peak_payload_bytes":80,"peak_capacity_bytes":80,"living":0,"energy":0,"matter_per_channel":0} +{"kind":"timing","tier":"individual","supply":300,"width":200,"sample":1,"elapsed_ns":25969900,"peak_rows":2625,"peak_payload_bytes":84000,"peak_capacity_bytes":122528,"living":75,"energy":6275,"matter_per_channel":52115} +{"kind":"timing","tier":"individual","supply":300,"width":200,"sample":2,"elapsed_ns":30360100,"peak_rows":2625,"peak_payload_bytes":84000,"peak_capacity_bytes":122528,"living":75,"energy":6275,"matter_per_channel":52115} +{"kind":"timing","tier":"aggregate","supply":300,"width":200,"sample":2,"elapsed_ns":46900,"peak_rows":2,"peak_payload_bytes":80,"peak_capacity_bytes":80,"living":0,"energy":0,"matter_per_channel":0} +{"kind":"timing","tier":"aggregate","supply":300,"width":200,"sample":3,"elapsed_ns":45200,"peak_rows":2,"peak_payload_bytes":80,"peak_capacity_bytes":80,"living":0,"energy":0,"matter_per_channel":0} +{"kind":"timing","tier":"individual","supply":300,"width":200,"sample":3,"elapsed_ns":27263300,"peak_rows":2625,"peak_payload_bytes":84000,"peak_capacity_bytes":122528,"living":75,"energy":6275,"matter_per_channel":52115} +{"kind":"sensitivity","supply":360,"width":200,"individual_living":130,"aggregate_living":0,"population_gap_ppt":1000,"energy_gap_ppt":1000,"matter_gap_ppt":1000,"max_population_gap_ppt":1000,"max_energy_gap_ppt":1000,"max_matter_gap_ppt":1000,"false_extinction":true,"research_gate_pass":false} +{"kind":"timing","tier":"aggregate","supply":360,"width":200,"sample":1,"elapsed_ns":45600,"peak_rows":2,"peak_payload_bytes":80,"peak_capacity_bytes":80,"living":0,"energy":0,"matter_per_channel":0} +{"kind":"timing","tier":"individual","supply":360,"width":200,"sample":1,"elapsed_ns":32157600,"peak_rows":3156,"peak_payload_bytes":100992,"peak_capacity_bytes":122528,"living":130,"energy":7040,"matter_per_channel":49113} +{"kind":"timing","tier":"individual","supply":360,"width":200,"sample":2,"elapsed_ns":31966100,"peak_rows":3156,"peak_payload_bytes":100992,"peak_capacity_bytes":122528,"living":130,"energy":7040,"matter_per_channel":49113} +{"kind":"timing","tier":"aggregate","supply":360,"width":200,"sample":2,"elapsed_ns":46900,"peak_rows":2,"peak_payload_bytes":80,"peak_capacity_bytes":80,"living":0,"energy":0,"matter_per_channel":0} +{"kind":"timing","tier":"aggregate","supply":360,"width":200,"sample":3,"elapsed_ns":45900,"peak_rows":2,"peak_payload_bytes":80,"peak_capacity_bytes":80,"living":0,"energy":0,"matter_per_channel":0} +{"kind":"timing","tier":"individual","supply":360,"width":200,"sample":3,"elapsed_ns":31683700,"peak_rows":3156,"peak_payload_bytes":100992,"peak_capacity_bytes":122528,"living":130,"energy":7040,"matter_per_channel":49113} +{"kind":"policy","width":200,"passes_all_scenarios":false} diff --git a/tests/benchmarks/resolution_benchmark.cpp b/tests/benchmarks/resolution_benchmark.cpp new file mode 100644 index 0000000..b874dd2 --- /dev/null +++ b/tests/benchmarks/resolution_benchmark.cpp @@ -0,0 +1,160 @@ +#include "../fixtures/resolution_model.hpp" + +#include +#include +#include +#include +#include + +namespace { +using namespace evo::fixture; +constexpr std::uint64_t ticks = 10'000; + +struct Peaks { + std::size_t rows{}; + std::size_t payload{}; + std::size_t capacity{}; + template void observe(const std::vector& values) { + rows = std::max(rows, values.size()); + payload = std::max(payload, values.size() * sizeof(T)); + capacity = std::max(capacity, values.capacity() * sizeof(T)); + } +}; + +template void observe(Peaks& peak, const Run& run) { + if constexpr (requires { run.members; }) { + peak.observe(run.members); + } else { + peak.observe(run.classes); + } +} + +template +void timed(Run run, std::string_view kind, std::uint64_t width, unsigned sample) { + Peaks peak; + observe(peak, run); + const auto start = std::chrono::steady_clock::now(); + for (std::uint64_t tick = 0; tick < ticks; ++tick) { + run.step(); + observe(peak, run); + } + const auto ns = std::chrono::duration_cast( + std::chrono::steady_clock::now() - start) + .count(); + const auto result = run.census(); + // Sample zero warms up the same workload but is not reported. + if (sample == 0) { + return; + } + std::cout << "{\"kind\":\"timing\",\"tier\":\"" << kind + << "\",\"supply\":" << run.rule.environment_energy << ",\"width\":" << width + << ",\"sample\":" << sample << ",\"elapsed_ns\":" << ns + << ",\"peak_rows\":" << peak.rows << ",\"peak_payload_bytes\":" << peak.payload + << ",\"peak_capacity_bytes\":" << peak.capacity << ",\"living\":" << result.living + << ",\"energy\":" << result.energy << ",\"matter_per_channel\":" << result.matter + << "}\n"; +} + +bool sensitivity(Rule rule, std::uint64_t width) { + auto individual = founding_individuals(100, rule); + auto aggregate = founding_classes(individual, width); + const auto energy = individual.total_energy(); + const auto matter = individual.total_matter(); + std::uint64_t max_population_gap = 0; + std::uint64_t max_energy_gap = 0; + std::uint64_t max_matter_gap = 0; + bool false_extinction = false; + for (std::uint64_t tick = 0; tick < ticks; ++tick) { + individual.step(); + aggregate.step(); + insist(individual.total_energy() == energy && aggregate.total_energy() == energy, + "energy conservation failed"); + insist(individual.total_matter() == matter && aggregate.total_matter() == matter, + "matter conservation failed"); + insist(representable(aggregate), "unrepresentable aggregate"); + const auto a = individual.census(); + const auto b = aggregate.census(); + max_population_gap = std::max(max_population_gap, gap_per_thousand(a.living, b.living)); + max_energy_gap = std::max(max_energy_gap, gap_per_thousand(a.energy, b.energy)); + max_matter_gap = std::max(max_matter_gap, gap_per_thousand(a.matter, b.matter)); + false_extinction = false_extinction || (a.living > 0 && b.living == 0); + } + const auto a = individual.census(); + const auto b = aggregate.census(); + const auto population_gap = gap_per_thousand(a.living, b.living); + const auto energy_gap = gap_per_thousand(a.energy, b.energy); + const auto matter_gap = gap_per_thousand(a.matter, b.matter); + const bool passes = + !false_extinction && population_gap <= 100 && energy_gap <= 100 && matter_gap <= 100; + std::cout << "{\"kind\":\"sensitivity\",\"supply\":" << rule.environment_energy + << ",\"width\":" << width << ",\"individual_living\":" << a.living + << ",\"aggregate_living\":" << b.living + << ",\"population_gap_ppt\":" << population_gap + << ",\"energy_gap_ppt\":" << energy_gap << ",\"matter_gap_ppt\":" << matter_gap + << ",\"max_population_gap_ppt\":" << max_population_gap + << ",\"max_energy_gap_ppt\":" << max_energy_gap + << ",\"max_matter_gap_ppt\":" << max_matter_gap + << ",\"false_extinction\":" << (false_extinction ? "true" : "false") + << ",\"research_gate_pass\":" << (passes ? "true" : "false") << "}\n"; + return passes; +} +} // namespace + +int main(int argc, char** argv) try { + if (argc > 2 || (argc == 2 && std::string_view{argv[1]} != "--verify")) { + std::cerr << "usage: evo-resolution-benchmark [--verify]\n"; + return 2; + } + const bool verify = argc == 2; + std::cout + << "{\"kind\":\"metadata\",\"model\":\"resolution-fixture-2\",\"founders\":100," + "\"ticks\":10000,\"randomness\":\"none\",\"warmups\":1,\"samples\":3,\"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"; + for (const auto width : {10U, 25U, 50U, 100U, 200U}) { + bool all_pass = true; + for (const auto supply : {240U, 300U, 360U}) { + Rule rule; + rule.environment_energy = supply; + all_pass = sensitivity(rule, width) && all_pass; + if (!verify) { + for (unsigned sample = 0; sample <= 3; ++sample) { + auto individual = founding_individuals(100, rule); + auto aggregate = founding_classes(individual, width); + // Alternate order to reduce systematic first-run bias. + if (sample % 2 == 0) { + timed(individual, "individual", width, sample); + timed(aggregate, "aggregate", width, sample); + } else { + timed(aggregate, "aggregate", width, sample); + timed(individual, "individual", width, sample); + } + } + } + } + std::cout << "{\"kind\":\"policy\",\"width\":" << width + << ",\"passes_all_scenarios\":" << (all_pass ? "true" : "false") << "}\n"; + } + return 0; +} catch (const 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 benchmark failure\n", stderr); + return 1; +} diff --git a/tests/equivalence_tests.cpp b/tests/equivalence_tests.cpp index 4d2c5c0..66b47e0 100644 --- a/tests/equivalence_tests.cpp +++ b/tests/equivalence_tests.cpp @@ -1,5 +1,6 @@ #include "evo/ecology/materialize.hpp" #include "evo/ecology/population.hpp" +#include "fixtures/resolution_model.hpp" #include #include @@ -23,513 +24,8 @@ void check(bool condition, std::source_location location = std::source_location: } #define CHECK(...) check(static_cast(__VA_ARGS__)) -// --------------------------------------------------------------------------- -// The rule. Written once, applied at both resolutions: the individual run gives -// it one member at a time and the aggregate run gives it a class average and -// multiplies by the count. It is a fixture constant, not a simulation contract: -// ADR-0038 deliberately declines to choose an ecology, and this does not choose -// one for it. Energy is the only scarce axis so that divergence is attributable. -// --------------------------------------------------------------------------- -namespace rule { -constexpr std::uint64_t environment_energy = 300; // per tick, shared by the living -constexpr std::uint64_t matter_intake = 4; // per member per tick, abundant -constexpr std::uint64_t upkeep_energy = 2; -constexpr std::uint64_t upkeep_matter = 1; -constexpr std::uint64_t lifespan = 400; -constexpr std::uint64_t maturity = 50; -constexpr std::uint64_t threshold_energy = 100; -constexpr std::uint64_t investment_energy = 40; -constexpr std::uint64_t investment_matter = 40; -constexpr std::uint64_t capacity = 200; - -// A member that cannot pay its upkeep starves. At class resolution the average -// member is asked the same question, which is exactly where the resolution bites. -[[nodiscard]] constexpr bool starves(std::uint64_t energy) noexcept { - return energy < upkeep_energy; -} -[[nodiscard]] constexpr bool breeds(std::uint64_t age, std::uint64_t energy) noexcept { - return age >= maturity && energy >= threshold_energy; -} -[[nodiscard]] constexpr std::uint64_t per_capita(std::uint64_t living) noexcept { - return living == 0 ? 0 : environment_energy / living; -} -// Who breeds when there are fewer free slots than eligible parents. This has to be -// part of the rule rather than a consequence of how each run happens to iterate: -// allocating contested slots in different orders changes which parents reproduce -// independently of any averaging, and a divergence caused by that is not a -// divergence caused by resolution. Oldest first, which both resolutions can honour: -// classes partition ages, so the oldest class holds the oldest members. Ordering -// within a class is the one thing only the individual run can see, and that is a -// resolution effect rather than an implementation difference. -} // namespace rule - -// Matter behaves identically in all four channels here, so one number is tracked -// and mirrored into the four. Stated rather than left for a reader to infer. -Inventory spread(std::uint64_t energy, std::uint64_t matter) { - return {{energy}, - {Femtograms{matter}, Femtograms{matter}, Femtograms{matter}, Femtograms{matter}}}; -} - -struct Census { - std::uint64_t living{}; - std::uint64_t energy{}; - std::uint64_t matter{}; - std::uint64_t births{}; - std::uint64_t deaths{}; - auto operator<=>(const Census&) const = default; -}; - -// --------------------------------------------------------------------------- -// Individual resolution. Inventory is tracked as plain values and conservation is -// asserted arithmetically: this is the reference fixture, not a contract, and a -// reservoir per member would exceed the ledger's per-transaction leg bound. -// --------------------------------------------------------------------------- -struct Member { - std::uint64_t age{}; - std::uint64_t energy{}; - std::uint64_t matter{}; - bool alive{true}; -}; - -struct IndividualRun { - std::vector members; - std::uint64_t environment{}; - std::uint64_t detritus_energy{}; - std::uint64_t detritus_matter{}; - std::uint64_t matter_store{}; - std::uint64_t births{}; - std::uint64_t deaths{}; - std::uint64_t capacity{rule::capacity}; - - [[nodiscard]] Census census() const { - Census now; - for (const auto& member : members) { - if (!member.alive) { - continue; - } - ++now.living; - now.energy += member.energy; - now.matter += member.matter; - } - now.births = births; - now.deaths = deaths; - return now; - } - [[nodiscard]] std::uint64_t total_energy() const { - std::uint64_t total = environment + detritus_energy; - for (const auto& member : members) { - if (member.alive) { - total += member.energy; - } - } - return total; - } - [[nodiscard]] std::uint64_t total_matter() const { - std::uint64_t total = matter_store + detritus_matter; - for (const auto& member : members) { - if (member.alive) { - total += member.matter; - } - } - return total; - } - - void step() { - std::uint64_t living = 0; - for (const auto& member : members) { - living += member.alive ? 1 : 0; - } - const auto share = rule::per_capita(living); - // Starvation first, then upkeep, then intake, then ageing, then breeding. - // The aggregate run applies the same five stages in the same order. - for (auto& member : members) { - if (!member.alive) { - continue; - } - if (rule::starves(member.energy)) { - member.alive = false; - detritus_energy += member.energy; - detritus_matter += member.matter; - member.energy = 0; - member.matter = 0; - ++deaths; - } - } - for (auto& member : members) { - if (!member.alive) { - continue; - } - member.energy -= rule::upkeep_energy; - detritus_energy += rule::upkeep_energy; - const auto paid = std::min(member.matter, rule::upkeep_matter); - member.matter -= paid; - detritus_matter += paid; - } - for (auto& member : members) { - if (!member.alive) { - continue; - } - // Bounded by what is there, so the arithmetic is safe, but the fixture - // is sized so that it never binds: if it ever did, intake would depend - // on the order members are visited in, and a divergence caused by that - // would look like a divergence caused by resolution. Checked rather - // than assumed, on both sides. - check(environment >= share); - check(matter_store >= rule::matter_intake); - const auto taken = std::min(share, environment); - environment -= taken; - member.energy += taken; - const auto gathered = std::min(rule::matter_intake, matter_store); - matter_store -= gathered; - member.matter += gathered; - } - for (auto& member : members) { - if (!member.alive) { - continue; - } - ++member.age; - if (member.age >= rule::lifespan) { - member.alive = false; - detritus_energy += member.energy; - detritus_matter += member.matter; - member.energy = 0; - member.matter = 0; - ++deaths; - } - } - std::uint64_t population = 0; - for (const auto& member : members) { - population += member.alive ? 1 : 0; - } - // Oldest first, as the rule requires, with the index breaking ties so the - // order is total. Insertion order would have allocated contested slots to - // different parents than the aggregate run does, which would have shown up - // in the comparison as if resolution had caused it. - std::vector eligible; - for (std::size_t index = 0; index < members.size(); ++index) { - const auto& member = members[index]; - if (!member.alive || !rule::breeds(member.age, member.energy)) { - continue; - } - if (member.matter < rule::investment_matter) { - continue; - } - eligible.push_back(index); - } - std::ranges::sort(eligible, [&](std::size_t left, std::size_t right) { - if (members[left].age != members[right].age) { - return members[left].age > members[right].age; - } - return left < right; - }); - for (const auto index : eligible) { - if (population >= capacity) { - break; - } - auto& member = members[index]; - member.energy -= rule::investment_energy; - member.matter -= rule::investment_matter; - members.push_back({0, rule::investment_energy, rule::investment_matter, true}); - ++population; - ++births; - } - } -}; - -IndividualRun founding_individuals(std::uint64_t count) { - IndividualRun run; - // Deliberately spread: variation is what averaging destroys, so a fixture - // where everyone is identical would measure nothing. - for (std::uint64_t index = 0; index < count; ++index) { - run.members.push_back({index % 60, 20 + index, 50 + index, true}); - } - run.environment = 100'000'000; - run.matter_store = 100'000'000; - return run; -} - -// --------------------------------------------------------------------------- -// Aggregate resolution. Real cohort records, real ledger, real conversions. The -// class state is advanced by the same five stages applied to a class average. -// --------------------------------------------------------------------------- -struct AggregateClass { - std::uint64_t age_low{}; - std::uint64_t age_high{}; - std::uint64_t count{}; - std::uint64_t energy{}; // held by the class in total - std::uint64_t matter{}; -}; - -struct AggregateRun { - std::vector classes; - std::uint64_t width{}; - std::uint64_t environment{}; - std::uint64_t matter_store{}; - std::uint64_t detritus_energy{}; - std::uint64_t detritus_matter{}; - std::uint64_t births{}; - std::uint64_t deaths{}; - std::uint64_t capacity{rule::capacity}; - - [[nodiscard]] Census census() const { - Census now; - for (const auto& age : classes) { - now.living += age.count; - now.energy += age.energy; - now.matter += age.matter; - } - now.births = births; - now.deaths = deaths; - return now; - } - [[nodiscard]] std::uint64_t total_energy() const { - std::uint64_t total = environment + detritus_energy; - for (const auto& age : classes) { - total += age.energy; - } - return total; - } - [[nodiscard]] std::uint64_t total_matter() const { - std::uint64_t total = matter_store + detritus_matter; - for (const auto& age : classes) { - total += age.matter; - } - return total; - } - - void step() { - std::uint64_t living = 0; - for (const auto& age : classes) { - living += age.count; - } - const auto share = rule::per_capita(living); - // Starvation, judged on the class average. This is the resolution error: - // the average pays for everyone until it cannot, and then the class goes - // together, where individuals would have starved one at a time. - for (auto& age : classes) { - if (age.count == 0) { - continue; - } - if (rule::starves(age.energy / age.count)) { - detritus_energy += age.energy; - detritus_matter += age.matter; - deaths += age.count; - age.count = 0; - age.energy = 0; - age.matter = 0; - } - } - std::erase_if(classes, [](const AggregateClass& age) { return age.count == 0; }); - for (auto& age : classes) { - const auto owed = rule::upkeep_energy * age.count; - age.energy -= owed; - detritus_energy += owed; - const auto paid = std::min(age.matter, rule::upkeep_matter * age.count); - age.matter -= paid; - detritus_matter += paid; - } - for (auto& age : classes) { - const auto wanted = share * age.count; - // The same bound, and the same reason for checking it: a shared store - // that ran out would make intake depend on the order classes are - // visited in, which is not a resolution effect. - check(environment >= wanted); - check(matter_store >= rule::matter_intake * age.count); - const auto taken = std::min(wanted, environment); - environment -= taken; - age.energy += taken; - const auto gathered = std::min(rule::matter_intake * age.count, matter_store); - matter_store -= gathered; - age.matter += gathered; - } - // Ageing shifts the class bounds rather than moving members between - // classes, so no member ever has to be placed by an age the record cannot - // know. A class is a birth cohort and stays one. - for (auto& age : classes) { - ++age.age_low; - ++age.age_high; - } - for (auto& age : classes) { - if (age.age_low >= rule::lifespan) { - detritus_energy += age.energy; - detritus_matter += age.matter; - deaths += age.count; - age.count = 0; - age.energy = 0; - age.matter = 0; - } - } - std::erase_if(classes, [](const AggregateClass& age) { return age.count == 0; }); - std::uint64_t population = 0; - for (const auto& age : classes) { - population += age.count; - } - std::uint64_t newborns = 0; - std::uint64_t taken_energy = 0; - std::uint64_t taken_matter = 0; - // Oldest class first, which is the same allocation rule the individual run - // follows. The classes are held youngest-first, so this walks them in - // reverse rather than relying on the storage order. - for (auto& age : std::ranges::reverse_view(classes)) { - if (age.count == 0 || population + newborns >= capacity) { - continue; - } - const auto average_energy = age.energy / age.count; - const auto average_matter = age.matter / age.count; - if (!rule::breeds(age.age_low, average_energy)) { - continue; - } - if (average_matter < rule::investment_matter) { - continue; - } - auto breeding = age.count; - if (population + newborns + breeding > capacity) { - breeding = capacity - population - newborns; - } - const auto energy_moved = rule::investment_energy * breeding; - const auto matter_moved = rule::investment_matter * breeding; - if (energy_moved > age.energy || matter_moved > age.matter) { - continue; - } - age.energy -= energy_moved; - age.matter -= matter_moved; - taken_energy += energy_moved; - taken_matter += matter_moved; - newborns += breeding; - } - if (newborns > 0) { - // Ageing shifted every class up by one, so nothing now starts at zero. - // The youngest class is extended down to cover the newborns while it is - // still narrower than the declared width, and otherwise a new one - // opens. That keeps the classes contiguous, ascending and never wider - // than the width, which is what makes the state a representable - // cohort: bounds that merely shift would overlap the moment a class - // was opened part way through an interval. - const auto at = std::ranges::min_element( - classes, {}, [](const AggregateClass& age) { return age.age_low; }); - if (at != classes.end() && at->age_low == 1 && at->age_high - at->age_low < width) { - at->age_low = 0; - at->count += newborns; - at->energy += taken_energy; - at->matter += taken_matter; - } else { - classes.push_back({0, 1, newborns, taken_energy, taken_matter}); - } - births += newborns; - } else { - // A class that opened and then had no births still has to grow to the - // declared width rather than staying one tick wide for ever, or the - // structure would drift towards one class per tick. - const auto at = std::ranges::min_element( - classes, {}, [](const AggregateClass& age) { return age.age_low; }); - if (at != classes.end() && at->age_low == 1 && at->age_high - at->age_low < width) { - at->age_low = 0; - } - } - std::ranges::sort(classes, {}, [](const AggregateClass& age) { return age.age_low; }); - } -}; - -AggregateRun founding_classes(const IndividualRun& source, std::uint64_t width) { - AggregateRun run; - run.width = width; - run.environment = source.environment; - run.matter_store = source.matter_store; - for (const auto& member : source.members) { - if (!member.alive) { - continue; - } - const auto low = member.age / width * width; - const auto at = std::ranges::find_if( - run.classes, [&](const AggregateClass& age) { return age.age_low == low; }); - if (at == run.classes.end()) { - run.classes.push_back({low, low + width, 1, member.energy, member.matter}); - } else { - ++at->count; - at->energy += member.energy; - at->matter += member.matter; - } - } - std::ranges::sort(run.classes, {}, [](const AggregateClass& age) { return age.age_low; }); - check(run.classes.size() <= max_age_classes); - return run; -} - -// Whether the aggregate state is something a cohort record can actually hold. The -// experiment is worthless if it measures a model the library could not represent, -// so this is checked at every step rather than assumed. -[[nodiscard]] bool representable(const AggregateRun& run) { - if (run.classes.empty()) { - return true; // Vacancy is representable; ADR-0036 has a factory for it. - } - CohortDraft probe; - probe.representation_version = cohort_representation_version; - probe.habitat = {7}; - probe.genome = 3; - probe.owner = {11, 1}; - probe.last_tick = Tick{0}; - probe.revision = 1; - probe.conversion = 1; - probe.reservoir = {1}; - for (const auto& age : run.classes) { - probe.classes.push_back( - {age.age_low, age.age_high, age.count, spread(age.energy, age.matter), 0}); - } - return Cohort::create(std::move(probe)).has_value(); -} - -// Every living member as (age, energy, matter), sorted. Comparing censuses is not -// enough to establish that the two runs allocate contested breeding slots to the -// same parents: whoever breeds hands the same investment to a child either way, so -// the totals agree while the distribution does not, and the disagreement only -// reaches a census many ticks later. The control therefore compares distributions. -using Portrait = std::vector>; - -[[nodiscard]] Portrait portrait(const IndividualRun& run) { - Portrait rows; - for (const auto& member : run.members) { - if (member.alive) { - rows.push_back({member.age, member.energy, member.matter}); - } - } - std::ranges::sort(rows); - return rows; -} -// Only meaningful at width one, where a class holds members of a single age. A -// class may still hold several of them, because several can be born in one tick, so -// each is expanded into its equal share; the division has to be exact or the -// expansion would not be the distribution it claims to be, and that is checked -// rather than assumed. -[[nodiscard]] Portrait portrait(const AggregateRun& run) { - Portrait rows; - for (const auto& age : run.classes) { - check(age.count > 0); - check(age.energy % age.count == 0); - check(age.matter % age.count == 0); - for (std::uint64_t index = 0; index < age.count; ++index) { - rows.push_back({age.age_low, age.energy / age.count, age.matter / age.count}); - } - } - std::ranges::sort(rows); - return rows; -} - -// Ages ascend with the index, so the oldest member is the last inserted and the -// youngest class is the first stored. Oldest-first therefore disagrees with both of -// the orders this fixture could have drifted into: allocating in insertion order -// would pick the first member, and walking the classes as stored would pick the -// youngest class. Each of those, introduced alone, makes the two runs choose -// different parents, which is what the control has to be able to see. -IndividualRun founding_contested(std::uint64_t count, std::uint64_t capacity) { - IndividualRun run; - for (std::uint64_t index = 0; index < count; ++index) { - run.members.push_back({rule::maturity + index, 100 + index, 60 + index, true}); - } - run.environment = 100'000'000; - run.matter_store = 100'000'000; - run.capacity = capacity; - return run; -} +using namespace evo::fixture; +constexpr Rule rule{}; // --------------------------------------------------------------------------- // The instrument check. With one member per class a class average is that member, @@ -575,7 +71,7 @@ void contested_breeding_slots_go_to_the_same_parents() { constexpr std::uint64_t count = 12; auto individual = founding_contested(count, count + 1); auto aggregate = founding_classes(individual, 1); - aggregate.capacity = individual.capacity; + CHECK(aggregate.rule.capacity == individual.rule.capacity); CHECK(aggregate.classes.size() == count); CHECK(portrait(individual) == portrait(aggregate)); bool contested = false; @@ -589,12 +85,13 @@ void contested_breeding_slots_go_to_the_same_parents() { continue; } ++living; - if (rule::breeds(member.age + 1, member.energy) && - member.matter >= rule::investment_matter) { + if (rule.breeds(member.age + 1, member.energy) && + member.matter >= rule.investment_matter) { ++eligible; } } - const auto free_slots = individual.capacity > living ? individual.capacity - living : 0; + const auto free_slots = + individual.rule.capacity > living ? individual.rule.capacity - living : 0; if (eligible > free_slots && free_slots > 0) { contested = true; } @@ -610,7 +107,7 @@ void contested_breeding_slots_go_to_the_same_parents() { // allocation and is just the first control again with fewer members. CHECK(contested); CHECK(individual.census().births > 0); - CHECK(individual.census().living == individual.capacity); + CHECK(individual.census().living == individual.rule.capacity); } // --------------------------------------------------------------------------- @@ -626,15 +123,6 @@ struct Divergence { std::uint64_t energy_gap_per_thousand{}; }; -std::uint64_t gap_per_thousand(std::uint64_t left, std::uint64_t right) { - const auto high = std::max(left, right); - const auto low = std::min(left, right); - if (high == 0) { - return 0; - } - return (high - low) * 1000 / high; -} - Divergence measure(std::uint64_t width, std::uint64_t ticks) { auto individual = founding_individuals(100); auto aggregate = founding_classes(individual, width); @@ -754,7 +242,7 @@ void the_full_scenario_conserves_and_keeps_its_genome() { // Sized for the capacity rather than the founders: the population grows, and a // draw of more members than there were founders would otherwise run off the // end of the ledger and be reported as a stale plan. - std::vector opening(first_member_store + rule::capacity); + std::vector opening(first_member_store + rule.capacity); opening[escrow.value - 1] = spread(individual.environment, individual.matter_store); std::vector members; for (std::size_t index = 0; index < individual.members.size(); ++index) { @@ -776,7 +264,7 @@ void the_full_scenario_conserves_and_keeps_its_genome() { gather.tick = Tick{0}; gather.revision = 1; gather.destination = backing; - for (std::uint64_t low = 0; low < rule::lifespan; low += 50) { + for (std::uint64_t low = 0; low < rule.lifespan; low += 50) { gather.bounds.push_back({low, low + 50}); } const auto folded = plan_collapse(members, gather); @@ -848,7 +336,7 @@ void the_full_scenario_conserves_and_keeps_its_genome() { std::uint64_t drawn = 0; for (std::size_t index = 0; index < cohort.classes().size(); ++index) { const auto count = cohort.classes()[index].count; - if (count == 0 || drawn + count > rule::capacity) { + if (count == 0 || drawn + count > rule.capacity) { continue; } draws.push_back({index, count}); diff --git a/tests/fixtures/resolution_model.hpp b/tests/fixtures/resolution_model.hpp new file mode 100644 index 0000000..6ef2ead --- /dev/null +++ b/tests/fixtures/resolution_model.hpp @@ -0,0 +1,617 @@ +#pragma once + +// The model compared at two resolutions in M4.5 and measured in M4.6. It lives in +// the test fixtures rather than in the library because ADR-0038 declines to choose +// an ecology and nothing here chooses one for it: these are declared measurement +// inputs. The comparison and the benchmark share this one definition, so what is +// timed is the same model whose error was measured. + +#include "evo/ecology/cohort.hpp" + +#include +#include +#include +#include +#include + +namespace evo::fixture { +using evo::accounting::Femtograms; +using evo::accounting::Inventory; +using evo::ecology::AgeClass; +using evo::ecology::Cohort; +using evo::ecology::cohort_representation_version; +using evo::ecology::CohortDraft; +using evo::ecology::HabitatId; +using evo::ecology::max_age_classes; +using evo::ecology::Owner; + +// Every quantity the model depends on, in one place, so a sensitivity study can +// move one and hold the rest. These were constants in M4.5; the defaults are those +// constants exactly, so the published envelope is unchanged by the move. +struct Rule { + std::uint64_t environment_energy = 300; // per tick, shared by the living + std::uint64_t matter_intake = 4; // per member per tick, abundant + std::uint64_t upkeep_energy = 2; + std::uint64_t upkeep_matter = 1; + std::uint64_t lifespan = 400; + std::uint64_t maturity = 50; + std::uint64_t threshold_energy = 100; + std::uint64_t investment_energy = 40; + std::uint64_t investment_matter = 40; + std::uint64_t capacity = 200; + + // A member that cannot pay its upkeep starves. At class resolution the average + // member is asked the same question, which is exactly where resolution bites. + [[nodiscard]] constexpr bool starves(std::uint64_t energy) const noexcept { + return energy < upkeep_energy; + } + [[nodiscard]] constexpr bool breeds(std::uint64_t age, std::uint64_t energy) const noexcept { + return age >= maturity && energy >= threshold_energy; + } + [[nodiscard]] constexpr std::uint64_t per_capita(std::uint64_t living) const noexcept { + return living == 0 ? 0 : environment_energy / living; + } + // Who breeds when there are fewer free slots than eligible parents is part of + // the rule, not a consequence of how either run happens to iterate: allocating + // contested slots in different orders changes which parents reproduce + // independently of any averaging, and that divergence is not a resolution + // effect. Oldest first, which both resolutions can honour because classes + // partition ages. Ordering inside a class is the one thing only the individual + // run can see, and that one is a resolution effect. +}; + +// Thrown rather than asserted, because this header is shared by a test and a +// benchmark and neither owns the other's harness. It guards arithmetic that would +// otherwise become silently order-dependent. +struct ModelViolation { + const char* what; +}; +inline void insist(bool condition, const char* what) { + if (!condition) { + throw ModelViolation{what}; + } +} + +// Matter behaves identically in all four channels here, so one number is tracked +// and mirrored into the four. Stated rather than left for a reader to infer. +[[nodiscard]] inline Inventory spread(std::uint64_t energy, std::uint64_t matter) { + return {{energy}, + {Femtograms{matter}, Femtograms{matter}, Femtograms{matter}, Femtograms{matter}}}; +} + +struct Census { + std::uint64_t living{}; + std::uint64_t energy{}; + std::uint64_t matter{}; + std::uint64_t births{}; + std::uint64_t deaths{}; + auto operator<=>(const Census&) const = default; +}; + +// --------------------------------------------------------------------------- +// Individual resolution. Inventory is tracked as plain values and conservation is +// asserted arithmetically: this is the reference fixture, not a contract, and a +// ledger reservoir per member would exceed the per-transaction leg bound. +// --------------------------------------------------------------------------- +struct Member { + std::uint64_t age{}; + std::uint64_t energy{}; + std::uint64_t matter{}; + bool alive{true}; +}; + +struct IndividualRun { + Rule rule{}; + std::vector members; + std::uint64_t environment{}; + std::uint64_t matter_store{}; + std::uint64_t detritus_energy{}; + std::uint64_t detritus_matter{}; + std::uint64_t births{}; + std::uint64_t deaths{}; + + [[nodiscard]] Census census() const { + Census now; + for (const auto& member : members) { + if (!member.alive) { + continue; + } + ++now.living; + now.energy += member.energy; + now.matter += member.matter; + } + now.births = births; + now.deaths = deaths; + return now; + } + [[nodiscard]] std::uint64_t total_energy() const { + std::uint64_t total = environment + detritus_energy; + for (const auto& member : members) { + if (member.alive) { + total += member.energy; + } + } + return total; + } + [[nodiscard]] std::uint64_t total_matter() const { + std::uint64_t total = matter_store + detritus_matter; + for (const auto& member : members) { + if (member.alive) { + total += member.matter; + } + } + return total; + } + // What the representation costs to hold. Reported as the state footprint rather + // than as process memory, because that is deterministic and comparable across + // platforms; it is not a claim about an allocator or about resident size. + [[nodiscard]] std::uint64_t footprint_bytes() const { + return static_cast(members.size() * sizeof(Member)); + } + [[nodiscard]] std::uint64_t rows() const { + return static_cast(members.size()); + } + + void step() { + std::uint64_t living = 0; + for (const auto& member : members) { + living += member.alive ? 1 : 0; + } + const auto share = rule.per_capita(living); + // Starvation, then upkeep, then intake, then ageing, then breeding. The + // aggregate run applies the same five stages in the same order. + for (auto& member : members) { + if (!member.alive) { + continue; + } + if (rule.starves(member.energy)) { + member.alive = false; + detritus_energy += member.energy; + detritus_matter += member.matter; + member.energy = 0; + member.matter = 0; + ++deaths; + } + } + for (auto& member : members) { + if (!member.alive) { + continue; + } + member.energy -= rule.upkeep_energy; + detritus_energy += rule.upkeep_energy; + const auto paid = std::min(member.matter, rule.upkeep_matter); + member.matter -= paid; + detritus_matter += paid; + } + for (auto& member : members) { + if (!member.alive) { + continue; + } + // Bounded by what is there so the arithmetic is safe, but the fixture is + // sized so it never binds: a shared store that ran out would make intake + // depend on the order members are visited in, and a divergence caused by + // that would look like one caused by resolution. Checked, not assumed. + insist(environment >= share, "environment depleted"); + insist(matter_store >= rule.matter_intake, "matter store depleted"); + environment -= share; + member.energy += share; + matter_store -= rule.matter_intake; + member.matter += rule.matter_intake; + } + for (auto& member : members) { + if (!member.alive) { + continue; + } + ++member.age; + if (member.age >= rule.lifespan) { + member.alive = false; + detritus_energy += member.energy; + detritus_matter += member.matter; + member.energy = 0; + member.matter = 0; + ++deaths; + } + } + std::uint64_t population = 0; + for (const auto& member : members) { + population += member.alive ? 1 : 0; + } + // Oldest first, as the rule requires, with the index breaking ties so the + // order is total. Insertion order would have allocated contested slots to + // different parents than the aggregate run does, which would have shown up + // in the comparison as if resolution had caused it. + std::vector eligible; + for (std::size_t index = 0; index < members.size(); ++index) { + const auto& member = members[index]; + if (!member.alive || !rule.breeds(member.age, member.energy)) { + continue; + } + if (member.matter < rule.investment_matter) { + continue; + } + eligible.push_back(index); + } + std::ranges::sort(eligible, [&](std::size_t left, std::size_t right) { + if (members[left].age != members[right].age) { + return members[left].age > members[right].age; + } + return left < right; + }); + for (const auto index : eligible) { + if (population >= rule.capacity) { + break; + } + auto& member = members[index]; + member.energy -= rule.investment_energy; + member.matter -= rule.investment_matter; + members.push_back({0, rule.investment_energy, rule.investment_matter, true}); + ++population; + ++births; + } + } +}; + +[[nodiscard]] inline IndividualRun founding_individuals(std::uint64_t count, Rule rule = {}) { + IndividualRun run; + run.rule = rule; + // Deliberately spread: variation is what averaging destroys, so a fixture where + // everyone is identical would measure nothing. + for (std::uint64_t index = 0; index < count; ++index) { + run.members.push_back({index % 60, 20 + index, 50 + index, true}); + } + run.environment = 100'000'000; + run.matter_store = 100'000'000; + return run; +} + +// Ages ascend with the index, so the oldest member is the last inserted and the +// youngest class the first stored. Oldest-first therefore disagrees with both orders +// this fixture could have drifted into, which is what lets a control see either. +[[nodiscard]] inline IndividualRun founding_contested(std::uint64_t count, std::uint64_t capacity) { + IndividualRun run; + run.rule.capacity = capacity; + for (std::uint64_t index = 0; index < count; ++index) { + run.members.push_back({run.rule.maturity + index, 100 + index, 60 + index, true}); + } + run.environment = 100'000'000; + run.matter_store = 100'000'000; + return run; +} + +// --------------------------------------------------------------------------- +// Aggregate resolution. The class state is advanced by the same five stages applied +// to a class average. +// --------------------------------------------------------------------------- +struct AggregateClass { + std::uint64_t age_low{}; + std::uint64_t age_high{}; + std::uint64_t count{}; + std::uint64_t energy{}; // held by the class in total + std::uint64_t matter{}; +}; + +struct AggregateRun { + Rule rule{}; + std::vector classes; + std::uint64_t width{}; + std::uint64_t environment{}; + std::uint64_t matter_store{}; + std::uint64_t detritus_energy{}; + std::uint64_t detritus_matter{}; + std::uint64_t births{}; + std::uint64_t deaths{}; + + [[nodiscard]] Census census() const { + Census now; + for (const auto& age : classes) { + now.living += age.count; + now.energy += age.energy; + now.matter += age.matter; + } + now.births = births; + now.deaths = deaths; + return now; + } + [[nodiscard]] std::uint64_t total_energy() const { + std::uint64_t total = environment + detritus_energy; + for (const auto& age : classes) { + total += age.energy; + } + return total; + } + [[nodiscard]] std::uint64_t total_matter() const { + std::uint64_t total = matter_store + detritus_matter; + for (const auto& age : classes) { + total += age.matter; + } + return total; + } + [[nodiscard]] std::uint64_t footprint_bytes() const { + return static_cast(classes.size() * sizeof(AggregateClass)); + } + [[nodiscard]] std::uint64_t rows() const { + return static_cast(classes.size()); + } + + void step() { + std::uint64_t living = 0; + for (const auto& age : classes) { + living += age.count; + } + const auto share = rule.per_capita(living); + // Starvation, judged on the class average. This is the resolution error: the + // average pays for everyone until it cannot, and then the class goes + // together, where individuals would have starved one at a time. + for (auto& age : classes) { + if (age.count == 0) { + continue; + } + if (rule.starves(age.energy / age.count)) { + detritus_energy += age.energy; + detritus_matter += age.matter; + deaths += age.count; + age.count = 0; + age.energy = 0; + age.matter = 0; + } + } + std::erase_if(classes, [](const AggregateClass& age) { return age.count == 0; }); + for (auto& age : classes) { + const auto owed = rule.upkeep_energy * age.count; + age.energy -= owed; + detritus_energy += owed; + const auto paid = std::min(age.matter, rule.upkeep_matter * age.count); + age.matter -= paid; + detritus_matter += paid; + } + for (auto& age : classes) { + const auto wanted = share * age.count; + // The same bound, and the same reason for checking it: a shared store + // that ran out would make intake depend on the order classes are visited + // in, which is not a resolution effect. + insist(environment >= wanted, "environment depleted"); + insist(matter_store >= rule.matter_intake * age.count, "matter store depleted"); + environment -= wanted; + age.energy += wanted; + matter_store -= rule.matter_intake * age.count; + age.matter += rule.matter_intake * age.count; + } + // Ageing shifts the class bounds rather than moving members between classes, + // so no member has to be placed by an age the record cannot know. A class is + // a birth cohort and stays one. + for (auto& age : classes) { + ++age.age_low; + ++age.age_high; + } + for (auto& age : classes) { + if (age.age_low >= rule.lifespan) { + detritus_energy += age.energy; + detritus_matter += age.matter; + deaths += age.count; + age.count = 0; + age.energy = 0; + age.matter = 0; + } + } + std::erase_if(classes, [](const AggregateClass& age) { return age.count == 0; }); + std::uint64_t population = 0; + for (const auto& age : classes) { + population += age.count; + } + std::uint64_t newborns = 0; + std::uint64_t taken_energy = 0; + std::uint64_t taken_matter = 0; + // Oldest class first, which is the same allocation rule the individual run + // follows. The classes are held youngest-first, so this walks them in reverse + // rather than relying on the storage order. + for (auto& age : std::ranges::reverse_view(classes)) { + if (age.count == 0 || population + newborns >= rule.capacity) { + continue; + } + const auto average_energy = age.energy / age.count; + const auto average_matter = age.matter / age.count; + if (!rule.breeds(age.age_low, average_energy)) { + continue; + } + if (average_matter < rule.investment_matter) { + continue; + } + auto breeding = age.count; + if (population + newborns + breeding > rule.capacity) { + breeding = rule.capacity - population - newborns; + } + const auto energy_moved = rule.investment_energy * breeding; + const auto matter_moved = rule.investment_matter * breeding; + if (energy_moved > age.energy || matter_moved > age.matter) { + continue; + } + age.energy -= energy_moved; + age.matter -= matter_moved; + taken_energy += energy_moved; + taken_matter += matter_moved; + newborns += breeding; + } + if (newborns > 0) { + // Ageing shifted every class up by one, so nothing now starts at zero. + // The youngest class is extended down to cover the newborns while it is + // still narrower than the declared width, and otherwise a new one opens. + // That keeps the classes contiguous, ascending and never wider than the + // width, which is what makes the state a representable cohort: bounds + // that merely shifted would overlap the moment a class was opened part + // way through an interval. + const auto at = std::ranges::min_element( + classes, {}, [](const AggregateClass& age) { return age.age_low; }); + if (at != classes.end() && at->age_low == 1 && at->age_high - at->age_low < width) { + at->age_low = 0; + at->count += newborns; + at->energy += taken_energy; + at->matter += taken_matter; + } else { + classes.push_back({0, 1, newborns, taken_energy, taken_matter}); + } + births += newborns; + } else { + // A class that opened and then had no births still has to grow to the + // declared width rather than staying one tick wide for ever, or the + // structure would drift towards one class per tick. + const auto at = std::ranges::min_element( + classes, {}, [](const AggregateClass& age) { return age.age_low; }); + if (at != classes.end() && at->age_low == 1 && at->age_high - at->age_low < width) { + at->age_low = 0; + } + } + std::ranges::sort(classes, {}, [](const AggregateClass& age) { return age.age_low; }); + } +}; + +[[nodiscard]] inline AggregateRun founding_classes(const IndividualRun& source, + std::uint64_t width) { + AggregateRun run; + run.rule = source.rule; + run.width = width; + run.environment = source.environment; + run.matter_store = source.matter_store; + for (const auto& member : source.members) { + if (!member.alive) { + continue; + } + const auto low = member.age / width * width; + const auto at = std::ranges::find_if( + run.classes, [&](const AggregateClass& age) { return age.age_low == low; }); + if (at == run.classes.end()) { + run.classes.push_back({low, low + width, 1, member.energy, member.matter}); + } else { + ++at->count; + at->energy += member.energy; + at->matter += member.matter; + } + } + std::ranges::sort(run.classes, {}, [](const AggregateClass& age) { return age.age_low; }); + insist(run.classes.size() <= max_age_classes, "too many founding classes"); + return run; +} + +// Whether the aggregate state is something a cohort record can actually hold. The +// experiment is worthless if it measures a model the library could not represent, +// so this is checked at every step rather than assumed. +[[nodiscard]] inline bool representable(const AggregateRun& run) { + if (run.classes.empty()) { + return true; // Vacancy is representable; ADR-0036 has a factory for it. + } + CohortDraft probe; + probe.representation_version = cohort_representation_version; + probe.habitat = {7}; + probe.genome = 3; + probe.owner = {11, 1}; + probe.last_tick = Tick{0}; + probe.revision = 1; + probe.conversion = 1; + probe.reservoir = {1}; + for (const auto& age : run.classes) { + probe.classes.push_back( + {age.age_low, age.age_high, age.count, spread(age.energy, age.matter), 0}); + } + return Cohort::create(std::move(probe)).has_value(); +} + +// Every living member as (age, energy, matter), sorted. Comparing censuses is not +// enough to establish that the two runs allocate contested breeding slots to the +// same parents: whoever breeds hands the same investment to a child either way, so +// the totals agree while the distribution does not, and the disagreement only +// reaches a census many ticks later. +using Portrait = std::vector>; + +[[nodiscard]] inline Portrait portrait(const IndividualRun& run) { + Portrait rows; + for (const auto& member : run.members) { + if (member.alive) { + rows.push_back({member.age, member.energy, member.matter}); + } + } + std::ranges::sort(rows); + return rows; +} +// Only meaningful at width one, where a class holds members of a single age. A class +// may still hold several of them, because several can be born in one tick, so each +// is expanded into its equal share; the division has to be exact or the expansion +// would not be the distribution it claims to be, and that is checked. +[[nodiscard]] inline Portrait portrait(const AggregateRun& run) { + Portrait rows; + for (const auto& age : run.classes) { + insist(age.count > 0, "empty class in a portrait"); + insist(age.energy % age.count == 0, "class energy does not divide"); + insist(age.matter % age.count == 0, "class matter does not divide"); + for (std::uint64_t index = 0; index < age.count; ++index) { + rows.push_back({age.age_low, age.energy / age.count, age.matter / age.count}); + } + } + std::ranges::sort(rows); + return rows; +} + +// One run of the comparison at a declared class width: what each resolution ended +// with, and what the aggregate one cost to hold. +struct Comparison { + std::uint64_t width{}; + std::uint64_t ticks{}; + std::uint64_t individual_living{}; + std::uint64_t aggregate_living{}; + std::uint64_t individual_energy{}; + std::uint64_t aggregate_energy{}; + std::uint64_t individual_rows{}; + std::uint64_t aggregate_rows{}; + std::uint64_t individual_bytes{}; + std::uint64_t aggregate_bytes{}; + std::uint64_t living_gap_per_thousand{}; + std::uint64_t energy_gap_per_thousand{}; + bool aggregate_survived{}; +}; + +[[nodiscard]] inline std::uint64_t gap_per_thousand(std::uint64_t left, std::uint64_t right) { + const auto high = std::max(left, right); + const auto low = std::min(left, right); + if (high == 0) { + return 0; + } + return (high - low) * 1000 / high; +} + +// Runs both resolutions over the same horizon and reports what each ended with. +// Conservation is checked on both sides at every tick, and the aggregate state is +// checked to be a representable cohort at every tick, so the result describes this +// library rather than a model it could not hold. +[[nodiscard]] inline Comparison compare(std::uint64_t founders, std::uint64_t width, + std::uint64_t ticks, Rule rule = {}) { + auto individual = founding_individuals(founders, rule); + auto aggregate = founding_classes(individual, width); + const auto opening_energy = individual.total_energy(); + const auto opening_matter = individual.total_matter(); + insist(aggregate.total_energy() == opening_energy, "opening energy differs"); + insist(aggregate.total_matter() == opening_matter, "opening matter differs"); + for (std::uint64_t tick = 0; tick < ticks; ++tick) { + individual.step(); + aggregate.step(); + insist(individual.total_energy() == opening_energy, "individual energy not conserved"); + insist(individual.total_matter() == opening_matter, "individual matter not conserved"); + insist(aggregate.total_energy() == opening_energy, "aggregate energy not conserved"); + insist(aggregate.total_matter() == opening_matter, "aggregate matter not conserved"); + insist(aggregate.classes.size() <= max_age_classes, "too many classes"); + insist(representable(aggregate), "aggregate state is not a representable cohort"); + } + const auto left = individual.census(); + const auto right = aggregate.census(); + return {width, + ticks, + left.living, + right.living, + left.energy, + right.energy, + individual.rows(), + aggregate.rows(), + individual.footprint_bytes(), + aggregate.footprint_bytes(), + gap_per_thousand(left.living, right.living), + gap_per_thousand(left.energy, right.energy), + right.living > 0}; +} +} // namespace evo::fixture diff --git a/tools/check-format.cmake b/tools/check-format.cmake index 276bf58..b232004 100644 --- a/tools/check-format.cmake +++ b/tools/check-format.cmake @@ -7,7 +7,7 @@ if(NOT result EQUAL 0 OR NOT version MATCHES "version 19\\.") endif() file(GLOB_RECURSE sources "${SOURCE_ROOT}/include/*.hpp" "${SOURCE_ROOT}/src/*.cpp" - "${SOURCE_ROOT}/apps/*.cpp" "${SOURCE_ROOT}/tests/*.cpp" + "${SOURCE_ROOT}/tests/*.hpp" "${SOURCE_ROOT}/apps/*.cpp" "${SOURCE_ROOT}/tests/*.cpp" "${SOURCE_ROOT}/experiments/*.cpp") execute_process(COMMAND "${FORMATTER}" --dry-run --Werror ${sources} RESULT_VARIABLE result) if(NOT result EQUAL 0)