In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of "how Python and Matlab implement bat algorithm". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to implement bat algorithm in Python and Matlab" can help you solve the problem.
1 preface
Bat algorithm is a heuristic search algorithm based on swarm intelligence proposed by Professor Yang in 2010. it is an effective method to search the global optimal solution. The algorithm is based on iterative optimization, initializes into a set of random solutions, then iteratively searches for the optimal solution, and generates a local new solution by random flight around the optimal solution to enhance the local search speed. The algorithm has the advantages of simple implementation and few parameters.
The algorithm is mainly used to optimize the objective function, which is based on the characteristics that the bat population uses the generated sound waves to search for prey and control flight direction. A bat is used as the basic unit, and each bat has a fitness value to optimize the function solution space. Each bat can adjust the loudness and frequency of sound waves to search the space, so that the activity of the whole population gradually changes from disorder to order. However, the bat algorithm is easy to fall into the local extremum at the end of optimization. In this paper, the variable speed weight factor correction coefficient (similar to the adaptive process of particle swarm parameter adjustment) is introduced to avoid the dilemma of local extremum as much as possible, so as to achieve the effect of global optimization.
2 detailed explanation of the principle of bat algorithm
First of all, the random position of the bat is generated within the range of independent variables.
Then each bat moves somewhere around, and the flight changes direction by sound wave feedback, and the movement around is random, and there is a certain frequency when the bat moves to a certain position at the next moment. So this paper adds sound waves and frequency to the motion formula. The displacement at each moment is regarded as a flight, and the distance of each flight is very short (the distance reflects the search accuracy).
The initial position of each bat is random, and at the time of the initial position, one of the bats corresponds to the maximum value of the function. The algorithm will record the position at first, and then all bats will gradually move closer to that position. The flight direction is roughly in the current maximum direction (that is, the bat appears more frequently in that direction), but it is also random in the flight direction, which is equivalent to searching the past step by step. In the process of bat flight, the flight trend of the population will not be changed because of the maximum position of the current flight, so that the algorithm can be avoided from falling into the local extremum as far as possible.
The algorithm records the maximum value of the position of the population every time it flies, and finally finds out the maximum value in the record.
The algorithm has two parameters that can affect the final result: the number of populations and the number of flights. Among them, the influence of population size is the greatest.
3 detailed steps
3.1 initialize relevant parameters
The position of the bat is Xi, the flight speed is Vi, the sound loudness is Ai, the frequency yi range, and the objective function is:
3.2 change the solution generated by the pulse frequency and change the bat position and flight speed
The position and flight speed of bat I at t-1 are expressed as and, respectively, and the optimal position found by the population is
. Then the prey is searched with different sounds according to itself, and the position xi and flight speed v (I) are adjusted by receiving feedback. The speed change formula of its flight is as follows:
W (t) is the inertia weight factor of time variable speed, and its function is to make the early search of bats provide reference for the later search, with wmax being the maximum value of w (t) and wmin being the minimum value of w (t). Generally, 2min Tmax is the maximum number of iterations.
Y (I) is a random number whose frequency satisfies normal uniform distribution, β is a random variable, and At the start of the operation, the bats are randomly assigned frequencies.
In order to control the position of the bat within the range of independent variables, this paper sets a boundary rule for this situation: if the position of the next motion is beyond the range of independent variables, then the position of the next flight is the position projected on the boundary.
3.3 search for local optimal solution
3.4 A number of new solutions are generated by multiple flights of bats, and a global search is conducted, if the new solutions are obtained.
Then accept the solution.
3.5 arrange the positions of all bats and find out the current optimal value and the corresponding position
3.6 set the current optimal solution to, and then make all bats continue to move to the next moment, and return to step 2 to recalculate.
3.7 time ends, output: optimal solution
4 Python implementation code # = import related library = import numpy as npfrom numpy.random import random as rand # = parameter setting = # objfun: objective function # N_pop: population size, usually 10 to 4 cycles N_gen: iterations # A: loudness (constant or decreasing) # r: pulse rate (constant or decreasing) # this frequency range determines the range # if necessary These values should be changed # Qmin: minimum frequency # Qmax: maximum frequency # d: dimension # lower: lower bound # upper: upper bound def bat_algorithm (objfun, N_pop=20, N_gen=1000, Av0.5, rust 0.5, Qmin=0, Qmax=2, dong10, lower=-2, upper=2): N_iter = 0 # Total number of function evaluations # = speed upper and lower limit = Lower_bound = lower * np.ones ((1) D)) Upper_bound = upper * np.ones ((1mind)) Q = np.zeros ((N_pop, 1)) # Frequency v = np.zeros ((N_pop, d)) # Velocity S = np.zeros ((N_pop, d)) # = initialization population, initial solution = # Sol = np.random.uniform (Lower_bound, Upper_bound, (N_pop) D) # Fitness = objfun (Sol) Sol = np.zeros ((N_pop, d)) Fitness = np.zeros ((N_pop, 1)) for i in range (N_pop): Sol [I] = np.random.uniform (Lower_bound, Upper_bound, (1) D) Fitness [I] = objfun (Soli]) # = find the initial optimal solution = fmin = min (Fitness) Index = list (Fitness) .index (fmin) best = Sol [Index] # = start iteration = for t in range (N_gen): # = = cycle through all bats / solutions = for i in range (N_pop): # Q [I] = Qmin + (Qmin-Qmax) * np.random.rand Q [I] = np.random.uniform (Qmin Qmax) v [I] = v [I] + (Sol [I]-best) * Q [I] S [I] = Sol [I] + v [I] # = apply simple boundaries / restrictions = Sol [I] = simplebounds (Sol [I], Lower_bound Upper_bound) # Pulse rate if rand () > r: # The factor 0.001 limits the step sizes of random walks S [I] = best + 0.001*np.random.randn (1 D) # = evaluate the new solution = # print (I) Fnew = objfun (S [I]) # = = if the solution is improved Or the sound is not too loud, please update = if (Fnew o S (iPermine 1) = o End if S (iMagne1) l S (iMagne2) = l; end if S (iPerson2)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.