Пример #1
0
        public void TestInitialize()
        {
            _decisionTreeReaderMock = new Mock<IDecisionTreeReader>();
            _ruleBuilderMock = new Mock<IRuleBuilder>();
            _classifierMock = new Mock<IClassifier<FakeRecord>>();

            _decisionTreeReaderMock
                .Setup(x => x.ReadSubTrees(It.IsAny<string>()));

            var treeBuilder = new StringBuilder();
            treeBuilder.AppendLine("Ask <= 1.1:Sell (31.0/1.0)");
            treeBuilder.AppendLine("Ask > 1.1:");
            treeBuilder.AppendLine("    Bid <= 1.2:Buy (123.0/1.0)");
            treeBuilder.AppendLine("    Bid > 1.2:Hold (31.0/1.0)");
            _treeString = treeBuilder.ToString();

            _decisionTreeReaderMock
                .Setup(x => x.NormalizeTreeSource(It.IsAny<string>()))
                .Returns(_treeString);

            _decisionTreeReaderMock
                .Setup(x => x.NormalizeTree(It.IsAny<string>()))
                .Returns(_treeString);

            _fakeRule = new Rule {Level = 0};
            _ruleBuilderMock
                .Setup(x => x.Read(It.IsAny<string>()))
                .Returns(_fakeRule);

            _tree = new DecisionTree<FakeRecord>(
                _decisionTreeReaderMock.Object,
                _ruleBuilderMock.Object,
                _classifierMock.Object);
        }
Пример #2
0
    /// <summary>
    /// Initializes a new instance of the <see cref="ChaseState"/> class.
    /// </summary>
    /// <param name="statePatternEnemy">State pattern enemy.</param>
    /// <param name="zombie">Zombie.</param>
    public ChaseState(StatePatternEnemy statePatternEnemy, HorrorAI zombie)
    {
        this.enemy = statePatternEnemy;
        this.zombie = zombie;

        MakeDecisionTree();
        SetNodes();

        decisionTree = new DecisionTree(decisions, actions, enemy.AddActionToQueue);
    }
Пример #3
0
        /// <summary>
        ///   Creates a new C4.5 learning algorithm.
        /// </summary>
        /// 
        /// <param name="tree">The decision tree to be generated.</param>
        /// 
        public C45Learning(DecisionTree tree)
        {
            this.tree = tree;
            this.attributes = new bool[tree.InputCount];
            this.inputRanges = new IntRange[tree.InputCount];
            this.outputClasses = tree.OutputClasses;

            for (int i = 0; i < inputRanges.Length; i++)
                inputRanges[i] = tree.Attributes[i].Range.ToIntRange(false);

        }
Пример #4
0
    /// <summary>
    /// Initializes a new instance of the <see cref="PatrolState"/> class.
    /// </summary>
    /// <param name="statePatternEnemy">State pattern enemy.</param>
    /// <param name="zombie">Zombie.</param>
    /// <param name="grid">Grid.</param>
    public PatrolState(StatePatternEnemy statePatternEnemy, HorrorAI zombie, Grid grid)
    {
        this.enemy = statePatternEnemy;
        this.zombie = zombie;
        this.grid = grid;

        MakeDecisionTree();
        SetNodes();

        decisionTree = new DecisionTree(decisions, actions, enemy.AddActionToQueue);
    }
Пример #5
0
        /// <summary>
        ///   Initializes a new instance of the <see cref="ReducedErrorPruning"/> class.
        /// </summary>
        /// 
        /// <param name="tree">The tree to be prunned.</param>
        /// <param name="inputs">The pruning set inputs.</param>
        /// <param name="outputs">The pruning set outputs.</param>
        /// 
        public ReducedErrorPruning(DecisionTree tree, double[][] inputs, int[] outputs)
        {
            this.tree = tree;
            this.inputs = inputs;
            this.outputs = outputs;
            this.info = new Dictionary<DecisionNode, NodeInfo>();
            this.actual = new int[outputs.Length];

            foreach (var node in tree)
                info[node] = new NodeInfo();

            for (int i = 0; i < inputs.Length; i++)
                trackDecisions(tree.Root, inputs[i], i);
        }
        public static void ClassInitialize(TestContext context)
        {
            var dataDirectory = ConfigurationManager.AppSettings["TestDataDirectory"];
            var c45Source = File.ReadAllText(Path.Combine(dataDirectory, "C4.5.txt"));
            var forexTreePath = Path.Combine(ConfigurationManager.AppSettings["TestDataDirectory"], "ForexTree.data");
            _repository = new CsvDataRepository<ForexTreeData>();
            _repository.LoadData(forexTreePath);
            _repository.NormalizeData(0);

            _tree = new DecisionTree<ForexTreeData>(
                        new DecisionTreeReader(),
                        new RuleBuilder(),
                        new Classifier<ForexTreeData>()
                    );

            _tree.SaveDecisionTree(c45Source);
        }
Пример #7
0
        /// <summary>
        ///   Initializes a new instance of the <see cref="ErrorBasedPrunning"/> class.
        /// </summary>
        /// 
        /// <param name="tree">The tree to be prunned.</param>
        /// <param name="inputs">The prunning set inputs.</param>
        /// <param name="outputs">The prunning set outputs.</param>
        /// 
        public ErrorBasedPrunning(DecisionTree tree, double[][] inputs, int[] outputs)
        {
            this.tree = tree;
            this.inputs = inputs;
            this.outputs = outputs;
            this.subsets = new Dictionary<DecisionNode, List<int>>();
            this.actual = new int[outputs.Length];

            // Create the cache to store how many times a sample
            // passes through each decision node of the tree.
            //
            createCache(tree.Root);

            // Compute the entire prunning set and track the path
            // taken by each observation during the reasoning.
            //
            trackDecisions(tree.Root, inputs);
        }
Пример #8
0
        public void testAdaBoostEnablesCollectionOfStumpsToClassifyDataSetAccurately()
        {
            DataSet             ds       = DataSetFactory.getRestaurantDataSet();
            List <DecisionTree> stumps   = DecisionTree.getStumpsFor(ds, YES, "No");
            List <Learner>      learners = new List <Learner>();

            foreach (Object stump in stumps)
            {
                DecisionTree sl           = (DecisionTree)stump;
                StumpLearner stumpLearner = new StumpLearner(sl, "No");
                learners.Add(stumpLearner);
            }
            AdaBoostLearner learner = new AdaBoostLearner(learners, ds);

            learner.train(ds);
            int[] result = learner.test(ds);
            Assert.AreEqual(12, result[0]);
            Assert.AreEqual(0, result[1]);
        }
Пример #9
0
        static DecisionTree DecisionTreeClassification(List <int[]> trainingData, List <int[]> testingData, out double precision)
        {
            int    testingCount      = testingData.Count / 10;
            int    trainingCount     = testingData.Count - testingCount;
            double errorAverage      = 0;
            int    indexTestingStart = testingData.Count - testingCount;
            int    indexTestingEnd   = testingData.Count;
            double prec = 0;

            Console.WriteLine("Decision Tree Classification");
            DecisionTree bestDecision = null;

            for (int i = 0; i < iterations; i++)
            {
                var watch = System.Diagnostics.Stopwatch.StartNew();
                Console.WriteLine("Testing from: {0} to {1}", indexTestingStart, indexTestingEnd);
                int[][] inputData, testinputData;
                int[]   outputData, testoutputData;

                PrepareInputOutput(out inputData, out outputData, out testinputData, out testoutputData, trainingData, testingData, indexTestingStart, indexTestingEnd);

                ID3Learning teacher  = new ID3Learning();
                var         decision = teacher.Learn(inputData, outputData);
                Console.WriteLine("Medis sukurtas - ismokta");
                double error = new ZeroOneLoss(testoutputData).Loss(decision.Decide(testinputData));
                Console.WriteLine("Apmokymo tikslumas: {0}", 1 - error);
                if (1 - error > prec)
                {
                    prec         = 1 - error;
                    bestDecision = decision;
                }
                watch.Stop();
                var elapsedMs = watch.ElapsedMilliseconds;
                Console.WriteLine("Iteracija baigta per: {0}ms", elapsedMs);
                indexTestingEnd    = indexTestingStart;
                indexTestingStart -= testingCount;
                errorAverage      += error;
                bestDecision       = decision;
                Console.WriteLine("------------------------------------------------------------------------------");
            }
            precision = 1 - (errorAverage / iterations);
            return(bestDecision);
        }
Пример #10
0
        public void TestDecisionTreeCreate()
        {
            var dt             = DecisionTree.Create(exampleDecisionTree);
            var nodeStrings    = dt.ToString().Split('\n');
            var exampleStrings = exampleDecisionTree.Split('\n');

            foreach (var n in nodeStrings)
            {
                bool isCool = false;
                foreach (var e in exampleStrings)
                {
                    isCool = isCool || e.StartsWith(n);
                }
                if (!isCool)
                {
                    Assert.True(isCool, "node failed to be found: '" + n + "'");
                }
            }
        }
Пример #11
0
        public GuiDecisionTree(DecisionTree dt)
        {
            InitializeComponent();

            _dt = dt;

            ListBox rootNodeListBox = null;

            rootNodeListBox = NewDecisionListBox(new List<DecisionTree>() { _dt });

            //rootNodeListBox.Location = new Point(-5, 0);
            decisionListBoxes.Add(rootNodeListBox);

            guiChessBoard1.IsLocked = true;
            this.splitContainer1.Panel2.Controls.Add(rootNodeListBox);
            rootNodeListBox.SelectedIndex = 0;

            this.hScrollBar1.LargeChange = rootNodeListBox.Width;
        }
Пример #12
0
        public void LargeRunTest()
        {
            Accord.Math.Random.Generator.Seed = 0;

            double[][]   inputs;
            int[]        outputs;
            DecisionTree tree = createTree(out inputs, out outputs);

            var rules = DecisionSet.FromDecisionTree(tree);

            Simplification simpl = new Simplification(rules);
            double         error = simpl.ComputeError(inputs, outputs);

            Assert.AreEqual(0, error);

            double newError = simpl.Compute(inputs, outputs);

            Assert.AreEqual(0.067515432098765427, newError, 1e-6);
        }
Пример #13
0
        private string Print_Tree(DecisionTree TreeNode, int tabs = 0)
        {
            //base case
            if (TreeNode == null)
            {
                TreeNode = DecisionTreeRoot;
            }

            //final case

            if (TreeNode.answer != null)
            {
                return(tabsFunc(tabs) + " " + TreeNode.answer);
            }

            //normalcase
            string outPut = "";

            if (findAtrib(TreeNode.AtributeName)[1] == "continuous")
            {
                outPut = outPut + "\n" + tabsFunc(tabs) + " " + AttributeSet[findAtribIndex(TreeNode.AtributeName)][0] + ">=" + TreeNode.continuasSplitValue.ToString() + ":";
                outPut = outPut + "\n" + Print_Tree(TreeNode.Paths.Last.Value, tabs + 1);
                outPut = outPut + "\n" + tabsFunc(tabs) + " " + AttributeSet[findAtribIndex(TreeNode.AtributeName)][0] + "<" + TreeNode.continuasSplitValue.ToString() + ":";
                outPut = outPut + "\n" + Print_Tree(TreeNode.Paths.First.Value, tabs + 1);
            }


            else
            {
                LinkedListNode <DecisionTree> IndexNode = TreeNode.Paths.First;

                while (IndexNode != null)
                {
                    outPut = outPut + "\n" + tabsFunc(tabs) + " " + TreeNode.AtributeName + "=" + IndexNode.Value.PreviosePathDecision + ":";
                    outPut = outPut + "\n" + Print_Tree(IndexNode.Value, tabs + 1);


                    IndexNode = IndexNode.Next;
                }
            }
            return(outPut.Remove(0, 1));
            //return outPut;
        }
Пример #14
0
        public void TestDecisionTreePrecomputeEverything()
        {
            var css       = new CharSetSolver();
            var regex     = new Regex("abc[^a-i]");
            var sr        = css.RegexConverter.ConvertToSymbolicRegex(regex, true);
            var partition = sr.ComputeMinterms();

            Assert.AreEqual <int>(5, partition.Length);
            var dt = DecisionTree.Create(css, partition, 0xFFFF);

            for (int i = 0; i < partition.Length; i++)
            {
                foreach (var c in css.GenerateAllCharacters(partition[i]))
                {
                    Assert.AreEqual(i, dt.GetId(c));
                }
            }
            Assert.IsTrue(dt.Tree == null);
        }
Пример #15
0
    private void SetupDecisionTree()
    {
        ObjectDecision isInProduction = new ObjectDecision(InProduction);

        ObjectDecision doIHaveProductsForFuture = new ObjectDecision(FutureProductsTest);

        ObjectDecision doIHaveProductsForProduction = new ObjectDecision(ProductCheck);

        ObjectDecision isStorageOutputFull = new ObjectDecision(TestOutputStorage);

        ObjectDecision canIGetProductsForProduction = new ObjectDecision(AreProductsOnMap);

        ObjectDecision isThereAnAvailableStorageFacility = new ObjectDecision(IsThereAStorageBuilding);

        ObjectDecision DoIHaveACourier = new ObjectDecision(IsThereAnAvailableCourier);

        Leaf WaitForNextCycle = new Leaf();

        Leaf beginProduction = new Leaf(BeginProduction);

        Leaf SendCourierWithProducts = new Leaf(SendCourierWithProductsFunc);

        Leaf SendCourierForProducts = new Leaf(SendCourierForProductsFunc);

        Vertex HasCourierBeenSentForProducts = new Vertex(DoIHaveACourier, WaitForNextCycle, SendCourierForProducts);

        Vertex HasCourierBeenSentWithProducts = new Vertex(DoIHaveACourier, WaitForNextCycle, SendCourierWithProducts);

        Vertex IsThereStorageFacility = new Vertex(isThereAnAvailableStorageFacility, HasCourierBeenSentWithProducts, WaitForNextCycle);

        Vertex IsOutputStorageFull = new Vertex(isStorageOutputFull, IsThereStorageFacility, beginProduction);

        Vertex CanIGetProductsForProduction = new Vertex(canIGetProductsForProduction, HasCourierBeenSentForProducts, WaitForNextCycle);

        Vertex DoIHaveProductsForProduction = new Vertex(doIHaveProductsForProduction, IsOutputStorageFull, CanIGetProductsForProduction);

        Vertex DoIHaveProductsForFutureProductions = new Vertex(doIHaveProductsForFuture, WaitForNextCycle, CanIGetProductsForProduction);

        Vertex IsInProduction = new Vertex(isInProduction, DoIHaveProductsForFutureProductions, DoIHaveProductsForProduction);

        ProductionTree = new DecisionTree(IsInProduction);
    }
Пример #16
0
    void BuildDecisionTree()
    {
        DecisionTree isAliveNode = new DecisionTree();

        isAliveNode.SetDecision(IsAlive);

        DecisionTree isInVisibleNode = new DecisionTree();

        isInVisibleNode.SetDecision(IsVisible);

        DecisionTree isInAttackNode = new DecisionTree();

        isInAttackNode.SetDecision(InAttackRange);

        DecisionTree actPatrolNode = new DecisionTree();

        actPatrolNode.SetAction(Patrol);

        DecisionTree actDieNode = new DecisionTree();

        actDieNode.SetAction(Die);

        DecisionTree actApproachNode = new DecisionTree();

        actApproachNode.SetAction(ApproachPlayer);

        DecisionTree actAttackNode = new DecisionTree();

        actAttackNode.SetAction(Attack);

        isAliveNode.SetLeft(actDieNode);
        isAliveNode.SetRight(isInVisibleNode);

        isInVisibleNode.SetLeft(actPatrolNode);
        isInVisibleNode.SetRight(isInAttackNode);

        isInAttackNode.SetLeft(actApproachNode);
        isInAttackNode.SetRight(actAttackNode);


        root = isAliveNode;
    }
        public TrainerHelper Train(System.Data.DataTable table, string columnName)
        {
            var container            = new TrainerHelper();
            var trainingCodification = new Codification()
            {
                DefaultMissingValueReplacement = Double.NaN
            };

            trainingCodification.Learn(table);
            DataTable symbols = trainingCodification.Apply(table);

            container.columnNamesArray =
                table.Columns.Cast <DataColumn>().Select(x => x.ColumnName).Where(s => s != columnName).ToArray();

            var columnOrdinal = table.Columns[columnName].Ordinal;

            int[][]    tempInputs = symbols.ToJagged <int>(container.columnNamesArray);
            double[][] inputs     = new double[tempInputs.Length][];
            for (var i = 0; i < tempInputs.Length; i++)
            {
                // var flattened = this.ExpandRow(trainingCodification, tempInputs[i], columnOrdinal);
                // inputs[i] = flattened;
            }


            int[] outputs = symbols.ToArray <int>(columnName);

            var id3learning = new ID3Learning();

            id3learning.Attributes = DecisionVariable.FromCodebook(trainingCodification);
            // Learn the training instances!
            DecisionTree tree = id3learning.Learn(tempInputs, outputs);

            container.decisionTree = tree;


            //var lbnr = new LowerBoundNewtonRaphson() { MaxIterations = 100, Tolerance = 1e-6 };
            //var mlr = lbnr.Learn(inputs, outputs);
            container.codification = trainingCodification;
            container.symbols      = symbols;
            return(container);
        }
        public void RunTest()
        {
            Accord.Math.Random.Generator.Seed = 0;

            double[][] inputs;
            int[]      outputs;

            int          trainingSamplesCount = 6000;
            DecisionTree tree = ReducedErrorPruningTest.createNurseryExample(out inputs, out outputs, trainingSamplesCount);

            int nodeCount = 0;

            foreach (var node in tree)
            {
                nodeCount++;
            }

            var pruningInputs       = inputs.Submatrix(trainingSamplesCount, inputs.Length - 1);
            var pruningOutputs      = outputs.Submatrix(trainingSamplesCount, inputs.Length - 1);
            ErrorBasedPruning prune = new ErrorBasedPruning(tree, pruningInputs, pruningOutputs);

            prune.Threshold = 0.1;

            double lastError, error = Double.PositiveInfinity;

            do
            {
                lastError = error;
                error     = prune.Run();
            } while (error < lastError);

            int nodeCount2 = 0;

            foreach (var node in tree)
            {
                nodeCount2++;
            }

            Assert.AreEqual(0.28922413793103446, error, 5e-4);
            Assert.AreEqual(447, nodeCount);
            Assert.AreEqual(424, nodeCount2);
        }
Пример #19
0
        public void Build2dDataSet()
        {
            // Built by estimating from the sample set in https://victorzhou.com/blog/gini-impurity/#picking-the-best-split
            var data = new[]
            {
                // Class 1
                new [] { 0.2, 1.5 },
                new [] { 0.5, 0.2 },
                new [] { 0.6, 1.2 },
                new [] { 1.0, 2.3 },
                new [] { 1.8, 0.3 },

                // Class 2
                new [] { 2.3, 1.6 },
                new [] { 2.4, 1.4 },
                new [] { 2.5, 3.1 },
                new [] { 2.5, 0.3 },
                new [] { 2.9, 2.1 }
            };

            var classes = new[]
            {
                1, 1, 1, 1, 1,
                2, 2, 2, 2, 2
            };

            var tree = DecisionTree.Build(data, classes);

            Assert.NotNull(tree);
            Assert.NotNull(tree.Root);
            Assert.False(tree.IsEmpty);

            Assert.True(tree.Root.IsLeaf);
            Assert.Null(tree.Root.Left);
            Assert.Null(tree.Root.Right);

            var predictionA = tree.Predict(new[] { 0.7, 5 });
            var predictionB = tree.Predict(new[] { 6, 0.3 });

            Assert.Equal(1, predictionA);
            Assert.Equal(2, predictionB);
        }
Пример #20
0
        static void Main(string[] args)
        {
            var data = new ExcelReader(@"c:\temp\accordemo\accordemo\titanic.xls").GetWorksheet("titanic3");

            data.Rows.RemoveAt(data.Rows.Count - 1);
            var          d     = new Elimination("age").Apply(data);
            var          fdata = new Codification(d, "sex").Apply(d);
            var          outp  = fdata.Columns["survived"].ToArray <int>();
            var          input = fdata.ToArray <double>("pclass", "sex", "age", "parch", "sibsp");
            DecisionTree T     = new DecisionTree(
                DecisionVariable.FromData(input), 2);
            var learn = new C45Learning(T);

            learn.Run(input, outp);
            var r1 = T.Decide(new double[] { 0, 1, 23, 0, 0 });
            var r2 = T.Decide(new double[] { 1, 0, 30, 1, 1 });

            Console.WriteLine($"Male={r1}, Female={r2}");
            Console.ReadKey();
        }
Пример #21
0
        //public static C45Model CreateC45Model(Codification codification)
        //{
        //    int lastIndex = codification.Columns.Count - 1;

        //    List<DecisionVariable> attributes = new List<DecisionVariable>();

        //    for (int indexColumn = 0; indexColumn < lastIndex; indexColumn++)
        //    {
        //        attributes.Add(new DecisionVariable(codification.Columns[indexColumn].ColumnName,
        //            codification[indexColumn].Symbols));
        //    }

        //    C45Model model = new C45Model(new DecisionTree(attributes.ToArray(), 2));

        //    return model;
        //}

        //public C45Model(DecisionTree tree)
        //{
        //    this.Tree = tree;
        //}

        // Trainning decision tree with C4.5 algorithm
        public override void TrainningModel(TrainningData trainningData)
        {
            // Get data for trainning tree
            Codification codification = trainningData.CodificationData;

            double[][] inputs  = trainningData.TrainningAttributes;
            int[]      outputs = trainningData.ClassificationAttribute;

            // Create tree
            this.Tree = this.CreateDecisionTree(codification);
            //var attributes = DecisionVariable.FromCodebook(codification, inputColumns);
            //DecisionTree tree = new DecisionTree(attributes, outputClasses: 5);


            // Creates a new instance of the C4.5 learning algorithm
            C45Learning c45 = new C45Learning(this.Tree);

            // Learn the decision tree
            double error = c45.Run(inputs, outputs);
        }
Пример #22
0
        public void buildDecisionTree(DecisionTree p_decisionTree, Entity p_sourceEntity)
        {
            Health p_sourceEntityHealth = EntityComponent.get_component <Health>(p_sourceEntity);

            if (Health.getHealthRatio(p_sourceEntityHealth) <= HealthPercentageForSearchingRecorevory)
            {
                if (EntityComponentContainer.Components.ContainsKey(typeof(HealthRecoveryTrigger)))
                {
                    TreeBuilderLibrary.buildMoveToHealthTrigger(p_decisionTree, p_sourceEntity);
                }
                else
                {
                    TreeBuilderLibrary.buildMoveToRangeOfEntity(p_decisionTree, p_sourceEntity);
                }
            }
            else
            {
                TreeBuilderLibrary.buildMoveToRangeOfEntity(p_decisionTree, p_sourceEntity);
            }
        }
Пример #23
0
        /// <summary>
        ///   Creates a new ID3 learning algorithm.
        /// </summary>
        /// 
        /// <param name="tree">The decision tree to be generated.</param>
        /// 
        public ID3Learning(DecisionTree tree)
        {
            // Initial argument checking
            if (tree == null)
                throw new ArgumentNullException("tree");

            this.tree = tree;
            this.inputRanges = new IntRange[tree.InputCount];
            this.outputClasses = tree.OutputClasses;
            this.attributes = new bool[tree.InputCount];
            this.maxHeight = attributes.Length;
            this.Rejection = true;

            for (int i = 0; i < tree.Attributes.Count; i++)
                if (tree.Attributes[i].Nature != DecisionVariableKind.Discrete)
                    throw new ArgumentException("The ID3 learning algorithm can only handle discrete inputs.");

            for (int i = 0; i < inputRanges.Length; i++)
                inputRanges[i] = tree.Attributes[i].Range.ToIntRange(false);
        }
Пример #24
0
        public double Learn(double[][] observations, int[] labels)
        {
            int max = observations[0].Length;

            DecisionVariable[] a = new DecisionVariable[max];
            for (int i = 0; i < max; i++)
            {
                a[i] = DecisionVariable.Continuous(i.ToString());
            }
            C45Learning teacher = new C45Learning(a);

            // Use the learning algorithm to induce the tree
            machine = teacher.Learn(observations, labels);

            // Classify the samples using the model
            int[]  predicted = machine.Decide(observations);
            double error     = new AccuracyLoss(labels).Loss(predicted);

            return(1 - error);
        }
Пример #25
0
    /// <summary>
    /// Used to set up the attacker type tree
    /// </summary>
    void SetupAttacker()
    {
        DecisionNode hasFlagNode = new DecisionNode(Decisions.Attacker_HasEnemyFlag, this);

        DecisionNode teamHasEnemyFlagNode = new DecisionNode(Decisions.Shared_TeamHasEnemyFlag, this);
        ActionNode   runBackAndScore      = new ActionNode(Actions.Attacker_GoBackToTeamFlag, this);

        hasFlagNode.AddFailureNode(teamHasEnemyFlagNode);
        hasFlagNode.AddSuccessNode(runBackAndScore);

        DecisionNode enemyInRadiusNode       = new DecisionNode(Decisions.Shared_IsEnemyInAttackRadius, this);
        DecisionNode enemySpottedNotInRadius = new DecisionNode(Decisions.Shared_HasSeenEnemy, this);

        teamHasEnemyFlagNode.AddFailureNode(enemyInRadiusNode);
        teamHasEnemyFlagNode.AddSuccessNode(enemySpottedNotInRadius);

        ActionNode attackTarget           = new ActionNode(Actions.Shared_AttackEnemyTarget, this);
        ActionNode followTeamMateWithFlag = new ActionNode(Actions.Shared_FollowTeamMateWithFlag, this);

        enemySpottedNotInRadius.AddSuccessNode(attackTarget);
        enemySpottedNotInRadius.AddFailureNode(followTeamMateWithFlag);

        DecisionNode isHealthLow = new DecisionNode(Decisions.Shared_IsHealthLow, this);
        ActionNode   findPickup  = new ActionNode(Actions.Shared_SearchForHealthpack, this);

        enemyInRadiusNode.AddFailureNode(isHealthLow);
        enemyInRadiusNode.AddSuccessNode(attackTarget);

        DecisionNode canTakeEnemyFlag = new DecisionNode(Decisions.Attacker_CanTakeEnemyFlag, this);

        isHealthLow.AddFailureNode(canTakeEnemyFlag);
        isHealthLow.AddSuccessNode(findPickup);

        ActionNode moveToEnemyFlag = new ActionNode(Actions.Attacker_MoveToEnemyFlag, this);
        ActionNode takeEnemyFlag   = new ActionNode(Actions.Attacker_TakeEnemyFlag, this);

        canTakeEnemyFlag.AddSuccessNode(takeEnemyFlag);
        canTakeEnemyFlag.AddFailureNode(moveToEnemyFlag);

        decTree = new DecisionTree(hasFlagNode);
    }
Пример #26
0
        private void TrainData_Click(object sender, EventArgs e)
        {
            dmC.LoadCSV();

            Dictionary <String, Dato> trainData = dmC.GetBanks();

            DecisionTree <Dato> destree = new DecisionTree <Dato>(trainData);

            List <Dato> rows = new List <Dato>();

            foreach (String k in trainData.Keys)
            {
                rows.Add(trainData[k]);
            }

            Node <Dato> t = destree.BuildTree(rows);

            //printTree(t, "");

            dmC.LoadCSVTest();

            Dictionary <String, Dato> test = dmC.GetClassifiedBank();

            List <String> classification = new List <string>();

            foreach (String k in test.Keys)
            {
                classification.Add("Actual -> " + test[k].getAttributes()[test[k].getAttributes().Length - 1] + "\n" +
                                   "Predicted -> " + destree.PrintLeaf(destree.Classify(test[k], t)));

                resultadosArbol.Add((test[k].getAttributes()[test[k].getAttributes().Length - 1] == 0 + "" ? "no" : "yes"));

                Console.WriteLine("Actual -> " + test[k].getAttributes()[test[k].getAttributes().Length - 1] + "\n" +
                                  "Predicted -> " + destree.PrintLeaf(destree.Classify(test[k], t)));
            }

            ResultClasification c = new ResultClasification(classification);

            // c.Show();
            Recorrer();
        }
Пример #27
0
        public static DecisionTree createNurseryExample(out double[][] inputs, out int[] outputs, int first)
        {
            string nurseryData = Resources.nursery;

            string[] inputColumns =
            {
                "parents", "has_nurs", "form",   "children",
                "housing", "finance",  "social", "health"
            };

            string outputColumn = "output";

            DataTable table = new DataTable("Nursery");

            table.Columns.Add(inputColumns);
            table.Columns.Add(outputColumn);

            string[] lines = nurseryData.Split(
                new[] { Environment.NewLine }, StringSplitOptions.None);

            foreach (var line in lines)
            {
                table.Rows.Add(line.Split(','));
            }

            Codification codebook = new Codification(table);
            DataTable    symbols  = codebook.Apply(table);

            inputs  = symbols.ToArray(inputColumns);
            outputs = symbols.ToArray <int>(outputColumn);

            var attributes = DecisionVariable.FromCodebook(codebook, inputColumns);
            var tree       = new DecisionTree(attributes, classes: 5);

            C45Learning c45   = new C45Learning(tree);
            double      error = c45.Run(inputs.Submatrix(first), outputs.Submatrix(first));

            Assert.AreEqual(0, error);

            return(tree);
        }
        public static void training()
        {
            string filepath = Path.Combine(HostingEnvironment.ApplicationPhysicalPath + @"Content\files\diabetes.xls");

            DataTable table = new ExcelReader(filepath).GetWorksheet("diabetes");

            double[][] inputs  = table.ToJagged <double>("Glucose", "BloodPressure", "Insulin", "BMI", "Age");
            int[]      outputs = table.ToArray <int>("Outcome");

            DecisionVariable[] var =
            {
                new DecisionVariable("G",   DecisionVariableKind.Continuous),
                new DecisionVariable("B",   DecisionVariableKind.Continuous),
                new DecisionVariable("I",   DecisionVariableKind.Continuous),
                new DecisionVariable("BMI", DecisionVariableKind.Continuous),
                new DecisionVariable("A",   DecisionVariableKind.Continuous),
            };

            tree = new DecisionTree(var, 2);

            C45Learning teacher = new C45Learning(tree);

            teacher.Learn(inputs, outputs);


            //From Here Trying to find out the probability
            var learner = new IterativeReweightedLeastSquares <LogisticRegression>()
            {
                Tolerance      = 1e-6, // Let's set some convergence parameters
                MaxIterations  = 1000, // maximum number of iterations to perform
                Regularization = 0
            };

            regression = learner.Learn(inputs, outputs);

            //lra = new LogisticRegressionAnalysis()
            //{
            //    Regularization = 0
            //};
            //lra.Learn(inputs, outputs);
        }
Пример #29
0
        //Entrapy for nonconinuas values
        private double Entrapy(DecisionTree DTnode, int i, string dataset = "all")
        {
            LinkedListNode <string[]> TrainindNode = DTnode.TrainSet.First;
            int sizeOfAnswerList = numOfAnswers;
            int denominator      = 0;

            double[] entropyNumerator = new double[sizeOfAnswerList];

            while (TrainindNode != null)
            {
                if (dataset != "all" && TrainindNode.Value[i + 1] != dataset)
                {
                }
                else
                {
                    int answerIndex = answerindex(TrainindNode.Value[0]);
                    entropyNumerator[answerIndex - 1]++;
                    denominator++;
                }
                TrainindNode = TrainindNode.Next;
            }

            double sum = 0.0, testsum = 0.0;

            for (int j = 0; j < sizeOfAnswerList; j++)
            {
                testsum = ((entropyNumerator[j] / denominator) * Math.Log((entropyNumerator[j] / denominator), 2));

                if (double.IsNaN(testsum))
                {
                    testsum = 0.0;
                }
                sum = sum + testsum;
            }



            sum = sum * denominator;

            return(0.0 - sum);   //negative value
        }
Пример #30
0
    private void Train()
    {
        DataTable data = GetDataTable(Application.dataPath + "/" + trainData);

        //DebugTable(data);
        codebook = new Codification(data);
        DataTable symbols = codebook.Apply(data);

        int[][] inputs  = symbols.ToArray <int>("LIFE", "TOWERS", "MELIANTS", "TIME", "ENEMY_COINS");
        int[]   outputs = symbols.ToArray <int>("POSITION");

        var id3learning = new ID3Learning();

        id3learning.Attributes = DecisionVariable.FromData(inputs);

        tree = id3learning.Learn(inputs, outputs);

        double error = new ZeroOneLoss(outputs).Loss(tree.Decide(inputs));

        tree.Save(Application.dataPath + "/" + treeLocation);
    }
Пример #31
0
        public void TestDecisionTreePrecomputeExtendedASCII()
        {
            var css       = new CharSetSolver();
            var regex     = new Regex("(?i:abc[^a-i])");
            var sr        = css.RegexConverter.ConvertToSymbolicRegex(regex, true);
            var partition = sr.ComputeMinterms();

            Assert.AreEqual <int>(5, partition.Length);
            var dt = DecisionTree.Create(css, partition, 0xFF);

            for (int i = 0; i < partition.Length; i++)
            {
                foreach (var c in css.GenerateAllCharacters(partition[i]))
                {
                    Assert.AreEqual(i, dt.GetId(c));
                }
            }
            //there is a special unicode 'i' character that is equivalent to i with ignore-case option
            //that forms a separate character class here
            Assert.IsFalse(dt.Tree.IsLeaf);
        }
Пример #32
0
    public void GenerateJS(DecisionTree root)
    {
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        sb.Append(@"<script language='javascript'>");
        sb.Append(@"window.onload = function () {");
        sb.Append(@"var g = new Graph();");

        TraverseTree(root, sb, 0);

        sb.Append(@"var layouter = new Graph.Layout.Spring(g);");
        sb.Append(@"layouter.layout();");
        sb.Append(@"var renderer = new Graph.Renderer.Raphael('canvas', g, 1660, 865);");
        sb.Append(@"renderer.draw();");
        sb.Append(@"};");
        sb.Append(@"</script>");

        if (!ClientScript.IsStartupScriptRegistered("JSScript"))
        {
            ClientScript.RegisterStartupScript(this.GetType(), "JSScript", sb.ToString());
        }
    }
Пример #33
0
        public void AddEntityDecision(DecisionGroup decisionGroup, int decId, int entityId)
        {
            EntityWorkData entityWorkData = ECSLayerLocate.Info.GetEntityWorkData(entityId);

            //清理旧的
            RemoveEntityDecision(decId, entityWorkData.Id);
            //加新的
            BaseDecisionServerObj newServer = GetServer(decisionGroup);

            if (!newServer.HasTree(decId))
            {
                DecisionTree tree = LoadDecision(decId);
                if (tree == null)
                {
                    return;
                }
                newServer.AddTree(tree);
            }
            newServer.AddEntity(decId, entityWorkData);
            decDict.Add(entityWorkData.Id, decisionGroup);
        }
Пример #34
0
    private void SetDecisionTree()
    {
        CheckNode playerVisible      = new CheckNode(PlayerVisible);
        CheckNode playerWithinRange  = new CheckNode(PlayerWithinRange);
        CheckNode playerSeenRecently = new CheckNode(PlayerSeenRecently);

        ActionNode attack       = new ActionNode(Attack);
        ActionNode moveToPlayer = new ActionNode(MoveToPlayer);
        ActionNode wander       = new ActionNode(Wander);

        playerVisible.True  = playerWithinRange;
        playerVisible.False = playerSeenRecently;

        playerWithinRange.True  = attack;
        playerWithinRange.False = moveToPlayer;

        playerSeenRecently.True  = moveToPlayer;
        playerSeenRecently.False = wander;

        decisionTree = new DecisionTree(playerVisible);
    }
Пример #35
0
    // Update is called once per frame
    protected virtual void Update()
    {
        _decision.Reset();
        foreach (System.Reflection.MethodInfo method in this.GetType().GetMethods())
        {
            if (((PerceptMethod[])method.GetCustomAttributes(typeof(PerceptMethod), true)).Length > 0)
            {
                DecisionTree <Action> .Percept p = _decision.GetPercept(method.Name);
                if (p != null && ((Func <bool>)Delegate.CreateDelegate(typeof(Func <bool>), this, method))())
                {
                    p.Activate();
                }
            }
        }
        Action action = _decision.Decide();

        if (action != null)
        {
            action();
        }
    }
Пример #36
0
        private static void decisionTree(double[][] inputs, int[] outputs)
        {
            // In our problem, we have 2 classes (samples can be either
            // positive or negative), and 2 continuous-valued inputs.

            C45Learning teacher = new C45Learning(new[] {
                DecisionVariable.Continuous("X"),
                DecisionVariable.Continuous("Y")
            });

            // Use the learning algorithm to induce the tree
            DecisionTree tree = teacher.Learn(inputs, outputs);

            // Classify the samples using the model
            int[] answers = tree.Decide(inputs);

            // Plot the results
            ScatterplotBox.Show("Expected results", inputs, outputs);
            ScatterplotBox.Show("Decision Tree results", inputs, answers)
            .Hold();
        }
Пример #37
0
        static double Decision_Tree(bool show)
        {
            DataTable    data       = DataController.MakeDataTable("../../drug_consumption.txt");
            DataTable    entireData = DataController.MakeDataTable("../../drug_consumption.txt");
            DataTable    tests      = DataController.MakeDataTable("../../drug_consumption_test2.txt");
            Codification codebook   = new Codification(entireData);

            DecisionVariable[] attributes = DataController.GetAttributes();
            int classCount = 7; // (7) "Never Used", "Used over a Decade Ago", "Used in Last Decade", "Used in Last Year", "Used in Last Month", "Used in Last Week", and "Used in Last Day"

            DecisionTree tree        = new DecisionTree(attributes, classCount);
            ID3Learning  id3learning = new ID3Learning(tree);

            id3learning.MaxHeight = 7;
            DataTable symbols    = codebook.Apply(data);
            string    LookingFor = "Cannabis";

            int[][] inputs  = symbols.ToJagged <int>("Age", "Gender", "Education", "Country", "Eticnity", "Nscore", "Escore", "Oscore", "Ascore", "Cscore", "Impulsive", "SS");
            int[]   outputs = symbols.ToArray <int>(LookingFor);

            id3learning.Learn(inputs, outputs);
            DataTable testSymbols = codebook.Apply(tests);

            int[][]     testIn   = testSymbols.ToJagged <int>("Age", "Gender", "Education", "Country", "Eticnity", "Nscore", "Escore", "Oscore", "Ascore", "Cscore", "Impulsive", "SS");
            int[]       testOut  = testSymbols.ToArray <int>(LookingFor);
            DecisionSet rules    = tree.ToRules();
            string      ruleText = rules.ToString(codebook, LookingFor, System.Globalization.CultureInfo.InvariantCulture);
            double      error    = new ZeroOneLoss(testOut).Loss(tree.Decide(testIn));

            if (show == true)
            {
                Console.WriteLine(LookingFor);
                Console.WriteLine();
                Console.WriteLine(ruleText);
                Console.ReadKey();
                Console.WriteLine("Blad - " + Math.Round(error, 4) + "%");
                Console.ReadKey();
            }
            return(error);
        }
Пример #38
0
        public void RunTest()
        {
            double[][] inputs;
            int[]      outputs;

            int          training = 6000;
            DecisionTree tree     = ReducedErrorPruningTest.createNurseryExample(out inputs, out outputs, training);

            int nodeCount = 0;

            foreach (var node in tree)
            {
                nodeCount++;
            }

            var pruningInputs       = inputs.Submatrix(training, inputs.Length - 1);
            var pruningOutputs      = outputs.Submatrix(training, inputs.Length - 1);
            ErrorBasedPruning prune = new ErrorBasedPruning(tree, pruningInputs, pruningOutputs);

            prune.Threshold = 0.1;

            double lastError, error = Double.PositiveInfinity;

            do
            {
                lastError = error;
                error     = prune.Run();
            } while (error < lastError);

            int nodeCount2 = 0;

            foreach (var node in tree)
            {
                nodeCount2++;
            }

            Assert.AreEqual(0.25459770114942532, error);
            Assert.AreEqual(447, nodeCount);
            Assert.AreEqual(193, nodeCount2);
        }
Пример #39
0
    void Start()
    {
        AIConfig mob = AIBuilder ("mob", weightedLevel);

        gameObject.AddComponent<PathManager> ().mobSp = mob.movementSpeed;
        gameObject.AddComponent<AIBehavior> ().rb = mob;
        gameObject.GetComponent<AIBehavior> ().pathing = gameObject.GetComponent<PathManager> ();
        gameObject.AddComponent<DecisionTree>();
        gameObject.AddComponent<CollisionHandler> ();

        //AI START//
        behavior = gameObject.GetComponent<DecisionTree> ();

        behavior.startDeciding ();
    }
Пример #40
0
    public void RunBotMove()
    {
        //computer's turn to go
        if ((int)player != (int)playerTurn)
        {
            State.Type startType;
            if (player == Player.Black)
                startType = State.Type.Max;
            else
                startType = State.Type.Min;

            decisionTree = new DecisionTree(currentState, startType);
            //Debug.Log("Bot Decisions:\n\n");
            foreach(State s in decisionTree.GetRoot().childrenStates)
            {
                //s.LogState();
                //Debug.Log("\n\n");
            }

            State decision = decisionTree.MakeDecision();

            //Debug.Log("Bot Decision");
            //Debug.Log("Decision Row: " + decision.newValueRow);
            //Debug.Log("Decision Col: " + decision.newValueCol);
            //Debug.Log("Decision score: " + decision.score);
            //decision.LogState();

            BoardGenerator.boardCells[decision.newValueRow, decision.newValueCol].disc = DropDisc(decision.newValueRow, decision.newValueCol,
                                                                                                  ref BoardGenerator.boardCells[decision.newValueRow, decision.newValueCol]);
        }
        //playerCanGo = true;
    }
Пример #41
0
    void Start()
    {
        StatCollectionClass tempStat;
        PathManager tempPath;
        AIBehavior tempBe;
        DecisionTree tempTre;
        ColliderCheck tempCol;

        tempStat = gameObject.AddComponent<StatCollectionClass>();
        tempStat.enabled = false;
        tempStat.copyStats (config.statExchange);

        tempPath = gameObject.AddComponent<PathManager> ();
        tempPath.enabled = false;
        tempPath.mobSp = config.movementSpeed;

        tempBe = gameObject.AddComponent<AIBehavior> ();
        tempBe.enabled = false;
        tempBe.anim = gameObject.GetComponent<Animator>();
        tempBe.rb = config;
        tempBe.pathing = tempPath;
        tempBe.reference = tempStat;
        tempBe.ps = gameObject.GetComponentInChildren<ProjectileSpawner>();

        tempTre = gameObject.AddComponent<DecisionTree>();
        tempTre.enabled = false;
        tempTre.behaviorType = config.AIType;

        tempCol = gameObject.AddComponent<ColliderCheck> ();
        tempCol.enabled = false;

        behavior = gameObject.GetComponent<DecisionTree> ();
        behavior.enabled = false;
        behavior.behaviorType = decisionType;

        //AI START//
        tempStat.enabled = true;
        tempPath.enabled = true;
        tempBe.enabled = true;
        tempTre.enabled = true;
        tempCol.enabled = true;
        behavior.enabled = true;

        behavior.startDeciding ();
    }
Пример #42
0
 public ChangeSourcesActionNode(ActionSource action, DecisionTree tree)
     : base(() => { tree.AddActionSource(action); return false; }, tree)
 {
 }
Пример #43
0
        /// <summary>
        ///   Creates a new C4.5 learning algorithm.
        /// </summary>
        /// 
        /// <param name="tree">The decision tree to be generated.</param>
        /// 
        public C45Learning(DecisionTree tree)
        {
            // Initial argument checking
            if (tree == null)
                throw new ArgumentNullException("tree");

            this.tree = tree;
            this.attributeUsageCount = new int[tree.InputCount];
            this.inputRanges = new IntRange[tree.InputCount];
            this.outputClasses = tree.OutputClasses;
            this.maxHeight = tree.InputCount;
            this.splitStep = 1;

            for (int i = 0; i < inputRanges.Length; i++)
                inputRanges[i] = tree.Attributes[i].Range.ToIntRange(false);
        }
Пример #44
0
 public static DecisionTree Generate_decision_tree(ArrayList TempDataList, ArrayList attribute_list)
 {
     DecisionTree tree = new DecisionTree();
     string c=null;
     if (IsAllSameClass(ElectronicsDataList,out c))
     {
         tree.Name = c;
         return tree;
     }
     if (attribute_list.Count == 0)
     {
         double countYes = 0;
         double countNo = 0;
         foreach (ElectronicsData data in ElectronicsDataList)
         {
             if (data.Buy_computer == "yes")
             {
                 countYes++;
             }
             if (data.Buy_computer == "no")
             {
                 countNo++;
             }
         }
         if (countYes > countNo)
         {
             tree.Name = "yes";
         }
         else
         {
             tree.Name = "no";
         }
         return tree;
     }
     string splitting_criterion = attribute_selection_method(ElectronicsDataList, attribute_list);
     attribute_list.Remove(splitting_criterion);
     if (R0.Age.Equals(splitting_criterion))
     {
         foreach (ElectronicsData s in ElectronicsDataList)
         {
             j.Add(s.Age);
         }
     }
     if (R0.Income.Equals(splitting_criterion))
     {
         foreach (ElectronicsData s in ElectronicsDataList)
         {
             j.Add(s.Income);
         }
     }
     if (R0.Student.Equals(splitting_criterion))
     {
         foreach (ElectronicsData s in ElectronicsDataList)
         {
             j.Add(s.Student);
         }
     }
     else if (R0.Credit_rating.Equals(splitting_criterion))
     {
         foreach (ElectronicsData s in ElectronicsDataList)
         {
             j.Add(s.Credit_rating);
         }
     }
     result.Add(splitting_criterion, 0.0f);
     foreach (string item in j)
     {
         foreach (ElectronicsData data in ElectronicsDataList)
         {
             if (data.Age.Equals(item) || data.Income.Equals(item) || data.Credit_rating.Equals(item) || data.Student.Equals(item))
             {
                 Dj.Add(data);
             }
         }
         if (item==null)
         {
             //加一个树叶到节点N,标记为D中的多数类
         }
         else
         {
             DecisionTree n = new DecisionTree();
             //此时计算的分裂节点还是age,出现循环递归
             n = Generate_decision_tree(Dj, attribute_list);
             tree.Next.Add(item, n);
             Dj.Clear();
         }
     }
     return tree;
 }