Jido.Evolve.Fitness behaviour (Jido Evolve v1.0.0)

Copy Markdown View Source

Behaviour for fitness evaluation functions.

Fitness functions determine how well an entity performs against the optimization criteria.

Summary

Callbacks

Batch evaluate multiple entities for efficiency.

Compare two entities directly without computing scores.

Evaluate a single entity's fitness.

Functions

Shared batch evaluation implementation.

Extract score from evaluate/2 result, raising on invalid format.

Types

context()

@type context() :: map()

entity()

@type entity() :: term()

eval_result()

@type eval_result() :: {:ok, score()} | {:ok, score_map()} | {:error, term()}

metadata()

@type metadata() :: map()

score()

@type score() :: float()

score_map()

@type score_map() :: %{:score => score(), optional(:metadata) => metadata()}

Callbacks

batch_evaluate(list, context)

(optional)
@callback batch_evaluate([entity()], context()) ::
  {:ok, [{entity(), score()}]} | {:error, term()}

Batch evaluate multiple entities for efficiency.

Default implementation calls evaluate/2 for each entity. Override for performance when batch evaluation is available.

compare(entity, entity, context)

(optional)
@callback compare(entity(), entity(), context()) :: :better | :worse | :equal

Compare two entities directly without computing scores.

This can be more efficient than scoring both entities when only relative fitness matters.

evaluate(entity, context)

@callback evaluate(entity(), context()) :: eval_result()

Evaluate a single entity's fitness.

Returns either a simple score or a score with metadata. Higher scores indicate better fitness.

Examples

def evaluate(text, _context) do
  similarity = String.jaro_distance(text, @target)
  {:ok, similarity}
end

def evaluate(text, _context) do
  score = String.jaro_distance(text, @target)
  {:ok, %{score: score, metadata: %{length: String.length(text)}}}
end

Functions

batch_evaluate(mod, entities, context)

@spec batch_evaluate(module(), [entity()], context()) :: {:ok, [{entity(), score()}]}

Shared batch evaluation implementation.

Evaluates multiple entities using the provided module's evaluate/2 callback.

score_or_raise!(other)

@spec score_or_raise!(eval_result()) :: score()

Extract score from evaluate/2 result, raising on invalid format.

Handles both simple scores and score maps with optional metadata.