Skip to content

Commit 92d1a64

Browse files
Fix cycle creation in crossover for feed-forward networks
- Add cycle checking in DefaultGenome.configure_crossover when config.feed_forward=True - Prevent inheritance of connections that would create cycles during crossover - Remove outdated TODO comment in DefaultReproduction - Uses existing creates_cycle function from neat.graphs for detection - Maintains backward compatibility and all existing functionality Fixes the issue where crossover could produce genomes with cycles even when feed_forward=True was configured, which would cause errors when creating FeedForwardNetwork instances.
1 parent 5209176 commit 92d1a64

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

neat/genome.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,13 +302,22 @@ def configure_crossover(self, genome1, genome2, config):
302302
)
303303
# Take the gene from the fitter parent
304304
new_gene = cg1.copy()
305+
# For feed-forward networks, check if this connection would create a cycle
306+
if config.feed_forward and creates_cycle(list(self.connections), new_gene.key):
307+
continue
305308
self.connections[new_gene.key] = new_gene
306309
else:
307310
new_gene = cg1.crossover(cg2)
311+
# For feed-forward networks, check if this connection would create a cycle
312+
if config.feed_forward and creates_cycle(list(self.connections), new_gene.key):
313+
continue
308314
self.connections[new_gene.key] = new_gene
309315
elif cg1 is not None:
310316
# Disjoint or excess gene from fittest parent (parent1)
311317
new_gene = cg1.copy()
318+
# For feed-forward networks, check if this connection would create a cycle
319+
if config.feed_forward and creates_cycle(list(self.connections), new_gene.key):
320+
continue
312321
self.connections[new_gene.key] = new_gene
313322
# Note: genes only in parent2 (less fit) are not inherited
314323

neat/reproduction.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,6 @@ def reproduce(self, config, species, pop_size, generation):
270270
child = config.genome_type(gid)
271271
child.configure_crossover(parent1, parent2, config.genome_config)
272272
child.mutate(config.genome_config)
273-
# TODO: if config.genome_config.feed_forward, no cycles should exist
274273
new_population[gid] = child
275274
self.ancestors[gid] = (parent1_id, parent2_id)
276275

0 commit comments

Comments
 (0)