コード例 #1
0
        /// <summary>
        /// Thread for training the decisition tree
        /// </summary>
        private void TrainingThread()
        {
            System.Console.WriteLine("Inside the training!!!!!!!!!!!!");
            List <FlowFeature> trainingFeatures;

            //  List<FlowFeature> trainingFeatures;

            //MySqlDao dao = null;


            try
            {
                //dao = new MySqlDao();
                // set data table
                //Flow.SetLabelTable(dao.GetFlowLabels());

                string traceFile = _openTrainingSetDlg.FileName;
                System.Console.WriteLine("the training file is:" + traceFile);
                if (File.Exists(traceFile) == false)
                {
                    throw new FileNotFoundException("Trace file " + traceFile + " doesn't exist.");
                }

                NMParser parser = new NMParser();

                IEnumerable <FlowFeature> allFeatures = GetFlowFeaturesFromTraceFile(parser, traceFile, -1);

                trainingFeatures = allFeatures.ToList();

                List <Attribute> attributes = new List <Attribute>();
                attributes.Add(new NumericalAttribute("PX"));
                attributes.Add(new NumericalAttribute("APL"));
                attributes.Add(new NumericalAttribute("PV"));
                attributes.Add(new NumericalAttribute("DPL"));
                attributes.Add(new NumericalAttribute("PPS"));
                attributes.Add(new IdSymbolicAttribute("Protocol", new List <string>()
                {
                    "TCP", "UDP", "Mixed"
                }));
                attributes.Add(new NumericalAttribute("FPS"));

                attributes.Add(new NumericalAttribute("AB"));
                attributes.Add(new NumericalAttribute("TBT"));
                attributes.Add(new NumericalAttribute("BS"));
                attributes.Add(new NumericalAttribute("PS"));
                attributes.Add(new NumericalAttribute("NNP"));
                attributes.Add(new NumericalAttribute("NSP"));
                attributes.Add(new NumericalAttribute("PSP"));
                attributes.Add(new NumericalAttribute("Duration"));
                attributes.Add(new NumericalAttribute("AIT"));
                attributes.Add(new NumericalAttribute("IOPR"));
                attributes.Add(new NumericalAttribute("Reconnect"));
                attributes.Add(new IdSymbolicAttribute("Type", Flow2.GetFlowTypeNames()));
                //  System.Diagnostics.Debug.WriteLine("TrainingThread1");



                AttributeSet attrSet = new AttributeSet(attributes);

                ItemSet itemSet = new ItemSet(attrSet);
                Dictionary <int, int> maliciousFlowCounter = new Dictionary <int, int>();
                int value;



                foreach (FlowFeature feature in trainingFeatures)
                {
                    List <AttributeValue> attrVals = new List <AttributeValue>();
                    attrVals.Add(new KnownNumericalValue(feature.PX));
                    attrVals.Add(new KnownNumericalValue(feature.APL));
                    attrVals.Add(new KnownNumericalValue(feature.PV));
                    attrVals.Add(new KnownNumericalValue(feature.DPL));
                    attrVals.Add(new KnownNumericalValue(feature.PPS));

                    attrVals.Add(new KnownSymbolicValue((int)feature.Protocol));
                    attrVals.Add(new KnownNumericalValue(feature.FPS));



                    attrVals.Add(new KnownNumericalValue(feature.AB));
                    attrVals.Add(new KnownNumericalValue(feature.TBT));
                    attrVals.Add(new KnownNumericalValue(feature.BS));
                    attrVals.Add(new KnownNumericalValue(feature.PS));
                    attrVals.Add(new KnownNumericalValue(feature.NNP));
                    attrVals.Add(new KnownNumericalValue(feature.NSP));
                    attrVals.Add(new KnownNumericalValue(feature.PSP));
                    attrVals.Add(new KnownNumericalValue(feature.Duration));
                    attrVals.Add(new KnownNumericalValue(feature.AIT));
                    attrVals.Add(new KnownNumericalValue(feature.IOPR));
                    attrVals.Add(new KnownNumericalValue(feature.Reconnect));
                    attrVals.Add(new KnownSymbolicValue(feature.Type));
                    //  System.Diagnostics.Debug.WriteLine("TrainingThread2");
                    //    attrVals.Add(new ((DateTime)feature.DetectionTimeStamp));



                    Item it = new Item(attrVals.ToArray());

                    if (feature.Type > 0)  // if the flow is not normal, count
                    {
                        if (!maliciousFlowCounter.TryGetValue(feature.Type, out value))
                        {
                            maliciousFlowCounter.Add(feature.Type, 1);
                        }
                        else
                        {
                            maliciousFlowCounter[feature.Type]++;
                        }
                    }

                    itemSet.Add(it);
                }


                foreach (int index in maliciousFlowCounter.Keys)
                {
                    System.Diagnostics.Debug.WriteLine("Number of Malicious Flows for type: " + Flow2.GetFlowTypeName(index) + "  is: " + maliciousFlowCounter[index].ToString());
                }



                SymbolicAttribute goalAttribute = attrSet.FindByName("Type") as SymbolicAttribute;

                List <Attribute> testAttributes = new List <Attribute>();

                testAttributes.Add(attrSet.FindByName("PX"));
                testAttributes.Add(attrSet.FindByName("APL"));
                testAttributes.Add(attrSet.FindByName("PV"));
                testAttributes.Add(attrSet.FindByName("DPL"));
                testAttributes.Add(attrSet.FindByName("PPS"));
                testAttributes.Add(attrSet.FindByName("Protocol"));
                testAttributes.Add(attrSet.FindByName("FPS"));
                //    testAttributes.Add(attrSet.FindByName("Type"));
                testAttributes.Add(attrSet.FindByName("AB"));
                testAttributes.Add(attrSet.FindByName("TBT"));
                testAttributes.Add(attrSet.FindByName("BS"));
                testAttributes.Add(attrSet.FindByName("PS"));
                testAttributes.Add(attrSet.FindByName("NNP"));
                testAttributes.Add(attrSet.FindByName("NSP"));
                testAttributes.Add(attrSet.FindByName("PSP"));
                testAttributes.Add(attrSet.FindByName("Duration"));
                testAttributes.Add(attrSet.FindByName("AIT"));
                testAttributes.Add(attrSet.FindByName("IOPR"));
                testAttributes.Add(attrSet.FindByName("Reconnect"));

                //   System.Diagnostics.Debug.WriteLine("TrainingThread3");


                SimpleDecisionTreeBuilder builder = new SimpleDecisionTreeBuilder(    /// create tree hear!
                    new WeightedItemSet(itemSet),
                    new AttributeSet(testAttributes),
                    goalAttribute);

                builder.ScoreThreshold = 0.0001d;  // 0.0001 * itemSet.Size();
                System.Diagnostics.Debug.WriteLine("DT ScoreThreshold is " + builder.ScoreThreshold.ToString());

                LearningDecisionTree dt = builder.Build();

                TestDecisionTree tdt = new TestDecisionTree(dt);

                StoreDecisionTree(tdt);
            }
            catch (ThreadInterruptedException)
            {
                ;
            }
            catch (ThreadAbortException)
            {
                ;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Error");
            }
            finally
            {
                menuTraining.Text = Properties.Resources.StartTrainingText;
            }
        }