#include "evo/processes/genome_strategy.hpp" #include #include #include #include #include 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(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) == 0 && budget->construction.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.matter[0].value == 200'000 + 1'562'400); CHECK(budget->construction.matter[1].value == 60'000 + 111'600); CHECK(budget->construction.matter[2].value == 20'000 + 223'200); CHECK(budget->construction.matter[3].value == 120'000 + 334'800); CHECK(matter_total(budget->construction) == 2'632'000); CHECK(budget->construction.energy.value == 105'280); // What a plan reports as material is exactly what the composition sums to. const auto plan = plan_development(genome, {500, 0}, temperate); CHECK(plan && plan->material_required == matter_total(budget->construction)); CHECK(plan->energy_required == budget->construction.energy.value); // Unaffordable growth leaves nothing to compose. const EnvironmentState barren{293'150, 0}; const auto starved = metabolic_budget(genome, {500, 0}, barren); CHECK(starved && matter_total(starved->construction) == 0); CHECK(starved->construction.energy.value == 0); } 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(); 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; }