Exemple #1
0
    public void AddNodeMutation(Random r)                                     //insert a node between two connected nodes
    {
        int            conKey = connectionKeys[r.Next(connectionKeys.Count)]; //get a random connection
        ConnectionGene con    = connectionList[conKey];
        int            node1  = con.GetInNode();
        int            node2  = con.GetOutNode();

        con.Disable();                                                                 //disable connection

        NodeGene newNode = new NodeGene(nodeList.Count + 1, NodeGene.TYPE.HIDDEN);     //create a new node

        nodeList.Add(newNode);                                                         //add new node to node list

        int innovation1 = InnovationGenerator.GetInnovation();
        int innovation2 = InnovationGenerator.GetInnovation();

        connectionKeys.Add(innovation1);
        connectionList.Add(innovation1, new ConnectionGene(node1, newNode.GetID(), 1f, true, innovation1));    //add new connections to connection list

        connectionKeys.Add(innovation2);
        connectionList.Add(innovation2, new ConnectionGene(newNode.GetID(), node2, con.GetWeight(), true, innovation2));
    }
Exemple #2
0
    public void MutateAddNode()
    {
        ConnectionGene randomConnection = GetConnectionGenes()[random.Next(GetConnectionGenes().Count)];

        NodeGene InNode  = GetNodeGenes()[randomConnection.GetInputNode()];
        NodeGene OutNode = GetNodeGenes()[randomConnection.GetOutputNode()];


        randomConnection.SetEnabled(false);

        NodeGene MiddleNode = new NodeGene(GetNodeGenes().Count + 1, NodeGene.TYPE.HIDDEN, 0);

        ConnectionGene InputToMiddle = new ConnectionGene(InNode.GetID(), MiddleNode.GetID(), 1, true, NEAT_CONFIGS.GLOBAL_INNOVATION_NUMBER++);

        ConnectionGenes.Add(InputToMiddle);
        MiddleNode.AddIncomingConnection(InputToMiddle);
        ConnectionGene MiddleToOutput = new ConnectionGene(MiddleNode.GetID(), OutNode.GetID(), randomConnection.GetWeight(), true, NEAT_CONFIGS.GLOBAL_INNOVATION_NUMBER++);

        ConnectionGenes.Add(MiddleToOutput);
        OutNode.AddIncomingConnection(MiddleToOutput);
        //Add to the total current Pool Connections Made in this instance
        NodeGenes.Add(MiddleNode.GetID(), MiddleNode);
    }
Exemple #3
0
    public void MutateAddConnection()
    {
        List <int> Keys = new List <int>(NodeGenes.Keys);

        int randomKey1 = Keys[random.Next(Keys.Count)];
        int randomKey2 = Keys[random.Next(Keys.Count)];


        NodeGene node1    = NodeGenes[randomKey1];
        NodeGene node2    = NodeGenes[randomKey2];
        int      ATTEMPTS = 0;

        while ((node1.GetType() == NodeGene.TYPE.OUTPUT && node2.GetType() == NodeGene.TYPE.OUTPUT || node1.GetType() == NodeGene.TYPE.INPUT && node2.GetType() == NodeGene.TYPE.INPUT) || node1.GetID() == node2.GetID() ||
               (node1.GetType() == NodeGene.TYPE.BIAS && node2.GetType() == NodeGene.TYPE.INPUT || node2.GetType() == NodeGene.TYPE.BIAS && node1.GetType() == NodeGene.TYPE.INPUT) || ATTEMPTS < 10)
        {
            randomKey1 = Keys[random.Next(Keys.Count)];
            node1      = NodeGenes[randomKey1];
            ATTEMPTS++;
        }

        if (ATTEMPTS < 10)
        {
            bool NeedsReversing = false;


            if ((node1.GetType() == NodeGene.TYPE.HIDDEN && node2.GetType() == NodeGene.TYPE.INPUT) || (node1.GetType() == NodeGene.TYPE.HIDDEN && node2.GetType() == NodeGene.TYPE.BIAS))
            {
                NeedsReversing = true;
            }
            else
            {
                if (node1.GetType() == NodeGene.TYPE.OUTPUT && node2.GetType() == NodeGene.TYPE.INPUT || node1.GetType() == NodeGene.TYPE.OUTPUT && node2.GetType() == NodeGene.TYPE.BIAS || node1.GetType() == NodeGene.TYPE.OUTPUT && node2.GetType() == NodeGene.TYPE.HIDDEN)
                {
                    NeedsReversing = true;
                }
            }


            bool ExistsInGenome = false;

            foreach (ConnectionGene connection in GetConnectionGenes())
            {
                if (connection.GetInputNode() == node1.GetID() && connection.GetOutputNode() == node2.GetID())
                {
                    ExistsInGenome = true;
                    break;
                }
                else
                {
                    if (connection.GetOutputNode() == node1.GetID() && connection.GetInputNode() == node2.GetID())
                    {
                        ExistsInGenome = true;
                    }
                }
            }


            if (!ExistsInGenome)
            {
                bool ExistsInPopulation = false;
                foreach (ConnectionGene connection in PoolManager.TOTALCONNECTIONSMADETHISGENERATION)
                {
                    if (connection.GetInputNode() == node1.GetID() && connection.GetOutputNode() == node2.GetID() || connection.GetOutputNode() == node1.GetID() && connection.GetInputNode() == node2.GetID())
                    {
                        ExistsInPopulation = true;
                        ConnectionGene newConnection = new ConnectionGene(NeedsReversing ? node2.GetID() : node1.GetID(), NeedsReversing ? node1.GetID() : node2.GetID(), random.NextDouble(), true, connection.GetInnovation());
                        ConnectionGenes.Add(newConnection);
                        GetNodeGenes()[newConnection.GetOutputNode()].AddIncomingConnection(newConnection);
                        break;
                    }
                }
                if (!ExistsInPopulation)
                {
                    ConnectionGene newConnection = new ConnectionGene(NeedsReversing ? node2.GetID() : node1.GetID(), NeedsReversing ? node1.GetID() : node2.GetID(), random.NextDouble(), true, NEAT_CONFIGS.GLOBAL_INNOVATION_NUMBER++);
                    ConnectionGenes.Add(newConnection);
                    GetNodeGenes()[newConnection.GetOutputNode()].AddIncomingConnection(newConnection);
                    PoolManager.TOTALCONNECTIONSMADETHISGENERATION.Add(newConnection);
                }
            }
        }
    }