Genome

The Genome class is the core genetic representation in NEAT. It encodes the structure and parameters of a neural network, including nodes (neurons) and connections between them. Each genome can be expressed as a neural network for evaluation, and can undergo genetic operations like mutation and crossover.

Properties

PropertyTypeDescription
IDNumberUnique identifier for the genome
nodeGenesArrayCollection of nodes in the genome
connectionGenesArrayCollection of connections between nodes
inputNodesArrayCollection of input nodes
outputNodesArrayCollection of output nodes
biasNodeNodeBias node (if available)
fitnessNumberThe genome's fitness score (higher is better)
populationIdString/NumberID of the population this genome belongs to

Constructor

new Genome(nodeGenes, connectionGenes, config, populationId)

Creates a new genome with the specified nodes, connections, configuration, and population ID.

ParameterTypeDescription
nodeGenesArrayArray of node genes (InputNode, HiddenNode, OutputNode, BiasNode)
connectionGenesArrayArray of connection genes (ConnectionGene)
configObjectConfiguration parameters for the genome
dString/NumberID of the population this genome belongs to

Methods

propagate

genome.propagate(inputs) → Array

Activates the neural network represented by the genome with the given inputs and returns the outputs.

ParameterTypeDescription
inputsArrayArray of input values for the network
ReturnsTypeDescription
outputsArrayArray of output values produced by the network
javascript
// Propagate inputs through the genome
const outputs = genome.propagate([0.5, 0.7]);
console.log(outputs); // [output value]

resetState

genome.resetState() → void

Resets the internal state of all nodes in the network. This is particularly useful when working with recurrent networks.

mutate

genome.mutate() → void

Applies random mutations to the genome according to the rates defined in the configuration. Mutations can include weight changes, adding connections, or adding nodes.

mutateWeights

genome.mutateWeights() → void

Mutates the weights of existing connections. Each connection's weight may be either perturbed or completely reinitialized according to the configuration parameters.

mutateAddConnection

genome.mutateAddConnection() → void

Attempts to add a new connection between two existing nodes. Checks for existing connections and potential recursion before adding.

mutateAddNode

genome.mutateAddNode() → void

Adds a new hidden node by splitting an existing connection. The original connection is disabled, and two new connections are created.

reinitializeWeights

genome.reinitializeWeights() → void

Reinitializes all connection weights according to the weight initialization method in the configuration.

copy

genome.copy() → Genome

Creates a deep copy of the genome with the same structure and weights but as a separate object.

ReturnsTypeDescription
newGenomeGenomeA new genome identical to the original

equalsGenome

genome.equalsGenome(genome) → Boolean

Compares this genome with another to check if they are structurally and parametrically identical.

ParameterTypeDescription
genomeGenomeThe genome to compare with
ReturnsTypeDescription
isEqualBooleanTrue if the genomes are identical, false otherwise

crossover

genome.crossover(parent2) → Genome

Performs crossover between this genome and another parent genome to create an offspring. Genes are inherited from both parents according to their fitness values.

ParameterTypeDescription
parent2GenomeThe second parent genome
ReturnsTypeDescription
offspringGenomeA new genome created from the genetic material of both parents

evaluateFitness

genome.evaluateFitness() → void

Evaluates the genome's fitness using the fitness function specified in the configuration. If no fitness function is provided, fitness must be assigned manually.

javascript
// Evaluate the genome's fitness
genome.evaluateFitness();
// Or manually set fitness
genome.fitness = 0.85;

toJSON

genome.toJSON() → String

Converts the genome to a JSON string representation for storage or transmission.

ReturnsTypeDescription
jsonStringStringJSON string representation of the genome

prune

genome.prune() → void

Removes disconnected hidden nodes and disabled connections from the genome to optimize its structure. This can be useful after learning a given task to minimize the network size.