Exemple #1
0
        public void DRT_TestValidator(SimpleTest test, Type expectedExceptionType)
        {
            try
            {
                test();
            }
            catch (Exception ex)
            {
                if (expectedExceptionType != null && expectedExceptionType == ex.GetType())
                {
                    return;
                }
                else
                {
                    // otherwise we got an unexpected exception
                    DRT.Assert(false, "XAML Test '{0}' failed.{1}", test.Method.Name, ex.ToString());
                    return;
                }
            }

            if (expectedExceptionType != null)
            {
                DRT.Assert(false, "XAAML Test '{0}' did not throw expected exception of type '{1}'.", test.Method.Name, expectedExceptionType);
            }
        }
Exemple #2
0
        private void VerifyHwndDestruction()
        {
            // Run through each class atom and attempt to query info on the class
            // If that fails, we know it's already been cleaned up.
            // Note that we don't need to verify the windows are gone since a
            // class can't be unregistered until all its windows are destroyed.

            int        returnValue;
            WNDCLASSEX wndclass = new WNDCLASSEX();

            IntPtr hInstance = GetModuleHandle(null);

            if (hInstance == IntPtr.Zero)
            {
                throw new Win32Exception();
            }

            for (int i = 0; i < _classAtoms.Count; i++)
            {
                returnValue = GetClassInfoEx(hInstance, new IntPtr(_classAtoms[i]), wndclass);

                // A return value of 0 means that UnregisterClass failed
                DRT.Assert(returnValue == 0, "Error: Window class wasn't unregistered properly");
            }
        }
Exemple #3
0
        public void ResourceResolutionWithDictionaryOverWriting()
        {
            var rr = new ResourceResolution_OverWrite();

            rr.Measure(new Size(900.0, 900.0));

            Button button = rr.FindName("_button0") as Button;

            DRT.AssertEqual("Test0", button.Content, "Resource overwritten");

            button = rr.FindName("_button1") as Button;
            TextBlock textBlock = button.Content as TextBlock;

            DRT.AssertEqual("Test1", textBlock.Text, "Resource overwritten");

            button = rr.FindName("_button2") as Button;
            Button button2 = button.Template.FindName("_templateButton2", button) as Button;

            DRT.AssertEqual("Test2", button2.Content, "Resource overwritten");

            button    = rr.FindName("_button3") as Button;
            button2   = button.Template.FindName("_templateButton3", button) as Button;
            textBlock = button2.Content as TextBlock;
            DRT.AssertEqual("Test3", textBlock.Text, "Resource overwritten");

            button  = rr.FindName("_button4") as Button;
            button2 = button.Template.FindName("_templateButton4", button) as Button;
            DRT.AssertEqual("Test4", button2.Content, "Resource overwritten");
        }
Exemple #4
0
        public void ResourceResolutionWithDictionaryInsertions()
        {
            var rr = new ResourceResolution_Insert();

            rr.Measure(new Size(900.0, 900.0));

            Button button = rr.FindName("_button0") as Button;

            DRT.AssertEqual("Test0", button.Content, "Inserted Resource interferred with correct resource lookup");

            button = rr.FindName("_button1") as Button;
            TextBlock textBlock = button.Content as TextBlock;

            DRT.AssertEqual("Test1", textBlock.Text, "Inserted Resource interferred with correct resource lookup");

            button = rr.FindName("_button2") as Button;
            Button button2 = button.Template.FindName("_templateButton2", button) as Button;

            DRT.AssertEqual("Test2", button2.Content, "Inserted Resource interferred with correct resource lookup");

            button    = rr.FindName("_button3") as Button;
            button2   = button.Template.FindName("_templateButton3", button) as Button;
            textBlock = button2.Content as TextBlock;
            DRT.AssertEqual("Test3", textBlock.Text, "Inserted Resource interferred with correct resource lookup");

            button  = rr.FindName("_button4") as Button;
            button2 = button.Template.FindName("_templateButton4", button) as Button;
            DRT.AssertEqual("Test4", button2.Content, "Inserted Resource interferred with correct resource lookup");
        }
Exemple #5
0
        // Create HwndSource
        // Set SizeToContent to SizeToContent.Width, add RootVisual, and call SetWindowPos
        // Set SizeToContent to SizeToContent.Height, call SetWindowPos
        // Resize RootVisual, set SizeToContent to SizeToContent.WidthAndHeight, and call SetWindowPos
        private void RunTest()
        {
            _dispatcher = Dispatcher.CurrentDispatcher;

            Console.WriteLine("Testing HwndSource SizeToContent behavior");
            HwndSourceParameters param = new HwndSourceParameters("DrtHwndSource", 100, 100);

            param.SetPosition(0, 0);
            if (!DRT.KeepAlive)
            {
                _source = new HwndSource(param);

                _source.ContentRendered += new EventHandler(OnContentRendered);
                _source.AddHook(new HwndSourceHook(ApplicationFilterMessage));

                _dispatcher.BeginInvoke(
                    DispatcherPriority.Background,
                    new DispatcherOperationCallback(SetAutoWidth),
                    null);
            }
            else  // 'hold' was passed in on the command line - don't finish this suite
            {
                _source = new HwndSource(param);
                _source.AddHook(new HwndSourceHook(ApplicationFilterMessage));
            }

            DRT.Suspend();
        }
Exemple #6
0
        public void TestAutoDetect()
        {
            List <DAttribute> attributes = new List <DAttribute>();
            //read in a simple file with 7 examples with 5 attributes
            List <Case> TestData = DRT.ParseCSV(TestPath + @"\simple\simple1.txt", out attributes);

            Assert.AreEqual(7, TestData.Count);
            Assert.AreEqual(5, attributes.Count);
        }
Exemple #7
0
        private object Quit(object arg)
        {
            if (!_success)
            {
                DRT.ReturnCode = 1;
            }

            _source.Dispose();

            DRT.Resume();
            return(null);
        }
Exemple #8
0
        public void ResourceResolutionForwardReference1()
        {
            bool caughtException = false;

            try
            {
                var rr = new ResourceResolutionForwardReference1();
            }
            catch (System.Windows.Markup.XamlParseException)
            {
                caughtException = true;
            }
            DRT.Assert(caughtException, "Static Resource forward reference did not throw");
        }
Exemple #9
0
        /// <summary>
        /// Creates a parent border to host the tests and returns the list of the tests
        /// </summary>
        /// <returns></returns>
        public override DrtTest[] PrepareTests()
        {
            _rootBorder = new System.Windows.Controls.Border();
            DRT.Show(_rootBorder);

            if (DRT.KeepAlive)
            {
                return(new DrtTest[0]);
            }
            else
            {
                return(new DrtTest[] {
                    new DrtTest(CheckEffectiveOpacity),
                    new DrtTest(CheckResultingStyles),
                });
            }
        }
Exemple #10
0
        public object DRT_XamlLoader(string name, string xamlString,
                                     XamlStringParser loader,
                                     Type expectedExceptionType,
                                     PostTreeValidator validator)
        {
            object root = null;
            bool   hasExpectedException = (expectedExceptionType != null);

            try
            {
                root = loader(xamlString);
            }
            catch (Exception ex)
            {
                if (expectedExceptionType != null && expectedExceptionType == ex.GetType())
                {
                    hasExpectedException = false;
                }
                else
                {
                    // otherwise we got the unexpected exception;
                    DRT.Assert(false, "XAML Test '{0}' failed.{1}", name, ex.ToString());
                    return(null);
                }
            }

            if (hasExpectedException)
            {
                DRT.Assert(false, "XAAML Test '{0}' did not throw expected exception of type '{1}'.", name, expectedExceptionType);
            }

            if (validator != null)
            {
                try
                {
                    validator(root);
                }
                catch (Exception ex)
                {
                    DRT.Assert(false, "XAML Test String '{0}' failed post validation.{1}", name, ex.ToString());
                }
            }
            return(root);
        }
Exemple #11
0
        public void TemplateResourceResolution()
        {
            var rr = new ResourceResolution_DeferredDictionary();

            rr.Measure(new Size(900.0, 900.0));

            SolidColorBrush brush;

            brush = FindTemplatedFillBrush(rr, "_templateParent1", "_colorRectangle1");
            DRT.AssertEqual(Brushes.Cyan.Color, brush.Color, "Resource inside the template was not found");

            brush = FindTemplatedFillBrush(rr, "_templateParent2", "_colorRectangle2");
            DRT.AssertEqual(Brushes.Cyan.Color, brush.Color, "Resource inside the template was not found");

            brush = FindTemplatedFillBrush(rr, "_templateParent3", "_colorRectangle3");
            DRT.AssertEqual(Brushes.Cyan.Color, brush.Color, "Resource inside the template was not found");

            brush = FindTemplatedFillBrush(rr, "_templateParent4", "_colorRectangle4");
            DRT.AssertEqual(Brushes.Cyan.Color, brush.Color, "Resource inside the template was not found");
        }
Exemple #12
0
        /// <summary>
        /// Creates the HwndSource and check the resulting style
        /// </summary>
        private void CheckResultingStyles()
        {
            TestHwndHost test = null;

            // First series with a top level window
            test = new TestHwndHost(child: false, usesPerPixelOpacity: false, usesPerPixelTransparency: false, style: 0);
            _rootBorder.Child = test;
            DRT.Assert(test.SourceCreated && (test.OutUsesPerPixelOpacity == false) && (test.LayeredWindow == false) && (test.ExceptionType == null), test.Description);

            test = new TestHwndHost(child: false, usesPerPixelOpacity: false, usesPerPixelTransparency: true, style: 0);
            _rootBorder.Child = test;
            DRT.Assert(test.SourceCreated && (test.OutUsesPerPixelOpacity == true) && (test.LayeredWindow == true) && (test.ExceptionType == null), test.Description);

            test = new TestHwndHost(child: false, usesPerPixelOpacity: true, usesPerPixelTransparency: false, style: 0);
            _rootBorder.Child = test;
            DRT.Assert(test.SourceCreated && (test.OutUsesPerPixelOpacity == true) && (test.LayeredWindow == true) && (test.ExceptionType == null), test.Description);

            test = new TestHwndHost(child: false, usesPerPixelOpacity: true, usesPerPixelTransparency: true, style: 0);
            _rootBorder.Child = test;
            DRT.Assert(!test.SourceCreated && (test.OutUsesPerPixelOpacity == false) && (test.LayeredWindow == false) && (test.ExceptionType == typeof(InvalidOperationException)), test.Description);


            // Second series with a child window
            test = new TestHwndHost(child: true, usesPerPixelOpacity: false, usesPerPixelTransparency: false, style: 0);
            _rootBorder.Child = test;
            DRT.Assert(test.SourceCreated && (test.OutUsesPerPixelOpacity == false) && (test.LayeredWindow == false) && (test.ExceptionType == null), test.Description);

            // This is the only test where W7 and W8 should differ
            test = new TestHwndHost(child: true, usesPerPixelOpacity: false, usesPerPixelTransparency: true, style: 0);
            _rootBorder.Child = test;
            DRT.Assert(test.SourceCreated && (test.OutUsesPerPixelOpacity == DoesPlatformSupportTransparentChildWindows) && (test.LayeredWindow == DoesPlatformSupportTransparentChildWindows) && (test.ExceptionType == null), test.Description);

            test = new TestHwndHost(child: true, usesPerPixelOpacity: true, usesPerPixelTransparency: false, style: 0);
            _rootBorder.Child = test;
            DRT.Assert(test.SourceCreated && (test.OutUsesPerPixelOpacity == false) && (test.LayeredWindow == false) && (test.ExceptionType == null), test.Description);

            test = new TestHwndHost(child: true, usesPerPixelOpacity: true, usesPerPixelTransparency: true, style: 0);
            _rootBorder.Child = test;
            DRT.Assert(!test.SourceCreated && (test.OutUsesPerPixelOpacity == false) && (test.LayeredWindow == false) && (test.ExceptionType == typeof(InvalidOperationException)), test.Description);
        }
Exemple #13
0
        public void TestDepthLimit()
        {
            List <DAttribute> attributes = new List <DAttribute>(5);

            string[] alphabet = new string[2];
            alphabet[0] = "0"; alphabet[1] = "1";
            attributes.Add(new DAttribute("X_1", 0, new List <string>(alphabet), false, false));
            attributes.Add(new DAttribute("X_2", 1, new List <string>(alphabet), false, false));
            attributes.Add(new DAttribute("X_3", 2, new List <string>(alphabet), false, false));
            attributes.Add(new DAttribute("X_4", 3, new List <string>(alphabet), false, false));
            attributes.Add(new DAttribute("VarFinal", 4, new List <string>(alphabet), false, true));

            List <Case> TestData = DRT.ParseCSV(attributes.ToArray(), TestPath + @"\simple\simple1.txt");


            ID3_Node Tree = ID3Tools.ID3(attributes, TestData, 1, ID3Tools.EntropyCalucalation.IG);

            //it works

            System.Console.WriteLine(Tree.PrintTree(attributes.ToArray()));
            int i = 0; // a line of code on which to wait afterwards.
        }
Exemple #14
0
        public void ResourceResolutionForwardReference2()
        {
            var rr = new ResourceResolutionForwardReference2();

            rr.Measure(new Size(900.0, 900.0));

            ContentControl  contentControl;
            Rectangle       rect;
            ContentControl  innerContentControl;
            SolidColorBrush brush;

            contentControl = rr.FindName("_contentControl1") as ContentControl;
            rect           = contentControl.Content as Rectangle;
            brush          = rect.Fill as SolidColorBrush;
            DRT.AssertEqual(Brushes.Cyan.Color, brush.Color, "Incorrect resource resolution (likely forward)");

            contentControl      = rr.FindName("_contentControl2") as ContentControl;
            innerContentControl = contentControl.Template.FindName("_innerContentControl2", contentControl) as ContentControl;
            rect  = innerContentControl.Content as Rectangle;
            brush = rect.Fill as SolidColorBrush;
            DRT.AssertEqual(Brushes.Cyan.Color, brush.Color, "Incorrect resource resolution (likely forward)");
        }
Exemple #15
0
        //----------------------------------------------------------------------
        // Private Methods - Handlers TestServices
        //----------------------------------------------------------------------

        /// <summary>
        /// Bridges TestServices Messages to DrtBase or Console.
        /// </summary>
        /// <param name="sender">Unused.</param>
        /// <param name="args">The message arguments.</param>
        private void MessageSentHandler(object sender, MessageEventArgs args)
        {
            switch (args.Category)
            {
            case MessageEventArgs.MessageCategory.Internal:
                if (TestServices.Diagnose)
                {
                    if (base.DelayOutput)
                    {
                        DRT.LogOutput(args.Message);
                    }
                    else
                    {
                        Console.ForegroundColor = ConsoleColor.Gray;
                        Console.WriteLine(args.Message);
                        Console.ResetColor();
                    }
                }
                break;

            case MessageEventArgs.MessageCategory.Trace:
                DRT.Trace(args.Message);
                break;

            case MessageEventArgs.MessageCategory.Log:
                DRT.LogOutput(args.Message);
                break;

            case MessageEventArgs.MessageCategory.Warning:
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine(args.Message);
                Console.ResetColor();
                break;

            case MessageEventArgs.MessageCategory.Assert:
                DRT.Assert(false, args.Message);
                break;
            }
        }
Exemple #16
0
        /// <summary>
        /// Tests that EffectivePerPixelOpacity has the expected value
        /// </summary>
        /// <param name="platformSupport"></param>
        /// <param name="child"></param>
        /// <param name="usesPerPixelOpacity"></param>
        /// <param name="usesPerPixelTransparency"></param>
        /// <param name="expectedEffectiveOpacity"></param>
        /// <param name="expectedException"></param>
        void TestOpacity(bool platformSupport, bool child, bool usesPerPixelOpacity, bool usesPerPixelTransparency, bool expectedEffectiveOpacity, Type expectedException)
        {
            string description = string.Format("platformSupport={0}, child={1}, usesPerPixelOpacity={2}, usesPerPixelTransparency={3}", platformSupport, child, usesPerPixelOpacity, usesPerPixelTransparency);

            try
            {
                SetPlatformSupportsTransparentChildWindows(platformSupport);
                HwndSourceParameters p = new HwndSourceParameters();
                p.WindowStyle              = child ? Win32.WS_CHILD : 0;
                p.UsesPerPixelOpacity      = usesPerPixelOpacity;
                p.UsesPerPixelTransparency = usesPerPixelTransparency;
                DRT.Assert(GetEffectivePerPixelOpacity(p) == expectedEffectiveOpacity, description);
            }
            catch (TargetInvocationException ex)
            {
                // Caused by use of reflection to get EffectivePerPixelOpacity
                DRT.Assert(ex.InnerException.GetType() == expectedException, description);
            }
            catch (Exception ex)
            {
                DRT.Assert(ex.GetType() == expectedException, description);
            }
        }
Exemple #17
0
        public void TestSimple()
        {
            //Initialize the attributes beforehand to make it more readable when debugging

            List <DAttribute> attributes = new List <DAttribute>(5);

            string[] alphabet = new string[2];
            alphabet[0] = "0"; alphabet[1] = "1";
            attributes.Add(new DAttribute("X_1", 0, new List <string>(alphabet), false, false));
            attributes.Add(new DAttribute("X_2", 1, new List <string>(alphabet), false, false));
            attributes.Add(new DAttribute("X_3", 2, new List <string>(alphabet), false, false));
            attributes.Add(new DAttribute("X_4", 3, new List <string>(alphabet), false, false));
            attributes.Add(new DAttribute("VarFinal", 4, new List <string>(alphabet), false, true));

            List <Case> TestData = DRT.ParseCSV(attributes.ToArray(), TestPath + @"\simple\simple1.txt");


            ID3_Node Tree = ID3Tools.ID3(attributes, TestData, 999, ID3Tools.EntropyCalucalation.IG);

            System.Console.WriteLine(Tree.PrintTree(attributes.ToArray()));

            Assert.AreEqual(0, ID3Tools.TestWithTree(TestData[6], Tree));
        }
        public static void Main()
        {
            //Attributes for the data
            DAttribute[] Attributes = new DAttribute[5];

            Attributes[0] = new DAttribute("Varaince", 1, null, DAttribute.Type.Numeric, false);
            Attributes[1] = new DAttribute("Skew", 1, null, DAttribute.Type.Numeric, false);
            Attributes[2] = new DAttribute("Curtosis", 1, null, DAttribute.Type.Numeric, false);
            Attributes[3] = new DAttribute("Entropy", 1, null, DAttribute.Type.Numeric, false);

            Attributes[4] = new DAttribute("Genuine", 1, new List <String>(new String[] { "0", "1" }), DAttribute.Type.Categorical, false);

            List <Case> TrainBank = DRT.ParseCSV(Attributes, TestPath + @"\bank-note\bank-note\train.csv", false);
            List <Case> TestBank  = DRT.ParseCSV(Attributes, TestPath + @"\bank-note\bank-note\test.csv", false);

            //Convert output to -1,1 as opposed to 0,1

            Case.ColXtoY(TrainBank, 4, 0, -1);
            Case.ColXtoY(TestBank, 4, 0, -1);

            StringBuilder output = new StringBuilder();

            //begin testing


            double[] C = new double[] { 100.0 / 873.0, 500.0 / 873.0, 700.0 / 873.0 };

            Console.WriteLine("Starting primal Subgradient Decent with C = { 100/873, 500/873, 700/873 }");
            Console.WriteLine("=====================================================================================");

            Console.WriteLine("\nUsing NewLR = LR / (1 + LR * T / D) for learning rate. \n");

            //set up parameters
            double LearningRate   = .2;
            double LearningAdjust = .75;
            int    Seed           = 1500;

            //report
            Console.WriteLine("\tBase Learning Rate = " + LearningRate);
            Console.WriteLine("\tNum epochs (T) = 100");
            Console.WriteLine("\tLearning Rate Adjustment = " + LearningAdjust);

            SVMGradient current; //set up the variable for the SVM for the all the tests

            for (int j = 0; j < 3; j++)
            {
                current = new SVMGradient(C[j], LearningRate, LearningAdjust, Seed, TrainBank);
                Console.WriteLine("\nCreated new primal learner with C = " + C[j]);


                for (int i = 0; i < 100; i++)
                {
                    if (i % 10 == 9)
                    {
                        Console.WriteLine("\tCompleted " + (i + 1) + " Epochs.");
                        Console.WriteLine("\tTraining error = " + current.getTrainingError());
                    }
                    current.PGradientEpoch(1); //do 100 epochs
                }

                Console.WriteLine("\n\tTraining error = " + current.getTrainingError());
                Console.WriteLine("\tTesting error  = " + current.getTestError(TestBank));
                double[] weight = current.getWeight();
                Console.Write("\tWeight = { " + weight[0]);
                for (int i = 1; i < weight.Length; i++)
                {
                    Console.Write(", " + weight[i]);
                }
                Console.Write("}\n");
                Console.WriteLine("\tBias = " + current.getBias());
            }

            Console.WriteLine("-------------------------------------------------------------------------------------\n");

            Console.WriteLine("Using NewLR = LR/ (1 + T) for learning rate.\n");
            Console.WriteLine("\tBase Learning Rate = " + LearningRate);
            Console.WriteLine("\tNum epochs (T) = 100");
            Console.WriteLine("\tLearning Rate Adjustment = " + LearningAdjust);

            for (int j = 0; j < 3; j++)
            {
                current = new SVMGradient(C[j], LearningRate, LearningAdjust, Seed, TrainBank);

                Console.WriteLine("\nCreated new primal learner with C = " + C[j]);

                for (int i = 0; i < 100; i++)
                {
                    if (i % 10 == 9)
                    {
                        Console.WriteLine("\tCompleted " + (i + 1) + " Epochs.");
                        Console.WriteLine("\tTraining error = " + current.getTrainingError());
                    }
                    current.PGradientEpoch(1); //do 100 epochs
                }

                Console.WriteLine("\n\tTraining error = " + current.getTrainingError());
                Console.WriteLine("\tTesting error  = " + current.getTestError(TestBank));
                double[] weight = current.getWeight();
                Console.Write("\tWeight = { " + weight[0]);
                for (int i = 1; i < weight.Length; i++)
                {
                    Console.Write(", " + weight[i]);
                }
                Console.Write("}\n");
                Console.WriteLine("\tBias = " + current.getBias());
            }
            //let the user read the stuff on screen.
            Console.WriteLine("\n\n\nFinished execution. Hit any key to exit.");

            Console.Read();
        }
        public static void Main()
        {
            // ========= Part 1 ============= //

            if (BuildCarTrees)
            {
                //This is the car example.
                List <DAttribute> attributeCars = new List <DAttribute>(7);
                //while I could auto detect this, it's much easier to read the trees if I name the DataAttributes ahead of time
                //below data descriptions come from data-desc.txt, located near the data for this training data.
                string[] AVariants = new string[] { "vhigh", "high", "med", "low" }; //array of attribute variants to pass in to an attribute


                attributeCars.Add(new DAttribute("buying", 0, new List <string>(AVariants), DAttribute.Type.Categorical, false));
                attributeCars.Add(new DAttribute("maint", 1, new List <string>(AVariants), DAttribute.Type.Categorical, false));

                AVariants = new string[] { "2", "3", "4", "5more" };
                attributeCars.Add(new DAttribute("doors", 2, new List <string>(AVariants), DAttribute.Type.Categorical, false));
                AVariants = new string[] { "2", "4", "more" };
                attributeCars.Add(new DAttribute("persons", 3, new List <string>(AVariants), DAttribute.Type.Categorical, false));
                AVariants = new string[] { "small", "med", "big" };
                attributeCars.Add(new DAttribute("lug_boot", 4, new List <string>(AVariants), DAttribute.Type.Categorical, false));
                AVariants = new string[] { "low", "med", "high" };
                attributeCars.Add(new DAttribute("safety", 5, new List <string>(AVariants), DAttribute.Type.Categorical, false));
                AVariants = new string[] { "unacc", "acc", "good", "vgood" };
                attributeCars.Add(new DAttribute("label", 6, new List <string>(AVariants), DAttribute.Type.Categorical, true));


                List <Case> TrainCars = DRT.ParseCSV(attributeCars.ToArray(), TestPath + @"\car\train.csv");
                List <Case> TestCars  = DRT.ParseCSV(attributeCars.ToArray(), TestPath + @"\car\test.csv");

                StringBuilder TreeLayout = new StringBuilder();

                for (int depth = 1; depth < 7; depth++)
                {
                    ID3_Node Tree = ID3Tools.ID3(attributeCars, TrainCars, depth, ID3Tools.EntropyCalucalation.IG);
                    //add the tree to the string builder and prepare to write it to a file.

                    Double TrainError = ID3Tools.FindTestError(TrainCars, attributeCars, Tree);
                    Double TestError  = ID3Tools.FindTestError(TestCars, attributeCars, Tree);

                    TreeLayout.Append("Information Gain Cars, Max Depth of " + depth + ". Test Error = " + TestError + ". TrainError = " + TrainError + " \n \n" + Tree.PrintTree(attributeCars.ToArray()) + "\n ----------------------------------------------------------------- \n");
                    Console.WriteLine("Finished an IG Tree");
                }

                for (int depth = 1; depth < 7; depth++)
                {
                    ID3_Node Tree = ID3Tools.ID3(attributeCars, TrainCars, depth, ID3Tools.EntropyCalucalation.GI);
                    //add the tree to the string builder and prepare to write it to a file.

                    Double TrainError = ID3Tools.FindTestError(TrainCars, attributeCars, Tree);
                    Double TestError  = ID3Tools.FindTestError(TestCars, attributeCars, Tree);

                    TreeLayout.Append("Gini Index Cars, Max Depth of " + depth + ". Test Error = " + TestError + ". TrainError = " + TrainError + " \n \n" + Tree.PrintTree(attributeCars.ToArray()) + "\n ----------------------------------------------------------------- \n");
                    Console.WriteLine("Finished a GI Tree");
                }

                for (int depth = 1; depth < 7; depth++)
                {
                    ID3_Node Tree = ID3Tools.ID3(attributeCars, TrainCars, depth, ID3Tools.EntropyCalucalation.ME);
                    //add the tree to the string builder and prepare to write it to a file.

                    Double TrainError = ID3Tools.FindTestError(TrainCars, attributeCars, Tree);
                    Double TestError  = ID3Tools.FindTestError(TestCars, attributeCars, Tree);

                    TreeLayout.Append("Majority Error Cars, Max Depth of " + depth + ". Test Error = " + TestError + ". TrainError = " + TrainError + " \n \n" + Tree.PrintTree(attributeCars.ToArray()) + "\n ----------------------------------------------------------------- \n");
                    Console.WriteLine("Finished an ME Tree");
                }

                Console.WriteLine("Writing all results to DecisionTree/TestingData/RunResults/ResultsCars.txt");
                System.IO.File.WriteAllText(TestPath + @"/RunResults/ResultsCars.txt", TreeLayout.ToString());
            }

            // ========= Part 2 ============= //
            // bank information
            if (BuildBankTrees)
            {
                List <DAttribute> attributeBank = new List <DAttribute>(7);
                //Once again, could auto detect, but doing so makes the data harder to read. Furthermore, autodetecting doesn't work for filling in missing values.
                //below data descriptions come from data-desc.txt, located near the data for this training data.

                string[] AVariants;

                //age being numeric means that the actual variants will be figured out at run time. The variant will be overwritten when we pull in the testing data.
                attributeBank.Add(new DAttribute("age", 0, null, DAttribute.Type.BinaryNumeric, false));
                AVariants = new string[] { "admin.", "unknown", "unemployed", "management", "housemaid", "entrepreneur", "student",
                                           "blue-collar", "self-employed", "retired", "technician", "services" };
                attributeBank.Add(new DAttribute("job", 1, new List <string>(AVariants), DAttribute.Type.Categorical, false));
                AVariants = new string[] { "married", "divorced", "single" };
                attributeBank.Add(new DAttribute("marital", 2, new List <string>(AVariants), DAttribute.Type.Categorical, false));
                AVariants = new string[] { "unknown", "secondary", "primary", "tertiary" };
                attributeBank.Add(new DAttribute("education", 3, new List <string>(AVariants), DAttribute.Type.Categorical, false));
                AVariants = new string[] { "yes", "no" };
                attributeBank.Add(new DAttribute("default", 4, new List <string>(AVariants), DAttribute.Type.Categorical, false));

                attributeBank.Add(new DAttribute("balance", 5, null, DAttribute.Type.BinaryNumeric, false));
                AVariants = new string[] { "yes", "no" };
                attributeBank.Add(new DAttribute("housing", 6, new List <string>(AVariants), DAttribute.Type.Categorical, false));
                AVariants = new string[] { "yes", "no" };
                attributeBank.Add(new DAttribute("loan", 7, new List <string>(AVariants), DAttribute.Type.Categorical, false));
                AVariants = new string[] { "unknown", "telephone", "cellular" };
                attributeBank.Add(new DAttribute("contact", 8, new List <string>(AVariants), DAttribute.Type.Categorical, false));

                attributeBank.Add(new DAttribute("day", 9, null, DAttribute.Type.BinaryNumeric, false));
                AVariants = new string[] { "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec" };
                attributeBank.Add(new DAttribute("month", 10, new List <string>(AVariants), DAttribute.Type.Categorical, false));

                attributeBank.Add(new DAttribute("duration", 11, null, DAttribute.Type.BinaryNumeric, false));

                attributeBank.Add(new DAttribute("campaign", 12, null, DAttribute.Type.BinaryNumeric, false));

                attributeBank.Add(new DAttribute("pdays", 13, null, DAttribute.Type.BinaryNumeric, false));

                attributeBank.Add(new DAttribute("previous", 14, null, DAttribute.Type.BinaryNumeric, false));
                AVariants = new string[] { "unknown", "other", "failure", "success" }; //If unknown needs to be filled in, remove it from this list.
                attributeBank.Add(new DAttribute("poutcome", 15, new List <string>(AVariants), DAttribute.Type.Categorical, false));
                AVariants = new string[] { "yes", "no" };
                attributeBank.Add(new DAttribute("result", 16, new List <string>(AVariants), DAttribute.Type.Categorical, true));

                if (BuildBankTreeNormal)
                {
                    List <Case> TrainBank = DRT.ParseCSV(attributeBank.ToArray(), TestPath + @"\bank\train.csv", true);
                    List <Case> TestBank  = DRT.ParseCSV(attributeBank.ToArray(), TestPath + @"\bank\test.csv", false);

                    StringBuilder TreeLayout = new StringBuilder();

                    for (int depth = 1; depth < 17; depth++)
                    {
                        ID3_Node Tree = ID3Tools.ID3(attributeBank, TrainBank, depth, ID3Tools.EntropyCalucalation.IG);
                        //add the tree to the string builder and prepare to write it to a file.

                        Double TrainError = ID3Tools.FindTestError(TrainBank, attributeBank, Tree);
                        Double TestError  = ID3Tools.FindTestError(TestBank, attributeBank, Tree);

                        TreeLayout.Append("Information Gain Bank, Max Depth of " + depth + ". Test Error = " + TestError + ". TrainError = " + TrainError + " \n \n" + Tree.PrintTree(attributeBank.ToArray()) + "\n ----------------------------------------------------------------- \n");
                        Console.WriteLine("Finished an IG Tree");
                    }

                    for (int depth = 1; depth < 17; depth++)
                    {
                        ID3_Node Tree = ID3Tools.ID3(attributeBank, TrainBank, depth, ID3Tools.EntropyCalucalation.GI);
                        //add the tree to the string builder and prepare to write it to a file.

                        Double TrainError = ID3Tools.FindTestError(TrainBank, attributeBank, Tree);
                        Double TestError  = ID3Tools.FindTestError(TestBank, attributeBank, Tree);

                        TreeLayout.Append("Gini Index Bank, Max Depth of " + depth + ". Test Error = " + TestError + ". TrainError = " + TrainError + " \n \n" + Tree.PrintTree(attributeBank.ToArray()) + "\n ----------------------------------------------------------------- \n");
                        Console.WriteLine("Finished a GI Tree");
                    }

                    for (int depth = 1; depth < 17; depth++)
                    {
                        ID3_Node Tree = ID3Tools.ID3(attributeBank, TrainBank, depth, ID3Tools.EntropyCalucalation.ME);
                        //add the tree to the string builder and prepare to write it to a file.

                        Double TrainError = ID3Tools.FindTestError(TrainBank, attributeBank, Tree);
                        Double TestError  = ID3Tools.FindTestError(TestBank, attributeBank, Tree);

                        TreeLayout.Append("Majority Error Bank, Max Depth of " + depth + ". Test Error = " + TestError + ". TrainError = " + TrainError + " \n \n" + Tree.PrintTree(attributeBank.ToArray()) + "\n ----------------------------------------------------------------- \n");
                        Console.WriteLine("Finished an ME Tree");
                    }

                    Console.WriteLine("Writing all results to DecisionTree/TestingData/RunResults/ResultsBankNormal.txt");
                    System.IO.File.WriteAllText(TestPath + @"/RunResults/ResultsBankNormal.txt", TreeLayout.ToString());
                }
                if (BuildBankMissingVals)
                {
                    //In this case, the "unknown" values in poutcome
                    attributeBank[15] = new DAttribute("poutcome", 15, new List <string>(new string[] { "unknown", "other", "failure", "success" }), DAttribute.Type.Categorical, false);

                    //Now we rebuild all the datasets, which will have elements filled in by the majority elements.
                    List <Case> TrainBank = DRT.ParseCSV(attributeBank.ToArray(), TestPath + @"\bank\train.csv", true);
                    List <Case> TestBank  = DRT.ParseCSV(attributeBank.ToArray(), TestPath + @"\bank\test.csv", false);

                    StringBuilder TreeLayout = new StringBuilder();

                    for (int depth = 1; depth < 17; depth++)
                    {
                        ID3_Node Tree = ID3Tools.ID3(attributeBank, TrainBank, depth, ID3Tools.EntropyCalucalation.IG);
                        //add the tree to the string builder and prepare to write it to a file.

                        Double TrainError = ID3Tools.FindTestError(TrainBank, attributeBank, Tree);
                        Double TestError  = ID3Tools.FindTestError(TestBank, attributeBank, Tree);

                        TreeLayout.Append("Information Gain Bank, Max Depth of " + depth + ". Test Error = " + TestError + ". TrainError = " + TrainError + " \n \n" + Tree.PrintTree(attributeBank.ToArray()) + "\n ----------------------------------------------------------------- \n");
                        Console.WriteLine("Finished an IG Tree");
                    }

                    for (int depth = 1; depth < 17; depth++)
                    {
                        ID3_Node Tree = ID3Tools.ID3(attributeBank, TrainBank, depth, ID3Tools.EntropyCalucalation.GI);
                        //add the tree to the string builder and prepare to write it to a file.

                        Double TrainError = ID3Tools.FindTestError(TrainBank, attributeBank, Tree);
                        Double TestError  = ID3Tools.FindTestError(TestBank, attributeBank, Tree);

                        TreeLayout.Append("Gini Index Bank, Max Depth of " + depth + ". Test Error = " + TestError + ". TrainError = " + TrainError + " \n \n" + Tree.PrintTree(attributeBank.ToArray()) + "\n ----------------------------------------------------------------- \n");
                        Console.WriteLine("Finished a GI Tree");
                    }

                    for (int depth = 1; depth < 17; depth++)
                    {
                        ID3_Node Tree = ID3Tools.ID3(attributeBank, TrainBank, depth, ID3Tools.EntropyCalucalation.ME);
                        //add the tree to the string builder and prepare to write it to a file.

                        Double TrainError = ID3Tools.FindTestError(TrainBank, attributeBank, Tree);
                        Double TestError  = ID3Tools.FindTestError(TestBank, attributeBank, Tree);

                        TreeLayout.Append("Majority Error Bank, Max Depth of " + depth + ". Test Error = " + TestError + ". TrainError = " + TrainError + " \n \n" + Tree.PrintTree(attributeBank.ToArray()) + "\n ----------------------------------------------------------------- \n");
                        Console.WriteLine("Finished an ME Tree");
                    }

                    Console.WriteLine("Writing all results to DecisionTree/TestingData/RunResults/ResultsBankMissingVals.txt");
                    System.IO.File.WriteAllText(TestPath + @"/RunResults/ResultsBankMissingVals.txt", TreeLayout.ToString());
                }
            }
        }
        public static void Main()
        {
            //Attributes for the data
            DAttribute[] Attributes = new DAttribute[5];

            Attributes[0] = new DAttribute("Varaince", 1, null, DAttribute.Type.Numeric, false);
            Attributes[1] = new DAttribute("Skew", 1, null, DAttribute.Type.Numeric, false);
            Attributes[2] = new DAttribute("Curtosis", 1, null, DAttribute.Type.Numeric, false);
            Attributes[3] = new DAttribute("Entropy", 1, null, DAttribute.Type.Numeric, false);

            Attributes[4] = new DAttribute("Genuine", 1, new List <String>(new String[] { "0", "1" }), DAttribute.Type.Categorical, false);

            List <Case> TrainBank = DRT.ParseCSV(Attributes, TestPath + @"\bank-note\bank-note\train.csv", false);
            List <Case> TestBank  = DRT.ParseCSV(Attributes, TestPath + @"\bank-note\bank-note\test.csv", false);

            //Convert output to -1,1 as opposed to 0,1

            Case.ColXtoY(TrainBank, 4, 0, -1);
            Case.ColXtoY(TestBank, 4, 0, -1);

            //StringBuilder output = new StringBuilder();

            //begin testing


            int[] NumNeurons = new int[] { 5, 10, 25, 50, 100 };

            Console.WriteLine("Testing 3 layer neural nets with X neurons per layer = { 5, 10, 25, 50, 100 }");
            Console.WriteLine("=====================================================================================");

            Console.WriteLine("\nUsing NewLR = Base LR / (1 + Base LR * T / D) for learning rate. \n");

            //set up parameters
            double LearningRate = 1; //arbitrary number
            int    Seed         = 1500;

            //report
            Console.WriteLine("\tBase Learning Rate = " + LearningRate);
            Console.WriteLine("\tNum epochs (T) = 100");

            NeuralNet current; //set up the variable for the SVM for the all the tests

            for (int j = 0; j < NumNeurons.Length; j++)
            {
                current = new NeuralNet(LearningRate, Seed, 4, NumNeurons[j], 2); //two hidden layers + 1 output, always
                Console.WriteLine("\nCreated new three layer Neural Net with " + NumNeurons[j] + " Neurons per layer.");


                for (int i = 0; i < 20; i++)
                {
                    current.runEpochs(10, TrainBank); //do 100 epochs
                    Console.WriteLine("\tCompleted " + (i + 1) * 10 + " Epochs.");
                    Console.WriteLine("\tTraining error at " + (i + 1) * 10 + " epochs = " + current.getError(TrainBank));
                }

                Console.WriteLine("\n\tFinal Training error = " + current.getError(TrainBank));
                Console.WriteLine("\tTesting error  \t= " + current.getError(TestBank));
            }

            /*
             * Console.WriteLine("-------------------------------------------------------------------------------------\n");
             *
             * Console.WriteLine("Using NewLR = LR/ (1 + T) for learning rate.\n");
             * Console.WriteLine("\tBase Learning Rate = " + LearningRate);
             * Console.WriteLine("\tNum epochs (T) = 100");
             *
             * for (int j = 0; j < 3; j++)
             * {
             *  current = new SVMGradient(C[j], LearningRate, LearningAdjust, Seed, TrainBank);
             *
             *  Console.WriteLine("\nCreated new primal learner with C = " + C[j]);
             *
             *  for (int i = 0; i < 100; i++)
             *  {
             *      if (i % 10 == 9)
             *      {
             *          Console.WriteLine("\tCompleted " + (i + 1) + " Epochs.");
             *          Console.WriteLine("\tTraining error = " + current.getTrainingError());
             *      }
             *      current.PGradientEpoch(1); //do 100 epochs
             *
             *  }
             *
             *  Console.WriteLine("\n\tTraining error = " + current.getTrainingError());
             *  Console.WriteLine("\tTesting error  = " + current.getTestError(TestBank));
             *  double[] weight = current.getWeight();
             *  Console.Write("\tWeight = { " + weight[0]);
             *  for (int i = 1; i < weight.Length; i++)
             *  {
             *      Console.Write(", " + weight[i]);
             *  }
             *  Console.Write("}\n");
             *  Console.WriteLine("\tBias = " + current.getBias());
             * }
             */
            //let the user read the stuff on screen.
            Console.WriteLine("\n\n\nFinished execution. Hit any key to exit.");

            Console.Read();
        }
Exemple #21
0
        public static void Main()
        {
            List <DAttribute> attributeBank = new List <DAttribute>(7);

            //Once again, could auto detect, but doing so makes the data harder to read. Furthermore, autodetecting doesn't work for filling in missing values.
            //below data descriptions come from data-desc.txt, located near the data for this training data.

            string[] AVariants;

            //age being numeric means that the actual variants will be figured out at run time. The variant will be overwritten when we pull in the testing data.
            attributeBank.Add(new DAttribute("age", 0, null, DAttribute.Type.BinaryNumeric, false));
            AVariants = new string[] { "admin.", "unknown", "unemployed", "management", "housemaid", "entrepreneur", "student",
                                       "blue-collar", "self-employed", "retired", "technician", "services" };
            attributeBank.Add(new DAttribute("job", 1, new List <string>(AVariants), DAttribute.Type.Categorical, false));
            AVariants = new string[] { "married", "divorced", "single" };
            attributeBank.Add(new DAttribute("marital", 2, new List <string>(AVariants), DAttribute.Type.Categorical, false));
            AVariants = new string[] { "unknown", "secondary", "primary", "tertiary" };
            attributeBank.Add(new DAttribute("education", 3, new List <string>(AVariants), DAttribute.Type.Categorical, false));
            AVariants = new string[] { "yes", "no" };
            attributeBank.Add(new DAttribute("default", 4, new List <string>(AVariants), DAttribute.Type.Categorical, false));

            attributeBank.Add(new DAttribute("balance", 5, null, DAttribute.Type.BinaryNumeric, false));
            AVariants = new string[] { "yes", "no" };
            attributeBank.Add(new DAttribute("housing", 6, new List <string>(AVariants), DAttribute.Type.Categorical, false));
            AVariants = new string[] { "yes", "no" };
            attributeBank.Add(new DAttribute("loan", 7, new List <string>(AVariants), DAttribute.Type.Categorical, false));
            AVariants = new string[] { "unknown", "telephone", "cellular" };
            attributeBank.Add(new DAttribute("contact", 8, new List <string>(AVariants), DAttribute.Type.Categorical, false));

            attributeBank.Add(new DAttribute("day", 9, null, DAttribute.Type.BinaryNumeric, false));
            AVariants = new string[] { "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec" };
            attributeBank.Add(new DAttribute("month", 10, new List <string>(AVariants), DAttribute.Type.Categorical, false));

            attributeBank.Add(new DAttribute("duration", 11, null, DAttribute.Type.BinaryNumeric, false));

            attributeBank.Add(new DAttribute("campaign", 12, null, DAttribute.Type.BinaryNumeric, false));

            attributeBank.Add(new DAttribute("pdays", 13, null, DAttribute.Type.BinaryNumeric, false));

            attributeBank.Add(new DAttribute("previous", 14, null, DAttribute.Type.BinaryNumeric, false));
            AVariants = new string[] { "unknown", "other", "failure", "success" }; //If unknown needs to be filled in, remove it from this list.
            attributeBank.Add(new DAttribute("poutcome", 15, new List <string>(AVariants), DAttribute.Type.Categorical, false));
            AVariants = new string[] { "yes", "no" };
            attributeBank.Add(new DAttribute("result", 16, new List <string>(AVariants), DAttribute.Type.Categorical, true));



            List <Case> TrainBank = DRT.ParseCSV(attributeBank.ToArray(), TestPath + @"\bank\train.csv", true);
            List <Case> TestBank  = DRT.ParseCSV(attributeBank.ToArray(), TestPath + @"\bank\test.csv", false);



            if (UseBoost)
            {
                StringBuilder output     = new StringBuilder();
                StringBuilder outputTree = new StringBuilder();

                output.Append("T(rees),Training Error,Testing Error\n"); //going to generate a csv file the ensemble learner's performance

                EnsembleLearner current = null;                          //initialize. Doesn't matter what to
                for (int i = 1; i < NumIterations; i++)                  //Assignment specifies 1000 iterations
                {
                    current = EnsembleTools.AdaBoost(i, TrainBank, attributeBank);

                    double TrainingError = current.TestEnsembleClassMass(TrainBank, attributeBank);
                    double TestingError  = current.TestEnsembleClassMass(TestBank, attributeBank);


                    Console.WriteLine("Built an AdaBoost Learner with " + i + " Trees.");
                    output.Append(i + "," + TrainingError + "," + TestingError + "\n");                     //write a new line for the CSV file
                    TrainBank = DRT.ParseCSV(attributeBank.ToArray(), TestPath + @"\bank\train.csv", true); //reset data since I'm too lazy to copy it
                }

                StringBuilder output2 = new StringBuilder();
                outputTree.Append("Tree#,Training Error,Testing Error\n");
                for (int i = 0; i < NumIterations - 1; i++)
                {
                    ID3_Node node = current.Trees[i];

                    double TrainingError = ID3Tools.FindTestError(TrainBank, attributeBank, node);
                    double TestingError  = ID3Tools.FindTestError(TestBank, attributeBank, node);

                    int index = i + 1;
                    outputTree.Append(index + "," + TrainingError + "," + TestingError + "\n"); //write a new line for the CSV file
                }

                Console.WriteLine("Writing all results to Ensemble\\ Learning/TestingData/RunResults/ResultsBankBoost.csv");
                System.IO.File.WriteAllText(TestPath + @"/RunResults/ResultsBankBoost.csv", output.ToString());
                System.IO.File.WriteAllText(TestPath + @"/RunResults/ResultsBankBoostTrees.csv", outputTree.ToString());
            }

            if (UseBag)
            {
                StringBuilder output     = new StringBuilder();
                StringBuilder outputTree = new StringBuilder();

                output.Append("T(rees),Training Error,Testing Error\n"); //going to generate a csv file the ensemble learner's performance

                EnsembleLearner current = null;                          //initialize. Doesn't matter what to
                for (int i = 1; i < NumIterations; i++)                  //Assignment specifies 1000 iterations
                {
                    current = EnsembleTools.Bagging(i, TrainBank.Count, true, RNGseed, TrainBank, attributeBank);

                    double TrainingError = current.TestEnsembleClassMass(TrainBank, attributeBank);
                    double TestingError  = current.TestEnsembleClassMass(TestBank, attributeBank);

                    Console.WriteLine("Built a Bagged Learner with " + i + " Trees.");

                    output.Append(i + "," + TrainingError + "," + TestingError + "\n"); //write a new line for the CSV file
                }

                StringBuilder output2 = new StringBuilder();
                outputTree.Append("Tree#,Training Error,Testing Error\n");
                for (int i = 0; i < NumIterations - 1; i++)
                {
                    ID3_Node node = current.Trees[i];

                    double TrainingError = ID3Tools.FindTestError(TrainBank, attributeBank, node);
                    double TestingError  = ID3Tools.FindTestError(TestBank, attributeBank, node);

                    int index = i + 1;
                    outputTree.Append(index + "," + TrainingError + "," + TestingError + "\n"); //write a new line for the CSV file
                }

                Console.WriteLine("Writing all results to Ensemble\\ Learning/TestingData/RunResults/ResultsBankBag.csv");
                System.IO.File.WriteAllText(TestPath + @"/RunResults/ResultsBankBagTemp.csv", output.ToString());
                System.IO.File.WriteAllText(TestPath + @"/RunResults/ResultsBankBagTreesTemp.csv", outputTree.ToString());
            }

            if (UseBagBias)
            {
                Random Gen = new Random(RNGseed);

                double averageResult = 0;
                foreach (Case c in TrainBank)
                {
                    averageResult += c.AttributeVals[16]; //add target label value
                }
                averageResult = averageResult / (double)TrainBank.Count;

                double AverageTreeVariance = 0;
                double AverageBagVariance  = 0;

                double AverageTreeBias = 0;
                double AverageBagBias  = 0;

                StringBuilder output = new StringBuilder();
                output.Append("TreeBias,EnsBias,TreeVar,EnsVar\n");

                for (int i = 1; i < 101; i++)
                {
                    List <Case>     Sample  = EnsembleTools.GetRandomSubset(true, 1000, Gen, TrainBank);               //Generate samples without replacement
                    EnsembleLearner current = EnsembleTools.Bagging(1000, 1000, true, RNGseed, Sample, attributeBank); //Generate samples allowing duplicates
                    //Calculate bias first

                    double Bias = 0;//tree
                    foreach (Case c in TrainBank)
                    {
                        // (1 - prediction) ^ 2
                        if (ID3Tools.TestWithTree(c, current.Trees[0]) != c.AttributeVals[16]) //Incorrect guess
                        {
                            Bias += 1;
                        }
                    }
                    Bias = Bias / (double)TrainBank.Count;
                    output.Append(Bias + ",");
                    AverageTreeBias += Bias;

                    Bias = 0;//Ensemble
                    foreach (Case c in TrainBank)
                    {
                        // (1 - prediction) ^ 2
                        if (current.TestEnsembleClassificaiton(c, attributeBank[16]) != c.AttributeVals[16]) //Incorrect guess
                        {
                            Bias += 1;
                        }
                    }
                    Bias            = Bias / (double)TrainBank.Count;
                    AverageBagBias += Bias;
                    output.Append(Bias + ",");

                    //now variance
                    double Variance = 0;//tree
                    foreach (Case c in TrainBank)
                    {
                        Variance += Math.Pow(ID3Tools.TestWithTree(c, current.Trees[0]) - averageResult, 2); //add target label value
                    }
                    Variance = Variance / (double)(TrainBank.Count);

                    AverageTreeVariance += Variance;
                    output.Append(Variance + ",");


                    Variance = 0;//ensemble
                    foreach (Case c in TrainBank)
                    {
                        Variance += Math.Pow(current.TestEnsembleClassificaiton(c, attributeBank[16]) - averageResult, 2); //add target label value
                    }
                    Variance = Variance / (double)(TrainBank.Count);

                    AverageBagVariance += Variance;
                    output.Append(Variance + "\n");

                    Console.WriteLine("Completed Bias and Variance calculations for Bagged Learner number " + i);
                }

                AverageTreeVariance = AverageTreeVariance / 100;
                AverageTreeBias     = AverageTreeBias / 100;
                AverageBagVariance  = AverageBagVariance / 100;
                AverageBagBias      = AverageBagBias / 100;

                output.Append("FinalVals\n" + AverageTreeBias + "," + AverageBagBias + "," + AverageTreeVariance + "," + AverageBagVariance);
                Console.WriteLine();
                System.IO.File.WriteAllText(TestPath + @"/RunResults/ResultsBankBagAnalysis.csv", output.ToString());
            }

            if (UseRandTrees)
            {
                for (int numAttributes = 2; numAttributes < 7; numAttributes += 2)
                {
                    StringBuilder output  = new StringBuilder();
                    StringBuilder output2 = new StringBuilder();

                    output.Append("T(rees),Training Error,Testing Error\n"); //going to generate a csv file the ensemble learner's performance

                    EnsembleLearner current = null;                          //initialize. Doesn't matter what to
                    for (int i = 1; i < NumIterations; i++)                  //Assignment specifies 1000 iterations
                    {
                        current = EnsembleTools.RandomForest(i, TrainBank.Count, true, RNGseed, numAttributes, TrainBank, attributeBank);

                        double TrainingError = current.TestEnsembleClassMass(TrainBank, attributeBank);
                        double TestingError  = current.TestEnsembleClassMass(TestBank, attributeBank);

                        Console.WriteLine("Built a Random Forest Learner with " + i + " Trees.");

                        output.Append(i + "," + TrainingError + "," + TestingError + "\n"); //write a new line for the CSV file
                    }

                    output2.Append("Tree#,Training Error,Testing Error\n");
                    for (int i = 0; i < NumIterations - 1; i++)
                    {
                        ID3_Node node = current.Trees[i];

                        double TrainingError = ID3Tools.FindTestError(TrainBank, attributeBank, node);
                        double TestingError  = ID3Tools.FindTestError(TestBank, attributeBank, node);

                        int index = i + 1;
                        output2.Append(index + "," + TrainingError + "," + TestingError + "\n"); //write a new line for the CSV file
                    }

                    Console.WriteLine("Writing all results to Ensemble\\ Learning/TestingData/RunResults/ResultsBankRForest.csv");
                    System.IO.File.WriteAllText(TestPath + @"/RunResults/ResultsBankRForest" + numAttributes + ".csv", output.ToString());
                    System.IO.File.WriteAllText(TestPath + @"/RunResults/ResultsBankRForest" + numAttributes + "Trees.csv", output2.ToString());
                }
            }


            if (UseRForestBias)
            {
                Random Gen = new Random(RNGseed);

                double averageResult = 0;
                foreach (Case c in TrainBank)
                {
                    averageResult += c.AttributeVals[16]; //add target label value
                }
                averageResult = averageResult / (double)TrainBank.Count;

                double AverageTreeVariance    = 0;
                double AverageRForestVariance = 0;

                double AverageTreeBias    = 0;
                double AverageRForestBias = 0;

                StringBuilder output = new StringBuilder();
                output.Append("TreeBias,EnsBias,TreeVar,EnsVar\n");

                for (int i = 1; i < 101; i++)
                {
                    List <Case>     Sample  = EnsembleTools.GetRandomSubset(true, 1000, Gen, TrainBank);                       //Generate samples without replacement
                    EnsembleLearner current = EnsembleTools.RandomForest(1000, 1000, true, RNGseed, 4, Sample, attributeBank); //Generate samples allowing duplicates
                    //Calculate bias first

                    double Bias = 0;//tree
                    foreach (Case c in TrainBank)
                    {
                        // (1 - prediction) ^ 2
                        if (ID3Tools.TestWithTree(c, current.Trees[0]) != c.AttributeVals[16]) //Incorrect guess
                        {
                            Bias += 1;
                        }
                    }
                    Bias = Bias / (double)TrainBank.Count;
                    output.Append(Bias + ",");
                    AverageTreeBias += Bias;

                    Bias = 0;//Ensemble
                    foreach (Case c in TrainBank)
                    {
                        // (1 - prediction) ^ 2
                        if (current.TestEnsembleClassificaiton(c, attributeBank[16]) != c.AttributeVals[16]) //Incorrect guess
                        {
                            Bias += 1;
                        }
                    }
                    Bias = Bias / (double)TrainBank.Count;
                    AverageRForestBias += Bias;
                    output.Append(Bias + ",");

                    //now variance
                    double Variance = 0;//tree
                    foreach (Case c in TrainBank)
                    {
                        Variance += Math.Pow(ID3Tools.TestWithTree(c, current.Trees[0]) - averageResult, 2); //add target label value
                    }
                    Variance = Variance / (double)(TrainBank.Count);

                    AverageTreeVariance += Variance;
                    output.Append(Variance + ",");


                    Variance = 0;//ensemble
                    foreach (Case c in TrainBank)
                    {
                        Variance += Math.Pow(current.TestEnsembleClassificaiton(c, attributeBank[16]) - averageResult, 2); //add target label value
                    }
                    Variance = Variance / (double)(TrainBank.Count);

                    AverageRForestVariance += Variance;
                    output.Append(Variance + "\n");

                    Console.WriteLine("Completed Bias and Variance calculations for RForest Learner number " + i);
                }

                AverageTreeVariance    = AverageTreeVariance / 100;
                AverageTreeBias        = AverageTreeBias / 100;
                AverageRForestVariance = AverageRForestVariance / 100;
                AverageRForestBias     = AverageRForestBias / 100;

                output.Append("FinalVals\n" + AverageTreeBias + "," + AverageRForestBias + "," + AverageTreeVariance + "," + AverageRForestVariance);
                Console.WriteLine();
                System.IO.File.WriteAllText(TestPath + @"/RunResults/ResultsBankRForest4Analysis.csv", output.ToString());
            }
        }
Exemple #22
0
        public static void Main()
        {
            //Attributes for the data
            DAttribute[] Attributes = new DAttribute[5];

            Attributes[0] = new DAttribute("Varaince", 1, null, DAttribute.Type.Numeric, false);
            Attributes[1] = new DAttribute("Skew", 1, null, DAttribute.Type.Numeric, false);
            Attributes[2] = new DAttribute("Curtosis", 1, null, DAttribute.Type.Numeric, false);
            Attributes[3] = new DAttribute("Entropy", 1, null, DAttribute.Type.Numeric, false);

            Attributes[4] = new DAttribute("Genuine", 1, new List <String>(new String[] { "0", "1" }), DAttribute.Type.Categorical, false);

            List <Case> TrainBank = DRT.ParseCSV(Attributes, TestPath + @"\bank-note\bank-note\train.csv", false);
            List <Case> TestBank  = DRT.ParseCSV(Attributes, TestPath + @"\bank-note\bank-note\test.csv", false);

            StringBuilder output = new StringBuilder();

            //start testing here


            PerceptronLearner NormalPerceptron = new PerceptronLearner(10, TrainBank, 1, 1500, PerceptronLearner.PType.Normal);

            output.Append("NormalPerceptron \nTrain,Test\n");

            for (int i = 1; i < 12; i++)
            {
                NormalPerceptron.SingleEpoch(); //do an epoch then test it
                double trainError = NormalPerceptron.GetError(TrainBank);
                double testError  = NormalPerceptron.GetError(TestBank);
                Console.WriteLine("Training error Normal Epoch# " + i + " = " + trainError);
                Console.WriteLine("Testing error Normal Epoch# " + i + " = " + testError);

                output.Append(trainError + "," + testError + "\n");
            }

            Console.Write("Final Weight =  {");
            foreach (double d in NormalPerceptron.getWeight())
            {
                Console.Write(d + ", ");
            }

            Console.Write("} with a bias of " + NormalPerceptron.getBias() + ". \n");

            PerceptronLearner VotedPerceptron = new PerceptronLearner(10, TrainBank, 1, 1500, PerceptronLearner.PType.Voted);

            output.Append("\nVotedPerceptron \nTrain,Test\n");
            for (int i = 1; i < 12; i++)
            {
                VotedPerceptron.SingleEpoch();
                double trainError = VotedPerceptron.GetError(TrainBank);
                double testError  = VotedPerceptron.GetError(TestBank);
                Console.WriteLine("Training error Voted Epoch# " + i + " = " + trainError);
                Console.WriteLine("Testing error Voted Epoch# " + i + " = " + testError);
                output.Append(trainError + "," + testError + "\n");
            }

            Console.Write("Final Weight =  {");
            foreach (double d in VotedPerceptron.getWeight())
            {
                Console.Write(d + ", ");
            }

            Console.Write("} with a bias of " + VotedPerceptron.getBias() + ". \n");

            PerceptronLearner AveragedPerceptron = new PerceptronLearner(10, TrainBank, 1, 1500, PerceptronLearner.PType.Averaged);

            output.Append("\nAveragedPerceptron \nTrain,Test\n");
            for (int i = 1; i < 12; i++)
            {
                AveragedPerceptron.SingleEpoch();
                double trainError = AveragedPerceptron.GetError(TrainBank);
                double testError  = AveragedPerceptron.GetError(TestBank);
                Console.WriteLine("Training error Averaged Epoch# " + i + " = " + trainError);
                Console.WriteLine("Testing error Averaged Epoch# " + i + " = " + testError);

                output.Append(trainError + "," + testError + "\n");
            }

            Console.Write("Final Weight =  {");
            foreach (double d  in AveragedPerceptron.getWeight())
            {
                Console.Write(d + ", ");
            }

            Console.Write("} with a bias of " + AveragedPerceptron.getBias() + ". \n");

            PerceptronLearner MarginPerceptron = new PerceptronLearner(10, TrainBank, 1, 1500, PerceptronLearner.PType.Margin, 6);

            output.Append("\nMarginPerceptron \nTrain,Test\n");
            for (int i = 1; i < 12; i++)
            {
                MarginPerceptron.SingleEpoch();
                double trainError = MarginPerceptron.GetError(TrainBank);
                double testError  = MarginPerceptron.GetError(TestBank);
                Console.WriteLine("Training error Margin Epoch# " + i + " = " + trainError);
                Console.WriteLine("Testing error Margin Epoch# " + i + " = " + testError);
                output.Append(trainError + "," + testError + "\n");
            }

            Console.Write("Final Weight =  {");
            foreach (double d in NormalPerceptron.getWeight())
            {
                Console.Write(d + ", ");
            }

            Console.Write("} with a bias of " + NormalPerceptron.getBias() + ". \n");

            Console.WriteLine("\n\n\n\n\n\n Writing all results to TestingData/RunResults/Perceptron.csv");
            System.IO.File.WriteAllText(TestPath + @"/RunResults/Perceptron.csv", output.ToString());

            Console.Read();
        }