⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 arpso.c

📁 一个遗传粒子群算法原代码
💻 C
📖 第 1 页 / 共 2 页
字号:
	double size = maxlimit - minlimit;      	swarm[a].pos[i] = fusionPoint[i] + randval(-0.1,0.1);      }      break;    case 1:      swarm[a].pos[i] = fusionPoint[i] + randval(-fac,fac);//minlimit*fac2,maxlimit*fac2);      break;    case 2:      swarm[a].pos[i] = fusionPoint[i] + randval(-fac2,fac2);//minlimit*fac2,maxlimit*fac2);      break;    case 3:      swarm[a].pos[i] = fusionPoint[i] + randval(-fac3,fac3);//minlimit*fac2,maxlimit*fac2);      break;    }    swarm[a].vel[i] = randval(-vmax,vmax);  }}void setPosAndVel2(int A, int a) {  int i;  double fac3 = getDiversity();  double fac2 = 1/(t+1);  double fac = (double)1-(double)evaluations/(double)tMax;  for(i=0;i<dim;i++) {    swarm[a].pos[i] = swarm[A].pos[i] + randval(-fac3,fac3);//minlimit*fac2,maxlimit*fac2);    swarm[a].vel[i] = randval(-vmax,vmax);  }}/*void fusion(int a, int b){  int i;	if (a < 0 || b < 0 || a >= swarmsize || b >= swarmsize) {//wrong index		printf("One of the particles, %d and %d, has an illegal index\n",a,b);		return;	}	if (a == b) { // may not be equal		printf("A particle cannot fuse with itselves\n");		return;	}	if (!mainParticle(a) || !mainParticle(b)) {// not a main particle		printf("One of the particles, %d and %d, is not a main particle and cannot fuse\n",a,b);		return;	}	// Now:	// 0 <= a,b < swarmsize, a != b and a and b are both main particles	// DO the fusion!!	for(i=0;i<dim;i++)	  fusionPoint[i] = swarm[a].pos[i];	if ((int)randval(0,2)) { // a < b  --  a fuses together with b, BUT a becomes the main particle: it eats b and all its paticles		int i;		if (swarm[b].bestfit < swarm[a].bestfit) {		  swarm[a].bestfit =swarm[b].bestfit;		  for(i=0;i<dim;i++) {		    swarm[a].best[i] = swarm[b].best[i];		    swarm[a].bestvel[i] = swarm[b].bestvel[i];		  }		}		// add the size of b to the size of a; then set the size of b to 0		swarm[a].parSize += swarm[b].parSize;		swarm[b].parSize = 0;		for(i=0;i<swarmsize;i++)			// let all particles previously fused with b now fuse with a			if (swarm[i].fusedWith == b)				swarm[i].fusedWith = a;	} else {// b fuses together with a, BUT b becomes the main particle: it eats a and all its paticles		int i;		if (swarm[a].bestfit < swarm[b].bestfit) {		  swarm[b].bestfit =swarm[a].bestfit;		  for(i=0;i<dim;i++) {		    swarm[b].best[i] = swarm[a].best[i];		    swarm[b].bestvel[i] = swarm[a].bestvel[i];		  }		}		// add the size of a to the size of b; then set the size of a to 0		swarm[b].parSize += swarm[a].parSize;		swarm[a].parSize = 0;		for(i=0;i<swarmsize;i++)			// let all particles previously fused with b now fuse with a			if (swarm[i].fusedWith == a)				swarm[i].fusedWith = b;	}	nParticles--;}void fission(int a){	int i,size1,size2,index2,nFused;	if (a < 0 || a >= swarmsize) {//wrong index		printf("The particle, %d, has an illegal index\n",a);		return;	}	if (!mainParticle(a)) { // not a main particle		printf("The particle, %d, is not a main particle and cannot do fission\n",a);		return;	}	if (swarm[a].parSize < 2) {// fission not possible		printf("Particle is too small to do fission\n");		return;	}	// Now:	// 0 <= a < swarmsize and a is a main particle	// DO the fission!!	size1 = swarm[a].parSize / 2;	size2 = swarm[a].parSize - size1;		// find a particle that previously was fused with a (and which is not a!!)	i = 0;	while ((swarm[i].fusedWith != a || i == a) && i < swarmsize) // checking for inf. loop		i++;	if (i == swarmsize) {		printf("Error:\tEven though particle %d had a size of %d\n\tno other particle could be found that had fused with it.\n\tTherefor no fission\n",a,swarm[a].parSize);		return;	}	index2 = i;	//printf("%d,%d",a,index2);	// changing (size2)-1 particles to belong to particle index2 instead of a	// setting sizes to 0	for(i=0,nFused=0;i<swarmsize && nFused < size2-1;i++) {		if (swarm[i].fusedWith == a && i != index2 && i!= a) {			swarm[i].fusedWith = index2;			swarm[i].parSize = 0;			nFused++;		}	}	if (nFused != size2-1) {		printf("Big error (can't undo!): Not %d particles performed fission (as calculated) but only %d\n",size2,nFused);		return;	}	// setting new sizes	swarm[a].parSize = size1;	swarm[a].fusedWith = a;	swarm[index2].parSize = size2;	swarm[index2].fusedWith = index2;	nParticles++;		setPosAndVel2(a,index2);}void explosion() {  int i,j;  meanPoint(fusionPoint);    //printf("FusionPoint: ");  //for(j=0;j<dim;j++) {  //  printf("%lf, ",fusionPoint[j]);  //}  //  nParticles = swarmsize;  for(i=0;i<swarmsize;i++) {    swarm[i].parSize = 1;    swarm[i].fusedWith = i;    swarm[i].fitness = DBL_MAX;    swarm[i].bestfit = DBL_MAX;    swarm[i].gbestfit = DBL_MAX;    setPosAndVel(i,0);    for(j=0;j<dim;j++) {      // swarm[i].pos[j] = randval(mininitlimit,maxinitlimit);      swarm[i].vel[j] = randval(-vmax,vmax);    }  }  // assignfitn();  //  for(i=0;i<swarmsize;i++) {  //  for(j=0;j<dim;j++)  //    swarm[i].gbest[j] = swarm[i].pos[j];  //  swarm[i].gbestfit = swarm[i].fitness;  //}}void collcontrol() {  int modelNr = 3;  int i1,i2,j,fisP;  updColllist();  switch (modelNr) {  case 1:    // both particles must be main particles and overlap    for(i1=0;i1<swarmsize;i1++) {      for(i2=i1+1;i2<swarmsize;i2++) {	if (mainParticle(i1) && mainParticle(i2) && overlap(i1,i2)) {	  printf("(%d,%d,%d,",100*evaluations/tMax,i1,i2);	  //printf("Fusion(%d,%d): %2d and %2d\n",t,evaluations,i1,i2);	  	  //****** fusion model 1: *******	  // Konstant antal partikler (20 stk.)	  fusion(i1,i2);	  //find fission-particle:	  //  1: partikel med st鴕st afstand til 	  //  swarm-snit-punkt 	  //  2: partikel med ringeste fitness	  //  3: partikel som har klaret sig 	  //  "d錼ligst"	  fisP = findFissionParticle(1);	  fission(fisP);	  printf(")\n");	  for(j=0;j<swarmsize;j++) {	    if (mainParticle(j)) printf("Partikel %d, size %d\n",j,swarm[j].parSize); 	  }		}      }    }    break;  case 2:    //****** fusion model 2: ********    // i mode 1:    // fusion n錼 paritkler st鴇er sammen    // skift til mode 2 n錼 under lowerbound partikler    // i mode 2:        //    if (modeFusion) {      int count = 0,count2,val=(int)randval(0,swarmsize);      //printf("(%d)",val);      for(i1=val;count<swarmsize;i1 = (i1+1)%swarmsize,count++) {	for(i2=(i1+1)%swarmsize,count2=1;count2<swarmsize;i2 = (i2+1)%swarmsize,count2++) {	  //printf("%d %d %d\n",i1,i2,count2);	  //printf("D(%d(%d),%d(%d)) %lf\n",i1,mainParticle(i1),i2,mainParticle(i2),dist(i1,i2));	  if (mainParticle(i1) && mainParticle(i2) && overlap(i1,i2)) {	    //printf("(%d,%d,%d,",100*evaluations/tMax,i1,i2);	    fusion(i1,i2);	    printf("%d ",nParticles);	    //printf(")\n");	    	    //for(j=0;j<swarmsize;j++) {	    //  if (mainParticle(j)) printf("Partikel %d, size %d fusedWith %d\n",j,swarm[j].parSize,swarm[j].fusedWith); 	    //}            	    if (nParticles < particleLowerBound) {	      modeFusion = 0; // i.e.: mode is fission now 	      printf("\nStarting fission ... (%d)\n",100*evaluations/tMax);	    }	  }	}      }    } else {      if (evaluations - tLastFission > 500) {	tLastFission = evaluations; 	fisP = findFissionParticle(2);	fission(fisP);	printf("%d ",nParticles);	//for(j=0;j<swarmsize;j++) {	// if (mainParticle(j)) printf("Partikel %d, size %d fusedWith %d\n",j,swarm[j].parSize,swarm[j].fusedWith); 	//}	if (nParticles == swarmsize) {	  modeFusion = 1;	  printf("\nStarting fusion ... (%d)\n",100*evaluations/tMax);	}      }    }  	    break;  case 3: {    // model 3    int count = 0,count2,val=(int)randval(0,swarmsize);    double *nul = malloc(dim * sizeof(double));    for(j=0;j<dim;j++) nul[j] = 0;    for(i1=val;count<swarmsize;i1 = (i1+1)%swarmsize,count++) {      for(i2=(i1+1)%swarmsize,count2=1;count2<swarmsize;i2 = (i2+1)%swarmsize,count2++) {	if (mainParticle(i1) && mainParticle(i2) && overlap(i1,i2)) {	  if (nParticles > particleLowerBound) {	    fusion(i1,i2);	    printf("(%d)",nParticles);	    if (nParticles == particleLowerBound) {	      printf("Evals gegangen: %d\n", evaluations);	      evalImproved = evaluations;	      printf("Bei der letzte fusion:\n");	      printpositions();	    }	  }	  //  if (nParticles < particleLowerBound) {	    // explode!!!!!!!	    //printpositions();	    //printbestfitness();	  //  explosion();	  //  printf("(explo:%d)",nParticles);	    //for(j=0;j<swarmsize;j++)	    //printf("%d %lf\n",j,dist2(swarm[j].pos,nul));	  //	  }	}      }    }  }  break;  }}int findFissionParticle(int ModelNr){   int index=-1,i;  double distance = 0;  double tempdist = 0;  double tempfit = 0;  int tempImp = 0;  switch(ModelNr) {  case 1: // model med fission ved partikel med st鴕st afstand til swarm-snit-punkt     for(i=0;i<swarmsize;i++) {       if(mainParticle(i) && swarm[i].parSize > 1)	{	  tempdist = distanceToMeanPoint(i);	  if(tempdist > distance) 	    {	      distance = tempdist;	      index = i;	    }	}    }        if ((index==-1))      printf("No possible fission.\n");    //else    //printf("fissionNumber: %d\n",index);    return(index);    break;  case 2: // model med fission ved partikel med d錼ligst fitness       for(i=0;i<swarmsize;i++) {       if(mainParticle(i) && swarm[i].parSize > 1)	{	  if(swarm[i].fitness > tempfit)	    {	      tempfit = swarm[i].fitness;	      index = i;	    }	}    }    if ((index==-1))      printf("No possible fission.\n");    //else    // printf("fissionNumber: %d\n",index);    return(index);    break;  case 3: // model med fission ved partikel som har klarest sig d錼ligst      for(i=0;i<swarmsize;i++) {       if(mainParticle(i) && swarm[i].parSize > 1)	{	  if(swarm[i].tImproved > tempImp)	    {	      tempImp = swarm[i].tImproved;	      index = i;	    }	}    }    if ((index==-1))      printf("No possible fission.\n");    //else    //  printf("fissionNumber: %d\n",index);    return(index);     // break;  case 4: // model med fission ved partikel med mindst afstand til swarm-snit-punkt     distance = DBL_MAX;    for(i=0;i<swarmsize;i++) {       if(mainParticle(i) && swarm[i].parSize > 1)	{	  tempdist = distanceToMeanPoint(i);	  if(tempdist < distance) 	    {	      distance = tempdist;	      index = i;	    }	}    }        if ((index==-1))      printf("No possible fission.\n");    //else    //printf("fissionNumber: %d\n",index);    return(index);    break;  default:    return -1;    break;  }}*/void fitImprovement() {	int fitImproveNr = 0;	int i;		// Warning: Only working for minimisation problems	switch(fitImproveNr) {	case 0: // Procentvis fitness improvement p

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -