示例#1
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);
        }
        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");
            });
        }