Decide affordability where composition is known
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>
This commit is contained in:
@@ -39,6 +39,13 @@ 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) {
|
||||
@@ -120,8 +127,9 @@ plan_development(const Genome& genome, GrowthState growth, EnvironmentState envi
|
||||
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.
|
||||
// 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;
|
||||
@@ -133,12 +141,14 @@ plan_development(const Genome& genome, GrowthState growth, EnvironmentState envi
|
||||
return std::unexpected{DevelopmentError::overflow};
|
||||
}
|
||||
const auto material = grown->total_mass - current->total_mass;
|
||||
if (material > environment.available_material) {
|
||||
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 = scaled(material, rules->construction_per_kilogram);
|
||||
plan.energy_required = energy;
|
||||
break;
|
||||
}
|
||||
return plan;
|
||||
|
||||
+55
-22
@@ -63,6 +63,19 @@ bool combine(accounting::Inventory& total, const accounting::Inventory& amount)
|
||||
}
|
||||
return true;
|
||||
}
|
||||
// Every channel and the energy must fit: a surplus of one element never pays for
|
||||
// a shortfall of another.
|
||||
bool fits(const accounting::Inventory& needed, const accounting::Inventory& available) noexcept {
|
||||
if (needed.energy > available.energy) {
|
||||
return false;
|
||||
}
|
||||
for (std::size_t index = 0; index < needed.matter.size(); ++index) {
|
||||
if (needed.matter[index] > available.matter[index]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
std::uint64_t matter_total(const accounting::Inventory& inventory) noexcept {
|
||||
std::uint64_t sum = 0;
|
||||
for (const auto& mass : inventory.matter) {
|
||||
@@ -136,30 +149,50 @@ metabolic_budget(const Genome& genome, GrowthState growth, EnvironmentState envi
|
||||
if (!plan) {
|
||||
return std::unexpected{plan.error()};
|
||||
}
|
||||
const auto grown = derive_phenotype(genome, plan->after, environment);
|
||||
if (!grown) {
|
||||
return std::unexpected{grown.error()};
|
||||
}
|
||||
// Compose the grown body and difference it against the current one. Composing
|
||||
// the mass difference instead would discard the rounding this body already
|
||||
// carries, and the budget could not then reproduce the derived body channel by
|
||||
// channel. Each compartment is still composed separately: the two differ in
|
||||
// what they cost.
|
||||
auto reached = compose(grown->structural_mass, rules->structure);
|
||||
if (!combine(reached, compose(grown->membrane_mass, rules->membrane))) {
|
||||
return std::unexpected{DevelopmentError::overflow};
|
||||
}
|
||||
for (std::size_t index = 0; index < reached.matter.size(); ++index) {
|
||||
const auto held = budget.body.matter[index].value;
|
||||
const auto wanted = reached.matter[index].value;
|
||||
if (wanted >= held) {
|
||||
budget.construction.intake.matter[index].value = wanted - held;
|
||||
} else {
|
||||
budget.construction.release.matter[index].value = held - wanted;
|
||||
// A plan bounds by total mass, which cannot tell whether the elements a step
|
||||
// consumes are actually there: released matter never substitutes for an
|
||||
// element the step needs. Narrow the proposal to the largest step whose own
|
||||
// intake fits channel by channel.
|
||||
budget.construction.after = growth;
|
||||
const auto proposed = plan->after.realized_per_thousand - growth.realized_per_thousand;
|
||||
for (std::uint32_t attempt = proposed; attempt > 0; --attempt) {
|
||||
const GrowthState candidate{growth.realized_per_thousand + attempt,
|
||||
growth.damage_per_thousand};
|
||||
const auto grown = derive_phenotype(genome, candidate, environment);
|
||||
if (!grown) {
|
||||
return std::unexpected{grown.error()};
|
||||
}
|
||||
// Compose the grown body and difference it against the current one.
|
||||
// Composing the mass difference instead would discard the rounding this
|
||||
// body already carries, and the budget could not then reproduce the
|
||||
// derived body channel by channel. Each compartment is still composed
|
||||
// separately: the two differ in what they cost.
|
||||
auto reached = compose(grown->structural_mass, rules->structure);
|
||||
if (!combine(reached, compose(grown->membrane_mass, rules->membrane))) {
|
||||
return std::unexpected{DevelopmentError::overflow};
|
||||
}
|
||||
ConstructionTransition move{};
|
||||
move.after = candidate;
|
||||
std::uint64_t acquired = 0, freed = 0;
|
||||
for (std::size_t index = 0; index < reached.matter.size(); ++index) {
|
||||
const auto held = budget.body.matter[index].value;
|
||||
const auto wanted = reached.matter[index].value;
|
||||
if (wanted >= held) {
|
||||
move.intake.matter[index].value = wanted - held;
|
||||
acquired += wanted - held;
|
||||
} else {
|
||||
move.release.matter[index].value = held - wanted;
|
||||
freed += held - wanted;
|
||||
}
|
||||
}
|
||||
// Energy is priced on the net mass change, as the plan prices it.
|
||||
move.intake.energy.value = scaled(acquired - freed, shape->construction_per_kilogram);
|
||||
if (!fits(move.intake, environment.available)) {
|
||||
continue;
|
||||
}
|
||||
budget.construction = move;
|
||||
break;
|
||||
}
|
||||
// Energy is priced on the net mass change, matching what the plan reports.
|
||||
budget.construction.intake.energy.value = plan->energy_required;
|
||||
if (genome.carries(Capability::reproduction)) {
|
||||
const auto fraction = *genome.parameter(Capability::reproduction);
|
||||
// A parent divides the body it has, so the investment keeps its composition.
|
||||
|
||||
Reference in New Issue
Block a user