Files
aevolution/tests/ecosystem_tests.cpp
T
dingoandClaude Opus 5 eaf58f2b85 Drive the energy cycle with measured ecosystem episodes
Close M2 with a deterministic driver that owns one community and one
seeded stream, holds no inventory and emits only the terminal, feeding
and reproduction intents the earlier increments already accept. No
simulation contract changed to make the cycle run unattended.

Two limits shaped the design. Identities are never reused and slots are
capped, so one community supports a bounded number of consumer lifetimes
and is not a population. No maintenance cost exists, so a consumer that
is never eaten never dies, and inventing a starvation rule here would
settle a question ENERGY_MODEL leaves open inside a measurement harness.
The harness therefore measures many bounded episodes, taking mortality
from predation and from a configured lifespan delivered as an ADR-0027
host terminal fact. Lifespan bounds an episode; it is not senescence.

The driver must never submit a batch the community rejects, so it probes
a discarded copy each tick to size bites against the state feeding will
actually see, claims each target for one feeder, and excludes a bitten
consumer from acting. A rejected tick is reported as a driver defect
rather than absorbed as ecology.

Across 64 seeds the suite steps tick by tick, requiring exact conserved
totals at every tick and an identical report on replay, and covers both
mortality paths, slot exhaustion, tick-limit termination, lineage
attribution, episode independence and configuration rejection.
evo-sim --ecosystem reports seeded ensembles: 3,000 episodes advanced
1,093,007 ticks, all reaching extinction, with deaths reconciling exactly
against founders plus births. That demonstrates arithmetic and turnover,
not ecological plausibility; strategy constants are chosen inputs.

Native Release, clang-tidy analysis, ASan and format checks pass 11/11;
golden d61ad5bf0c928c76 is unchanged. Linux CI has not run yet.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-22 17:38:34 -04:00

244 lines
10 KiB
C++

#include "evo/processes/ecosystem.hpp"
#include <algorithm>
#include <iostream>
#include <limits>
#include <source_location>
#include <stdexcept>
#include <string>
#include <vector>
namespace {
using namespace evo::processes;
using namespace evo::accounting;
void check(bool condition, std::source_location location = std::source_location::current()) {
if (!condition) {
throw std::runtime_error("ecosystem line " + std::to_string(location.line()));
}
}
#define CHECK(expression) check(static_cast<bool>(expression))
Inventory bundle(std::uint64_t value) {
return {{value}, {Femtograms{value}, Femtograms{value}, Femtograms{value}, Femtograms{value}}};
}
// Captures 10 energy and grows two whole recipe units each tick while funded.
ProducerConfig producer() {
return {{{10}, 1}, {2, 1}, 1000, {1}, bundle(1).matter};
}
Inventory sum(const FeedingCommunity& community) {
Inventory total{};
for (const auto& inventory : community.balances()) {
CHECK(inventory.energy.value <=
std::numeric_limits<std::uint64_t>::max() - total.energy.value);
total.energy.value += inventory.energy.value;
for (std::size_t index = 0; index < 4; ++index) {
CHECK(inventory.matter[index].value <=
std::numeric_limits<std::uint64_t>::max() - total.matter[index].value);
total.matter[index].value += inventory.matter[index].value;
}
}
return total;
}
EcosystemConfig grazing_world() {
return {producer(), {100'000}, bundle(200).matter, {bundle(4)}, 800, 14, 2000};
}
StrategyConfig grazer() {
return {bundle(20), bundle(3), 900, bundle(30), bundle(20), 50};
}
StrategyConfig forager() {
return {bundle(20), bundle(2), 1000, bundle(24), bundle(16), 70};
}
void solitary_founder_retires() {
// No source, no nutrients and no intake: nothing happens but ageing.
const EcosystemConfig config{producer(), {}, {}, {}, 1000, 0, 100};
const std::array founders{StrategyConfig{bundle(7), {}, 1000, bundle(1), bundle(1), 5}};
auto episode = EcosystemEpisode::create(config, founders, 1);
CHECK(episode);
const auto opening = sum(episode->community());
const auto summary = episode->run();
CHECK(summary);
CHECK(summary->ticks == 5 && summary->extinction_tick == 5);
CHECK(summary->lifespan_deaths == 1 && summary->predation_deaths == 0);
CHECK(summary->births == 0 && summary->surviving_lineages == 0);
CHECK(summary->founders == 1 && summary->conserved == opening);
// A retired founder keeps its identity and its founder ancestry.
CHECK(!*episode->community().active(2) && episode->lineage(2) == 2);
CHECK(episode->community().parent(2) && *episode->community().parent(2) == 0);
CHECK(episode->lineage(1) == 0); // The producer is not a consumer lineage.
// The episode is over; further steps do nothing.
const auto exhausted = episode->step();
CHECK(exhausted && !*exhausted && episode->report() == *summary);
}
void predation_retires_a_target() {
// The producer holds nothing, so the only target with inventory is the prey.
const EcosystemConfig config{producer(), {}, {}, {bundle(1)}, 1000, 0, 40};
const std::array founders{StrategyConfig{{}, bundle(100), 1000, bundle(10'000), bundle(1), 9},
StrategyConfig{bundle(5), {}, 1000, bundle(10'000), bundle(1), 30}};
auto episode = EcosystemEpisode::create(config, founders, 2);
CHECK(episode);
const auto opening = sum(episode->community());
const auto stepped = episode->step();
CHECK(stepped && *stepped);
CHECK(!*episode->community().active(3)); // Bitten for its whole inventory.
CHECK(episode->report().predation_deaths == 1);
CHECK(episode->report().lifespan_deaths == 0);
const auto summary = episode->run();
CHECK(summary);
// The predator outlives its prey and then reaches its own lifespan.
CHECK(summary->predation_deaths == 1 && summary->lifespan_deaths == 1);
CHECK(summary->extinction_tick == 9 && summary->ticks == 9);
CHECK(summary->births == 0 && summary->conserved == opening);
CHECK(episode->lineage(3) == 3); // Ancestry survives a predation death.
}
void births_exhaust_capacity() {
// Each parent funds one child per tick; children inherit the same strategy.
const EcosystemConfig config{producer(), {}, {}, {}, 1000, 6, 200};
const std::array founders{StrategyConfig{bundle(64), {}, 1000, bundle(2), bundle(2), 120}};
auto episode = EcosystemEpisode::create(config, founders, 3);
CHECK(episode);
const auto opening = sum(episode->community());
const auto summary = episode->run();
CHECK(summary);
CHECK(summary->births == 6); // Exactly the reserved slots, never more.
CHECK(summary->predation_deaths == 0 && summary->lifespan_deaths == 7);
CHECK(summary->extinction_tick == summary->ticks && summary->conserved == opening);
CHECK(summary->surviving_lineages == 0);
for (std::uint32_t id = 2; id <= 8; ++id) {
CHECK(*episode->community().born(id) && episode->lineage(id) == 2);
}
}
void tick_limit_stops_a_living_community() {
auto config = grazing_world();
config.tick_limit = 25;
const std::array founders{grazer(), forager()};
auto episode = EcosystemEpisode::create(config, founders, 4);
CHECK(episode);
const auto summary = episode->run();
CHECK(summary);
CHECK(summary->ticks == 25 && summary->extinction_tick == 0);
CHECK(summary->surviving_lineages >= 1 && summary->conserved == sum(episode->community()));
}
void seeded_episodes_conserve_and_end() {
const auto config = grazing_world();
const std::array founders{grazer(), forager()};
std::uint64_t total_ticks{}, extinctions{}, births{}, predation{}, lifespan{};
std::uint64_t shortest = std::numeric_limits<std::uint64_t>::max(), longest{};
std::uint64_t both_lineages{};
for (std::uint64_t seed = 0; seed < 64; ++seed) {
auto episode = EcosystemEpisode::create(config, founders, seed);
CHECK(episode);
const auto opening = sum(episode->community());
std::uint32_t peak_lineages{};
while (true) {
const auto stepped = episode->step();
CHECK(stepped); // The driver must never provoke a rejected batch.
if (!*stepped) {
break;
}
// Conservation is exact at every tick, not only at the end.
CHECK(sum(episode->community()) == opening);
peak_lineages = std::max(peak_lineages, episode->report().surviving_lineages);
}
const auto summary = episode->report();
CHECK(summary.conserved == opening && summary.seed == seed);
CHECK(summary.ticks > 0 && summary.ticks <= config.tick_limit);
CHECK(summary.births <= config.capacity);
// Every process dies at most once; the producer may also be grazed out.
CHECK(summary.predation_deaths + summary.lifespan_deaths <=
summary.founders + summary.births + 1);
if (summary.extinction_tick != 0) {
CHECK(summary.extinction_tick == summary.ticks);
CHECK(summary.surviving_lineages == 0);
++extinctions;
} else {
CHECK(summary.ticks == config.tick_limit && summary.surviving_lineages >= 1);
}
// Repeating one seed reproduces the whole episode exactly.
auto replay = EcosystemEpisode::create(config, founders, seed);
CHECK(replay);
const auto repeated = replay->run();
CHECK(repeated && *repeated == summary);
total_ticks += summary.ticks;
births += summary.births;
predation += summary.predation_deaths;
lifespan += summary.lifespan_deaths;
shortest = std::min(shortest, summary.ticks);
longest = std::max(longest, summary.ticks);
both_lineages += peak_lineages == 2 ? 1 : 0;
}
// Both mortality paths and reproduction must actually occur across the ensemble.
CHECK(births > 0 && predation > 0 && lifespan > 0);
CHECK(extinctions > 0 && both_lineages > 0 && shortest <= longest);
std::cout << "ecosystem ensemble: seeds=64 ticks=" << total_ticks << " births=" << births
<< " predation=" << predation << " lifespan=" << lifespan
<< " extinctions=" << extinctions << " shortest=" << shortest
<< " longest=" << longest << '\n';
}
void episodes_are_independent() {
const auto config = grazing_world();
const std::array founders{grazer(), forager()};
auto first = EcosystemEpisode::create(config, founders, 11);
auto second = EcosystemEpisode::create(config, founders, 11);
auto other = EcosystemEpisode::create(config, founders, 12);
CHECK(first && second && other);
// Interleaving two episodes cannot let one observe the other.
while (true) {
const auto left = first->step();
const auto right = second->step();
const auto unrelated = other->step();
CHECK(left && right && unrelated);
CHECK(first->report() == second->report());
if (!*left) {
CHECK(!*right);
break;
}
}
CHECK(first->report() == second->report());
CHECK(other->report().seed == 12 && other->report() != first->report());
}
void invalid_configuration_is_rejected() {
const auto config = grazing_world();
const std::array founders{grazer()};
CHECK(!EcosystemEpisode::create(config, {}, 1));
const std::vector<StrategyConfig> crowded(max_strategies + 1, grazer());
CHECK(!EcosystemEpisode::create(config, crowded, 1));
auto no_ticks = config;
no_ticks.tick_limit = 0;
CHECK(!EcosystemEpisode::create(no_ticks, founders, 1));
auto oversized = config;
oversized.capacity = static_cast<std::uint32_t>(max_consumers);
CHECK(!EcosystemEpisode::create(oversized, founders, 1));
for (auto broken : {0U, 1U, 2U}) {
auto invalid = founders[0];
if (broken == 0) {
invalid.lifespan = 0;
} else if (broken == 1) {
invalid.investment = {};
} else {
invalid.assimilation_per_thousand = 1001;
}
const std::array rejected{invalid};
CHECK(!EcosystemEpisode::create(config, rejected, 1));
}
}
} // namespace
int main() try {
solitary_founder_retires();
predation_retires_a_target();
births_exhaust_capacity();
tick_limit_stops_a_living_community();
episodes_are_independent();
invalid_configuration_is_rejected();
seeded_episodes_conserve_and_end();
std::cout << "PASS ecosystem episodes, mortality, lineage and conservation\n";
return 0;
} catch (...) {
return 1;
}