Files
aevolution/tests/photosynthesis_tests.cpp
T
dingoandClaude Opus 5.5 2efb01d1a3
Foundation / checks (ecs) (push) Successful in 2m29s
Foundation / checks (deploy) (push) Successful in 5m29s
Foundation / checks (network) (push) Successful in 23m57s
Foundation / checks (client) (push) Successful in 24m50s
Foundation / checks (tsan) (push) Successful in 47m40s
Foundation / checks (postgres) (push) Successful in 1h22m11s
Foundation / checks (checks) (push) Successful in 1h49m20s
Foundation / postgres-tools (push) Successful in 1m13s
Foundation / server-image (push) Successful in 3m45s
Foundation / image (push) Successful in 6m1s
ci: give the checks job headroom and trim producer tests
Run 469 failed only the checks job, at exactly its 90-minute limit; full
checks runs had grown from 67 to 78 minutes through M12 and every step
passes locally. The limit is now 120 minutes. The producer test checks dark
and brighter light on the capture rule instead of growing two extra
histories, and the trace test takes over the grown population.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 23:21:29 -04:00

147 lines
7.8 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");
// Dark light captures nothing; capture scales exactly with irradiance when it divides.
const auto producer = with(four, 100);
require(cellular_light_capture(producer, 20, {0, 1, {0}}, false) == 0,
"dark light captured energy");
const auto trial =
cellular_light_capture(producer, 20, {cellular_light_trial_irradiance, 1, {1}}, false);
const auto bright =
cellular_light_capture(producer, 20, {cellular_light_trial_irradiance * 20, 1, {1}}, false);
require(trial == 1'600 && bright == 32'000, "capture did not scale with irradiance");
}
// 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;
}
}