public void Mutate(Random random = null) { EnsurePopulationIsCreated(); if (random == null) { random = _random; } // Figure out if mutation should occur for this generation, based on a roll of a random number decimal percentage = _mutationChance; int randomNumber = random.Next(1, 100); if (percentage > 0 && randomNumber <= percentage) { // Loop through all genome pairs for (int i = 0; i < Population.Count; i += 2) { if (i > Population.Count) { break; } Genome genome = Population[i]; // Pick two random positions to swap at int position1 = random.Next(0, _geneSize); int position2 = random.Next(0, _geneSize); genome.SwapGenes(position1, position2); } } else { // (No mutation, return) if (ShowDebugMessages) { Console.WriteLine("No mutation performed - the random {0}% was over the {1}% threshold.", randomNumber, percentage); } } }
public void Mutate(Random random = null) { EnsurePopulationIsCreated(); if (random == null) { random = _random; } decimal percentage = _mutationChance; int randomNumber = random.Next(1, 100); if (percentage > 0 && randomNumber <= percentage) { for (int i = 0; i < Population.Count; i += 2) { if (i > Population.Count) { break; } Genome genome = Population[i]; int position1 = random.Next(0, _geneSize); int position2 = random.Next(0, _geneSize); genome.SwapGenes(position1, position2); } } else { if (ShowDebugMessages) { Console.WriteLine("No mutation performed - the random {0}% was over the {1}% threshold.", randomNumber, percentage); } } }