Пример #1
0
        private IPhenotype SelectOne(Population population, EAMode mode, IRandomNumberGenerator random)
        {
            List <IPhenotype> pool = new List <IPhenotype>(_tournamentSize);

            for (int i = 0; i < _tournamentSize; i++)
            {
                pool.Add(population.DrawRandom(random));
            }

            if (random.NextDouble() < _prob)
            {
                switch (mode)
                {
                case EAMode.MaximizeFitness:
                    return(pool.Max());

                case EAMode.MinimizeFitness:
                    return(pool.Min());

                default:
                    throw new NotImplementedException(mode.ToString());
                }
            }
            else
            {
                return(pool[random.Next(pool.Count)]);
            }
        }
Пример #2
0
 public void SelectAdults(Population offspring, Population population, int n, EAMode eamode)
 {
     population.Clear();
     for (int i = 0; i < n; i++)
     {
         population.Add(offspring[i]);
     }
 }
Пример #3
0
        public void SelectAdults(Population offspring, Population population, int n, EAMode mode)
        {
            var roulette = new Roulette <IPhenotype>(_rng, offspring.Size + population.Size);

            if (mode == EAMode.MaximizeFitness)
            {
                roulette.AddAll(offspring, e => e.Fitness);
                roulette.AddAll(population, e => e.Fitness);

                population.Clear();
                for (int i = 0; i < n; i++)
                {
                    population.Add(roulette.SpinAndRemove());
                }
            }
            else if (mode == EAMode.MinimizeFitness)
            {
                double p_max = Double.MinValue;
                double p_min = Double.MaxValue;

                foreach (var e in offspring)
                {
                    p_max = Math.Max(e.Fitness, p_max);
                    p_min = Math.Min(e.Fitness, p_min);
                }
                foreach (var e in population)
                {
                    p_max = Math.Max(e.Fitness, p_max);
                    p_min = Math.Min(e.Fitness, p_min);
                }

                foreach (var e in offspring)
                {
                    roulette.Add(e, (p_max + p_min) - e.Fitness);
                }
                foreach (var e in population)
                {
                    roulette.Add(e, (p_max + p_min) - e.Fitness);
                }

                population.Clear();
                for (int i = 0; i < n; i++)
                {
                    population.Add(roulette.SpinAndRemove());
                }
            }
            else
            {
                throw new NotImplementedException(mode.ToString());
            }
        }
Пример #4
0
 public IEnumerable <(IPhenotype, IPhenotype)> SelectParents(Population population, int n, EAMode mode, IRandomNumberGenerator random)
 {
     if (!population.IsSorted)
     {
         throw new ArgumentException("Population is not sorted");
     }
     for (int i = 0; i < n; i++)
     {
         yield return(SelectOne(population, random), SelectOne(population, random));
     }
 }
Пример #5
0
        public IEnumerable <(IPhenotype, IPhenotype)> SelectParents(Population population, int n, EAMode mode, IRandomNumberGenerator random)
        {
            var roulette = new AliasRoulette <IPhenotype>(random, population, population.GetProbabilitySelector(mode));

            for (int i = 0; i < n; i++)
            {
                yield return(roulette.Spin(), roulette.Spin());
            }
        }
Пример #6
0
        private const double S = 1; // I think this is configurable, but keep it as is until we are sure.

        public IEnumerable <(IPhenotype, IPhenotype)> SelectParents(Population population, int n, EAMode mode, IRandomNumberGenerator random)
        {
            var std = population.Select(p => p.Fitness).StandardDeviation();
            var avg = population.Select(p => p.Fitness).Average();

            var roulette = new AliasRoulette <IPhenotype>(random, population, p => S + (p.Fitness - avg) / (2 * std));

            for (int i = 0; i < n; i++)
            {
                yield return(roulette.Spin(), roulette.Spin());
            }
        }
Пример #7
0
        public IEnumerable <(IPhenotype, IPhenotype)> SelectParents(Population population, int n, EAMode mode, IRandomNumberGenerator random)
        {
            if (!population.IsSorted)
            {
                throw new ArgumentException("Population is not sorted");
            }
            var roulette = new AliasRoulette <IPhenotype>(random, population, (p, i) => GetProb(i, population.Size));

            for (int i = 0; i < n; i++)
            {
                yield return(roulette.Spin(), roulette.Spin());
            }
        }