Particle swarm optimization algorithm

Source: Internet
Author: User

2011-12-05 19:49:55

Tags: Swarm intelligent Leisure ant colony optimization pso optimization particle swarm optimization original works, allow reprint, please be sure to use hyperlinks in the form of the original source of the article, author information and this Statement. otherwise, the legal liability will be investigated. http://nxlhero.blog.51cto.com/962631/734212

Particle swarm optimization (pso) algorithm belongs to swarm intelligence (swarm Intelligence) optimization algorithm. Group intelligence is divided into two kinds, one is particle swarm optimization, the other is ant colony Optimization.

Swarm Intelligence Concept

Suppose you and your friend are looking for treasure, and everyone has a detector that knows the distance from the treasure to the Detector. You guys are looking, everyone can share the information, and you can have your teammates ' vision when you play dota, you can know the distance of everyone else from the treasure, so that you see who is nearest to the treasure, to whom to close, this will make you find the treasure of the opportunity to become larger, and this method than you single-looking more quickly.

This is a simple example of a group behavior (swarm behavior) in which each individual interacts, using a method more efficient than a single individual to solve a global goal. A group (swarm) can be defined as a collection of structures or agents of an interaction, and in the study of swarm intelligence computation, the individual groups of groups include ants, termites, bees, wasps, fish, birds, etc. In these groups, individuals are structurally simple, and their collective behavior can become quite complex. The researchers found that the Ant's transport route between the Bird's Nest and the food, no matter how random the beginning, eventually found a shortest path.

Particle swarm optimization Concept

Particle swarm optimization (particle swarm Optimization,pso) algorithm is based on group search, which is based on the simulation of bird Society. The initial meaning of the swarm concept is to simulate the graceful and unpredictable dance movements of birds through graphs, discovering the ability of the birds to control simultaneous flight and to suddenly change the direction of flight and re-formation in the best formation. This concept has been included in a simple and efficient optimization algorithm.

In pso, individuals called "particles" (particle) "flow" through the hyper-dimensional search Space. The change in the position of the particle in the search space is based on the Individual's success in exceeding the social psychological intentions of other individuals, so the change of particles in a group is affected by the experience or knowledge of its neighboring particles (individuals). The search behavior of a particle is affected by the search behavior of other particles in the Group. therefore, particle swarm optimization is a symbiotic cooperative algorithm.

Algorithm description

Let's describe it in one image: 5 birds foraging, each bird knows its distance from food, and shares this information with other BIRDS.

At first, 5 birds scattered in different places, assuming that no bird updates its speed and direction every second, the question is how to update it?

Each bird notes its nearest position to the food, called pbest,pbest0,pbest1,.. Pbest of 5 birds, Choose a gbest from here, the best in the Group.

Every second, the bird updates its own speed v (here is the vector),

V_new = v_old + c1*rand () * (pbest-pcurrent) +c2*rand () * (gbest-pcurrent)

C1,C2 generally 2,rand () generates 0~1 random Numbers.

Then fly at this speed for 1 seconds, update again, and eventually get closer to the Food.

The following pseudo-code from the Baidu Encyclopedia

The pseudo code of the program is as follows

For each particle

____initialize particle

END

Do

____for each particle

________calculate Fitness Value

________if The fitness value is better than the best fitness value (pbest) in history

____________set current value as the new Pbest

____end

____choose the particle with the best fitness value of the the particles as the gbest

____for each particle

________calculate particle velocity According equation (a)

________update particle position according equation (b)

____end

While maximum iterations or minimum error criteria are not attained

The speed of each particle is limited to a maximum speed vmax, and if a dimension is updated faster than the user-defined vmax, the speed of this dimension is limited to vmax.

Program examples

Calculates the minimum value of f (x) = x*x-20x + 100 and the x when the minimum value is taken

  1. #include <iostream>
  2. #include <cmath>
  3. #include <cstdlib>
  4. Using namespace std;
  5. #define C1 2
  6. #define C2 2
  7. #define VMAX 5.0
  8. #define MAX_ITERATIONS 100
  9. Float RAND01 ()
  10. {
  11. return (float) (rand ()/(double) rand_max);
  12. }
  13. struct particle{
  14. float current;
  15. float pbest;
  16. };
  17. Float Fitness (float x)
  18. {
  19. return x*x-20*x + 100;
  20. }
  21. float gbest = 10000;
  22. struct particle p[5];
  23. float v[5] = {0};
  24. void Init_particles ()
  25. {
  26. int i;
  27. for (i = 0; i < 5; i++)
  28. {
  29. P[i].current = -2+i;
  30. P[i].pbest = p[i].current;
  31. }
  32. }
  33. void Find_gbest ()
  34. {
  35. int i;
  36. for (i = 0; i < 5; i++)
  37. {
  38. if (fitness (gbest) > Fitness (p[i].current))
  39. Gbest = p[i].current;
  40. }
  41. }
  42. void Adjust_v ()
  43. {
  44. int i;
  45. for (i = 0; i < 5; i++)
  46. {
  47. v[i] = v[i] + c1*rand01 () * (p[i].pbest-p[i].current) + c2*rand01 () * (gbest-p[i].current);
  48. if (v[i] > VMAX)
  49. v[i] = VMAX;
  50. }
  51. }
  52. void PSO ()
  53. {
  54. int i,iter_num;
  55. Iter_num = 1;
  56. while (iter_num < Max_iterations)
  57. {
  58. /*for (i = 0; i < 5; i++)
  59. {
  60. cout << "p" <<i<< ": current" <<p[i].current<< "pbest" <<p[i].pbest<<endl;
  61. }
  62. cout << "gbest:" <<gbest<<endl;
  63. cout <<endl;
  64. GetChar (); */
  65. for (i = 0; i < 5; i++)
  66. {
  67. if (fitness (p[i].current) < fitness (p[i].pbest))
  68. P[i].pbest = p[i].current;
  69. }
  70. Find_gbest ();
  71. Adjust_v ();
  72. for (i = 0; i < 5; i++)
  73. P[i].current + = v[i];
  74. Iter_num + +;
  75. }
  76. }
  77. int main ()
  78. {
  79. Init_particles ();
  80. PSO ();
  81. printf ("after%d iterations,gbest is%f\n", max_iterations,gbest);
  82. return 0;
  83. }

Run results

Here is the result after each iteration, and you can see that after 5 iterations, the results are good.

  1. After 1 iterations
  2. P0:current-2 pbest-2
  3. P1:current-1 pbest-1
  4. P2:current 0 Pbest 0
  5. P3:current 1 Pbest 1
  6. P4:current 2 Pbest 2
  7. gbest:10000
  8. After 2 iterations
  9. P0:current 1.15506 pbest-2
  10. P1:current 3.79064 pbest-1
  11. P2:current 0.790205 pbest 0
  12. P3:current 2.53646 pbest 1
  13. P4:current 2 Pbest 2
  14. Gbest:2
  15. After 3 iterations
  16. P0:current 6.15506 pbest 1.15506
  17. P1:current 8.58128 pbest 3.79064
  18. P2:current 5.79021 pbest 0.790205
  19. P3:current 5.87216 pbest 2.53646
  20. P4:current 4.17373 pbest 2
  21. gbest:3.79064
  22. After 4 iterations
  23. P0:current 11.1551 pbest 6.15506
  24. P1:current 13.3719 pbest 8.58128
  25. P2:current 10.7902 pbest 5.79021
  26. P3:current 9.79741 pbest 5.87216
  27. P4:current 8.27141 pbest 4.17373
  28. gbest:8.58128
  29. After 5 iterations
  30. P0:current 13.8766 pbest 11.1551
  31. P1:current 10.1764 pbest 8.58128
  32. P2:current 14.7492 pbest 10.7902
  33. P3:current 13.7227 pbest 9.79741
  34. P4:current 13.2714 pbest 8.27141
  35. gbest:9.79741
  36. After 6 iterations
  37. P0:current 8.03327 pbest 11.1551
  38. P1:current 6.98078 pbest 10.1764
  39. P2:current 13.2414 pbest 10.7902
  40. P3:current 4.78856 pbest 9.79741
  41. P4:current 11.6974 pbest 8.27141
  42. gbest:10.1764
  43. After 7 iterations
  44. P0:current 5.84287 pbest 11.1551
  45. P1:current 9.25245 pbest 10.1764
  46. P2:current 5.23059 pbest 10.7902
  47. p3:current-3.28694 pbest 9.79741
  48. P4:current 9.93147 pbest 11.6974
  49. gbest:10.1764

This article is from the "niu blog" blog, make sure to keep this source http://nxlhero.blog.51cto.com/962631/734212

Particle swarm optimization algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.