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.

ParameterTypeDescription
configObjObjectOptional object containing configuration parameters to override defaults
javascript
// 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

ParameterTypeDefaultDescription
inputSizeNumber2Number of input nodes in the network
outputSizeNumber1Number of output nodes in the network
activationFunctionString/Object'Sigmoid'Activation function for the nodes. Can be specified as a string name from available functions or as a custom function object
weightInitializationObject{ type: 'Random', params: [-1, 1] }Strategy for initializing connection weights. Can be specified as an object with type and parameters
minWeightNumber-4.0Minimum allowed weight value for connections
maxWeightNumber4.0Maximum allowed weight value for connections
allowRecurrentConnectionsBooleantrueWhether recurrent connections are allowed in the network
recurrentConnectionRateNumber1.0Rate for forming recurrent connections when they are allowed

Bias Settings

ParameterTypeDefaultDescription
biasNumber1.0The value for bias inputs (typically 1.0)
connectBiasBooleantrueWhen true, automatically connects the bias node to all output nodes during network construction
biasModeString'WEIGHTED_NODE'Determines how bias is applied in the network. Options include:
  • WEIGHTED_NODE: Uses weights with bias (sum += biasWeight * bias)
  • DIRECT_NODE: Uses connections but not weights (sum += bias if connected)
  • CONSTANT: Traditional approach adding bias to every node (sum += bias)
  • DISABLED: Disables bias entirely

Evolution Parameters

ParameterTypeDefaultDescription
populationSizeNumber150Size of the population
generationsNumber100Maximum number of generations for evolution
fitnessFunctionString/Object'XOR'Function to evaluate genome fitness. Can be specified as a string name from available functions or as a custom function object
survivalRateNumber0.2Proportion of genomes in species that survives each generation
numOfEliteNumber10Number of elite individuals to preserve unchanged in each generation
populationStagnationLimitNumber20Maximum number of generations without fitness improvement before population is considered stagnant
interspeciesMatingRateNumber0.001Rate of mating between individuals from different species
mutateOnlyProbNumber0.25Probability of producing offspring through mutation only (without crossover)

Speciation Parameters

ParameterTypeDefaultDescription
c1Number1.0Coefficient for excess genes in compatibility distance calculation
c2Number1.0Coefficient for disjoint genes in compatibility distance calculation
c3Number0.4Coefficient for weight differences in compatibility distance calculation
compatibilityThresholdNumber3.0Maximum compatibility distance for genomes to be considered part of the same species
dropOffAgeNumber15Maximum age for a species before it is removed if no fitness improvement is observed

Mutation Parameters

ParameterTypeDefaultDescription
mutationRateNumber1.0Overall probability of applying mutation to offspring
weightMutationRateNumber0.8Probability of mutating connection weights
addConnectionMutationRateNumber0.05Probability of adding a new connection
addNodeMutationRateNumber0.03Probability of adding a new node
minPerturbNumber-0.5Minimum perturbation value when mutating weights
maxPerturbNumber0.5Maximum perturbation value when mutating weights
reinitializeWeightRateNumber0.1Chance of reinitializing a weight when weight mutation is selected (each weight is evaluated individually)
keepDisabledOnCrossOverRateNumber0.75Probability of keeping connections disabled during crossover if they are disabled in either parent

Available Built-in Functions

Activation Functions

NameDescription
'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

NameParametersDescription
'Random'[min, max]Initializes weights randomly within the specified range

Fitness Functions

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

Using Custom Functions

Custom Activation Function

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

Custom Fitness Function

javascript
// 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)
});