Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 2m26s
Foundation / checks (tsan) (push) Successful in 11m44s
Foundation / checks (client) (push) Successful in 19m23s
Foundation / checks (network) (push) Successful in 19m45s
Foundation / checks (checks) (push) Successful in 45m39s
Foundation / checks (postgres) (push) Successful in 45m56s
Foundation / postgres-tools (push) Successful in 23s
Foundation / server-image (push) Successful in 31s
Foundation / image (push) Successful in 3m35s
219 lines
8.7 KiB
C++
219 lines
8.7 KiB
C++
#pragma once
|
|
|
|
// ADR-0134: an isolated exact-state experiment, not Cohort v1 or runtime biology.
|
|
#include "../../tests/fixtures/resolution_model.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <compare>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <limits>
|
|
#include <numeric>
|
|
#include <span>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
namespace evo::experiment {
|
|
using fixture::insist;
|
|
|
|
inline std::uint64_t checked_add(std::uint64_t left, std::uint64_t right) {
|
|
insist(right <= std::numeric_limits<std::uint64_t>::max() - left, "strata sum overflow");
|
|
return left + right;
|
|
}
|
|
inline std::uint64_t checked_product(std::uint64_t left, std::uint64_t right) {
|
|
insist(right == 0 || left <= std::numeric_limits<std::uint64_t>::max() / right,
|
|
"strata product overflow");
|
|
return left * right;
|
|
}
|
|
|
|
struct ReserveStratum {
|
|
std::uint64_t first{}, count{}, age{}, energy{}, matter{};
|
|
auto operator<=>(const ReserveStratum&) const = default;
|
|
};
|
|
struct IdentifiedReserve {
|
|
std::uint64_t identity{}, age{}, energy{}, matter{};
|
|
auto operator<=>(const IdentifiedReserve&) const = default;
|
|
};
|
|
|
|
class ReserveStrata {
|
|
public:
|
|
explicit ReserveStrata(const fixture::IndividualRun& source)
|
|
: rule_{source.rule}, next_identity_{source.members.size()},
|
|
environment_{source.environment}, matter_store_{source.matter_store},
|
|
detritus_energy_{source.detritus_energy}, detritus_matter_{source.detritus_matter},
|
|
births_{source.births}, deaths_{source.deaths} {
|
|
insist(rule_.threshold_energy >= rule_.investment_energy,
|
|
"strata unsupported underfunded breeding rule");
|
|
for (std::size_t index = 0; index < source.members.size(); ++index) {
|
|
const auto& member = source.members[index];
|
|
if (member.alive) {
|
|
append(groups_, {index, 1, member.age, member.energy, member.matter});
|
|
}
|
|
}
|
|
// Validate the exact totals rather than trusting wrapped reference sums.
|
|
(void)total_energy();
|
|
(void)total_matter();
|
|
}
|
|
|
|
[[nodiscard]] std::span<const ReserveStratum> groups() const noexcept {
|
|
return groups_;
|
|
}
|
|
[[nodiscard]] std::size_t capacity_bytes() const noexcept {
|
|
return groups_.capacity() * sizeof(ReserveStratum);
|
|
}
|
|
[[nodiscard]] fixture::Census census() const {
|
|
fixture::Census result;
|
|
result.births = births_;
|
|
result.deaths = deaths_;
|
|
for (const auto& group : groups_) {
|
|
result.living = checked_add(result.living, group.count);
|
|
result.energy = checked_add(result.energy, checked_product(group.count, group.energy));
|
|
result.matter = checked_add(result.matter, checked_product(group.count, group.matter));
|
|
}
|
|
return result;
|
|
}
|
|
[[nodiscard]] std::uint64_t total_energy() const {
|
|
return checked_add(checked_add(environment_, detritus_energy_), census().energy);
|
|
}
|
|
[[nodiscard]] std::uint64_t total_matter() const {
|
|
return checked_add(checked_add(matter_store_, detritus_matter_), census().matter);
|
|
}
|
|
[[nodiscard]] std::uint64_t environment() const noexcept {
|
|
return environment_;
|
|
}
|
|
[[nodiscard]] std::uint64_t matter_store() const noexcept {
|
|
return matter_store_;
|
|
}
|
|
[[nodiscard]] std::uint64_t detritus_energy() const noexcept {
|
|
return detritus_energy_;
|
|
}
|
|
[[nodiscard]] std::uint64_t detritus_matter() const noexcept {
|
|
return detritus_matter_;
|
|
}
|
|
[[nodiscard]] std::uint64_t next_identity() const noexcept {
|
|
return next_identity_;
|
|
}
|
|
|
|
[[nodiscard]] std::vector<IdentifiedReserve> portrait() const {
|
|
std::vector<IdentifiedReserve> result;
|
|
for (const auto& group : groups_) {
|
|
for (std::uint64_t index = 0; index < group.count; ++index) {
|
|
result.push_back(
|
|
{checked_add(group.first, index), group.age, group.energy, group.matter});
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
void advance(std::uint64_t ticks) {
|
|
// Interval partitioning only; never replace fixed steps with a larger dt.
|
|
for (std::uint64_t tick = 0; tick < ticks; ++tick) {
|
|
step();
|
|
}
|
|
}
|
|
|
|
void step() {
|
|
const auto share = rule_.per_capita(census().living); // Before starvation, like reference.
|
|
for (auto& group : groups_) {
|
|
if (rule_.starves(group.energy)) {
|
|
retire(group);
|
|
continue;
|
|
}
|
|
group.energy -= rule_.upkeep_energy;
|
|
detritus_energy_ =
|
|
checked_add(detritus_energy_, checked_product(group.count, rule_.upkeep_energy));
|
|
const auto paid = std::min(group.matter, rule_.upkeep_matter);
|
|
group.matter -= paid;
|
|
detritus_matter_ = checked_add(detritus_matter_, checked_product(group.count, paid));
|
|
const auto energy = checked_product(group.count, share);
|
|
const auto matter = checked_product(group.count, rule_.matter_intake);
|
|
insist(environment_ >= energy, "strata environment depleted");
|
|
insist(matter_store_ >= matter, "strata matter store depleted");
|
|
environment_ -= energy;
|
|
matter_store_ -= matter;
|
|
group.energy = checked_add(group.energy, share);
|
|
group.matter = checked_add(group.matter, rule_.matter_intake);
|
|
group.age = checked_add(group.age, 1);
|
|
if (group.age >= rule_.lifespan) {
|
|
retire(group);
|
|
}
|
|
}
|
|
std::erase_if(groups_, [](const auto& group) { return group.count == 0; });
|
|
|
|
auto population = census().living;
|
|
std::vector<std::size_t> order(groups_.size());
|
|
std::iota(order.begin(), order.end(), std::size_t{0});
|
|
std::ranges::sort(order, [&](std::size_t left, std::size_t right) {
|
|
if (groups_[left].age != groups_[right].age) {
|
|
return groups_[left].age > groups_[right].age;
|
|
}
|
|
return groups_[left].first < groups_[right].first;
|
|
});
|
|
std::vector<std::uint64_t> reproducing(groups_.size());
|
|
std::uint64_t newborns = 0;
|
|
for (const auto index : order) {
|
|
const auto& group = groups_[index];
|
|
if (population >= rule_.capacity) {
|
|
break;
|
|
}
|
|
if (!rule_.breeds(group.age, group.energy) || group.matter < rule_.investment_matter) {
|
|
continue;
|
|
}
|
|
const auto count = std::min(group.count, rule_.capacity - population);
|
|
reproducing[index] = count;
|
|
population = checked_add(population, count);
|
|
newborns = checked_add(newborns, count);
|
|
}
|
|
std::vector<ReserveStratum> after;
|
|
for (std::size_t index = 0; index < groups_.size(); ++index) {
|
|
auto group = groups_[index];
|
|
const auto count = reproducing[index];
|
|
if (count != 0) {
|
|
append(after,
|
|
{group.first, count, group.age, group.energy - rule_.investment_energy,
|
|
group.matter - rule_.investment_matter});
|
|
group.first = checked_add(group.first, count);
|
|
group.count -= count;
|
|
}
|
|
append(after, group);
|
|
}
|
|
append(after,
|
|
{next_identity_, newborns, 0, rule_.investment_energy, rule_.investment_matter});
|
|
next_identity_ = checked_add(next_identity_, newborns);
|
|
births_ = checked_add(births_, newborns);
|
|
groups_ = std::move(after);
|
|
}
|
|
|
|
private:
|
|
static void append(std::vector<ReserveStratum>& target, ReserveStratum group) {
|
|
if (group.count == 0) {
|
|
return;
|
|
}
|
|
(void)checked_add(group.first, group.count);
|
|
if (!target.empty()) {
|
|
auto& previous = target.back();
|
|
if (checked_add(previous.first, previous.count) == group.first &&
|
|
previous.age == group.age && previous.energy == group.energy &&
|
|
previous.matter == group.matter) {
|
|
previous.count = checked_add(previous.count, group.count);
|
|
return;
|
|
}
|
|
}
|
|
target.push_back(group);
|
|
}
|
|
void retire(ReserveStratum& group) {
|
|
detritus_energy_ =
|
|
checked_add(detritus_energy_, checked_product(group.count, group.energy));
|
|
detritus_matter_ =
|
|
checked_add(detritus_matter_, checked_product(group.count, group.matter));
|
|
deaths_ = checked_add(deaths_, group.count);
|
|
group.count = 0;
|
|
}
|
|
fixture::Rule rule_;
|
|
std::uint64_t next_identity_{};
|
|
std::vector<ReserveStratum> groups_;
|
|
std::uint64_t environment_{}, matter_store_{}, detritus_energy_{}, detritus_matter_{};
|
|
std::uint64_t births_{}, deaths_{};
|
|
};
|
|
} // namespace evo::experiment
|