Files
aevolution/tests/offspring_tests.cpp
dingo 5d8aa4d2e3
Foundation / checks (ecs) (push) Successful in 2m27s
Foundation / checks (deploy) (push) Successful in 4m15s
Foundation / checks (network) (push) Successful in 21m4s
Foundation / checks (client) (push) Successful in 22m22s
Foundation / checks (tsan) (push) Successful in 31m40s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (postgres) (push) Canceled after 45m38s
Foundation / checks (checks) (push) Canceled after 45m43s
feat(biology): fund offspring from a distinct inherited genome
2026-10-19 13:09:52 -04:00

210 lines
11 KiB
C++

#include "evo/biology/budding.hpp"
#include "evo/biology/founder.hpp"
#include "evo/biology/lineage.hpp"
#include <algorithm>
#include <iostream>
#include <limits>
#include <optional>
#include <source_location>
#include <stdexcept>
#include <string>
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("offspring line " + std::to_string(location.line()));
}
#define CHECK(expression) check(static_cast<bool>(expression))
template <typename T, typename E> T checked(std::expected<T, E> result) {
CHECK(result);
return *result;
}
bool refuses(const std::expected<BudTransition, BuddingError>& result, BuddingError error) {
return !result && result.error() == error;
}
constexpr std::uint32_t temperate = 293'150;
Inventory plenty() {
constexpr std::uint64_t lots = 1'000'000'000'000;
return {{lots}, {Femtograms{lots}, Femtograms{lots}, Femtograms{lots}, Femtograms{lots}}};
}
GenomeDraft draft(const Genome& parent, std::uint64_t id) {
return {id,
id,
parent.development_version(),
{parent.id()},
{parent.capabilities().begin(), parent.capabilities().end()},
parent.schema_version()};
}
Genome variant(const Genome& parent, std::uint64_t id, Capability target,
std::optional<std::uint32_t> parameter) {
auto change = draft(parent, id);
if (!parameter)
std::erase_if(change.capabilities, [target](auto record) { return record.kind == target; });
else {
const auto found = std::ranges::find(change.capabilities, target, &CapabilityRecord::kind);
CHECK(found != change.capabilities.end());
found->parameter = *parameter;
}
return checked(Genome::create(change));
}
void conservation(const Inventory& before, const BudTransition& result) {
CHECK(result.body.energy.value == 0);
CHECK(before.energy.value ==
result.parent_reserve.energy.value + result.reserve.energy.value + result.heat.value);
for (std::size_t channel = 0; channel < before.matter.size(); ++channel)
CHECK(before.matter[channel].value == result.parent_reserve.matter[channel].value +
result.body.matter[channel].value +
result.reserve.matter[channel].value);
}
void verify_pair(const Genome& parent, const Genome& child, std::uint32_t share) {
const auto parent_bytes = parent.encode();
const auto child_bytes = child.encode();
const auto reserve = plenty();
const auto result =
checked(fund_offspring(parent, child, {1000, 0}, reserve, temperate, share));
conservation(reserve, result);
const auto investment =
checked(metabolic_budget(parent, {1000, 0}, {temperate, {}})).reproduction_investment;
const auto developed = checked(metabolic_budget(child, result.growth, {temperate, {}}));
CHECK(result.body == developed.body);
CHECK(result.heat.value == investment.energy.value / share);
CHECK(result.reserve.energy.value ==
developed.demand.value * bud_provision_seconds * 1'000'000 / developed.rate_microseconds);
for (std::size_t channel = 0; channel < reserve.matter.size(); ++channel)
CHECK(result.body.matter[channel].value + result.reserve.matter[channel].value ==
investment.matter[channel].value / share);
if (result.growth.realized_per_thousand < 1000) {
const auto larger = checked(
metabolic_budget(child, {result.growth.realized_per_thousand + 1, 0}, {temperate, {}}));
bool does_not_fit = false;
for (std::size_t channel = 0; channel < reserve.matter.size(); ++channel)
does_not_fit |=
larger.body.matter[channel].value > investment.matter[channel].value / share;
CHECK(does_not_fit);
}
const auto cost = offspring_cost(parent, child, temperate, share);
if (!cost)
throw std::runtime_error("offspring has no exact cost");
const auto exact = checked(fund_offspring(parent, child, {1000, 0}, *cost, temperate, share));
conservation(*cost, exact);
CHECK(exact.parent_reserve == Inventory{} && exact.body == result.body &&
exact.growth == result.growth && exact.reserve == result.reserve);
for (std::size_t channel = 0; channel < 5; ++channel) {
auto short_one = *cost;
auto& value = channel == 0 ? short_one.energy.value : short_one.matter[channel - 1].value;
if (value == 0)
continue;
--value;
if (channel != 0)
short_one.energy.value = std::numeric_limits<std::uint64_t>::max();
const auto before = short_one;
const auto refused = fund_offspring(parent, child, {1000, 0}, short_one, temperate, share);
CHECK(!refused && refused.error() == BuddingError::insufficient_funding);
CHECK(short_one == before);
}
CHECK(parent.encode() == parent_bytes && child.encode() == child_bytes);
CHECK(fund_offspring(parent, child, {1000, 0}, reserve, temperate, share) == result);
}
void distinct_anatomy_and_demand() {
const auto parent = checked(primitive_founder(5, 5, primitive_founder_v3));
const auto thicker = variant(parent, 6, Capability::membrane, 2);
const auto inherited = checked(fund_offspring(parent, thicker, {1000, 0}, plenty(), temperate));
const auto copied = checked(fund_bud(parent, {1000, 0}, plenty(), temperate));
CHECK(inherited.body != copied.body);
CHECK(inherited.growth != copied.growth);
for (const auto share : {1U, 2U}) {
verify_pair(parent, thicker, share);
verify_pair(parent, variant(parent, 7, Capability::target_size, 80'000'000), share);
verify_pair(parent, variant(parent, 8, Capability::movement, 40'000), share);
verify_pair(parent, variant(parent, 9, Capability::reproduction, std::nullopt), share);
}
// Two share-2 offspring spend at most the same parent matter investment.
const auto first = checked(fund_offspring(parent, thicker, {1000, 0}, plenty(), temperate, 2));
const auto second =
checked(fund_offspring(parent, thicker, {1000, 0}, first.parent_reserve, temperate, 2));
conservation(first.parent_reserve, second);
CHECK(first.body == second.body && first.growth == second.growth);
const auto investment =
checked(metabolic_budget(parent, {1000, 0}, {temperate, {}})).reproduction_investment;
for (std::size_t channel = 0; channel < 4; ++channel)
CHECK(plenty().matter[channel].value - second.parent_reserve.matter[channel].value <=
investment.matter[channel].value);
}
void compatibility_and_parent_eligibility() {
const auto parent = checked(primitive_founder(5, 5, primitive_founder_v3));
const auto child = variant(parent, 6, Capability::membrane, 2);
const auto sterile = variant(parent, 7, Capability::reproduction, std::nullopt);
CHECK(refuses(fund_offspring(sterile, child, {1000, 0}, plenty(), temperate),
BuddingError::no_capability));
CHECK(refuses(fund_offspring(parent, child, {999, 0}, plenty(), temperate),
BuddingError::not_grown));
CHECK(refuses(fund_offspring(parent, child, {1000, 1}, plenty(), temperate),
BuddingError::not_grown));
CHECK(refuses(fund_offspring(parent, child, {1000, 0}, plenty(), temperate, 0),
BuddingError::no_capability));
CHECK(!offspring_cost(sterile, child, temperate));
CHECK(!offspring_cost(parent, child, temperate, 0));
auto incompatible = draft(child, 8);
incompatible.schema_version = genome_schema_v3;
auto refused = fund_offspring(parent, checked(Genome::create(incompatible)), {1000, 0},
plenty(), temperate);
CHECK(!refused && refused.error() == BuddingError::incompatible_genome);
incompatible = draft(child, 9);
incompatible.development_version = 3;
CHECK(refuses(fund_offspring(parent, checked(Genome::create(incompatible)), {1000, 0}, plenty(),
temperate),
BuddingError::incompatible_genome));
auto alias = draft(child, parent.id());
alias.parents.clear();
CHECK(refuses(
fund_offspring(parent, checked(Genome::create(alias)), {1000, 0}, plenty(), temperate),
BuddingError::incompatible_genome));
const auto huge = variant(parent, 10, Capability::target_size, 1'000'000'000);
const auto unbuildable = variant(huge, 11, Capability::membrane, 10'000);
refused = fund_offspring(parent, unbuildable, {1000, 0}, plenty(), temperate);
CHECK(!refused && refused.error() == BuddingError::insufficient_funding);
CHECK(!offspring_cost(parent, unbuildable, temperate));
}
void old_founders_and_archive_identity() {
for (const auto version : {primitive_founder_v1, primitive_founder_v2, primitive_founder_v3}) {
const auto parent = checked(primitive_founder(5, 5, version));
for (const auto share : {1U, 2U})
CHECK(fund_bud(parent, {1000, 0}, plenty(), temperate, share) ==
fund_offspring(parent, parent, {1000, 0}, plenty(), temperate, share));
CHECK(bud_cost(parent, temperate) == offspring_cost(parent, parent, temperate));
}
const auto parent = checked(primitive_founder(5, 5, primitive_founder_v3));
const auto plan = variant(parent, 6, Capability::membrane, 2);
const auto restored_plan = checked(Genome::decode(plan.encode()));
const auto first = checked(fund_offspring(parent, plan, {1000, 0}, plenty(), temperate));
const auto sibling =
checked(fund_offspring(parent, restored_plan, {1000, 0}, first.parent_reserve, temperate));
CHECK(first.body == sibling.body && first.growth == sibling.growth);
LineageArchive archive;
CHECK(archive.admit(parent));
CHECK(archive.admit(plan));
CHECK(archive.record_birth(100, parent.id(), {0}));
CHECK(archive.record_birth(101, plan.id(), {1}));
CHECK(archive.record_birth(102, plan.id(), {2}));
const auto restored = checked(LineageArchive::decode(archive.encode()));
CHECK(checked(restored.current_genome(100)) == parent.id());
CHECK(checked(restored.current_genome(101)) == plan.id());
CHECK(checked(restored.current_genome(102)) == plan.id());
CHECK(checked(restored.common_ancestor(parent.id(), plan.id())) == parent.id());
CHECK(checked(restored.living_carriers(parent.id())) == 1);
CHECK(checked(restored.living_carriers(plan.id())) == 2);
}
} // namespace
int main() try {
distinct_anatomy_and_demand();
compatibility_and_parent_eligibility();
old_founders_and_archive_identity();
return 0;
} catch (const std::exception& error) {
std::cerr << error.what() << '\n';
return 1;
}