Ejemplo n.º 1
0
Archivo: NEAT.cs Proyecto: Johbja/NEAT
    public void NodeMutate()
    {
        //adds a new node by disableing a connection. The new node will be connected to the to both the old nodes, there will be a new connection
        //from the old input node and then the new node will copy the old connection and add it as its own connection to the old output node.

        if (genom.GetConnectionGenesLength() <= 0)
        {
            return;
        }

        int        index = Random.Range(0, genom.GetConnectionGenesLength() - 1);
        Connection connectionToReplace = genom.GetConnectionGene(index);

        Node oldInput  = genom.GetNodeGenom().Find(n => n.nodeID == connectionToReplace.inNode);
        Node oldOutput = genom.GetNodeGenom().Find(n => n.nodeID == connectionToReplace.outNode);

        Node newNode = new Node(genom.counter, oldOutput.order, NodeType.Hidden);

        Connection inNew = new Connection(newNode.nodeID, oldOutput.nodeID, connectionToReplace.weight, true, genom.currentInnovation);

        newNode.AddConnection(inNew);
        genom.AddConnectionGene(inNew);

        Connection inNewOther = new Connection(oldInput.nodeID, newNode.nodeID, 1, true, genom.currentInnovation);

        if (oldInput.nodeType != NodeType.Input)
        {
            oldInput.AddConnection(inNewOther);
        }

        genom.AddConnectionGene(inNewOther);

        genom.AddNodeGene(newNode);
        genom.ChangeConnectionGneneState(index, false);

        List <Node> nodes = genom.GetNodeGenom();

        newNode.AddOrder(ref nodes);
    }