A construction transition could require more of an element than was available, because the check compared net mass. A step growing a body from 13 to 14 femtograms nets one femtogram but acquires a carbon and a nitrogen while freeing one of other, and released other pays for neither. Matching totals let one element silently fund another. Net mass was never the right question, and the layer asking it could not ask a better one: plan_development has no composition. A single scalar pool could not carry the answer either, since it cannot say that carbon is plentiful while nitrogen is not. The environment now states what is available per ledger channel. plan_development keeps a total-mass bound, which is sound because net never exceeds gross, but it is a proposal rather than an authority. metabolic_budget narrows it to the largest step whose own intake fits channel by channel, and reports the growth that step actually funds. That revises ADR-0031 on this point, recorded in both ADRs. The tests place a single femtogram in each channel in turn and require that no step needing a different element is funded, that nothing is acquired beyond what is present, and that the mass-only plan would have admitted the step; a carbon and a nitrogen together then fund it exactly. Across six target sizes by seven growth fractions by four stock levels, every reported intake fits the availability channel by channel and never exceeds the proposed step. Confirmed to fail with the check removed. The releasing case moved to a target of 20 femtograms, where the step occurs at the full rule step rather than depending on a scarce supply that the corrected plan no longer bounds the same way. Native Release, clang-tidy analysis, ASan and format checks pass 19/19. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
157 lines
6.7 KiB
C++
157 lines
6.7 KiB
C++
#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;
|
|
}
|
|
std::uint64_t matter_total(const accounting::Inventory& inventory) noexcept {
|
|
std::uint64_t sum = 0;
|
|
for (const auto& mass : inventory.matter) {
|
|
sum += mass.value;
|
|
}
|
|
return sum;
|
|
}
|
|
} // 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 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
|