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
| Property | Type | Description |
|---|---|---|
| ID | Number | Unique identifier for the genome |
| nodeGenes | Array | Collection of nodes in the genome |
| connectionGenes | Array | Collection of connections between nodes |
| inputNodes | Array | Collection of input nodes |
| outputNodes | Array | Collection of output nodes |
| biasNode | Node | Bias node (if available) |
| fitness | Number | The genome's fitness score (higher is better) |
| populationId | String/Number | ID 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.
| Parameter | Type | Description |
|---|---|---|
| nodeGenes | Array | Array of node genes (InputNode, HiddenNode, OutputNode, BiasNode) |
| connectionGenes | Array | Array of connection genes (ConnectionGene) |
| config | Object | Configuration parameters for the genome |
| d | String/Number | ID 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.
| Parameter | Type | Description |
|---|---|---|
| inputs | Array | Array of input values for the network |
| Returns | Type | Description |
|---|---|---|
| outputs | Array | Array of output values produced by the network |
// Propagate inputs through the genomeconst 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.
| Returns | Type | Description |
|---|---|---|
| newGenome | Genome | A 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.
| Parameter | Type | Description |
|---|---|---|
| genome | Genome | The genome to compare with |
| Returns | Type | Description |
|---|---|---|
| isEqual | Boolean | True 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.
| Parameter | Type | Description |
|---|---|---|
| parent2 | Genome | The second parent genome |
| Returns | Type | Description |
|---|---|---|
| offspring | Genome | A 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.
// Evaluate the genome's fitnessgenome.evaluateFitness();// Or manually set fitnessgenome.fitness = 0.85;toJSON
genome.toJSON() → String
Converts the genome to a JSON string representation for storage or transmission.
| Returns | Type | Description |
|---|---|---|
| jsonString | String | JSON 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.