Exemplo n.º 1
0
    public void GetConnectionInnovationNumber_Test()
    {
        GeneCounter connectionCounter = new GeneCounter(10);

        //Check the amount before
        Assert.AreEqual(1, mutationLog.ConnectionMutations.Count);

        //Insert an existing connection
        int existingConnection = mutationLog.GetConnectionInnovationNumber(1, 2, connectionCounter);

        //Check the amount after getting the existing connection
        Assert.AreEqual(1, mutationLog.ConnectionMutations.Count);
        Assert.AreEqual(1, existingConnection);

        //Insert new connection
        int newConnection = mutationLog.GetConnectionInnovationNumber(2, 1, connectionCounter);

        //Check the amount after the inserting the new connection
        Assert.AreEqual(2, mutationLog.ConnectionMutations.Count);
        Assert.AreEqual(10, newConnection);

        //Insert new connection again
        newConnection = mutationLog.GetConnectionInnovationNumber(2, 1, connectionCounter);

        //Check the amount after the inserting the new connection
        Assert.AreEqual(2, mutationLog.ConnectionMutations.Count);
        Assert.AreEqual(10, newConnection);
    }
Exemplo 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;
            }
        }
    }
Exemplo n.º 3
0
    public void AddConnectionMutation(AgentObject agent, int amountTries, MutationLog mutationLog, GeneCounter connectionInnovationCounter)
    {
        List <int> nodeKeys = agent.Genome.Nodes.Keys.ToList();

        for (int i = 0; i < amountTries; i++)
        {
            //Selet two random nodes
            NodeGene inNode  = agent.Genome.Nodes[nodeKeys[Random.Range(0, nodeKeys.Count)]];
            NodeGene outNode = agent.Genome.Nodes[nodeKeys[Random.Range(0, nodeKeys.Count)]];

            //If mutation is possible, mutate the agent
            if (agent.Genome.IsConnectionPossible(inNode, outNode))
            {
                int innovationNumber = mutationLog.GetConnectionInnovationNumber(inNode.ID, outNode.ID, connectionInnovationCounter);
                agent.Genome.AddConnectionMutation(inNode, outNode, innovationNumber);

                break;
            }
        }
    }