예제 #1
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));
        }
예제 #2
0
 public void TestSetGetChromosome()
 {
     var c1 = LongChromosome.Of(0, 100, 10);
     var c2 = LongChromosome.Of(0, 100, 10);
     var c3 = LongChromosome.Of(0, 100, 10);
     var g  = Genotype.Of(c1, c2);
 }
예제 #3
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}."
                    );
            }
        }
예제 #4
0
        public static Population <EnumGene <double>, double> NewPermutationDoubleGenePopulation(int ngenes,
                                                                                                int nchromosomes, int npopulation)
        {
            var random  = new Random(122343);
            var alleles = MutableSeq.OfLength <double>(ngenes);

            for (var i = 0; i < ngenes; ++i)
            {
                alleles[i] = random.NextDouble() * 10;
            }
            var ialleles = alleles.ToImmutableSeq();

            var chromosomes = MutableSeq.OfLength <PermutationChromosome <double> >(nchromosomes);

            for (var i = 0; i < nchromosomes; ++i)
            {
                chromosomes[i] = PermutationChromosome.Of(ialleles);
            }

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

            for (var i = 0; i < npopulation; ++i)
            {
                population.Add(Phenotype.Of(genotype.NewInstance(), 0, gt => gt.Gene.Allele));
            }

            return(population);
        }
예제 #5
0
        private Genotype <TGene> Mutate(Genotype <TGene> genotype, double p, IntRef alterations)
        {
            var chromosomes = genotype.ToSeq().Copy();

            alterations.Value += Indexes(RandomRegistry.GetRandom(), genotype.Length, p)
                                 .Select(i => Mutate(chromosomes, i, p)).Sum();

            return(genotype.NewInstance(chromosomes.ToImmutableSeq()));
        }
예제 #6
0
        public void TestGenotypeGenotypeOfT()
        {
            var c1 = BitChromosome.Of(12);
            var c2 = BitChromosome.Of(12);
            var g2 = Genotype.Of(c1, c2, c2);
            var g4 = g2;

            Assert.Equal(g4, g2);
            Assert.Equal(g4.GetHashCode(), g2.GetHashCode());
        }
예제 #7
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);
            });
        }
예제 #8
0
        public void TestCreate()
        {
            var c1 = LongChromosome.Of(0, 100, 10);
            var c2 = LongChromosome.Of(0, 100, 10);
            var g1 = Genotype.Of(c1, c2);
            var g2 = g1.NewInstance();

            Assert.False(ReferenceEquals(g1, g2));
            Assert.False(g1.Equals(g2));
        }
예제 #9
0
        public Phenotype(Genotype <TGene> genotype, long generation, Func <Genotype <TGene>, TAllele> function,
                         Func <TAllele, TAllele> scaler)
        {
            _genotype   = genotype;
            _generation = generation;
            _function   = function;
            _scaler     = scaler;

            _rawFitness = new Lazy <TAllele>(() => _function(_genotype));
            _fitness    = new Lazy <TAllele>(() => _scaler(_rawFitness.Value));
        }
예제 #10
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());
        }
예제 #11
0
        protected Phenotype(SerializationInfo info, StreamingContext context)
        {
            _genotype   = (Genotype <TGene>)info.GetValue("_genotype", typeof(Genotype <TGene>));
            _generation = info.GetInt64("_generation");

            _rawFitness = new Lazy <TAllele>(() => (TAllele)info.GetValue("_rawFitness", typeof(TAllele)));
            _fitness    = new Lazy <TAllele>(() => (TAllele)info.GetValue("_fitness", typeof(TAllele)));

            _function = a => default(TAllele);
            _scaler   = a => a;
        }
예제 #12
0
        public void SelectNegativeCountArgument()
        {
            Func <Genotype <DoubleGene> > F = delegate()
            {
                return(Genotype.Of(new DoubleChromosome(0.0, 1.0)));
            };

            var population = new Population <DoubleGene, double>(2);

            for (int i = 0, n = 2; i < n; ++i)
            {
                population.Add(Phenotype.Of(F(), 12, TestUtils.Ff));
            }

            Assert.Throws <ArgumentOutOfRangeException>(() => Selector().Select(population, -1, Optimize.Maximum));
        }
예제 #13
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}");
                    }
                }
            });
        }
예제 #14
0
        public void SelectMinimum()
        {
            Func <Genotype <IntegerGene>, int> Ff = delegate(Genotype <IntegerGene> gt)
            {
                return(gt.GetChromosome().ToSeq().Select(g => g.IntValue()).Sum());
            };

            Func <Genotype <IntegerGene> > Gtf = delegate()
            {
                return(Genotype.Of(IntegerChromosome.Of(0, 100, 10)));
            };

            var population = Enumerable.Range(0, 50).Select(i => Phenotype.Of(Gtf(), 50, Ff)).ToPopulation();

            var selector = new StochasticUniversalSelector <IntegerGene, int>();

            var selection = selector.Select(population, 50, Optimize.Minimum);
        }
예제 #15
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);
        }
예제 #16
0
        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);
        }
예제 #17
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);
            }
        }
        public void Maximize()
        {
            RandomRegistry.Using(new Random(), r =>
            {
                Func <Genotype <IntegerGene>, int> ff =
                    g => g.GetChromosome().GetGene().Allele;

                Phenotype <IntegerGene, int> F()
                {
                    return(Phenotype.Of(Genotype.Of(IntegerChromosome.Of(0, 100)), 1, ff));
                }

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

                var selector =
                    new RouletteWheelSelector <IntegerGene, int>();

                var p = selector.Probabilities(population, 100, Optimize.Maximum);
                Assert.True(ProbabilitySelector.Sum2One(p), p + " != 1");
            });
        }
예제 #19
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);
            }
        }
예제 #20
0
 public Phenotype <TGene, TAllele> NewInstance(Genotype <TGene> genotype, long generation)
 {
     return(Phenotype.Of(genotype, generation, _function, _scaler));
 }
예제 #21
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));
 }
예제 #22
0
 public static Phenotype <DoubleGene, double> NewDoublePhenotype(double value)
 {
     return(Phenotype.Of(Genotype.Of(
                             DoubleChromosome.Of(DoubleGene.Of(value, 0, 10))), 0, Ff
                         ).Evaluate());
 }
예제 #23
0
 private static Phenotype <DoubleGene, double> Pt(double value)
 {
     return(Phenotype.Of(Genotype.Of(DoubleChromosome.Of(DoubleGene.Of(value, 0, 10))), 0, Ff, Fs));
 }