public Population <TGene, TAllele> Select(Population <TGene, TAllele> population, int count, Optimize opt) { NonNull(population, "Population"); if (count < 0) { throw new ArgumentOutOfRangeException( $"Selection count must be greater or equal then zero, but was {count}"); } var selection = new Population <TGene, TAllele>(count); if (count > 0 && !population.IsEmpty) { var copy = population.Copy(); copy.SortWith(opt.Descending <TAllele>()); var size = count; do { var length = Math.Min(Math.Min(copy.Count, size), _n); selection.AddAll(copy.SubList(0, length)); size -= length; } while (size > 0); } return(selection); }
private Phenotype <TGene, TAllele> Select(Population <TGene, TAllele> population, Optimize opt, int sampleSize, Random random) { return(SelectAtRandom(population, random).Take(sampleSize) .OrderBy(p => p, opt.Descending <Phenotype <TGene, TAllele> >()).FirstOrDefault()); }