Exemplo n.º 1
0
    /*
        // Crossover Genomes
        private static Genome crossover(Genome genome1, Genome genome2) {
            Genome offspring = new Genome(3,2,1);
            // Loop through genes
            for (int i = 0; i < genome1.size(); i++) {
                // Crossover
                if (Math.random() <= uniformRate) {
                    offspring.setGene(i, genome1.getGene(i));
                } else {
                    offspring.setGene(i, genome2.getGene(i));
                }
            }
            return offspring;
        }
        */
    // Mutate a genome
    public static void mutateGenome(Genome genome)
    {
        foreach (GeneLink link in genome.getLinks()){
            GeneNode source = genome.nodes[link.in_layer][link.in_place];

            //if (Random.value <= source.maxDiff){ // used to be mutationRate
            if (Random.value <= mutationRate){
                if (Random.value <= uniformRate){
                    link.weight = link.weight + learningrate;
                }
                else {
                    link.weight = link.weight - learningrate;
                }
            }
        }

        foreach (GeneNode node in genome.getNodes()){
            if (Random.value <= mutationRate)
            {
                if (Random.value <= uniformRate)
                {
                    node.bias = node.bias + learningrate;
                }
                else
                {
                    node.bias = node.bias - learningrate;
                }
            }
        }

        // must not add to input or output layers
        for (int i = 1; i < genome.nrOfNodeLayers() - 1; i++)
        {
            if (Random.value <= layerMutationRate)
            {
                genome.addNewNodeToLayer(i);
            }
        }
    }