M12.3: persist exact population state and automatic conversion policy
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 3m16s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (client) (push) Canceled after 13m8s
Foundation / checks (checks) (push) Canceled after 13m12s
Foundation / checks (postgres) (push) Canceled after 13m2s
Foundation / checks (network) (push) Canceled after 13m4s
Foundation / checks (tsan) (push) Canceled after 13m9s

This commit is contained in:
dingo
2026-10-17 12:11:10 -04:00
parent 2b074b9ae1
commit 5decaa0326
14 changed files with 624 additions and 15 deletions
+30 -7
View File
@@ -3,17 +3,23 @@
#include "evo/server/lifecycle/wildlife_controller.hpp"
#include <algorithm>
#include <limits>
#include <set>
#include <utility>
namespace evo::server {
CellularPopulation::CellularPopulation(std::unique_ptr<CellularLives> state)
: state_(std::move(state)) {}
CellularPopulation::CellularPopulation(std::unique_ptr<CellularLives> state,
CellularTierPolicy policy)
: state_(std::move(state)), policy_(policy) {}
std::expected<std::unique_ptr<CellularPopulation>, CellularPopulationError>
CellularPopulation::create(std::unique_ptr<CellularLives> state) {
if (!state || state->rules_version() != cellular_lives_foraging_rules)
CellularPopulation::create(std::unique_ptr<CellularLives> state, CellularTierPolicy policy) {
if (!state || state->rules_version() != cellular_lives_foraging_rules ||
policy.materialize_radius_um == 0 ||
policy.collapse_radius_um <= policy.materialize_radius_um ||
policy.collapse_radius_um > biology::cellular_motion_axis_limit_um)
return std::unexpected(CellularPopulationError::unsupported);
return std::unique_ptr<CellularPopulation>(new CellularPopulation(std::move(state)));
return std::unique_ptr<CellularPopulation>(new CellularPopulation(std::move(state), policy));
}
const CellularLives& CellularPopulation::state() const noexcept {
return *state_;
@@ -24,6 +30,12 @@ std::span<const CellularCohortMember> CellularPopulation::members() const noexce
bool CellularPopulation::in_cohort(std::uint64_t life) const noexcept {
return std::ranges::binary_search(members_, life, {}, &CellularCohortMember::life);
}
CellularTierPolicy CellularPopulation::policy() const noexcept {
return policy_;
}
std::uint64_t CellularPopulation::conversion_sequence() const noexcept {
return conversion_sequence_;
}
std::expected<std::size_t, CellularPopulationError>
CellularPopulation::collapse(std::uint64_t genome) {
auto candidate = members_;
@@ -42,21 +54,30 @@ CellularPopulation::collapse(std::uint64_t genome) {
}
if (!found)
return std::unexpected(CellularPopulationError::absent);
if (converted > 0 && conversion_sequence_ == std::numeric_limits<std::uint64_t>::max())
return std::unexpected(CellularPopulationError::overflow);
std::ranges::sort(candidate);
members_.swap(candidate);
if (converted > 0)
++conversion_sequence_;
return converted;
}
std::expected<std::size_t, CellularPopulationError>
CellularPopulation::materialize(std::uint64_t genome) {
if (std::ranges::none_of(members_, [genome](const auto& m) { return m.genome == genome; }))
return std::unexpected(CellularPopulationError::absent);
if (conversion_sequence_ == std::numeric_limits<std::uint64_t>::max())
return std::unexpected(CellularPopulationError::overflow);
const auto removed =
std::erase_if(members_, [genome](const auto& m) { return m.genome == genome; });
if (removed == 0)
return std::unexpected(CellularPopulationError::absent);
++conversion_sequence_;
return removed;
}
std::expected<CellularTickExecution, CellularLivesError>
CellularPopulation::advance_tick(std::span<const CellularLifeIntent> controlled,
std::span<const CellularMutationSelection> selections) {
if (policy_.automatic && conversion_sequence_ == std::numeric_limits<std::uint64_t>::max())
return std::unexpected(CellularLivesError::overflow);
auto wildlife = wildlife_intents(*state_, state_->tick() + 1);
if (!wildlife)
return std::unexpected(wildlife.error());
@@ -87,6 +108,8 @@ CellularPopulation::advance_tick(std::span<const CellularLifeIntent> controlled,
return life.identity.life;
}) == state_->lives().end();
});
if (policy_.automatic)
apply_policy();
return executed;
}
} // namespace evo::server