Esempio n. 1
0
        public static Offspring <TIndividual> SelectElite <TIndividual>(this Offspring <TIndividual> offspring, int count)
        {
            if (offspring is null)
            {
                throw new ArgumentNullException(nameof(offspring));
            }

            var fitness = offspring.ParentPopulation.Fitness;

            var children = offspring
                           .ParentPopulation
                           .Individuals
                           .OrderBy(fitness)
                           .Take(count);

            return(offspring.AddChildren(children));
        }
Esempio n. 2
0
        public static Offspring <TIndividual> Recombine <TIndividual>(this Offspring <TIndividual> offspring, int count, Func <TIndividual, TIndividual, TRandom, TIndividual> recombine)
        {
            if (offspring is null)
            {
                throw new ArgumentNullException(nameof(offspring));
            }
            if (recombine is null)
            {
                throw new ArgumentNullException(nameof(recombine));
            }

            var random = offspring.ParentPopulation.Random;

            var children = offspring.RandomParents
                           .Zip(offspring.RandomParents, (a, b) => recombine(a, b, random))
                           .Take(count);

            return(offspring.AddChildren(children));
        }
Esempio n. 3
0
        public static Offspring <TIndividual> Mutate <TIndividual>(this Offspring <TIndividual> offspring, int count, Func <TIndividual, TRandom, TIndividual> mutate)
        {
            if (offspring is null)
            {
                throw new ArgumentNullException(nameof(offspring));
            }
            if (mutate is null)
            {
                throw new ArgumentNullException(nameof(mutate));
            }

            var random = offspring.ParentPopulation.Random;

            var children = offspring.RandomParents
                           .Select(p => mutate(p, random))
                           .Take(count);

            return(offspring.AddChildren(children));
        }