Пример #1
0
    //#########################################################
    //Left Over data serilization - use with caution.
    //########################################################

    public static void PrintTree(DecisionTree.DecisionTree n)
    {
        using (StreamWriter s = new StreamWriter(Application.persistentDataPath + "/DecisionTree.txt", false))
        {
            s.Write(RecursiveTreePrintBuilder(n.RootNode, null).ToString());
        }
    }
Пример #2
0
 //TODO Need a similiar structure for the Association rules indexed by actor(type) preferably.
 //TrainingExample[] examples, Feature[] features, DecisionNode decisionNode
 public static void CreateTree(DecisionTree.TrainingExample[] examples)
 {
     try
     {
         DecisionTrees.Add(examples[0].Actor.name, new DecisionTree.DecisionTree(examples, examples[0].Features));
         AIContainer d = new AIContainer();
         d.Key   = examples[0].Actor.name;
         d.DTree = DecisionTrees[examples[0].Actor.name];
         XMLManager.SaveAI(d);
     }
     catch (ArgumentException)
     {
         DecisionTree.DecisionTree d = new DecisionTree.DecisionTree(examples, examples[0].Features);
         Debug.Log("There already exists a Decision Tree for this actor!, Overwriting previous tree.");
         DecisionTrees[examples[0].Actor.name] = d;
         AIContainer con = new AIContainer();
         con.Key   = examples[0].Actor.name;
         con.DTree = DecisionTrees[examples[0].Actor.name];
         XMLManager.SaveAI(con);
     }
 }
Пример #3
0
        private TreeNode buildTree(DataTable samples, string targetAttribute, TreeAttributeCollection attributes)
        {
            if (samples == null)
            {
                return(new TreeNode(new OutcomeTreeAttribute(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 = calculateEntropy(mTotalPositives, mTotal - mTotalPositives);

            TreeAttribute bestAttribute = getBestAttribute(samples, attributes);

            TreeNode root = new TreeNode(bestAttribute);

            if (bestAttribute == null)
            {
                return(root);
            }

            DataTable aSample = samples.Clone();

            foreach (string value in bestAttribute.PossibleValues)
            {
                aSample.Rows.Clear();

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

                foreach (DataRow row in rows)
                {
                    aSample.Rows.Add(row.ItemArray);
                }

                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]);
                    }
                }

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

            return(root);
        }
        static void Main(string[] args)
        {
            bool         quit   = false;
            DecisionTree myTree = new DecisionTree("root");

            #region XML Reading and Tree Building

            XmlReader doc          = XmlReader.Create("C:\\Users\\Andrew\\Documents\\Visual Studio 2017\\Projects\\DecisionTree\\DecisionTree\\DecisionTree.xml");
            string    responseName = String.Empty;


            while (doc.Read())
            {
                if ((doc.NodeType == XmlNodeType.Element) && (doc.Name == "node") && (doc.GetAttribute("response") == ""))
                {
                    responseName = doc.GetAttribute("behavior");
                }

                if ((doc.NodeType == XmlNodeType.Element) && (doc.Name == "node") && (doc.GetAttribute("behavior") == ""))
                {
                    responseName = doc.GetAttribute("response");
                }

                // This is ugly.
                #region Tree Building

                switch (responseName)
                {
                case "Idle":
                {
                    myTree.root.Right = new Node(responseName);
                    break;
                }

                case "Use Computer":
                {
                    myTree.root.Right.Left = new Node(responseName);
                    break;
                }

                case "Patrol":
                {
                    myTree.root.Right.Right = new Node(responseName);
                    break;
                }

                case "Incoming Projectile":
                {
                    myTree.root.Left = new Node(responseName);
                    break;
                }

                case "Evade":
                {
                    myTree.root.Left.Left = new Node(responseName);
                    break;
                }

                case "Combat":
                {
                    myTree.root.Middle = new Node(responseName);
                    break;
                }

                case "Melee":
                {
                    myTree.root.Middle.Left = new Node(responseName);
                    break;
                }

                case "Flee":
                {
                    myTree.root.Middle.Left.Left = new Node(responseName);
                    break;
                }

                case "Attack":
                {
                    myTree.root.Middle.Left.Right = new Node(responseName);
                    break;
                }

                case "Ranged":
                {
                    myTree.root.Middle.Right = new Node(responseName);
                    break;
                }

                case "Weapon 1":
                {
                    myTree.root.Middle.Right.Left = new Node(responseName);
                    break;
                }

                case "Weapon 2":
                {
                    myTree.root.Middle.Right.Middle = new Node(responseName);
                    break;
                }

                case "Weapon 3":
                {
                    myTree.root.Middle.Right.Right = new Node(responseName);
                    break;
                }

                default:
                {
                    break;
                }

                    #endregion
                }
            }

            #endregion


            while (!quit)
            {
                Console.Write("Event ('quit' to exit) : ");
                var command = Console.ReadLine();
                if (command == "quit")
                {
                    quit = true;
                    break;
                }
                Console.WriteLine($"Response = {myTree.Lookup(command)}");
            }
        }
 public static DecisionTreeNode <T> CreateRootNode <T>(DecisionTree <T> tree)
     where T : DDataRecord
 {
     return(new DecisionTreeNode <T>(tree, null, null));
 }