public void MutateShouldCheckMutationChance() { // Arrange int worldSize = 4; int geneCount = 6; int swapPosition = 3; int crossOverChance = 0; int mutationChance = 0; RandomStub random = new RandomStub(0, 5); World world = new World(geneCount, worldSize, crossOverChance, mutationChance); Genome genome1 = Genome.FromString("000 111"); Genome genome2 = Genome.FromString("111 001"); world.Population.Add(genome1); world.Population.Add(genome2); // Act world.Mutate(random); string g1 = world.Population[0].ToString(); string g2 = world.Population[1].ToString(); // Assert Assert.That(g1, Is.StringStarting("000 111")); Assert.That(g2, Is.StringStarting("111 001")); }
public void CrossOver() { // // Arrange // int worldSize = 4; int geneCount = 6; int swapPosition = 3; int crossOverChance = 100; int mutationChance = 0; World world = new World(geneCount, worldSize, crossOverChance, mutationChance); RandomStub random = new RandomStub(swapPosition); // - Two genomes: // 000 111 // 101 000 // // - swapPosition = 4 // 000 [111] // 101 [000] // //- New genomes: // 000 000 // 101 111 Genome genome1 = Genome.FromString("000 111"); Genome genome2 = Genome.FromString("101 000"); world.Population.Add(genome2); world.Population.Add(genome1); string g1 = world.Population[0].ToString(); string g2 = world.Population[1].ToString(); // Act Console.WriteLine("Before:"); Console.WriteLine(g1); Console.WriteLine(g2); world.CrossOver(random); // Assert string g1a = world.Population[0].ToString(); string g2a = world.Population[1].ToString(); Console.WriteLine("------"); Console.WriteLine("After:"); Console.WriteLine(g1a); Console.WriteLine(g2a); Assert.That(g1a, Is.StringStarting("000 000")); Assert.That(g2a, Is.StringStarting("101 111")); }
public void Mutate() { // // Arrange // int worldSize = 4; int geneCount = 6; int swapPosition = 3; int crossOverChance = 0; int mutationChance = 100; RandomStub random = new RandomStub(0, 5); World world = new World(geneCount, worldSize, crossOverChance, mutationChance); // - Two genomes: // 000 111 // 111 001 // // Mutate both using position 1 and 5: // // 0[0]0 11[1] = 0[1]0 11[0] // 1[1]1 00[1] = 0[1]0 11[1] Genome genome1 = Genome.FromString("000 111"); Genome genome2 = Genome.FromString("111 001"); world.Population.Add(genome1); world.Population.Add(genome2); string g1 = genome1.ToString(); string g2 = genome2.ToString(); // Act Console.WriteLine("Before:"); Console.WriteLine(g1); Console.WriteLine(g2); world.Mutate(random); // Assert string g1a = genome1.ToString(); string g2a = genome2.ToString(); Console.WriteLine("------"); Console.WriteLine("After:"); Console.WriteLine(g1a); Console.WriteLine(g2a); Assert.That(g1a, Is.StringStarting("100 110")); Assert.That(g2a, Is.StringStarting("111 001")); }
public void BiasedRoulette() { // // Arrange // int worldSize = 4; int geneCount = 6; World world = new World(geneCount, worldSize, 0, 0); RandomStub random = new RandomStub(50); Genome genome1 = Genome.FromString("111 111"); // 14 Genome genome2 = Genome.FromString("110 000"); // 6 Genome genome3 = Genome.FromString("100 000"); // 4 Genome genome4 = Genome.FromString("000 000"); // 0 world.Population.Add(genome1); world.Population.Add(genome2); world.Population.Add(genome3); world.Population.Add(genome4); // // Act // // Total = 24 // G1 = 12 / 24 * 100 = 50 <--- should be picked // G2 = 6 / 24 * 100 = 25 // G3 = 4 / 24 * 100 = 16.6 // G4 = 0 Genome actualGenome1 = world.SpinBiasedRouletteWheel(random); // Assert Assert.That(world.Population.Count, Is.EqualTo(4)); Assert.That(actualGenome1, Is.EqualTo(genome1)); }