Files

618 lines
25 KiB
C++

#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 <algorithm>
#include <array>
#include <cstdint>
#include <ranges>
#include <vector>
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<Member> 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<std::uint64_t>(members.size() * sizeof(Member));
}
[[nodiscard]] std::uint64_t rows() const {
return static_cast<std::uint64_t>(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<std::size_t> 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<AggregateClass> 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<std::uint64_t>(classes.size() * sizeof(AggregateClass));
}
[[nodiscard]] std::uint64_t rows() const {
return static_cast<std::uint64_t>(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<std::array<std::uint64_t, 3>>;
[[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