Beispiel #1
0
        public void PmxOperatorShouldReturnCorrectChildren()
        {
            IGene[] parent1 = new IGene[]
            {
                new UintGene(1), new UintGene(2), new UintGene(3),
                new UintGene(5), new UintGene(4), new UintGene(6),
                new UintGene(7), new UintGene(8), new UintGene(9)
            };
            IGene[] parent2 = new IGene[]
            {
                new UintGene(4), new UintGene(5), new UintGene(2),
                new UintGene(1), new UintGene(8), new UintGene(7),
                new UintGene(6), new UintGene(9), new UintGene(3)
            };
            IGene[] expectedChild1 = new IGene[]
            {
                new UintGene(8), new UintGene(1), new UintGene(2),
                new UintGene(5), new UintGene(4), new UintGene(6),
                new UintGene(7), new UintGene(9), new UintGene(3)
            };
            IGene[] expectedChild2 = new IGene[]
            {
                new UintGene(5), new UintGene(2), new UintGene(3),
                new UintGene(1), new UintGene(8), new UintGene(7),
                new UintGene(6), new UintGene(4), new UintGene(9)
            };
            int cutPoint1 = 2, cutPoint2 = 6;

            IGene[] child1, child2;
            var     pmx = new PartiallyMatchedCrossover();

            pmx.Run(parent1, parent2, out child1, out child2, cutPoint1, cutPoint2);
            bool areFirstChildrenEqual = true, areSecondChildrenEqual = true;

            for (int i = 0; i < expectedChild1.Length; ++i)
            {
                areFirstChildrenEqual &= expectedChild1[i].Equals(child1[i]);
            }
            for (int i = 0; i < expectedChild2.Length; ++i)
            {
                areSecondChildrenEqual &= expectedChild2[i].Equals(child2[i]);
            }
            Assert.AreEqual(true, areFirstChildrenEqual);
            Assert.AreEqual(true, areSecondChildrenEqual);
        }