Пример #1
0
        public void CreateNewGeneration()
        {
            int maxSize = 20;

            var pop = new SpeciedPopulation(maxSize, maxSize, chromosome, strat, 2);

            pop.CreateInitialGeneration();
            var list = new List <IChromosome>(10);

            for (int i = 0; i < maxSize; i++)
            {
                list.Add(chromosome.CreateNew());
            }
            pop.CreateNewGeneration(list);

            Assert.AreEqual(2, pop.Species.Count);
            Assert.AreEqual(0u, pop.Species[0].ID);
            Assert.AreEqual(1u, pop.Species[1].ID);
            var concatSpecies = pop.Species[0].CurrentGeneration.Chromosomes.Concat(
                pop.Species[1].CurrentGeneration.Chromosomes).ToList();
            var chroms = pop.CurrentGeneration.Chromosomes;

            Assert.AreEqual(chroms.Count, concatSpecies.Count);
            for (int i = 0; i < concatSpecies.Count; i++)
            {
                Assert.IsTrue(chroms.Contains(concatSpecies[i]));
            }
        }
Пример #2
0
        public void CreateInitialGeneration()
        {
            var pop = new SpeciedPopulation(5, 10, chromosome, strat, 2);

            pop.CreateInitialGeneration();
            Assert.AreEqual(1, pop.Species.Count);
            Assert.AreEqual(0u, pop.Species[0].ID);
            Assert.AreSame(chromosome, pop.Species[0].Centroid);
        }
        public void TestInit()
        {
            int minSpecieSize = 2;
            int maxSize       = 12;

            Population = new SpeciedPopulation(
                minSize: maxSize,
                maxSize: maxSize,
                adamChromosome: new FloatingPointChromosome(0, 1, 32, 2),
                new SpeciationStrategy((a, b) => Math.Abs(TestHelpers.GetValue(a) - TestHelpers.GetValue(b)), 0.5),
                minSpecieSize: minSpecieSize);
            Population.CreateInitialGeneration();
            double[] minValue       = new double[] { 0 };
            double[] maxValue       = new double[] { 1 };
            int[]    totalBits      = new int[] { 32 };
            int[]    fractionDigits = new int[] { 1 };
            Population.Species[0].Centroid = new FloatingPointChromosome(
                minValue, maxValue,
                totalBits, fractionDigits,
                new double[] { 0.0 });
            Population.Species.Add(new Specie(minSpecieSize, maxSize, 1u, new FloatingPointChromosome(
                                                  minValue, maxValue,
                                                  totalBits, fractionDigits,
                                                  new double[] { 1.0 })));
            Specie0 = Population.Species[0];
            Specie1 = Population.Species[1];

            // Divide up the species
            Population.CreateNewGeneration(Population.CurrentGeneration.Chromosomes);
            Assert.AreEqual(2, Population.Species.Count);

            // Set Specie fitnesses
            TestHelpers.SetFitnessesAndOrder(Specie0, 0);
            TestHelpers.SetFitnessesAndOrder(Specie1, 1);

            // Create an older specie for testing
            OldSpecie = new Specie(minSpecieSize, maxSize, 2u, new FloatingPointChromosome(minValue, maxValue, totalBits, fractionDigits));
            OldSpecie.CreateInitialGeneration();
            TestHelpers.SetFitnessesAndOrder(OldSpecie, 0);
            for (int i = 1; i < 15; i++)
            {
                OldSpecie.CreateNewGeneration(OldSpecie.CurrentGeneration.Chromosomes);
                // Set the best chromosome to a different member
                OldSpecie.CurrentGeneration.Chromosomes[i % OldSpecie.CurrentGeneration.Chromosomes.Count].Fitness = i;
                OldSpecie.EndCurrentGeneration();
            }

            Crossover = new UniformCrossover(0);
        }