Derive a phenotype from the genome without touching it
M3.2 adds derive_phenotype and plan_development as pure integer functions of a genome, a growth state and an environment. Nothing is stored: a phenotype is recomputed from retained inputs rather than cached, so a cached copy can always be checked against a recomputation. The risk this increment was scoped against is confusing the genome with runtime damage. Growth and damage live in a separate state that no function may write back, and no entry point takes a mutable genome. Damage changes the phenotype and leaves the encoded bytes and content digest identical, so heredity cannot carry acquired condition. The genome's development version selects a rule set, which is what makes that field worth carrying; an unknown version fails closed rather than deriving under the wrong rules. Version 1 declares every constant. The shape model is explicit rather than implied: the body is a cube of uniform declared density, so extent is an exact integer cube root and surface is six extents squared. Membrane mass follows surface and the genome's thickness, maintenance follows total mass plus a membrane upkeep term, and uptake scales with surface, so size trades uptake against maintenance. A capability the genome does not carry yields zero. A plan advances growth by at most the rule step, reduced to the largest step the available material funds exactly, never a partial one and never a grant. It reserves nothing and pays for nothing: the matching transfer does not exist until M3.3, and an apply that moved no inventory would look like a committed operation while conserving nothing. The fixture genome is an exact 20 micrometre cube, so every derived value is asserted as a hand-calculated literal, as are the half-built, damaged, maximum and cube-root-boundary cases. Determinism is checked over 45 growth and temperature combinations. The declared flooring is pinned rather than hidden: a body whose mass floors to zero advances its counter for no material, granting no structure because none exists either way. Omitted physics is a documented model limitation, not a modifier. Nothing here is balance, and no organism yet develops. Native Release, clang-tidy analysis, ASan and format checks pass 18/18; every existing golden and fingerprint is unchanged. Linux CI has not run yet. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,146 @@
|
||||
#include "evo/biology/development.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <limits>
|
||||
|
||||
namespace evo::biology {
|
||||
namespace {
|
||||
constexpr std::uint32_t thousand = 1000;
|
||||
|
||||
bool multiply(std::uint64_t& result, std::uint64_t left, std::uint64_t right) noexcept {
|
||||
if (left != 0 && right > std::numeric_limits<std::uint64_t>::max() / left) {
|
||||
return false;
|
||||
}
|
||||
result = left * right;
|
||||
return true;
|
||||
}
|
||||
bool add(std::uint64_t& result, std::uint64_t left, std::uint64_t right) noexcept {
|
||||
if (right > std::numeric_limits<std::uint64_t>::max() - left) {
|
||||
return false;
|
||||
}
|
||||
result = left + right;
|
||||
return true;
|
||||
}
|
||||
// Exact: the largest r with r*r*r <= value. No floating point is involved.
|
||||
std::uint64_t integer_cube_root(std::uint64_t value) noexcept {
|
||||
std::uint64_t root = 0;
|
||||
// 2^21 cubed already exceeds u64, so 21 bits are enough.
|
||||
for (std::uint64_t bit = 1ULL << 21U; bit != 0; bit >>= 1U) {
|
||||
const auto candidate = root | bit;
|
||||
std::uint64_t cube{};
|
||||
if (multiply(cube, candidate, candidate) && multiply(cube, cube, candidate) &&
|
||||
cube <= value) {
|
||||
root = candidate;
|
||||
}
|
||||
}
|
||||
return root;
|
||||
}
|
||||
std::uint64_t scaled(std::uint64_t value, std::uint64_t numerator) noexcept {
|
||||
// Declared flooring; the remainder is discarded rather than carried.
|
||||
return value / thousand * numerator + value % thousand * numerator / thousand;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
std::expected<DevelopmentRules, DevelopmentError> rules_for(std::uint32_t development_version) {
|
||||
if (development_version != 1) {
|
||||
return std::unexpected{DevelopmentError::unsupported_rules};
|
||||
}
|
||||
return DevelopmentRules{1000, 1200, 2, 3, 40, 60'000, 293'150, 50};
|
||||
}
|
||||
|
||||
std::expected<Phenotype, DevelopmentError>
|
||||
derive_phenotype(const Genome& genome, GrowthState growth, EnvironmentState environment) {
|
||||
if (growth.realized_per_thousand > thousand || growth.damage_per_thousand > thousand) {
|
||||
return std::unexpected{DevelopmentError::invalid_growth};
|
||||
}
|
||||
const auto rules = rules_for(genome.development_version());
|
||||
if (!rules) {
|
||||
return std::unexpected{rules.error()};
|
||||
}
|
||||
Phenotype phenotype;
|
||||
// target_size, membrane and energy_conversion are required by schema 1.
|
||||
const auto target = *genome.parameter(Capability::target_size);
|
||||
const auto built = scaled(target, growth.realized_per_thousand);
|
||||
phenotype.structural_mass = scaled(built, thousand - growth.damage_per_thousand);
|
||||
phenotype.extent = static_cast<std::uint32_t>(
|
||||
integer_cube_root(phenotype.structural_mass / rules->structure_density));
|
||||
std::uint64_t face{};
|
||||
if (!multiply(face, phenotype.extent, phenotype.extent) ||
|
||||
!multiply(phenotype.surface, face, 6)) {
|
||||
return std::unexpected{DevelopmentError::overflow};
|
||||
}
|
||||
std::uint64_t shell{};
|
||||
if (!multiply(shell, phenotype.surface, *genome.parameter(Capability::membrane)) ||
|
||||
!multiply(phenotype.membrane_mass, shell, rules->membrane_density)) {
|
||||
return std::unexpected{DevelopmentError::overflow};
|
||||
}
|
||||
if (!add(phenotype.total_mass, phenotype.structural_mass, phenotype.membrane_mass)) {
|
||||
return std::unexpected{DevelopmentError::overflow};
|
||||
}
|
||||
// A thicker membrane is held as well as built, so upkeep is charged twice over.
|
||||
if (!add(phenotype.maintenance, scaled(phenotype.total_mass, rules->maintenance_per_kilogram),
|
||||
scaled(phenotype.membrane_mass, rules->membrane_upkeep_per_kilo))) {
|
||||
return std::unexpected{DevelopmentError::overflow};
|
||||
}
|
||||
phenotype.conversion_per_thousand =
|
||||
static_cast<std::uint32_t>(*genome.parameter(Capability::energy_conversion));
|
||||
// A capability the genome does not carry yields nothing, whatever the size.
|
||||
if (genome.carries(Capability::movement)) {
|
||||
phenotype.movement_speed =
|
||||
static_cast<std::uint32_t>(*genome.parameter(Capability::movement));
|
||||
}
|
||||
if (genome.carries(Capability::ingestion)) {
|
||||
std::uint64_t uptake{};
|
||||
if (!multiply(uptake, *genome.parameter(Capability::ingestion), phenotype.surface)) {
|
||||
return std::unexpected{DevelopmentError::overflow};
|
||||
}
|
||||
phenotype.ingestion_rate = uptake / rules->reference_surface;
|
||||
}
|
||||
const auto tolerance = genome.carries(Capability::thermal_tolerance)
|
||||
? *genome.parameter(Capability::thermal_tolerance)
|
||||
: 0;
|
||||
const auto deviation = environment.temperature_millikelvin > rules->reference_temperature
|
||||
? environment.temperature_millikelvin - rules->reference_temperature
|
||||
: rules->reference_temperature - environment.temperature_millikelvin;
|
||||
phenotype.thermal_shortfall = deviation > tolerance ? deviation - tolerance : 0;
|
||||
return phenotype;
|
||||
}
|
||||
|
||||
std::expected<DevelopmentPlan, DevelopmentError>
|
||||
plan_development(const Genome& genome, GrowthState growth, EnvironmentState environment) {
|
||||
const auto current = derive_phenotype(genome, growth, environment);
|
||||
if (!current) {
|
||||
return std::unexpected{current.error()};
|
||||
}
|
||||
const auto rules = rules_for(genome.development_version());
|
||||
if (!rules) {
|
||||
return std::unexpected{rules.error()};
|
||||
}
|
||||
DevelopmentPlan plan;
|
||||
plan.after = growth;
|
||||
const auto remaining = thousand - growth.realized_per_thousand;
|
||||
const auto step = std::min(remaining, rules->growth_step_per_thousand);
|
||||
// Take the largest step the available material funds exactly, never a
|
||||
// partially funded one and never a grant.
|
||||
for (std::uint32_t attempt = step; attempt > 0; --attempt) {
|
||||
auto proposed = growth;
|
||||
proposed.realized_per_thousand += attempt;
|
||||
const auto grown = derive_phenotype(genome, proposed, environment);
|
||||
if (!grown) {
|
||||
return std::unexpected{grown.error()};
|
||||
}
|
||||
if (grown->total_mass < current->total_mass) {
|
||||
return std::unexpected{DevelopmentError::overflow};
|
||||
}
|
||||
const auto material = grown->total_mass - current->total_mass;
|
||||
if (material > environment.available_material) {
|
||||
continue;
|
||||
}
|
||||
plan.after = proposed;
|
||||
plan.material_required = material;
|
||||
plan.energy_required = scaled(material, rules->construction_per_kilogram);
|
||||
break;
|
||||
}
|
||||
return plan;
|
||||
}
|
||||
} // namespace evo::biology
|
||||
Reference in New Issue
Block a user