Beispiel #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);
            }
        }
Beispiel #2
0
        public void ShouldComposeNewProcessInformationForNewGeneration()
        {
            Random rng = new Random();

            for (Int32 i = 0; i < TestConstants.TestRepeats; i++)
            {
                Int32 populationSize = rng.Next(1, 1000);
                Int32 geneSize       = rng.Next(1, 1000);

                Population <TestIndividual <Int32>, Int32> GetPopulation()
                {
                    var population = new Population <TestIndividual <Int32>, Int32>();
                    IList <TestIndividual <Int32> > individuals = new List <TestIndividual <Int32> >();

                    for (Int32 j = 0; j < populationSize; j++)
                    {
                        var newIndividual = new TestIndividual <Int32>();

                        Chromosome <Int32> newChromosome =
                            ChromosomeInitializers.GetRandomChromosome <Chromosome <Int32>, Int32>(geneSize
                                                                                                   , rng_ => ChromosomeInitializers.GetRandomInt32(rng_, 0, 10));

                        newIndividual.SetChromosome(newChromosome);
                        individuals.Add(newIndividual);
                    }

                    population.SetPopulation(individuals);
                    population.OrderByFitness(new PopulationTests.TestFitnessFunctionInt32());

                    return(population);
                }

                Population <TestIndividual <Int32>, Int32> population1 = GetPopulation();
                Population <TestIndividual <Int32>, Int32> population2 = GetPopulation();

                var composer =
                    new ProcessInformationComposer <Population <TestIndividual <Int32>, Int32>, TestIndividual <Int32>,
                                                    Int32>();

                ProcessInformation firstProcessInformation  = composer.ComposeProcessInformation(null, population1);
                ProcessInformation secondProcessInformation =
                    composer.ComposeProcessInformation(firstProcessInformation, population2);

                Assert.Equal(firstProcessInformation.Generation, secondProcessInformation.Generation - 1);

                Assert.Equal(firstProcessInformation.TotalFitnessDelta, firstProcessInformation.TotalFitness);
                Assert.Equal(firstProcessInformation.FittestFitnessDelta, firstProcessInformation.FittestFitness);
                Assert.Equal(firstProcessInformation.FittestNFitnessDelta, firstProcessInformation.FittestNFitness);

                Assert.Equal(secondProcessInformation.TotalFitnessDelta
                             , secondProcessInformation.TotalFitness - firstProcessInformation.TotalFitness);
                Assert.Equal(secondProcessInformation.FittestFitnessDelta
                             , secondProcessInformation.FittestFitness - firstProcessInformation.FittestFitness);
                Assert.Equal(secondProcessInformation.FittestNFitnessDelta
                             , secondProcessInformation.FittestNFitness - firstProcessInformation.FittestNFitness);
            }
        }
Beispiel #3
0
        public void RandomChromosomeInitializerShouldWork()
        {
            Random rng = new Random();

            Int32 chromosomeLength = rng.Next(1, 1000_000);

            Int32 geneMin = rng.Next(1, 1000_000);
            Int32 geneMax = rng.Next(geneMin, 1000_000);

            // Int32
            Func <Random, Int32> randomProvider = ChromosomeInitializers.CreateRandomProvider(geneMin
                                                                                              , geneMax, ChromosomeInitializers.GetRandomInt32);

            Chromosome <Int32> chromosome =
                ChromosomeInitializers.GetRandomChromosome <Chromosome <Int32>, Int32>(chromosomeLength, randomProvider);

            Int32[] fullRange = chromosome.GetGeneRange(0, chromosomeLength);

            Assert.Equal(chromosomeLength, fullRange.Length);

            foreach (Int32 gene in fullRange)
            {
                Assert.True(gene >= geneMin);
                Assert.True(gene <= geneMax);
            }

            // Boolean
            Func <Random, Boolean> randomProvider2 = ChromosomeInitializers.CreateRandomProvider(false
                                                                                                 , true, ChromosomeInitializers.GetRandomBoolean);

            Chromosome <Boolean> chromosome2 =
                ChromosomeInitializers.GetRandomChromosome <Chromosome <Boolean>, Boolean>(chromosomeLength
                                                                                           , randomProvider2);

            Boolean[] fullRange2 = chromosome2.GetGeneRange(0, chromosomeLength);

            Assert.Equal(chromosomeLength, fullRange2.Length);

            // Double
            Func <Random, Double> randomProvider3 = ChromosomeInitializers.CreateRandomProvider(Convert.ToDouble(geneMin)
                                                                                                , Convert.ToDouble(geneMax), ChromosomeInitializers.GetRandomDouble);

            Chromosome <Double> chromosome3 =
                ChromosomeInitializers.GetRandomChromosome <Chromosome <Double>, Double>(chromosomeLength
                                                                                         , randomProvider3);

            Double[] fullRange3 = chromosome3.GetGeneRange(0, chromosomeLength);

            Assert.Equal(chromosomeLength, fullRange3.Length);
        }
Beispiel #4
0
        public static TestIndividual <Int32>[] GetRandomTestIndividuals(Int32 parentCount, Int32 chromosomeSize
                                                                        , Int32 geneMin = 0, Int32 geneMax = 10)
        {
            var    individuals = new TestIndividual <Int32> [parentCount];
            Random rng         = new Random();

            for (Int32 i = 0; i < parentCount; i++)
            {
                individuals[i] = GetRandomTestIndividual(chromosomeSize
                                                         , () => ChromosomeInitializers.GetRandomInt32(rng, geneMin, geneMax));
            }

            return(individuals);
        }
        public void ShouldFetchFittestIndividualsAsParents()
        {
            Random rng = new Random();

            for (Int32 i = 0; i < TestConstants.TestRepeats; i++)
            {
                Int32 populationSize = rng.Next(2, 1000);

                Population <TestIndividual <Int32>, Int32> population = TestUtils.GetPopulation(populationSize
                                                                                                , () => TestUtils.GetRandomTestIndividual(10
                                                                                                                                          , () => ChromosomeInitializers.GetRandomInt32(rng, 0, 10)));

                population.OrderByFitness(new PopulationTests.TestFitnessFunctionInt32());

                var selector =
                    new SingleGroupFittestParentsSelector <Population <TestIndividual <Int32>, Int32>,
                                                           TestIndividual <Int32>, Int32>(2);

                IList <IEnumerable <TestIndividual <Int32> > > selectedParents    = selector.GetParents(population).ToList();
                IEnumerable <TestIndividual <Int32> >          selectedParentPair = selectedParents.First();

                Assert.Equal(1, selectedParents.Count);

                IList <IndividualWithFitness <TestIndividual <Int32>, Int32> > twoFittest = population.GetNFittest(2);

                foreach (IndividualWithFitness <TestIndividual <Int32>, Int32> fitIndividual in twoFittest)
                {
                    Assert.Contains(fitIndividual.GetIndividual(), selectedParentPair);
                }
            }
        }
        public void ShouldTerminateBasedOnGeneration()
        {
            Random rng = new Random();

            Population <TestIndividual <Int32>, Int32> population = TestUtils.GetPopulation(10
                                                                                            , () => TestUtils.GetRandomTestIndividual(10, () => ChromosomeInitializers.GetRandomInt32(rng, -5, 5)));

            population.OrderByFitness(new PopulationTests.TestFitnessFunctionInt32());

            for (Int32 i = 0; i < TestConstants.TestRepeats; i++)
            {
                Int32 maxRun           = 1000;
                Int32 terminationPoint = rng.Next(1, maxRun);

                var composer =
                    new ProcessInformationComposer <Population <TestIndividual <Int32>, Int32>, TestIndividual <Int32>,
                                                    Int32>();
                var terminator =
                    new GenerationsCountTerminator <Population <TestIndividual <Int32>, Int32>, TestIndividual <Int32>,
                                                    Int32>((UInt64)terminationPoint);

                ProcessInformation previousProcessInformation = null;

                for (Int32 j = 1; j < maxRun; j++)
                {
                    ProcessInformation newProcessInformation =
                        composer.ComposeProcessInformation(previousProcessInformation, population);
                    previousProcessInformation = newProcessInformation;

                    Boolean shouldTerminate = terminator.ShouldTerminate(population, newProcessInformation);
                    if (j >= terminationPoint)
                    {
                        Assert.True(shouldTerminate);
                    }
                    else
                    {
                        Assert.False(shouldTerminate);
                    }
                }
            }
        }
Beispiel #7
0
        public void ShouldOrderByFitness()
        {
            Random rng = new Random();

            for (Int32 i = 0; i < TestConstants.TestRepeats; i++)
            {
                Int32 populationSize = rng.Next(1, 1000);
                Int32 chromosomeSize = rng.Next(1, 1000);

                Population <TestIndividual <Double>, Double> population = TestUtils.GetPopulation(populationSize
                                                                                                  , () => TestUtils.GetRandomTestIndividual(chromosomeSize
                                                                                                                                            , () => ChromosomeInitializers.GetRandomDouble(rng, 0, 100)));

                List <(TestIndividual <Double> individual, Double)> individuals = population.GetIndividuals()
                                                                                  .Select(individual => (individual, individual.GetChromosome().GetGenome().Sum())).ToList();

                individuals = individuals.OrderBy(tuple => tuple.Item2).ToList();

                (TestIndividual <Double> individual, Double)[] expectedOrdering = individuals.ToArray();
Beispiel #8
0
        public void ShouldNotAllowAccessToDirtyOrdering()
        {
            Random rng = new Random();
            Population <TestIndividual <Int32>, Int32> population = TestUtils.GetPopulation(100
                                                                                            , () => TestUtils.GetRandomTestIndividual(100
                                                                                                                                      , () => ChromosomeInitializers.GetRandomInt32(rng, 0, 10)));

            Assert.Throws <InvalidOperationException>(() => population.GetFittest());
            Assert.Throws <InvalidOperationException>(() => population.GetNFittest(1));
            Assert.Throws <InvalidOperationException>(() => population.GetPopulationByFitness());
        }
Beispiel #9
0
        public void ShouldComposeNewFirstProcessInformation()
        {
            Random rng = new Random();
            Population <TestIndividual <Int32>, Int32> population = TestUtils.GetPopulation(10
                                                                                            , () => TestUtils.GetRandomTestIndividual(10, () => ChromosomeInitializers.GetRandomInt32(rng, 0, 10)));

            population.OrderByFitness(new PopulationTests.TestFitnessFunctionInt32());

            var composer =
                new ProcessInformationComposer <Population <TestIndividual <Int32>, Int32>, TestIndividual <Int32>, Int32
                                                >();

            ProcessInformation newProcessInformation = composer.ComposeProcessInformation(null, population);

            Assert.Equal((UInt64)1, newProcessInformation.Generation);

            Double expectedTotalFitness = population
                                          .GetPopulationByFitness().Select(fitness => fitness.GetFitness()).Sum();

            Assert.Equal(expectedTotalFitness, newProcessInformation.TotalFitness);
        }