Propose mutations without proposing a best build
M3.4 adds the candidate-generation experiment GENOME_MODEL describes: a static catalog of four effects over the seven capabilities, drawn from a recorded context with an explicit stream. The named risk is a hidden best build or offerings biased toward what would help, so the defence is structural rather than asserted. Nothing is ranked: candidates are drawn without replacement by rejection sampling, making every eligible operator exactly as likely as any other, and the result is returned sorted by operator ID. No score exists in the record, position carries no meaning, and nothing about an organism's situation makes a helpful mutation more likely to be offered. Eligibility is not a second rulebook. An operator is eligible when the genome it would produce passes the M3.1 validation unchanged, so prerequisites, required capabilities, ranges and the acyclic development graph are enforced once, where they were defined. Shedding a capability another declares as a prerequisite fails that check with no rule written here to catch it. Range boundaries are refused rather than clamped, since a clamp would make two operators agree and offer a no-op as a choice. The draw reads the parent digest and the context and nothing else, so the same context always returns the same candidates and a retry cannot reroll; a new event index is what earns a new draw. select_mutation regenerates the offer and refuses unless it still matches, so a changed context is rejected rather than answered with a different mutation. Cost is not invented: a candidate reports the successor's own maintenance and mass under ADR-0032, and reserves nothing. The eligible set is pinned by hand from the schema rather than observed, and the uniformity check is confirmed to fail against a draw that takes the first eligible operators. The rejection sampling that removes modulo bias is correct by construction, not test-verified: at this scale the bias is one part in 10^18. That is recorded rather than claimed. No organism mutates and no fixture calls any of this. Candidate count, mutation cost as a game rule, inherited versus acquired expression, NPC mutation and recombination all remain open, as GENOME_MODEL requires. Native Release, clang-tidy analysis, ASan and format checks pass 20/20. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,294 @@
|
||||
#include "evo/biology/mutation.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <source_location>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace {
|
||||
using namespace evo::biology;
|
||||
using namespace evo::accounting;
|
||||
void check(bool condition, std::source_location location = std::source_location::current()) {
|
||||
if (!condition) {
|
||||
throw std::runtime_error("mutation line " + std::to_string(location.line()));
|
||||
}
|
||||
}
|
||||
#define CHECK(expression) check(static_cast<bool>(expression))
|
||||
constexpr Inventory pooled(std::uint64_t value) {
|
||||
return {{value}, {Femtograms{value}, Femtograms{value}, Femtograms{value}, Femtograms{value}}};
|
||||
}
|
||||
constexpr EnvironmentState temperate{293'150, pooled(1'000'000'000)};
|
||||
GenomeDraft draft() {
|
||||
return {7,
|
||||
1,
|
||||
1,
|
||||
{},
|
||||
{{Capability::energy_conversion, 400, 0},
|
||||
{Capability::membrane, 120, 0},
|
||||
{Capability::target_size, 8'000'000, 0}}};
|
||||
}
|
||||
Genome build(const GenomeDraft& source) {
|
||||
const auto genome = Genome::create(source);
|
||||
check(genome.has_value());
|
||||
return *genome;
|
||||
}
|
||||
MutationContext context(std::uint64_t seed, std::uint16_t count = 4) {
|
||||
return {seed, 1, 1, count, {1000, 0}, temperate};
|
||||
}
|
||||
|
||||
void the_same_context_always_offers_the_same_candidates() {
|
||||
const auto genome = build(draft());
|
||||
const auto first = mutation_candidates(genome, context(99));
|
||||
const auto again = mutation_candidates(genome, context(99));
|
||||
CHECK(first && again && *first == *again); // A retry is not a reroll.
|
||||
CHECK(first->size() == 4);
|
||||
// A new event earns a new draw; the seed and the stream do too.
|
||||
auto later = context(99);
|
||||
later.event_index = 2;
|
||||
const auto next_event = mutation_candidates(genome, later);
|
||||
CHECK(next_event && *next_event != *first);
|
||||
auto elsewhere = context(99);
|
||||
elsewhere.stream = 2;
|
||||
CHECK(mutation_candidates(genome, elsewhere));
|
||||
CHECK(*mutation_candidates(genome, elsewhere) != *first);
|
||||
CHECK(*mutation_candidates(genome, context(100)) != *first);
|
||||
// Context that changes the organism changes the draw as well.
|
||||
auto grown = context(99);
|
||||
grown.growth = {500, 0};
|
||||
CHECK(*mutation_candidates(genome, grown) != *first);
|
||||
}
|
||||
|
||||
void offers_are_canonical_and_unranked() {
|
||||
const auto genome = build(draft());
|
||||
bool ever_unsorted_by_cost = false;
|
||||
for (std::uint64_t seed = 0; seed < 64; ++seed) {
|
||||
const auto offered = mutation_candidates(genome, context(seed));
|
||||
CHECK(offered && offered->size() == 4);
|
||||
// Canonical order, so position cannot encode a preference. Strictly
|
||||
// increasing, because a draw without replacement never repeats one.
|
||||
for (std::size_t index = 1; index < offered->size(); ++index) {
|
||||
CHECK((*offered)[index - 1].operator_id < (*offered)[index].operator_id);
|
||||
}
|
||||
if (!std::ranges::is_sorted(*offered, {}, &MutationCandidate::maintenance)) {
|
||||
ever_unsorted_by_cost = true;
|
||||
}
|
||||
}
|
||||
// Offers are not arranged by their reported cost either, so no position in
|
||||
// the list is the cheap one or the good one.
|
||||
CHECK(ever_unsorted_by_cost);
|
||||
}
|
||||
|
||||
// The operators a founder genome with conversion, membrane and target size can
|
||||
// legitimately reach. Derived by hand from the schema: required capabilities
|
||||
// cannot be shed, absent ones cannot be adjusted, present ones cannot be
|
||||
// acquired, and target size cannot step down from 8e6 by its 9,999,999 step.
|
||||
std::vector<std::pair<Capability, MutationEffect>> expected_eligible() {
|
||||
return {{Capability::energy_conversion, MutationEffect::adjust_up},
|
||||
{Capability::energy_conversion, MutationEffect::adjust_down},
|
||||
{Capability::membrane, MutationEffect::adjust_up},
|
||||
{Capability::membrane, MutationEffect::adjust_down},
|
||||
{Capability::movement, MutationEffect::acquire},
|
||||
{Capability::ingestion, MutationEffect::acquire},
|
||||
{Capability::reproduction, MutationEffect::acquire},
|
||||
{Capability::target_size, MutationEffect::adjust_up},
|
||||
{Capability::thermal_tolerance, MutationEffect::acquire}};
|
||||
}
|
||||
|
||||
void every_eligible_operator_is_offered_and_none_is_favoured() {
|
||||
const auto genome = build(draft());
|
||||
constexpr std::uint64_t trials = 2000;
|
||||
constexpr std::uint64_t drawn = 4;
|
||||
std::map<std::uint16_t, std::uint64_t> offers;
|
||||
for (std::uint64_t seed = 0; seed < trials; ++seed) {
|
||||
const auto offered = mutation_candidates(genome, context(seed));
|
||||
CHECK(offered && offered->size() == drawn);
|
||||
for (const auto& candidate : *offered) {
|
||||
++offers[candidate.operator_id];
|
||||
}
|
||||
}
|
||||
// The set that was offered is exactly the set the schema permits, no more.
|
||||
const auto permitted = expected_eligible();
|
||||
CHECK(offers.size() == permitted.size());
|
||||
for (const auto& [kind, effect] : permitted) {
|
||||
const auto entry = std::ranges::find_if(mutation_catalog(), [&](const auto& candidate) {
|
||||
return candidate.target == kind && candidate.effect == effect;
|
||||
});
|
||||
CHECK(entry != mutation_catalog().end() && offers.contains(entry->id));
|
||||
}
|
||||
// Each is offered within a fifth of its exact share. The draw is uniform by
|
||||
// construction, so this is about six standard deviations wide; a draw that
|
||||
// favoured any operator would be the biased-adaptation failure.
|
||||
const auto share = trials * drawn / permitted.size();
|
||||
for (const auto& [id, count] : offers) {
|
||||
CHECK(count * 5 > share * 4);
|
||||
CHECK(count * 4 < share * 5);
|
||||
}
|
||||
}
|
||||
|
||||
void only_legitimate_operators_are_offered() {
|
||||
const auto genome = build(draft());
|
||||
const auto offered = mutation_candidates(genome, context(5, 8));
|
||||
CHECK(offered);
|
||||
for (const auto& candidate : *offered) {
|
||||
// A required capability can never be shed, however it is drawn.
|
||||
const auto& limits = capability_schema[static_cast<std::size_t>(candidate.target)];
|
||||
CHECK(!(candidate.effect == MutationEffect::shed && limits.required));
|
||||
// Acquiring only ever applies to something the parent lacks, and
|
||||
// adjusting only to something it carries.
|
||||
if (candidate.effect == MutationEffect::acquire) {
|
||||
CHECK(!genome.carries(candidate.target));
|
||||
CHECK(candidate.parameter == limits.minimum);
|
||||
} else if (candidate.effect != MutationEffect::shed) {
|
||||
CHECK(genome.carries(candidate.target));
|
||||
CHECK(candidate.parameter >= limits.minimum && candidate.parameter <= limits.maximum);
|
||||
}
|
||||
// Every offer produces a genome the schema accepts.
|
||||
const auto successor =
|
||||
select_mutation(genome, context(5, 8), *offered, candidate.operator_id, 99, 42);
|
||||
CHECK(successor);
|
||||
CHECK(successor->id() == 99 && successor->origin_event() == 42);
|
||||
CHECK(successor->parents().size() == 1 && successor->parents()[0] == 7);
|
||||
CHECK(Genome::decode(successor->encode()));
|
||||
}
|
||||
}
|
||||
|
||||
void boundaries_are_refused_rather_than_clamped() {
|
||||
// At the top of its range, adjusting a capability up is not on offer at all.
|
||||
auto source = draft();
|
||||
source.capabilities[1].parameter = 10'000; // membrane at its maximum
|
||||
const auto topped = build(source);
|
||||
const auto offers = mutation_candidates(topped, context(3, 8));
|
||||
CHECK(offers);
|
||||
for (const auto& candidate : *offers) {
|
||||
CHECK(!(candidate.target == Capability::membrane &&
|
||||
candidate.effect == MutationEffect::adjust_up));
|
||||
}
|
||||
// The same at the bottom.
|
||||
source.capabilities[1].parameter = 1;
|
||||
const auto floored = build(source);
|
||||
const auto low = mutation_candidates(floored, context(3, 8));
|
||||
CHECK(low);
|
||||
for (const auto& candidate : *low) {
|
||||
CHECK(!(candidate.target == Capability::membrane &&
|
||||
candidate.effect == MutationEffect::adjust_down));
|
||||
}
|
||||
// A capability another one declares as a prerequisite cannot be shed, and no
|
||||
// rule here says so: the schema's own validation refuses it.
|
||||
source = draft();
|
||||
source.capabilities[1].parameter = 120;
|
||||
source.capabilities.push_back({Capability::thermal_tolerance, 500, 0});
|
||||
source.capabilities.push_back(
|
||||
{Capability::movement, 250, capability_bit(Capability::thermal_tolerance)});
|
||||
const auto dependent = build(source);
|
||||
const auto guarded = mutation_candidates(dependent, context(11, 8));
|
||||
CHECK(guarded);
|
||||
for (const auto& candidate : *guarded) {
|
||||
CHECK(!(candidate.target == Capability::thermal_tolerance &&
|
||||
candidate.effect == MutationEffect::shed));
|
||||
}
|
||||
// Shedding the dependent capability itself is still legitimate.
|
||||
const auto sheddable = std::ranges::find_if(*guarded, [](const auto& candidate) {
|
||||
return candidate.target == Capability::movement && candidate.effect == MutationEffect::shed;
|
||||
});
|
||||
if (sheddable != guarded->end()) {
|
||||
const auto successor =
|
||||
select_mutation(dependent, context(11, 8), *guarded, sheddable->operator_id, 99, 42);
|
||||
CHECK(successor && !successor->carries(Capability::movement));
|
||||
}
|
||||
}
|
||||
|
||||
void selection_refuses_a_changed_context_or_an_unknown_choice() {
|
||||
const auto genome = build(draft());
|
||||
const auto offered = mutation_candidates(genome, context(7));
|
||||
CHECK(offered);
|
||||
const auto chosen = offered->front().operator_id;
|
||||
CHECK(select_mutation(genome, context(7), *offered, chosen, 99, 42));
|
||||
// An operator that was never offered is refused even though it is valid.
|
||||
std::uint16_t absent = 0;
|
||||
for (const auto& entry : mutation_catalog()) {
|
||||
if (std::ranges::find(*offered, entry.id, &MutationCandidate::operator_id) ==
|
||||
offered->end()) {
|
||||
absent = entry.id;
|
||||
break;
|
||||
}
|
||||
}
|
||||
CHECK(absent != 0);
|
||||
const auto unknown = select_mutation(genome, context(7), *offered, absent, 99, 42);
|
||||
CHECK(!unknown && unknown.error() == MutationError::unknown_candidate);
|
||||
// A context that moved is rejected outright, never answered with a
|
||||
// different mutation that happens to still be valid.
|
||||
auto moved = context(7);
|
||||
moved.event_index = 5;
|
||||
const auto stale = select_mutation(genome, moved, *offered, chosen, 99, 42);
|
||||
CHECK(!stale && stale.error() == MutationError::context_changed);
|
||||
auto regrown = context(7);
|
||||
regrown.growth = {250, 0};
|
||||
const auto shifted = select_mutation(genome, regrown, *offered, chosen, 99, 42);
|
||||
CHECK(!shifted && shifted.error() == MutationError::context_changed);
|
||||
// A tampered offer is a changed context too.
|
||||
auto edited = *offered;
|
||||
edited.front().maintenance += 1;
|
||||
const auto forged = select_mutation(genome, context(7), edited, chosen, 99, 42);
|
||||
CHECK(!forged && forged.error() == MutationError::context_changed);
|
||||
// A different parent genome cannot be answered with this offer.
|
||||
auto other = draft();
|
||||
other.capabilities[0].parameter = 401;
|
||||
const auto cousin = build(other);
|
||||
const auto mismatched = select_mutation(cousin, context(7), *offered, chosen, 99, 42);
|
||||
CHECK(!mismatched && mismatched.error() == MutationError::context_changed);
|
||||
}
|
||||
|
||||
void reported_cost_is_what_the_budget_charges() {
|
||||
const auto genome = build(draft());
|
||||
const auto offered = mutation_candidates(genome, context(13, 8));
|
||||
CHECK(offered);
|
||||
for (const auto& candidate : *offered) {
|
||||
const auto successor =
|
||||
select_mutation(genome, context(13, 8), *offered, candidate.operator_id, 99, 42);
|
||||
CHECK(successor);
|
||||
// The candidate showed exactly what ADR-0032 would charge; nothing here
|
||||
// invents a price, and nothing was reserved by displaying it.
|
||||
const auto budget = metabolic_budget(*successor, {1000, 0}, temperate);
|
||||
CHECK(budget && budget->maintenance.value == candidate.maintenance);
|
||||
CHECK(budget->phenotype.total_mass == candidate.total_mass);
|
||||
}
|
||||
// The parent is untouched by every one of those proposals.
|
||||
CHECK(genome == build(draft()));
|
||||
}
|
||||
|
||||
void invalid_contexts_are_refused() {
|
||||
const auto genome = build(draft());
|
||||
for (const std::uint16_t count :
|
||||
{std::uint16_t{0}, static_cast<std::uint16_t>(max_candidates + 1)}) {
|
||||
const auto refused = mutation_candidates(genome, context(1, count));
|
||||
CHECK(!refused && refused.error() == MutationError::invalid_context);
|
||||
}
|
||||
// An unsupported development version leaves no eligible operator at all,
|
||||
// because every successor fails to derive a budget.
|
||||
auto source = draft();
|
||||
source.development_version = 4;
|
||||
const auto future = build(source);
|
||||
const auto empty = mutation_candidates(future, context(1));
|
||||
CHECK(!empty && empty.error() == MutationError::no_candidates);
|
||||
CHECK(mutation_catalog().size() == 4 * capability_count);
|
||||
}
|
||||
} // namespace
|
||||
int main() try {
|
||||
the_same_context_always_offers_the_same_candidates();
|
||||
offers_are_canonical_and_unranked();
|
||||
only_legitimate_operators_are_offered();
|
||||
boundaries_are_refused_rather_than_clamped();
|
||||
selection_refuses_a_changed_context_or_an_unknown_choice();
|
||||
reported_cost_is_what_the_budget_charges();
|
||||
invalid_contexts_are_refused();
|
||||
every_eligible_operator_is_offered_and_none_is_favoured();
|
||||
std::cout << "PASS mutation catalog, uniform seeded offers and guarded selection\n";
|
||||
return 0;
|
||||
} catch (...) {
|
||||
return 1;
|
||||
}
|
||||
Reference in New Issue
Block a user