Am Konzept weitergearbeitet
This commit is contained in:
@ -2,7 +2,8 @@ from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
from enum import Enum
|
||||
from random import choice
|
||||
from random import choice, random
|
||||
import networkx as nx
|
||||
|
||||
|
||||
class NodeType(Enum):
|
||||
@ -89,7 +90,7 @@ class Genome:
|
||||
|
||||
|
||||
def mutate(genome: Genome) -> None:
|
||||
mutation = choice([MutationType.ADD_NODE])
|
||||
mutation = choice([MutationType.ADD_CONNECTION])
|
||||
|
||||
if mutation is MutationType.ADD_CONNECTION:
|
||||
_mutate_add_connection(genome)
|
||||
@ -103,7 +104,22 @@ def _mutate_add_connection(genome: Genome) -> None:
|
||||
is added connecting two previously unconnected nodes.
|
||||
"""
|
||||
|
||||
...
|
||||
from_node = choice([node for node in genome.nodes if not node.type != NodeType.OUTPUT])
|
||||
|
||||
inputs = [node.id for node in genome.nodes.values() if node.type == NodeType.INPUT]
|
||||
distance_to_input = _distance_to_input(genome, from_node, inputs)
|
||||
to_node = choice([node for node in genome.nodes if _distance_to_input(genome, node, inputs) < distance_to_input and tuple[from_node, node] not in genome.connections])
|
||||
|
||||
genome.add_connection(from_node, to_node, weight=random.uniform(0,1))
|
||||
|
||||
|
||||
def _distance_to_input(g: Genome, node, inputs) -> int:
|
||||
paths = []
|
||||
for input_node in inputs:
|
||||
paths += list(nx.all_simple_paths(g, input_node, node))
|
||||
|
||||
path_lengths = [len(path) for path in paths]
|
||||
return max(path_lengths)
|
||||
|
||||
|
||||
def _mutate_add_node(genome: Genome) -> None:
|
||||
|
||||
Reference in New Issue
Block a user