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)]); } }
public void SelectAdults(Population offspring, Population population, int n, EAMode eamode) { population.Clear(); for (int i = 0; i < n; i++) { population.Add(offspring[i]); } }
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()); } }
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)); } }
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()); } }
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()); } }
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()); } }