ModelIndividual<T, TInput, TOutput, TGene>

Models & Types · AiDotNet.Genetics

Represents an individual that is also a full model, allowing direct evolution of models without conversion between individuals and models.

Implements: IEvolvable<TGene, T>, IFullModel<T, TInput, TOutput>

For Beginners

This class combines the functionality of an individual in a genetic algorithm with a machine learning model. This means:

  • You can evolve the model directly without converting between different representations
  • The individual can make predictions like any other model
  • It simplifies the implementation of genetic algorithms for model optimization

Use this when you want to directly evolve machine learning models using genetic algorithms.

How It Works

This class implements both IEvolvable and IFullModel interfaces, allowing it to be used directly in genetic algorithms while also providing model prediction capabilities.

Example

var features = new Matrix<double>(new double[,] { { 1.0, 2.0 }, { 3.0, 4.0 }, { 5.0, 6.0 }, { 7.0, 8.0 } });
var targets = new Vector<double>(new double[] { 3.1, 7.2, 11.0, 15.1 });
            
// A genetic individual wrapping a model, built from its genes by a factory.
var genes = new List<RealGene> { new(0.5), new(1.2), new(-0.3) };
Func<ICollection<RealGene>, IFullModel<double, Matrix<double>, Vector<double>>> factory =
    g => new VectorModel<double>(new Vector<double>(g.Select(x => x.Value).ToArray()));
            
var result = new AiModelBuilder<double, Matrix<double>, Vector<double>>()
    .ConfigureModel(new ModelIndividual<double, Matrix<double>, Vector<double>, RealGene>(genes, factory))
    .Build(features, targets);
            
var inputMatrix = new Matrix<double>(new double[,] { { 9.0, 10.0 } });
Vector<double> prediction = result.Predict(inputMatrix);

Constructors

Constructor Summary
ModelIndividual(ICollection<TGene> genes, Func<ICollection<TGene>, IFullModel<T, TInput, TOutput>> modelFactory) Creates a new model individual with the specified genes and model factory.
ModelIndividual(IFullModel<T, TInput, TOutput> model, ICollection<TGene> genes, Func<ICollection<TGene>, IFullModel<T, TInput, TOutput>> modelFactory) Creates a new model individual by wrapping an existing model.

Properties

Property Summary
ILossFunction<T> DefaultLossFunction { get; } Gets the default loss function for gradient computation by delegating to the inner model.
bool SupportsParameterInitialization { get; }
long ParameterCount { get; }

Methods

Method Summary
Dictionary<string, T> GetFeatureImportance()
ICollection<TGene> GetGenes() Gets the genes of this individual.
IEnumerable<int> GetActiveFeatureIndices()
IEvolvable<TGene, T> Clone() Creates a deep clone of this individual.
IFullModel<T, TInput, TOutput> DeepCopy()
IFullModel<T, TInput, TOutput> WithParameters(Vector<T> parameters) Creates a new model with the specified parameters.
ModelMetadata<T> GetMetaData() Gets the metadata for the model.
ModelMetadata<T> GetModelMetadata()
T GetFitness() Gets the fitness of this individual.
TOutput Predict(TInput input) Makes a prediction using the inner model.
Vector<T> ComputeGradients(TInput input, TOutput target, ILossFunction<T> lossFunction) Computes gradients by delegating to the inner model.
Vector<T> GetParameters() Gets the parameters of the model.
Vector<T> SanitizeParameters(Vector<T> parameters)
bool IsFeatureUsed(int featureIndex)
byte[] Serialize() Serializes the model to a byte array.
void ApplyGradients(Vector<T> gradients, T learningRate) Applies gradients by delegating to the inner model.
void Deserialize(byte[] data) Deserializes the model from a byte array.
void Dispose()
void LoadModel(string filePath)
void LoadState(Stream stream) Loads the model’s state from a stream.
void SaveModel(string filePath)
void SaveState(Stream stream) Saves the model’s current state to a stream.
void SetActiveFeatureIndices(IEnumerable<int> featureIndices)
void SetFitness(T fitness) Sets the fitness of this individual.
void SetGenes(ICollection<TGene> genes) Sets the genes of this individual and updates the inner model.
void SetParameters(Vector<T> parameters)
void Train(TInput input, TOutput expectedOutput)
void UpdateParameters(Vector<T> parameters) Updates the parameters of the model.