Particle Swarm Optimization .*;
- View VB.Net code
- View Java code
- View Python code
- View C++ code:
- Run Javascript example in a new window
Simple Function Example 1
This is a simple example where the algorithm finds three numbers that add up to a target value.
A fully connected neighborhood is used, so all particles can be compared to each other.
This example's simplicity makes it very easy to experiment with. Almost all variables can be modified.
- TARGET - the answer the algorithm is looking for.
- MAX_INPUTS - this number of operands in the expression.
- MAX_PARTICLES - number of particles employed in the test.
- V_MAX - maximum velocity change allowed.
- MAX_EPOCHS - number of iterations for the algorithm.
- START_RANGE_MIN - smallest random number to start operands at.
- START_RANGE_MAX - largest random number to start operands at.
Example Results 1
Easy enough. Find three operands that add up to 50. Ten particles used. V_MAX = 10.
47 + -3 + 8 = 52 27 + 3 + 10 = 40 41 + 6 + 31 = 78 47 + 40 + -4 = 83 41 + -3 + 30 = 68 6 + 14 + 35 = 55 3 + 11 + 36 = 50 47 + 2 + 9 = 58 40 + -3 + 29 = 66 1 + 8 + 11 = 20 epoch number: 21 Particle 6 has achieved target. 3 + 11 + 36 = 50
Example Results 2
Ten operands and 20 particles. V_MAX = 10. The algorithm typically has work alittle harder to find the solution.
5 + -3 + 18 + 12 + 9 + 15 + 16 + 27 + -17 + -1 = 81 11 + 29 + -5 + 15 + -11 + 15 + -9 + 12 + -1 + 6 = 62 -10 + -2 + 5 + 12 + 14 + 21 + -23 + 10 + 5 + -5 = 27 23 + -10 + -3 + 12 + -9 + 1 + 7 + 8 + -42 + -15 = -28 23 + 1 + -9 + 12 + -12 + 23 + -9 + 12 + -1 + 10 = 50 22 + 19 + 36 + 8 + -18 + 3 + 36 + -17 + -1 + 0 = 88 -10 + -3 + 7 + 12 + 15 + 11 + 24 + -7 + 15 + -18 = 46 0 + -16 + -9 + -3 + 9 + 15 + 15 + 22 + 5 + 10 = 48 -10 + 25 + 30 + 12 + -18 + -3 + -9 + -7 + -1 + 10 = 29 0 + 2 + -9 + 12 + -1 + 6 + 26 + 7 + -36 + -17 = -10 -9 + -11 + -4 + 12 + -10 + 15 + -30 + 17 + 14 + -20 = -26 18 + -5 + 21 + -22 + -1 + 5 + 26 + 17 + 23 + -21 = 61 -20 + -17 + -10 + -8 + -1 + 6 + 13 + 14 + 21 + -8 = -10 23 + 1 + -1 + -18 + 7 + 13 + 9 + 18 + 25 + -6 = 71 23 + 25 + -9 + 12 + 26 + -19 + -9 + -7 + -1 + 10 = 51 -13 + 25 + 1 + 7 + 26 + -19 + -9 + 7 + -35 + -16 = -26 -11 + -4 + 21 + 3 + 9 + 20 + 36 + 9 + -15 + -5 = 63 20 + 2 + -14 + 3 + 18 + 21 + -9 + 25 + -1 + 10 = 75 0 + -12 + -9 + 12 + -7 + 4 + 7 + 14 + -1 + 10 = 18 4 + 6 + 34 + -20 + 8 + 23 + -9 + -25 + -18 + 10 = 13 epoch number: 103 Particle 4 has achieved target. 23 + 1 + -9 + 12 + -12 + 23 + -9 + 12 + -1 + 10 = 50
Example Results 3
Ten operands and only three particles. V_MAX = 10. It doesn't always find the solution quickly.
20 + 22 + -10 + -3 + 19 + 7 + 13 + 19 + -21 + -19 = 47 -8 + -11 + -10 + -3 + 4 + 7 + 22 + 7 + 14 + 24 = 46 20 + 22 + 29 + -19 + -12 + -5 + 13 + 3 + 14 + -19 = 46 epoch number: 196 20 + 22 + -10 + -3 + 19 + 7 + 13 + 19 + -21 + -19 = 47 -16 + -19 + -10 + -3 + -4 + 7 + 14 + -1 + 6 + 16 = -10 20 + 22 + 19 + -29 + -22 + -15 + 13 + -7 + 4 + -19 = -14 epoch number: 197 20 + 22 + -10 + -3 + 19 + 7 + 13 + 19 + -21 + -19 = 47 -6 + -9 + -10 + -3 + 6 + 7 + 24 + 9 + 16 + 26 = 60 20 + 22 + 29 + -19 + -12 + -5 + 13 + 3 + 14 + -19 = 46 epoch number: 198 20 + 22 + -10 + -3 + 19 + 7 + 13 + 19 + -21 + -19 = 47 -11 + -14 + -10 + -3 + 1 + 7 + 19 + 4 + 11 + 21 = 25 20 + 22 + 19 + -29 + -22 + -15 + 13 + -7 + 4 + -19 = -14 epoch number: 199 No solution found.
Example Results 4
This time V_MAX is turned way down to 2. Ten operands and ten particles. When the velocity only varies by a small amount, the algorithm doesn't have much breathing room to search in. So it will take a little longer.
4 + -28 + -4 + 4 + 21 + -4 + 16 + -14 + 32 + 35 = 62 3 + 5 + 10 + 0 + -15 + 22 + 16 + 41 + 28 + -3 = 107 -16 + 5 + 5 + 1 + 4 + 22 + 5 + -14 + 28 + 10 = 50 3 + 25 + 5 + -26 + -22 + -16 + -9 + 41 + 13 + 31 = 45 0 + 25 + -43 + 11 + -15 + -3 + 5 + 41 + 28 + -3 = 46 -27 + -20 + 10 + -12 + 20 + 34 + -9 + 41 + 28 + -36 = 29 -16 + 5 + -22 + 1 + 4 + -3 + 16 + 41 + 13 + 10 = 49 7 + 0 + 10 + -26 + -15 + 34 + -18 + 41 + 28 + -20 = 41 -3 + 0 + 5 + 15 + -28 + 22 + 15 + -11 + 28 + -3 = 40 -16 + 5 + 10 + 11 + 20 + 34 + -18 + -30 + -9 + 31 = 38 epoch number: 100 Particle 2 has achieved target. -16 + 5 + 5 + 1 + 4 + 22 + 5 + -14 + 28 + 10 = 50
Example Results 5
All the above tests used START_RANGE_MIN = 140, START_RANGE_MAX = 190, which starts the test off relatively close to the solution. But what if the possible values were in the hundreds of thousands or greater?
Here, START_RANGE_MIN = 1400, START_RANGE_MAX = 1900, while the target remains 50. Tends to take a little longer.
Also, when START_RANGE_MIN and START_RANGE_MAX are on either side of the target, the algorithm finds the target faster, but when both are on the same side of the target, it takes a little longer.
94 + 124 + -207 + -141 + 220 + -46 + 55 + -95 + -46 + 31 = -11 -239 + -20 + 46 + -103 + -51 + 204 + 31 + 93 + 172 + -128 = 5 117 + -87 + -12 + 59 + 125 + 138 + -299 + -95 + 122 + 62 = 130 -108 + 228 + -212 + 36 + -134 + -46 + 38 + 92 + -40 + 207 = 61 -190 + 147 + -99 + -55 + -167 + -113 + -51 + 53 + 185 + 254 = -36 241 + 79 + 138 + -297 + 100 + -301 + -234 + 86 + 359 + -121 = 50 -214 + -142 + 114 + -52 + 42 + 64 + 131 + -114 + 343 + -226 = -54 -112 + -268 + -16 + 48 + -126 + -46 + 206 + -67 + 187 + 256 = 62 -259 + 221 + -171 + -107 + 215 + -175 + 185 + -95 + 107 + 64 = -15 241 + 147 + 218 + -172 + -167 + -46 + -1 + -95 + -70 + -4 = 51 epoch number: 923 Particle 5 has achieved target. 241 + 79 + 138 + -297 + 100 + -301 + -234 + 86 + 359 + -121 = 50
Example Results 6
START_RANGE_MIN = 14000, START_RANGE_MAX = 890000, TARGET = 500000, Ten particles.
I can see this taking a while: 45676 epochs.
-161052 + 299586 + 252113 + 386773 + 490515 + -275387 + -233018 + -53148 + 53262 + -259686 = 499958 -160756 + -112809 + 506706 + -243423 + -56657 + 42534 + -186919 + 248137 + 344973 + 118188 = 499974 365538 + -363874 + 222170 + 341214 + -96356 + 489296 + -233013 + -154501 + -132369 + 61853 = 499958 31407 + 7319 + 130522 + 268635 + 273033 + 399296 + -353579 + -178191 + 169571 + -247989 = 500024 -222206 + -180847 + -57409 + 67128 + -239307 + 209953 + 319737 + 98838 + 193005 + 311108 = 500000 340496 + -394958 + -20484 + -242482 + -125557 + 27155 + 106096 + 129955 + 244919 + 434824 = 499964 491575 + -245527 + -133389 + 269294 + -162783 + -215121 + 229508 + -64292 + 109840 + 220958 = 500063 132599 + 254939 + -244154 + 343303 + 476551 + -296728 + -262981 + -73233 + 25957 + 143746 = 499999 16293 + 145067 + 256656 + -30710 + 442092 + -345151 + 2607 + 126595 + 313753 + 276089 = 1203291 392326 + -99077 + -389817 + -274226 + 185392 + -103308 + -11338 + 203343 + 280190 + 316448 = 499933 epoch number: 45676 Particle 4 has achieved target. -222206 + -180847 + -57409 + 67128 + -239307 + 209953 + 319737 + 98838 + 193005 + 311108 = 500000