IGeneticAlgorithm<T, TInput, TOutput, TIndividual, TGene>
Interfaces · AiDotNet.Interfaces
Represents a machine learning model that uses genetic algorithms or evolutionary computation while maintaining the core capabilities of a full model.
Known implementations: GeneticBase<T, TInput, TOutput>
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
How It Works
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.
Methods
| Method | Summary |
|---|---|
EvolutionStats<T, TInput, TOutput> Evolve(int generations, TInput trainingInput, TOutput trainingOutput, TInput validationInput, TOutput validationOutput, Func<EvolutionStats<T, TInput, TOutput>, bool> stopCriteria) |
Evolves the population for a specified number of generations. |
EvolutionStats<T, TInput, TOutput> GetEvolutionStats(IFitnessCalculator<T, TInput, TOutput> fitnessCalculator) |
Gets statistics about the current evolutionary state, including generation number, population diversity, and fitness distribution. |
GeneticParameters GetGeneticParameters() |
Gets the current genetic algorithm parameters. |
ICollection<TIndividual> Crossover(TIndividual parent1, TIndividual parent2, double crossoverRate) |
Performs crossover between two parent individuals to produce offspring. |
ICollection<TIndividual> GetPopulation() |
Gets the current population of individuals in the genetic model. |
ICollection<TIndividual> InitializePopulation(int populationSize, InitializationMethod initializationMethod) |
Initializes a new population with random individuals. |
ICollection<TIndividual> LoadPopulation(string filePath) |
Loads a population from a file. |
ICollection<TIndividual> Select(int selectionSize, SelectionMethod selectionMethod) |
Selects individuals from the population for reproduction. |
IFitnessCalculator<T, TInput, TOutput> GetFitnessCalculator() |
Gets the fitness calculator used to evaluate individuals. |
IFullModel<T, TInput, TOutput> IndividualToModel(TIndividual individual) |
Converts an individual to a trained model that can make predictions. |
T EvaluateIndividual(TIndividual individual, TInput trainingInput, TOutput trainingOutput, TInput validationInput, TOutput validationOutput) |
Evaluates an individual by converting it to a model and generating evaluation data. |
TIndividual CreateIndividual(ICollection<TGene> genes) |
Creates a new individual with the specified genes. |
TIndividual GetBestIndividual() |
Gets the best individual from the current population. |
TIndividual Mutate(TIndividual individual, double mutationRate) |
Applies mutation to an individual. |
void AddCrossoverOperator(string name, Func<TIndividual, TIndividual, double, ICollection<TIndividual>> crossoverOperator) |
Adds a custom crossover operator. |
void AddMutationOperator(string name, Func<TIndividual, double, TIndividual> mutationOperator) |
Adds a custom mutation operator. |
void ConfigureGeneticParameters(GeneticParameters parameters) |
Configures the genetic algorithm parameters. |
void SavePopulation(string filePath) |
Saves the current population to a file. |
void SetFitnessCalculator(IFitnessCalculator<T, TInput, TOutput> fitnessCalculator) |
Sets the fitness calculator to be used for evaluating individuals. |