public string GetTree(string sourceFile)
        {
            _sourceFile = sourceFile;
            RawDataSource samples = new RawDataSource(_sourceFile);

            TreeAttributeCollection attributes = samples.GetValidAttributeCollection();

            DecisionTree id3  = new DecisionTree();
            TreeNode     root = id3.mountTree(samples, "result", attributes);

            return(PrintNode(root, "") + Environment.NewLine + PrologPrintNode(root, "", "result"));
        }
        public void GenRulesFromMt(SIProlog pEngine, string sourceMt, string destMt, string targetAttribute)
        {
            MtDataSource samples = new MtDataSource(pEngine, sourceMt);

            TreeAttributeCollection attributes = samples.GetValidAttributeCollection(targetAttribute);

            DecisionTree id3  = new DecisionTree();
            TreeNode     root = id3.mountTree(samples, targetAttribute, attributes);

            string prologCode = PrologPrintNode(root, "", targetAttribute);

            pEngine.insertKB(prologCode, destMt);
            string codeSummary = PrintNode(root, "") + Environment.NewLine + PrologPrintNode(root, "", "result");

            Console.WriteLine(codeSummary);
        }
        /// <summary>
        ///Returns the best attribute.
        /// </summary>
        /// <param name="attributes"> A vector with attributes </param>
        /// <returns>Returns which has higher gain </returns>
        private TreeAttribute getBestAttribute(DataTable samples, TreeAttributeCollection attributes)
        {
            double        maxGain = -9999999.0;
            TreeAttribute result  = null;

            foreach (TreeAttribute attribute in attributes)
            {
                double aux = gain(samples, attribute);
                if (aux > maxGain)
                {
                    maxGain = aux;
                    result  = attribute;
                }
            }
            return(result);
        }
        public TreeAttributeCollection GetValidAttributeCollection()
        {
            TreeAttributeCollection returnCollection = new TreeAttributeCollection();

            foreach (DataColumn column in this.Columns)
            {
                TreeAttribute currentAttribute = new TreeAttribute(column.ColumnName, GetValuesFromColumn(column.ColumnName));

                if (returnCollection.ContainsAttribute(currentAttribute) || currentAttribute.AttributeName.ToUpper().Trim() == "RESULT")
                {
                    continue;
                }
                returnCollection.Add(currentAttribute);
            }
            return(returnCollection);
        }
 /// <summary>
 /// Sets up a decision tree based on samples submitted
 /// </summary>
 /// <param name="samples">Table with samples that will be provided for mounting the tree </param>
 /// <param name="targetAttribute">Name column of the table that otherwise has the value true or false to
 ///  Validate or not a sample </param>
 /// <returns>The root of the decision tree mounted</returns></returns?>
 public TreeNode mountTree(DataTable samples, string targetAttribute, TreeAttributeCollection attributes)
 {
     _sampleData = samples;
     return(internalMountTree(_sampleData, targetAttribute, attributes));
 }
        /// <summary>
        /// Sets up a decision tree based on samples submitted
        /// </summary>
        /// <param name="samples">Table with samples that will be provided for mounting the tree </param>
        /// <param name="targetAttribute"> Name column of the table that otherwise has the value true or false to
        /// Validate or not a sample</param>
        /// <returns>The root of the decision tree mounted </returns></returns?>
        private TreeNode internalMountTree(DataTable samples, string targetAttribute, TreeAttributeCollection attributes)
        {
            if (allSamplesAreUniform(samples, targetAttribute) == true)
            {
                return(new TreeNode(new OutcomeTreeAttribute(getUniformRefValue(samples, targetAttribute))));
            }
            //if (allSamplesArePositive(samples, targetAttribute) == true)
            //    return new TreeNode(new OutcomeTreeAttribute(true));

            //if (allSamplesAreNegative(samples, targetAttribute) == true)
            //    return new TreeNode(new OutcomeTreeAttribute(false));


            if (attributes.Count == 0)
            {
                return(new TreeNode(new OutcomeTreeAttribute(getMostCommonValue(samples, targetAttribute))));
            }
            mTotal           = samples.Rows.Count;
            mTargetAttribute = targetAttribute;
            mTotalPositives  = countTotalPositives(samples);

            mEntropySet = getCalculatedEntropy(mTotalPositives, mTotal - mTotalPositives);

            TreeAttribute bestAttribute = getBestAttribute(samples, attributes);

            TreeNode root = new TreeNode(bestAttribute);

            if (bestAttribute == null)
            {
                return(root);
            }
            PossibleValueCollection bestAttrValues = bestAttribute.PossibleValues;
            bool continousSet = isContinousSet(bestAttrValues);

            //DataTable aSample = samples.Clone();
            if (continousSet)
            {
                string value = bestSplitValue(samples, bestAttribute);
                {
                    DataTable aSample = samples.Clone();
                    //First Below then Above
                    DataRow[] rows;
                    string    cond = bestAttribute.AttributeName + " <= " + "" + value + "";
                    rows = samples.Select(cond);

                    aSample.Rows.Clear();
                    foreach (DataRow row in rows)
                    {
                        aSample.Rows.Add(row.ItemArray);
                        Console.WriteLine(" SPLIT {0} ROW:", cond);
                        foreach (DataColumn myCol in samples.Columns)
                        {
                            Console.WriteLine("  " + row[myCol]);
                        }
                    }
                    // Create a new attribute list unless the attribute which is the current best attribute
                    TreeAttributeCollection aAttributes = new TreeAttributeCollection();
                    //ArrayList aAttributes = new ArrayList(attributes.Count - 1);
                    for (int i = 0; i < attributes.Count; i++)
                    {
                        if (attributes[i].AttributeName != bestAttribute.AttributeName)
                        {
                            aAttributes.Add(attributes[i]);
                        }
                    }
                    //Recycle the best continous attribute if there are others
                    if (aAttributes.Count > 0)
                    {
                        aAttributes.Add(bestAttribute);
                    }

                    // Create a new attribute list unless the attribute which is the current best attribute

                    if (rows.Length == 0)
                    {
                        //return new TreeNode(new OutcomeTreeAttribute(getMostCommonValue(aSample, targetAttribute)));
                        return(new TreeNode(new OutcomeTreeAttribute(getMostCommonValue(samples, targetAttribute))));
                    }
                    else
                    {
                        DecisionTree dc3       = new DecisionTree();
                        TreeNode     ChildNode = dc3.mountTree(aSample, targetAttribute, aAttributes);
                        root.AddTreeNode(ChildNode, value, "leq");
                    }
                }
                {
                    DataTable aSample = samples.Clone();
                    DataRow[] rows2;
                    string    cond = bestAttribute.AttributeName + " > " + "" + value + "";
                    rows2 = samples.Select(cond);

                    aSample.Rows.Clear();
                    foreach (DataRow row in rows2)
                    {
                        aSample.Rows.Add(row.ItemArray);
                        Console.WriteLine(" SPLIT {0} ROW:", cond);
                        foreach (DataColumn myCol in samples.Columns)
                        {
                            Console.WriteLine("  " + row[myCol]);
                        }
                    }
                    // Create a new attribute list unless the attribute which is the current best attribute
                    TreeAttributeCollection aAttributes2 = new TreeAttributeCollection();
                    //ArrayList aAttributes = new ArrayList(attributes.Count - 1);
                    for (int i = 0; i < attributes.Count; i++)
                    {
                        if (attributes[i].AttributeName != bestAttribute.AttributeName)
                        {
                            aAttributes2.Add(attributes[i]);
                        }
                    }
                    //Recycle the best continous attribute if there are others
                    if (aAttributes2.Count > 0)
                    {
                        aAttributes2.Add(bestAttribute);
                    }

                    // Create a new attribute list unless the attribute which is the current best attribute

                    if (rows2.Length == 0)
                    {
                        //return new TreeNode(new OutcomeTreeAttribute(getMostCommonValue(aSample, targetAttribute)));
                        return(new TreeNode(new OutcomeTreeAttribute(getMostCommonValue(samples, targetAttribute))));
                    }
                    else
                    {
                        DecisionTree dc3       = new DecisionTree();
                        TreeNode     ChildNode = dc3.mountTree(aSample, targetAttribute, aAttributes2);
                        root.AddTreeNode(ChildNode, value, "gt");
                    }
                }
            }
            else
            {
                DataTable aSample = samples.Clone();
                foreach (string value in bestAttribute.PossibleValues)
                {
                    // Select all elements with the value of this attribute
                    aSample.Rows.Clear();

                    DataRow[] rows;

                    rows = samples.Select(bestAttribute.AttributeName + " = " + "'" + value + "'");

                    foreach (DataRow row in rows)
                    {
                        aSample.Rows.Add(row.ItemArray);
                    }
                    // Select all elements with the value of this attribute

                    // Create a new attribute list unless the attribute which is the current best attribute
                    TreeAttributeCollection aAttributes = new TreeAttributeCollection();
                    //ArrayList aAttributes = new ArrayList(attributes.Count - 1);
                    for (int i = 0; i < attributes.Count; i++)
                    {
                        if (attributes[i].AttributeName != bestAttribute.AttributeName)
                        {
                            aAttributes.Add(attributes[i]);
                        }
                    }
                    // Create a new attribute list unless the attribute which is the current best attribute

                    if (aSample.Rows.Count == 0)
                    {
                        //return new TreeNode(new OutcomeTreeAttribute(getMostCommonValue(aSample, targetAttribute)));
                        return(new TreeNode(new OutcomeTreeAttribute(getMostCommonValue(samples, targetAttribute))));
                    }
                    else
                    {
                        DecisionTree dc3       = new DecisionTree();
                        TreeNode     ChildNode = dc3.mountTree(aSample, targetAttribute, aAttributes);
                        root.AddTreeNode(ChildNode, value, "eq");
                    }
                }
            }


            return(root);
        }