Beispiel #1
0
        public static void Main(string[] args)
        {
            Problem train = Problem.Read("a1a.train.txt");
            Problem test  = Problem.Read("a1a.test.txt");


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


            //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.

            ParameterSelection.Grid(train, parameters, "params.txt", out C, out Gamma);
            parameters.C     = C;
            parameters.Gamma = Gamma;


            //Train the model using the optimal parameters.

            Model model = Training.Train(train, parameters);


            //Perform classification on the test data, putting the
            //results in results.txt.

            Prediction.Predict(test, "results.txt", model, false);
        }
Beispiel #2
0
        /// <summary>
        /// Move all fireflies toward brighter ones
        /// </summary>
        public void ffa_move(double[] Lightn, Object[] fireflies0, double[] Lighto, double alpha, double gamma, List <Object> fireflies,
                             Problem prob, Parameter param, List <double> avgAcc, List <int> changedIndex)
        {
            int    nj = fireflies0.Length;
            double rC, rG, rF; //rC -> distance for C value, rG-> distance for Gamma value, rF - distance for the feature mask
            double beta0;
            double beta;       // beta -> attrativeness value for C and G, betaF -> attrativeness for the feature mask

            //specifying the ranges for C and Gamma
            double minC = Math.Pow(2, MIN_C);                   // minimum value for C
            double maxC = Math.Pow(2, MAX_C);                   // maximum value for C
            double minG = Math.Pow(2, MIN_G);                   // minimum value for G
            double maxG = Math.Pow(2, MAX_G);                   // maximum value for G

            int nF = fireflies[0].Attribute_Values.Count();     //nF -> number of features

            double[] CBackup     = new double[fireflies.Count]; //back up array for C value
            double[] GammaBackup = new double[fireflies.Count]; ////back up array for Gamma value

            Random rnd = new Random(); Random rx = new Random(); Random ry = new Random();

            duplicateValue(fireflies, CBackup, GammaBackup);
            for (int i = 0; i < nj; i++)
            {
                for (int j = 0; j < nj; j++)
                {
                    if (j == i) //avoid comparism with the same element
                    {
                        continue;
                    }
                    rF = 0.0;

                    rC = Math.Pow(((double)fireflies[i].cValue - (double)fireflies0[j].cValue), 2);
                    rG = Math.Pow(((double)fireflies[i].GValue - (double)fireflies0[j].GValue), 2);
                    double r = Math.Sqrt(rC + rG); //r -> total distance for both C and Gamma

                    if (Lightn[i] < Lighto[j])
                    {
                        beta0 = 1;                                         //setting beta to 1
                        beta  = beta0 * Math.Exp(-gamma * Math.Pow(r, 2)); //The attractiveness parameter for C and Gamma -> beta=exp(-gamma*r)

                        //changing firefly i position for the continuous values - i.e C and Gamma value respectively
                        fireflies[i].cValue = ((double)fireflies[i].cValue * (1 - beta)) + (CBackup[j] * beta) + (alpha * (rnd.NextDouble() - 0.5));
                        fireflies[i].GValue = ((double)fireflies[i].GValue * (1 - beta)) + (GammaBackup[j] * beta) + (alpha * (rnd.NextDouble() - 0.5));

                        findrange(fireflies[i], minC, maxC, minG, maxG); //restrict the values of C and Gamma to the specified range
                    }
                }
                if ((double)fireflies[i].cValue != CBackup[i] || (double)fireflies[i].GValue != GammaBackup[i])
                {
                    changedIndex.Add(i); //saving the index of the firefly that has been moved for the purpose of accuracy calculation
                }
            }

            //calculate the new accuracy for the newly updated C and Gamma value
            ParameterSelection.Grid(prob, param, fireflies, changedIndex, avgAcc, CBackup, GammaBackup, NFOLD);
        }