Skip to content

Commit 79f49b1

Browse files
Always set new node bias to zero to preserve the original NEAT paper concept of keeping the change almost neutral.
1 parent 69a5051 commit 79f49b1

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

neat/genome.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,13 @@ def mutate_add_node(self, config):
386386
conn_to_split = choice(list(self.connections.values()))
387387
new_node_id = config.get_new_node_key(self.nodes)
388388
ng = self.create_node(config, new_node_id)
389+
390+
# Make the new node as neutral as possible with respect to the
391+
# existing connection: start with zero bias regardless of the
392+
# global bias initialization distribution.
393+
if hasattr(ng, "bias"):
394+
ng.bias = 0.0
395+
389396
self.nodes[new_node_id] = ng
390397

391398
# Disable this connection and create two new connections joining its nodes via

tests/test_genome_mutations.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,30 @@ def test_add_node_preserves_weight_path(self):
246246
self.assertEqual(incoming_conn.weight, 1.0,
247247
"Incoming connection should have weight 1.0")
248248

249+
def test_add_node_initial_bias_zero(self):
250+
"""Test that newly added nodes start with zero bias."""
251+
genome = self.create_minimal_genome()
252+
253+
if not genome.connections:
254+
genome.mutate_add_connection(self.config.genome_config)
255+
256+
# Record nodes before mutation
257+
nodes_before = set(genome.nodes.keys())
258+
259+
genome.mutate_add_node(self.config.genome_config)
260+
261+
# Identify the newly added node
262+
nodes_after = set(genome.nodes.keys())
263+
new_nodes = nodes_after - nodes_before
264+
265+
self.assertEqual(len(new_nodes), 1, "Should add exactly one new node")
266+
new_node_id = next(iter(new_nodes))
267+
268+
new_node = genome.nodes[new_node_id]
269+
self.assertTrue(hasattr(new_node, "bias"))
270+
self.assertEqual(new_node.bias, 0.0,
271+
"Newly added node bias should be initialized to 0.0")
272+
249273
# ========== Add Connection Mutation Tests ==========
250274

251275
def test_add_connection_creates_new_connection(self):

0 commit comments

Comments
 (0)