Beispiel #1
0
        public void StreamWithInitialGenotypes()
        {
            var problem = Problem.Of(
                a => a,
                Codec.Of(
                    () => Genotype.Of(IntegerChromosome.Of(0, 1000)),
                    g => g.Gene.Allele
                    )
                );

            const int genotypeCount = 10;
            const int max           = 1000;
            var       genotypes     = IntRange.Of(1, genotypeCount)
                                      .Select(i => IntegerChromosome.Of(IntegerGene.Of(max, 0, max)))
                                      .Select(i => Genotype.Of(i))
                                      .ToImmutableSeq();

            var engine = Engine.Builder(problem).Build();

            var result = engine.Stream(genotypes)
                         .Take(1)
                         .ToBestEvolutionResult();

            long maxCount = result.GetPopulation().Count(pt => pt.GetFitness() == max);

            Assert.True(maxCount >= genotypeCount, $"{maxCount} >= {genotypeCount}");
        }
Beispiel #2
0
        public static ICodec <int, IntegerGene> OfScalar(IntRange domain)
        {
            NonNull(domain);

            return(Codec.Of(
                       () => Genotype.Of(IntegerChromosome.Of(domain)),
                       gt => gt.GetChromosome().GetGene().Allele
                       ));
        }
Beispiel #3
0
        private static EvolutionResult <IntegerGene, int> NewResult(Optimize opt, int value)
        {
            const int length = 1000;

            Func <Genotype <IntegerGene>, int> F = delegate(Genotype <IntegerGene> gt)
            {
                return(gt.Gene.Allele);
            };

            var pop = new Population <IntegerGene, int>(length);

            for (var i = 0; i < length; ++i)
            {
                var gt = Genotype.Of(IntegerChromosome.Of(
                                         IntegerGene.Of(value, 0, length)
                                         ));
                pop.Add(Phenotype.Of(gt, 1, F));
            }

            Shuffle(pop, RandomRegistry.GetRandom());

            return(EvolutionResult.Of(opt, pop, 0, 0, EvolutionDurations.Zero, 0, 0, 0));
        }
Beispiel #4
0
        public void BestWorstPhenotype()
        {
            const int length = 100;

            Func <Genotype <IntegerGene>, int> F = delegate(Genotype <IntegerGene> gt)
            {
                return(gt.Gene.Allele);
            };

            var population = new Population <IntegerGene, int>(length);

            for (var i = 0; i < length; ++i)
            {
                var gt = Genotype.Of(IntegerChromosome.Of(
                                         IntegerGene.Of(i, 0, length)
                                         ));
                population.Add(Phenotype.Of(gt, 1, F));
            }

            Shuffle(population, RandomRegistry.GetRandom());

            var maxResult = EvolutionResult.Of(
                Optimize.Maximum, population,
                0, 0, EvolutionDurations.Zero, 0, 0, 0
                );

            Assert.Equal(length - 1, maxResult.GetBestFitness());
            Assert.Equal(0, maxResult.GetWorstFitness());

            var minResult = EvolutionResult.Of(
                Optimize.Minimum, population,
                0, 0, EvolutionDurations.Zero, 0, 0, 0
                );

            Assert.Equal(0, minResult.GetBestFitness());
            Assert.Equal(length - 1, minResult.GetWorstFitness());
        }
Beispiel #5
0
        public void CompareTo()
        {
            const int length = 100;

            Func <Genotype <IntegerGene>, int> F = delegate(Genotype <IntegerGene> gt)
            {
                return(gt.Gene.Allele);
            };

            var small = new Population <IntegerGene, int>(length);

            for (var i = 0; i < length; ++i)
            {
                var gt = Genotype.Of(IntegerChromosome.Of(
                                         IntegerGene.Of(i, 0, length)
                                         ));
                small.Add(Phenotype.Of(gt, 1, F));
            }
            Shuffle(small, RandomRegistry.GetRandom());

            var big = new Population <IntegerGene, int>(length);

            for (var i = 0; i < length; ++i)
            {
                var gt = Genotype.Of(IntegerChromosome.Of(
                                         IntegerGene.Of(i + length, 0, length)
                                         ));
                big.Add(Phenotype.Of(gt, 1, F));
            }
            Shuffle(big, RandomRegistry.GetRandom());


            var smallMaxResult = EvolutionResult.Of(
                Optimize.Maximum, small,
                0, 0, EvolutionDurations.Zero, 0, 0, 0
                );
            var bigMaxResult = EvolutionResult.Of(
                Optimize.Maximum, big,
                0, 0, EvolutionDurations.Zero, 0, 0, 0
                );

            Assert.True(smallMaxResult.CompareTo(bigMaxResult) < 0);
            Assert.True(bigMaxResult.CompareTo(smallMaxResult) > 0);
            Assert.True(smallMaxResult.CompareTo(smallMaxResult) == 0);
            Assert.True(bigMaxResult.CompareTo(bigMaxResult) == 0);


            var smallMinResult = EvolutionResult.Of(
                Optimize.Minimum, small,
                0, 0, EvolutionDurations.Zero, 0, 0, 0
                );
            var bigMinResult = EvolutionResult.Of(
                Optimize.Minimum, big,
                0, 0, EvolutionDurations.Zero, 0, 0, 0
                );

            Assert.True(smallMinResult.CompareTo(bigMinResult) > 0);
            Assert.True(bigMinResult.CompareTo(smallMinResult) < 0);
            Assert.True(smallMinResult.CompareTo(smallMinResult) == 0);
            Assert.True(bigMinResult.CompareTo(bigMinResult) == 0);
        }