Foundation / checks (deploy) (push) Successful in 3m28s
Foundation / checks (ecs) (push) Successful in 1m55s
Foundation / checks (client) (push) Successful in 12m43s
Foundation / checks (network) (push) Successful in 13m32s
Foundation / checks (tsan) (push) Successful in 7m48s
Foundation / checks (checks) (push) Successful in 32m55s
Foundation / checks (postgres) (push) Successful in 46m26s
Foundation / postgres-tools (push) Successful in 1m41s
Foundation / server-image (push) Successful in 4m13s
Foundation / image (push) Successful in 2m35s
CI run 136 failed network: Phenotype::rate_microseconds (added with development version 2) is not carried by either session protocol, so the organism view no longer re-encoded canonically. The phenotype keeps its replicated shape; the period follows from the development version (development_rate_microseconds) and MetabolicBudget carries it. No protocol change; first-bud times unchanged. Refresh the dev session window. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
180 lines
8.1 KiB
C++
180 lines
8.1 KiB
C++
#include "evo/biology/development.hpp"
|
|
|
|
#include "evo/accounting/rates.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;
|
|
}
|
|
// Saturating on purpose. Availability is caller-supplied and unrestricted, and a
|
|
// wrapped total would read as no material at all, refusing growth that is plainly
|
|
// funded. Saturation is also the right answer: no step can need more than u64.
|
|
std::uint64_t matter_total(const accounting::Inventory& inventory) noexcept {
|
|
constexpr auto maximum = std::numeric_limits<std::uint64_t>::max();
|
|
std::uint64_t sum = 0;
|
|
for (const auto& mass : inventory.matter) {
|
|
sum = mass.value > maximum - sum ? maximum : sum + mass.value;
|
|
}
|
|
return sum;
|
|
}
|
|
} // namespace
|
|
|
|
std::expected<DevelopmentRules, DevelopmentError> rules_for(std::uint32_t development_version) {
|
|
if (development_version == 1) {
|
|
return DevelopmentRules{1000, 1200, 2, 3, 40, 60'000, 293'150, 50};
|
|
}
|
|
// Version 2 (ADR-0099; tunable, ADR-0098 decision 7): rates per second. Maintenance
|
|
// and membrane upkeep are a quarter of version 1's per-second equivalent (40, 60), so
|
|
// a primitive cell can fund its first bud within a session of feeding.
|
|
if (development_version == 2) {
|
|
return DevelopmentRules{1000, 1200, 10, 15, 40, 60'000, 293'150, 50};
|
|
}
|
|
return std::unexpected{DevelopmentError::unsupported_rules};
|
|
}
|
|
|
|
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.
|
|
// Version 1 is defined in 50 ms ticks: genome rates (per second) enter it at that
|
|
// quantum, rounded down as capacities (ADR-0099; exact for schema 1 records).
|
|
// Version 2 is defined per second and reads them as they are.
|
|
const auto period = development_rate_microseconds(genome.development_version());
|
|
const auto rate_per_tick = [period](std::uint64_t per_second) {
|
|
return accounting::per_tick(per_second, period, accounting::Rounding::down);
|
|
};
|
|
if (genome.carries(Capability::movement)) {
|
|
const auto speed = rate_per_tick(*genome.per_second(Capability::movement));
|
|
if (!speed || *speed > std::numeric_limits<std::uint32_t>::max()) {
|
|
return std::unexpected{DevelopmentError::overflow};
|
|
}
|
|
phenotype.movement_speed = static_cast<std::uint32_t>(*speed);
|
|
}
|
|
if (genome.carries(Capability::ingestion)) {
|
|
const auto intake = rate_per_tick(*genome.per_second(Capability::ingestion));
|
|
std::uint64_t uptake{};
|
|
if (!intake || !multiply(uptake, *intake, 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 mass and energy admit, never a
|
|
// partially funded one and never a grant. This bounds by total mass only;
|
|
// which elements the step consumes is settled by the composition layer.
|
|
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;
|
|
const auto energy = scaled(material, rules->construction_per_kilogram);
|
|
if (material > matter_total(environment.available) ||
|
|
energy > environment.available.energy.value) {
|
|
continue;
|
|
}
|
|
plan.after = proposed;
|
|
plan.material_required = material;
|
|
plan.energy_required = energy;
|
|
break;
|
|
}
|
|
return plan;
|
|
}
|
|
} // namespace evo::biology
|