Exemple #1
0
        public static Population <TIndividual> ToPopulation <TIndividual>(this Offspring <TIndividual> offspring)
        {
            if (offspring is null)
            {
                throw new ArgumentNullException(nameof(offspring));
            }

            return(new Population <TIndividual>(offspring.ParentPopulation.Fitness, offspring.ParentPopulation.Random, offspring.Children));
        }
Exemple #2
0
        public static Offspring <TIndividual> Eagerly <TIndividual>(this Offspring <TIndividual> offspring)
        {
            if (offspring is null)
            {
                throw new ArgumentNullException(nameof(offspring));
            }

            return(offspring.WithChildren(offspring.Children.ToArray()));
        }
Exemple #3
0
        public static Offspring <TIndividual> WithChildren <TIndividual>(this Offspring <TIndividual> offspring, IEnumerable <TIndividual> children)
        {
            if (offspring is null)
            {
                throw new ArgumentNullException(nameof(offspring));
            }
            if (children is null)
            {
                throw new ArgumentNullException(nameof(children));
            }

            return(new Offspring <TIndividual>(offspring.ParentPopulation, offspring.RandomParents, children));
        }
Exemple #4
0
        public static Offspring <TIndividual> SelectSurvivors <TIndividual>(this Offspring <TIndividual> offspring, int count)
        {
            if (offspring is null)
            {
                throw new ArgumentNullException(nameof(offspring));
            }

            var fitness = offspring.ParentPopulation.Fitness;

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

            return(offspring.WithChildren(children));
        }
Exemple #5
0
        public static Offspring <TIndividual> AddChildren <TIndividual>(this Offspring <TIndividual> offspring, IEnumerable <TIndividual> children)
        {
            if (offspring is null)
            {
                throw new ArgumentNullException(nameof(offspring));
            }
            if (children is null)
            {
                throw new ArgumentNullException(nameof(children));
            }

            var c = offspring
                    .Children
                    .Concat(children);

            return(offspring.WithChildren(c));
        }
Exemple #6
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));
        }
Exemple #7
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));
        }