public List <IntChromosome> Selection(List <IntChromosome> chromosomes, FitnessFuncGoal goal) { List <IntChromosome> bestChromosomes = new List <IntChromosome>(); while (bestChromosomes.Count < chromosomes.Count) { chromosomeIndexes.AsParallel().ForAll(val => val = random.Next(chromosomes.Count)); bestChromosomes.Add(FindBest(chromosomes, goal)); } return(bestChromosomes); }
private IntChromosome FindBest(List <IntChromosome> chromosomes, FitnessFuncGoal goal) { IntChromosome bestChromosome = null; foreach (int index in chromosomeIndexes) { if (bestChromosome == null || comparator.Compare(bestChromosome, chromosomes.ElementAt(index)) > 0) { bestChromosome = chromosomes.ElementAt(index); } } return(bestChromosome); }
public TournamentSelection(int populationSize, int tournamentSize, FitnessFuncGoal goal) { if (tournamentSize >= 2 && tournamentSize < populationSize) { chromosomeIndexes = new List <int>(tournamentSize); comparator = initComparator(goal); } else { throw new ArgumentException ("Tournament size must be between 2 and population size minus 1."); } }
public IComparer <IntChromosome> initComparator(FitnessFuncGoal goal) { switch (goal) { case FitnessFuncGoal.Min: return(new MinComparator()); case FitnessFuncGoal.Max: return(new MaxComparator()); default: throw new Exception("Fitness function type must be initialized!"); } }
public SphericalFunction(FitnessFuncGoal goal) { this.goal = goal; }