Configuration

The Config class provides a centralized way to configure all aspects of the NEAT algorithm. It manages parameters for network structure, evolution, speciation, and mutation with sensible defaults that can be overridden as needed.

Constructor

new Config(configObj = {})

Creates a new configuration object with default values that can be overridden by the provided configuration object.

Parameter Type Description
configObj Object Optional object containing configuration parameters to override defaults
// Create a configuration with default values const config = new Config(); // Create a configuration with custom values const customConfig = new Config({ inputSize: 4, outputSize: 2, activationFunction: 'ReLU', populationSize: 200, allowRecurrentConnections: false }); // Set values explicitly customConfig.generations = 100;

Configuration Parameters

Network Structure

Parameter Type Default Description
inputSize Number 2 Number of input nodes in the network
outputSize Number 1 Number of output nodes in the network
activationFunction String/Object 'Sigmoid' Activation function for the nodes. Can be specified as a string name from available functions or as a custom function object
weightInitialization Object { type: 'Random', params: [-1, 1] } Strategy for initializing connection weights. Can be specified as an object with type and parameters
minWeight Number -4.0 Minimum allowed weight value for connections
maxWeight Number 4.0 Maximum allowed weight value for connections
allowRecurrentConnections Boolean true Whether recurrent connections are allowed in the network
recurrentConnectionRate Number 1.0 Rate for forming recurrent connections when they are allowed
useBias Boolean true Enables or disables the use of bias nodes in the network
bias Number 1.0 The value for bias inputs (typically 1.0)
useBiasWeights Boolean true When true, applies weights to bias connections (bias*weight); when false, adds bias directly (+bias)
connectBias Boolean true When true, automatically connects the bias node to all output nodes during network construction
addBiasEverywhere Boolean false When true, adds bias to every node's activation function (standard neural network approach)

Evolution Parameters

Parameter Type Default Description
populationSize Number 150 Size of the population
generations Number 100 Maximum number of generations for evolution
fitnessFunction String/Object 'XOR' Function to evaluate genome fitness. Can be specified as a string name from available functions or as a custom function object
survivalRate Number 0.2 Proportion of population that survives each generation
numOfElite Number 10 Number of elite individuals to preserve unchanged in each generation
populationStagnationLimit Number 20 Maximum number of generations without fitness improvement before population is considered stagnant
interspeciesMatingRate Number 0.001 Rate of mating between individuals from different species
mutateOnlyProb Number 0.25 Probability of producing offspring through mutation only (without crossover)

Speciation Parameters

Parameter Type Default Description
c1 Number 1.0 Coefficient for excess genes in compatibility distance calculation
c2 Number 1.0 Coefficient for disjoint genes in compatibility distance calculation
c3 Number 0.4 Coefficient for weight differences in compatibility distance calculation
compatibilityThreshold Number 3.0 Maximum compatibility distance for genomes to be considered part of the same species
dropOffAge Number 15 Maximum age for a species before it is removed if no fitness improvement is observed

Mutation Parameters

Parameter Type Default Description
mutationRate Number 1.0 Overall probability of applying mutation to offspring
weightMutationRate Number 0.8 Probability of mutating connection weights
addConnectionMutationRate Number 0.05 Probability of adding a new connection
addNodeMutationRate Number 0.03 Probability of adding a new node
perturbRange Number 0.5 Range for weight perturbation when mutating weights
keepDisabledOnCrossOverRate Number 0.75 Probability of inheriting disabled genes during crossover

Available Built-in Functions

Activation Functions

Name Description
'Sigmoid' Standard sigmoid function: f(x) = 1 / (1 + e^(-x))
'NEATSigmoid' Modified sigmoid function as used in the original NEAT paper
'Tanh' Hyperbolic tangent: f(x) = tanh(x)
'ReLU' Rectified Linear Unit: f(x) = max(0, x)
'LeakyReLU' Leaky Rectified Linear Unit: f(x) = x if x > 0, else αx
'Gaussian' Gaussian function: f(x) = e^(-x²)

Weight Initializations

Name Parameters Description
'Random' [min, max] Initializes weights randomly within the specified range

Fitness Functions

Name Description
'XOR' Evaluates fitness based on the XOR problem (built-in test case)

Using Custom Functions

Custom Activation Function

// Create a custom activation function class CustomActivation { constructor(param = 1.0) { this.param = param; } activate(x) { return Math.sin(x * this.param); } derivative(x) { return this.param * Math.cos(x * this.param); } } // Use the custom activation function in configuration const config = new Config({ activationFunction: new CustomActivation(0.5) });

Custom Fitness Function

// Create a custom fitness function class CartPoleFunction { constructor(simulationSteps = 500) { this.simulationSteps = simulationSteps; } calculateFitness(genome) { let fitness = 0; // Run cart-pole simulation with the genome // ... return fitness; } } // Use the custom fitness function in configuration const config = new Config({ inputSize: 4, // Cart position, velocity, pole angle, pole velocity outputSize: 1, // Force direction fitnessFunction: new CartPoleFunction(1000) });