Construction composed the mass difference instead of differencing the two composed bodies. Those are not the same: composing the difference throws away the rounding the existing body already carries, so the budget summed to the right total while funding individual channels short or over. At target mass 1001 growing from 10 to 60 it funded C25 N7 P2 other16, leaving nitrogen and phosphorus one short each and other two over, so spending it could not produce the body the same rules derive. Construction is now the per-channel difference between the composed bodies, which funds C25 N8 P3 other14 for that case. That difference can be negative on a channel, because the remainder rule is not monotone: a body growing from 13 to 14 femtograms sees other fall from 6 to 5. Construction is therefore a transition with an intake and a release rather than a single requirement, and the release is a rounding artifact bounded by a few femtograms, not excretion. The invariant held + intake - release == reached is asserted per channel for the reported case, for the releasing case, across six target sizes by seven growth fractions by four material budgets, and across a forty-step walk from nothing to full growth where accumulated intake and release are checked against the whole history. The net of every transition is also asserted to equal what a plan charges material for. These tests were confirmed to fail against the previous implementation. Native Release, clang-tidy analysis, ASan and format checks pass 19/19. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
390 lines
19 KiB
C++
390 lines
19 KiB
C++
#include "evo/processes/genome_strategy.hpp"
|
|
|
|
#include <iostream>
|
|
#include <source_location>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
namespace {
|
|
using namespace evo::biology;
|
|
using namespace evo::accounting;
|
|
void check(bool condition, std::source_location location = std::source_location::current()) {
|
|
if (!condition) {
|
|
throw std::runtime_error("metabolism line " + std::to_string(location.line()));
|
|
}
|
|
}
|
|
#define CHECK(expression) check(static_cast<bool>(expression))
|
|
// The ADR-0031 fixture: an exact 20 micrometre cube at full growth, so every
|
|
// budget below is hand-calculable against the declared compositions.
|
|
GenomeDraft draft() {
|
|
return {7,
|
|
1,
|
|
1,
|
|
{},
|
|
{{Capability::energy_conversion, 400, 0},
|
|
{Capability::membrane, 10, 0},
|
|
{Capability::target_size, 8'000'000, 0}}};
|
|
}
|
|
Genome build(const GenomeDraft& source) {
|
|
const auto genome = Genome::create(source);
|
|
check(genome.has_value());
|
|
return *genome;
|
|
}
|
|
GenomeDraft capable() {
|
|
auto source = draft();
|
|
source.capabilities.push_back({Capability::movement, 250, 0});
|
|
source.capabilities.push_back({Capability::ingestion, 120'000, 0});
|
|
source.capabilities.push_back({Capability::reproduction, 300, 0});
|
|
return source;
|
|
}
|
|
constexpr EnvironmentState temperate{293'150, 1'000'000'000};
|
|
std::uint64_t matter_total(const Inventory& inventory) {
|
|
std::uint64_t sum = 0;
|
|
for (const auto& mass : inventory.matter) {
|
|
sum += mass.value;
|
|
}
|
|
return sum;
|
|
}
|
|
|
|
void hand_calculated_budget() {
|
|
const auto genome = build(capable());
|
|
const auto budget = metabolic_budget(genome, {1000, 0}, temperate);
|
|
CHECK(budget);
|
|
// Structure 8e6 fg at 500/150/50/300, membrane 28.8e6 fg at 700/50/100/150.
|
|
CHECK(budget->body.matter[0].value == 24'160'000); // 4.0e6 + 20.16e6 carbon
|
|
CHECK(budget->body.matter[1].value == 2'640'000); // 1.2e6 + 1.44e6 nitrogen
|
|
CHECK(budget->body.matter[2].value == 3'280'000); // 0.4e6 + 2.88e6 phosphorus
|
|
CHECK(budget->body.matter[3].value == 6'720'000); // 2.4e6 + 4.32e6 other
|
|
CHECK(budget->body.energy.value == 0); // A body is matter, not energy.
|
|
// A split conserves the whole mass exactly, with nothing lost to rounding.
|
|
CHECK(matter_total(budget->body) == budget->phenotype.total_mass);
|
|
CHECK(budget->maintenance.value == 160'000);
|
|
CHECK(budget->movement.value == 184'000); // 36800 * 20 * 250 / 1000
|
|
CHECK(budget->demand.value == 344'000);
|
|
CHECK(budget->intake_limit.energy.value == 240'000); // 4800 fg at 50000 per 1000
|
|
CHECK(budget->intake_limit.matter[0].value == 2400); // 4800 fg by structure shares
|
|
CHECK(budget->intake_limit.matter[1].value == 720);
|
|
CHECK(budget->intake_limit.matter[2].value == 240);
|
|
CHECK(budget->intake_limit.matter[3].value == 1440);
|
|
CHECK(matter_total(budget->intake_limit) == budget->phenotype.ingestion_rate);
|
|
CHECK(budget->assimilation_per_thousand == 400);
|
|
// A parent divides the body it has, so the investment keeps its composition.
|
|
CHECK(budget->reproduction_investment.matter[0].value == 7'248'000);
|
|
CHECK(budget->reproduction_investment.matter[1].value == 792'000);
|
|
CHECK(budget->reproduction_investment.matter[2].value == 984'000);
|
|
CHECK(budget->reproduction_investment.matter[3].value == 2'016'000);
|
|
CHECK(matter_total(budget->reproduction_investment) == 11'040'000); // 30% of the body
|
|
CHECK(budget->reproduction_investment.energy.value == 441'600); // priced at 40 per 1000
|
|
// A finished organism has nothing left to build.
|
|
CHECK(matter_total(budget->construction.intake) == 0);
|
|
CHECK(matter_total(budget->construction.release) == 0);
|
|
CHECK(budget->construction.intake.energy.value == 0);
|
|
}
|
|
|
|
void construction_composes_each_compartment() {
|
|
const auto genome = build(capable());
|
|
const auto budget = metabolic_budget(genome, {500, 0}, temperate);
|
|
CHECK(budget);
|
|
// The step adds 400,000 fg of structure and 2,232,000 fg of membrane, each
|
|
// composed by its own shares rather than by one blended ratio.
|
|
CHECK(budget->construction.intake.matter[0].value == 200'000 + 1'562'400);
|
|
CHECK(budget->construction.intake.matter[1].value == 60'000 + 111'600);
|
|
CHECK(budget->construction.intake.matter[2].value == 20'000 + 223'200);
|
|
CHECK(budget->construction.intake.matter[3].value == 120'000 + 334'800);
|
|
CHECK(matter_total(budget->construction.intake) == 2'632'000);
|
|
CHECK(matter_total(budget->construction.release) == 0); // Nothing floors here.
|
|
CHECK(budget->construction.intake.energy.value == 105'280);
|
|
// What a plan reports as material is the net of the transition.
|
|
const auto plan = plan_development(genome, {500, 0}, temperate);
|
|
CHECK(plan && plan->material_required == matter_total(budget->construction.intake) -
|
|
matter_total(budget->construction.release));
|
|
CHECK(plan->energy_required == budget->construction.intake.energy.value);
|
|
// Unaffordable growth leaves nothing to move.
|
|
const EnvironmentState barren{293'150, 0};
|
|
const auto starved = metabolic_budget(genome, {500, 0}, barren);
|
|
CHECK(starved && matter_total(starved->construction.intake) == 0);
|
|
CHECK(matter_total(starved->construction.release) == 0);
|
|
CHECK(starved->construction.intake.energy.value == 0);
|
|
}
|
|
|
|
void construction_reproduces_the_derived_body() {
|
|
// Composing the mass difference would fund C25 N7 P2 other16 here, leaving
|
|
// nitrogen and phosphorus one short and other two over, so spending it could
|
|
// not produce the body the same rules derive.
|
|
auto source = draft();
|
|
source.capabilities[2].parameter = 1001; // target size
|
|
const auto genome = build(source);
|
|
const auto before = metabolic_budget(genome, {10, 0}, temperate);
|
|
CHECK(before);
|
|
CHECK(before->body.matter[0].value == 5 && before->body.matter[1].value == 1);
|
|
CHECK(before->body.matter[2].value == 0 && before->body.matter[3].value == 4);
|
|
CHECK(before->construction.intake.matter[0].value == 25);
|
|
CHECK(before->construction.intake.matter[1].value == 8);
|
|
CHECK(before->construction.intake.matter[2].value == 3);
|
|
CHECK(before->construction.intake.matter[3].value == 14);
|
|
CHECK(matter_total(before->construction.intake) == 50);
|
|
CHECK(matter_total(before->construction.release) == 0);
|
|
const auto after = metabolic_budget(genome, {60, 0}, temperate);
|
|
CHECK(after);
|
|
CHECK(after->body.matter[0].value == 30 && after->body.matter[1].value == 9);
|
|
CHECK(after->body.matter[2].value == 3 && after->body.matter[3].value == 18);
|
|
for (std::size_t index = 0; index < 4; ++index) {
|
|
CHECK(before->body.matter[index].value + before->construction.intake.matter[index].value -
|
|
before->construction.release.matter[index].value ==
|
|
after->body.matter[index].value);
|
|
}
|
|
// A step can also free a femtogram as the declared shares rebalance: this
|
|
// body goes from 13 to 14 fg, and other falls from 6 to 5.
|
|
source.capabilities[2].parameter = 1000;
|
|
const auto small = build(source);
|
|
const EnvironmentState scarce{293'150, 1};
|
|
const auto crawling = metabolic_budget(small, {13, 0}, scarce);
|
|
CHECK(crawling);
|
|
CHECK(crawling->body.matter[3].value == 6);
|
|
CHECK(crawling->construction.intake.matter[0].value == 1);
|
|
CHECK(crawling->construction.intake.matter[1].value == 1);
|
|
CHECK(crawling->construction.release.matter[3].value == 1);
|
|
const auto stepped = metabolic_budget(small, {14, 0}, scarce);
|
|
CHECK(stepped && stepped->body.matter[3].value == 5);
|
|
for (std::size_t index = 0; index < 4; ++index) {
|
|
CHECK(crawling->body.matter[index].value +
|
|
crawling->construction.intake.matter[index].value -
|
|
crawling->construction.release.matter[index].value ==
|
|
stepped->body.matter[index].value);
|
|
}
|
|
}
|
|
|
|
void every_transition_conserves_each_channel() {
|
|
auto source = capable();
|
|
for (const std::uint32_t target : {1U, 999U, 1001U, 9'999U, 123'457U, 8'000'000U}) {
|
|
source.capabilities[2].parameter = target;
|
|
const auto genome = build(source);
|
|
for (const std::uint32_t realized : {0U, 1U, 13U, 333U, 500U, 950U, 1000U}) {
|
|
for (const std::uint64_t material : {0ULL, 1ULL, 7ULL, 1'000'000'000ULL}) {
|
|
const EnvironmentState environment{293'150, material};
|
|
const auto budget = metabolic_budget(genome, {realized, 0}, environment);
|
|
const auto plan = plan_development(genome, {realized, 0}, environment);
|
|
CHECK(budget && plan);
|
|
const auto reached = metabolic_budget(genome, plan->after, environment);
|
|
CHECK(reached);
|
|
// Spending the budget must reproduce the derived body exactly, on
|
|
// every channel, not merely in total.
|
|
for (std::size_t index = 0; index < 4; ++index) {
|
|
CHECK(budget->body.matter[index].value +
|
|
budget->construction.intake.matter[index].value -
|
|
budget->construction.release.matter[index].value ==
|
|
reached->body.matter[index].value);
|
|
}
|
|
// The net of the transition is what the plan charges material for.
|
|
CHECK(matter_total(budget->construction.intake) >=
|
|
matter_total(budget->construction.release));
|
|
CHECK(matter_total(budget->construction.intake) -
|
|
matter_total(budget->construction.release) ==
|
|
plan->material_required);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void repeated_growth_conserves_each_channel() {
|
|
auto source = capable();
|
|
source.capabilities[2].parameter = 123'457;
|
|
const auto genome = build(source);
|
|
// Walk a body from nothing to its target one funded step at a time, checking
|
|
// every channel across the whole history rather than only one transition.
|
|
GrowthState growth{0, 0};
|
|
Inventory acquired{}, freed{};
|
|
const auto opening = metabolic_budget(genome, growth, temperate);
|
|
CHECK(opening);
|
|
for (int step = 0; step < 40 && growth.realized_per_thousand < 1000; ++step) {
|
|
const auto budget = metabolic_budget(genome, growth, temperate);
|
|
const auto plan = plan_development(genome, growth, temperate);
|
|
CHECK(budget && plan);
|
|
for (std::size_t index = 0; index < 4; ++index) {
|
|
acquired.matter[index].value += budget->construction.intake.matter[index].value;
|
|
freed.matter[index].value += budget->construction.release.matter[index].value;
|
|
}
|
|
growth = plan->after;
|
|
}
|
|
CHECK(growth.realized_per_thousand == 1000);
|
|
const auto finished = metabolic_budget(genome, growth, temperate);
|
|
CHECK(finished);
|
|
for (std::size_t index = 0; index < 4; ++index) {
|
|
CHECK(opening->body.matter[index].value + acquired.matter[index].value -
|
|
freed.matter[index].value ==
|
|
finished->body.matter[index].value);
|
|
}
|
|
}
|
|
|
|
void thicker_membrane_costs_matter_and_maintenance() {
|
|
auto source = capable();
|
|
const auto thin = metabolic_budget(build(source), {1000, 0}, temperate);
|
|
source.capabilities[1].parameter = 20; // membrane, twice as thick
|
|
const auto thick = metabolic_budget(build(source), {1000, 0}, temperate);
|
|
CHECK(thin && thick);
|
|
// Costs more matter to build and more energy to hold.
|
|
CHECK(matter_total(thick->body) == 65'600'000);
|
|
CHECK(matter_total(thick->body) > matter_total(thin->body));
|
|
CHECK(thick->maintenance.value == 304'000); // 65600*2 + 57600*3
|
|
CHECK(thick->maintenance.value > thin->maintenance.value);
|
|
// Membrane is the carbon-rich compartment, so composition shifts, not just mass.
|
|
CHECK(thick->body.matter[0].value == 44'320'000);
|
|
const auto thin_carbon_share = thin->body.matter[0].value * 1000 / matter_total(thin->body);
|
|
const auto thick_carbon_share = thick->body.matter[0].value * 1000 / matter_total(thick->body);
|
|
CHECK(thick_carbon_share > thin_carbon_share);
|
|
// Hauling the heavier body costs more, so a thick membrane is paid for twice.
|
|
CHECK(thick->movement.value > thin->movement.value);
|
|
// These budgets are untouched: nothing here is a multiplier over a total.
|
|
CHECK(thick->intake_limit == thin->intake_limit);
|
|
CHECK(thick->phenotype.movement_speed == thin->phenotype.movement_speed);
|
|
CHECK(thick->assimilation_per_thousand == thin->assimilation_per_thousand);
|
|
CHECK(thick->phenotype.structural_mass == thin->phenotype.structural_mass);
|
|
// Doubling one parameter moves the affected budgets by different ratios.
|
|
const auto mass_ratio = matter_total(thick->body) * 1000 / matter_total(thin->body);
|
|
const auto maintenance_ratio = thick->maintenance.value * 1000 / thin->maintenance.value;
|
|
CHECK(mass_ratio != maintenance_ratio);
|
|
}
|
|
|
|
void size_moves_several_budgets_together() {
|
|
const auto genome = build(capable());
|
|
const auto small = metabolic_budget(genome, {500, 0}, temperate);
|
|
const auto large = metabolic_budget(genome, {1000, 0}, temperate);
|
|
CHECK(small && large);
|
|
CHECK(matter_total(large->body) > matter_total(small->body));
|
|
CHECK(large->maintenance.value > small->maintenance.value);
|
|
CHECK(large->movement.value > small->movement.value);
|
|
CHECK(matter_total(large->intake_limit) > matter_total(small->intake_limit));
|
|
CHECK(matter_total(large->reproduction_investment) >
|
|
matter_total(small->reproduction_investment));
|
|
// Uptake rises with surface while maintenance rises with mass, so the two do
|
|
// not move together: size is a genuine tradeoff, not a uniform gain.
|
|
const auto uptake_ratio =
|
|
matter_total(large->intake_limit) * 1000 / matter_total(small->intake_limit);
|
|
const auto maintenance_ratio = large->maintenance.value * 1000 / small->maintenance.value;
|
|
CHECK(uptake_ratio != maintenance_ratio);
|
|
}
|
|
|
|
void absent_capabilities_cost_nothing() {
|
|
const auto bare = build(draft());
|
|
const auto budget = metabolic_budget(bare, {1000, 0}, temperate);
|
|
CHECK(budget);
|
|
CHECK(budget->movement.value == 0); // No movement capability.
|
|
CHECK(budget->demand.value == budget->maintenance.value);
|
|
CHECK(matter_total(budget->intake_limit) == 0); // No ingestion capability.
|
|
CHECK(budget->intake_limit.energy.value == 0);
|
|
CHECK(matter_total(budget->reproduction_investment) == 0); // No reproduction capability.
|
|
CHECK(budget->reproduction_investment.energy.value == 0);
|
|
// A body still costs maintenance whatever it cannot do.
|
|
CHECK(budget->maintenance.value == 160'000);
|
|
CHECK(matter_total(budget->body) == 36'800'000);
|
|
}
|
|
|
|
void rules_fail_closed() {
|
|
CHECK(metabolic_rules_for(1));
|
|
for (const auto version : {0U, 2U, 99U}) {
|
|
const auto missing = metabolic_rules_for(version);
|
|
CHECK(!missing && missing.error() == DevelopmentError::unsupported_rules);
|
|
}
|
|
auto source = draft();
|
|
source.development_version = 3;
|
|
const auto future = build(source);
|
|
const auto refused = metabolic_budget(future, {1000, 0}, temperate);
|
|
CHECK(!refused && refused.error() == DevelopmentError::unsupported_rules);
|
|
const auto genome = build(draft());
|
|
const auto invalid = metabolic_budget(genome, {1001, 0}, temperate);
|
|
CHECK(!invalid && invalid.error() == DevelopmentError::invalid_growth);
|
|
// Both compositions are declared, and both sum to the whole.
|
|
const auto rules = metabolic_rules_for(1);
|
|
std::uint32_t structure_total{}, membrane_total{};
|
|
for (std::size_t index = 0; index < 4; ++index) {
|
|
structure_total += rules->structure[index];
|
|
membrane_total += rules->membrane[index];
|
|
}
|
|
CHECK(structure_total == 1000 && membrane_total == 1000);
|
|
}
|
|
|
|
void splits_conserve_at_awkward_masses() {
|
|
// Masses chosen so every share floors, forcing the last channel to absorb it.
|
|
auto source = draft();
|
|
for (const std::uint32_t target : {1001U, 9'999U, 123'457U, 999'999'999U}) {
|
|
source.capabilities[2].parameter = target;
|
|
const auto genome = build(source);
|
|
for (const std::uint32_t realized : {1U, 333U, 999U, 1000U}) {
|
|
const auto budget = metabolic_budget(genome, {realized, 0}, temperate);
|
|
CHECK(budget);
|
|
// Whatever the rounding, the parts always sum back to the whole.
|
|
CHECK(matter_total(budget->body) == budget->phenotype.total_mass);
|
|
CHECK(matter_total(budget->intake_limit) == budget->phenotype.ingestion_rate);
|
|
}
|
|
}
|
|
}
|
|
|
|
void a_genome_drives_a_feeding_tick() {
|
|
using namespace evo::processes;
|
|
const auto genome = build(capable());
|
|
const auto opening =
|
|
Inventory{{0}, {Femtograms{0}, Femtograms{0}, Femtograms{0}, Femtograms{0}}};
|
|
const auto consumer = consumer_for(genome, {1000, 0}, temperate, opening);
|
|
CHECK(consumer);
|
|
const auto budget = metabolic_budget(genome, {1000, 0}, temperate);
|
|
CHECK(budget && consumer->intake_limit == budget->intake_limit);
|
|
CHECK(consumer->assimilation_per_thousand == 400);
|
|
CHECK(consumer->opening == opening); // The caller's, never invented.
|
|
// The derived cap drives a real community tick and the ledger still balances.
|
|
const ProducerConfig plants{
|
|
{{1'000'000}, 1},
|
|
{2, 1},
|
|
1000,
|
|
{1},
|
|
{Femtograms{4000}, Femtograms{4000}, Femtograms{4000}, Femtograms{4000}}};
|
|
const std::array configs{*consumer};
|
|
auto community = FeedingCommunity::create(
|
|
plants, {10'000'000},
|
|
{Femtograms{100'000}, Femtograms{100'000}, Femtograms{100'000}, Femtograms{100'000}},
|
|
configs);
|
|
CHECK(community);
|
|
Inventory total{};
|
|
for (const auto& balance : community->balances()) {
|
|
total.energy.value += balance.energy.value;
|
|
for (std::size_t index = 0; index < 4; ++index) {
|
|
total.matter[index].value += balance.matter[index].value;
|
|
}
|
|
}
|
|
const std::array meal{FeedIntent{
|
|
1, 2, 1, {{100}, {Femtograms{2000}, Femtograms{720}, Femtograms{240}, Femtograms{1440}}}}};
|
|
const auto receipts = community->advance(evo::Tick{1}, meal);
|
|
CHECK(receipts && receipts->size() == 1);
|
|
Inventory after{};
|
|
for (const auto& balance : community->balances()) {
|
|
after.energy.value += balance.energy.value;
|
|
for (std::size_t index = 0; index < 4; ++index) {
|
|
after.matter[index].value += balance.matter[index].value;
|
|
}
|
|
}
|
|
CHECK(after == total); // Conserved exactly, as every M2 tick must be.
|
|
// A bite past the genome's derived cap is refused by the existing contract.
|
|
const std::array excessive{FeedIntent{
|
|
1, 2, 1, {{100}, {Femtograms{2401}, Femtograms{0}, Femtograms{0}, Femtograms{0}}}}};
|
|
const auto denied = community->advance(evo::Tick{2}, excessive);
|
|
CHECK(!denied && denied.error() == FeedingError::intake_limit);
|
|
}
|
|
} // namespace
|
|
int main() try {
|
|
hand_calculated_budget();
|
|
construction_composes_each_compartment();
|
|
construction_reproduces_the_derived_body();
|
|
every_transition_conserves_each_channel();
|
|
repeated_growth_conserves_each_channel();
|
|
thicker_membrane_costs_matter_and_maintenance();
|
|
size_moves_several_budgets_together();
|
|
absent_capabilities_cost_nothing();
|
|
rules_fail_closed();
|
|
splits_conserve_at_awkward_masses();
|
|
a_genome_drives_a_feeding_tick();
|
|
std::cout << "PASS metabolic budgets, composition splits and genome-driven feeding\n";
|
|
return 0;
|
|
} catch (...) {
|
|
return 1;
|
|
}
|