The helper summing available material added four unrestricted u64 channels without a check. With 2^62 in each, the sum reaches exactly 2^64 and wraps to zero, so the bound read as no material at all and both planning and budgeting refused growth that a billion femtograms per channel funds. The sum now saturates. That is also the right answer rather than merely a safe one: no step can need more than u64 femtograms, so a saturated bound is correctly permissive. Regressions in both suites use the exact 2^62 case and a single saturated channel, and require the step that an ordinary supply funds. Both were confirmed to fail without saturation. The equivalent helper in metabolism.cpp is applied only to inventories derived from bounded phenotype masses, never to caller input, so it is left alone rather than given a branch no test could reach. That is recorded in the validation report. Native Release, clang-tidy analysis, ASan and format checks pass 19/19. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
490 lines
24 KiB
C++
490 lines
24 KiB
C++
#include "evo/processes/genome_strategy.hpp"
|
|
|
|
#include <iostream>
|
|
#include <limits>
|
|
#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;
|
|
}
|
|
// Every channel stocked alike, so availability never accidentally hides a
|
|
// per-element shortfall except where a test sets one deliberately.
|
|
constexpr Inventory pooled(std::uint64_t value) {
|
|
return {{value}, {Femtograms{value}, Femtograms{value}, Femtograms{value}, Femtograms{value}}};
|
|
}
|
|
constexpr EnvironmentState temperate{293'150, pooled(1'000'000'000)};
|
|
bool fits_within(const Inventory& needed, const Inventory& available) {
|
|
if (needed.energy.value > available.energy.value) {
|
|
return false;
|
|
}
|
|
for (std::size_t index = 0; index < 4; ++index) {
|
|
if (needed.matter[index].value > available.matter[index].value) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
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);
|
|
CHECK(budget->construction.after == plan->after);
|
|
// Unaffordable growth leaves nothing to move.
|
|
const EnvironmentState barren{293'150, pooled(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. At a
|
|
// target of 20 fg a full step moves the body from 13 to 14 fg: carbon and
|
|
// nitrogen each gain one while other falls from 6 to 5.
|
|
source.capabilities[2].parameter = 20;
|
|
const auto small = build(source);
|
|
const auto crawling = metabolic_budget(small, {650, 0}, temperate);
|
|
CHECK(crawling && crawling->phenotype.structural_mass == 13);
|
|
CHECK(crawling->body.matter[3].value == 6);
|
|
CHECK(crawling->construction.after == (GrowthState{700, 0}));
|
|
CHECK(crawling->construction.intake.matter[0].value == 1);
|
|
CHECK(crawling->construction.intake.matter[1].value == 1);
|
|
CHECK(crawling->construction.intake.matter[2].value == 0);
|
|
CHECK(crawling->construction.release.matter[3].value == 1);
|
|
const auto stepped = metabolic_budget(small, {700, 0}, temperate);
|
|
CHECK(stepped && stepped->phenotype.structural_mass == 14);
|
|
CHECK(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 released_matter_never_funds_another_element() {
|
|
// The 13 to 14 fg step again: net growth is one femtogram, but it acquires a
|
|
// carbon and a nitrogen while freeing one of other. Released other is neither
|
|
// carbon nor nitrogen, so a single femtogram cannot fund it whatever channel
|
|
// that femtogram sits in.
|
|
auto source = draft();
|
|
source.capabilities[2].parameter = 20;
|
|
const auto genome = build(source);
|
|
for (std::size_t channel = 0; channel < 4; ++channel) {
|
|
Inventory single{{1'000'000}, {}};
|
|
single.matter[channel].value = 1; // One femtogram, and only one.
|
|
const EnvironmentState scarce{293'150, single};
|
|
// The mass-only plan is what would be fooled: it sees a net of one
|
|
// femtogram against a pool of one and admits the step.
|
|
const auto plan = plan_development(genome, {650, 0}, scarce);
|
|
CHECK(plan && plan->after == (GrowthState{700, 0}) && plan->material_required == 1);
|
|
const auto budget = metabolic_budget(genome, {650, 0}, scarce);
|
|
CHECK(budget);
|
|
CHECK(budget->construction.after != (GrowthState{700, 0})); // Not funded.
|
|
// Nothing is acquired at all: the steps that remain change no mass.
|
|
CHECK(matter_total(budget->construction.intake) == 0);
|
|
CHECK(matter_total(budget->construction.release) == 0);
|
|
CHECK(fits_within(budget->construction.intake, single));
|
|
const auto reached = metabolic_budget(genome, budget->construction.after, scarce);
|
|
CHECK(reached && reached->phenotype.structural_mass == 13);
|
|
}
|
|
// A carbon and a nitrogen together are exactly what the step needs.
|
|
Inventory exact{{1'000'000}, {}};
|
|
exact.matter[0].value = 1;
|
|
exact.matter[1].value = 1;
|
|
const auto precise = metabolic_budget(genome, {650, 0}, {293'150, exact});
|
|
CHECK(precise && precise->construction.after == (GrowthState{700, 0}));
|
|
CHECK(precise->construction.intake.matter[0].value == 1);
|
|
CHECK(precise->construction.intake.matter[1].value == 1);
|
|
CHECK(precise->construction.release.matter[3].value == 1);
|
|
}
|
|
|
|
void enormous_availability_still_funds_growth() {
|
|
const auto genome = build(capable());
|
|
// Budgeting reads the same availability bound, so a wrapped total would
|
|
// refuse this step too. Four channels of 2^62 sum to exactly 2^64.
|
|
constexpr std::uint64_t vast = 1ULL << 62U;
|
|
const Inventory enormous{
|
|
{std::numeric_limits<std::uint64_t>::max()},
|
|
{Femtograms{vast}, Femtograms{vast}, Femtograms{vast}, Femtograms{vast}}};
|
|
const auto budget = metabolic_budget(genome, {500, 0}, {293'150, enormous});
|
|
const auto ordinary = metabolic_budget(genome, {500, 0}, temperate);
|
|
CHECK(budget && ordinary);
|
|
CHECK(budget->construction.after == (GrowthState{550, 0}));
|
|
CHECK(budget->construction == ordinary->construction);
|
|
CHECK(matter_total(budget->construction.intake) == 2'632'000);
|
|
}
|
|
|
|
void every_transition_is_affordable_channel_by_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 stock : {0ULL, 1ULL, 7ULL, 1'000'000ULL}) {
|
|
const EnvironmentState environment{293'150, pooled(stock)};
|
|
const auto budget = metabolic_budget(genome, {realized, 0}, environment);
|
|
CHECK(budget);
|
|
// Whatever a step reports, every element of it must be there.
|
|
CHECK(fits_within(budget->construction.intake, environment.available));
|
|
// A funded step never exceeds what the mass-only plan proposed.
|
|
const auto plan = plan_development(genome, {realized, 0}, environment);
|
|
CHECK(plan && budget->construction.after.realized_per_thousand <=
|
|
plan->after.realized_per_thousand);
|
|
CHECK(budget->construction.after.damage_per_thousand == 0);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
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, pooled(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, budget->construction.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);
|
|
CHECK(budget &&
|
|
budget->construction.after.realized_per_thousand > growth.realized_per_thousand);
|
|
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 = budget->construction.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();
|
|
released_matter_never_funds_another_element();
|
|
enormous_availability_still_funds_growth();
|
|
every_transition_is_affordable_channel_by_channel();
|
|
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;
|
|
}
|