Exemplo n.º 1
0
        public void GenerateIndividualTest()
        {
            TownHelper.Initialize();

            var individual = WorldHelper.GenerateIndividual(Configuration.TownCount);

            // Ensure that individual contains no repeated values
            var uniqueValueGroups = individual.Sequence.GroupBy(s => s);

            Assert.IsTrue(uniqueValueGroups.All(g => g.Count() == 1));
        }
Exemplo n.º 2
0
        public void EnsureSwapMutationResultTest()
        {
            TownHelper.Initialize();

            var individual = new Individual(new List <int> {
                0, 1, 2, 3, 4, 5, 6, 7, 8, 9
            });

            var result = WorldHelper.DoSwapMutate(individual);

            // Ensure that all values are still in the list
            var distinctResultCount = result.Sequence.Distinct().Count();

            Assert.AreEqual(distinctResultCount, 10);

            // Ensure that there are no duplicate entries
            var sequences = result.Sequence.GroupBy(s => s);

            Assert.IsTrue(sequences.All(s => s.Count() == 1));

            // Perform manual swap to ensure result is correct
            var firstIndex = -1;
            var lastIndex  = -1;

            for (int i = 0; i < individual.Sequence.Count; i++)
            {
                if (firstIndex == -1 && result.Sequence[i] != individual.Sequence[i])
                {
                    firstIndex = i;
                    continue;
                }

                if (firstIndex != -1 && result.Sequence[i] != individual.Sequence[i])
                {
                    lastIndex = i;
                    break;
                }
            }

            var originalSequence = individual.Sequence.ToList();

            originalSequence.SwapInPlace(firstIndex, lastIndex);

            Assert.IsTrue(originalSequence.SequenceEqual(result.Sequence));
        }
Exemplo n.º 3
0
        public void EnsureRotationMutationResultTest()
        {
            TownHelper.Initialize();

            var individual = new Individual(new List <int> {
                0, 1, 2, 3, 4, 5, 6, 7, 8, 9
            });

            var result = WorldHelper.DoRotateMutate(individual);

            // Ensure that all values are still in the list
            var distinctResultCount = result.Sequence.Distinct().Count();

            Assert.AreEqual(distinctResultCount, 10);

            // Ensure that there are no duplicate entries
            var sequences = result.Sequence.GroupBy(s => s);

            Assert.IsTrue(sequences.All(s => s.Count() == 1));
        }
Exemplo n.º 4
0
        public void EnsureSpawningUniqueTest()
        {
            TownHelper.Initialize();

            var population = WorldHelper.SpawnPopulation();

            var populationSequences = population.Select(i => i.Sequence);

            // Manually check that each individual in the population is unique
            foreach (var sequenceA in populationSequences)
            {
                foreach (var sequenceB in populationSequences)
                {
                    if (sequenceA == sequenceB)
                    {
                        continue;
                    }

                    Assert.IsTrue(!sequenceA.SequenceEqual(sequenceB));
                }
            }
        }
Exemplo n.º 5
0
        public void EnsureCrossoverTest()
        {
            TownHelper.Initialize();

            var individualA = new Individual(new List <int> {
                0, 9, 1, 8, 2, 7, 3, 6, 4, 5
            });
            var individualB = new Individual(new List <int> {
                0, 1, 2, 3, 4, 5, 6, 7, 8, 9
            });

            var crossoverPointA = 3;
            var crossoverPointB = 1;
            var crossoverPointC = 8;

            var childA = WorldHelper.DoCrossover(individualA, individualB, crossoverPointA);
            var childB = WorldHelper.DoCrossover(individualA, individualB, crossoverPointB);
            var childC = WorldHelper.DoCrossover(individualA, individualB, crossoverPointC);

            var expectedChildASequence = new List <int> {
                0, 9, 1, 2, 3, 4, 5, 6, 7, 8
            };
            var expectedChildBSequence = new List <int> {
                0, 1, 2, 3, 4, 5, 6, 7, 8, 9
            };
            var expectedChildCSequence = new List <int> {
                0, 9, 1, 8, 2, 7, 3, 6, 4, 5
            };

            Assert.IsTrue(childA.Sequence.SequenceEqual(expectedChildASequence));
            Assert.IsTrue(childB.Sequence.SequenceEqual(expectedChildBSequence));
            Assert.IsTrue(childC.Sequence.SequenceEqual(expectedChildCSequence));

            Assert.AreEqual(childA.Sequence.Count(), individualA.Sequence.Count());
            Assert.AreEqual(childB.Sequence.Count(), individualA.Sequence.Count());
            Assert.AreEqual(childC.Sequence.Count(), individualA.Sequence.Count());
        }