Example #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)]);
            }
        }
Example #2
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());
            }
        }