Example #1
0
        private void NetAnalyze(object obj)
        {
            OperationContext context = (OperationContext)obj;

            while (true)
            {
                if (netQueue.Count > 0)
                {
                    string[] packets;
                    lock (netQueue)
                    {
                        packets = netQueue.Dequeue();
                    }

                    foreach (string packet in packets)
                    {
                        string[] packetInfoArray = packet.Split('|');
                        double[] input           = Utilities.ParseToNetUnit(packetInfoArray[4], DELIMETER);
                        if (!netClassifier.Classify(input))
                        {
                            context.GetCallbackChannel <IAnalyzerCallback>().GenerateNetWarning(packetInfoArray);
                        }
                    }
                }
                else
                {
                    Thread.Sleep(5000);
                }
            }
        }
Example #2
0
        static void Main(string[] args)
        {
            int ecount = 0, fcount = 0;

            double[,] sampling = Utilities.ReadNetTrainingFile(@"E:\Диплом\WorkingDirectory\training.txt", ref ecount, ref fcount);

            SimpleClassifierNN classifier = new SimpleClassifierNN(sampling, 36, 6000, 10, 1500);

            classifier.Train();
            classifier.SaveNetwork(String.Format(@"E:\Диплом\WorkingDirectory\netSave.txt", Directory.GetCurrentDirectory()));
            int i = 0;

            using (System.IO.StreamReader file = new System.IO.StreamReader(@"E:\Диплом\WorkingDirectory\test1.txt"))
            {
                string line;
                while ((line = file.ReadLine()) != null && line != "")
                {
                    string[] allNetEntry = line.Split('|');
                    double[] input       = Utilities.ParseToNetUnit(allNetEntry[4], ';');
                    string   desc        = "";
                    Console.WriteLine(String.Format("Result{0}: {1}", i, classifier.Classify(input).ToString()));
                    i++;
                }
            }

            string infile   = @"E:\Диплом\WorkingDirectory\SecurityTraining.txt";
            string outfile  = @"E:\Диплом\WorkingDirectory\logOutput.txt";
            string testfile = @"E:\Диплом\WorkingDirectory\logTest.txt";
            string saveFile = @"E:\Диплом\WorkingDirectory\logSave.txt";

            LogClassifier logcl = new LogClassifier(saveFile, infile);
            var           test  = Utilities.ReadHostClassifyFile(testfile);

            logcl.SaveClassifier(saveFile);
            CheckHostPackets(test[0], logcl);
        }
Example #3
0
        public double[] CreateNewNN(string trainingFileName, string testFileName, string goal, int epochCount, int neuronCountInHiddenLayer)
        {
            List <double> results = new List <double>();
            int           trainingSamples = 0, countFeatures = 0, testSamples = 0, testGood = 0, testBad = 0, TP = 0, TN = 0, FP = 0, FN = 0;

            temporaryGoal = goal;

            if (goal == "NET")
            {
                temporaryNetClassifier = null;
                var trainingData = Utilities.ReadNetTrainingFile(trainingFileName, ref trainingSamples, ref countFeatures);

                if (trainingData == null)
                {
                    return(null);
                }

                temporaryNetClassifier = new SimpleClassifierNN(trainingData, countFeatures, trainingSamples, neuronCountInHiddenLayer, epochCount);
                var trainingResult = temporaryNetClassifier.Train();
                var testData       = Utilities.ReadNetTestFile(testFileName, ref testSamples);

                foreach (var unitTest in testData)
                {
                    bool classifyResult = temporaryNetClassifier.Classify(unitTest.Item1);

                    if (unitTest.Item2)
                    {
                        testGood++;

                        if (unitTest.Item2 != classifyResult)
                        {
                            FN++;
                        }
                        else
                        {
                            TP++;
                        }
                    }
                    else
                    {
                        testBad++;

                        if (unitTest.Item2 != classifyResult)
                        {
                            FP++;
                        }
                        else
                        {
                            TN++;
                        }
                    }
                }
            }

            if (goal == "HOST")
            {
                temporaryHostClassifier = null;
                temporaryHostClassifier = new LogClassifier(trainingFileName, neuronCountInHiddenLayer, epochCount);
                var testData = Utilities.ReadHostFile(testFileName, ref testSamples);

                for (int i = 0; i < testData.Item1.Length; i++)
                {
                    bool classifyResult = temporaryHostClassifier.TestAnalyze(testData.Item1[i]);

                    if (testData.Item2[i])
                    {
                        testGood++;

                        if (testData.Item2[i] != classifyResult)
                        {
                            FN++;
                        }
                        else
                        {
                            TP++;
                        }
                    }
                    else
                    {
                        testBad++;

                        if (testData.Item2[i] != classifyResult)
                        {
                            FP++;
                        }
                        else
                        {
                            TN++;
                        }
                    }
                }
            }

            double precision = (double)TP / (double)(TP + FP);
            double recall = (double)TP / (double)(TP + FN);
            double accuracy = 2 * ((double)(precision * recall) / (double)(precision + recall));
            double firstMistake  = (double)FP / (double)testSamples;
            double secondMistake = (double)FN / (double)testSamples;

            results.Add(trainingSamples);
            results.Add(testGood);
            results.Add(testBad);
            results.Add(testSamples);
            results.Add(epochCount);
            results.Add(accuracy);
            results.Add(firstMistake);
            results.Add(secondMistake);

            return(results.ToArray());
        }