Ejemplo n.º 1
0
        public void Learn_xorOperation_SeeAssert(int iterations)
        {
            ///Why does this sometimes fail? There are no bad examples.

            string[] headers = new string[]
            {
                "bool1",
                "bool2",
                "xor"
            };
            double[] importance = new double[] {
                0,
                0,
                0
            };
            Policy thePolicy = new Policy();

            #region Training
            Random rand = new Random();
            for (int i = 0; i < iterations; i++)
            {
                bool               bool1 = Convert.ToBoolean(rand.Next(0, 2));
                bool               bool2 = Convert.ToBoolean(rand.Next(0, 2));
                object[]           data  = new object[] { bool1, bool2, (bool1 ^ bool2) };
                DataVectorTraining dvt   = new DataVectorTraining(headers, data, importance, "xor");
                thePolicy.Learn(dvt);
            }
            #endregion

            string htmlTree = thePolicy.ToDecisionTree(new DecisionTree.TreeSettings()
            {
                ShowSubScores = true, ShowBlanks = true
            }).ToHtmlTree();
            System.IO.File.WriteAllText("xor" + iterations + ".html", htmlTree);

            #region Assert
            var dataVectorTT = new DataVector(
                new string[] { "bool1", "bool2" },
                new object[] { true, true });
            var dataVectorFF = new DataVector(
                new string[] { "bool1", "bool2" },
                new object[] { false, false });
            var dataVectorTF = new DataVector(
                new string[] { "bool1", "bool2" },
                new object[] { true, false });
            var dataVectorFT = new DataVector(
                new string[] { "bool1", "bool2" },
                new object[] { false, true });

            Assert.False(Convert.ToBoolean(thePolicy.Classify_ByPolicy(dataVectorTT)));
            Assert.False(Convert.ToBoolean(thePolicy.Classify_ByTree(dataVectorTT)));
            Assert.False(Convert.ToBoolean(thePolicy.Classify_ByPolicy(dataVectorFF)));
            Assert.False(Convert.ToBoolean(thePolicy.Classify_ByTree(dataVectorFF)));
            Assert.True(Convert.ToBoolean(thePolicy.Classify_ByPolicy(dataVectorTF)));
            Assert.True(Convert.ToBoolean(thePolicy.Classify_ByTree(dataVectorTF)));
            Assert.True(Convert.ToBoolean(thePolicy.Classify_ByPolicy(dataVectorFT)));
            Assert.True(Convert.ToBoolean(thePolicy.Classify_ByTree(dataVectorFT)));
            #endregion
        }