public Problem SocialSpider(Problem prob, out double storagePercentage)
        {
            int    nSpiders = 5; //population size of spiders
            int    subsetSize = 100;
            int    totalInstances = prob.X.Count();;
            int    bound = 100, maxGen = 5;
            double r_a  = 1;   //This parameter controls the attenuation rate of the vibration intensity over distance
            double p_c  = 0.7; // p_c describes the probability of changing mask of spider
            double p_m  = 0.1; // This is also a user-controlled parameter defined in (0, 1). It controls the probability of assigning a one or zero to each bit of a mask
            bool   info = true;

            double[][] globalBestPosition = new double[1][];
            double[]   targetIntensity    = new double[nSpiders]; //best vibration for each spider
            //double[] targetPosition = new double[nSpiders]; //target position for each spider
            double[,] mask     = new double[nSpiders, subsetSize];
            double[,] newMask  = new double[nSpiders, subsetSize];
            double[,] movement = new double[nSpiders, subsetSize];
            double[] inactive                        = new double[nSpiders];
            double[] spiderFitnessVal                = new double[nSpiders];
            double[] newSpiderFitnessVal             = new double[nSpiders];
            ObjectInstanceSelection globalBestSpider = null;
            double globalBest                        = double.MinValue;
            Random rand = new Random();
            FlowerPollinationAlgorithm fpa = new FlowerPollinationAlgorithm();

            //initialize population
            List <ObjectInstanceSelection> spiders    = InitializeBinarySpider(nSpiders, subsetSize, totalInstances, prob);
            List <ObjectInstanceSelection> newSpiders = new List <ObjectInstanceSelection>(spiders.Count); //create a clone of bats

            spiders.ForEach((item) =>
            {
                newSpiders.Add(new ObjectInstanceSelection(item.Attribute_Values, item.Attribute_Values_Continuous, item.Pointers, item.Fitness, item.Position)); //create a clone of flowers
            });

            spiderFitnessVal    = EvaluateObjectiveFunction(spiders, prob);                                                                //evaluate fitness value for all the bats
            newSpiderFitnessVal = EvaluateObjectiveFunction(newSpiders, prob);                                                             //evaluate fitness value for new spiders. Note: this will be the same for this function call, since pollination has not occur
            SpiderFitness(spiderFitnessVal, spiders);                                                                                      //fitness value for each spiders
            SpiderFitness(newSpiderFitnessVal, newSpiders);                                                                                //fitness value for new spider
            globalBestSpider = EvaluateSolution(spiderFitnessVal, newSpiderFitnessVal, globalBest, spiders, newSpiders, globalBestSpider); //get the global best spider
            globalBest       = globalBestSpider.Fitness;

            double[]        standDev        = new double[subsetSize];
            List <double>   listPositions   = new List <double>();
            List <double[]> spiderPositions = new List <double[]>();

            //calculate the standard deviation of all spider positions
            for (int a = 0; a < subsetSize; a++)
            {
                double[] sPositions = new double[nSpiders];
                for (int b = 0; b < nSpiders; b++)
                {
                    sPositions[b] = spiders[b].Attribute_Values[a]; //get all spider positions column wise
                    //sPositions[b] = spiders[b].Attribute_Values_Continuous[a]; //get all spider positions column wise
                }
                spiderPositions.Add(sPositions); //save positions in list
            }

            for (int a = 0; a < subsetSize; a++)
            {
                standDev[a] = getStandardDeviation(spiderPositions[a].ToList()); //calculate standard deviation for each spider solution
            }
            double baseDistance = standDev.Average();                            //calculate the mean of standev

            //compute paired euclidean distances of all vectors in spider; similar to pdist function in matlab. Reference: http://www.mathworks.com/help/stats/pdist.html
            int n = (nSpiders * (nSpiders - 1)) / 2; //total number of elements array dist.

            double[] euclidenDist = new double[n];   //Note that, this is array for paired eucliden distance, similar to pdist() function in matlab.
            int      kk           = 0;

            for (int i = 0; i < nSpiders; i++)
            {
                for (int j = 1 + i; j < nSpiders; j++)
                {
                    //this distance is in pairs -> 1,0; 2,0; 3,0,...n,0; 2,1; 3,1; 4,1,...n,1;.... It is similar to pdist function in matlab
                    //euclidenDist[kk++] = computeEuclideanDistance(spiders[j].Attribute_Values_Continuous, spiders[i].Attribute_Values_Continuous); //generate a vibration for each spider position
                    euclidenDist[kk++] = computeEuclideanDistance(spiders[j].Attribute_Values, spiders[i].Attribute_Values); //generate a vibration for each spider position
                    //distance[i][j] = computeEuclideanDistance(spiders[i].Attribute_Values, spiders[j].Attribute_Values);
                }
            }

            double[,] distance = SquareForm(euclidenDist, nSpiders); //Convert vibration to square matix, using SquareForm() function in matlab. Reference: see Squareform function in google
            //double[,] intensityReceive = new double[nSpiders, nSpiders];
            double[][] intensityReceive = new double[nSpiders][];

            for (int a = 0; a < maxGen; a++)
            {
                for (int j = 0; j < nSpiders; j++)
                {
                    //calculate the intensity for all the generated vibrations
                    intensityReceive[j] = new double[nSpiders];
                    double A = (spiders[j].Fitness + Math.Exp(-100)) + 1;
                    double intensitySource = Math.Log(1 / A);
                    for (int k = 0; k < nSpiders; k++)
                    {
                        double intensityAttenuation = Math.Exp(-distance[j, k] / (baseDistance * r_a));
                        //intensityReceive[j, k] = intensitySource * intensityAttenuation; //intensity for each spider vibration
                        intensityReceive[j][k] = intensitySource * intensityAttenuation; //intensity for each spider vibration
                    }
                }

                //select strongest vibration from intensity
                int row    = intensityReceive.GetLength(0);
                int column = intensityReceive[0].Count();
                //IEnumerable<double> bestReceive = Enumerable.Range(0, row).Select(i => Enumerable.Range(0, column).Select(j => intensityReceive[i, j]).Max()); //get the max value in each row
                IEnumerable <double> bestReceive = Enumerable.Range(0, row).Select(i => Enumerable.Range(0, column).Select(j => intensityReceive[i][j]).Max()); //get the max value in each row

                //IEnumerable<int> bestReceiveIndex = Enumerable.Range(0, row).Select(i => Enumerable.Range(0, column).Select(j => intensityReceive[i, j]).Max()); //get the max value in each row

                //get the index of the strongest vibration
                int[] maxIndex = new int[nSpiders];
                for (int i = 0; i < nSpiders; i++)
                {
                    maxIndex[i] = Array.IndexOf(intensityReceive[i], bestReceive.ElementAt(i));
                }

                //Store the current best vibration
                int[] keepTarget = new int[nSpiders];
                int[] keepMask   = new int[nSpiders];
                double[,] targetPosition = new double[nSpiders, subsetSize];
                for (int i = 0; i < nSpiders; i++)
                {
                    if (bestReceive.ElementAt(i) <= targetIntensity[i])
                    {
                        keepTarget[i] = 1;
                    }

                    inactive[i]        = inactive[i] * keepTarget[i] + keepTarget[i];
                    targetIntensity[i] = (targetIntensity[i] * keepTarget[i]) + bestReceive.ElementAt(i) * (1 - keepTarget[i]);


                    if (rand.NextDouble() < Math.Pow(p_c, inactive[i]))
                    {
                        keepMask[i] = 1;
                    }
                    inactive[i] = inactive[i] * keepMask[i];

                    for (int j = 0; j < subsetSize; j++)
                    {
                        //newSpiders[i].Attribute_Values[j] = fi.Binarize(newSpiders[i].Attribute_Values[j] * spiders[maxIndex[i]].Attribute_Values[j] * (1 - keepTarget[i]), rand.NextDouble()); //update solution
                        targetPosition[i, j] = targetPosition[i, j] * keepTarget[i] + spiders[maxIndex[i]].Attribute_Values[j] * (1 - keepTarget[i]);
                        //targetPosition[i, j] = targetPosition[i, j] * keepTarget[i] + spiders[maxIndex[i]].Attribute_Values_Continuous[j] * (1 - keepTarget[i]);
                        newMask[i, j] = Math.Ceiling(rand.NextDouble() + rand.NextDouble() * p_m - 1);
                        mask[i, j]    = keepMask[i] * mask[i, j] + (1 - keepMask[i]) * newMask[i, j]; //update dimension mask of spider
                    }
                }

                //Reshuffule the Spider solution
                //Method: randomly generated positions pointing to rows and columns in the solution space. With the pointers, we can acess indivdual indices(or positions) in the solution
                double[,] randPosition = GenerateRandomSpiderPosition(nSpiders, subsetSize, spiders);

                //generate psfo, and perform random walk
                double[,] followPosition = new double[nSpiders, subsetSize];
                for (int i = 0; i < nSpiders; i++)
                {
                    for (int j = 0; j < subsetSize; j++)
                    {
                        followPosition[i, j] = mask[i, j] * randPosition[i, j] + (1 - mask[i, j]) * targetPosition[i, j];
                        movement[i, j]       = rand.NextDouble() * movement[i, j] + (followPosition[i, j] - spiders[i].Attribute_Values[j]) * rand.NextDouble(); //perform random movement
                        //movement[i, j] = rand.NextDouble() * movement[i, j] + (followPosition[i, j] - spiders[i].Attribute_Values_Continuous[j]) * rand.NextDouble(); //perform random movement
                        //newSpiders[i].Attribute_Values[j] = fi.Binarize(newSpiders[i].Attribute_Values_Continuous[j] + movement[i, j], rand.NextDouble()); //actual random walk
                        newSpiders[i].Attribute_Values[j] = fi.Binarize(newSpiders[i].Attribute_Values[j] + movement[i, j], rand.NextDouble()); //actual random walk
                    }
                }

                //Select best solutions from the original population and matured population for the next generation;
                fpa.SelectBestSolution(spiders, newSpiders);

                //evaluate new solution
                newSpiderFitnessVal = EvaluateObjectiveFunction(newSpiders, prob);                                                             //evaluate fitness value for all the bats
                SpiderFitness(newSpiderFitnessVal, newSpiders);                                                                                //fitness value for new bats
                globalBestSpider = EvaluateSolution(spiderFitnessVal, newSpiderFitnessVal, globalBest, spiders, newSpiders, globalBestSpider); //get the global best flower
                globalBest       = globalBestSpider.Fitness;

                //if solution has converged to a optimal user-defined point, stop search
                int Max = 60;          // maximum percentage reduction
                if (globalBest >= Max) //if the percentage reduction has approached 60%, stop search!
                {
                    break;
                }
            }

            //ensure that at least, N instances are selected for classification
            int Min = 15; //minimum number of selected instances

            globalBestSpider = fpa.AddInstances(globalBestSpider, Min);

            Problem subBest = fi.buildModelMultiClass(globalBestSpider, prob); //build model for the best Instance Mast

            storagePercentage = Training.StoragePercentage(subBest, prob);     //calculate the percent of the original training set was retained by the reduction algorithm
            return(subBest);
        }
예제 #2
0
        //Binary Bat
        public Problem BinaryBat(Problem prob, out double storagePercentage)
        {
            //default parameters
            int    populationSize = 3; //number of bats in the population
            int    subsetSize     = 100;
            int    maxGeneration  = 3;
            double loudness       = 0.5;
            double pulseRate      = 0.5;
            int    totalInstances = prob.X.Count(); //problem size
            double frequencyMin   = 0;              //minimum frequency. Frequency range determine the scalings
            double frequencyMax   = 2;              //maximum frequency.
            int    lowerBound     = -2;             //set lower bound - lower boundary
            int    upperBound     = 2;              //set upper bound - upper boundary

            double[] batFitnessVal                = new double[populationSize];
            double[] newbatFitnessVal             = new double[populationSize];
            double   globalBest                   = double.MinValue;
            ObjectInstanceSelection globalBestBat = null;
            Random r = new Random();
            FlowerPollinationAlgorithm fpa = new FlowerPollinationAlgorithm();

            //initialize population
            List <ObjectInstanceSelection> bats    = InitializeBinaryBat(populationSize, subsetSize, totalInstances, prob);
            List <ObjectInstanceSelection> newBats = new List <ObjectInstanceSelection>(bats.Count); //create a clone of bats

            bats.ForEach((item) =>
            {
                newBats.Add(new ObjectInstanceSelection(item.Attribute_Values, item.Attribute_Values_Continuous, item.Frequency, item.Velocity, item.Pointers, item.Fitness)); //create a clone of flowers
            });

            batFitnessVal    = EvaluateObjectiveFunction(bats, prob);                                                              //evaluate fitness value for all the bats
            newbatFitnessVal = EvaluateObjectiveFunction(newBats, prob);                                                           //evaluate fitness value for new bats. Note: this will be the same for this function call, since pollination has not occur
            BatFitness(batFitnessVal, bats);                                                                                       //fitness value for each bats
            BatFitness(newbatFitnessVal, newBats);                                                                                 //fitness value for new bats
            globalBestBat = EvaluateSolution(batFitnessVal, newbatFitnessVal, globalBest, bats, newBats, globalBestBat, loudness); //get the global best flower
            globalBest    = globalBestBat.Fitness;

            //start bat algorithm
            double rand = r.NextDouble(); //generate random number

            for (int i = 0; i < maxGeneration; i++)
            {
                //loop over all bats or solutions
                for (int j = 0; j < populationSize; j++)
                {
                    for (int k = 0; k < subsetSize; k++)
                    {
                        bats[j].Frequency = frequencyMin + (frequencyMin - frequencyMax) * r.NextDouble();                                                       //Adjust frequency
                        double randNum = SimpleRNG.GetNormal();                                                                                                  //generate random number with normal distribution
                        newBats[j].Velocity[k] = newBats[j].Velocity[k] + (bats[j].Attribute_Values[k] - globalBestBat.Attribute_Values[k]) * bats[j].Frequency; //update velocity
                        //newBats[j].Attribute_Values[k] = fpa.ConvertToBinary(newBats[j].Velocity[k], newBats[j].Attribute_Values[k]); //update bat position in the binary space
                        newBats[j].Attribute_Values[k] = TransferFunction(newBats[j].Velocity[k], newBats[j].Attribute_Values[k]);                               //update bat position in the binary space

                        if (rand > pulseRate)
                        {
                            newBats[j].Attribute_Values[k] = globalBestBat.Attribute_Values[k]; //change some of the dimensions of the position vector with some dimension of global best. Refer to reference for more explaination
                        }
                    }
                }

                //Select best solutions from the original population and matured population for the next generation;
                fpa.SelectBestSolution(bats, newBats);

                //evaluate new solution
                newbatFitnessVal = EvaluateObjectiveFunction(newBats, prob);                                                           //evaluate fitness value for all the bats
                BatFitness(newbatFitnessVal, newBats);                                                                                 //fitness value for new bats
                globalBestBat = EvaluateSolution(batFitnessVal, newbatFitnessVal, globalBest, bats, newBats, globalBestBat, loudness); //get the global best flower
                globalBest    = globalBestBat.Fitness;

                //if solution has converged to a optimal user-defined point, stop search
                int Max = 60;          // maximum percentage reduction
                if (globalBest >= Max) //if the percentage reduction has approached 60%, stop search!
                {
                    break;
                }
            }

            //ensure that at least, N instances are selected for classification
            int min = 15; //minimum number of selected instances

            globalBestBat = fpa.AddInstances(globalBestBat, min);

            Problem subBest = fi.buildModelMultiClass(globalBestBat, prob); //build model for the best Instance Mast

            storagePercentage = Training.StoragePercentage(subBest, prob);  //calculate the percent of the original training set was retained by the reduction algorithm
            return(subBest);
        }
예제 #3
0
        public Problem CuckooSearch(Problem prob, out double storagePercentage)
        {
            int    nNests         = 5;    //number of nests, or number of solutions
            int    subsetSize     = 100;
            int    maxGen         = 5;    //maximum generation
            double discoveryRate  = 0.25; //discovery rate of alien eggs
            double tolerance      = Math.Exp(-5);
            int    lowerBound     = -5;
            int    upperBound     = 5;
            int    totalInstances = prob.X.Count(); //problem size

            double[] cuckooFitnessVal                = new double[nNests];
            double[] newCuckooFitnessVal             = new double[nNests];
            ObjectInstanceSelection globalBestCuckoo = null;
            double globalBest = double.MinValue;
            Random rand       = new Random();

            FlowerPollinationAlgorithm fpa = new FlowerPollinationAlgorithm();

            //initialize population
            List <ObjectInstanceSelection> cuckoos    = InitializeBinaryCuckoo(nNests, subsetSize, totalInstances, prob);
            List <ObjectInstanceSelection> newCuckoos = new List <ObjectInstanceSelection>(cuckoos.Count); //create a clone of bats

            cuckoos.ForEach((item) =>
            {
                newCuckoos.Add(new ObjectInstanceSelection(item.Attribute_Values, item.Attribute_Values_Continuous, item.Pointers, item.Fitness)); //create a clone of flowers
            });

            cuckooFitnessVal    = EvaluateObjectiveFunction(cuckoos, prob);                                                                //evaluate fitness value for all the bats
            newCuckooFitnessVal = EvaluateObjectiveFunction(newCuckoos, prob);                                                             //evaluate fitness value for new bats. Note: this will be the same for this function call, since pollination has not occur
            CuckooFitness(cuckooFitnessVal, cuckoos);                                                                                      //fitness value for each bats
            CuckooFitness(newCuckooFitnessVal, newCuckoos);                                                                                //fitness value for new bats
            globalBestCuckoo = EvaluateSolution(cuckooFitnessVal, newCuckooFitnessVal, globalBest, cuckoos, newCuckoos, globalBestCuckoo); //get the global best flower
            globalBest       = globalBestCuckoo.__Fitness;

            //generate new solutions
            double beta  = 3 / 2;
            double A     = fp.Gamma(1 + beta) * Math.Sin(Math.PI * (beta / 2));
            double B     = fp.Gamma((1 + beta) / 2) * beta;
            double C     = (beta - 1) / 2;
            double D     = Math.Pow(2, C);
            double E     = A / (B * D);
            double sigma = Math.Pow(E, (1 / beta));

            double F;
            double G;
            double step;
            double stepSize;
            int    x = 0;

            for (int i = 0; i <= maxGen; i++)
            {
                for (int j = 0; j < nNests; j++)
                {
                    for (int k = 0; k < subsetSize; k++)
                    {
                        F    = SimpleRNG.GetNormal() * sigma;
                        G    = SimpleRNG.GetNormal();
                        step = F / Math.Pow(Math.Abs(G), (1 / beta));

                        //In the next equation, the difference factor (s-best) means that when the solution is the best solution, it remains unchanged.
                        //Here the factor 0.01 comes from the fact that L/100 should the typical step size of walks/flights where L is the typical lenghtscale;
                        //otherwise, Levy flights may become too aggresive/efficient, which makes new solutions (even) jump out side of the design domain (and thus wasting evaluations).
                        stepSize = 0.01 * step * (cuckoos[j].Attribute_Values[k] - globalBestCuckoo.Attribute_Values[k]);

                        //Now the actual random walks or levyy flights
                        newCuckoos[j].Attribute_Values[k] = fi.Binarize((newCuckoos[j].Attribute_Values[k] + stepSize) * SimpleRNG.GetNormal(), rand.NextDouble());

                        if (cuckoos[j].Attribute_Values[k] == 1 && newCuckoos[j].Attribute_Values[k] == 0)
                        {
                            x++;
                        }
                    }
                }

                //discovery and randomization - replace some nest by constructing new solutions
                newCuckoos = EmptyNest(cuckoos, newCuckoos, discoveryRate, subsetSize, nNests);

                //Select best solutions from the original population and matured population for the next generation;
                fpa.SelectBestSolution(cuckoos, newCuckoos);

                //evaluate new solution
                newCuckooFitnessVal = EvaluateObjectiveFunction(newCuckoos, prob);                                                             //evaluate fitness value for all the bats
                CuckooFitness(newCuckooFitnessVal, newCuckoos);                                                                                //fitness value for new bats
                globalBestCuckoo = EvaluateSolution(cuckooFitnessVal, newCuckooFitnessVal, globalBest, cuckoos, newCuckoos, globalBestCuckoo); //get the global best flower
                globalBest       = globalBestCuckoo.Fitness;

                //if solution has converged to a optimal user-defined point, stop search
                int Max = 60;          // maximum percentage reduction
                if (globalBest >= Max) //if the percentage reduction has approached 60%, stop search!
                {
                    break;
                }
            }

            //ensure that at least, N instances are selected for classification
            int min = 40; //minimum number of selected instances

            globalBestCuckoo = fpa.AddInstances(globalBestCuckoo, min);

            Problem subBest = fi.buildModelMultiClass(globalBestCuckoo, prob); //build model for the best Instance Mast

            storagePercentage = Training.StoragePercentage(subBest, prob);     //calculate the percent of the original training set was retained by the reduction algorithm
            return(subBest);
        }