Exemplo n.º 1
0
    private NEAT Crossover(NEAT dominant, NEAT submissive)
    {
        NEAT child = new NEAT(dominant);

        List <Connection> newConnections = new List <Connection>();

        foreach (var connection in child.GetConnetionGenom())
        {
            Connection c = submissive.GetConnetionGenom().Find(x => x.innovation == connection.innovation);

            if (c != null && Random.Range(0f, 1f) > 0.5f)
            {
                Node inNode  = child.GetNodeGenom().Find(n => n.nodeID == c.inNode);
                Node outNode = child.GetNodeGenom().Find(n => n.nodeID == c.outNode);

                if (outNode == null && inNode != null)
                {
                    outNode = new Node(c.outNode, inNode.order + 1, NodeType.Hidden);
                    child.AddNode(outNode);
                }
                else if (outNode == null && inNode == null)
                {
                    outNode = new Node(c.outNode, 1, NodeType.Hidden);
                    child.AddNode(outNode);
                }

                if (inNode == null)
                {
                    inNode = new Node(c.inNode, 0, NodeType.Hidden);
                    child.AddNode(inNode);
                }

                Connection cNew = new Connection(inNode.nodeID, outNode.nodeID, c.weight, c.enabled, c.innovation);

                newConnections.Add(cNew);
            }
        }

        foreach (var connection in newConnections)
        {
            if (!CompareConnection(connection, child))
            {
                if (!LoopSearch(connection, child.GetConnetionGenom()))
                {
                    child.RemoveConnection(child.GetConnetionGenom().Find(n => n.innovation == connection.innovation));
                    child.AddConnection(connection);
                    child.GetNodeGenom().Find(n => n.nodeID == connection.inNode).AddConnection(connection);
                }
            }
        }
        return(child);
    }