ProgramGenome

Models & Types · AiDotNet.Evolution.Programs

An immutable program source treated as one evolvable candidate.

Implements: IEquatable<ProgramGenome>, IImmutableEvolutionGenome<ProgramGenome>

For Beginners

This class is one candidate program in an evolutionary search: the code itself, the language it is written in, and an optional note about what changed. The important part is Id, a fingerprint computed from the code after cosmetic differences are removed. If a model reformats a file’s line endings but changes nothing else, the fingerprint stays the same and the search knows it has already tried that program. Because the object never changes after construction, it is safe to keep in an archive, write to a checkpoint, and share between threads.

How It Works

The genome carries the exact Source text the run will hand to an execution engine plus the Language that decides comment markers, fence labels, and file extensions. Id is a lowercase hexadecimal SHA-256 over NormalizedSource and Language, and normalization strips a byte-order mark, rewrites CRLF and CR terminators as line feeds, trims trailing white space from every line, and drops trailing blank lines. Two proposals that differ only in those incidental ways therefore share one identity, which is exactly what IEvolutionTask<TGenome>.CanonicalizeAsync needs so the engine can deduplicate them and reuse a cached evaluation instead of paying to run the same program twice.

Construction validates that the source is non-empty after normalization and no longer than MaxSourceLength characters, so an unbounded model response cannot become an unbounded genome. Identity and value equality cover exactly the same fields: NormalizedSource and Language. Language belongs in both because it selects the interpreter, so byte-identical text in two languages is two candidates that evaluate differently and must never share a cached result. Description belongs in neither: it is the model’s note about what a proposal changed, it cannot alter how the program runs or scores, and putting it in equality while leaving it out of the identity is what previously let two genomes be unequal yet share an Id. A run that genuinely wants a description-carrying identity should fold it in at the task’s canonicalization step instead.

Constructors

Constructor Summary
ProgramGenome(string source, ProgramLanguage language, string description) Initializes an immutable program genome.

Properties

Property Summary
ProgramLanguage Language { get; } Gets the language the source is written in.
int LineCount { get; } Gets the number of lines in NormalizedSource.
string Description { get; } Gets the optional bounded description of this candidate, or null when none was supplied.
string Id { get; } Gets the lowercase hexadecimal SHA-256 over NormalizedSource and Language.
string NormalizedSource { get; } Gets the source after byte-order-mark removal, line-ending normalization, and trailing-space trimming.
string Source { get; } Gets the source text exactly as supplied, including its original line endings.

Methods

Method Summary
ProgramGenome CreateOwnedSnapshot()
ProgramGenome WithDescription(string description) Returns a copy of this genome with a different description.
ProgramGenome WithSource(string source, string description) Returns a copy of this genome with a different source and the same language.
bool Equals(ProgramGenome other)
bool Equals(object obj)
bool op_Equality(ProgramGenome left, ProgramGenome right) Determines whether two genomes are value equal.
bool op_Inequality(ProgramGenome left, ProgramGenome right) Determines whether two genomes differ.
int GetHashCode()
string ComputeId(string source, ProgramLanguage language) Computes the identity a genome built from source would have.
string Normalize(string source) Normalizes source text the same way the constructor does, without building a genome.
string ToString() Returns the identity and language, never the source text, so logs stay bounded.

Fields

Field Summary
int MaxDescriptionLength The largest description length, in characters, that a genome may carry.
int MaxSourceLength The largest source length, in characters, that a genome may carry.