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>
228 lines
9.7 KiB
C++
228 lines
9.7 KiB
C++
#include "evo/biology/cellular_offers.hpp"
|
|
#include "evo/biology/cellular_triggers.hpp"
|
|
#include "evo/core/random.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <array>
|
|
#include <cstdio>
|
|
#include <numeric>
|
|
#include <set>
|
|
#include <source_location>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
// Rules-13 offers (ADR-0179; Cellular_mutation_plan.md section 3): triggers move affinity
|
|
// scores, scores set weights, weights set floored and capped chances summing to the options,
|
|
// and a systematic draw picks distinct mutations with exactly those chances.
|
|
namespace {
|
|
using namespace evo;
|
|
using namespace evo::biology;
|
|
|
|
void check(bool condition, std::source_location at = std::source_location::current()) {
|
|
if (!condition)
|
|
throw std::runtime_error("cellular offers line " + std::to_string(at.line()));
|
|
}
|
|
#define CHECK(...) check(static_cast<bool>(__VA_ARGS__))
|
|
|
|
CellularActivity with(CellularSignal signal, std::uint64_t value = 1, std::uint16_t heading = 0) {
|
|
CellularActivity activity{};
|
|
activity[signal] = value;
|
|
activity.heading = heading;
|
|
return activity;
|
|
}
|
|
// Runs a trigger over `ticks` ticks of `activity`, counting fires.
|
|
std::uint32_t run(const CellularTriggerSpec& spec, CellularTriggerState& state,
|
|
const CellularActivity& activity, std::uint32_t ticks) {
|
|
std::uint32_t fired = 0;
|
|
for (std::uint32_t t = 0; t < ticks; ++t)
|
|
fired += cellular_trigger_tick(spec, 0, state, activity) ? 1U : 0U;
|
|
return fired;
|
|
}
|
|
|
|
void triggers() {
|
|
using K = CellularTriggerKind;
|
|
using S = CellularSignal;
|
|
const auto thrusting = with(S::thrust);
|
|
const CellularActivity idle{};
|
|
// Accumulate: every 200 thrust ticks, idle ticks do not count.
|
|
CellularTriggerState s{};
|
|
const CellularTriggerSpec thrust10{K::accumulate, S::thrust, 200};
|
|
CHECK(run(thrust10, s, thrusting, 199) == 0 && run(thrust10, s, idle, 50) == 0 &&
|
|
run(thrust10, s, thrusting, 1) == 1 && run(thrust10, s, thrusting, 400) == 2);
|
|
// Absence: fires after 100 idle ticks running; any thrust restarts it.
|
|
s = {};
|
|
const CellularTriggerSpec quiet{K::absence, S::thrust, 100};
|
|
CHECK(run(quiet, s, idle, 99) == 0 && run(quiet, s, thrusting, 1) == 0 &&
|
|
run(quiet, s, idle, 99) == 0 && run(quiet, s, idle, 1) == 1);
|
|
// A tuned threshold replaces the spec's.
|
|
s = {};
|
|
CHECK(!cellular_trigger_tick(quiet, 2, s, idle) && cellular_trigger_tick(quiet, 2, s, idle));
|
|
// Count: 3 presses within 10 ticks fire; spread out they do not.
|
|
s = {};
|
|
const CellularTriggerSpec presses{K::count, S::brake_press, 10, 3};
|
|
const auto press = with(S::brake_press);
|
|
CHECK(run(presses, s, press, 1) == 0 && run(presses, s, idle, 3) == 0 &&
|
|
run(presses, s, press, 1) == 0 && run(presses, s, idle, 3) == 0 &&
|
|
run(presses, s, press, 1) == 1); // The third press 8 ticks after the first.
|
|
s = {};
|
|
CHECK(run(presses, s, press, 1) + run(presses, s, idle, 12) + run(presses, s, press, 1) +
|
|
run(presses, s, idle, 12) + run(presses, s, press, 1) ==
|
|
0);
|
|
// Count without a window: any 3.
|
|
s = {};
|
|
const CellularTriggerSpec failures{K::count, S::engulf_too_large, 0, 3};
|
|
const auto fail = with(S::engulf_too_large);
|
|
CHECK(run(failures, s, fail, 1) + run(failures, s, idle, 5'000) + run(failures, s, fail, 2) ==
|
|
1);
|
|
// Event: each tick it happens.
|
|
s = {};
|
|
CHECK(run({K::event, S::engulf_success}, s, with(S::engulf_success), 3) == 3);
|
|
// Share: at the end of each 4-tick window, carbon at least half of all matter collected.
|
|
s = {};
|
|
const CellularTriggerSpec diet{K::share, S::collected_carbon, 4, 0, S::collected_matter, 500};
|
|
CellularActivity rich{};
|
|
rich[S::collected_carbon] = 6;
|
|
rich[S::collected_matter] = 10;
|
|
CellularActivity poor{};
|
|
poor[S::collected_carbon] = 1;
|
|
poor[S::collected_matter] = 10;
|
|
CHECK(run(diet, s, rich, 3) == 0 && run(diet, s, rich, 1) == 1 && run(diet, s, poor, 4) == 0 &&
|
|
run(diet, s, idle, 4) == 0);
|
|
// Regular: thrust at least every 3 ticks for 10 ticks fires; a longer gap breaks the run.
|
|
s = {};
|
|
const CellularTriggerSpec pulsed{K::regular, S::thrust, 10, 0, {}, 0, 3};
|
|
std::uint32_t fired = 0;
|
|
for (int t = 0; t < 12; ++t)
|
|
fired += cellular_trigger_tick(pulsed, 0, s, t % 3 == 0 ? thrusting : idle) ? 1U : 0U;
|
|
CHECK(fired == 1);
|
|
s = {};
|
|
fired = 0;
|
|
for (int t = 0; t < 30; ++t)
|
|
fired += cellular_trigger_tick(pulsed, 0, s, t % 5 == 0 ? thrusting : idle) ? 1U : 0U;
|
|
CHECK(fired == 0);
|
|
// Straight: 5 thrust ticks within 20 degrees of one heading; a sharp change restarts.
|
|
s = {};
|
|
const CellularTriggerSpec line{K::straight, S::thrust, 5, 0, {}, 0, 0, 3'641};
|
|
CHECK(run(line, s, with(S::thrust, 1, 1'000), 4) == 0 &&
|
|
run(line, s, with(S::thrust, 1, 3'000), 1) == 1);
|
|
s = {};
|
|
CHECK(run(line, s, with(S::thrust, 1, 0), 4) == 0 &&
|
|
run(line, s, with(S::thrust, 1, 10'000), 4) == 0 &&
|
|
run(line, s, with(S::thrust, 1, 10'000), 1) == 1);
|
|
// Turns: 3 turns of more than 90 degrees within 20 ticks.
|
|
s = {};
|
|
const CellularTriggerSpec weave{K::turns, S::thrust, 20, 3, {}, 0, 0, 16'384};
|
|
fired = 0;
|
|
for (const std::uint16_t heading :
|
|
std::array<std::uint16_t, 5>{0, 20'000, 40'000, 60'000, 60'100})
|
|
fired += cellular_trigger_tick(weave, 0, s, with(S::thrust, 1, heading)) ? 1U : 0U;
|
|
CHECK(fired == 1);
|
|
// Wrapping: 65,000 to 1,000 is a small turn.
|
|
s = {};
|
|
CHECK(!cellular_trigger_tick(weave, 0, s, with(S::thrust, 1, 65'000)) &&
|
|
!cellular_trigger_tick(weave, 0, s, with(S::thrust, 1, 1'000)) && s.first == 0);
|
|
}
|
|
|
|
void affinity() {
|
|
std::vector<CellularMutationTuning> tuning;
|
|
for (const auto& spec : cellular_catalog())
|
|
tuning.push_back(cellular_default_tuning(spec));
|
|
CellularAffinity a{};
|
|
// Ten seconds of thrust raises Strong Propulsion (index 0) and lowers Light Propulsion (6).
|
|
const auto thrusting = with(CellularSignal::thrust);
|
|
for (int t = 0; t < 200; ++t)
|
|
cellular_affinity_tick(a, thrusting, tuning, {});
|
|
CHECK(a.score[0] == 100 && a.score[6] == -100);
|
|
// Scores stay within bounds, and a disabled mutation does not move.
|
|
tuning[0].increase_points = 5'000;
|
|
tuning[6].enabled = false;
|
|
for (int t = 0; t < 200; ++t)
|
|
cellular_affinity_tick(a, thrusting, tuning, {-1'000, 1'000});
|
|
CHECK(a.score[0] == 1'000 && a.score[6] == -100);
|
|
}
|
|
|
|
void weights() {
|
|
const CellularOfferRules rules{};
|
|
CHECK(cellular_offer_weight(100, 0, rules) == 100'000);
|
|
CHECK(cellular_offer_weight(100, 1'000, rules) == 1'000'000);
|
|
CHECK(cellular_offer_weight(100, 500, rules) == 550'000);
|
|
CHECK(cellular_offer_weight(100, -1'000, rules) == 0);
|
|
CHECK(cellular_offer_weight(100, -500, rules) == 50'000);
|
|
CHECK(cellular_offer_weight(100, 9'999, rules) == 1'000'000); // Clamped to score_max.
|
|
}
|
|
|
|
std::uint64_t sum(const std::vector<std::uint32_t>& values) {
|
|
return std::accumulate(values.begin(), values.end(), std::uint64_t{0});
|
|
}
|
|
|
|
void chances() {
|
|
const CellularOfferRules rules{};
|
|
// Ten equal weights share 300% equally.
|
|
const std::vector<std::uint64_t> equal(10, 1'000);
|
|
const auto even = cellular_offer_chances(equal, rules);
|
|
CHECK(even && std::ranges::all_of(*even, [](std::uint32_t c) { return c == 300'000; }));
|
|
// One heavy weight is capped at 70%; one zero weight sits at the floor.
|
|
std::vector<std::uint64_t> mixed{1'000, 1'000, 1'000, 1'000, 1'000'000, 0};
|
|
const auto capped = cellular_offer_chances(mixed, rules);
|
|
CHECK(capped && (*capped)[4] == 700'000 && (*capped)[5] == 10 && sum(*capped) == 3'000'000);
|
|
CHECK(capped && capped.value()[0] - capped.value()[3] <= 1); // Rounding over four.
|
|
// Four mutations cannot fill three options at a 70% cap.
|
|
CHECK(!cellular_offer_chances(std::vector<std::uint64_t>(4, 1'000), rules));
|
|
// Seed ensemble: every allocation sums exactly and respects the bounds and the order.
|
|
Random random{11};
|
|
for (int round = 0; round < 300; ++round) {
|
|
const auto m = 5 + random.next() % 45;
|
|
std::vector<std::uint64_t> w(m);
|
|
for (auto& weight : w)
|
|
weight =
|
|
random.next() % 4 == 0 ? random.next() % 1'000'000'000 : random.next() % 200'000;
|
|
const auto c = cellular_offer_chances(w, rules);
|
|
CHECK(c.has_value());
|
|
if (!c)
|
|
continue;
|
|
CHECK(sum(*c) == 3'000'000);
|
|
for (std::size_t i = 0; i < m; ++i) {
|
|
CHECK((*c)[i] >= 10 && (*c)[i] <= 700'000);
|
|
for (std::size_t j = 0; j < m; ++j)
|
|
if (w[i] > w[j])
|
|
CHECK((*c)[i] + 1 >= (*c)[j]);
|
|
}
|
|
}
|
|
}
|
|
|
|
void systematic_draw() {
|
|
const CellularOfferRules rules{};
|
|
const std::vector<std::uint64_t> w{5, 900'000, 3'000, 3'000, 40'000, 0, 120'000};
|
|
const auto c = cellular_offer_chances(w, rules);
|
|
CHECK(c.has_value());
|
|
if (!c)
|
|
return;
|
|
std::vector<std::uint64_t> picked(c->size(), 0);
|
|
for (std::uint32_t u = 0; u < cellular_chance_whole; ++u) {
|
|
const auto draw = cellular_systematic_draw(*c, 3, u);
|
|
CHECK(draw.size() == 3 && std::set<std::size_t>(draw.begin(), draw.end()).size() == 3);
|
|
for (const auto index : draw)
|
|
++picked[index];
|
|
}
|
|
// Over every u, each mutation appears in exactly its chance's share of draws.
|
|
for (std::size_t i = 0; i < c->size(); ++i)
|
|
CHECK(picked[i] == (*c)[i]);
|
|
}
|
|
} // namespace
|
|
|
|
int main() {
|
|
try {
|
|
triggers();
|
|
affinity();
|
|
weights();
|
|
chances();
|
|
systematic_draw();
|
|
} catch (const std::exception& error) {
|
|
std::fprintf(stderr, "%s\n", error.what());
|
|
return 1;
|
|
}
|
|
std::puts("PASS cellular triggers, affinity, chances and the systematic draw");
|
|
return 0;
|
|
}
|