/*-------------------------------------------------------------------------*/ /* Program: ppso.c */ /* This program is parallel particle swarm optimization. */ /*-------------------------------------------------------------------------*/ #include #include #include #define POPSIZE 40 /* Population size *//**********/ #define MAXGENS 2000 /* Maximum number of generations */ #define GA_GAP 20 /**********/ #define GA_GAP2 100 #define GP 4 /**********/ #define BITNO 2 #define NoCopy 20 #define NoCopy2 2 #define NoSeed 50 #define MAXV 100 /* Maximum velocity */ //#define MAXR 100 /* Maximum range */ #define LeftInitR 15 /* left initialization range */ #define RightInitR 30 /* right initialization range */ #define InitWeight 0.9 /* initial inertia weight */ #define FinalWeight 0.4 /* final inertial weight */ #define DIMENSION 30 #define C1 2 #define C2 2 int Generation; /* Current generation number */ int Best[GP]; /* The best particle for each population */ float GlobalBest[GP]; /* the global best fitness for each population */ float GlobalBestParticle[GP][DIMENSION]; /* the global best path for each population */ float TotalBest; float TotalBestParticle[DIMENSION]; float weight; struct genotype /* Each genotype is a number of the population */ { float Particle[DIMENSION]; /* A string of vars makes a genotype, codebook index *//*****/ float Fitness; /* The genotype's fitness */ float BestParticle[DIMENSION]; float BestParticleFitness; /* the best value for the path of each particle */ float Velocity[DIMENSION]; }; struct genotype Population[GP][POPSIZE]; /* The population of genotypes */ /* GA function prototypes */ void init(void); void evaluate(void); float f6(int,int); float sphere(int,int,int); float rosenbrock(int,int,int); float rastrigrin(int,int,int); float griewank(int,int,int); void exchange(void); void exchange2(void); void exchange3(void); void exchange4(void); void exchange5(void); void exchange6(void); void exchange7(void); void exchange8(void); void exchange9(void); void exchange10(void); void exchange20(void); void exchange21(void); void sorting(void); FILE *fptr; int main() { int t; float calculation=0.0; unsigned seed; // printf("\nEnter a seed :"); // scanf("%u",&seed); if((fptr = fopen("test-ros440.txt","w"))==NULL) { exit(1); } for(seed=1;seed0.5) Population[m][i].Velocity[j]=-Population[m][i].Velocity[j];}}} for(m=0;mMAXV) Population[m][i].Velocity[j]=MAXV; else if( Population[m][i].Velocity[j]<-MAXV) Population[m][i].Velocity[j]=-MAXV; Population[m][i].Particle[j]=Population[m][i].Particle[j] +Population[m][i].Velocity[j]; } } } for(m=0;m