private void Mutate(Random rand, ref TextMatchGenome genome) { var sb = new StringBuilder(genome.Text); sb[rand.Next(0, genome.Text.Length)] = _validChars[rand.Next(0, _validChars.Length)]; genome.Text = sb.ToString(); }
private TextMatchGenome[] CreateChildren( TextMatchGenome parent1, TextMatchGenome parent2) { // Crossover parents to create two children TextMatchGenome child1, child2; if (_random.NextDouble() < _settings.CrossoverProbability) { Crossover(_random, parent1, parent2, out child1, out child2); } else { child1 = parent1; child2 = parent2; } // Potentially mutate one or both children if (_random.NextDouble() < _settings.MutationProbability) { Mutate(_random, ref child1); } if (_random.NextDouble() < _settings.MutationProbability) { Mutate(_random, ref child2); } // Return the young'ens return(new[] { child1, child2 }); }
private void Crossover(Random rand, TextMatchGenome p1, TextMatchGenome p2, out TextMatchGenome child1, out TextMatchGenome child2) { int crossoverPoint = rand.Next(1, p1.Text.Length); child1 = new TextMatchGenome { Text = p1.Text.Substring(0, crossoverPoint) + p2.Text.Substring(crossoverPoint), TargetText = _targetText }; child2 = new TextMatchGenome { Text = p2.Text.Substring(0, crossoverPoint) + p1.Text.Substring(crossoverPoint), TargetText = _targetText }; }
private TextMatchGenome[] CreateChildren( TextMatchGenome parent1, TextMatchGenome parent2) { // Crossover parents to create two children TextMatchGenome child1, child2; if (_random.NextDouble() < _settings.CrossoverProbability) { Crossover(_random, parent1, parent2, out child1, out child2); } else { child1 = parent1; child2 = parent2; } // Potentially mutate one or both children if (_random.NextDouble() < _settings.MutationProbability) Mutate(_random, ref child1); if (_random.NextDouble() < _settings.MutationProbability) Mutate(_random, ref child2); // Return the young'ens return new[] { child1, child2 }; }