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
126 lines
6.2 KiB
C++
126 lines
6.2 KiB
C++
#include "evo/biology/budding.hpp"
|
|
|
|
#include <limits>
|
|
|
|
namespace evo::biology {
|
|
namespace {
|
|
constexpr std::uint64_t maximum = std::numeric_limits<std::uint64_t>::max();
|
|
|
|
bool fits(const accounting::Inventory& part, const accounting::Inventory& whole) noexcept {
|
|
for (std::size_t c = 0; c < part.matter.size(); ++c)
|
|
if (part.matter[c].value > whole.matter[c].value)
|
|
return false;
|
|
return part.energy.value <= whole.energy.value;
|
|
}
|
|
} // namespace
|
|
|
|
std::expected<BudTransition, BuddingError> fund_bud(const Genome& genome, GrowthState parent_growth,
|
|
const accounting::Inventory& parent_reserve,
|
|
std::uint32_t temperature_millikelvin,
|
|
std::uint32_t share) {
|
|
return fund_offspring(genome, genome, parent_growth, parent_reserve, temperature_millikelvin,
|
|
share);
|
|
}
|
|
|
|
std::expected<BudTransition, BuddingError>
|
|
fund_offspring(const Genome& genome, const Genome& offspring, GrowthState parent_growth,
|
|
const accounting::Inventory& parent_reserve, std::uint32_t temperature_millikelvin,
|
|
std::uint32_t share) {
|
|
if (genome.schema_version() != offspring.schema_version() ||
|
|
genome.development_version() != offspring.development_version() ||
|
|
(genome.id() == offspring.id() && genome != offspring))
|
|
return std::unexpected{BuddingError::incompatible_genome};
|
|
if (share == 0)
|
|
return std::unexpected{BuddingError::no_capability};
|
|
if (!genome.carries(Capability::reproduction))
|
|
return std::unexpected{BuddingError::no_capability};
|
|
if (parent_growth.realized_per_thousand != 1000 || parent_growth.damage_per_thousand != 0)
|
|
return std::unexpected{BuddingError::not_grown};
|
|
const auto parent = metabolic_budget(genome, parent_growth, {temperature_millikelvin, {}});
|
|
if (!parent)
|
|
return std::unexpected{BuddingError::development};
|
|
// ADR-0137 twins: each of `share` buds is funded from that fraction of the investment
|
|
// (rounded down; the remainder stays with the parent).
|
|
auto investment = parent->reproduction_investment;
|
|
investment.energy.value /= share;
|
|
for (auto& mass : investment.matter)
|
|
mass.value /= share;
|
|
std::uint64_t invested_matter = 0;
|
|
for (const auto& mass : investment.matter)
|
|
invested_matter += mass.value;
|
|
if (invested_matter == 0)
|
|
return std::unexpected{BuddingError::no_capability};
|
|
// The cost is at least the investment, so a reserve short of it refuses before the
|
|
// growth scan (the same answer, without up to a thousand development derivations).
|
|
if (!fits(investment, parent_reserve))
|
|
return std::unexpected{BuddingError::insufficient_funding};
|
|
|
|
// The largest realized growth whose developed body fits the invested matter on every
|
|
// channel. Bodies grow with realized growth, so the scan stops at the first fit.
|
|
for (std::uint32_t realized = 1000; realized > 0; --realized) {
|
|
const GrowthState growth{realized, 0};
|
|
const auto bud = metabolic_budget(offspring, growth, {temperature_millikelvin, {}});
|
|
if (!bud)
|
|
return std::unexpected{BuddingError::development};
|
|
accounting::Inventory body_only = bud->body;
|
|
body_only.energy = {};
|
|
accounting::Inventory matter_only = investment;
|
|
matter_only.energy = {};
|
|
if (!fits(body_only, matter_only))
|
|
continue;
|
|
if (bud->rate_microseconds == 0)
|
|
return std::unexpected{BuddingError::development};
|
|
const auto periods = bud_provision_seconds * 1'000'000 / bud->rate_microseconds;
|
|
if (periods != 0 && bud->demand.value > maximum / periods)
|
|
return std::unexpected{BuddingError::development};
|
|
const auto provision = bud->demand.value * periods;
|
|
if (provision > maximum - investment.energy.value)
|
|
return std::unexpected{BuddingError::insufficient_funding};
|
|
accounting::Inventory cost = investment;
|
|
cost.energy.value = investment.energy.value + provision;
|
|
if (!fits(cost, parent_reserve))
|
|
return std::unexpected{BuddingError::insufficient_funding};
|
|
|
|
BudTransition transition;
|
|
transition.growth = growth;
|
|
transition.body = body_only;
|
|
transition.heat = investment.energy;
|
|
transition.reserve.energy.value = provision;
|
|
transition.parent_reserve = parent_reserve;
|
|
transition.parent_reserve.energy.value -= cost.energy.value;
|
|
for (std::size_t c = 0; c < investment.matter.size(); ++c) {
|
|
transition.reserve.matter[c].value =
|
|
investment.matter[c].value - body_only.matter[c].value;
|
|
transition.parent_reserve.matter[c].value -= investment.matter[c].value;
|
|
}
|
|
return transition;
|
|
}
|
|
return std::unexpected{BuddingError::insufficient_funding}; // Too little to build a cell.
|
|
}
|
|
|
|
std::optional<accounting::Inventory> bud_cost(const Genome& genome,
|
|
std::uint32_t temperature_millikelvin) {
|
|
return offspring_cost(genome, genome, temperature_millikelvin);
|
|
}
|
|
|
|
std::optional<accounting::Inventory> offspring_cost(const Genome& parent, const Genome& offspring,
|
|
std::uint32_t temperature_millikelvin,
|
|
std::uint32_t share) {
|
|
// Fund against an ample reserve and read the charge: one code path decides both.
|
|
constexpr std::uint64_t ample = std::uint64_t{1} << 60U;
|
|
const accounting::Inventory unlimited{
|
|
{ample},
|
|
{accounting::Femtograms{ample}, accounting::Femtograms{ample},
|
|
accounting::Femtograms{ample}, accounting::Femtograms{ample}}};
|
|
const auto funded =
|
|
fund_offspring(parent, offspring, {1000, 0}, unlimited, temperature_millikelvin, share);
|
|
if (!funded)
|
|
return std::nullopt;
|
|
accounting::Inventory cost;
|
|
cost.energy.value = ample - funded->parent_reserve.energy.value;
|
|
for (std::size_t channel = 0; channel < cost.matter.size(); ++channel)
|
|
cost.matter[channel].value = ample - funded->parent_reserve.matter[channel].value;
|
|
return cost;
|
|
}
|
|
} // namespace evo::biology
|