Files
aevolution/tests/photosynthesis_tests.cpp
T
dingoandClaude Opus 5.5 108f29eda5
Foundation / checks (ecs) (push) Successful in 2m25s
Foundation / checks (deploy) (push) Successful in 5m15s
Foundation / checks (network) (push) Successful in 21m56s
Foundation / checks (client) (push) Successful in 22m54s
Foundation / checks (tsan) (push) Successful in 47m4s
Foundation / checks (postgres) (push) Successful in 1h15m49s
Foundation / checks (checks) (push) Failing after 1h30m4s
Foundation / postgres-tools (push) Skipped
Foundation / server-image (push) Skipped
Foundation / image (push) Skipped
feat(biology): evolve photosynthetic producers that capture light
ADR-0175 implements ADR-0136 step C with trial costs to be balanced after
playtesting. Genome schema 5 adds a photosynthesis efficiency (empty in
schemas 1-4, so earlier offers and draws are unchanged; mutation operators
29-32 are appended). Development version 5 builds the organelles as
internal membrane (p/4 of structure, upkept twice) and slows movement by
p/2 per thousand. Founder 4 and cellular rules 11 let lineages earn the
capability as an offspring plan that children express.

Owner format 9 declares the region's light. Inside each tick's detached
candidate, active unfrozen producers capture cross-section x irradiance x
efficiency (a quarter while moving), credited to the reserve and boundary
inflow together and replayed from recorded intentions. Well-lit wildlife
producers hold still. The joint owner keeps infall, water and radiation and
nests a rules-11 owner unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 18:06:02 -04:00

138 lines
7.2 KiB
C++

#include "evo/biology/founder.hpp"
#include "evo/biology/mutation.hpp"
#include "evo/server/lifecycle/cellular_light.hpp"
#include <algorithm>
#include <iostream>
#include <stdexcept>
#include <string>
namespace {
using namespace evo;
using namespace evo::biology;
using namespace evo::server;
void require(bool condition, const char* message) {
if (!condition)
throw std::runtime_error(message);
}
template <typename T, typename E> T checked(std::expected<T, E> value, const char* message) {
if (!value)
throw std::runtime_error(std::string{message} + ": " +
std::to_string(static_cast<unsigned>(value.error())));
return std::move(*value);
}
Genome with(const Genome& base, std::uint32_t efficiency) {
GenomeDraft draft{base.id() + 1, 2, base.development_version(), {base.id()}, {}, {}};
draft.schema_version = base.schema_version();
draft.capabilities.assign(base.capabilities().begin(), base.capabilities().end());
draft.capabilities.push_back({Capability::photosynthesis, efficiency, 0});
return checked(Genome::create(draft), "photosynthetic genome");
}
constexpr EnvironmentState reference{293'150, {}};
// Founder 4 is founder 3 with a schema that can express photosynthesis; it starts without.
void founders() {
const auto three = checked(primitive_founder(5, 5, primitive_founder_v3), "founder 3");
const auto four = checked(primitive_founder(5, 5, primitive_founder_v4), "founder 4");
require(!four.carries(Capability::photosynthesis) && four.schema_version() == 5 &&
four.development_version() == 5,
"founder 4 carries photosynthesis or wrong versions");
const auto a = checked(metabolic_budget(three, {1000, 0}, reference), "founder 3 budget");
const auto b = checked(metabolic_budget(four, {1000, 0}, reference), "founder 4 budget");
require(a.phenotype == b.phenotype && a.body == b.body && a.maintenance == b.maintenance &&
a.reproduction_investment == b.reproduction_investment,
"founder 4 differs from founder 3 without photosynthesis");
require(checked(primitive_endowment(primitive_founder_v4), "endowment 4") ==
checked(primitive_endowment(primitive_founder_v3), "endowment 3"),
"founder 4 endowment changed");
// Earlier schemas cannot carry it at any efficiency.
GenomeDraft old{9, 9, 4, {}, {}, genome_schema_v4};
old.capabilities.assign(three.capabilities().begin(), three.capabilities().end());
old.capabilities.push_back({Capability::photosynthesis, 100, 0});
require(Genome::create(old).error() == GenomeError::parameter_out_of_range,
"schema 4 accepted photosynthesis");
}
// Organelles: p/4 of structure as membrane mass (upkept twice), speed down by p/2.
void costs() {
const auto four = checked(primitive_founder(5, 5, primitive_founder_v4), "founder 4");
const auto base = checked(metabolic_budget(four, {1000, 0}, reference), "base budget");
for (const auto efficiency : {100U, 500U, 1000U}) {
const auto producer = with(four, efficiency);
const auto budget = checked(metabolic_budget(producer, {1000, 0}, reference), "producer");
const auto organelles = base.phenotype.structural_mass / 4 * efficiency / 1000;
require(budget.phenotype.membrane_mass == base.phenotype.membrane_mass + organelles &&
budget.phenotype.structural_mass == base.phenotype.structural_mass &&
budget.phenotype.extent == base.phenotype.extent &&
budget.maintenance.value > base.maintenance.value &&
budget.phenotype.movement_speed ==
base.phenotype.movement_speed * (1000 - efficiency / 2) / 1000,
"photosynthesis costs differ from the declared trial rule");
const CellularLight light{cellular_light_trial_irradiance, 1, {1ULL << 50}};
const auto captured_still =
cellular_light_capture(producer, budget.phenotype.extent, light, false);
const auto captured_moving =
cellular_light_capture(producer, budget.phenotype.extent, light, true);
require(captured_still.has_value() && captured_moving.has_value(), "capture overflowed");
const auto still = captured_still.value_or(0), moving = captured_moving.value_or(0);
const auto section = std::uint64_t{budget.phenotype.extent} * budget.phenotype.extent;
require(still == section * cellular_light_trial_irradiance * efficiency / 1000 &&
moving == still / 4,
"light capture differs from cross-section x irradiance x efficiency");
std::cout << "photosynthesis " << efficiency << ": maintenance "
<< budget.maintenance.value / 20 << " pJ/tick (founder "
<< base.maintenance.value / 20 << "), capture still " << still << ", moving "
<< moving << ", speed " << budget.phenotype.movement_speed << " um/s\n";
}
require(cellular_light_capture(four, 20, {cellular_light_trial_irradiance, 1, {1}}, false) == 0,
"a genome without the capability captured light");
}
// Schema 5 offers photosynthesis through the appended operators; earlier schemas never do.
void offers() {
const auto catalog = mutation_catalog();
require(catalog.size() == 32, "catalog size");
for (std::uint16_t id = 29; id <= 32; ++id)
require(catalog[id - 1].target == Capability::photosynthesis,
"photosynthesis operators are not IDs 29-32");
const auto four = checked(primitive_founder(5, 5, primitive_founder_v4), "founder 4");
const auto three = checked(primitive_founder(5, 5, primitive_founder_v3), "founder 3");
bool offered = false;
for (std::uint64_t event = 1; event <= 400 && !offered; ++event) {
const MutationContext context{0x5EED, 1, event, 8, {1000, 0}, reference};
const auto old = checked(mutation_candidates(three, context), "schema 4 offer");
require(std::ranges::none_of(
old.candidates,
[](const auto& c) { return c.target == Capability::photosynthesis; }),
"schema 4 lineage offered photosynthesis");
const auto offer = checked(mutation_candidates(four, context), "schema 5 offer");
const auto found = std::ranges::find_if(offer.candidates, [](const auto& c) {
return c.target == Capability::photosynthesis && c.effect == MutationEffect::acquire;
});
if (found == offer.candidates.end())
continue;
offered = true;
require(found->parameter == photosynthesis_acquired_start,
"acquired photosynthesis does not start at the trial value");
const auto successor =
checked(select_mutation(four, context, offer, found->operator_id, 77, 77), "select");
require(*successor.parameter(Capability::photosynthesis) == photosynthesis_acquired_start,
"selected successor lost photosynthesis");
}
require(offered, "schema 5 never offered photosynthesis");
}
} // namespace
int main() {
try {
founders();
costs();
offers();
std::cout << "photosynthesis contracts passed\n";
return 0;
} catch (const std::exception& error) {
std::cerr << error.what() << '\n';
return 1;
}
}