EmbeddingCosineGenomeDistance

Models & Types · AiDotNet.Evolution.Programs.Novelty

A cosine distance over program embeddings, served from a primed cache so no comparison blocks on I/O.

Implements: IGenomeDistance<ProgramGenome>

For Beginners

Embeddings tell you whether two programs mean similar things, but fetching one takes a network call. This class keeps the two steps apart: first you ask it to fetch vectors for a set of programs, then comparing any two of them is instant. If you compare a program it never fetched, it quietly falls back to a text-only comparison instead of stalling.

How It Works

Distance is synchronous, and a synchronous method must never block on a network call — doing so is how a search deadlocks under a synchronization context. So this metric separates the two halves: PrimeAsync fetches vectors in one batched, cancellable request, and Distance then reads them out of the cache and returns 1 - cosine, clamped to 0 to 1. A pair whose vectors are not both cached falls back to Fallback, which never needs a provider, so a missing vector degrades precision rather than correctness.

Keying is by Id, the SHA-256 of the normalized source, so two candidates that differ only in line endings share one vector and one request. Priming a genome already primed costs nothing. Wrapping the supplied client in a CachingEmbeddingClient extends that reuse across metric instances and across runs in the same process.

The reference implementation has no equivalent: it embeds inside the insertion path, one blocking request per candidate, then compares the new vector against every member of the island in a Python loop. Here the requests are batched and explicit, the comparison is arithmetic over cached vectors, and CosineComparisons against FallbackComparisons reports exactly how often the paid path was actually used.

Constructors

Constructor Summary
EmbeddingCosineGenomeDistance(IEmbeddingClient client, IGenomeDistance<ProgramGenome> fallback) Initializes a cosine distance over embeddings.

Properties

Property Summary
IEmbeddingClient Client { get; } Gets the embedding client this metric primes from.
IGenomeDistance<ProgramGenome> Fallback { get; } Gets the metric used when a pair has no cached vectors.
int PrimedCount { get; } Gets how many genome vectors are currently cached.
long CosineComparisons { get; } Gets how many comparisons were answered from cached vectors.
long FallbackComparisons { get; } Gets how many comparisons fell back because a vector was missing.
long PrimeRequests { get; } Gets how many batched priming requests were issued.
string Id { get; }
string VersionHash { get; }

Methods

Method Summary
ValueTask<bool> PrimeAsync(IEnumerable<ProgramGenome> genomes, CancellationToken cancellationToken) Fetches and caches vectors for every genome not already primed, in one batched request.
bool IsPrimed(ProgramGenome genome) Reports whether a genome’s vector is cached.
double Distance(ProgramGenome first, ProgramGenome second)
double? Similarity(ProgramGenome first, ProgramGenome second) Computes the cosine similarity of two primed genomes.
void Clear() Discards every cached vector.

Fields

Field Summary
string MetricId The identifier this metric reports.