NEAT<T>

Models & Types · AiDotNet.NeuralNetworks

NeuroEvolution of Augmenting Topologies (Stanley & Miikkulainen 2002): evolves the structure and the weights of feed-forward networks with historical markings, speciation and explicit fitness sharing.

Inheritance: NeuralNetworkBase<T> → DeclaredModelLayoutBase<T> → VectorModelLayoutBase<T> → NEAT<T>

For Beginners

NEAT is a way to grow neural networks through evolution rather than training them with fixed structures. It starts with simple networks, and the networks that do better on your task are more likely to pass on their “genes”. Grouping similar networks into species gives new structures time to improve before they have to compete with everything else.

How It Works

The algorithm follows the paper:

  • Historical markings: every structural innovation gets a global innovation number; the same

innovation within one generation (the same connection split, or the same new link) gets the same numbers in every genome. Initial genomes share one set of markings.

  • Crossover: genes are lined up by innovation number; matching genes are inherited at random,

disjoint and excess genes from the fitter parent (from both when fitness is equal), and a gene disabled in either parent stays disabled with probability 0.75.

  • Speciation: compatibility distance delta = c1 E / N + c2 D / N + c3 W (c1 = c2 = 1,

c3 = 0.4, delta_t = 3); each genome joins the first species whose representative (a random member of the previous generation) is within delta_t.

  • Explicit fitness sharing: adjusted fitness is fitness divided by species size, and each

species gets offspring in proportion to its summed adjusted fitness. Species that have not improved in 15 generations do not reproduce; after 20 generations without population-wide improvement only the top two species do. The lowest-performing members are eliminated before reproduction, and the champion of every species with more than five members is copied unchanged.

  • Mutation: weights are mutated with probability 0.8 (each perturbed with probability 0.9,

otherwise replaced); add-node (0.03) splits a connection; add-connection (0.05) links two unconnected nodes without creating a cycle.

  • Activation: the steepened sigmoid 1 / (1 + e^(-4.9 x)) on every hidden and output

node, evaluated layer by layer so hidden activations propagate after squashing.

Example

var input = Tensor<float>.CreateRandom(new[] { 1, 4 });
var trainX = Tensor<float>.CreateRandom(4, 8);
var trainY = Tensor<float>.CreateRandom(4, 2);
var result = new AiModelBuilder<float, Tensor<float>, Tensor<float>>()
    .ConfigureModel(new NEAT<float>())
    .Build(trainX, trainY);
var output = result.Predict(input);

Constructors

Constructor Summary
NEAT() Initializes a new instance with default settings.
NEAT(NeuralNetworkArchitecture<T> architecture, int populationSize, ILossFunction<T> lossFunction, NEATOptions options) Initializes a new instance of the NEAT class with the specified architecture and evolution parameters.

Methods

Method Summary
Dictionary<string, Tensor<T>> GetNamedLayerActivations(Tensor<T> input) Gets named activations from the best genome’s network when processing input.
LayerInputDomain GetOutputDomain(int[] outputShape)
ModelMetadata<T> GetModelMetadata() Gets metadata about the NEAT model.
Vector<T> GetParameterGradients() NEAT evolves connection weights and does not compute gradient-descent derivatives.
bool IsReadyToPredict() Checks if the NEAT model is ready to make predictions.
void EvolvePopulation(Func<Genome<T>, T> fitnessFunction, int generations) Evolves the population over a specified number of generations using the provided fitness function.