Esempio n. 1
0
        public void Select(int size, int count, Optimize opt)
        {
            Func <Genotype <DoubleGene>, double> ff = gt => gt.Gene.Allele;

            Func <Phenotype <DoubleGene, double> > F = delegate()
            {
                return(Phenotype.Of(Genotype.Of(DoubleChromosome.Of(0.0, 1000.0)), 1, ff));
            };

            var population = Enumerable.Range(0, size)
                             .Select(i => F())
                             .ToPopulation();

            var selection = Selector().Select(population, count, opt);

            if (size == 0)
            {
                Assert.Empty(selection);
            }
            else
            {
                Assert.Equal(count, selection.Count);
            }
            foreach (var pt in selection)
            {
                Assert.True(
                    population.Contains(pt),
                    $"Population doesn't contain {pt}."
                    );
            }
        }
Esempio n. 2
0
        protected Histogram <double> Distribution(
            ISelector <DoubleGene, double> selector,
            Optimize opt,
            int populationCount,
            int loops
            )
        {
            Func <Genotype <DoubleGene>, double> ff = gt => gt.Gene.Allele;

            Factory <Phenotype <DoubleGene, double> > ptf = () =>
                                                            Phenotype.Of(Genotype.Of(DoubleChromosome.Of(Min, Max)), 1, ff);

            return(Enumerable.Range(0, loops).AsParallel().Select(j =>
            {
                var hist = Histogram.OfDouble(Min, Max, ClassCount);

                var population =
                    Enumerable.Range(0, populationCount)
                    .Select(i => ptf())
                    .ToPopulation();

                var selectionCount = (int)(populationCount / SelectionFraction);
                foreach (var pt in selector.Select(population, selectionCount, opt)
                         .Select(pt => pt.GetGenotype().Gene.Allele))
                {
                    hist.Accept(pt);
                }

                return hist;
            }).ToDoubleHistogram(Min, Max, ClassCount));
        }
Esempio n. 3
0
        protected override Factory <Population <DoubleGene, double> > Factory()
        {
            return(() =>
            {
                var gt = Genotype.Of(DoubleChromosome.Of(0, 1));

                return new Population <DoubleGene, double>(100)
                .Fill(() => Phenotype.Of(gt.NewInstance(), 1, Ff, Fs), 100);
            });
        }
Esempio n. 4
0
        public void NumberOfGenes()
        {
            var genotype = Genotype.Of(
                DoubleChromosome.Of(0.0, 1.0, 8),
                DoubleChromosome.Of(1.0, 2.0, 10),
                DoubleChromosome.Of(0.0, 10.0, 9),
                DoubleChromosome.Of(0.1, 0.9, 5)
                );

            Assert.Equal(32, genotype.GetNumberOfGenes());
        }
Esempio n. 5
0
        public void SelectionPerformance(int size, int count, Optimize opt)
        {
            Func <Genotype <DoubleGene>, double> ff = g => g.Gene.Allele;

            Func <Phenotype <DoubleGene, double> > F = delegate()
            {
                return(Phenotype.Of(Genotype.Of(DoubleChromosome.Of(0.0, 100.0)), 1, ff));
            };

            RandomRegistry.Using(new Random(543455), r =>
            {
                var population = Enumerable.Range(0, size)
                                 .Select(i => F())
                                 .ToPopulation();

                ISelector <DoubleGene, double> selector = Selector();

                if (!(selector is MonteCarloSelector <DoubleGene, double>))
                {
                    var monteCarloSelectionSum =
                        new MonteCarloSelector <DoubleGene, double>()
                        .Select(population, count, opt)
                        .Select(p => p.GetFitness())
                        .Sum();

                    var selectionSum =
                        selector
                        .Select(population, count, opt)
                        .Select(p => p.GetFitness())
                        .Sum();

                    if (opt == Optimize.Maximum)
                    {
                        Assert.True(
                            selectionSum > monteCarloSelectionSum,
                            $"{selectionSum} <= {monteCarloSelectionSum}");
                    }
                    else
                    {
                        Assert.True(
                            selectionSum < monteCarloSelectionSum,
                            $"{selectionSum} >= {monteCarloSelectionSum}");
                    }
                }
            });
        }
Esempio n. 6
0
        public void Empty()
        {
            var genotype = Genotype.Of(
                Enumerable.Range(0, 10)
                .Select(i => DoubleChromosome.Of(0, 10, 10))
                .ToImmutableSeq()
                );

            var pop = Population.Empty <DoubleGene, bool>();

            Assert.True(0 == pop.Count);
            Assert.True(pop.IsEmpty);

            pop.Add(Phenotype.Of(genotype, 1, chromosomes => true));

            Assert.True(1 == pop.Count);
            Assert.False(pop.IsEmpty);
        }
        public static Population <DoubleGene, double> Population(int ngenes, int nchromosomes, int npopulation)
        {
            var chromosomes = MutableSeq.OfLength <IChromosome <DoubleGene> >(nchromosomes);

            for (var i = 0; i < nchromosomes; ++i)
            {
                chromosomes[i] = DoubleChromosome.Of(0, 10, ngenes);
            }

            var genotype   = new Genotype <DoubleGene>(chromosomes.ToImmutableSeq());
            var population = new Population <DoubleGene, double>(npopulation);

            for (var i = 0; i < npopulation; ++i)
            {
                population.Add(Phenotype.Of(genotype.NewInstance(), 0, TestUtils.Ff));
            }

            return(population);
        }
Esempio n. 8
0
        public void WorstIndividual()
        {
            const int size       = 20;
            var       population = new Population <DoubleGene, int>(size);

            for (var i = 0; i < size; ++i)
            {
                var gene = DoubleGene.Of(i, 0, size + 10);
                var ch   = DoubleChromosome.Of(gene);
                var gt   = Genotype.Of(ch);
                var pt   = Phenotype.Of(gt, 1, g => g.Gene.IntValue());

                population.Add(pt);
            }

            var selector = new TruncationSelector <DoubleGene, int>(5);
            var selected = selector.Select(population, 10, Optimize.Minimum);

            foreach (var pt in selected)
            {
                Assert.True(pt.GetFitness() < 5);
            }
        }
Esempio n. 9
0
        public void NewInstance()
        {
            var gt1 = Genotype.Of(
                //Rotation
                DoubleChromosome.Of(DoubleGene.Of(-Math.PI, Math.PI)),

                //Translation
                DoubleChromosome.Of(DoubleGene.Of(-300, 300), DoubleGene.Of(-300, 300)),

                //Shear
                DoubleChromosome.Of(DoubleGene.Of(-0.5, 0.5), DoubleGene.Of(-0.5, 0.5))
                );

            var gt2 = gt1.NewInstance();

            Assert.Equal(gt1.Length, gt2.Length);
            for (var i = 0; i < gt1.Length; ++i)
            {
                var ch1 = gt1.GetChromosome(i);
                var ch2 = gt2.GetChromosome(i);
                Assert.Equal(ch1.Length, ch2.Length);
            }
        }
Esempio n. 10
0
 private static Phenotype <DoubleGene, double> Pt(double value)
 {
     return(Phenotype.Of(Genotype.Of(DoubleChromosome.Of(DoubleGene.Of(value, 0, 10))), 0, Ff, Fs));
 }
Esempio n. 11
0
 public static Phenotype <DoubleGene, double> NewDoublePhenotype(double value)
 {
     return(Phenotype.Of(Genotype.Of(
                             DoubleChromosome.Of(DoubleGene.Of(value, 0, 10))), 0, Ff
                         ).Evaluate());
 }
Esempio n. 12
0
 private static Phenotype <DoubleGene, double> CreatePhenotype(double value)
 {
     return(Phenotype.Of(Genotype.Of(DoubleChromosome.Of(DoubleGene.Of(value, 0, 10))), 0, gt => gt.Gene.Allele));
 }