Beispiel #1
0
 private void addParameterToIdentificationParameter(ParameterSelection parameterSelection)
 {
     _identificationParameter.AddLinkedParameter(parameterSelection);
     ParameterLinkedToIdentificationParameter(this, new ParameterInIdentificationParameterEventArgs(_identificationParameter, parameterSelection));
 }
 public IdentificationParameter CreateFor(ParameterSelection parameterSelection, ParameterIdentification parameterIdentification)
 {
     return(CreateFor(new[] { parameterSelection }, parameterIdentification));
 }
 protected override void Context()
 {
     base.Context();
     sut.ParameterLinkedToIdentificationParameter += (o, e) => { _newLinkedParameter = e.LinkedParameter; };
 }
Beispiel #4
0
        public void svmproject(Dictionary <int, Characteristic> Characteristics)
        {
            Dictionary <int, Characteristic> _characteristics = new Dictionary <int, Characteristic>();

            _characteristics = Characteristics;
            Model          model;
            RangeTransform range;
            double         C;
            double         gamma;
            // default values
            Parameter parameters = new Parameter();
            //parameters.SvmType = SvmType.C_SVC;
            //parameters.KernelType = KernelType.RBF;
            //parameters.Degree = 3;
            //parameters.Gamma = 0;
            //parameters.Coefficient0 = 0;
            //parameters.Nu = 0.5;
            //parameters.CacheSize = 40;
            //parameters.C = 1000;
            //parameters.EPS = 1e-3;
            //parameters.P = 0.1;
            //parameters.Shrinking = true;
            //parameters.WeightCount = 0;
            //parameters.WeightLabels = new int[0];
            //parameters.Weights = new double[0];
            //parameters.C = 5;
            //parameters.Gamma = 1;
            string str = null;

            for (int i = 1; i < _characteristics.Count(); i++)
            {
                if (_characteristics[i].IsQualifiedColony == false && _characteristics[i].IsInvalidColony == true)
                {
                    str = str + "0";
                    str = str + " 1:" + _characteristics[i].Area.ToString() + " 2:" + _characteristics[i].MajToMinAxisRatio.ToString() +
                          " 3:" + _characteristics[i].CentreAcerageColor.R.ToString() + " 4:" + _characteristics[i].CentreAcerageColor.G.ToString()
                          + " 5:" + _characteristics[i].CentreAcerageColor.B.ToString() + "\r\n";
                }
                if (_characteristics[i].IsQualifiedColony == true && _characteristics[i].IsInvalidColony == false)
                {
                    str = str + "1";
                    str = str + " 1:" + _characteristics[i].Area.ToString() + " 2:" + _characteristics[i].MajToMinAxisRatio.ToString() +
                          " 3:" + _characteristics[i].CentreAcerageColor.R.ToString() + " 4:" + _characteristics[i].CentreAcerageColor.G.ToString()
                          + " 5:" + _characteristics[i].CentreAcerageColor.B.ToString() + "\r\n";
                }
            }
            if (str != null)
            {
                byte[]       array  = Encoding.ASCII.GetBytes(str);
                MemoryStream stream = new MemoryStream(array);             //convert stream 2 string
                Problem      train  = new Problem();
                train = Problem.Read(stream);
                range = Scaling.DetermineRange(train);
                train = Scaling.Scale(train, range);
                //String outfile001="D:\\parameters.txt";
                ParameterSelection.Grid(train, parameters, @"D:\\parameters.txt", out C, out gamma);
                parameters.C     = C;
                parameters.Gamma = gamma;
                model            = Training.Train(train, parameters);
                //MessageBox.Show("学习完毕");
                //stream.Dispose();
                stream.Close();
            }
            else
            {
                MessageBox.Show("无学习数据");
                model = null;
                range = null;
            }

            string str1 = null;

            for (int i = 1; i < _characteristics.Count(); i++)
            {
                str1 = str1 + "0";
                str1 = str1 + " 1:" + _characteristics[i].Area.ToString() + " 2:" + _characteristics[i].MajToMinAxisRatio.ToString() +
                       " 3:" + _characteristics[i].CentreAcerageColor.R.ToString() + " 4:" + _characteristics[i].CentreAcerageColor.G.ToString()
                       + " 5:" + _characteristics[i].CentreAcerageColor.B.ToString() + "\r\n";
            }
            if (str1 != null)
            {
                byte[]       array  = Encoding.ASCII.GetBytes(str1);
                MemoryStream stream = new MemoryStream(array);             //convert stream 2 string
                Problem      pre    = new Problem();
                pre = Problem.Read(stream);
                pre = Scaling.Scale(pre, range);
                Prediction.Predict(pre, @"D:\result.txt", model, false);
                MessageBox.Show("筛选完毕");
                //stream.Dispose();
                stream.Close();
            }
            else
            {
                MessageBox.Show("无筛选数据");
            }
            //svm_problem prob = new svm_problem();
            //prob.l = point_list.Count;
            //prob.y = new double[prob.l];
            //            if(param.svm_type == svm_parameter.EPSILON_SVR ||
            //    param.svm_type == svm_parameter.NU_SVR)
            //{
            //    if(param.gamma == 0) param.gamma = 1;
            //    prob.x = new svm_node[prob.l][];
            //    for(int i=0;i<prob.l;i++)
            //    {
            //        point p = (point)point_list[i];
            //        prob.x[i][0] = new svm_node();
            //        prob.x[i][0].index = 1;
            //        prob.x[i][0].value_Renamed = p.x;
            //        prob.y[i] = p.y;
            //    }
            //    svm_model model = svm.svm_train(prob, param);
            //    svm_node[] x = new svm_node[1];
            //    x[0] = new svm_node();
            //    x[0].index = 1;
            //    int[] j = new int[XLEN];
            //C = Convert.ToInt16(numericUpDown8.Value);
            //gamma = Convert.ToInt16(numericUpDown9.Value);
            //StudyAlgorithm study = new StudyAlgorithm();
            //study.GetModel(AllColony, C, gamma, out model, out range);
            //ScreenAlgorithm screenAlgorithm = new ScreenAlgorithm();
            //screenAlgorithm.ScreenTheColony(CharacteristicsValue, model, range);
        }
Beispiel #5
0
        protected override void Context()
        {
            _modelCoreSimulationMapper   = A.Fake <ISimulationToModelCoreSimulationMapper>();
            _residualCalculatorFactory   = A.Fake <IResidualCalculatorFactory>();
            _timeGridUpdater             = A.Fake <ITimeGridUpdater>();
            _simModelBatchFactory        = A.Fake <ISimModelBatchFactory>();
            _optimizationAlgorithmMapper = A.Fake <IParameterIdentificationAlgorithmToOptmizationAlgorithmMapper>();
            _outputSelectionUpdater      = A.Fake <IOutputSelectionUpdater>();
            _coreUserSettings            = A.Fake <ICoreUserSettings>();
            _jacobianMatrixCalculator    = A.Fake <IJacobianMatrixCalculator>();

            _coreUserSettings.MaximumNumberOfCoresToUse = 2;
            sut = new ParameterIdentificationRun(_residualCalculatorFactory, _timeGridUpdater, _simModelBatchFactory, _modelCoreSimulationMapper,
                                                 _optimizationAlgorithmMapper, _outputSelectionUpdater, _coreUserSettings, _jacobianMatrixCalculator);

            _simulation           = A.Fake <ISimulation>();
            _parameter1           = A.Fake <IParameter>();
            _parameter1.Dimension = DomainHelperForSpecs.ConcentrationDimensionForSpecs();
            _parameter1.Value     = 15;
            _parameter2           = A.Fake <IParameter>();
            _parameter2.Value     = 35;
            _parameter2.Dimension = DomainHelperForSpecs.ConcentrationDimensionForSpecs();

            _parameterIdentification = new ParameterIdentification();
            _parameterIdentification.Configuration.LLOQMode       = LLOQModes.OnlyObservedData;
            _parameterIdentification.Configuration.RemoveLLOQMode = RemoveLLOQModes.NoTrailing;

            _parameterIdentification.AddSimulation(_simulation);

            _parameterSelection1 = ParameterSelectionFor(_parameter1, "ParameterPath1");
            _parameterSelection2 = ParameterSelectionFor(_parameter2, "ParameterPath2");

            _identificationParameter = DomainHelperForSpecs.IdentificationParameter("IdParam", min: 10, max: 20, startValue: 15);

            _identificationParameter.AddLinkedParameter(_parameterSelection1);
            _identificationParameter.AddLinkedParameter(_parameterSelection2);

            _modelCoreSimulation = A.Fake <IModelCoreSimulation>();

            A.CallTo(() => _modelCoreSimulationMapper.MapFrom(_simulation, true)).Returns(_modelCoreSimulation);

            _outputMapping = A.Fake <OutputMapping>();
            A.CallTo(() => _outputMapping.UsesSimulation(_simulation)).Returns(true);
            A.CallTo(() => _outputMapping.WeightedObservedData.ObservedData).Returns(DomainHelperForSpecs.ObservedData());
            _parameterIdentification.AddOutputMapping(_outputMapping);

            _simModelBatch = A.Fake <ISimModelBatch>();
            A.CallTo(() => _simModelBatchFactory.Create()).Returns(_simModelBatch);

            _parameterIdentification.AddIdentificationParameter(_identificationParameter);

            _residualCalculator = A.Fake <IResidualCalculator>();
            A.CallTo(_residualCalculatorFactory).WithReturnType <IResidualCalculator>().Returns(_residualCalculator);

            _algorithm = A.Fake <IOptimizationAlgorithm>();
            A.CallTo(() => _optimizationAlgorithmMapper.MapFrom(_parameterIdentification.AlgorithmProperties)).Returns(_algorithm);

            _cancellationTokenSource = new CancellationTokenSource();
            _cancellationToken       = _cancellationTokenSource.Token;

            _runInitializer = A.Fake <IParameterIdentifcationRunInitializer>();
            A.CallTo(() => _runInitializer.InitializeRun()).ReturnsAsync(_parameterIdentification);

            PerformExtraInitializationSteps();
            sut.InitializeWith(_runInitializer);
        }
Beispiel #6
0
 public virtual bool Analyzes(ParameterSelection parameterSelection)
 {
     return(Equals(ParameterSelection, parameterSelection));
 }
        private static bool isNonFormulaParameter(ParameterSelection parameterSelection)
        {
            var parameter = parameterSelection.Parameter;

            return(parameter.IsFixedValue || !parameter.Formula.IsExplicit());
        }
Beispiel #8
0
        static void Main(string[] args)
        {
            PDSClass           pds = new PDSClass();
            FireFlyAlgorithm   ff  = new FireFlyAlgorithm();
            ParameterSelection ps  = new ParameterSelection();
            //reading the phishing emails and the test Datas
            string testFileFolderName = "TrainTestData";
            string trainTestDataPath  = String.Format(Environment.CurrentDirectory + "\\{0}", testFileFolderName); //filepath for the training and test dataset

            string[] trainTestFileURL = System.IO.Directory.GetFileSystemEntries(trainTestDataPath);

            string extractedVectorsFolderName = "ExtractedFeatures";
            string extractedVectorsFilePath   = String.Format(Environment.CurrentDirectory + "\\{0}", extractedVectorsFolderName);

            string[] evFileURL = System.IO.Directory.GetFileSystemEntries(extractedVectorsFilePath);

            string outputEvaluationFilePath = String.Format(Environment.CurrentDirectory + "\\{0}", "OutputEvaluations.txt");

            //string SVAccuracyFilePath = String.Format(Environment.CurrentDirectory + "\\{0}", "SVAccuracy.txt");

            File.WriteAllText(ps.SVAccuracyFilePath, string.Empty);
            File.WriteAllText(outputEvaluationFilePath, string.Empty); //deleting the contents of the file holding the results for new results
            File.WriteAllText(ps.filePath, string.Empty);              //deleting the contents of the file holding the extracted C,Gamma and CV values
            File.WriteAllText(pds.extractedFeaturesFilePathTrainDS, string.Empty);
            File.WriteAllText(ps.filePath2, string.Empty);
            File.WriteAllText(ps.filePath3, string.Empty);

            int    NumofFeatures  = 17;
            double DatasetEntropy = 0.0;
            int    TotalMails;

            string[] features = pds.Features(); //saving the features in the string array

            int[,] HamPhishCount = new int[, ] {
            };                                     //count for the total number of ham and phish emails

            string[] testTrainFolderFiles = new string[] { };
            string   folderFiles;

            double[] informationGain = new double[NumofFeatures];
            Dictionary <string, double> feat_infoGain = pds.Feature_InfoGain(features, informationGain); //saving the information gain for all the individual features

            double   classficatnAccuracySum = 0.0;                                                       //cummulative classification accuracy
            double   classficatnAccuracy    = 0;                                                         //classification accuracy for each iteration
            DateTime start = DateTime.Now;
            double   totalPhishing = 0, totalHam = 0; int mailGrandTotal = 0;

            //initializing all the variables that will used for evaluating the performance of the classifier
            double FP = 0.0, FN = 0.0, TP = 0.0, TN = 0.0, P = 0.0, F_M = 0.0, sumFP = 0.0, sumF_M = 0.0, sumFN = 0.0, sumTP = 0.0, sumTN = 0.0, sumP = 0.0;

            int N_Fold = 10; //number of folds
            int n_Runs = 1;  //number of runs

            double[,] NormalizedVector = new double[, ] {
            };                                              //normalized vector values for each features

            Program p = new Program();
            double  avgRuns = 0.0, avgFP = 0.0, avgFN = 0.0, avgR = 0.0, avgPr = 0.0, avgFM = 0.0; //avg=> average
            double  globalBest = double.MinValue;

            //change the boolean value appropriately to choose the task you want to perform (either vector value extraction or email classification)
            //both values should not be true. This is to reduce processing time
            bool extractVectorValues = true;
            bool Emailclassification = false;

            double C     = new double();
            double Gamma = new double();

            //double[] CV = new double[2];
            List <double> CV1 = new List <double>(); //Save the CV accuracy, C and Gamma for comparison
            List <double> CV2 = new List <double>(); //Save the CV accuracy, C and Gamma for comparison

            for (int aa = 0; aa < n_Runs; aa++)
            {
                classficatnAccuracySum = 0.0;
                sumFP  = 0.0;
                sumTP  = 0.0; //Recall
                sumFN  = 0.0;
                sumF_M = 0.0;
                sumP   = 0.0;
                for (int a = 0; a < N_Fold; a++)
                {
                    if (extractVectorValues == true)                                               //if the value of ExtractVectorValues is true, only extract email vector values and dont perform classification
                    {
                        n_Runs = 1;                                                                // change number of runs from its default value (i.e 10) to 1 (to avoid repeating the extraction process 10 times) since we wanna do is to extract the vector values from each emails
                        string[] trainFileURLs = new string[] { };                                 //urls for the train emails (i.e. the emails used for training the classifier)
                        string[] testFileURLs  = new string[] { };                                 //urls for the test emails (i.e. the emails used for testing the classifier)
                        Dictionary <string, int> trainMail_Class = new Dictionary <string, int>(); //variable containing the emails and classes of all the training emails
                        Dictionary <string, int> testMail_Class  = new Dictionary <string, int>(); //variable containing the emails and classes of all the test emails
                        string[] trainMailFileNames = new string[trainMail_Class.Count];           //the file names for all the emails in the training dataset
                        string[] testMailFileNames = new string[] { };                             //the file names for all the emails in the test dataset
                        string   trainMailLabel; int phishCount = 0, hamCount = 0; double phishPercentage, hamPercentage;

                        //processing the training dataset for the each fold
                        for (int i = 0; i < trainTestFileURL.Length; i++)
                        {
                            if (i.Equals(a))
                            {
                                continue;                                                                               //skipping one email folder, which is to be used for testing the trained classifier (i.e. the current test dataset)
                            }
                            testTrainFolderFiles = System.IO.Directory.GetFiles(trainTestFileURL[i]);                   //getting the filenames of all the emails in the training dataset
                            trainFileURLs        = trainFileURLs.Concat(testTrainFolderFiles).ToArray();                //get all the urls for the test emails
                            trainMailFileNames   = pds.getFileNames(trainFileURLs);                                     //get the file names for all the test mails
                            for (int j = 0; j < testTrainFolderFiles.Length; j++)                                       //processing all the emails in the current training dataset for classification
                            {
                                trainMailLabel = trainMailFileNames[j].Substring(0, 2);                                 //getting the label for each email, HM(for Ham Mails) and PM(for Phish Mails)
                                folderFiles    = File.ReadAllText(testTrainFolderFiles[j]);                             //extracting the content of each email in each email folder
                                trainMail_Class[pds.ProcessMails(folderFiles)] = (trainMailLabel.Equals("PM")) ? 1 : 0; //processing each email and assigning label to the emails based on the folders each emails come from.
                                if (trainMail_Class.ElementAt(j).Value == 1)
                                {
                                    phishCount++; //counting the total number of ham and phishing to get their percentage
                                }
                                else
                                {
                                    hamCount++;
                                }
                            }
                        }

                        //processing the test dataset for each fold
                        for (int i = a; i < a + 1; i++)
                        {
                            testTrainFolderFiles = System.IO.Directory.GetFiles(trainTestFileURL[i]);
                            testFileURLs         = testFileURLs.Concat(testTrainFolderFiles).ToArray();
                            testMailFileNames    = pds.getFileNames(testFileURLs);
                            for (int j = 0; j < testTrainFolderFiles.Length; j++)
                            {
                                trainMailLabel = testMailFileNames[j].Substring(0, 2);
                                folderFiles    = File.ReadAllText(testTrainFolderFiles[j]);
                                testMail_Class[pds.ProcessMails(folderFiles)] = (trainMailLabel.Equals("PM")) ? 1 : 0; //processing each email and assigning label to the emails based on the folders each emails come from.
                                if (testMail_Class.ElementAt(j).Value == 1)
                                {
                                    phishCount++;
                                }
                                else
                                {
                                    hamCount++;
                                }
                            }
                        }

                        //calculating the percentage of phishing and ham email in the dataset
                        phishPercentage = (double)phishCount / (double)(trainMail_Class.Count + testMail_Class.Count);
                        hamPercentage   = (double)hamCount / (double)(trainMail_Class.Count + testMail_Class.Count);
                        mailGrandTotal  = phishCount + hamCount;
                        totalHam        = hamCount; totalPhishing = phishCount;

                        //Information Gain
                        TotalMails    = trainMail_Class.Count;
                        int[,] vector = new int[TotalMails, NumofFeatures];
                        pds.processVector(vector, trainMail_Class, features, trainFileURLs, NumofFeatures);                //extracting the vector values of all the features
                        HamPhishCount = new int[NumofFeatures, 4];
                        pds.FeatureVectorSum(NumofFeatures, TotalMails, vector, trainMail_Class, HamPhishCount);           // calculating the total number of zeros and ones for both phishing and ham emails
                        DatasetEntropy = pds.Entropy(trainMail_Class);                                                     //calculating the entropy for the entire dataset
                        pds.CalInformationGain(NumofFeatures, HamPhishCount, informationGain, TotalMails, DatasetEntropy); //calculating information gain for each feature
                        feat_infoGain = pds.Feature_InfoGain(features, informationGain);                                   //assisgning the calculated information gain to each feature

                        //process vector for training Dataset
                        int      NumofFeatures2 = NumofFeatures - 8;
                        string[] newFeatures    = new string[NumofFeatures2];
                        for (int i = 0; i < NumofFeatures2; i++)
                        {
                            newFeatures[i] = feat_infoGain.ElementAt(i).Key; //copying the best 8 features with the highest information gain
                        }

                        TotalMails = trainMail_Class.Count;
                        vector     = new int[TotalMails, NumofFeatures2];
                        pds.processVector(vector, trainMail_Class, newFeatures, trainFileURLs, NumofFeatures2);
                        NormalizedVector = ff.Normalize(vector); //normalize the all vector values for train data

                        //extract vectors of the training data
                        pds.extractVectors(vector, trainMail_Class, NumofFeatures2, "trainingDS", a);

                        //process vector for testing Dataset
                        TotalMails = testMail_Class.Count;
                        vector     = new int[TotalMails, NumofFeatures2];
                        pds.processVector(vector, testMail_Class, newFeatures, testFileURLs, NumofFeatures2);
                        NormalizedVector = ff.Normalize(vector); //normalize the all vector values for test data

                        //extract vectors of the test data
                        pds.extractVectors(vector, testMail_Class, NumofFeatures2, "testDS", a);
                    }

                    if (Emailclassification == true) //if the value of EmailClassification is true, perform email classification and dont extract emails
                    {
                        //SVM Classfication begins here
                        //First, read in the training and test data.
                        Problem train = Problem.Read(string.Format("ExtractedFeaturesTrain{0}.{1}", (a + 1).ToString(), "txt"));
                        Problem test  = Problem.Read(string.Format("ExtractedFeaturesTest{0}.{1}", (a + 1).ToString(), "txt"));

                        //scalling the data
                        GaussianTransform gt = GaussianTransform.Compute(train);
                        Problem           trainScaledProblem = gt.Scale(train);
                        Problem           testScaledProblem  = gt.Scale(test);

                        //For this example (and indeed, many scenarios), the default parameters will suffice.
                        Parameter parameters = new Parameter();
                        //double C = new double();
                        //double Gamma = new double();

                        Console.WriteLine("\nClassification Number {0} Step: {1}...............................\n", a + 1, aa + 1);

                        //This will do a grid optimization to find the best parameters and store them in C and Gamma, outputting the entire
                        //search to params.txt.

                        /*
                         * if (a == 0)
                         * {
                         *   ParameterSelection.Grid(trainScaledProblem, parameters, "params.txt", out C, out Gamma);
                         *   CV1.Add(ParameterSelection.CVAccuracy);
                         *   CV1.Add(C);
                         *   CV1.Add(Gamma);
                         * }
                         * else if (a == 1)
                         * {
                         *   ParameterSelection.Grid(trainScaledProblem, parameters, "params.txt", out C, out Gamma);
                         *   CV2.Add(ParameterSelection.CVAccuracy);
                         *   CV2.Add(C);
                         *   CV2.Add(Gamma);
                         *
                         *   if (CV1[0] > CV2[0]) //if the previous CV rate is greater than the present, then, discard the present and use the C and Gamma of the previous.
                         *   {
                         *       C = CV1[1];
                         *       Gamma = CV1[2];
                         *   }
                         *
                         * }*/

                        //Bootstrap Sampling Method
                        //Training.samplingGellingPoint(trainScaledProblem, testScaledProblem);
                        //int subsetNumber = 5;
                        //int samplesPerSubset = 30;
                        //Problem subsets = new Problem();
                        //Parameter bestPara = new Parameter();
                        //subsets = Training.BootstrapSampling(trainScaledProblem, parameters, subsetNumber, samplesPerSubset, testScaledProblem, out bestPara); //select subsets using boothtrap sampling method

                        //parameters.C = C;
                        //parameters.Gamma = Gamma;



                        //KNN-Based boundary instance Selection
                        int     k = 50;
                        int     numberOfSubset = 300; //subset to select for training
                        Problem dataSubset     = Training.computeNearestNeighbour(k, trainScaledProblem, numberOfSubset);
                        ParameterSelection.Grid(dataSubset, parameters, "params.txt", out C, out Gamma);
                        parameters.C     = C;
                        parameters.Gamma = Gamma;
                        //Model model = Training.buildModel(dataSubset, parameters);
                        Model model = Training.Train(dataSubset, parameters);


                        //ParameterSelection.Grid(boundaryInstance, parameters, "params.txt", out C, out Gamma);
                        //ParameterSelection.Grid(trainScaledProblem, parameters, "params.txt", out C, out Gamma);

                        /* //FFA_Based Instance Selection
                         * FireflyInstanceSelection fi = new FireflyInstanceSelection();
                         * Problem subP = fi.firefly_simple(trainScaledProblem);
                         * ParameterSelection.Grid(subP, parameters, "params.txt", out C, out Gamma);
                         * parameters.C = C;
                         * parameters.Gamma = Gamma;
                         * Model model = Training.Train(subP, parameters);
                         */

                        /*
                         * //Clustering-Based Instance Selection Algorithm
                         * Problem boundaryInstance = Training.ClusteringBoundaryInstance(trainScaledProblem);
                         * ParameterSelection.Grid(boundaryInstance, parameters, "params.txt", out C, out Gamma);
                         * parameters.C = C;
                         * parameters.Gamma = Gamma;
                         * Model model = Training.Train(boundaryInstance, parameters);
                         */

                        /* //Edge Instance Selection
                         * Problem edgeNN = Training.EdgeInstanceSelection(trainScaledProblem);
                         * ParameterSelection.Grid(edgeNN, parameters, "params.txt", out C, out Gamma);
                         * parameters.C = C;
                         * parameters.Gamma = Gamma;
                         * Model model = Training.Train(edgeNN, parameters);
                         */

                        /*
                         * //Hybrid: KNN-based based + FFA-Based
                         * //Problem boundaryInstance = Training.ClusteringBoundaryInstance(trainScaledProblem);
                         * FireflyInstanceSelection fi = new FireflyInstanceSelection();
                         * Problem subP = fi.firefly_simple(trainScaledProblem);
                         * int k = 50;
                         * int numberOfSubset = 100; //subset to select for training
                         * Problem dataSubset = Training.computeNearestNeighbour(k, subP, numberOfSubset);
                         * ParameterSelection.Grid(dataSubset, parameters, "params.txt", out C, out Gamma);
                         * parameters.C = C;
                         * parameters.Gamma = Gamma;
                         * Model model = Training.Train(subP, parameters);
                         */

                        /*
                         * //Hybrid: Clustering-Based + FFA-Based
                         * Problem boundaryInstance = Training.ClusteringBoundaryInstance(trainScaledProblem);
                         * FireflyInstanceSelection fi = new FireflyInstanceSelection();
                         * Problem subP = fi.firefly_simple(boundaryInstance);
                         * ParameterSelection.Grid(subP, parameters, "params.txt", out C, out Gamma);
                         * parameters.C = C;
                         * parameters.Gamma = Gamma;
                         * Model model = Training.Train(boundaryInstance, parameters);
                         */

                        /* //Hybrid: Clustering based + FFA-Based + KNN-Based
                         * Problem boundaryInstance = Training.ClusteringBoundaryInstance(trainScaledProblem);
                         * FireflyInstanceSelection fi = new FireflyInstanceSelection();
                         * Problem subP = fi.firefly_simple(boundaryInstance);
                         * int k = 50;
                         * int numberOfSubset = 100; //subset to select for training
                         * Problem dataSubset = Training.computeNearestNeighbour(k, boundaryInstance, numberOfSubset);
                         * ParameterSelection.Grid(dataSubset, parameters, "params.txt", out C, out Gamma);
                         * parameters.C = C;
                         * parameters.Gamma = Gamma;
                         * Model model = Training.Train(dataSubset, parameters);
                         */
                        //Train the model using the optimal parameters.
                        //Model model = Training.Train(trainScaledProblem, parameters);
                        //removing support vectors that contributes less to the decision surface
                        //Model submod = Training.performSupportVectorReduction(model, trainScaledProblem);

                        //Perform classification on the test data, putting the results in results.txt.
                        //classficatnAccuracySum += Prediction.Predict(testScaledProblem, "ClassificationResults.txt", model, false);
                        classficatnAccuracy     = Prediction.Predict(testScaledProblem, "ClassificationResults.txt", model, false); //classfication accuracy for each iteration ->for the purpose of outputting to the text file
                        classficatnAccuracySum += classficatnAccuracy;
                        Console.WriteLine("\nClassification Accuracy: {0}%", 100 * classficatnAccuracy);

                        PerformanceEvaluator pp = new PerformanceEvaluator("ClassificationResults.txt", test, out TP, out TN, out FP, out FN, out P, out F_M);

                        sumTP += TP; sumTN += TN; sumFP += FP; sumFN += FN; sumP += P; sumF_M += F_M;

                        //saving all the output to a file
                        string outpt = string.Format("Cross Validation: {0}, Run number {1}, CAccuracy: {2:0.0000} FP: {3:0.0000}, FN: {4:0.0000}, Recall: {5:0.0000}, Precision: {6:0.0000}, FMeasure: {7:0.0000}", a + 1, aa + 1, (classficatnAccuracy * 100), (FP * 100), (FN * 100), (TP * 100), (P * 100), (F_M * 100));
                        File.AppendAllText(outputEvaluationFilePath, outpt);
                        File.AppendAllText(outputEvaluationFilePath, Environment.NewLine);
                    }

                    if (classficatnAccuracy * 100 > globalBest)
                    {
                        globalBest = classficatnAccuracy * 100;
                    }
                }

                classficatnAccuracySum = (classficatnAccuracySum * 100) / N_Fold; //converting to percentage and dividing by the number of folds
                sumFP  = (sumFP * 100) / N_Fold;                                  //calculating the average cross validation for False Positive over 10 folds
                sumTP  = (sumTP * 100) / N_Fold;                                  //calculating the average cross validation for Recall over 10 folds
                sumFN  = (sumFN * 100) / N_Fold;                                  //calculating the average cross validation for False Negative over 10 folds
                sumF_M = (sumF_M * 100) / N_Fold;                                 //calculating the average cross validation for F Measure over 10 folds
                sumP   = (sumP * 100) / N_Fold;                                   //calculating the average cross validation for Precision over 10 folds


                avgRuns += classficatnAccuracySum;
                avgFP   += sumFP;
                avgFN   += sumFN;
                avgR    += sumTP;
                avgPr   += sumP;
                avgFM   += sumF_M;

                //saving all the outputs to a file
                File.AppendAllText(outputEvaluationFilePath, Environment.NewLine);
                File.AppendAllText(outputEvaluationFilePath, string.Format("Average Calculations....Run Number: {0}", aa + 1));
                File.AppendAllText(outputEvaluationFilePath, Environment.NewLine);
                string outpt2 = string.Format("Run number {0}, Average CAccuracy: {1:0.0000} FP: {2:0.0000}, FN: {3:0.0000}, Recall: {4:0.0000}, Precision: {5:0.0000}, FMeasure: {6:0.0000}", aa + 1, classficatnAccuracySum, sumFP, sumFN, sumTP, sumP, sumF_M);
                File.AppendAllText(outputEvaluationFilePath, outpt2);
                File.AppendAllText(outputEvaluationFilePath, Environment.NewLine);
                File.AppendAllText(outputEvaluationFilePath, Environment.NewLine);

                Console.WriteLine("\nStep {0}...............................\n", aa + 1);
            }

            DateTime end      = DateTime.Now;
            TimeSpan duration = end - start;
            double   time     = duration.Minutes * 60.0 + duration.Seconds + duration.Milliseconds / 1000.0;

            Console.WriteLine("\nTotal processing time {0:########.00} seconds\n", time);

            File.AppendAllText(outputEvaluationFilePath, Environment.NewLine);
            File.AppendAllText(outputEvaluationFilePath, "Total processing time:\n" + time + " Seconds");
            File.AppendAllText(outputEvaluationFilePath, Environment.NewLine);

            //sending all the outputs to the screen
            Console.WriteLine("\nOverall Average Accuracy: {0:0.00}% \nGlobal Best: {1:0.00}%", avgRuns / n_Runs, globalBest);
            Console.WriteLine("\n\nTotal False Positive: {0:0.00}%\nTotal False Negative: {1:0.00}%\nRecall: {2:0.00}%\nPrecision: {3:0.00}%\nF_Measure: {4:0.00}%", (avgFP / n_Runs), (avgFN / n_Runs), (avgR / n_Runs), (avgPr / n_Runs), (avgFM / n_Runs));

            File.AppendAllText(outputEvaluationFilePath, Environment.NewLine);
            File.AppendAllText(outputEvaluationFilePath, "Overall Average Calculations.......");
            File.AppendAllText(outputEvaluationFilePath, Environment.NewLine);
            File.AppendAllText(outputEvaluationFilePath, Environment.NewLine);
            string outpt3 = string.Format("Overall Average CAccuracy: {0:0.0000} FP: {1:0.0000}, FN: {2:0.0000}, Recall: {3:0.0000}, Precision: {4:0.0000}, FMeasure: {5:0.0000}", avgRuns / n_Runs, avgFP / n_Runs, avgFN / n_Runs, avgR / n_Runs, avgPr / n_Runs, avgFM / n_Runs);

            File.AppendAllText(outputEvaluationFilePath, outpt3);

            Console.ReadKey();
        }
 private static bool isConstantParameter(ParameterSelection parameterSelection) => parameterSelection.Parameter.IsConstantParameter();
Beispiel #10
0
 public double OptimizedParameterValueFor(OptimizedParameterValue optimizedParameterValue, ParameterSelection linkedParameter)
 {
     return(OptimizedParameterValueFor(optimizedParameterValue.Value, linkedParameter));
 }
Beispiel #11
0
 public ParameterInIdentificationParameterEventArgs(IdentificationParameter identificationParameter, ParameterSelection linkedParameter) : base(identificationParameter)
 {
     LinkedParameter = linkedParameter;
 }
Beispiel #12
0
 public bool LinksParameter(ParameterSelection parameterSelection)
 {
     return(parameterSelection.Simulation != null && _allLinkedParameters.Contains(parameterSelection));
 }
 protected override void ActionForLinkedParameterIsNotInSimulation(ParameterIdentification parameterIdentification, ISimulation newSimulation, ParameterSelection linkedParameter, IdentificationParameter identificationParameter)
 {
     identificationParameter.RemovedLinkedParameter(linkedParameter);
 }
 public virtual bool IdentifiesParameter(ParameterSelection parameterSelection)
 {
     return(_allIdentificationParameters.Any(x => x.LinksParameter(parameterSelection)));
 }
        //context shoud represent use case defined in 47-7990 Management of Jacobi Matrix
        protected override void Context()
        {
            _parameterIdentification = A.Fake <ParameterIdentification>();
            _runResult         = A.Fake <OptimizationRunResult>();
            _simModelBatches   = new Dictionary <ISimulation, ISimModelBatch>();
            _allOutputMappings = new List <OutputMapping>();
            _allVariableIdentificationParameters = new List <IdentificationParameter>();

            A.CallTo(() => _parameterIdentification.AllOutputMappings).Returns(_allOutputMappings);
            A.CallTo(() => _parameterIdentification.AllVariableIdentificationParameters).Returns(_allVariableIdentificationParameters);
            sut = new JacobianMatrixCalculator();

            _output1        = A.Fake <OutputMapping>();
            _simulation1    = A.Fake <ISimulation>();
            _simModelBatch1 = A.Fake <ISimModelBatch>();
            _simModelBatches.Add(_simulation1, _simModelBatch1);
            A.CallTo(() => _output1.Simulation).Returns(_simulation1);
            A.CallTo(() => _output1.FullOutputPath).Returns("S1|OutputPath1");
            A.CallTo(() => _output1.OutputPath).Returns("OutputPath1");
            _allOutputMappings.Add(_output1);

            _output2        = A.Fake <OutputMapping>();
            _simulation2    = A.Fake <ISimulation>();
            _simModelBatch2 = A.Fake <ISimModelBatch>();
            A.CallTo(() => _output2.Simulation).Returns(_simulation2);
            A.CallTo(() => _output2.FullOutputPath).Returns("S2|OutputPath2");
            A.CallTo(() => _output2.OutputPath).Returns("OutputPath2");
            _simModelBatches.Add(_simulation2, _simModelBatch2);
            _allOutputMappings.Add(_output2);

            _identificationParameter1 = new IdentificationParameter().WithName("IP1");
            _identificationParameter2 = new IdentificationParameter().WithName("IP2");

            _ps1 = A.Fake <ParameterSelection>();
            _ps1.Parameter.Value = 100;
            A.CallTo(() => _ps1.FullQuantityPath).Returns("S1|ParameterPath1");
            A.CallTo(() => _ps1.Path).Returns("ParameterPath1");
            A.CallTo(() => _ps1.Simulation).Returns(_simulation1);
            _identificationParameter1.AddLinkedParameter(_ps1);

            _ps2 = A.Fake <ParameterSelection>();
            _ps2.Parameter.Value = 200;
            A.CallTo(() => _ps2.FullQuantityPath).Returns("S2|ParameterPath2");
            A.CallTo(() => _ps2.Path).Returns("ParameterPath2");
            A.CallTo(() => _ps2.Simulation).Returns(_simulation2);
            A.CallTo(() => _ps2.Dimension).Returns(_ps1.Dimension);
            _identificationParameter1.AddLinkedParameter(_ps2);

            _ps3 = A.Fake <ParameterSelection>();
            _ps3.Parameter.Value = 300;
            A.CallTo(() => _ps3.FullQuantityPath).Returns("S2|ParameterPath3");
            A.CallTo(() => _ps3.Path).Returns("ParameterPath3");
            A.CallTo(() => _ps3.Simulation).Returns(_simulation2);
            _identificationParameter2.AddLinkedParameter(_ps3);

            _allVariableIdentificationParameters.Add(_identificationParameter1);
            _allVariableIdentificationParameters.Add(_identificationParameter2);

            A.CallTo(() => _runResult.AllResidualsFor(_output1.FullOutputPath)).Returns(new[] { new Residual(1, 11, 1), new Residual(2, 21, 2), new Residual(3, 31, 0) });
            A.CallTo(() => _runResult.AllResidualsFor(_output2.FullOutputPath)).Returns(new[] { new Residual(1, 12, 3), new Residual(3, 32, 4), new Residual(4, 42, 5) });

            A.CallTo(() => _simModelBatch1.SensitivityValuesFor(_output1.OutputPath, _ps1.Path)).Returns(new[] { 11d, 21d, 31d, 41d, 51d });
            A.CallTo(() => _simModelBatch1.SensitivityValuesFor(_output1.OutputPath, _ps2.Path)).Throws(new OSPSuiteException());
            A.CallTo(() => _simModelBatch1.SensitivityValuesFor(_output1.OutputPath, _ps3.Path)).Throws(new OSPSuiteException());

            A.CallTo(() => _simModelBatch2.SensitivityValuesFor(_output2.OutputPath, _ps1.Path)).Throws(new OSPSuiteException());
            A.CallTo(() => _simModelBatch2.SensitivityValuesFor(_output2.OutputPath, _ps2.Path)).Returns(new[] { 12d, 22d, 32d, 42d, 52d, 62d });
            A.CallTo(() => _simModelBatch2.SensitivityValuesFor(_output2.OutputPath, _ps3.Path)).Returns(new[] { 13d, 23d, 33d, 43d, 53d, 63d });

            _simResults1 = new DataColumn
            {
                BaseGrid = new BaseGrid("Time", DomainHelperForSpecs.TimeDimensionForSpecs())
                {
                    Values = new[] { 1f, 2f, 3f, 4f, 5f }
                },
                Values = new[] { 10f, 20f, 30f, 40f, 50f }
            };

            A.CallTo(() => _runResult.SimulationResultFor(_output1.FullOutputPath)).Returns(_simResults1);

            _simResults2 = new DataColumn
            {
                BaseGrid = new BaseGrid("Time", DomainHelperForSpecs.TimeDimensionForSpecs())
                {
                    Values = new[] { 1f, 2f, 3f, 4f, 5f, 6f }
                },
                Values = new[] { 10f, 20f, 30f, 40f, 50f, 60f }
            };

            A.CallTo(() => _runResult.SimulationResultFor(_output2.FullOutputPath)).Returns(_simResults2);
        }
        private void addParameterSelectionToParameterIdentification(ParameterIdentification parameterIdentification, ParameterSelection parameterSelection)
        {
            var identificationParameter = parameterIdentification.IdentificationParameterByLinkedPath(parameterSelection.Path);

            if (identificationParameter != null)
            {
                identificationParameter.AddLinkedParameter(parameterSelection);
            }
            else
            {
                parameterIdentification.AddIdentificationParameter(_identificationParameterFactory.CreateFor(parameterSelection, parameterIdentification));
            }
        }
Beispiel #17
0
        public override void GlobalContext()
        {
            base.GlobalContext();
            _observedData = DomainHelperForSpecs.ObservedData();
            _sim1         = new IndividualSimulation
            {
                Id       = "Sim1",
                Name     = "Sim1",
                IsLoaded = true,
                Model    = new Model {
                    Root = new Container()
                }
            };
            _sim1.Model.Root.Add(new Container {
                new Parameter().WithName("P")
            }.WithName("Liver"));
            _sim2 = new IndividualSimulation
            {
                Id       = "Sim2",
                Name     = "Sim2",
                IsLoaded = true,
                Model    = new Model {
                    Root = new Container()
                }
            };
            _sim2.Model.Root.Add(new Container {
                new Parameter().WithName("P")
            }.WithName("Liver"));

            _objectBaseRepository = IoC.Resolve <IWithIdRepository>();
            var workspace = IoC.Resolve <IWorkspace>();

            _project          = IoC.Resolve <IPKSimProject>();
            workspace.Project = _project;
            _objectBaseRepository.Register(_sim1);
            _objectBaseRepository.Register(_sim2);
            _project.AddObservedData(_observedData);
            _project.AddBuildingBlock(_sim1);
            _project.AddBuildingBlock(_sim2);

            _parameterIdentification = new ParameterIdentification();
            _parameterIdentification.AddSimulation(_sim1);
            _parameterIdentification.AddSimulation(_sim2);

            _outputMapping = new OutputMapping
            {
                WeightedObservedData = new WeightedObservedData(_observedData),
                OutputSelection      = new SimulationQuantitySelection(_sim1, new QuantitySelection("A|B", QuantityType.Metabolite)),
                Weight  = 5,
                Scaling = Scalings.Log
            };

            _outputMapping.WeightedObservedData.Weights[1] = 10;
            _parameterIdentification.AddOutputMapping(_outputMapping);

            _identificationParameter = DomainHelperForSpecs.IdentificationParameter(min: 1, max: 10, startValue: 5);

            _parameterSelection1 = new ParameterSelection(_sim1, new QuantitySelection("Liver|P", QuantityType.Parameter));
            _parameterSelection2 = new ParameterSelection(_sim2, new QuantitySelection("Liver|P", QuantityType.Parameter));
            _identificationParameter.AddLinkedParameter(_parameterSelection1);
            _identificationParameter.AddLinkedParameter(_parameterSelection2);
            _parameterIdentification.AddIdentificationParameter(_identificationParameter);
            _identificationParameter.Scaling = Scalings.Linear;

            _parameterIdentification.Configuration.AlgorithmProperties = new OptimizationAlgorithmProperties("AA");
            _parameterIdentification.AlgorithmProperties.Add(new ExtendedProperty <double> {
                Name = "Toto", Value = 5
            });

            _runResult = new ParameterIdentificationRunResult();

            _parameterIdentification.AddResult(_runResult);

            _parameterIdentification.AddAnalysis(new ParameterIdentificationPredictedVsObservedChart());
            _parameterIdentification.AddAnalysis(new ParameterIdentificationTimeProfileChart());
            _parameterIdentification.AddAnalysis(new ParameterIdentificationResidualHistogram());
            _parameterIdentification.AddAnalysis(new ParameterIdentificationResidualVsTimeChart());

            GlobalBecause();
        }
Beispiel #18
0
 protected override void Context()
 {
     base.Context();
     _linkedParameter         = A.Fake <ParameterSelection>();
     _identificationParameter = new IdentificationParameter();
 }
Beispiel #19
0
 public virtual void UpdateSimulation(ISimulation newSimulation)
 {
     ParameterSelection.UpdateSimulation(newSimulation);
 }
Beispiel #20
0
 protected override void ActionForLinkedParameterIsNotInSimulation(ParameterIdentification parameterIdentification, ISimulation newSimulation, ParameterSelection linkedParameter, IdentificationParameter identificationParameter)
 {
     _validationResult.AddMessage(
         NotificationType.Warning,
         parameterIdentification,
         Captions.ParameterIdentification.SimulationDoesNotContainParameterWithPath(newSimulation.Name, linkedParameter.Path));
 }