コード例 #1
0
    public void CrossOverSpawn__()
    {
        var parents  = new List <Individual>();
        var p1Genome = new Genome(3, 1, 2, 2, new List <decimal> {
            1, 3, 5, 7, 9, 11, 13, 15, 17, 19
        });

        parents.Add(new Individual {
            Genome = p1Genome
        });

        var p2Genome = new Genome(3, 1, 2, 2, new List <decimal> {
            2, 4, 6, 8, 10, 12, 14, 16, 18, 20
        });

        parents.Add(new Individual {
            Genome = p2Genome
        });


        var baby = crossOver.Spawn(parents);

        Assert.That(baby, Is.Not.Null);
        Assert.That(baby.Genome.NumInputNeurons, Is.EqualTo(3));
        Assert.That(baby.Genome.NumHiddenLayers, Is.EqualTo(1));
        Assert.That(baby.Genome.NumNeuronsPerHiddenLayer, Is.EqualTo(2));
        Assert.That(baby.Genome.NumOutputNeurons, Is.EqualTo(2));
    }
コード例 #2
0
    public List <Individual> MakeNewGeneration(List <Individual> population, int numInNewPopulation)
    {
        population.Sort((a, b) => a.Fitness.CompareTo(b.Fitness));


        var newPopulation = new List <Individual>();

        for (int i = 0; i < numInNewPopulation; i++)
        {
            var parents = selectionFunction.Select(population);
            if (parents == null || parents.Count != 2)
            {
                throw new Exception("parents were null, or not 2 of them!");
            }


            var baby = spawnFunction.Spawn(parents);

            //do mutation?
            var doMutation = rand.NextDouble();
            if (doMutation.CompareTo(MUTATION_RATE) < 0)
            {
                var mutationSeverity = rand.NextDouble() * MAX_MUTATION_SEVERITY;
                Mutate(baby, mutationSeverity);
            }


            newPopulation.Add(baby);
        }

        return(newPopulation);
    }