/* * PSO algorithm * Tyler Simon * 7/1/2010 * vi(t+1)=a*vi(t)+b1*r1(Pi1 - xi(t)) + b2r2(pi2 - xi(t)) * xi(t+1)=xi(t)+vi(t+1) * a=1 * b1=b2=2 * r1=r2 random [0,1] */ #include #include /*Function Defs*/ int particle_init(int); int evaluate(int); int update_past(int); int update_neighborhood_best(int,float); int move(int,float); /*Globals*/ typedef struct particle{ double x; /* x location*/ double y; /* y location*/ double v; /* z location*/ double vx; /* velocity */ double vy; /* velocity */ double vz; /* velocity */ }particle; struct particle *particles; struct particle *tmp; struct {double x; double y;}best; int beta1=2; //learning factors, positive acceleration coeffs. int beta2=2; //learning factors, positive acceleration coeffs. double r1,r2; int alpha=1; int main( int argc, char **argv){ int MAX, tstep; int i; if(argc > 2){ MAX=atoi(argv[1]); tstep=atoi(argv[2]); FILE *fp; r1=drand48(); r2=drand48(); particle_init(MAX); best.x=0.8; best.y=0.8; fp=fopen("input.dat", "w"); //fprintf(fp, "#%d\n", MAX); while(tstep--) { update_past(MAX); for (i=0; i