示例#1
0
        public void MutatorModifierShouldMutateGenome()
        {
            Random rng = new Random();

            Int32 RandomGeneProvider()
            {
                return(ChromosomeInitializers.GetRandomInt32(rng, 0, 10));
            }

            for (Int32 r = 0; r < TestConstants.TestRepeats; r++)
            {
                Double mutationRatio = rng.NextDouble();

                var mutator =
                    new RandomMutationModifier <Population <TestIndividual <Int32>, Int32>, TestIndividual <Int32>, Int32>(
                        mutationRatio, RandomGeneProvider);

                Int32 chromosomeSize  = 10000;
                Int32 populationCount = 100;
                Int32 geneMin         = 0;
                Int32 geneMax         = 100;

                Population <TestIndividual <Int32>, Int32> population = TestUtils.GetPopulation(populationCount
                                                                                                , () => TestUtils.GetRandomTestIndividual(chromosomeSize
                                                                                                                                          , () => ChromosomeInitializers.GetRandomInt32(rng, geneMin, geneMax)));

                Int32[][] originalGenes = population.GetIndividuals()
                                          .Select(individual => individual.GetChromosome().GetGenome()).ToArray();
                originalGenes = Utils.DeepCopy(originalGenes);

                Population <TestIndividual <Int32>, Int32> modifiedPopulation = mutator.ModifyPopulation(population);

                Int32 modified   = 0;
                Int32 unmodified = 0;

                TestIndividual <Int32>[] modifiedIndividuals = modifiedPopulation.GetIndividuals().ToArray();

                for (Int32 i = 0; i < modifiedIndividuals.Length; i++)
                {
                    Int32[] modifiedGenes = modifiedIndividuals[i].GetChromosome().GetGenome();

                    for (Int32 j = 0; j < originalGenes.Length; j++)
                    {
                        if (originalGenes[i][j] == modifiedGenes[j])
                        {
                            unmodified++;
                        }
                        else
                        {
                            modified++;
                        }
                    }
                }

                // Verify that the ratio of mutated genes is about the same as specified to modifier.
                Double mutatedRatio = (Double)modified / (modified + unmodified);
                mutatedRatio.AreDoublesEqual(mutationRatio, 0.02);
            }
        }
示例#2
0
        public void TestFitnessFunctionShouldWork()
        {
            Random rng          = new Random();
            Int32  genomeLength = rng.Next(1, 1000);

            // Test fitness on boolean genome.
            for (Int32 i = 0; i < 5; i++)
            {
                Double expectedFitness = 0;

                var individual = new TestIndividual <Boolean>();
                var chromosome = new Chromosome <Boolean>();
                chromosome.InitializeChromosome(genomeLength);

                IList <Boolean> testChromosome = new List <Boolean>();
                for (Int32 j = 0; j < genomeLength; j++)
                {
                    Boolean gene = rng.Next(2) == 1;
                    if (gene)
                    {
                        expectedFitness++;
                    }
                    testChromosome.Add(gene);
                }

                chromosome.SetChromosome(testChromosome.ToArray());

                individual.SetChromosome(chromosome);

                TestFitnessFunctionBoolean fitnessFunction = new TestFitnessFunctionBoolean();
                Double fitness = fitnessFunction.CalculateFitness(individual);

                Assert.True(fitness.AreDoublesEqual(expectedFitness));
            }

            // Test fitness on double genome.
            for (Int32 i = 0; i < 5; i++)
            {
                Double expectedFitness = 0;

                var individual = new TestIndividual <Double>();
                var chromosome = new Chromosome <Double>();
                chromosome.InitializeChromosome(genomeLength);

                IList <Double> testChromosome = new List <Double>();
                for (Int32 j = 0; j < genomeLength; j++)
                {
                    Double gene = rng.NextDouble() * 5;
                    expectedFitness += gene;
                    testChromosome.Add(gene);
                }

                chromosome.SetChromosome(testChromosome.ToArray());

                individual.SetChromosome(chromosome);

                TestFitnessFunctionDouble fitnessFunction = new TestFitnessFunctionDouble();
                Double fitness = fitnessFunction.CalculateFitness(individual);

                Assert.True(fitness.AreDoublesEqual(expectedFitness));
            }
        }
示例#3
0
        public void TrivialMaximizeTruesProcessShouldRun()
        {
            Random rng = new Random();

            Int32  genomeSize     = 100;
            Int32  targetFitness  = 90;
            Int32  populationSize = 100;
            Double mutationRate   = 0.01;

            var matingFunctions = new List <IMatingFunction <TestIndividual <Boolean>, Chromosome <Boolean>, Boolean> >
            {
                new CrossOverMatingFunction <TestIndividual <Boolean>, Chromosome <Boolean>, Boolean>(1)
                , new CrossOverMatingFunction <TestIndividual <Boolean>, Chromosome <Boolean>, Boolean>(3)
                , new RandomCombinationMatingFunction <TestIndividual <Boolean>, Chromosome <Boolean>, Boolean>()
            };

            foreach (IMatingFunction <TestIndividual <Boolean>, Chromosome <Boolean>, Boolean> matingFunction in
                     matingFunctions)
            {
                var builder =
                    new GeneticSelectionProcess <Population <TestIndividual <Boolean>, Boolean>, TestIndividual <Boolean>,
                                                 Chromosome <Boolean>, Boolean, ProcessInformation> .Builder();

                var process = builder.SetInitialPopulationSize(populationSize).UseDefaultConsoleLogger()
                              .SetProcessInformationComposer(
                    new ProcessInformationComposer <Population <TestIndividual <Boolean>, Boolean>,
                                                    TestIndividual <Boolean>, Boolean>()).SetMatingFunction(matingFunction)
                              .SetParentSelector(
                    new SingleGroupFittestParentsSelector <Population <TestIndividual <Boolean>, Boolean>,
                                                           TestIndividual <Boolean>, Boolean>()).SetFitnessFunction(new TestFitnessFunction())
                              .SetSurvivorSelector(
                    new ReplaceLeastFitSelector <Population <TestIndividual <Boolean>, Boolean>,
                                                 TestIndividual <Boolean>, Boolean>())
                              .SetPopulationInitializer(
                    new BooleanRandomPopulationInitializer <Population <TestIndividual <Boolean>, Boolean>,
                                                            TestIndividual <Boolean>, Chromosome <Boolean> >(genomeSize))
                              .AddTerminationCondition(
                    new FitnessTerminator <Population <TestIndividual <Boolean>, Boolean>, TestIndividual <Boolean>,
                                           Boolean>(targetFitness))
                              .AddPopulationModifier(
                    new RandomMutationModifier <Population <TestIndividual <Boolean>, Boolean>, TestIndividual <Boolean>
                                                , Boolean>(mutationRate, () => rng.Next(2) == 1)).Build();

                process.Initialize();
                process.RunUntilConvergence();

                IndividualWithFitness <TestIndividual <Boolean>, Boolean> fittest = process.GetPopulation().GetFittest();
                Double    fittestFitness = fittest.GetFitness();
                Boolean[] fittestGenome  = fittest.GetIndividual().GetChromosome().GetGenome();

                Double expectedFitness = fittestGenome.Select(b => b ? 1 : 0).Sum();

                Assert.True(fittestFitness >= 90);
                Assert.True(expectedFitness >= 90);
                Assert.True(expectedFitness.AreDoublesEqual(fittestFitness));

                IList <ProcessInformation> history = process.GetHistory();
                Assert.True(history.Count > 1);
                Assert.True(fittestFitness.AreDoublesEqual(history[history.Count - 1].FittestFitness));
            }
        }