DelegateProgramFitnessEvaluator

Models & Types · AiDotNet.Evolution.Programs

Adapts a caller-supplied function into an IProgramFitnessEvaluator.

Implements: IProgramFitnessEvaluator

For Beginners

This is the quickest way to plug your own scoring rule into program evolution: hand it a small function that takes a candidate program and returns a score, and you have a working evaluator. Give it a name and a version string, and remember to change the version whenever you change the way you score, so old saved runs are not resumed with the new rules by accident.

How It Works

Most first evaluators are a handful of lines: compile the candidate, run one benchmark, return a number. This adapter removes the ceremony of declaring a class for that, while still requiring the two pieces of identity the engine needs — a stable Id and a VersionHash that changes when the scoring rule changes — so a checkpoint cannot be resumed against a silently different evaluator.

The delegate is invoked exactly once per evaluation attempt and its result is returned unchanged. Exceptions escaping the delegate are not caught here; the engine converts them into a redacted diagnostic and applies its failure policy, so a delegate that wants a retryable failure should return Failed instead of throwing.

Constructors

Constructor Summary
DelegateProgramFitnessEvaluator(Func<ProgramGenome, EvolutionEvaluationContext, CancellationToken, ValueTask<EvolutionTaskResult>> evaluate, string id, string versionHash) Initializes an evaluator backed by an asynchronous function.
DelegateProgramFitnessEvaluator(Func<ProgramGenome, double> score, string id, string versionHash, EvolutionOptimizationDirection direction) Initializes an evaluator backed by a synchronous scoring function.

Properties

Property Summary
string Id { get; }
string VersionHash { get; }

Methods

Method Summary
ValueTask<EvolutionTaskResult> EvaluateAsync(ProgramGenome candidate, EvolutionEvaluationContext context, CancellationToken cancellationToken)