Table of Contents

Class GeneticAlgorithmOptimizer<T, TInput, TOutput>

Namespace
AiDotNet.Optimizers
Assembly
AiDotNet.dll

Represents a Genetic Algorithm optimizer for machine learning models.

public class GeneticAlgorithmOptimizer<T, TInput, TOutput> : OptimizerBase<T, TInput, TOutput>, IOptimizer<T, TInput, TOutput>, IModelSerializer

Type Parameters

T

The numeric type used for calculations, typically float or double.

TInput
TOutput
Inheritance
OptimizerBase<T, TInput, TOutput>
GeneticAlgorithmOptimizer<T, TInput, TOutput>
Implements
IOptimizer<T, TInput, TOutput>
Inherited Members
Extension Methods

Remarks

The Genetic Algorithm optimizer is an evolutionary optimization technique inspired by the process of natural selection. It evolves a population of potential solutions over multiple generations to find an optimal or near-optimal solution.

For Beginners: Think of the Genetic Algorithm optimizer like breeding the best solutions:

  • Start with a group of random solutions (like a group of different recipes)
  • Test how good each solution is (like tasting each recipe)
  • Choose the best solutions (like picking the tastiest recipes)
  • Create new solutions by mixing the best ones (like combining ingredients from the best recipes)
  • Sometimes make small random changes (like accidentally adding a new spice)
  • Repeat this process many times to find the best solution (or the tastiest recipe!)

This approach is good at finding solutions for complex problems where traditional methods might struggle.

Constructors

GeneticAlgorithmOptimizer(IFullModel<T, TInput, TOutput>?, GeneticAlgorithmOptimizerOptions<T, TInput, TOutput>?, GeneticBase<T, TInput, TOutput>?, IFitnessCalculator<T, TInput, TOutput>?, IModelEvaluator<T, TInput, TOutput>?)

Initializes a new instance of the GeneticAlgorithmOptimizer class.

public GeneticAlgorithmOptimizer(IFullModel<T, TInput, TOutput>? model, GeneticAlgorithmOptimizerOptions<T, TInput, TOutput>? options = null, GeneticBase<T, TInput, TOutput>? geneticAlgorithm = null, IFitnessCalculator<T, TInput, TOutput>? fitnessCalculator = null, IModelEvaluator<T, TInput, TOutput>? modelEvaluator = null)

Parameters

model IFullModel<T, TInput, TOutput>

The model to be optimized.

options GeneticAlgorithmOptimizerOptions<T, TInput, TOutput>

The options for configuring the genetic algorithm.

geneticAlgorithm GeneticBase<T, TInput, TOutput>
fitnessCalculator IFitnessCalculator<T, TInput, TOutput>
modelEvaluator IModelEvaluator<T, TInput, TOutput>

Remarks

For Beginners: This sets up the genetic algorithm with its initial settings. You can customize various aspects of how it works, or use default settings if you're unsure.

Methods

Deserialize(byte[])

Deserializes the genetic algorithm optimizer from a byte array.

public override void Deserialize(byte[] data)

Parameters

data byte[]

The byte array containing the serialized data of the optimizer.

Remarks

For Beginners: This method recreates the genetic algorithm optimizer from previously saved data. It's like using your written notes to set up your cooking competition exactly as it was before.

Exceptions

InvalidOperationException

Thrown when deserialization of the optimizer options fails.

GetOptions()

Gets the current options for the genetic algorithm optimizer.

public override OptimizationAlgorithmOptions<T, TInput, TOutput> GetOptions()

Returns

OptimizationAlgorithmOptions<T, TInput, TOutput>

The current genetic algorithm optimizer options.

Remarks

For Beginners: This method returns the current settings of the genetic algorithm. It's like checking the current rules of your cooking competition.

Optimize(OptimizationInputData<T, TInput, TOutput>)

Performs the main optimization process using the genetic algorithm.

public override OptimizationResult<T, TInput, TOutput> Optimize(OptimizationInputData<T, TInput, TOutput> inputData)

Parameters

inputData OptimizationInputData<T, TInput, TOutput>

The input data for the optimization process.

Returns

OptimizationResult<T, TInput, TOutput>

The result of the optimization process.

Remarks

For Beginners: This is the heart of the genetic algorithm. It: 1. Creates an initial group of random solutions 2. Evaluates how good each solution is 3. Selects the best solutions 4. Creates new solutions by mixing the best ones 5. Sometimes makes small random changes to solutions 6. Repeats this process for many generations

It's like running a cooking competition where each round you keep the best recipes, combine them to make new recipes, and occasionally add a surprise ingredient.

Serialize()

Serializes the genetic algorithm optimizer to a byte array.

public override byte[] Serialize()

Returns

byte[]

A byte array containing the serialized data of the optimizer.

Remarks

For Beginners: This method saves all the important information about the current state of the genetic algorithm into a format that can be easily stored or transmitted. It's like writing down all the details of your cooking competition so you can recreate it later.

UpdateAdaptiveParameters(OptimizationStepData<T, TInput, TOutput>, OptimizationStepData<T, TInput, TOutput>)

Updates the adaptive parameters used in the genetic algorithm.

protected override void UpdateAdaptiveParameters(OptimizationStepData<T, TInput, TOutput> currentStepData, OptimizationStepData<T, TInput, TOutput> previousStepData)

Parameters

currentStepData OptimizationStepData<T, TInput, TOutput>

Data from the current optimization step.

previousStepData OptimizationStepData<T, TInput, TOutput>

Data from the previous optimization step.

Remarks

For Beginners: This method adjusts how the algorithm behaves based on its recent performance. It's like a chef adjusting their cooking technique based on how the last few dishes turned out.

UpdateOptions(OptimizationAlgorithmOptions<T, TInput, TOutput>)

Updates the options for the genetic algorithm optimizer.

protected override void UpdateOptions(OptimizationAlgorithmOptions<T, TInput, TOutput> options)

Parameters

options OptimizationAlgorithmOptions<T, TInput, TOutput>

The new options to apply to the optimizer.

Remarks

For Beginners: This method allows you to change the settings of the genetic algorithm while it's running. It's like adjusting the rules of your cooking competition mid-way through.

Exceptions

ArgumentException

Thrown when the provided options are not of the correct type.