Exemplo n.º 1
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);
        }
Exemplo 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));
        }
Exemplo n.º 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}."
                    );
            }
        }
Exemplo n.º 4
0
 public Phenotype <TGene, TAllele> NewInstance(
     long generation,
     Func <Genotype <TGene>, TAllele> function,
     Func <TAllele, TAllele> scaler
     )
 {
     return(Phenotype.Of(_genotype, generation, function, scaler));
 }
Exemplo n.º 5
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);
            });
        }
Exemplo n.º 6
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));
        }
Exemplo n.º 7
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}");
                    }
                }
            });
        }
Exemplo n.º 8
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);
        }
Exemplo n.º 9
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);
        }
Exemplo n.º 10
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);
        }
Exemplo n.º 11
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);
            }
        }
Exemplo n.º 12
0
        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");
            });
        }
Exemplo n.º 13
0
 protected override Factory <Phenotype <DoubleGene, double> > Factory()
 {
     return(() => Phenotype.Of(_genotype(), 0, _ff).Evaluate());
 }
Exemplo n.º 14
0
 public void Insert(int index, Phenotype <TGene, TAllele> item)
 {
     _population.Insert(index, item);
 }
Exemplo n.º 15
0
 public int IndexOf(Phenotype <TGene, TAllele> item)
 {
     return(_population.IndexOf(item));
 }
Exemplo n.º 16
0
 public bool Remove(Phenotype <TGene, TAllele> item)
 {
     return(_population.Remove(item));
 }
Exemplo n.º 17
0
 public bool Contains(Phenotype <TGene, TAllele> item)
 {
     return(_population.Contains(item));
 }
Exemplo n.º 18
0
 public void Add(Phenotype <TGene, TAllele> item)
 {
     _population.Add(item);
 }
Exemplo n.º 19
0
 public static Phenotype <DoubleGene, double> NewDoublePhenotype(double value)
 {
     return(Phenotype.Of(Genotype.Of(
                             DoubleChromosome.Of(DoubleGene.Of(value, 0, 10))), 0, Ff
                         ).Evaluate());
 }
Exemplo n.º 20
0
 public Phenotype <TGene, TAllele> NewInstance(Genotype <TGene> genotype, long generation)
 {
     return(Phenotype.Of(genotype, generation, _function, _scaler));
 }
Exemplo n.º 21
0
 public int CompareTo(Phenotype <TGene, TAllele> other)
 {
     return(GetFitness().CompareTo(other.GetFitness()));
 }
Exemplo n.º 22
0
 private static Phenotype <DoubleGene, double> Pt(double value)
 {
     return(Phenotype.Of(Genotype.Of(DoubleChromosome.Of(DoubleGene.Of(value, 0, 10))), 0, Ff, Fs));
 }
Exemplo n.º 23
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));
 }