public static List <Individual> BreedNextGeneration_Tournament() { List <Individual> _OldGeneration = Population; List <Individual> _Population_Poll = new List <Individual>(); List <Individual> _NewGeneration = new List <Individual>(); List <Individual> _BreedingRoom = new List <Individual>(); List <Individual> _Tournament_Group = new List <Individual>(); Individual Survivor; Individual _Breeded = new Individual(); int iRandomPickIndex = 0; int iRandomTournamentSize = 0; int _EliteCount; //Add the elites to the new generation. Elites are carried trough generations to preserve the good traits foreach (Individual item in _OldGeneration) { if (item.bIsElite) { _NewGeneration.Add(item); } } _EliteCount = _NewGeneration.Count; //Now breed until a new generation is born //For breeding each iteration a random do { do { foreach (Individual item in _OldGeneration) { _Population_Poll.Add(item); } iRandomTournamentSize = r.Next(2, (_Population_Poll.Count)); Thread.Sleep(100); do { iRandomPickIndex = r.Next(0, (_Population_Poll.Count - 1)); Thread.Sleep(100); _Tournament_Group.Add(_Population_Poll[iRandomPickIndex]); _Population_Poll.RemoveAt(iRandomPickIndex); } while (_Tournament_Group.Count < iRandomTournamentSize); Survivor = World.Tournament_Round(_Tournament_Group); _BreedingRoom.Add(Survivor); _Population_Poll = new List <Individual>(); _OldGeneration.Remove(Survivor); } while (_BreedingRoom.Count < 2); if (_BreedingRoom[0].DNA_Code != _BreedingRoom[1].DNA_Code | bAllowInnBreeding == true) { //Mutate and Add breeded newborn to new generation _Breeded = World.BreedIndividuals(0, 1, _BreedingRoom); _Breeded = _Breeded.Mutate(_Breeded, dMutationChance); _NewGeneration.Add(_Breeded); //Return Parents to old generation _OldGeneration.Add(_BreedingRoom[0]); _OldGeneration.Add(_BreedingRoom[1]); _BreedingRoom.RemoveAt(1); _BreedingRoom.RemoveAt(0); //Re-sort the old generation _OldGeneration.Sort((x, y) => y.dFitnessScore.CompareTo(x.dFitnessScore)); } else { //Return Parents to old generation _OldGeneration.Add(_BreedingRoom[0]); _OldGeneration.Add(_BreedingRoom[1]); _BreedingRoom.RemoveAt(1); _BreedingRoom.RemoveAt(0); //Add a new random individual to the population (an inmigrant) _NewGeneration.Add(new Individual(1)); } //repeat breeding proccess until the new generation is born } while (_NewGeneration.Count < PopulationSize); return(_NewGeneration); }