/// <summary> /// generating the initial locations of n spiders /// </summary> public List <ObjectInstanceSelection> InitializeBinarySpider(int nSpiders, int subsetSize, int probSize, Problem prob) { Random rnd = new Random(); List <int> rNum = Training.GetRandomNumbers(probSize, probSize); //generate N random numbers FireflyInstanceSelection fpa = new FireflyInstanceSelection(); List <ObjectInstanceSelection> attr_values = new List <ObjectInstanceSelection>(); int cnt1 = 0, cnt2 = 0, cnt3 = 0; //create an array of size n for x and y int[] xn = new int[subsetSize]; //instance mask double[] xn_Con = new double[subsetSize]; //instance mask continuous double freq = new double(); //initialize the frequency of all the bats to zero double[] vel = new double[subsetSize]; //initialize the velocity of all the bats to zero int[] pointers = new int[subsetSize]; //array contain pointer to actual individual instance represented in the instance mask double spiderPosition = 0; int k = 0; int bound = 100; for (int i = 0; i < nSpiders; i++) { xn = new int[subsetSize]; xn_Con = new double[subsetSize]; pointers = new int[subsetSize]; cnt1 = 0; cnt2 = 0; cnt3 = 0; for (int j = 0; j < prob.Count; j++) { if (cnt1 < (0.7 * subsetSize) && prob.Y[rNum[j]] == -1) //select 70% positive instance of the subset { //xn_Con[cnt3] = rnd.NextDouble(); //xn[cnt3] = fi.Binarize(xn_Con[cnt3], rnd.NextDouble()); xn[cnt3] = rnd.Next(0, 2); //initialize each spider position. pointers[cnt3] = rNum[j]; spiderPosition = rnd.NextDouble() * 2 * bound - bound; //generate position of spider k++; cnt1++; cnt3++; } else if (cnt2 < (0.3 * subsetSize) && prob.Y[rNum[j]] == 1) { //xn_Con[cnt3] = rnd.NextDouble(); //xn[cnt3] = fi.Binarize(xn_Con[cnt3], rnd.NextDouble()); xn[cnt3] = rnd.Next(0, 2); //initialize each spider position. pointers[cnt3] = rNum[j]; spiderPosition = rnd.NextDouble() * 2 * bound - bound; //generate position of spider k++; cnt2++; cnt3++; } if (cnt3 >= subsetSize) { break; } } ObjectInstanceSelection OI = new ObjectInstanceSelection(xn, xn_Con, pointers, 0.0, spiderPosition); attr_values.Add(OI); } return(attr_values); }
/// <summary> /// generating the initial locations of n flower /// </summary> public List <ObjectInstanceSelection> InitializeBinaryFlower(int nFlower, int subsetSize, int probSize, Problem prob) { Random rnd = new Random(); List <int> rNum = Training.GetRandomNumbers(probSize, probSize); //generate N random numbers FireflyInstanceSelection fpa = new FireflyInstanceSelection(); List <ObjectInstanceSelection> attr_values = new List <ObjectInstanceSelection>(); int cnt1 = 0, cnt2 = 0, cnt3 = 0; //create an array of size n for x and y int[] xn = new int[subsetSize]; //instance mask double[] xn_Con = new double[subsetSize]; //instance mask continuous int[] pointers = new int[subsetSize]; //array contain pointer to actual individual instance represented in the instance mask int k = 0; for (int i = 0; i < nFlower; i++) { xn = new int[subsetSize]; xn_Con = new double[subsetSize]; pointers = new int[subsetSize]; cnt1 = 0; cnt2 = 0; cnt3 = 0; for (int j = 0; j < prob.Count; j++) { if (cnt1 < (0.7 * subsetSize) && prob.Y[rNum[j]] == -1) //select 70% positive instance of the subset { //xn[cnt3] = rnd.NextDouble() <= 0.5 ? 0 : 1; xn[cnt3] = rnd.Next(0, 2); //xn_Con[cnt3] = rnd.NextDouble(); //xn[cnt3] = fi.Binarize(xn_Con[cnt3], rnd.NextDouble()); pointers[cnt3] = rNum[j]; k++; cnt1++; cnt3++; } else if (cnt2 < (0.3 * subsetSize) && prob.Y[rNum[j]] == 1) { //xn[cnt3] = rnd.NextDouble() <= 0.5 ? 0 : 1; xn[cnt3] = rnd.Next(0, 2); //xn_Con[cnt3] = rnd.NextDouble(); //xn[cnt3] = fi.Binarize(xn_Con[cnt3], rnd.NextDouble()); pointers[cnt3] = rNum[j]; k++; cnt2++; cnt3++; } if (cnt3 >= subsetSize) { break; } } ObjectInstanceSelection OI = new ObjectInstanceSelection(xn, xn_Con, pointers, 0.0); attr_values.Add(OI); } return(attr_values); }
/// <summary> /// generating the initial locations of n bats /// </summary> public List <ObjectInstanceSelection> InitializeBinaryBat(int nBats, int subsetSize, int probSize, Problem prob) { Random rnd = new Random(); List <int> rNum = Training.GetRandomNumbers(probSize, probSize); //generate N random numbers FireflyInstanceSelection fpa = new FireflyInstanceSelection(); List <ObjectInstanceSelection> attr_values = new List <ObjectInstanceSelection>(); int cnt1 = 0, cnt2 = 0, cnt3 = 0; //create an array of size n for x and y int[] xn = new int[subsetSize]; //instance mask double[] xn_Con = new double[subsetSize]; //instance mask continuous double freq = new double(); //initialize the frequency of all the bats to zero double[] vel = new double[subsetSize]; //initialize the velocity of all the bats to zero int[] pointers = new int[subsetSize]; //array contain pointer to actual individual instance represented in the instance mask int k = 0; for (int i = 0; i < nBats; i++) { xn = new int[subsetSize]; xn_Con = new double[subsetSize]; pointers = new int[subsetSize]; cnt1 = 0; cnt2 = 0; cnt3 = 0; for (int j = 0; j < prob.Count; j++) { if (cnt1 < (0.7 * subsetSize) && prob.Y[j] == -1) //select 70% negative instance (i.e. ham) of the subset { xn[cnt3] = rnd.Next(0, 2); //xn[cnt3] = 0; pointers[cnt3] = rNum[j]; k++; cnt1++; cnt3++; } else if (cnt2 < (0.3 * subsetSize) && prob.Y[j] == 1) { xn[cnt3] = rnd.Next(0, 2); //xn[cnt3] = 0; pointers[cnt3] = rNum[j]; k++; cnt2++; cnt3++; } if (cnt3 >= subsetSize) { break; } } ObjectInstanceSelection OI = new ObjectInstanceSelection(xn, xn_Con, freq, vel, pointers, 0.0); attr_values.Add(OI); } return(attr_values); }
//flower pollination algorithm by Yang public Problem FlowerPollination(Problem prob) { int nargin = 0, totalInstances = prob.X.Count(), maxGeneration = 500; int numOfFlower = 10; //population size double probabilitySwitch = 0.8; //assign probability switch int subsetSize = 200; //dimension for each flower double[] flowerFitnessVal = new double[numOfFlower]; double[] newFlowerFitnessVal = new double[numOfFlower]; FireflyInstanceSelection fw = new FireflyInstanceSelection(); double globalBest = double.MinValue; double newBest = new double(); ObjectInstanceSelection globalBestFlower = null; int lowerBound = -2; //set lower bound - lower boundary int upperBound = 2; //set upper bound - upper boundary int maxIndex; //inittalize flowers, and get global best List <ObjectInstanceSelection> flowers = InitializeFlower(numOfFlower, subsetSize, totalInstances, prob); //initialize solution List <ObjectInstanceSelection> newFlowers = new List <ObjectInstanceSelection>(flowers.Count); //create a clone of flowers flowers.ForEach((item) => { newFlowers.Add(new ObjectInstanceSelection(item.__Attribute_Values, item.__Attribute_Values_Continuous, item.__Pointers, item.__Fitness)); //create a clone of flowers }); flowerFitnessVal = fw.EvaluateObjectiveFunction(flowers, prob); //evaluate fitness value for all the flowers newFlowerFitnessVal = fw.EvaluateObjectiveFunction(newFlowers, prob); //evaluate fitness value for new flowers. Note: this will be the same for this function call, since pollination has not occur FlowerFitness(flowerFitnessVal, flowers); //fitness value for each flower FlowerFitness(newFlowerFitnessVal, newFlowers); //fitness value for new flower globalBestFlower = EvaluateSolution(flowerFitnessVal, newFlowerFitnessVal, globalBest, flowers, newFlowers, globalBestFlower); //get the global best flower globalBest = flowerFitnessVal.Max(); //start flower algorithm Random r = new Random(); double[] levy = new double[subsetSize]; for (int i = 0; i < maxGeneration; i++) { double rand = r.NextDouble(); if (rand > probabilitySwitch) //global pollination { //global pollination for (int j = 0; j < numOfFlower; j++) { levy = LevyFlight(subsetSize); for (int k = 0; k < subsetSize; k++) { double A = levy[k] * (flowers[j].__Attribute_Values_Continuous[k] - globalBestFlower.__Attribute_Values_Continuous[k]); double B = flowers[j].__Attribute_Values_Continuous[k] + A; A = SimpleBounds(B, lowerBound, upperBound); //ensure that value does not go beyond defined boundary newFlowers[j].__Attribute_Values_Continuous[k] = A; newFlowers[j].__Attribute_Values[k] = fw.Binarize(B, r.NextDouble()); //convert to binary } } } else //local pollination { for (int j = 0; j < numOfFlower; j++) { List <int> randNum = Training.GetRandomNumbers(2, numOfFlower); //generate 2 distinct random numbers double epsilon = rand; //local pollination for (int k = 0; k < subsetSize; k++) { double A = flowers[j].__Attribute_Values_Continuous[k] + epsilon * (flowers[randNum[0]].__Attribute_Values_Continuous[k] - flowers[randNum[1]].__Attribute_Values_Continuous[k]); //randomly select two flowers from neighbourhood for pollination A = SimpleBounds(A, lowerBound, upperBound); //ensure that value does not exceed defined boundary newFlowers[j].__Attribute_Values_Continuous[k] = A; //save computation newFlowers[j].__Attribute_Values[k] = fw.Binarize(A, r.NextDouble()); //convert to binary } } } //evaluate new solution newFlowerFitnessVal = fw.EvaluateObjectiveFunction(newFlowers, prob); //evaluate fitness value for all the flowers FlowerFitness(newFlowerFitnessVal, newFlowers); //fitness value for new flower globalBestFlower = EvaluateSolution(flowerFitnessVal, newFlowerFitnessVal, globalBest, flowers, newFlowers, globalBestFlower); //Evaluate solution, update better solution and get global best flower globalBest = flowerFitnessVal.Max(); } //ensure that at least, 40 instances is selected for classification int countSelected = globalBestFlower.__Attribute_Values.Count(q => q == 1); //count the total number of selected instances int diff, c = 0, d = 0; int Min = 40; //minimum number of selected instances if (countSelected < Min) { //if there are less than N, add N instances, where N = the number of selected instances diff = Min - countSelected; while (c < diff) { if (globalBestFlower.__Attribute_Values[d++] == 1) { continue; } else { globalBestFlower.__Attribute_Values[d++] = 1; c++; } } } Problem subBest = fw.buildModel(globalBestFlower, prob); //build model for the best Instance Mast return(subBest); }