Ejemplo n.º 1
0
        public void TournamentParentSelection_VerifySelectionProbability()
        {
            // Test tournament selection by comparing it to the old "established" tournament selection implementation.
            var random = new DefaultRandomNumberGenerator("seed".GetHashCode());

            for (int tournamentSize = 2; tournamentSize <= 10; tournamentSize++)
            {
                var population = new Population(11);
                var index      = 0;
                population.Fill(() => new TestPhenotype(index++, index * index));
                population.Sort(EAMode.MaximizeFitness);

                var config = new EAConfiguration
                {
                    TournamentSize        = 3,
                    TournamentProbability = 0.5
                };

#pragma warning disable CS0618 // Type or member is obsolete
                var oldselection = new TournamentParentSelectionOld(config);
#pragma warning restore CS0618 // Type or member is obsolete
                var oldselected = oldselection.SelectParents(population, 275000, EAMode.MaximizeFitness, random);

                foreach (var(a, b) in oldselected)
                {
                    ((TestPhenotype)a).Count2++;
                    ((TestPhenotype)b).Count2++;
                }

                var selection = new TournamentParentSelection(config);
                var selected  = selection.SelectParents(population, 275000, EAMode.MaximizeFitness, random);

                foreach (var(a, b) in selected)
                {
                    ((TestPhenotype)a).Count++;
                    ((TestPhenotype)b).Count++;
                }

                var counts = population.Cast <TestPhenotype>().Select(p => p.Count).Zip(population.Cast <TestPhenotype>().Select(p => p.Count2), (c1, c2) => (c1, c2));

                foreach (var count in counts)
                {
                    (count.c1 - count.c2).Should().BeLessThan(1000, $"tournament size = {tournamentSize}");
                }
            }
        }
Ejemplo n.º 2
0
        public void TournamentSelection_PerfTest()
        {
            var random = new DefaultRandomNumberGenerator();

            var config = new EAConfiguration
            {
                TournamentSize        = 20,
                TournamentProbability = 0.5
            };

            var popsize = 10000;
            var pop     = new Population(popsize);

            pop.Fill(() => CreatePhenotypeMock(random.NextDouble() * 10).Object);
            pop.Sort(EAMode.MaximizeFitness);

            Console.WriteLine($"popsize = {popsize}, tournament size = {config.TournamentSize}");

            var watch = new Stopwatch();

            watch.Start();
#pragma warning disable CS0618 // Type or member is obsolete
            IParentSelection selection = new TournamentParentSelectionOld(config);
#pragma warning restore CS0618 // Type or member is obsolete
            foreach (var selected in selection.SelectParents(pop, popsize, EAMode.MaximizeFitness, random))
            {
            }
            watch.Stop();
            Console.WriteLine($"old: {watch.ElapsedMilliseconds} ms");

            watch.Restart();
            selection = new TournamentParentSelection(config);
            foreach (var selected in selection.SelectParents(pop, popsize, EAMode.MaximizeFitness, random))
            {
            }
            watch.Stop();
            Console.WriteLine($"new: {watch.ElapsedMilliseconds} ms");
        }