def bestBeeQuality(self): distance = 0 i = 0 pos = -1 while (i < self.bees_number): max_val = self.beeList[i].fitness nbUn = Solution.nbrUn(self.beeList[i].solution.get_state()) while ((i < self.bees_number) and (self.beeList[i].solution.get_accuracy( self.beeList[i].solution.get_state()) == max_val)): distanceTemp = self.distanceTabou(self.beeList[i]) nbUnTemp = Solution.nbrUn(self.beeList[i].solution.get_state()) if (distanceTemp > distance) or ((distanceTemp == distance) and (nbUnTemp < nbUn)): if ((distanceTemp == distance) and (nbUnTemp < nbUn)): print("We pick the solution with less features") nbUn = nbUnTemp distance = distanceTemp pos = i i += 1 if (pos != -1): return self.beeList[pos] bee = Bee(-1, self.data, self.locIterations, Bee.Rand(self.data.nb_attribs)) return bee
def searchArea(self): i = 0 h = 0 self.beeList = [] while ((i < self.bees_number) and (i < self.flip)): #print ("First method to generate") solution = self.refSolution.solution.get_state() k = 0 while ((self.flip * k + h) < len(solution)): solution[self.flip * k + h] = ((solution[self.flip * k + h] + 1) % 2) k += 1 newBee = Bee(i, self.data, self.locIterations, solution) self.beeList.append(newBee) i += 1 h = h + 1 h = 0 while ((i < self.bees_number) and (i < 2 * self.flip)): #print("Second method to generate") solution = self.refSolution.solution.get_state() k = 0 while ((k < int(len(solution) / self.flip)) and (self.flip * k + h < len(solution))): solution[int(self.data.nb_attribs / self.flip) * h + k] = ( (solution[int(self.data.nb_attribs / self.flip) * h + k] + 1) % 2) k += 1 newBee = Bee(i, self.data, self.locIterations, solution) self.beeList.append(newBee) i += 1 h = h + 1 while (i < self.bees_number): #print("Random method to generate") solution = self.refSolution.solution.get_state() indice = random.randint(0, len(solution) - 1) solution[indice] = ((solution[indice] + 1) % 2) newBee = Bee(i, self.data, self.locIterations, solution) self.beeList.append(newBee) i += 1 for bee in (self.beeList): lista = [ j for j, n in enumerate(bee.solution.get_state()) if n == 1 ] if (len(lista) == 0): bee.setSolution(Bee.Rand(self.data.nb_attribs))
def bestBeeDiversity(self): max_val = 0 for i in range(len(self.beeList)): if (self.distanceTabou(self.beeList[i]) > max_val): max_val = self.distanceTabou(self.beeList[i]) if (max_val == 0): bee = Bee(-1, self.data, self.locIterations, Bee.Rand(self.data.nb_attribs)) return bee i = 0 while (i < len(self.beeList) and self.distanceTabou(self.beeList[i]) != max_val): i += 1 return self.beeList[i]
def __init__(self, problem, flip, max_chance, bees_number, maxIterations, locIterations): self.data = problem self.flip = flip self.max_chance = max_chance self.nbChance = max_chance self.bees_number = bees_number self.maxIterations = maxIterations self.locIterations = locIterations self.beeList = [] self.refSolution = Bee(-1, self.data, self.locIterations, Bee.Rand(self.data.nb_attribs)) self.bestSolution = self.refSolution self.tabou = [] self.feature_count = {i: 0 for i in range(self.data.nb_attribs)} Solution.solutions.clear()