Exemple #1
0
 public int CountNodes(Node node)
 {
     int result = 1;
     if (node.GetLeft() != null)
         result += CountNodes(node.GetLeft());
     if (node.GetRight() != null)
         result += CountNodes(node.GetRight());
     return result;
 }
Exemple #2
0
 public int CountLeafLastNodes(Node node)
 {
     int result = 0;
     if (node.GetLeft() != null)
         result += CountLeafLastNodes(node.GetLeft());
     if (node.GetRight() != null)
         result += CountLeafLastNodes(node.GetRight());
     if (node.GetLeft() == null && node.GetRight() == null)
         return node.leaf;
     return result;
 }
Exemple #3
0
        public void Algorithm(double[][] dataInput, Node node)
        {
            /*
            * Choose class for node if is the case.
            */
            node.classType = sameClass(dataInput);
            if (node.classType != -1)
            {
                node.leaf = dataInput.Length;
                return;
            }

            node.classType = sameAtributtes(dataInput);
            if (node.classType != -1)
            {
                node.leaf = dataInput.Length;
                return;
            }

            /*
             * Choose best axis-parallel split node for dataInput.
             */
            bestAxisParallel(dataInput, node);

            /*
             * Apply OC1
             */
            Node auxNode = (Node)node.Clone();
            for (int i = 0; i < R; i++)
            {
                if (i > 0)
                    auxNode.Random();

            step1:
                double initialGain = gain(dataInput, node); // Gain for better node.
                for (int d = 0; d < dataInput[0].Length; d++)
                {
                    pertube(d, auxNode, dataInput);
                    if (gain(dataInput, auxNode) > initialGain)
                    {
                        node.SetNode(auxNode);
                        break;
                    }
                }

                // step 2
                initialGain = gain(dataInput, node);
                for (int j = 0; j < J; j++)
                {
                    Random rand = new Random();
                    int direction = rand.Next(dataInput[0].Length - 1);
                    pertube(direction, auxNode, dataInput);
                    if (gain(dataInput, auxNode) > initialGain)
                    {
                        node.SetNode(auxNode);
                        goto step1;
                    }
                }
                if (gain(dataInput, auxNode) > gain(dataInput, node))
                    node.SetNode(auxNode);
            }

            /*
             * Recursive Algorithm for each branch for node.
             */
            Node left = new Node(new double[dataInput[0].Length], 0);
            Node right = new Node(new double[dataInput[0].Length], 0);

            node.SetLeft(left);
            node.SetRight(right);
            List<double[]>[] data = divideData(dataInput, node);
            Algorithm(data[0].ToArray(), node.GetLeft());
            Algorithm(data[1].ToArray(), node.GetRight());
        }
Exemple #4
0
        private void Prunning(int n, Node node)
        {
            if (node.GetLeft() != null)
                Prunning(n, node.GetLeft());
            if (node.GetRight() != null)
                Prunning(n, node.GetRight());

            if (node.GetRight() == null && node.GetLeft() == null)
                return;

            if (node.GetRight() == null && node.GetLeft().leaf < n)
            {
                node.leaf += node.GetLeft().leaf;
                node.classType = node.GetLeft().classType;
                node.RemoveLeft();
            }
            else if (node.GetLeft() == null && node.GetRight().leaf < n)
            {
                node.leaf += node.GetRight().leaf;
                node.classType = node.GetRight().classType;
                node.RemoveRight();
            }

            else if (node.GetRight().leaf < n && node.GetLeft().leaf >= n)
            {
                node.classType = node.GetRight().classType;
                node.leaf += node.GetRight().leaf;
                node.RemoveRight();
            }
            else if (node.GetRight().leaf >= n && node.GetLeft().leaf < n)
            {
                node.classType = node.GetLeft().classType;
                node.leaf += node.GetLeft().leaf;
                node.RemoveLeft();
            }
            else if (node.GetRight().leaf < n && node.GetLeft().leaf < n)
            {
                if (node.GetRight().leaf > node.GetLeft().leaf)
                    node.classType = node.GetRight().classType;
                else
                    node.classType = node.GetLeft().classType;

                node.leaf += node.GetLeft().leaf + node.GetRight().leaf;
                node.RemoveLeft();
                node.RemoveRight();
            }
        }