C language standard PSO algorithm

Source: Internet
Author: User

Introduction and implementation of basic PSO: http://blog.csdn.net/fovwin/article/category/1256709

Compared with the basic PSO, the standard PSO is added with the inertial weight coefficient w, which represents the trade-off between the search capability of the global space and the local convergence speed of the particle swarm.

That is to say, if the inertia weight W is larger, the first item of the velocity update formula occupies a large proportion, that is, the inertia item of the velocity update formula. Particles are not restricted and can be rushed. Not subject to secular constraints, it is not easy to change to "vulgar" (fall into a local minimization point); If the inertia weight W is smaller, the latter two items of the speed update formula occupy the proportion, that is, the historical extreme values of an individual and the global optimal solution are relatively large. Particles are constrained by their own experience and secular society. If you listen to "Mom", your life goes smoothly, but you will know the path of your life when you are 20 years old, that is, it is easy to fall into a local minimization point-premature convergence and early maturity.

Therefore, we usually set a large value of W at the beginning to make it fully search for the global space (just as we were not obedient when we were young, and sometimes our mom and dad said this would not work, we will try again on our own, huh, huh). By the second half of particle iteration, we can set w to a smaller value, which can increase the convergence speed (most of the rest of our lives are based on experience ).

Set W in this article as follows:

/* Inertial weight coefficient */# define w_start 1.4 # define w_end0.4 # define w_fun (w_start-(W_START-W_END) * POW (double) cur_n/ite_n, 2 ))

Of course, we can set some other formulas, but it shows a general downward trend. We can also set them using fuzzy control rules (to achieve the goal in the next step ~~~

As for the effect, it is faster than the basic to get to the optimal solution ~~~ However, wood has PP, and 1.x versions are available ~~~

Compared with Verson 0.0, this version reduces several functions. In the initial stage of the first version, the version 1.0 code is more concise to learn the principles. C and PSO. H is independent. After expansion, define is used to set your functions to be optimized (some of them are pretty nice to try a variety of weird functions in the Next Step), but they need to be improved, however, the framework of the extended fitness function is added ~~~ Merge the values of P, X, and X into one ~~~

Main. c

# Include "PSO. H "# include" stdio. H "int main (INT argc, const char * argv []) {cur_n = 0; randinitofswarm (); // initialize the particle swarm while (cur_n ++! = Ite_n) {updatepandgbest (); // update the historical optimal solution of an individual P and the global optimal solution gbestupdateofvandx (); // the speed and position are updated, that is, flying} getchar (); Return 0 ;}

PSO. h

# Ifndef _ pso_h _ # DEFINE _ pso_h _/* for selection of various fitness functions, set the value to 1 if you want to use the function, but only one of them can be 1 */# define test 0 # define goldstein_price0 # define Schaffer 1 # define hansen0 # define needle0 # define dim 2 // Particle Dimension # define pnum 20/Population scale # define ite_n 50 // maximum number of iterations int cur_n; // current iterations/* inertial Weight Function */# define w_start 1.4 # define w_end0.4 # define w_fun (w_start-(W_START-W_END) * POW (double) cur_n/ite_n, 2)/* individual and population structures */struct particle {Double X [dim]; Double P [dim]; Double V [dim]; double fitness;} particle; struct swarm {struct particle [pnum]; int gbestindex; double gbest [dim]; double C1; double C2; double xup [dim]; double xdown [dim]; double Vmax [dim];} swarm;/* Yes, only three of them are needed. This makes it easier to understand. */void randinitofswarm (void); void updateofvandx (void ); void updatepandgbest (void); # endif

PSO. c

# Include "stdio. H "# include" stdlib. H "# include" time. H "# include" math. H "# include" PSO. H "// initialize the population void randinitofswarm (void) {int I, j; // learning factor C1, c2swarm. c1 = 2.0; swarm. c2 = 2.0; For (j = 0; j <dim; j ++) {swarm. xdown [J] =-10; // the search space range is swarm. xup [J] = 10; swarm. vmax [J] = 0.1; // maximum particle fly speed} srand (unsigned) Time (null); for (I = 0; I <pnum; I ++) {for (j = 0; j <dim; j ++) {swarm. particle [I]. X [J] = rand ()/(double) RA Nd_max * (Swarm. xup [J]-swarm. xdown [J]) + swarm. xdown [J]; //-xdown ~ Xupswarm. particle [I]. V [J] = rand ()/(double) rand_max * swarm. vmax [J] * 2-swarm. vmax [J]; //-Vmax ~ Vmax }}/ * calculate the fitness of the fitness function, to be further improved */static double computafitness (Double X []) {/* okmin-F) = 3 */# If testreturn X [0] * X [0] + X [1] * X [1] + 3; # endif/* Min-F (0, -1) = 3.x, y-(-) */# If goldstein_pricereturn (1 + POW (X [0] + X [1] +) * (19-14 * X [0] + 3 * POW (X [0], 2) -14 * X [1] + 6 * X [0] * X [1] + 3 * POW (X [1], 2 )) * (30 + POW (2 * X [0]-3 * X [1]), 2) * \ (18-32 * X [0] + 12 * POW (X [0], 2) + 48 * X [1]-36 * X [0] * X [1] + 27 * POW (X [1], 2 )))); # endif/* Min-F (0, 0) =-1.x, y-(-4) */# If Schaffer // function: schaffer's f6return-1 * (0.5-(sin (SQRT (X [0] * X [0] + X [1] * X [1]) * \ sin (SQRT (X [0] * X [0] + X [1] * X [1])-0.5) /POW (1 + 0.001 * (X [0] * X [0] + X [1] * X [1]), 2 )); # endif/* this function has 760 local extreme points. Min-f (x, y) =-176.541793.x, Y-(-7.589893) (-7.708314,-7.589893), (-1.425128,-7.5898893) (-4.858057 ), (-1.306708,-7.708314) (-1.306707,-1.425128), (-1.306708, 4.858057) (4.976478,-7.708314), (4.976478,-1.425128) (4.976478, 4.858057) */# If hansenint I; double temp1 = 0, temp2 = 0; double hansenf = 0; for (I = 1; I <= 5; I ++) {temp1 + = I * Cos (I-1) * X [0] + I); temp2 + = I * Cos (I-1) * X [1] + I );} hansenf =-1 * temp1 * temp2; return hansenf; # endif/* Min-F (3600) =-5.12, X, Y-(-5.12) the global optimal solution of this problem is surrounded by the worst solution. The local extreme points are: (-5.12, 5.12), (-5.12,-5.12) (5.12,-5.12), (5.12, 5.12) F =-2748.78 */# If needlereturn-1 * POW (3/(0.05 + POW (X [0) + POW (X [1]-1, 2), 2); # endif} // update V and xvoid updateofvandx (void) {int I, j; srand (unsigned) time (null); for (I = 0; I <pnum; I ++) {for (j = 0; j <dim; j ++) swarm. particle [I]. V [J] = w_fun * swarm. particle [I]. V [J] + rand ()/(double) rand_max * swarm. c1 * (swarm. particle [I]. P [J]-swarm. particle [I]. X [J]) + rand ()/(double) rand_max * swarm. c2 * (swarm. gbest [J]-swarm. particle [I]. X [J]); For (j = 0; j <dim; j ++) {If (swarm. particle [I]. V [J]> swarm. vmax [J]) swarm. particle [I]. V [J] = swarm. vmax [J]; If (swarm. particle [I]. V [J] <-swarm. vmax [J]) swarm. particle [I]. V [J] =-swarm. vmax [J] ;}for (j = 0; j <dim; j ++) {swarm. particle [I]. X [J] + = swarm. particle [I]. V [J]; If (swarm. particle [I]. X [J]> swarm. xup [J]) swarm. particle [I]. X [J] = swarm. xup [J]; If (swarm. particle [I]. X [J] <swarm. xdown [J]) swarm. particle [I]. X [J] = swarm. xdown [J] ;}}/ * update the individual extreme values (P) and Global extreme values (gbest) */void updatepandgbest (void) {int I, J; // update of P if the X is bigger than current pfor (I = 0; I <pnum; I ++) {If (swarm. particle [I]. fitness <computafitness (swarm. particle [I]. p) {for (j = 0; j <dim; j ++) {swarm. particle [I]. P [J] = swarm. particle [I]. X [J] ;}}// update of gbestfor (I = 0; I <pnum; I ++) if (computafitness (swarm. particle [I]. p) <computafitness (swarm. particle [swarm. gbestindex]. p) swarm. gbestindex = I; for (j = 0; j <dim; j ++) {swarm. gbest [J] = swarm. particle [swarm. gbestindex]. P [J];} printf ("the % DTH iteraction. \ n ", cur_n); printf (" gbestindex: % d \ n ", swarm. gbestindex); printf ("gbest:"); For (j = 0; j <dim; j ++) {printf ("%. 4f, ", swarm. gbest [J]);} printf ("\ n"); printf ("Fitness of gbest: % F \ n", computafitness (swarm. particle [swarm. gbestindex]. p ));}

Version 1.0 standard PSO

Related Article

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.