Пример #1
0
        public void TreeEntityBase_GetSize()
        {
            MockGeneticAlgorithm algorithm = new MockGeneticAlgorithm
            {
                PopulationSeed    = new MockPopulation(),
                SelectionOperator = new MockSelectionOperator(),
                FitnessEvaluator  = new MockFitnessEvaluator(),
                GeneticEntitySeed = new TestTreeEntity()
            };
            TestTreeEntity entity = new TestTreeEntity();

            entity.Initialize(algorithm);

            int size = entity.GetSize();

            Assert.Equal(0, size);

            entity.SetRootNode(new TreeNode());
            entity.RootNode.AppendChild(new TreeNode());
            entity.RootNode.ChildNodes[0].AppendChild(new TreeNode());
            entity.RootNode.AppendChild(new TreeNode());

            size = entity.GetSize();
            Assert.Equal(4, size);
        }
Пример #2
0
        public void TreeNode_CopyTo()
        {
            GeneticAlgorithm algorithm = GetAlgorithm();
            TestTreeEntity   entity    = new TestTreeEntity();

            entity.Initialize(algorithm);
            TreeNode node = new TreeNode();

            entity.SetRootNode(node);
            node.AppendChild(new TreeNode());
            node.Value = 10;
            TestTreeEntity newEntity = new TestTreeEntity();

            newEntity.Initialize(algorithm);
            TreeNode newParent = new TreeNode();

            newEntity.SetRootNode(newParent);
            TreeNode newNode = new TreeNode();

            node.CopyTo(newNode, newEntity, newParent);

            Assert.Same(newEntity, newNode.Tree);
            Assert.Same(newParent, newNode.ParentNode);
            Assert.NotSame(node.ChildNodes[0], newNode.ChildNodes[0]);
            Assert.Equal(node.Value, newNode.Value);
        }
Пример #3
0
        private static TestTreeEntity CreateTestTree()
        {
            TestTreeEntity entity = new TestTreeEntity();
            TreeNode       root   = new TreeNode();

            root.Value = 1;
            entity.SetRootNode(root);

            TreeNode node2 = new TreeNode();

            node2.Value = 2;
            root.ChildNodes.Add(node2);

            TreeNode node3 = new TreeNode();

            node3.Value = 3;
            root.ChildNodes.Add(node3);

            TreeNode node4 = new TreeNode();

            node4.Value = 4;
            node3.ChildNodes.Add(node4);

            return(entity);
        }
Пример #4
0
        public void TreeEntityBase_SetRootNode()
        {
            GeneticAlgorithm algorithm = new MockGeneticAlgorithm
            {
                PopulationSeed    = new MockPopulation(),
                SelectionOperator = new MockSelectionOperator(),
                FitnessEvaluator  = new MockFitnessEvaluator(),
                GeneticEntitySeed = new TestTreeEntity()
            };
            TestTreeEntity entity = new TestTreeEntity();

            entity.Initialize(algorithm);
            TreeNode      node         = new TreeNode();
            TreeNode      childNode    = new TreeNode();
            PrivateObject nodeAccessor = new PrivateObject(childNode);

            nodeAccessor.SetField("parentNode", node);

            TreeNode grandChildNode = new TreeNode();

            node.ChildNodes.Add(childNode);
            childNode.ChildNodes.Add(grandChildNode);

            entity.SetRootNode(childNode);

            Assert.Same(childNode, entity.RootNode);
            Assert.Null(childNode.ParentNode);
            Assert.Same(entity, childNode.Tree);
            Assert.Same(entity, grandChildNode.Tree);
        }
Пример #5
0
        public void TreeNode_AppendChild()
        {
            GeneticAlgorithm algorithm = GetAlgorithm();
            TestTreeEntity   entity    = new TestTreeEntity();

            entity.Initialize(algorithm);
            TreeNode node = new TreeNode();

            entity.SetRootNode(node);

            TreeNode child = new TreeNode();

            node.AppendChild(child);
            Assert.Same(child, node.ChildNodes[0]);
            Assert.Same(node, child.ParentNode);
            Assert.Same(entity, child.Tree);
        }
Пример #6
0
        public void MeanTreeSize_GetResultValue()
        {
            GeneticAlgorithm algorithm = new MockGeneticAlgorithm
            {
                SelectionOperator = new MockSelectionOperator(),
                FitnessEvaluator  = new MockFitnessEvaluator(),
                GeneticEntitySeed = new TestTreeEntity(),
                PopulationSeed    = new SimplePopulation(),
            };

            algorithm.Metrics.Add(new MeanTreeSize());

            MeanTreeSize target = new MeanTreeSize();

            target.Initialize(algorithm);
            SimplePopulation population = new SimplePopulation();

            population.Initialize(algorithm);

            TreeEntityBase entity = new TestTreeEntity();

            entity.Initialize(algorithm);
            entity.SetRootNode(new TreeNode());
            entity.RootNode.ChildNodes.Add(new TreeNode());
            entity.RootNode.ChildNodes.Add(new TreeNode());
            entity.RootNode.ChildNodes[0].ChildNodes.Add(new TreeNode());
            population.Entities.Add(entity);

            entity = new TestTreeEntity();
            entity.Initialize(algorithm);
            entity.SetRootNode(new TreeNode());
            population.Entities.Add(entity);

            entity = new TestTreeEntity();
            entity.Initialize(algorithm);
            entity.SetRootNode(new TreeNode());
            entity.RootNode.ChildNodes.Add(new TreeNode());
            population.Entities.Add(entity);

            object result = target.GetResultValue(population);

            Assert.Equal(2.33, Math.Round((double)result, 2));
        }
Пример #7
0
        public void TreeNodeOfT_Clone()
        {
            TestTreeEntity entity = new TestTreeEntity();
            TreeNode <int> node   = new TreeNode <int>();

            entity.SetRootNode(node);
            node.AppendChild(new TreeNode());
            node.Value = 10;
            TestTreeEntity newEntity = new TestTreeEntity();
            TreeNode       newParent = new TreeNode();

            newEntity.SetRootNode(newParent);
            TreeNode clone = node.Clone(newEntity, newParent);

            Assert.NotSame(node, clone);
            Assert.NotSame(node.ChildNodes[0], clone.ChildNodes[0]);
            Assert.Same(newEntity, clone.Tree);
            Assert.Same(newParent, clone.ParentNode);
            Assert.Equal(node.Value, clone.Value);
        }
Пример #8
0
        public void TreeEntityBase_CopyTo()
        {
            MockGeneticAlgorithm algorithm = new MockGeneticAlgorithm
            {
                PopulationSeed    = new MockPopulation(),
                SelectionOperator = new MockSelectionOperator(),
                FitnessEvaluator  = new MockFitnessEvaluator(),
                GeneticEntitySeed = new TestTreeEntity()
            };
            TestTreeEntity entity = new TestTreeEntity();

            entity.Initialize(algorithm);
            entity.Age = 10;
            entity.SetRootNode(new TreeNode());
            TestTreeEntity newEntity = new TestTreeEntity();

            newEntity.Initialize(algorithm);
            entity.CopyTo(newEntity);

            Assert.Equal(entity.Age, newEntity.Age);
            Assert.NotNull(entity.RootNode);
        }
Пример #9
0
        public void TreeNode_InsertChild()
        {
            GeneticAlgorithm algorithm = GetAlgorithm();
            TestTreeEntity   entity    = new TestTreeEntity();

            entity.Initialize(algorithm);
            TreeNode node = new TreeNode();

            entity.SetRootNode(node);

            TreeNode child1 = new TreeNode();
            TreeNode child2 = new TreeNode();

            node.InsertChild(0, child1);
            node.InsertChild(0, child2);

            Assert.Same(child2, node.ChildNodes[0]);
            Assert.Same(node, child2.ParentNode);
            Assert.Same(entity, child2.Tree);
            Assert.Same(child1, node.ChildNodes[1]);
            Assert.Same(node, child1.ParentNode);
            Assert.Same(entity, child1.Tree);
        }
Пример #10
0
        public void TreeEntityBase_SetRootNode_NullNode()
        {
            TestTreeEntity tree = new TestTreeEntity();

            Assert.Throws <ArgumentNullException>(() => tree.SetRootNode(null));
        }