Table of Contents

Class GeneticAlgorithmRegression<T>

Namespace
AiDotNet.Regression
Assembly
AiDotNet.dll

Implements a regression model that uses genetic algorithms to optimize model parameters, mimicking the process of natural selection to find the best solution.

public class GeneticAlgorithmRegression<T> : RegressionBase<T>, IRegression<T>, IFullModel<T, Matrix<T>, Vector<T>>, IModel<Matrix<T>, Vector<T>, ModelMetadata<T>>, IModelSerializer, ICheckpointableModel, IParameterizable<T, Matrix<T>, Vector<T>>, IFeatureAware, IFeatureImportance<T>, ICloneable<IFullModel<T, Matrix<T>, Vector<T>>>, IGradientComputable<T, Matrix<T>, Vector<T>>, IJitCompilable<T>

Type Parameters

T

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

Inheritance
GeneticAlgorithmRegression<T>
Implements
IFullModel<T, Matrix<T>, Vector<T>>
IModel<Matrix<T>, Vector<T>, ModelMetadata<T>>
IParameterizable<T, Matrix<T>, Vector<T>>
ICloneable<IFullModel<T, Matrix<T>, Vector<T>>>
IGradientComputable<T, Matrix<T>, Vector<T>>
Inherited Members
Extension Methods

Remarks

Genetic Algorithm Regression uses evolutionary principles to find optimal model coefficients. It maintains a population of potential solutions (models) that evolve over generations through selection, crossover, and mutation operations. This approach is particularly useful for complex problems where traditional optimization methods might struggle, as it can effectively explore large solution spaces and avoid local optima.

For Beginners: This model uses a technique inspired by natural evolution to find the best solution.

Think of it like breeding the best solution:

  • Start with a random "population" of potential solutions (different sets of coefficients)
  • Test how well each solution performs on your data (fitness evaluation)
  • Keep the best solutions and let them "reproduce" to create new solutions
  • Occasionally introduce random changes (mutations) to explore new possibilities
  • Repeat this process over multiple "generations" until you find an excellent solution

The benefit of this approach is that it can find good solutions to complex problems without getting stuck in suboptimal answers. It's similar to how nature evolves successful organisms over time, but applied to finding the best mathematical model.

Constructors

GeneticAlgorithmRegression(RegressionOptions<T>?, GeneticAlgorithmOptimizerOptions<T, Matrix<T>, Vector<T>>?, IRegularization<T, Matrix<T>, Vector<T>>?, INormalizer<T, Matrix<T>, Vector<T>>?, IFeatureSelector<T, Matrix<T>>?, IOutlierRemoval<T, Matrix<T>, Vector<T>>?, IDataPreprocessor<T, Matrix<T>, Vector<T>>?)

Initializes a new instance of the GeneticAlgorithmRegression<T> class.

public GeneticAlgorithmRegression(RegressionOptions<T>? options = null, GeneticAlgorithmOptimizerOptions<T, Matrix<T>, Vector<T>>? gaOptions = null, IRegularization<T, Matrix<T>, Vector<T>>? regularization = null, INormalizer<T, Matrix<T>, Vector<T>>? normalizer = null, IFeatureSelector<T, Matrix<T>>? featureSelector = null, IOutlierRemoval<T, Matrix<T>, Vector<T>>? outlierRemoval = null, IDataPreprocessor<T, Matrix<T>, Vector<T>>? dataPreprocessor = null)

Parameters

options RegressionOptions<T>

Optional regression options for the model.

gaOptions GeneticAlgorithmOptimizerOptions<T, Matrix<T>, Vector<T>>

Optional configuration options for the genetic algorithm optimizer.

regularization IRegularization<T, Matrix<T>, Vector<T>>

Optional regularization strategy to prevent overfitting.

normalizer INormalizer<T, Matrix<T>, Vector<T>>

Optional component for normalizing feature values.

featureSelector IFeatureSelector<T, Matrix<T>>

Optional component for selecting relevant features.

outlierRemoval IOutlierRemoval<T, Matrix<T>, Vector<T>>

Optional component for removing outliers.

dataPreprocessor IDataPreprocessor<T, Matrix<T>, Vector<T>>

Optional component for preprocessing data.

Remarks

This constructor creates a new Genetic Algorithm Regression model with the specified components and configuration options. If components are not provided, default implementations are used. The constructor sets up all the necessary infrastructure for the genetic algorithm to optimize model parameters.

For Beginners: This is how you create a new Genetic Algorithm Regression model.

The constructor allows you to customize many aspects of the model:

  • General regression settings (like whether to include an intercept term)
  • Genetic algorithm settings (like population size and mutation rate)
  • How to measure how well solutions perform (fitness calculation)
  • How to prepare your data before training (normalization, feature selection, outlier removal)

If you don't specify these parameters, the model will use reasonable default settings.

Example:

// Create a basic model with default settings
var gaRegression = new GeneticAlgorithmRegression<double>();

// Create a model with custom genetic algorithm settings
var gaOptions = new GeneticAlgorithmOptimizerOptions {
    PopulationSize = 200,
    MaxGenerations = 100,
    MutationRate = 0.05
};
var customGaRegression = new GeneticAlgorithmRegression<double>(gaOptions: gaOptions);

Methods

CreateNewInstance()

Creates a new instance of the GeneticAlgorithmRegression with the same configuration as the current instance.

protected override IFullModel<T, Matrix<T>, Vector<T>> CreateNewInstance()

Returns

IFullModel<T, Matrix<T>, Vector<T>>

A new GeneticAlgorithmRegression instance with the same options and components as the current instance.

Remarks

This method creates a new instance of the GeneticAlgorithmRegression model with the same configuration options, regularization settings, and preprocessing components as the current instance. This is useful for model cloning, ensemble methods, or cross-validation scenarios where multiple instances of the same model with identical configurations are needed.

For Beginners: This method creates a fresh copy of the model's blueprint.

When you need multiple versions of the same type of model with identical settings:

  • This method creates a new, empty model with the same configuration
  • It's like making a copy of a recipe before you start cooking
  • The new model has the same settings but no trained data
  • This is useful for techniques that need multiple models, like cross-validation

For example, when testing your model on different subsets of data, you'd want each test to use a model with identical settings.

Deserialize(byte[])

Loads a previously serialized Genetic Algorithm Regression model from a byte array.

public override void Deserialize(byte[] modelData)

Parameters

modelData byte[]

The byte array containing the serialized model.

Remarks

This method reconstructs a Genetic Algorithm Regression model from a byte array that was previously created using the Serialize method. It restores the base regression model data, the best model coefficients found by the genetic algorithm, and the genetic algorithm configuration options, allowing the model to be used for predictions without retraining.

For Beginners: This method loads a previously saved model from a sequence of bytes.

Deserialization allows you to:

  • Load a model that was saved earlier
  • Use a model without having to retrain it
  • Share models between different applications

When you deserialize a model:

  • All settings are restored
  • The best solution found by the genetic algorithm is recovered
  • The model is ready to make predictions immediately

Example:

// Load from a file
byte[] modelData = File.ReadAllBytes("gaRegression.model");

// Deserialize the model
var gaRegression = new GeneticAlgorithmRegression<double>();
gaRegression.Deserialize(modelData);

// Now you can use the model for predictions
var predictions = gaRegression.Predict(newFeatures);

GetModelType()

Gets the model type of the Genetic Algorithm Regression model.

protected override ModelType GetModelType()

Returns

ModelType

The model type enumeration value.

Predict(Matrix<T>)

Predicts target values for the provided input features using the trained Genetic Algorithm Regression model.

public override Vector<T> Predict(Matrix<T> x)

Parameters

x Matrix<T>

A matrix where each row represents a sample to predict and each column represents a feature.

Returns

Vector<T>

A vector of predicted values corresponding to each input sample.

Remarks

This method predicts target values for new input data using the best model found during the genetic algorithm optimization process. It applies the learned coefficients to the input features to compute the predictions.

For Beginners: This method uses your trained model to make predictions on new data.

Once your model has been trained, you can use it to predict values for new data points. The model applies the best set of coefficients discovered by the genetic algorithm to calculate predicted values for each input sample.

Example:

// Make predictions
var predictions = gaRegression.Predict(newFeatures);

Serialize()

Serializes the Genetic Algorithm Regression model to a byte array for storage or transmission.

public override byte[] Serialize()

Returns

byte[]

A byte array containing the serialized model.

Remarks

This method converts the Genetic Algorithm Regression model into a byte array that can be stored in a file, database, or transmitted over a network. The serialized data includes the base regression model data, the best model coefficients found by the genetic algorithm, and the genetic algorithm configuration options.

For Beginners: This method saves your trained model as a sequence of bytes.

Serialization allows you to:

  • Save your model to a file
  • Store your model in a database
  • Send your model over a network
  • Keep your model for later use without having to retrain it

The serialized data includes:

  • The model coefficients discovered by the genetic algorithm
  • Settings like population size and mutation rate
  • Other information needed to recreate the exact same model

Example:

// Serialize the model
byte[] modelData = gaRegression.Serialize();

// Save to a file
File.WriteAllBytes("gaRegression.model", modelData);

Train(Matrix<T>, Vector<T>)

Trains the Genetic Algorithm Regression model using the provided input features and target values.

public override void Train(Matrix<T> x, Vector<T> y)

Parameters

x Matrix<T>

A matrix where each row represents a sample and each column represents a feature.

y Vector<T>

A vector of target values corresponding to each sample in x.

Remarks

This method trains the Genetic Algorithm Regression model by first preprocessing the data, then splitting it into training, validation, and test sets, and finally using a genetic algorithm to find the optimal model parameters. The genetic algorithm evolves a population of potential solutions over multiple generations, gradually improving the model's fit to the training data.

For Beginners: This method teaches the model how to make predictions using your data.

The training process involves several steps:

  1. Preprocessing the data (normalizing features, removing outliers, etc.)
  2. Splitting the data into separate sets for training and testing
  3. Running the genetic algorithm, which:
    • Creates a starting population of random solutions
    • Evaluates how well each solution performs
    • Selects the best solutions to "reproduce"
    • Creates new solutions through crossover and mutation
    • Repeats this process over multiple generations
  4. Selects the best performing solution as the final model

After training, the model will be ready to make predictions on new data.

Example:

// Train the model
gaRegression.Train(features, targets);