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
TThe numeric type used for calculations (e.g., double, float).
TInputThe type of input data for the model.
TOutputThe type of output data produced by the model.
TIndividualThe type representing an individual in the genetic population.
TGeneThe 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:
Population Management
- The model maintains multiple candidate solutions (individuals)
- Each individual represents a possible solution to your problem
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
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
namestringThe name of the crossover operator.
crossoverOperatorFunc<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
namestringThe name of the mutation operator.
mutationOperatorFunc<TIndividual, double, TIndividual>The mutation function.
ConfigureGeneticParameters(GeneticParameters)
Configures the genetic algorithm parameters.
void ConfigureGeneticParameters(GeneticParameters parameters)
Parameters
parametersGeneticParametersThe genetic algorithm parameters to use.
CreateIndividual(ICollection<TGene>)
Creates a new individual with the specified genes.
TIndividual CreateIndividual(ICollection<TGene> genes)
Parameters
genesICollection<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
parent1TIndividualThe first parent individual.
parent2TIndividualThe second parent individual.
crossoverRatedoubleThe 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
individualTIndividualThe individual to evaluate.
trainingInputTInputThe input training data.
trainingOutputTOutputThe expected output for training.
validationInputTInputOptional validation input data.
validationOutputTOutputOptional 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
generationsintThe number of generations to evolve.
trainingInputTInputThe input training data used for fitness evaluation.
trainingOutputTOutputThe expected output for training used for fitness evaluation.
validationInputTInputOptional validation input data.
validationOutputTOutputOptional validation output data.
stopCriteriaFunc<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
fitnessCalculatorIFitnessCalculator<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
individualTIndividualThe 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
populationSizeintThe size of the population to create.
initializationMethodInitializationMethodThe 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
filePathstringThe 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
individualTIndividualThe individual to mutate.
mutationRatedoubleThe probability of each gene mutating.
Returns
- TIndividual
The mutated individual.
SavePopulation(string)
Saves the current population to a file.
void SavePopulation(string filePath)
Parameters
filePathstringThe 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
selectionSizeintThe number of individuals to select.
selectionMethodSelectionMethodThe 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
fitnessCalculatorIFitnessCalculator<T, TInput, TOutput>The fitness calculator to use.