示例#1
0
        /// <summary>
        /// Performs a Grid parameter selection, trying all possible combinations of the two lists and returning the
        /// combination which performed best.  Use this method if validation data isn't available, as it will
        /// divide the training data and train on a portion of it and test on the rest.
        /// </summary>
        /// <param name="problem">The training data</param>
        /// <param name="parameters">The parameters to use when optimizing</param>
        /// <param name="CValues">The set of C values to use</param>
        /// <param name="GammaValues">The set of Gamma values to use</param>
        /// <param name="outputFile">Output file for the parameter results.</param>
        /// <param name="nrfold">The number of times the data should be divided for validation</param>
        /// <param name="C">The optimal C value will be placed in this variable</param>
        /// <param name="Gamma">The optimal Gamma value will be placed in this variable</param>
        public static void Grid(
            Problem problem,
            Parameter parameters,
            List <double> CValues,
            List <double> GammaValues,
            string outputFile,
            int nrfold,
            out double C,
            out double Gamma)
        {
            C     = 0;
            Gamma = 0;
            List <double> avgAcc          = new List <double>(); //avgAcc->average accuracy; it stores the average accuracies for all the C and Gamma values
            List <double> CVal            = new List <double>(); //store the C values with high accuracies
            List <double> GVal            = new List <double>(); //store the C values with high accuracies
            double        crossValidation = double.MinValue;
            StreamWriter  output          = null;
            List <double> nCVal           = new List <double>(); //nCVal -> randomly selected new C Values
            List <double> nGVal           = new List <double>(); //nGVal -> randomly selected new Gamma Values
            List <double> nAvgAcc         = new List <double>(); //nAvgAcc -> randomly selected new average accuracies
            string        outputValues    = "";                  // outputValues -> hold the C,Gamma and accuracy for each iteration. This is for to derive a pattern and formula
            //List<string> outputValues = new List<string>(); // outputValues -> hold the C,Gamma and accuracy for each iteration. This is for to derive a pattern and formula

            ParameterSelection ps = new ParameterSelection();
            FireFly            ff = new FireFly();
            int    nFF            = CValues.Count * GammaValues.Count;//nFF -> number of fireflies
            Random r       = new Random();
            int    nValues = CValues.Count - 1;

            //int nValues = CValues.Count;

            if (outputFile != null)
            {
                output = new StreamWriter(outputFile);
            }

            //****Firefly Optimized SVM
            //for (int i = 0; i < GammaValues.Count; i++)
            //{
            //    parameters.C = CValues[nValues--];
            //    parameters.Gamma = GammaValues[i];
            //    double test = Training.PerformCrossValidation(problem, parameters, nrfold);

            //    //avgAcc.Add(test);
            //    //CVal.Add(parameters.C);
            //    //GVal.Add(parameters.Gamma);

            //    Console.Write("{0} {1} {2}", parameters.C, parameters.Gamma, test);

            //    outputValues = parameters.C.ToString() + " " + parameters.Gamma.ToString() + " " + test.ToString();
            //    File.AppendAllText(ps.filePath, outputValues);
            //    File.AppendAllText(ps.filePath, Environment.NewLine);

            //    if (output != null)
            //        output.WriteLine("{0} {1} {2}", parameters.C, parameters.Gamma, test);
            //    if (test > crossValidation)
            //    {
            //        C = parameters.C;
            //        Gamma = parameters.Gamma;
            //        crossValidation = test;
            //        Console.WriteLine(" New Maximum!");

            //        //break from loop if the cross validation rate is equal to 1 (i.e. 100%)
            //        /*if (crossValidation == 1.0)
            //        {
            //           proceed = true;
            //           break;
            //        }*/

            //    }
            //    else
            //        Console.WriteLine();
            //}
            //Object selectedFirefly = ff.firefly_simple(avgAcc, CVal, GVal, problem, parameters);
            //C = (double)selectedFirefly.cValue;
            //Gamma = (double)selectedFirefly.GValue;

            //Standard SVM Optimization
            //for (int i = 0; i < CValues.Count; i++)
            //    for (int j = 0; j < GammaValues.Count; j++)
            //    {
            //        parameters.C = CValues[i];
            //        parameters.Gamma = GammaValues[j];
            //        double test = Training.PerformCrossValidation(problem, parameters, nrfold);
            //        Console.Write("{0} {1} {2}", parameters.C, parameters.Gamma, test);
            //        if (output != null)
            //            output.WriteLine("{0} {1} {2}", parameters.C, parameters.Gamma, test);
            //        if (test > crossValidation)
            //        {
            //            C = parameters.C;
            //            Gamma = parameters.Gamma;
            //            crossValidation = test;
            //            Console.WriteLine(" New Maximum!");
            //        }
            //        else Console.WriteLine();
            //    }

            for (int i = 0; i < CValues.Count; i++)
            {
                double test = new double();
                for (int j = 0; j < GammaValues.Count; j++)
                {
                    parameters.C     = CValues[i];
                    parameters.Gamma = GammaValues[j];
                    test             = Training.PerformCrossValidation(problem, parameters, nrfold);

                    File.AppendAllText(ps.filePath2, Environment.NewLine); //insert double line to file at the end of each parameter evaluation
                    File.AppendAllText(ps.filePath2, Environment.NewLine); //insert double line to file at the end of each parameter evaluation

                    Console.Write("{0} {1} {2}", parameters.C, parameters.Gamma, test);

                    outputValues = parameters.C.ToString() + " " + parameters.Gamma.ToString() + " " + test.ToString();
                    File.AppendAllText(ps.filePath, outputValues);
                    File.AppendAllText(ps.filePath, Environment.NewLine);
                    if (output != null)
                    {
                        output.WriteLine("{0} {1} {2}", parameters.C, parameters.Gamma, test);
                    }
                    if (test > crossValidation)
                    {
                        C               = parameters.C;
                        Gamma           = parameters.Gamma;
                        crossValidation = test;
                        Console.WriteLine(" New Maximum!");

                        if (test == 1)
                        {
                            CVAccuracy = test;
                            break;
                        }
                        else
                        {
                            CVAccuracy = test;
                        }
                        //outputValues.Add(C.ToString()); outputValues.Add(Gamma.ToString()); outputValues.Add(test.ToString());
                        //outputValues = C.ToString() + " " + Gamma.ToString() + " " + test.ToString();
                        //File.AppendAllText(ps.filePath, outputValues);
                        //File.AppendAllText(ps.filePath, Environment.NewLine);
                    }
                    else
                    {
                        Console.WriteLine();
                    }
                }
                if (test == 1)
                {
                    CVAccuracy = test;
                    break;
                }
                else
                {
                    CVAccuracy = test;
                }
            }

            //File.AppendAllText(ps.filePath, Environment.NewLine);
            if (output != null)
            {
                output.Close();
            }
        }