src/GenFx.Components.Tests/SingleNodeTreeCrossoverOperatorTest.cs
/// <summary>
/// Tests that the Crossover method works correctly.
/// </summary>
Tests that the Crossover method works correctly.
Tests that the Crossover method works correctly .
[Fact]
        public void SingleNodeTreeCrossoverOperator_Crossover()
        {
            GeneticAlgorithm algorithm = new MockGeneticAlgorithm
            {
                FitnessEvaluator = new MockFitnessEvaluator(),
                SelectionOperator = new MockSelectionOperator(),
                PopulationSeed = new MockPopulation(),
                GeneticEntitySeed = new FakeTreeEntity(),
                CrossoverOperator = new SingleNodeTreeCrossoverOperator
                {
                    CrossoverRate = 1
                }
            };
            SingleNodeTreeCrossoverOperator op = (SingleNodeTreeCrossoverOperator)algorithm.CrossoverOperator;
            op.Initialize(algorithm);
            FakeTreeEntity entity1 = new FakeTreeEntity();
            entity1.Initialize(algorithm);
            entity1.SetRootNode(new FakeTreeNode(1));
            entity1.RootNode.ChildNodes.Add(new FakeTreeNode(2));
            entity1.RootNode.ChildNodes.Add(new FakeTreeNode(3));
            entity1.RootNode.ChildNodes[1].ChildNodes.Add(new FakeTreeNode(4));
            entity1.RootNode.ChildNodes.Add(new FakeTreeNode(5));
            FakeTreeEntity entity2 = new FakeTreeEntity();
            entity2.Initialize(algorithm);
            entity2.SetRootNode(new FakeTreeNode(6));
            entity2.RootNode.ChildNodes.Add(new FakeTreeNode(7));
            entity2.RootNode.ChildNodes[0].ChildNodes.Add(new FakeTreeNode(8));
            entity2.RootNode.ChildNodes[0].ChildNodes[0].ChildNodes.Add(new FakeTreeNode(9));
            RandomNumberService.Instance = new TestRandomUtil();
            IList<GeneticEntity> result = op.Crossover(new GeneticEntity[] { entity1, entity2 }).ToList();
            FakeTreeNode rootNode1 = (FakeTreeNode)((TreeEntityBase)result[0]).RootNode;
            FakeTreeNode rootNode2 = (FakeTreeNode)((TreeEntityBase)result[1]).RootNode;
            Assert.Equal(1, rootNode1.Value);
            Assert.Equal(3, rootNode1.ChildNodes.Count);
            Assert.Equal(2, rootNode1.ChildNodes[0].Value);
            Assert.Equal(7, rootNode1.ChildNodes[1].Value);
            Assert.Single(rootNode1.ChildNodes[1].ChildNodes);
            Assert.Equal(8, rootNode1.ChildNodes[1].ChildNodes[0].Value);
            Assert.Single(rootNode1.ChildNodes[1].ChildNodes[0].ChildNodes);
            Assert.Equal(9, rootNode1.ChildNodes[1].ChildNodes[0].ChildNodes[0].Value);
            Assert.Equal(5, rootNode1.ChildNodes[2].Value);
            Assert.Equal(6, rootNode2.Value);
            Assert.Single(rootNode2.ChildNodes);
            Assert.Equal(3, rootNode2.ChildNodes[0].Value);
            Assert.Single(rootNode2.ChildNodes[0].ChildNodes);
            Assert.Equal(4, rootNode2.ChildNodes[0].ChildNodes[0].Value);
        }
[ Fact ] public void SingleNodeTreeCrossoverOperator_Crossover ( ) { GeneticAlgorithm algorithm = new MockGeneticAlgorithm { FitnessEvaluator = new MockFitnessEvaluator ( ) , SelectionOperator = new MockSelectionOperator ( ) , PopulationSeed = new MockPopulation ( ) , GeneticEntitySeed = new FakeTreeEntity ( ) , CrossoverOperator = new SingleNodeTreeCrossoverOperator { CrossoverRate = 1 } } ; SingleNodeTreeCrossoverOperator op = ( SingleNodeTreeCrossoverOperator ) algorithm . CrossoverOperator ; op . Initialize ( algorithm ) ; FakeTreeEntity entity1 = new FakeTreeEntity ( ) ; entity1 . Initialize ( algorithm ) ; entity1 . SetRootNode ( new FakeTreeNode ( 1 ) ) ; entity1 . RootNode . ChildNodes . Add ( new FakeTreeNode ( 2 ) ) ; entity1 . RootNode . ChildNodes . Add ( new FakeTreeNode ( 3 ) ) ; entity1 . RootNode . ChildNodes [ 1 ] . ChildNodes . Add ( new FakeTreeNode ( 4 ) ) ; entity1 . RootNode . ChildNodes . Add ( new FakeTreeNode ( 5 ) ) ; FakeTreeEntity entity2 = new FakeTreeEntity ( ) ; entity2 . Initialize ( algorithm ) ; entity2 . SetRootNode ( new FakeTreeNode ( 6 ) ) ; entity2 . RootNode . ChildNodes . Add ( new FakeTreeNode ( 7 ) ) ; entity2 . RootNode . ChildNodes [ 0 ] . ChildNodes . Add ( new FakeTreeNode ( 8 ) ) ; entity2 . RootNode . ChildNodes [ 0 ] . ChildNodes [ 0 ] . ChildNodes . Add ( new FakeTreeNode ( 9 ) ) ; RandomNumberService . Instance = new TestRandomUtil ( ) ; IList < GeneticEntity > result = op . Crossover ( new GeneticEntity [ ] { entity1 , entity2 } ) . ToList ( ) ; FakeTreeNode rootNode1 = ( FakeTreeNode ) ( ( TreeEntityBase ) result [ 0 ] ) . RootNode ; FakeTreeNode rootNode2 = ( FakeTreeNode ) ( ( TreeEntityBase ) result [ 1 ] ) . RootNode ; Assert . Equal ( 1 , rootNode1 . Value ) ; Assert . Equal ( 3 , rootNode1 . ChildNodes . Count ) ; Assert . Equal ( 2 , rootNode1 . ChildNodes [ 0 ] . Value ) ; Assert . Equal ( 7 , rootNode1 . ChildNodes [ 1 ] . Value ) ; Assert . Single ( rootNode1 . ChildNodes [ 1 ] . ChildNodes ) ; Assert . Equal ( 8 , rootNode1 . ChildNodes [ 1 ] . ChildNodes [ 0 ] . Value ) ; Assert . Single ( rootNode1 . ChildNodes [ 1 ] . ChildNodes [ 0 ] . ChildNodes ) ; Assert . Equal ( 9 , rootNode1 . ChildNodes [ 1 ] . ChildNodes [ 0 ] . ChildNodes [ 0 ] . Value ) ; Assert . Equal ( 5 , rootNode1 . ChildNodes [ 2 ] . Value ) ; Assert . Equal ( 6 , rootNode2 . Value ) ; Assert . Single ( rootNode2 . ChildNodes ) ; Assert . Equal ( 3 , rootNode2 . ChildNodes [ 0 ] . Value ) ; Assert . Single ( rootNode2 . ChildNodes [ 0 ] . ChildNodes ) ; Assert . Equal ( 4 , rootNode2 . ChildNodes [ 0 ] . ChildNodes [ 0 ] . Value ) ; }
Tests that the Crossover method works correctly.
Tests that the Crossover method works correctly .