def updateBat(bat, bmin, bmax, best, fmin, fmax, A, G, dim, size, alpha = 0.8, gamma = 0.9): """bat.frequency = fmin + (fmax - fmin) * random.uniform(0,1) bat.velocity = list(map(operator.add, bat.velocity, (_ * bat.frequency for _ in map(operator.sub, bat, best)))) solution = creator.Bat(list(map(operator.add, bat , bat.velocity)))""" rand = random.uniform(0,1)#numpy.random.random_sample() #print("best random: ",rand,"the rate copared to it: ",bat.rate) if rand > bat.rate: #random walk using equation 5 from the paper on the global best solution. eps = random.uniform(-1,1) eps_A = eps * A solution = numpy.add(best,eps_A)#( _ + eps_A for _ in best) solution = creator.Bat(list(solution)) """print("-around the best-") print("eps: ",eps) print("A: ",A) print("solution: ",solution)""" else: bat.frequency = [[fmin + (fmax - fmin) * random.uniform(0,1) for _ in range(dim)] for _ in range(size)] #equation 2 from the paper. dis = numpy.multiply(numpy.subtract(bat, best), bat.frequency) bat.velocity = numpy.add(bat.velocity, dis) #eauqtion 3 from the paper. solution = creator.Bat(numpy.add(bat , bat.velocity)) #equation 4 from the paper """print("-random walk-") print("frequency: ",bat.frequency) print("velocity: ",bat.velocity) print("solution: ",solution)""" for _ in solution: #making sure the bat doesn't go too far and stays in the objective function domain. for __ in range(len(_)): if _[__] > bmax: _[__] = bmax if _[__] < bmin: _[__] = bmin #print("solution after boudning: ",solution) solution.fitness = toolbox.evaluate(data = histogram , bat = solution)#calculation the fitness of the Bat. rand = random.uniform(0,1)#numpy.random.random_sample() #print("best random: ",rand,"the loudness copared to it: ",bat.loudness,rand < bat.loudness) if bat.fitness > solution.fitness and rand < bat.loudness: #asserting the solution only if the solution is better and the bat iss too loud. #print("-asserting the solution- rand:",rand,"loudness:",bat.loudness) bat[:] = solution bat.fitness = solution.fitness bat.loudness = alpha * bat.loudness bat.rate = bat.init_rate * (1 - math.exp(-alpha * (G+1))) #print("new loudness",bat.loudness) #print("new rate",bat.rate) if bat.best.fitness > bat.fitness: #updating the personal best. #print("pbest updated") bat.best = creator.Bat(bat) bat.best.fitness = bat.fitness if best.fitness > solution.fitness: #updating the global best. #print("best updated") best[:] = creator.Bat(solution) best.fitness = solution.fitness
def generate(bmin, bmax, dim, size): bat = creator.Bat([random.uniform(bmin,bmax), random.uniform(bmin,numpy.amax(histogram))] for _ in range(size)) bat.init_rate = random.uniform(0,0.95) bat.rate = bat.init_rate bat.loudness = 1 bat.velocity = [[0,0] for _ in range(size)] bat.fitness = toolbox.evaluate(data = histogram , bat = bat) bat.best = creator.Bat(bat) bat.best.fitness = bat.fitness return bat
def updateBat(bat, best, fmin, fmax, G, A, m, histogram, size, dim, alpha=0.9, gamma=0.9): bat.frequency = fmin + (fmax - fmin) * numpy.random.uniform(0, 1) dis = numpy.multiply(numpy.subtract(bat, best), bat.frequency) # eauqtion 3 from the paper. bat.velocity = list(numpy.add(bat.velocity, dis)) # equation 4 from the paper solution = creator.Bat(list(numpy.add(bat, bat.velocity))) rand = numpy.random.random_sample() # random walk using equation 5 from the paper on the global best solution. if rand > bat.rate: solution = numpy.add(best, [[random.uniform(-1, 1) * A for _ in range(dim)] for _ in range(size)]) #solution = numpy.add(best , numpy.multiply( A ,[random.gauss(0,1), random.gauss(0,1)])) solution = creator.Bat(list(solution)) for i in range(len(solution)): for _, x in enumerate(solution[i]): if solution[i][0] > 255: print("top changed", solution[i][0]) solution[i][0] = 255 print("top changed") if x < 0: solution[i][_] = 0 print("x") # calculation the fitness of the Bat. solution.fitness = toolbox.evaluate( solution, m, histogram=histogram) # calculation the fitness of the Bat. rand = numpy.random.random_sample() # asserting the solution only if the solution is better and the bat is too loud. if bat.fitness >= solution.fitness and rand < bat.loudness: bat[:] = solution bat.fitness = solution.fitness bat.loudness = alpha * bat.loudness bat.rate = bat.init_rate * (1 - math.exp(-gamma * (G + 1))) if best.fitness > solution.fitness: # updating the global best. best[:] = creator.Bat(solution) best.fitness = solution.fitness
def main(): Population = toolbox.population(n=20) """print(Population) print(" ")""" GEN = 1500 best = None A = 0 for bat in Population: #finding the initial global best if not best or best.fitness > bat.fitness: best = creator.Bat(bat) best.fitness = bat.fitness A += bat.loudness """print("the bat: ",bat , "the fitness: ", bat.fitness,"the loudness: ",bat.loudness,"the rate: ",bat.rate,"initrate",bat.init_rate, ) print(" ")""" mean_A = A / len(Population) #calculating the average loudness to use in equation 5 of the paper. for G in range(GEN): #computing the BA GEN times A=0 """print("the best:", best, "fitness: ", best.fitness) print(" ") print("average loudness:",mean_A)""" for bat in Population: #updating the Bat postion one by one and opdating the Global best as well. toolbox.update(bat, best = best, A = mean_A, G = G) A += bat.loudness """print("the bat: ",bat , "the fitness: ", bat.fitness,"the loudness: ",bat.loudness,"the rate: ",bat.rate,"initrate",bat.init_rate, ) print(" ")""" mean_A = A / len(Population) #calculating the new average loudness """for bat in Population: if best.fitness > bat.fitness: best = creator.Bat(bat) best.fitness = bat.fitness""" print(best, best.fitness)
def updateBat(bat, best, fmin, fmax, G, A, m, histogram, size, dim, alpha=0.9, gamma=0.9): bat.frequency = fmin + (fmax - fmin) * numpy.random.uniform(0, 1) dis = numpy.multiply(numpy.subtract(bat, best), bat.frequency) bat.velocity = list(numpy.add(bat.velocity, dis)) #eauqtion 3 from the paper. solution = creator.Bat(list(numpy.add( bat, bat.velocity))) #equation 4 from the paper rand = numpy.random.random_sample() if rand > bat.rate: #random walk using equation 5 from the paper on the global best solution. solution = numpy.add(best, [[random.uniform(-1, 1) * A for _ in range(dim)] for _ in range(size)]) #solution = numpy.add(best , numpy.multiply( A ,[random.gauss(0,1), random.gauss(0,1)])) solution = creator.Bat(list(solution)) """ for _ in range(len(solution)): #making sure the bat doesn't go too far and stays in the objective function domain. if solution[_] > bmax: solution[_] = bmax if solution[_] < bmin: solution[_] = bmin """ solution.fitness = toolbox.evaluate( solution, m, histogram=histogram) #calculation the fitness of the Bat. rand = numpy.random.random_sample() if bat.fitness >= solution.fitness and rand < bat.loudness: #asserting the solution only if the solution is better and the bat is too loud. bat[:] = solution bat.fitness = solution.fitness bat.loudness = alpha * bat.loudness bat.rate = bat.init_rate * (1 - math.exp(-gamma * (G + 1))) if best.fitness > solution.fitness: #updating the global best. best[:] = creator.Bat(solution) best.fitness = solution.fitness
def generate(bmin, bmax, m, histogram, size, init_R, init_A): bat = creator.Bat([[ random.uniform(bmin, bmax), random.uniform(bmin, numpy.amax(histogram)) ] for _ in range(size)]) bat.init_rate = random.uniform(0, init_R) bat.rate = bat.init_rate bat.loudness = random.uniform(init_A, 2) bat.velocity = [[0, 0] for _ in range(size)] bat.fitness = toolbox.evaluate(bat, m, histogram=histogram) return bat