diff --git a/global.json b/global.json index a05a252d..86b3c9e0 100644 --- a/global.json +++ b/global.json @@ -1,5 +1,5 @@ { "sdk": { - "version": "6.0.400" + "version": "6" } -} \ No newline at end of file +} \ No newline at end of file diff --git a/src/GeneticSharp.Domain/Selections/TournamentSelection.cs b/src/GeneticSharp.Domain/Selections/TournamentSelection.cs index 58c531b5..3b48a761 100644 --- a/src/GeneticSharp.Domain/Selections/TournamentSelection.cs +++ b/src/GeneticSharp.Domain/Selections/TournamentSelection.cs @@ -85,7 +85,7 @@ protected override IList PerformSelectChromosomes(int number, Gener var candidates = generation.Chromosomes.ToList(); var selected = new List(); - while (selected.Count < number) + while (selected.Count < number && Size <= candidates.Count) { var randomIndexes = RandomizationProvider.Current.GetUniqueInts(Size, 0, candidates.Count); var tournamentWinner = candidates.Where((c, i) => randomIndexes.Contains(i)).OrderByDescending(c => c.Fitness).First(); @@ -97,8 +97,15 @@ protected override IList PerformSelectChromosomes(int number, Gener candidates.Remove(tournamentWinner); } } - - return selected; + + while(selected.Count < number && candidates.Any()) + { + var canditate = candidates.First().Clone(); + selected.Add(canditate); + candidates.Remove(canditate); + } + + return selected; } } }