Table of Contents

Interface IGeneticAlgorithm<T, TInput, TOutput, TIndividual, TGene>

Namespace
AiDotNet.Interfaces
Assembly
AiDotNet.dll

Represents a machine learning model that uses genetic algorithms or evolutionary computation while maintaining the core capabilities of a full model.

public interface IGeneticAlgorithm<T, TInput, TOutput, TIndividual, TGene> where TIndividual : class, IEvolvable<TGene, T> where TGene : class

Type Parameters

T

The numeric type used for calculations (e.g., double, float).

TInput

The type of input data for the model.

TOutput

The type of output data produced by the model.

TIndividual

The type representing an individual in the genetic population.

TGene

The type representing a gene in the genetic model.

Remarks

This interface extends the IFullModel interface by adding genetic algorithm capabilities that allow for evolutionary optimization, including population management, crossover operations, mutation, selection, and fitness evaluation through integration with IFitnessCalculator.

For Beginners: This interface provides functionality for AI models that use genetic algorithms - methods inspired by natural evolution.

Think of a genetic model as a population of potential solutions that evolve over time:

  1. Population Management

    • The model maintains multiple candidate solutions (individuals)
    • Each individual represents a possible solution to your problem
  2. Evolution Process

    • Individuals are evaluated based on how well they solve the problem (fitness)
    • The best individuals are selected to "reproduce" (selection)
    • New individuals are created by combining parts of successful ones (crossover)
    • Random changes are introduced to maintain diversity (mutation)
    • This process repeats over many generations, with solutions improving over time
  3. Advantages

    • Can solve complex problems where traditional algorithms struggle
    • Often finds creative solutions humans might not consider
    • Good for optimization problems and symbolic regression
    • Can adapt to changing conditions and problems

This is particularly useful for problems like:

  • Finding optimal neural network architectures
  • Symbolic regression (discovering mathematical equations from data)
  • Optimizing complex systems with many parameters
  • Evolving game-playing strategies or agent behaviors

Methods

AddCrossoverOperator(string, Func<TIndividual, TIndividual, double, ICollection<TIndividual>>)

Adds a custom crossover operator.

void AddCrossoverOperator(string name, Func<TIndividual, TIndividual, double, ICollection<TIndividual>> crossoverOperator)

Parameters

name string

The name of the crossover operator.

crossoverOperator Func<TIndividual, TIndividual, double, ICollection<TIndividual>>

The crossover function.

AddMutationOperator(string, Func<TIndividual, double, TIndividual>)

Adds a custom mutation operator.

void AddMutationOperator(string name, Func<TIndividual, double, TIndividual> mutationOperator)

Parameters

name string

The name of the mutation operator.

mutationOperator Func<TIndividual, double, TIndividual>

The mutation function.

ConfigureGeneticParameters(GeneticParameters)

Configures the genetic algorithm parameters.

void ConfigureGeneticParameters(GeneticParameters parameters)

Parameters

parameters GeneticParameters

The genetic algorithm parameters to use.

CreateIndividual(ICollection<TGene>)

Creates a new individual with the specified genes.

TIndividual CreateIndividual(ICollection<TGene> genes)

Parameters

genes ICollection<TGene>

The genes to include in the individual.

Returns

TIndividual

A new individual with the specified genes.

Crossover(TIndividual, TIndividual, double)

Performs crossover between two parent individuals to produce offspring.

ICollection<TIndividual> Crossover(TIndividual parent1, TIndividual parent2, double crossoverRate)

Parameters

parent1 TIndividual

The first parent individual.

parent2 TIndividual

The second parent individual.

crossoverRate double

The probability of crossover occurring.

Returns

ICollection<TIndividual>

One or more offspring produced by crossover.

EvaluateIndividual(TIndividual, TInput, TOutput, TInput?, TOutput?)

Evaluates an individual by converting it to a model and generating evaluation data.

T EvaluateIndividual(TIndividual individual, TInput trainingInput, TOutput trainingOutput, TInput? validationInput = default, TOutput? validationOutput = default)

Parameters

individual TIndividual

The individual to evaluate.

trainingInput TInput

The input training data.

trainingOutput TOutput

The expected output for training.

validationInput TInput

Optional validation input data.

validationOutput TOutput

Optional validation output data.

Returns

T

The calculated fitness score for the individual.

Remarks

This method uses the configured IFitnessCalculator to evaluate the individual's fitness. It first converts the individual to a model, then evaluates the model against the provided data to generate evaluation metrics, which are then passed to the fitness calculator.

Evolve(int, TInput, TOutput, TInput?, TOutput?, Func<EvolutionStats<T, TInput, TOutput>, bool>?)

Evolves the population for a specified number of generations.

EvolutionStats<T, TInput, TOutput> Evolve(int generations, TInput trainingInput, TOutput trainingOutput, TInput? validationInput = default, TOutput? validationOutput = default, Func<EvolutionStats<T, TInput, TOutput>, bool>? stopCriteria = null)

Parameters

generations int

The number of generations to evolve.

trainingInput TInput

The input training data used for fitness evaluation.

trainingOutput TOutput

The expected output for training used for fitness evaluation.

validationInput TInput

Optional validation input data.

validationOutput TOutput

Optional validation output data.

stopCriteria Func<EvolutionStats<T, TInput, TOutput>, bool>

Optional function that determines when to stop evolution.

Returns

EvolutionStats<T, TInput, TOutput>

Statistics about the evolutionary process.

GetBestIndividual()

Gets the best individual from the current population.

TIndividual GetBestIndividual()

Returns

TIndividual

The individual with the highest fitness.

GetEvolutionStats(IFitnessCalculator<T, TInput, TOutput>)

Gets statistics about the current evolutionary state, including generation number, population diversity, and fitness distribution.

EvolutionStats<T, TInput, TOutput> GetEvolutionStats(IFitnessCalculator<T, TInput, TOutput> fitnessCalculator)

Parameters

fitnessCalculator IFitnessCalculator<T, TInput, TOutput>

Returns

EvolutionStats<T, TInput, TOutput>

Statistics about the current evolutionary state.

GetFitnessCalculator()

Gets the fitness calculator used to evaluate individuals.

IFitnessCalculator<T, TInput, TOutput> GetFitnessCalculator()

Returns

IFitnessCalculator<T, TInput, TOutput>

The fitness calculator instance used by this genetic model.

GetGeneticParameters()

Gets the current genetic algorithm parameters.

GeneticParameters GetGeneticParameters()

Returns

GeneticParameters

The current genetic algorithm parameters.

GetPopulation()

Gets the current population of individuals in the genetic model.

ICollection<TIndividual> GetPopulation()

Returns

ICollection<TIndividual>

A collection of individuals representing the current population.

IndividualToModel(TIndividual)

Converts an individual to a trained model that can make predictions.

IFullModel<T, TInput, TOutput> IndividualToModel(TIndividual individual)

Parameters

individual TIndividual

The individual to convert.

Returns

IFullModel<T, TInput, TOutput>

A model capable of making predictions based on the individual's genes.

InitializePopulation(int, InitializationMethod)

Initializes a new population with random individuals.

ICollection<TIndividual> InitializePopulation(int populationSize, InitializationMethod initializationMethod)

Parameters

populationSize int

The size of the population to create.

initializationMethod InitializationMethod

The method to use for initialization.

Returns

ICollection<TIndividual>

The newly created population.

LoadPopulation(string)

Loads a population from a file.

ICollection<TIndividual> LoadPopulation(string filePath)

Parameters

filePath string

The path from which to load the population.

Returns

ICollection<TIndividual>

The loaded population.

Mutate(TIndividual, double)

Applies mutation to an individual.

TIndividual Mutate(TIndividual individual, double mutationRate)

Parameters

individual TIndividual

The individual to mutate.

mutationRate double

The probability of each gene mutating.

Returns

TIndividual

The mutated individual.

SavePopulation(string)

Saves the current population to a file.

void SavePopulation(string filePath)

Parameters

filePath string

The path where the population should be saved.

Select(int, SelectionMethod)

Selects individuals from the population for reproduction.

ICollection<TIndividual> Select(int selectionSize, SelectionMethod selectionMethod)

Parameters

selectionSize int

The number of individuals to select.

selectionMethod SelectionMethod

The method to use for selection (e.g., tournament, roulette wheel).

Returns

ICollection<TIndividual>

The selected individuals.

SetFitnessCalculator(IFitnessCalculator<T, TInput, TOutput>)

Sets the fitness calculator to be used for evaluating individuals.

void SetFitnessCalculator(IFitnessCalculator<T, TInput, TOutput> fitnessCalculator)

Parameters

fitnessCalculator IFitnessCalculator<T, TInput, TOutput>

The fitness calculator to use.