public void GetNodeID_Test()
    {
        GeneCounter nodeCounter   = new GeneCounter(10);
        List <int>  nodesInGenome = new List <int>();

        //Get an existing node
        int result = mutationLog.GetNodeID(connection1.InnovationNumber, nodesInGenome, nodeCounter);

        Assert.AreEqual(node1.ID, result);

        //Get a new node number
        nodesInGenome.Add(result);
        int result2 = mutationLog.GetNodeID(connection1.InnovationNumber, nodesInGenome, nodeCounter);

        Assert.AreEqual(10, result2);
        Assert.AreEqual(2, mutationLog.NodeMutations[connection1.InnovationNumber].Count);

        //Get the second node again
        int result3 = mutationLog.GetNodeID(connection1.InnovationNumber, nodesInGenome, nodeCounter);

        Assert.AreEqual(10, result3);
        Assert.AreEqual(2, mutationLog.NodeMutations[connection1.InnovationNumber].Count);
    }
Esempio n. 2
0
    public void AddNodeMutation(AgentObject agent, int amountTries, MutationLog mutationLog, GeneCounter connectionInnovationCounter, GeneCounter nodeCounter)
    {
        List <int> connectionKeys = agent.Genome.Connections.Keys.OrderBy(x => x).ToList();

        if (connectionKeys.Count <= 0)
        {
            return;
        }

        for (int i = 0; i < amountTries; i++)
        {
            ConnectionGene connection = null;
            //Check the size of the genome, if the genome is small, take an older gene
            if (agent.Genome.Nodes.Keys.Count <= _SIZE_THRESHOLD)
            {
                connection = agent.Genome.Connections[connectionKeys[Random.Range(0, Mathf.RoundToInt(connectionKeys.Count - (Mathf.Sqrt(connectionKeys.Count))))]];
            }
            else
            {
                //Select random connection
                connection = agent.Genome.Connections[connectionKeys[Random.Range(0, connectionKeys.Count)]];
            }

            //If mutation is possible, mutate the agent
            if (agent.Genome.IsNodePossible(connection))
            {
                //int nodeID = mutationLog.GetNodeID(connection.InnovationNumber, nodeCounter);
                int nodeID = mutationLog.GetNodeID(connection.InnovationNumber, agent.Genome.Nodes.Keys.ToList(), nodeCounter);

                int innovationNumberInToNewNode  = mutationLog.GetConnectionInnovationNumber(connection.InNode, nodeID, connectionInnovationCounter);
                int innovationNumberNewNodeToOut = mutationLog.GetConnectionInnovationNumber(nodeID, connection.OutNode, connectionInnovationCounter);

                agent.Genome.AddNodeMutation(connection, nodeID, innovationNumberInToNewNode, innovationNumberNewNodeToOut);
                break;
            }
        }
    }