コード例 #1
0
ファイル: Train.cs プロジェクト: stgolarrain/OC1
        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());
        }
コード例 #2
0
ファイル: Train.cs プロジェクト: stgolarrain/OC1
        private void pertube(int m, Node node, double[][] dataInput)
        {
            if (!node.ActiveAtributtes[m])
                return;

            Node auxNode = (Node)node.Clone();
            double[] U = new double[dataInput.Length];
            for (int i = 0; i < dataInput.Length; i++)
                U[i] = Math.Min((node.weights[m] * dataInput[i][m] - node.Evaluate(dataInput[i])) / dataInput[i][m], 0);
            Array.Sort(U);

            int bestA = 0;
            int dif = Int16.MaxValue;
            for (int i = 0; i < U.Length; i++)
            {
                int positive = 0;
                int negative = 0;
                for (int j = 0; j < U.Length; j++)
                {
                    if (j <= i && U[j] <= 0)
                        negative++;
                    else if (j > i && U[j] > 0)
                        positive++;
                }
                if (Math.Abs(positive - negative) < dif)
                {
                    dif = Math.Abs(positive - negative);
                    bestA = i;
                }

            }

            auxNode.weights[m] = U[bestA];
            if (gain(dataInput, auxNode) > gain(dataInput, node))
            {
                node.weights[m] = U.Max();
                node.ResetPmove();
            }
            else if (gain(dataInput, auxNode) == gain(dataInput, node))
            {
                if(node.EvaluatePmove())
                    node.weights[m] = U.Max();
                node.DecreasePmove();
            }
        }