Files
aevolution/tests/ecosystem_tests.cpp
T
dingoandClaude Opus 5 72ed03f4dd Stop calling the end of watching a death
Three defects from review of 89ad95b.

An organism still alive when an episode stopped was recorded dead. A one-tick
run reported no extinctions while writing terminal records for founders that had
not died, and their watched time was added to the same total as completed
lifetimes. Observation ending is a fact about the run, not about the organism,
and ADR-0034 makes a recorded death terminal, so there was nothing to be
terminal about. Survivors stay living now and are counted apart, with their
watched time as the lower bound it is; lived_ticks and observed_ticks answer
different questions and are never summed together.

A genome without the reproduction capability derives a zero investment, which
the driver refused outright, so seeds 10, 12 and 15 failed the whole run.
Filtering those variants out before sampling would have been the worse repair:
the measured set would then be the fertile subset a guard approved of, and being
unable to reproduce is a result rather than grounds for exclusion. The driver
runs such a strategy and proposes no birth for it. ADR-0029 is amended rather
than quietly contradicted.

A half's thermal shortfall was assigned where it was documented as a total, so
two episodes at 15,000 reported 15,000.

Only configurations the driver already rejected behave differently, so the M2.6
pin 2cd4a15f9fbe9949 was checked rather than assumed and still holds. The
ensemble pin moved because the reported facts changed, not to pass a test; the
reason sits beside it. Each fix was confirmed load-bearing by reverting it alone.

Release 33/33, analysis, ASan 33/33 and clang-format all ran and passed here.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-24 05:58:22 -04:00

251 lines
11 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}) {
auto invalid = founders[0];
if (broken == 0) {
invalid.lifespan = 0;
} else {
invalid.assimilation_per_thousand = 1001;
}
const std::array rejected{invalid};
CHECK(!EcosystemEpisode::create(config, rejected, 1));
}
// A strategy with nothing to invest does not reproduce, which is a genome a
// mutation can produce. It is run rather than refused, and it leaves nothing.
auto barren = founders[0];
barren.investment = {};
const std::array sterile{barren};
auto episode = EcosystemEpisode::create(config, sterile, 1);
CHECK(episode);
const auto summary = episode->run();
CHECK(summary && summary->births == 0);
}
} // 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;
}