/// <summary> /// Calculates the fitness for all chromosones in this population /// </summary> /// <param name="fitnessCalculator">The fitness.</param> /// <param name="generation">The generation.</param> internal void Calculate(IFitness fitnessCalculator, int generation) { // calculate fitness for all chromosones in parallel var tasks = new List <Task>(); foreach (var chromosome in this) { tasks.Add(Task.Run(() => { bool finished; chromosome.Fitness = fitnessCalculator.CalculateFitness(chromosome, generation, out finished); if (finished) { Finished = true; } })); } // wait until all fitnesses are calculated Task.WaitAll(tasks.ToArray()); // sort chromosones on descending fitness this.Sort(new ChromosomeSorter()); }