feat(biology): rules 13 mutation catalog, behaviour triggers and offer draw
Foundation / checks (checks) (push) Successful in 5s
Foundation / checks (client) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 5s
Foundation / checks (network) (push) Successful in 4s
Foundation / checks (postgres) (push) Successful in 4s
Foundation / checks (tsan) (push) Successful in 4s
Foundation / checks (deploy) (push) Successful in 4m30s
Foundation / postgres-tools (push) Successful in 22s
Foundation / server-image (push) Successful in 4m10s
Foundation / image (push) Successful in 4m46s
Foundation / checks (checks) (push) Successful in 5s
Foundation / checks (client) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 5s
Foundation / checks (network) (push) Successful in 4s
Foundation / checks (postgres) (push) Successful in 4s
Foundation / checks (tsan) (push) Successful in 4s
Foundation / checks (deploy) (push) Successful in 4m30s
Foundation / postgres-tools (push) Successful in 22s
Foundation / server-image (push) Successful in 4m10s
Foundation / image (push) Successful in 4m46s
Phase 3 of the cellular revamp build plan (ADR-0179), first part: the headless pieces the owner will use. - The named catalog: all 49 mutations of Cellular_mutation_plan.md as data (stable ids and tuning keys, name, pro, con, effect, availability, increase and decrease triggers, base weight). Applying one edits a schema-7 plan by fixed reference points (a point is a hundredth of the founder's value), with paired edits, spreads across a channel group, recipe shifts, photosynthesis acquire and shed, and Adhesion; bounds, gates (carbon burned, buds made) and the enabled switch close it. 43 mutations are open at birth, as planned. - Triggers: per-tick activity signals and eight trigger kinds (accumulate, absence, count in a window, event, share over a window, regular recurrence, straight heading, sharp turns) move each mutation's affinity score within bounds; integer state only, so replay reproduces scores. - Offers: weight = base x a score multiplier; chances clamp(k x weight, floor, cap) summing to the options, in parts per million (floor 0.001%, cap 70%), found exactly by water-filling over sorted weights; a systematic draw picks distinct mutations whose inclusion frequencies equal their chances exactly. Tests cover the catalog's identities, the pool at birth, every kind of edit, bounds and gates; every trigger kind; score bounds; weights; a 300-seed chance ensemble; and an exhaustive draw over all million draws. Refreshes the DEV session window. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,190 @@
|
||||
#pragma once
|
||||
|
||||
#include "evo/accounting/ledger.hpp"
|
||||
#include "evo/biology/genome.hpp"
|
||||
|
||||
#include <array>
|
||||
#include <compare>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <optional>
|
||||
#include <span>
|
||||
#include <string_view>
|
||||
|
||||
// The named mutation catalog of rules 13 (ADR-0179; Cellular_mutation_plan.md sections 5-9).
|
||||
// Each entry edits a genome's plan within bounds; what triggers raise or lower its chance and
|
||||
// how offers are drawn live in cellular_triggers.hpp and cellular_offers.hpp. IDs are stable.
|
||||
namespace evo::biology {
|
||||
// What a mutation edits. Point-based traits move in fixed reference points: one point is a
|
||||
// hundredth of the founder's value, whatever the trait's unit.
|
||||
enum class CellularTrait : std::uint8_t {
|
||||
none,
|
||||
move_output,
|
||||
move_efficiency,
|
||||
move_steering,
|
||||
move_streamlining,
|
||||
move_braking,
|
||||
move_coasting,
|
||||
collect_energy,
|
||||
collect_carbon,
|
||||
collect_nitrogen,
|
||||
collect_phosphorus,
|
||||
collect_other,
|
||||
intake, // Ingestion capability, femtograms per second.
|
||||
energy_kept, // Energy conversion capability, per thousand.
|
||||
burn_carbon,
|
||||
burn_nitrogen,
|
||||
burn_phosphorus,
|
||||
burn_other,
|
||||
light, // Photosynthesis capability, per thousand.
|
||||
membrane, // Nanometres.
|
||||
engulf_capacity,
|
||||
kill_absorption,
|
||||
size, // Target size, femtograms.
|
||||
recipe_carbon,
|
||||
recipe_nitrogen,
|
||||
recipe_phosphorus,
|
||||
recipe_other,
|
||||
upkeep_efficiency,
|
||||
growth_speed,
|
||||
density,
|
||||
bud_share, // Reproduction capability, per thousand.
|
||||
litter, // Hundredths of an extra offspring; moves in its own units, not points.
|
||||
};
|
||||
|
||||
enum class CellularEffect : std::uint8_t {
|
||||
adjust, // Primary by the primary step, paired by the paired step, spread.
|
||||
recipe, // Recipe: primary loses the recipe step, the other three share it.
|
||||
acquire_light, // Photosynthesis at the founder reference, with the paired edit.
|
||||
shed_light, // Photosynthesis removed.
|
||||
acquire_adhesion, // Adhesion set.
|
||||
};
|
||||
// A spread lowers each other channel of the primary's group by the spread step.
|
||||
enum class CellularSpread : std::uint8_t { none, collect_matter, burn };
|
||||
|
||||
// Extra eligibility beyond staying within bounds.
|
||||
enum class CellularAvailability : std::uint8_t {
|
||||
always,
|
||||
light_absent_after_carbon, // No photosynthesis, and enough carbon burned.
|
||||
light_present,
|
||||
adhesion_absent_after_buds, // No adhesion, and enough on-demand buds.
|
||||
};
|
||||
|
||||
// What a cell does that a trigger counts. Flags are 0 or 1 per tick; amounts are pJ or fg.
|
||||
enum class CellularSignal : std::uint8_t {
|
||||
thrust, // Thrust applied this tick.
|
||||
drifting, // No thrust this tick.
|
||||
brake_press, // Brake pressed this tick and not the last.
|
||||
fed, // Anything collected.
|
||||
intake_limited, // Matter collection held by a limit.
|
||||
energy_limited, // Energy collection held by its limit.
|
||||
energy_collected, // Amount of energy collected.
|
||||
collected_carbon, // Amounts of matter collected, by channel, and in total.
|
||||
collected_nitrogen,
|
||||
collected_phosphorus,
|
||||
collected_other,
|
||||
collected_matter,
|
||||
burned_carbon, // Amounts of matter burned for energy, by channel, and in total.
|
||||
burned_nitrogen,
|
||||
burned_phosphorus,
|
||||
burned_other,
|
||||
burned_matter,
|
||||
burned_not_carbon,
|
||||
energy_upkeep, // Energy spent on upkeep, on movement, and in total.
|
||||
energy_movement,
|
||||
energy_spent,
|
||||
larger_contact, // Touching a cell over 1.5 times this cell's mass.
|
||||
engulf_attempt, // An engulf was asked for.
|
||||
engulf_success,
|
||||
engulf_too_large, // Failed: prey too large.
|
||||
engulf_too_large_or_membrane, // Failed: prey too large or membrane too thick.
|
||||
reserve_over_body, // More matter in the reserve than in the body.
|
||||
regrowing, // Growing toward full size.
|
||||
fully_grown,
|
||||
blocked_carbon, // Growth or a bud held back by this channel.
|
||||
blocked_nitrogen,
|
||||
blocked_phosphorus,
|
||||
blocked_other,
|
||||
bud_done, // An on-demand bud was made.
|
||||
bud_refused, // An on-demand bud was refused for lack of resources.
|
||||
bud_lost_early, // One of this cell's buds was engulfed within 2 minutes of birth.
|
||||
bud_with_surplus, // A bud made with the reserve covering twice its cost.
|
||||
};
|
||||
inline constexpr std::size_t cellular_signal_count = 37;
|
||||
|
||||
enum class CellularTriggerKind : std::uint8_t {
|
||||
accumulate, // Fires each time the signal has been present for `ticks` ticks in total.
|
||||
absence, // Fires when the signal has been absent for `ticks` ticks running.
|
||||
count, // Fires when the signal happened `count` times within `ticks` (0: any time).
|
||||
event, // Fires each time the signal happens.
|
||||
share, // Every `ticks` ticks: fires if signal / denominator >= per_thousand.
|
||||
regular, // Fires when the signal recurred with gaps of at most `gap` for `ticks`.
|
||||
straight, // Fires after `ticks` thrust ticks within `angle` of one heading.
|
||||
turns, // Fires after `count` turns of more than `angle` within `ticks`.
|
||||
};
|
||||
struct CellularTriggerSpec {
|
||||
CellularTriggerKind kind{};
|
||||
CellularSignal signal{};
|
||||
std::uint32_t ticks{};
|
||||
std::uint32_t count{};
|
||||
CellularSignal denominator{}; // share only.
|
||||
std::uint32_t per_thousand{}; // share only.
|
||||
std::uint32_t gap{}; // regular only.
|
||||
std::uint16_t angle{}; // straight and turns: heading units (65,536 per turn).
|
||||
auto operator<=>(const CellularTriggerSpec&) const = default;
|
||||
};
|
||||
|
||||
struct CellularMutationSpec {
|
||||
std::uint16_t id{};
|
||||
std::string_view key, name, pro, con;
|
||||
CellularEffect effect{};
|
||||
CellularTrait primary{}, paired{};
|
||||
std::int8_t primary_sign{}, paired_sign{};
|
||||
CellularSpread spread{};
|
||||
CellularAvailability available{};
|
||||
CellularTriggerSpec increase{}, decrease{};
|
||||
std::uint32_t base_weight{100};
|
||||
std::uint32_t primary_step{5}; // Default primary step in its units (points; recipe 20).
|
||||
};
|
||||
inline constexpr std::size_t cellular_catalog_size = 49;
|
||||
[[nodiscard]] std::span<const CellularMutationSpec> cellular_catalog() noexcept;
|
||||
[[nodiscard]] const CellularMutationSpec* cellular_mutation(std::uint16_t id) noexcept;
|
||||
|
||||
// One mutation's tunables (Cellular_mutation_plan.md sections 3-4); defaults from its spec.
|
||||
struct CellularMutationTuning {
|
||||
bool enabled{true};
|
||||
std::uint32_t base_weight{100};
|
||||
std::int32_t increase_points{100}, decrease_points{100};
|
||||
std::uint32_t primary_step{5}, paired_step{2}, spread_step{1};
|
||||
accounting::Inventory cost{};
|
||||
std::uint32_t cost_scale_percent{};
|
||||
std::uint32_t increase_ticks{}, decrease_ticks{}; // 0: the spec's threshold.
|
||||
auto operator<=>(const CellularMutationTuning&) const = default;
|
||||
};
|
||||
// Bounds and gates shared by every mutation.
|
||||
struct CellularCatalogBounds {
|
||||
std::uint32_t min_points{25}, max_points{1'000};
|
||||
std::uint32_t recipe_min_share{20}; // Per thousand; the founder's phosphorus is 50.
|
||||
std::uint32_t max_litter{900}; // 10 offspring per bud.
|
||||
std::uint64_t carbon_for_light{}; // Femtograms of carbon burned before photosynthesis.
|
||||
std::uint32_t buds_for_adhesion{5};
|
||||
auto operator<=>(const CellularCatalogBounds&) const = default;
|
||||
};
|
||||
// Lineage progress the gates read.
|
||||
struct CellularLineageProgress {
|
||||
std::uint64_t carbon_burned{};
|
||||
std::uint32_t buds{};
|
||||
auto operator<=>(const CellularLineageProgress&) const = default;
|
||||
};
|
||||
[[nodiscard]] CellularMutationTuning
|
||||
cellular_default_tuning(const CellularMutationSpec& spec) noexcept;
|
||||
|
||||
// The successor plan the mutation makes from `plan` (a schema-7 genome), or nothing when it is
|
||||
// not available: a gate is closed, the trait is absent, or an edit would leave its bounds. The
|
||||
// successor has one parent, `plan`, and identity `successor_id` from `origin_event`.
|
||||
[[nodiscard]] std::optional<GenomeDraft>
|
||||
apply_cellular_mutation(const Genome& plan, const CellularMutationSpec& spec,
|
||||
const CellularMutationTuning& tuning, const CellularCatalogBounds& bounds,
|
||||
const CellularLineageProgress& progress, std::uint64_t successor_id,
|
||||
std::uint64_t origin_event);
|
||||
} // namespace evo::biology
|
||||
@@ -0,0 +1,44 @@
|
||||
#pragma once
|
||||
|
||||
#include <compare>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <optional>
|
||||
#include <span>
|
||||
#include <vector>
|
||||
|
||||
// Mutation offers (rules 13, ADR-0179; Cellular_mutation_plan.md section 3): a mutation's
|
||||
// weight from its affinity score, every eligible mutation's chance of appearing in an offer
|
||||
// (floored and capped, summing to the number of options), and a systematic draw whose
|
||||
// inclusion frequencies equal those chances. Chances are parts per million (1,000,000 = 100%).
|
||||
namespace evo::biology {
|
||||
inline constexpr std::uint32_t cellular_chance_whole = 1'000'000;
|
||||
|
||||
struct CellularOfferRules {
|
||||
std::uint32_t options{3};
|
||||
std::uint32_t floor_ppm{10}; // 0.001%.
|
||||
std::uint32_t cap_ppm{700'000}; // 70%.
|
||||
std::int32_t score_min{-1'000}, score_max{1'000};
|
||||
std::uint32_t max_multiplier_per_mille{10'000}; // At score_max: 10x.
|
||||
std::uint32_t min_multiplier_per_mille{0}; // At score_min: 0x.
|
||||
auto operator<=>(const CellularOfferRules&) const = default;
|
||||
};
|
||||
|
||||
// base_weight times the score's multiplier: 1 at score 0, rising linearly to the maximum at
|
||||
// score_max and falling to the minimum at score_min.
|
||||
[[nodiscard]] std::uint64_t cellular_offer_weight(std::uint32_t base_weight, std::int32_t score,
|
||||
const CellularOfferRules& rules) noexcept;
|
||||
|
||||
// Each weight's chance: clamp(k x weight, floor, cap) with k chosen so the chances sum to
|
||||
// `options` x 100%, in the weights' order. Nothing when no such allocation exists (too few
|
||||
// mutations for the cap, too many for the floor, or weights too large).
|
||||
[[nodiscard]] std::optional<std::vector<std::uint32_t>>
|
||||
cellular_offer_chances(std::span<const std::uint64_t> weights, const CellularOfferRules& rules);
|
||||
|
||||
// Systematic draw over chances summing to `options` x 100%: the indices at
|
||||
// u, u + 100%, u + 200%, ... along the cumulative chances, `u` in [0, 100%). Distinct by
|
||||
// construction; each index appears with exactly its chance over uniform `u`.
|
||||
[[nodiscard]] std::vector<std::size_t>
|
||||
cellular_systematic_draw(std::span<const std::uint32_t> chances, std::uint32_t options,
|
||||
std::uint32_t u) noexcept;
|
||||
} // namespace evo::biology
|
||||
@@ -0,0 +1,56 @@
|
||||
#pragma once
|
||||
|
||||
#include "evo/biology/cellular_catalog.hpp"
|
||||
|
||||
#include <array>
|
||||
#include <compare>
|
||||
#include <cstdint>
|
||||
|
||||
// Behaviour triggers (rules 13, ADR-0179; Cellular_mutation_plan.md section 3): what a cell did
|
||||
// each tick raises or lowers its affinity score for each mutation. Integer state only, advanced
|
||||
// once per tick by the owner, so replay reproduces every score exactly.
|
||||
namespace evo::biology {
|
||||
// One tick of a cell's activity: flags are 0 or 1, amounts are pJ or fg (CellularSignal).
|
||||
struct CellularActivity {
|
||||
std::array<std::uint64_t, cellular_signal_count> signal{};
|
||||
std::uint16_t heading{}; // The cell's heading after the tick (65,536 per turn).
|
||||
[[nodiscard]] std::uint64_t operator[](CellularSignal s) const noexcept {
|
||||
return signal[static_cast<std::size_t>(s)];
|
||||
}
|
||||
std::uint64_t& operator[](CellularSignal s) noexcept {
|
||||
return signal[static_cast<std::size_t>(s)];
|
||||
}
|
||||
};
|
||||
|
||||
// One trigger's progress. Its meaning depends on the trigger kind; all fields start at zero.
|
||||
struct CellularTriggerState {
|
||||
std::uint64_t first{}, second{};
|
||||
std::uint32_t ticks{};
|
||||
std::uint16_t heading{};
|
||||
std::uint8_t running{};
|
||||
auto operator<=>(const CellularTriggerState&) const = default;
|
||||
};
|
||||
|
||||
// Advances one trigger by one tick; true when it fires. `threshold` replaces the spec's tick
|
||||
// threshold (or window) when non-zero.
|
||||
[[nodiscard]] bool cellular_trigger_tick(const CellularTriggerSpec& spec, std::uint32_t threshold,
|
||||
CellularTriggerState& state,
|
||||
const CellularActivity& activity) noexcept;
|
||||
|
||||
// Affinity: every mutation's score, and its increase and decrease triggers' progress.
|
||||
struct CellularAffinity {
|
||||
std::array<std::int16_t, cellular_catalog_size> score{};
|
||||
std::array<CellularTriggerState, cellular_catalog_size> increase{}, decrease{};
|
||||
auto operator<=>(const CellularAffinity&) const = default;
|
||||
};
|
||||
// Bounds of every score.
|
||||
struct CellularScoreBounds {
|
||||
std::int32_t min{-1'000}, max{1'000};
|
||||
auto operator<=>(const CellularScoreBounds&) const = default;
|
||||
};
|
||||
// Advances every mutation's triggers by one tick and applies their points to its score
|
||||
// (`tuning` is indexed like the catalog). Disabled mutations do not move.
|
||||
void cellular_affinity_tick(CellularAffinity& affinity, const CellularActivity& activity,
|
||||
std::span<const CellularMutationTuning> tuning,
|
||||
CellularScoreBounds bounds) noexcept;
|
||||
} // namespace evo::biology
|
||||
Reference in New Issue
Block a user