1. Overview
As an optimization algorithm, particle swarm has been applied in many fields. The so-called optimization, I understand, is to find a good solution to a problem, there are many current optimization algorithms, such as Ant Colony Algorithm, genetic algorithm and so on. Compared with these algorithms, particle swarm is simpler and faster.
2. Algorithm Description
For example, if the minimum value is obtained, you can use mathematical methods to find that the minimum value of a vector is 0, but for more complex function expressions, it is complex to use mathematical methods to solve the problem. In practical application, it is not necessarily required to obtain its precise solution. As long as it is a precise solution that satisfies sufficient precision, at this time, it is very convenient to solve the problem through certain optimization algorithms.
The idea of particle swarm algorithms comes from the process of bird prey in real life. Assume that in an n-dimensional space, a group of birds (m only) are prey on each other. Food is located at a certain point in the n-dimensional space. For the I-bird, there are two vectors to describe, one is the position vector of the bird, and the second is the speed of the bird (I = 1, 2... m ). If a bird can determine the quality of a position, the so-called "quality" means that it is closer to the food or farther away. In the process of bird prey, the bird determines its own speed based on its own experience and the positions of other birds in the bird group. Based on the current position and speed, you can get the position of the next moment, in this way, each bird constantly updates its speed position to learn from itself and the birds, and finally finds food or points close enough to the food. The expression for the update speed and position is as follows.
Update speed:
The corresponding python implementation is as follows:
[Python]
Import random
Import copy
Birds = int (raw_input ('enter count of bird :'))
Xcount = int (raw_input ('enter count of x :'))
Pos = []
Speed = []
Bestpos = []
Birdsbestpos = []
W = 0.8
C1 = 2
C2 = 2
R1 = 0.6
R2 = 0.3
For I in range (birds ):
Pos. append ([])
Speed. append ([])
Bestpos. append ([])
Def GenerateRandVec (list ):
For I in range (xcount ):
List. append (random. randrange (1,100 ))
Def CalDis (list ):
Dis = 0.0
For I in list:
Dis + = I ** 2
Return dis
For I in range (birds): # initial all birds 'pos, speed
GenerateRandVec (pos [I])
GenerateRandVec (speed [I])
Bestpos [I] = copy. deepcopy (pos [I])
Def FindBirdsMostPos ():
Best = CalDis (bestpos [0])
Index = 0
For I in range (birds ):
Temp = CalDis (bestpos [I])
If temp <best:
Best = temp
Index = I
Return bestpos [index]
Birdsbestpos = FindBirdsMostPos () # initial birdsbestpos
Def NumMulVec (num, list): # result is in list
For I in range (len (list )):
List [I] * = num
Return list
Def VecSubVec (list1, list2): # result is in list1
For I in range (len (list1 )):
List1 [I]-= list2 [I]
Return list1
Def VecAddVec (list1, list2): # result is in list1
For I in range (len (list1 )):
List1 [I] + = list2 [I]
Return list1
Def UpdateSpeed ():
# Global speed
For I in range (birds ):
Temp1 = NumMulVec (w, speed [I] [:])
Temp2 = VecSubVec (bestpos [I] [:], pos [I])
Temp2 = NumMulVec (c1 * r1, temp2 [:])
Temp1 = VecAddVec (temp1 [:], temp2)
Temp2 = VecSubVec (birdsbestpos [:], pos [I])
Temp2 = NumMulVec (c2 * r2, temp2 [:])
Speed [I] = VecAddVec (temp1, temp2)
Def UpdatePos ():
Global bestpos, birdsbestpos
For I in range (birds ):
VecAddVec (pos [I], speed [I])
If CalDis (pos [I]) <CalDis (bestpos [I]):
Bestpos [I] = copy. deepcopy (pos [I])
Birdsbestpos = FindBirdsMostPos ()
For I in range (100 ):
# Print birdsbestpos
Print CalDis (birdsbestpos)
UpdateSpeed ()
UpdatePos ()
Raw_input ()
If the number of birds in the bird group in the search space is 30, and the number of problem x is 5, the results of 100 iterations are as follows, we can see that the final value is very close to 0, which is the optimal solution.
Enter count of bird: 30
Enter count of x: 5
6300.0
6300.0
5286.56
253.7792
253.7792
169.750784
169.750784
29.27174656
29.27174656
14.3572668416
14.3572668416
10.7095755489
10.7095755489
10.4166336974
10.4166336974
10.3952346067
10.3952346067
10.38162211
10.38162211
10.38162211
10.38162211
10.38162211
10.38162211
10.3816078435
10.3816078435
10.3815990193
10.3815990193
10.3815990193
10.3815990193
10.3815990193
10.3815990193
10.3815990038
8.61600314002
6.75814610104
0.697031173541
0.697031173541
0.586257672539
0.319653330666
0.308201304448
0.277551198357
0.152964935388
0.11330877896
0.0897094795931
0.0849797134585
0.0824510053969
0.0824510053969
0.0817642679444
0.0293278926344
0.0101946030255
0.0101946030255
0.00880640894843
0.00517872172034
0.00517872172034
0.00517872172034
0.00517872172034
0.00517872172034
0.00514217487311
0.00511832820187
0.00510609755302
0.00510609755302
0.00510609755302
0.0039233096712
0.00319253923712
0.00142224947992
0.000847531318472
0.000682710187325
0.000126289737569
0.000126289737569
0.000109415873528
0.000109415873528
0.000106080598721
0.000106080598721
0.000105801137376
0.000105801137376
0.000105654750511
0.000105654750511
0.000105654750511
0.000105654750511
0.000105654750511
0.000105654750511
0.000105653808938
0.000105653808938
0.000105653808938
7.63547737464e-05
2.56599311271e-05
6.88805040513e-06
6.88805040513e-06
2.93921399366e-07
2.93921399366e-07
2.93921399366e-07
1.65997040259e-07
1.49983822855e-07
1.45620647032e-07
1.30809105417e-07
1.30631326401e-07
1.29726054702e-07
1.2360770395e-07
1.2360770395e-07
1.2360770395e-07
1.22.167030689e-07