Exemple #1
0
        public static void TestThatChildChanged(this ICrossoverManager crossoverManager)
        {
            var elements = new List <string>()
            {
                "A", "B", "C", "D", "E", "F", "G", "H", "I", "J"
            };
            var generator = new AllElementsVectorChromosomePopulationGenerator <string>(elements, null, null);

            // Since there's a certain chance that this test will fail, I want to run it twice
            var passed = false;

            for (int i = 0; i < 2; i++)
            {
                var parentChromosomes = generator.GeneratePopulation(2);
                var child             = (VectorChromosome <string>)crossoverManager.Crossover(parentChromosomes.ElementAt(0),
                                                                                              parentChromosomes.ElementAt(1));

                try
                {
                    ((VectorChromosome <string>)parentChromosomes.ElementAt(0)).AssertAreNotTheSame(child);
                    ((VectorChromosome <string>)parentChromosomes.ElementAt(1)).AssertAreNotTheSame(child);
                    passed = true;
                }
                catch
                {
                    // Do nothing
                }
            }
            Assert.IsTrue(passed);
        }
Exemple #2
0
        public void InsertionMutationManager_OneGenomeInserted(int vectors)
        {
            var elements = new List <string> {
                "A", "B", "C", "D", "E", "F", "G", "H", "I", "J"
            };
            var generator = new AllElementsVectorChromosomePopulationGenerator <string>(elements, null, null);

            for (int i = 0; i < vectors; i++)
            {
                var before = ((VectorChromosome <string>)generator.GeneratePopulation(1).First()).GetVector();
                var after  = mutationManager.Mutate(before.ToArray());
                var length = after.Length;

                var removedGenomes = 0;

                // The only genome that should meet these conditions is the removed one
                if (after[0] != before[0] && after[0] != before[1])
                {
                    removedGenomes++;
                }
                if (after[length - 1] != before[length - 1] && after[length - 1] != before[length - 2])
                {
                    removedGenomes++;
                }
                for (int k = 1; k < after.Length - 1; k++)
                {
                    if (after[k] != before[k] && after[k] != before[k - 1] && after[k] != before[k + 1])
                    {
                        removedGenomes++;
                    }
                }

                Assert.IsTrue(removedGenomes <= 1, "Only one genome should have been removed");
            }
        }
        public static void TestChromosomeChanged(this IMutationManager <string> mutationManager)
        {
            var elements = new List <string> {
                "A", "B", "C", "D", "E", "F", "G", "H", "I", "J"
            };
            var generator = new AllElementsVectorChromosomePopulationGenerator <string>(elements, null, null);

            // Since there's a certain chance that this test will fail, I want to run it twice
            var passed = false;

            for (int i = 0; i < 4; i++)
            {
                try
                {
                    var before = ((VectorChromosome <string>)generator.GeneratePopulation(1).First()).GetVector();
                    var after  = mutationManager.Mutate(before.ToArray());

                    before.AssertAreNotTheSame(after);
                    passed = true;
                }
                catch
                {
                    // Do nothing
                }
            }
            Assert.IsTrue(passed);
        }
        public void AllElementsVectorChromosomePopulationGenerator_ElementsScattered()
        {
            var elements = new List <string>()
            {
                "A", "B", "C", "D", "E", "F", "G", "H", "I", "J"
            };
            var generator = new AllElementsVectorChromosomePopulationGenerator <string>(elements,
                                                                                        A.Fake <IMutationManager <string> >(), A.Fake <IEvaluator>());
            var chromosomes = generator.GeneratePopulation(1);

            ((VectorChromosome <string>)chromosomes.First()).AssertAreNotTheSame(elements);
        }
        public static void TestAllElementsInEachVector(this IMutationManager <string> mutationManager, int testRuns)
        {
            var elements = new List <string> {
                "A", "B", "C", "D", "E", "F", "G", "H", "I", "J"
            };
            var generator = new AllElementsVectorChromosomePopulationGenerator <string>(elements, null, null);

            for (int i = 0; i < testRuns; i++)
            {
                var before = ((VectorChromosome <string>)generator.GeneratePopulation(1).First()).GetVector();
                var after  = mutationManager.Mutate(before.ToArray());
                before.AssertContainSameElements(after);
            }
        }
Exemple #6
0
        public static void TestThatAllElementsInEachVector(this ICrossoverManager crossoverManager, int testRuns)
        {
            var elements = new List <string>()
            {
                "A", "B", "C", "D", "E", "F", "G", "H", "I", "J"
            };
            var generator = new AllElementsVectorChromosomePopulationGenerator <string>(elements, null, null);

            for (int i = 0; i < testRuns; i++)
            {
                var parentChromosomes = generator.GeneratePopulation(2);
                var child             = (VectorChromosome <string>)crossoverManager.Crossover(parentChromosomes.ElementAt(0), parentChromosomes.ElementAt(1));
                child.AssertContainSameElements(elements);
            }
        }
        public void ExchangeMutationManager_ExactlyTwoGenomesAreDiffrent(int vectors)
        {
            var elements = new List <string> {
                "A", "B", "C", "D", "E", "F", "G", "H", "I", "J"
            };
            var generator = new AllElementsVectorChromosomePopulationGenerator <string>(elements, null, null);

            for (int i = 0; i < vectors; i++)
            {
                var before          = ((VectorChromosome <string>)generator.GeneratePopulation(1).First()).GetVector();
                var after           = mutationManager.Mutate(before.ToArray());
                var diffrentGenomes = before.Where((t, j) => t != after[j]).Count();
                Assert.IsTrue(diffrentGenomes == 0 || diffrentGenomes == 2);
            }
        }
        public void AllElementsVectorChromosomePopulationGenerator_AllElementsInEachVector(int vectors)
        {
            var elements = new List <string>()
            {
                "A", "B", "C", "D", "E", "F", "G", "H", "I", "J"
            };
            var generator = new AllElementsVectorChromosomePopulationGenerator <string>(elements,
                                                                                        A.Fake <IMutationManager <string> >(), A.Fake <IEvaluator>());
            var chromosomes = generator.GeneratePopulation(vectors);

            foreach (var chromosome in chromosomes)
            {
                ((VectorChromosome <string>)chromosome).AssertContainSameElements(elements);
            }
        }
Exemple #9
0
        public void SimpleInversionMutationManager_OneScratchInversed(int vectors)
        {
            var elements = new List <string> {
                "A", "B", "C", "D", "E", "F", "G", "H", "I", "J"
            };
            var generator = new AllElementsVectorChromosomePopulationGenerator <string>(elements, null, null);

            for (int i = 0; i < vectors; i++)
            {
                var before = ((VectorChromosome <string>)generator.GeneratePopulation(1).First()).GetVector();
                var after  = mutationManager.Mutate(before.ToArray());

                var(start, end) = GetStartAndEndInversionPoints(before, after);
                AssertInversionIsCorrect(start, end, before, after);
            }
        }
        public void HeuristicCrossover_NextNodeIsNeighborsOfPreviousNode()
        {
            var elements = new List <string>()
            {
                "A", "B", "C", "D", "E", "F", "G", "H", "I", "J"
            };
            var generator         = new AllElementsVectorChromosomePopulationGenerator <string>(elements, null, null);
            var parentChromosomes = generator.GeneratePopulation(2);

            var child = new HeuristicCrossover <string>(null, null).Crossover(parentChromosomes.ElementAt(0), parentChromosomes.ElementAt(1)).ToArray <string>();

            for (int i = 0; i < child.Length - 1; i++)
            {
                var neigbors = GetNeighbors(parentChromosomes.ElementAt(0).ToArray <string>(),
                                            parentChromosomes.ElementAt(1).ToArray <string>(), child[i]);
                RemoveAlreadyVisitedNeigbors(neigbors, child, i);
                if (neigbors.Count == 0)
                {
                    continue;
                }

                Assert.IsTrue(neigbors.Contains(child[i + 1]), "Didn't jump to neighbor");
            }
        }