GeneticBase<T, TInput, TOutput>
Base Classes · AiDotNet.Genetics
Provides a base implementation of IGeneticModel that handles common genetic algorithm operations.
Known derived types: StandardGeneticAlgorithm<T, TInput, TOutput>
For Beginners
This class provides a ready-to-use foundation for genetic algorithm models. It handles:
- Managing a population of candidate solutions
- Evolving the population through selection, crossover, and mutation
- Tracking statistics about the evolutionary process
- Saving and loading populations
When creating your own genetic model, you can inherit from this class and focus on the specific implementation details of your model type rather than reimplementing the entire genetic algorithm framework.
How It Works
This abstract base class implements the IGeneticModel interface, providing standard implementations for common genetic algorithm operations while allowing derived classes to customize behavior specific to their genetic model type.
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. |
GeneticParameters GetGeneticParameters() |
Gets the current genetic algorithm parameters. |
ICollection<ModelIndividual<T, TInput, TOutput, ModelParameterGene<T>>> Crossover(ModelIndividual<T, TInput, TOutput, ModelParameterGene<T>> parent1, ModelIndividual<T, TInput, TOutput, ModelParameterGene<T>> parent2, double crossoverRate) |
Performs crossover between two parent individuals to produce offspring. |
ICollection<ModelIndividual<T, TInput, TOutput, ModelParameterGene<T>>> GetPopulation() |
Gets the current population of individuals in the genetic model. |
ICollection<ModelIndividual<T, TInput, TOutput, ModelParameterGene<T>>> InitializePopulation(int populationSize, InitializationMethod initializationMethod) |
Initializes a new population with random individuals. |
ICollection<ModelIndividual<T, TInput, TOutput, ModelParameterGene<T>>> LoadPopulation(string filePath) |
Loads a population from a file. |
ICollection<ModelIndividual<T, TInput, TOutput, ModelParameterGene<T>>> 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(ModelIndividual<T, TInput, TOutput, ModelParameterGene<T>> individual) |
Converts an individual to a trained model that can make predictions. |
ModelIndividual<T, TInput, TOutput, ModelParameterGene<T>> CreateIndividual(ICollection<ModelParameterGene<T>> genes) |
Creates a new individual with the specified genes. |
ModelIndividual<T, TInput, TOutput, ModelParameterGene<T>> GetBestIndividual() |
Gets the best individual from the current population. |
ModelIndividual<T, TInput, TOutput, ModelParameterGene<T>> Mutate(ModelIndividual<T, TInput, TOutput, ModelParameterGene<T>> individual, double mutationRate) |
Applies mutation to an individual. |
ModelMetadata<T> GetMetaData() |
Gets the metadata for the model. |
T EvaluateIndividual(ModelIndividual<T, TInput, TOutput, ModelParameterGene<T>> individual, TInput trainingInput, TOutput trainingOutput, TInput validationInput, TOutput validationOutput) |
Evaluates an individual by converting it to a model and generating evaluation data. |
TOutput Predict(TInput input) |
Makes a prediction using the current best model. |
byte[] Serialize() |
Serializes the model to a byte array. |
void AddCrossoverOperator(string name, Func<ModelIndividual<T, TInput, TOutput, ModelParameterGene<T>>, ModelIndividual<T, TInput, TOutput, ModelParameterGene<T>>, double, ICollection<ModelIndividual<T, TInput, TOutput, ModelParameterGene<T>>>> crossoverOperator) |
Adds a custom crossover operator. |
void AddMutationOperator(string name, Func<ModelIndividual<T, TInput, TOutput, ModelParameterGene<T>>, double, ModelIndividual<T, TInput, TOutput, ModelParameterGene<T>>> mutationOperator) |
Adds a custom mutation operator. |
void ConfigureGeneticParameters(GeneticParameters parameters) |
Configures the genetic algorithm parameters. |
void Deserialize(byte[] data) |
Deserializes the model from a byte array. |
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. |