int Fun2(int n){//O(n) if(n==0) return 0; if(n==1) return 1; if(n==2) return 2; map<int,int>::iterator iter = tmp.find(n); if(iter!=tmp.end()){ return tmp[n]; } else{ int value = Fun2(n-1)+Fun2(n-2); tmp[n]=value; return value; } }
int main() { int i; for (i = 0; i < 256; i++) chartest[i].c = i; chartest[0].c = 0; /* chartest-done */ Fun1(foo1); Fun2(foo2); Fun3(foo3); Fun4(foo4); Fun5(foo5); Fun6(foo6); Fun7(foo7); Fun8(foo8); Fun9(foo9); Fun10(foo10); Fun11(foo11); Fun12(foo12); Fun13(foo13); Fun14(foo14); Fun15(foo15); Fun16(foo16); Fun17(foo17); Fun18(foo18); /* An (almost-)infinite loop that first clears all the variables and then calls each function. This "hack" is to make testing random functions easier - "advance funN" is guaranteed to have always been preceded by a global variable clearing zed call. We don't let this run forever in case gdb crashes while testing, we don't want to be left eating all cpu on the user's system. */ for (i = 0; i < 1000000; ++i) { zed (); L1 = fun1(); L2 = fun2(); L3 = fun3(); L4 = fun4(); L5 = fun5(); L6 = fun6(); L7 = fun7(); L8 = fun8(); L9 = fun9(); L10 = fun10(); L11 = fun11(); L12 = fun12(); L13 = fun13(); L14 = fun14(); L15 = fun15(); L16 = fun16(); L17 = fun17(); L18 = fun18(); } return 0; }
int main() { srand((unsigned)time(NULL)); /* Generic & Procedural Vars */ int i, j, k, l, vfun, vtest, vgen; double edge, Vmax, goal, gb; /* Vars for Radius */ float absRadius; // Absolute perceptive radius float relRadius; // A iteration var for radius /* Vars for Real-time Recording */ int adjmat[N][N]; // Adjacent Matrix int sucCounter; int radRec; // A var to count the index of the radius /* Vars for Storage Section */ double AveOptGen[relRange]; // Average number of generations of success optimization double AveOptRate[relRange]; // Ratio of success tests/ total tests double AveEndFit[relRange]; // End fit double AveSucFit[relRange];// SUCCESSFUL EndFit The average of the gbest fitness in the end of each SUCCESSFUL test (last generation) double FIndex[relRange], recordInd[testnum]; /* End of Storage Section */ /* Initialization of some variables */ for(i=0; i<relRange; i++) { AveOptGen[i]=0; AveOptRate[i]=0; AveSucFit[i]=0; AveEndFit[i]=0; FIndex[i]=99; // Just a dummy and impossible value } pso = fopen("../../vicfdrpso.txt", "a+"); // We cannot use 'w' on the server in the lab in lack of privileges. vfun=2; switch(vfun) { case 1: edge = 100; Vmax = edge; goal = 0.01; break; case 2: edge = 30; Vmax = edge; goal = 100; break; case 3: edge = 5.12; Vmax = edge; goal = 100; break; case 4: edge = 600; Vmax = edge; goal = 0.1; break; default: edge = 100; Vmax = edge; goal = 0.01; } radRec=0; for(relRadius=relStart; relRadius<relEnd; relRadius+=relStep) // Loop layer 1 (relRadius) { sucCounter = 0; for(vtest = 0; vtest < testnum; vtest++) { int sum = 1; for(i = 0; i < coornum; i++) { pbest[i] = INT_MAX; gbest[i] = INT_MAX; } for(i = 0; i < dim + 1; i++) //Initialization { for(j = 0; j < N; j++) { if(i < dim) { coor[(dim+1)*j+i] = (double)rand() / RAND_MAX * 2 * edge - edge; vel[(dim+1)*j+i] = (double)rand() / RAND_MAX * 2 * Vmax - Vmax; pbest[(dim+1)*j+i] = coor[(dim+1)*j+i]; //printf("%f\t", coor[(dim+1)*j+i]); } else { if(vfun == 1) Fun1(j); if(vfun == 2) Fun2(j); if(vfun == 3) Fun3(j); if(vfun == 4) Fun4(j); //printf("\n%d:%f,", j, coor[(dim+1)*j+i]); pbest[(dim+1)*j+i] = coor[(dim+1)*j+i]; } } } absRadius = relRadius * sqrt(dim * pow(2 * edge , 2)); for(j=0; j<N; j++) for(k=0; k<N; k++) // Avoid Relapse { if( getDistance(j,k) <= absRadius && j!=k ) // k Can be perceived by j { adjmat[j][k]=1; } else { adjmat[j][k]=0; } } // Updating each bird's gbest: for(j=0; j<N; j++) { for(k=0; k<N; k++) if( adjmat[j][k] != 0 ) // k Can be perceived by j { // Updating the gbest of j //printf("%f\t",getDistance(j,k)); if( coor[(dim+1)*k+dim] < gbest[(dim+1)*j+dim] ) { for(l=0; l<=dim; l++) gbest[(dim+1)*j+l] = coor[(dim+1)*k+l]; } } //printf("gbest of %d:%f\nfitness of %d:%f\n",j,gbest[(dim+1)*j+dim],j,coor[(dim+1)*j+dim]); } gb = gbest[dim]; // Updating the global gbest for(i = 1; i < N; i++) if(gbest[(dim+1)*i+dim] < gb) gb = gbest[(dim+1)*i+dim]; //End of initialization for(vgen = 0; vgen < gennum; vgen++) //gennum´ú½ø»¯£» { float inert_w; inert_w=(wmax-wmin)*(gennum-vgen-1)/(gennum+wmin); for(i = 0; i < dim + 1; i++) { for(j = 0; j < N; j++) { if(i < dim) { int lneigh; // The neighbor for the particle to learn from lneigh=findNeigh_fdr(j,i); double r1 = (double)rand() / RAND_MAX * 1; double r2 = (double)rand() / RAND_MAX * 1; double r3 = (double)rand() / RAND_MAX * 1; vel[(dim+1)*j+i] = inert_w * vel[(dim+1)*j+i] + c1 * r1 * (pbest[(dim+1)*j+i] - coor[(dim+1)*j+i])\ + c2 * r2 * (gbest[(dim+1)*j+i] - coor[(dim+1)*j+i])+ c3 * r3 * (pbest[(dim+1)*lneigh+i] - coor[(dim+1)*j+i]); //vel[(dim+1)*j+i] = getMin( getMax(vel[(dim+1)*j+i],(-Vmax) ),Vmax); coor[(dim+1)*j+i] += vel[(dim+1)*j+i]; // Moving //coor[(dim+1)*j+i] = getMin( getMax(coor[(dim+1)*j+i], (-edge) ) ,edge); } else { if(vfun == 1) Fun1(j); if(vfun == 2) Fun2(j); if(vfun == 3) Fun3(j); if(vfun == 4) Fun4(j); // When i = dim, updating pbest: if(coor[(dim+1)*j+i] < pbest[(dim+1)*j+i]) for(l = 0; l < dim + 1; l++) pbest[(dim+1)*j+l] = coor[(dim+1)*j+l]; // Comparing itself with gbest: if(coor[(dim+1)*j+i] < gbest[(dim+1)*j+i]) { for(l = 0; l < dim + 1; l++) gbest[(dim+1)*j+l] = coor[(dim+1)*j+l]; } } // End of else } // for birds } // for dims absRadius = relRadius * sqrt(dim * pow(2 * edge , 2)); // Producing the initiating Adjacent Matrix for( j=0; j<N ; j++ ) for( k=0; k<N; k++) // Avoid Relapse { if( getDistance(j,k) <= absRadius && j!=k ) // k Can be perceived by j { adjmat[j][k]=1; } else { adjmat[j][k]=0; } } // Updating each bird's gbest: for( j=0; j<N ; j++ ) { for( k=0; k<N ; k++ ) if( adjmat[j][k] != 0 ) // k Can be perceived by j // Updating the gbest of j if( coor[(dim+1)*k+dim] < gbest[(dim+1)*j+dim] ) for(l=0; l<=dim; l++) gbest[(dim+1)*j+l] = coor[(dim+1)*k+l]; //printf("gbest of %d:%f\nfitness of %d:%f\n",j,gbest[(dim+1)*j+dim],j,coor[(dim+1)*j+dim]); } // End of Updating each bird's gbest gb = gbest[dim]; // Updating the global gbest for(i = 1; i < N; i++) if(gbest[(dim+1)*i+dim] < gb) gb = gbest[(dim+1)*i+dim]; if(gb < goal && sum != 0) { sum = 0; // Change the flag //ngoal++; sucCounter += 1; AveOptGen[radRec] += vgen; printf("suc gb:%1.15f\n",gb); } if(vgen+1 == 1000) recordInd[vtest]=gb; if(vgen == gennum - 1) { printf("last gb:%1.15f\n",gb); AveEndFit[radRec] += gb; if(sum == 0) // Succeeded AveSucFit[radRec] += gb; } } // End of for VGEN! } // End of vtest FIndex[radRec]=countFIndex(recordInd); AveEndFit[radRec] = AveEndFit[radRec]/testnum; if(sucCounter!=0) // If succeeded { AveSucFit[radRec] = AveSucFit[radRec]/sucCounter; AveOptGen[radRec] = AveOptGen[radRec]/sucCounter; } else { AveSucFit[radRec] = -1; // A flag to show the failure AveOptGen[radRec] = -1; // A flag to show the failure } AveOptRate[radRec]=(double) sucCounter / testnum * 100; printf("%1.15f,%1.15f,%f,%f,%f\n",AveEndFit[radRec],AveSucFit[radRec],AveOptGen[radRec],AveOptRate[radRec],FIndex[radRec]); fprintf(pso,"%1.15f,%1.15f,%f,%f,%f\n",AveEndFit[radRec],AveSucFit[radRec],AveOptGen[radRec],AveOptRate[radRec],FIndex[radRec]); // AveSucFit is the Average Endfit for only successful runs radRec++; printf("\n\nRadius %d is tested\n\n",radRec); } // End of Loop layer 1 (relRadius) printf("\n\n Done! You got it, dude!"); fclose(pso); return 0; }
int main() { //printf("a"); srand((unsigned)time(NULL)); int i, j, l, vfun, vtest, vgen, vlneighs; float x, edge, Vmax, goal, gb, res = 0,avggennew=0; float vel[coornum], results[2*testnum], stdindex[N-1]; int sucCounter; // vlneighs:The number of neighbors a paricle learns from (self-excluded) float AveEndFit[4]; float AveAllFit[4]; //Storage float Sto_SucEndFit[N-1]; // N-1 circumstances float Sto_SucGens[N-1]; float Sto_SucRate[N-1]; for(i=0;i<N-1;i++){ Sto_SucEndFit[i]=-1; Sto_SucGens[i]=-1; Sto_SucRate[i]=-1; } pso = fopen("lipso2.3-all-f2.txt", "a+"); //fprintf(pso, "num\tw\tc1\tc2\tfuncnum\tavggen\tratio\tfinalresult\n"); x = 0.7298; for(vlneighs =0; vlneighs< N-1; vlneighs++) { fprintf(pso, "lneighs:%d\n",vlneighs); for(i = 0; i < 4; i++){ AveAllFit[i]=0; AveEndFit[i]=0; } for(vfun = 2; vfun < 3; vfun++) { for(i = 0; i < testnum; i++) { results[2*i] = gennum; results[2*i+1] = 0; } if(vfun == 1) { edge = 100; Vmax = edge; goal = 0.01; } if(vfun == 2) { edge = 30; Vmax = edge; goal = 100; } if(vfun == 3) { edge = 5.12; Vmax = edge; goal = 100; } if(vfun == 4) { edge = 600; Vmax = edge; goal = 0.1; } int ngoal = 0, avggen = 0; sucCounter = 0; for(vtest = 0; vtest < testnum; vtest++) { int sum = 1; for(i = 0; i < coornum; i++) { pbest[i] = INT_MAX; } for(i = 0; i < dim + 1; i++) //粒子初始化; { for(j = 0; j < N; j++) { if(i < dim) { coor[(dim+1)*j+i] = (double)rand() / RAND_MAX * 2 * edge - edge; vel[(dim+1)*j+i] = (double)rand() / RAND_MAX * 2 * Vmax - Vmax; pbest[(dim+1)*j+i] = coor[(dim+1)*j+i]; //printf("%f\t", coor[(dim+1)*j+i]); } else { // i=dim if(vfun == 1) Fun1(j); if(vfun == 2) Fun2(j); if(vfun == 3) Fun3(j); if(vfun == 4) Fun4(j); //printf("\n%d:%f,", j, coor[(dim+1)*j+i]); pbest[(dim+1)*j+i] = coor[(dim+1)*j+i]; } } } /* int gbp; gb = pbest[dim]; for(i = 1; i < N; i++) if(pbest[(dim+1)*i+dim] < gb) { gb = pbest[(dim+1)*i+dim]; gbp = i; } */ for(vgen = 0; vgen < gennum; vgen++) //gennum代进化; { // Finding gbest double tempgb=pbest[dim]; int gbindex=0; for( i = 1; i < N; i++) if(pbest[(dim+1)*i+dim]<tempgb ){ tempgb=pbest[(dim+1)*i+dim]; gbindex=i; } get_vel_update(vlneighs, gbindex); for(i = 0; i < dim + 1; i++) { for(j = 0; j < N; j++) { if(i < dim) { //double r1=(double)rand() / RAND_MAX; //double r2=(double)rand() / RAND_MAX; //vel[(dim+1)*j+i] = x * ( vel[(dim+1)*j+i] + phi*( r1*( pbest[(dim+1)*gbp+i]-coor[(dim+1)*j+i]) + r2*(pbest[(dim+1)*j+i]-coor[(dim+1)*j+i]) )/2 ); //vel[(dim+1)*j+i] = x * ( vel[(dim+1)*j+i] + 2.05*r1*(pbest[(dim+1)*j+i]-coor[(dim+1)*j+i])+2.05*r2*(pbest[(dim+1)*gbp+i]-coor[(dim+1)*j+i])); vel[(dim+1)*j+i] = x * (vel[(dim+1)*j+i]+update[(dim+1)*j+i]); coor[(dim+1)*j+i] += vel[(dim+1)*j+i]; } else { if(vfun == 1) Fun1(j); if(vfun == 2) Fun2(j); if(vfun == 3) Fun3(j); if(vfun == 4) Fun4(j); if(coor[(dim+1)*j+i] < pbest[(dim+1)*j+i]) for(l = 0; l < dim + 1; l++) pbest[(dim+1)*j+l] = coor[(dim+1)*j+l]; } } } gb = pbest[dim]; for(i = 1; i < N; i++) if(pbest[(dim+1)*i+dim] < gb) { gb = pbest[(dim+1)*i+dim]; //gbp = i; } //printf("\ngb:%f\n",gb); if(gb < goal && sum != 0) { results[2*vtest] = (double)vgen; results[2*vtest+1] = gb; sum = 0; //ngoal++; printf("\ngb:%f,%d\t",gb,vgen); sucCounter+=1; } if(vgen == 1000) { stdindmemo[vtest]=gb; printf("\nvtest:%d,1000gen's gb:%1.30f\n",vtest,gb); } if(vgen == gennum - 1) { printf("lastgb:%f\n",gb); AveAllFit[vfun-1] += gb; if(sum == 0) // Succeeded AveEndFit[vfun-1] += gb; else results[2*vtest] = vgen; } } // End of for VGEN! } // End of for VTEST ! //Calculating the Standard Index double avememofit=0; for(i=0; i<testnum; i++) avememofit+=stdindmemo[i]; avememofit/=testnum; double delta=0; for(i=0; i<testnum; i++) delta += pow((stdindmemo[i]-avememofit),2); delta = sqrt(delta/testnum); double tempmemeo, median; for(i=0; i<testnum-1; i++) for(j=i+1; j<testnum; j++) if(stdindmemo[i]>stdindmemo[j]) { tempmemeo = stdindmemo[i]; stdindmemo[i] = stdindmemo[j]; stdindmemo[j] = tempmemeo; } if(testnum%2 == 0) { median = (stdindmemo[testnum/2]+stdindmemo[testnum/2+1])/2; printf("\nmedian:%f; %f,%f",median,stdindmemo[testnum/2],stdindmemo[testnum/2+1]); } else median = stdindmemo[testnum/2+1]; stdindex[vlneighs] = (median - avememofit)/delta; printf("\nstdindex[vlneighs]%f",stdindex[vlneighs]); if(sucCounter!=0){ Sto_SucEndFit[vlneighs]=AveEndFit[vfun-1]/sucCounter; // fprintf(pso,"\n\n\nLast succeeded average gb fitness:%1.30f\n",AveEndFit[vfun-1]/sucCounter); } else printf("\n\n\nLast succeeded average gb fitness: U gotta be kidding, man!\n"); for(i = 0; i < testnum; i++) { if(results[2*i] != gennum - 1) { ngoal++; avggen += results[2*i]; res += results[2*i+1]; } } printf("%d,%d", avggen/vtest, ngoal); if(ngoal == 0) // Unsuccessful avggennew = (double)gennum - 1; else avggennew = (double)avggen / ngoal; fprintf(pso, "AveGen,%f,SucRate,%f,SucEndFit,%f,LastFit,%f,StdIndex,%f\n", avggennew, (double)sucCounter/testnum*100,Sto_SucEndFit[vlneighs],AveAllFit[vfun-1]/testnum,stdindex[vlneighs]); printf("\nAveGen,%f,SucRate,%f,SucEndFit,%f,LastFit,%f,StdIndex,%f\n", avggennew, (double)sucCounter/testnum*100,Sto_SucEndFit[vlneighs],AveAllFit[vfun-1]/testnum,stdindex[vlneighs]); Sto_SucGens[vlneighs]=avggennew; Sto_SucRate[vlneighs]=(double)sucCounter / testnum * 100; } // End of for VFUN! } // End of for vlneighs! fclose(pso); pso = fopen("lipso2.3-all-f2-SucEndFit.txt", "a+"); for(i=0;i<N;i++) fprintf(pso,"%d,%1.30f\n",i+1,Sto_SucEndFit[i]); fclose(pso); pso = fopen("lipso2.3-all-f2-SucGens.txt", "a+"); for(i=0;i<N;i++) fprintf(pso,"%d,%f\n",i+1,Sto_SucGens[i]); fclose(pso); pso = fopen("lipso2.3-all-f2-SucRate.txt", "a+"); for(i=0;i<N;i++) fprintf(pso,"%d,%f\n",i+1,Sto_SucRate[i]); fclose(pso); }
int main() { srand((unsigned)time(NULL)); /* Generic & Procedural Vars */ int i, j, k, l, vfun, vtest, vgen; double edge, Vmax, goal, gb; double vel[coornum], pbest[coornum], gbest[coornum]; /* Vars for Real-time Recording */ int sucCounter; /* Vars for Storage Section */ double AveOptGen; // Average number of generations of success optimization double AveOptRate; // Ratio of success tests/ total tests double AveEndFit; // End fit double AveSucFit;// SUCCESSFUL EndFit The average of the gbest fitness in the end of each SUCCESSFUL test (last generation) double FIndex, recordInd[testnum]; /* End of Storage Section */ /* Initialization of some variables */ AveOptGen=0; AveOptRate=0; AveSucFit=0; AveEndFit=0; FIndex=99; // Just a dummy and impossible value pso = fopen("../../cpso.txt", "a+"); // We cannot use 'w' on the server in the lab in lack of privileges. vfun=2; switch(vfun) { case 1: edge = 100; Vmax = edge; goal = 0.01; break; case 2: edge = 30; Vmax = edge; goal = 100; break; case 3: edge = 5.12; Vmax = edge; goal = 100; break; case 4: edge = 600; Vmax = edge; goal = 0.1; break; default: edge = 100; Vmax = edge; goal = 0.01; } //int ngoal = 0, avggen = 0; sucCounter = 0; for(vtest = 0; vtest < testnum; vtest++) { int sum = 1; for(i = 0; i < coornum; i++) { pbest[i] = INT_MAX; gbest[i] = INT_MAX; } for(i = 0; i < dim + 1; i++) //Initialization { for(j = 0; j < N; j++) { if(i < dim) { coor[(dim+1)*j+i] = (double)rand() / RAND_MAX * 2 * edge - edge; vel[(dim+1)*j+i] = (double)rand() / RAND_MAX * 2 * Vmax - Vmax; pbest[(dim+1)*j+i] = coor[(dim+1)*j+i]; //printf("%f\t", coor[(dim+1)*j+i]); } else { if(vfun == 1) Fun1(j); if(vfun == 2) Fun2(j); if(vfun == 3) Fun3(j); if(vfun == 4) Fun4(j); //printf("\n%d:%f,", j, coor[(dim+1)*j+i]); pbest[(dim+1)*j+i] = coor[(dim+1)*j+i]; if(coor[(dim+1)*j+i] < gbest[(dim+1)*j+i]) { for(l = 0; l < dim + 1; l++) gbest[(dim+1)*j+l] = coor[(dim+1)*j+l]; //printf("gbest:%f\t", gbest[(dim+1)*j+i]); } for(k = 0; k < N; k++) if( gbest[(dim+1)*j+i] < gbest[(dim+1)*k+i]) { //printf("t[%d][%d],",j,k); for(l = 0; l < dim + 1; l++) gbest[(dim+1)*k+l] = gbest[(dim+1)*j+l]; } } } } for(vgen = 0; vgen < gennum; vgen++) //gennum´ú½ø»¯£» { for(i = 0; i < dim + 1; i++) { for(j = 0; j < N; j++) { if(i < dim) { double r1 = (double)rand() / RAND_MAX * 1; double r2 = (double)rand() / RAND_MAX * 1; vel[(dim+1)*j+i] = w * vel[(dim+1)*j+i] + c1 * r1 * (pbest[(dim+1)*j+i] - coor[(dim+1)*j+i])\ + c2 * r2 * (gbest[(dim+1)*j+i] - coor[(dim+1)*j+i]); /* if(vel[(dim+1)*j+i] > Vmax) vel[(dim+1)*j+i] = Vmax; if(vel[(dim+1)*j+i] < -Vmax) vel[(dim+1)*j+i] = -Vmax; */ coor[(dim+1)*j+i] += vel[(dim+1)*j+i]; // Moving } else { if(vfun == 1) Fun1(j); if(vfun == 2) Fun2(j); if(vfun == 3) Fun3(j); if(vfun == 4) Fun4(j); if(coor[(dim+1)*j+i] < pbest[(dim+1)*j+i]) for(l = 0; l < dim + 1; l++) pbest[(dim+1)*j+l] = coor[(dim+1)*j+l]; if(coor[(dim+1)*j+i] < gbest[(dim+1)*j+i]) { for(l = 0; l < dim + 1; l++) gbest[(dim+1)*j+l] = coor[(dim+1)*j+l]; } for(k = 0; k < N; k++) if( gbest[(dim+1)*j+i] < gbest[(dim+1)*k+i]) for(l = 0; l < dim + 1; l++) gbest[(dim+1)*k+l] = gbest[(dim+1)*j+l]; } } } gb = gbest[dim]; for(i = 1; i < N; i++) if(gbest[(dim+1)*i+dim] < gb) gb = gbest[(dim+1)*i+dim]; if(gb < goal && sum != 0) { sum = 0; // Change the flag //ngoal++; sucCounter += 1; AveOptGen += vgen; printf("suc gb:%1.30f\n",gb); } if(vgen == 1000) recordInd[vtest]=gb; if(vgen == gennum - 1) { printf("last gb:%1.15f\n",gb); AveEndFit += gb; if(sum == 0) // Succeeded AveSucFit += gb; } } // End of for VGEN! } // End of vtest FIndex=countFIndex(recordInd); AveEndFit = AveEndFit/testnum; if(sucCounter!=0) // If succeeded { AveSucFit = AveSucFit/sucCounter; AveOptGen = AveOptGen/sucCounter; } else { AveSucFit = -1; // A flag to show the failure AveOptGen = -1; // A flag to show the failure } AveOptRate=(double) sucCounter / testnum * 100; printf("%1.30f,%1.30f,%f,%f,%f\n",AveEndFit,AveSucFit,AveOptGen,AveOptRate,FIndex); fprintf(pso,"%1.30f,%1.30f,%f,%f,%f\n",AveEndFit,AveSucFit,AveOptGen,AveOptRate,FIndex); // AveSucFit is the Average Endfit for only successful runs fclose(pso); return 0; }
int main() { //printf("a"); srand((unsigned)time(NULL)); int i, j, l, vfun, vtest, vgen, vlneighs; float x, edge, Vmax, goal, gb, res = 0,avggennew=0; float vel[coornum], results[2*testnum],midians[4]; int sucCounter; // vlneighs:The number of neighbors a paricle learns from (self-excluded) float AveEndFit[4]; //Storage float Sto_SucEndFit[4][N-1]; // N-1 circumstances for learning float Sto_SucGens[4][N-1]; float Sto_SucRate[4][N-1]; float Sto_StdIndex[N-1]; for(j=0; j<4; j++) for(i=0; i<N-1; i++) { Sto_SucEndFit[j][i]=-1; Sto_SucGens[j][i]=-1; Sto_SucRate[j][i]=-1; Sto_StdIndex[i]=999; } pso = fopen("fipso1.1-all-f1-f4-stdlized.txt", "a+"); fprintf(pso, "num\tw\tc1\tc2\tfuncnum\tavggen\tratio\tfinalresult\n"); x = 0.7298; for(vlneighs =18; vlneighs< 36; vlneighs++) { fprintf(pso, "lneighs:%d\n",vlneighs); for(i = 0; i < 4; i++) AveEndFit[i]=0; for(vfun = 1; vfun < 5; vfun++) { for(i = 0; i < testnum; i++) { results[2*i] = gennum; results[2*i+1] = 0; } if(vfun == 1) { edge = 100; Vmax = edge; goal = 0.01; } if(vfun == 2) { edge = 30; Vmax = edge; goal = 100; } if(vfun == 3) { edge = 5.12; Vmax = edge; goal = 100; } if(vfun == 4) { edge = 600; Vmax = edge; goal = 0.1; } int ngoal = 0, avggen = 0; sucCounter = 0; for(vtest = 0; vtest < testnum; vtest++) { int sum = 1; for(i = 0; i < coornum; i++) { pbest[i] = INT_MAX; } for(i = 0; i < dim + 1; i++) //粒子初始化; { for(j = 0; j < N; j++) { if(i < dim) { coor[(dim+1)*j+i] = (double)rand() / RAND_MAX * 2 * edge - edge; vel[(dim+1)*j+i] = (double)rand() / RAND_MAX * 2 * Vmax - Vmax; pbest[(dim+1)*j+i] = coor[(dim+1)*j+i]; //printf("%f\t", coor[(dim+1)*j+i]); } else { // i=dim if(vfun == 1) Fun1(j); if(vfun == 2) Fun2(j); if(vfun == 3) Fun3(j); if(vfun == 4) Fun4(j); //printf("\n%d:%f,", j, coor[(dim+1)*j+i]); pbest[(dim+1)*j+i] = coor[(dim+1)*j+i]; } } } /* int gbp; gb = pbest[dim]; for(i = 1; i < N; i++) if(pbest[(dim+1)*i+dim] < gb) { gb = pbest[(dim+1)*i+dim]; gbp = i; } */ for(vgen = 0; vgen < gennum; vgen++) //gennum代进化; { /* int k; double phi = 4.1; double PBNINDEX[2][N]; // Pbest & Index for(k=0; k<N; k++) { PBNINDEX[0][k]=pbest[(dim+1)*k+dim]; // pbest PBNINDEX[1][k]=k; // The index of particle } for(k=0; k<N; k++){ printf("<%f,%d> ",PBNINDEX[0][k],(int)PBNINDEX[1][k]); } printf("\n");*/ //QuickSort(PBNINDEX,0,N-1); // Done sorting the fitness while keeping the index // The quicksort gives a small to large sequence! (And fitness is better when smaller //for(k=0; k<N; k++) //printf("%f,[%f],",PBNINDEX[0][k],PBNINDEX[1][k]); /*for(k=0; k<N; k++){ printf("<%f,%d> ",PBNINDEX[0][k],(int)PBNINDEX[1][k]); }*/ //printf("\n"); /* for(j = 0; j < N; j++) { for(i = 0; i < dim; i++) { double r1=(double)rand() / RAND_MAX, r2=(double)rand() / RAND_MAX; int kkk=(int)PBNINDEX[1][0]; update[(dim+1)*j+i] = phi*( r1*( pbest[(dim+1)*kkk+i]-coor[(dim+1)*j+i]) + r2*(pbest[(dim+1)*j+i]-coor[(dim+1)*j+i]) )/2; } } */ get_vel_update(vlneighs); for(i = 0; i < dim + 1; i++) { for(j = 0; j < N; j++) { if(i < dim) { //double r1=(double)rand() / RAND_MAX; //double r2=(double)rand() / RAND_MAX; //vel[(dim+1)*j+i] = x * ( vel[(dim+1)*j+i] + phi*( r1*( pbest[(dim+1)*gbp+i]-coor[(dim+1)*j+i]) + r2*(pbest[(dim+1)*j+i]-coor[(dim+1)*j+i]) )/2 ); //vel[(dim+1)*j+i] = x * ( vel[(dim+1)*j+i] + 2.05*r1*(pbest[(dim+1)*j+i]-coor[(dim+1)*j+i])+2.05*r2*(pbest[(dim+1)*gbp+i]-coor[(dim+1)*j+i])); vel[(dim+1)*j+i] = x * (vel[(dim+1)*j+i]+update[(dim+1)*j+i]); coor[(dim+1)*j+i] += vel[(dim+1)*j+i]; } else { if(vfun == 1) Fun1(j); if(vfun == 2) Fun2(j); if(vfun == 3) Fun3(j); if(vfun == 4) Fun4(j); if(coor[(dim+1)*j+i] < pbest[(dim+1)*j+i]) for(l = 0; l < dim + 1; l++) pbest[(dim+1)*j+l] = coor[(dim+1)*j+l]; } } } gb = pbest[dim]; for(i = 1; i < N; i++) if(pbest[(dim+1)*i+dim] < gb) { gb = pbest[(dim+1)*i+dim]; //gbp = i; } //printf("\ngb:%f\n",gb); if(gb < goal && sum != 0) { results[2*vtest] = (double)vgen; results[2*vtest+1] = gb; sum = 0; //ngoal++; printf("\ngb:%f,%d\t",gb,vgen); sucCounter+=1; } if(vgen == gennum - 1) { printf("lastgb:%f\n",gb); if(sum == 0) // Succeeded AveEndFit[vfun-1] += gb; else results[2*vtest] = vgen; } if(vgen ==1000) { allfun[vtest] = gb; printf("\n1000gb:%1.30f\n",gb); } } // End of for VGEN! } // End of for VTEST ! double miu=0; for(i=0; i<testnum; i++) miu += allfun[i]; miu /= testnum; double delta=0; for(i=0; i<testnum; i++) delta += pow((allfun[i]-miu),2); delta = sqrt(delta/testnum); double tempfun; for(i=0; i<testnum-1; i++) for(j=i+1; j<testnum; j++) if(allfun[i]>allfun[j]) { tempfun = allfun[i]; allfun[i] = allfun[j]; allfun[j] = tempfun; } if(testnum%2 == 0) { midians[vfun-1] = (allfun[testnum/2]+allfun[testnum/2+1])/2; printf("\ndelta:%f,%f,%f",midians[vfun-1],allfun[testnum/2],allfun[testnum/2+1]); } else midians[vfun-1] = allfun[testnum/2+1]; midians[vfun-1] = (midians[vfun-1] - miu)/delta; printf("\nmidians:%f",midians[vfun-1]); printf("\nmidians[%d] is %f",vfun-1,midians[vfun-1]); if(sucCounter!=0) { Sto_SucEndFit[vfun-1][vlneighs-1]=AveEndFit[vfun-1]/sucCounter; fprintf(pso,"\n\n\nLast succeeded average gb fitness:%1.30f\n",AveEndFit[vfun-1]/sucCounter); } else fprintf(pso,"\n\n\nLast succeeded average gb fitness: U gotta be kidding, man!\n"); for(i = 0; i < testnum; i++) { if(results[2*i] != gennum - 1) { ngoal++; avggen += results[2*i]; res += results[2*i+1]; } } printf("%d,%d", avggen/vtest, ngoal); if(ngoal == 0) // Unsuccessful avggennew = (double)gennum - 1; else avggennew = (double)avggen / ngoal; fprintf(pso, "\nAveGen:%f\nSucRate%f%%", avggennew, (double)sucCounter / testnum * 100); Sto_SucGens[vfun-1][vlneighs-1]=avggennew; Sto_SucRate[vfun-1][vlneighs-1]=(double)sucCounter / testnum * 100; } // End of for VFUN! double midian=0; for(i=0; i<4; i++) midian += midians[i]; midian /=4; printf("\nmidian is %f\n",midian); Sto_StdIndex[vlneighs-1]=midian; //fprintf(pso,"\nstandardized performance:%fasdf\n",midian); } // End of for vlneighs! fclose(pso); pso = fopen("fipso1.1-all-f1-f4-SucEndFit&StdIndex-stdlized.txt", "a+"); fprintf(pso,"vlneighs,f1-SEndfit,f2-SEndfit,f3-SEndfit,f4-SEndfit,StdIndex\n"); for(i=0; i<N; i++) fprintf(pso,"%d,%1.30f,%1.30f,%1.30f,%1.30f,%f\n",i+1,Sto_SucEndFit[0][i],Sto_SucEndFit[1][i],Sto_SucEndFit[2][i],Sto_SucEndFit[3][i],Sto_StdIndex[i]); fclose(pso); pso = fopen("fipso1.1-all-f1-f4-SucGens-stdlized.txt", "a+"); for(i=0; i<N; i++) fprintf(pso,"%d,%f,%f,%f,%f\n",i+1,Sto_SucGens[0][i],Sto_SucGens[1][i],Sto_SucGens[2][i],Sto_SucGens[3][i]); fclose(pso); pso = fopen("fipso1.1-all-f1-f4-SucRate-stdlized.txt", "a+"); for(i=0; i<N; i++) fprintf(pso,"%d,%f,%f,%f,%f\n",i+1,Sto_SucRate[0][i],Sto_SucRate[1][i],Sto_SucRate[2][i],Sto_SucRate[3][i]); fclose(pso); return 0; }
int main() { //printf("a"); srand((unsigned)time(NULL)); int i, j, l, vfun, vtest, vgen, vlneighs; float x, edge, Vmax, goal, gb, res = 0,avggennew=0; float vel[coornum], results[2*testnum]; int sucCounter; // vlneighs:The number of neighbors a paricle learns from (self-excluded) float AveEndFit[4]; //Storage float Sto_SucEndFit[N-1]; // N-1 circumstances float Sto_SucGens[N-1]; float Sto_SucRate[N-1]; for(i=0;i<N-1;i++){ Sto_SucEndFit[i]=-1; Sto_SucGens[i]=-1; Sto_SucRate[i]=-1; } pso = fopen("lipso2.0-all-f2.txt", "a+"); fprintf(pso, "num\tw\tc1\tc2\tfuncnum\tavggen\tratio\tfinalresult\n"); x = 0.7298; for(vlneighs =1; vlneighs< N; vlneighs++) { fprintf(pso, "lneighs:%d\n",vlneighs); for(i = 0; i < 4; i++) AveEndFit[i]=0; for(vfun = 2; vfun < 3; vfun++) { for(i = 0; i < testnum; i++) { results[2*i] = gennum; results[2*i+1] = 0; } if(vfun == 1) { edge = 100; Vmax = edge; goal = 0.01; } if(vfun == 2) { edge = 30; Vmax = edge; goal = 100; } if(vfun == 3) { edge = 5.12; Vmax = edge; goal = 100; } if(vfun == 4) { edge = 600; Vmax = edge; goal = 0.1; } int ngoal = 0, avggen = 0; sucCounter = 0; for(vtest = 0; vtest < testnum; vtest++) { int sum = 1; for(i = 0; i < coornum; i++) { pbest[i] = INT_MAX; } for(i = 0; i < dim + 1; i++) //粒子初始化; { for(j = 0; j < N; j++) { if(i < dim) { coor[(dim+1)*j+i] = (double)rand() / RAND_MAX * 2 * edge - edge; vel[(dim+1)*j+i] = (double)rand() / RAND_MAX * 2 * Vmax - Vmax; pbest[(dim+1)*j+i] = coor[(dim+1)*j+i]; //printf("%f\t", coor[(dim+1)*j+i]); } else { // i=dim if(vfun == 1) Fun1(j); if(vfun == 2) Fun2(j); if(vfun == 3) Fun3(j); if(vfun == 4) Fun4(j); //printf("\n%d:%f,", j, coor[(dim+1)*j+i]); pbest[(dim+1)*j+i] = coor[(dim+1)*j+i]; } } } /* int gbp; gb = pbest[dim]; for(i = 1; i < N; i++) if(pbest[(dim+1)*i+dim] < gb) { gb = pbest[(dim+1)*i+dim]; gbp = i; } */ for(vgen = 0; vgen < gennum; vgen++) //gennum代进化; { for(i=0; i<N; i++) randlneighs[i]=0; // initializing the indices of learnees choose_rand_neighbors(vlneighs); //for(i=0; i<N; i++) //printf("%d",randlneighs[i]); // initializing the indices of learnees get_vel_update(vlneighs); for(i = 0; i < dim + 1; i++) { for(j = 0; j < N; j++) { if(i < dim) { //double r1=(double)rand() / RAND_MAX; //double r2=(double)rand() / RAND_MAX; //vel[(dim+1)*j+i] = x * ( vel[(dim+1)*j+i] + phi*( r1*( pbest[(dim+1)*gbp+i]-coor[(dim+1)*j+i]) + r2*(pbest[(dim+1)*j+i]-coor[(dim+1)*j+i]) )/2 ); //vel[(dim+1)*j+i] = x * ( vel[(dim+1)*j+i] + 2.05*r1*(pbest[(dim+1)*j+i]-coor[(dim+1)*j+i])+2.05*r2*(pbest[(dim+1)*gbp+i]-coor[(dim+1)*j+i])); vel[(dim+1)*j+i] = x * (vel[(dim+1)*j+i]+update[(dim+1)*j+i]); coor[(dim+1)*j+i] += vel[(dim+1)*j+i]; } else { if(vfun == 1) Fun1(j); if(vfun == 2) Fun2(j); if(vfun == 3) Fun3(j); if(vfun == 4) Fun4(j); if(coor[(dim+1)*j+i] < pbest[(dim+1)*j+i]) for(l = 0; l < dim + 1; l++) pbest[(dim+1)*j+l] = coor[(dim+1)*j+l]; } } } gb = pbest[dim]; for(i = 1; i < N; i++) if(pbest[(dim+1)*i+dim] < gb) { gb = pbest[(dim+1)*i+dim]; //gbp = i; } //printf("\ngb:%f\n",gb); if(gb < goal && sum != 0) { results[2*vtest] = (double)vgen; results[2*vtest+1] = gb; sum = 0; //ngoal++; printf("\ngb:%f,%d\t",gb,vgen); sucCounter+=1; } if(vgen == gennum - 1) { printf("lastgb:%f\n",gb); if(sum == 0) // Succeeded AveEndFit[vfun-1] += gb; else results[2*vtest] = vgen; } } // End of for VGEN! } // End of for VTEST ! if(sucCounter!=0){ Sto_SucEndFit[vlneighs-1]=AveEndFit[vfun-1]/sucCounter; fprintf(pso,"\n\n\nLast succeeded average gb fitness:%1.30f\n",AveEndFit[vfun-1]/sucCounter); } else fprintf(pso,"\n\n\nLast succeeded average gb fitness: U gotta be kidding, man!\n"); for(i = 0; i < testnum; i++) { if(results[2*i] != gennum - 1) { ngoal++; avggen += results[2*i]; res += results[2*i+1]; } } printf("%d,%d", avggen/vtest, ngoal); if(ngoal == 0) // Unsuccessful avggennew = (double)gennum - 1; else avggennew = (double)avggen / ngoal; fprintf(pso, "\nAveGen:%f\nSucRate%f%%", avggennew, (double)sucCounter / testnum * 100); Sto_SucGens[vlneighs-1]=avggennew; Sto_SucRate[vlneighs-1]=(double)sucCounter / testnum * 100; } // End of for VFUN! } // End of for vlneighs! fclose(pso); pso = fopen("lipso2.0-all-f2-SucEndFit.txt", "a+"); for(i=0;i<N;i++) fprintf(pso,"%d,%1.30f\n",i+1,Sto_SucEndFit[i]); fclose(pso); pso = fopen("lipso2.0-all-f2-SucGens.txt", "a+"); for(i=0;i<N;i++) fprintf(pso,"%d,%f\n",i+1,Sto_SucGens[i]); fclose(pso); pso = fopen("lipso2.0-all-f2-SucRate.txt", "a+"); for(i=0;i<N;i++) fprintf(pso,"%d,%f\n",i+1,Sto_SucRate[i]); fclose(pso); }
int main() { srand((unsigned)time(NULL)); /* Generic & Procedural Vars */ int i, j, k, l, vfun, vtest, vgen; // iteration vars float edge, Vmax, goal, gb; // gb is the global gbest float vel[coornum], pbest[coornum], gbest[coornum]; // Each bird's got their own gbest /* Vars for Radius */ float absRadius; // Absolute perceptive radius float relRadius; // A iteration var for radius /* Vars for Real-time Recording */ int adjmat[N][N]; // Adjacent Matrix int radRec; // A var to count the index of the radius float res = 0, avggennew=0, results[4*testnum]; /* Vars for Storage */ float AveOptGen[relRange]; // FuncNumber*RadiusNumber: Every 19[radius] with a func float AveOptRate[relRange]; // FuncNumber*RadiusNumber: Every 19[radius] with a func //So the truth is, we're getting the average(a matrix) of average(a test) //float AveOptFit[relRange][gennum/aveDegInterval+1]; // The average of the gbest fitness of the interval generations float AveEndFit[relRange]; // End fit float AveOptFit[relRange][gennum/aveDegInterval+1]; // The average of the gbest fitness of the interval generations float AveSucFit[relRange];// SUCCESSFUL EndFit The average of the gbest fitness in the end of each SUCCESSFUL test (last generation) float FIndex[relRange], recordInd[testnum]; /* End of Storage Section */ char buffer[19]; // A buffer to write the file, 15 is the estimated char numbers char storage[19*relRange]; for(i=0; i<relRange; i++) { AveOptGen[i]=0; AveOptRate[i]=0; AveEndFit[i]=0; AveSucFit[i]=0; for(j=0; j<gennum/aveDegInterval; j++) { AveOptFit[i][j]=0; } } for(i=0; i<19*relRange; i++) { storage[i]=' '; } /* Some settings */ pso = fopen("../../vicpso-dynarad.txt", "w"); vfun=1; if(vfun == 1) { edge = 100; Vmax = edge; goal = 0.01; } if(vfun == 2) { edge = 30; Vmax = edge; goal = 100; } if(vfun == 3) { edge = 5.12; Vmax = edge; goal = 100; } if(vfun == 4) { edge = 600; Vmax = edge; goal = 0.1; } radRec=0; // Set the radius index counting number printf("changeInte:%d",changeInte); for(relRadius=relStart; relRadius<relEnd; relRadius+=relStep) // Loop layer 1 (relRadius) { int ngoal = 0, avggen = 0; // Goal achieving flag & Average generations for(i = 0; i < testnum; i++) { results[4*i] = gennum; results[4*i+1] = 0; results[4*i+2] = gennum; results[4*i+3] = 0; } for(vtest = 0; vtest < testnum; vtest++) // Run the experiment { float neoDiag=0; float dist=0; int sum = 1; for(i = 0; i < coornum; i++) { pbest[i] = INT_MAX; gbest[i] = INT_MAX; } //Initialization: for(i = 0; i < dim + 1; i++) { for(j = 0; j < N; j++) { if(i < dim) { coor[(dim+1)*j+i] = (double)rand() / RAND_MAX * 2 * edge - edge; vel[(dim+1)*j+i] = (double)rand() / RAND_MAX * 2 * Vmax - Vmax; pbest[(dim+1)*j+i] = coor[(dim+1)*j+i]; gbest[(dim+1)*j+i] = coor[(dim+1)*j+i]; } else { // i=dim if(vfun == 1) Fun1(j); if(vfun == 2) Fun2(j); if(vfun == 3) Fun3(j); if(vfun == 4) Fun4(j); // Initializing pbest pbest[(dim+1)*j+i] = coor[(dim+1)*j+i]; gbest[(dim+1)*j+i] = coor[(dim+1)*j+i]; } // End of the condition i = dim } // for birds } // for dims //neoDiag=0; // Producing the initiating Adjacent Matrix /*for(j=0; j<N; j++) for(k=0; k<N; k++) // Avoid Relapse { dist=getDistance(j,k); if(dist>neoDiag) neoDiag = dist; } */ absRadius = relRadius * sqrt(dim * pow(2 * edge , 2)); for(j=0; j<N; j++) for(k=0; k<N; k++) // Avoid Relapse { if( getDistance(j,k) <= absRadius && j!=k ) // k Can be perceived by j { adjmat[j][k]=1; } else { adjmat[j][k]=0; } } // End of producing the initiating Adjacent Matrix /* Average Degree for( j=0; j<N ; j++ ) for( k=0; k<N ; k++ ) if( adjmat[j][k] != 0) // k Can be perceived by j temp+=1; temp/=N; AveDeg[radRec][0]+=temp; // Got it */ // Updating each bird's gbest: for(j=0; j<N; j++) { for(k=0; k<N; k++) if( adjmat[j][k] != 0 ) // k Can be perceived by j { // Updating the gbest of j //printf("%f\t",getDistance(j,k)); if( coor[(dim+1)*k+dim] < gbest[(dim+1)*j+dim] ) { for(l=0; l<=dim; l++) gbest[(dim+1)*j+l] = coor[(dim+1)*k+l]; } } //printf("gbest of %d:%f\nfitness of %d:%f\n",j,gbest[(dim+1)*j+dim],j,coor[(dim+1)*j+dim]); } gb = gbest[dim]; // Updating the global gbest for(i = 1; i < N; i++) if(gbest[(dim+1)*i+dim] < gb) gb = gbest[(dim+1)*i+dim]; AveOptFit[radRec][0]+=gb; // End of Updating each bird's gbest in initialization for(vgen = 0; vgen < gennum; vgen++) // Evolution starts { for(i = 0; i < dim + 1; i++) { for(j = 0; j < N; j++) { if(i < dim) { float r1 = (double)rand() / RAND_MAX * 1; float r2 = (double)rand() / RAND_MAX * 1; vel[(dim+1)*j+i] = w * vel[(dim+1)*j+i] + c1 * r1 * (pbest[(dim+1)*j+i] - coor[(dim+1)*j+i])\ + c2 * r2 * (gbest[(dim+1)*j+i] - coor[(dim+1)*j+i]); coor[(dim+1)*j+i] += vel[(dim+1)*j+i]; } else // i = dim { if(vfun == 1) Fun1(j); if(vfun == 2) Fun2(j); if(vfun == 3) Fun3(j); if(vfun == 4) Fun4(j); // When i = dim, updating pbest: if(coor[(dim+1)*j+i] < pbest[(dim+1)*j+i]) for(l = 0; l < dim + 1; l++) pbest[(dim+1)*j+l] = coor[(dim+1)*j+l]; // Comparing itself with gbest: if(coor[(dim+1)*j+i] < gbest[(dim+1)*j+i]) for(l = 0; l < dim + 1; l++) gbest[(dim+1)*j+l] = coor[(dim+1)*j+l]; } // End of else } // for birds } // for dims if( (vgen+1) % (changeInte) == 0 && vgen!=0) { //printf("%d,%f\n",vgen,neoDiag); absRadius=relRadius*neoDiag; } neoDiag=0;// reset // Producing the Adjacent Matrix for( j=0; j<N ; j++ ) for( k=0; k<N; k++) // Avoid Relapse { dist=getDistance(j,k); if( getDistance(j,k) <= absRadius && j!=k ) // k Can be perceived by j { adjmat[j][k]=1; } else { adjmat[j][k]=0; } if( (vgen+1) % changeInte == (changeInte-1) ) if(dist>neoDiag) { neoDiag=dist; //printf("%d,%d,dist:%f\t",j,k,dist); } } //if(neoDiag!=0) //printf("%d,%f\n",vgen,neoDiag); // End of producing the initiating Adjacent Matrix /* //Caculating Average Degree of the generation if( (vgen+1) % aveDegInterval == 0 ) { for( j=0; j<N ; j++ ) for( k=0; k<N ; k++ ) if( adjmat[j][k] != 0) // k Can be perceived by j aveDegree+=1; aveDegree/=N; // Got it AveDeg[radRec][(vgen+1) / aveDegInterval] += aveDegree; // Storing Average Degree of this particular generation } */ // Updating each bird's gbest: for( j=0; j<N ; j++ ) { for( k=0; k<N ; k++ ) if( adjmat[j][k] != 0 ) // k Can be perceived by j // Updating the gbest of j if( coor[(dim+1)*k+dim] < gbest[(dim+1)*j+dim] ) for(l=0; l<=dim; l++) gbest[(dim+1)*j+l] = coor[(dim+1)*k+l]; //printf("gbest of %d:%f\nfitness of %d:%f\n",j,gbest[(dim+1)*j+dim],j,coor[(dim+1)*j+dim]); } // End of Updating each bird's gbest gb = gbest[dim]; // Updating the global gbest for(i = 1; i < N; i++) if(gbest[(dim+1)*i+dim] < gb) gb = gbest[(dim+1)*i+dim]; if( (vgen+1) % aveDegInterval == 0 ) { AveOptFit[radRec][(vgen+1) / aveDegInterval]+=gb; } if(gb < goal && sum != 0) { results[4*vtest] = (double)vgen+1; results[4*vtest+1] = gb; sum = 0; printf("\ngb:%f,%d\t",gb,vgen); //ngoal++; //break; } if(vgen == 1000) recordInd[vtest]=gb; if(vgen == gennum - 1) { results[4*vtest+2] = gennum; results[4*vtest+3] = gb; printf("lastgb:%f\n",gb); AveEndFit[radRec]+=gb; } } // for vgens printf("\nTest%d for Function%d of relRadius%f is done. The next one's coming at ya.\n\n",vtest,vfun,relRadius); } // for vtest FIndex[radRec]=countFIndex(recordInd); AveEndFit[radRec] = AveEndFit[radRec]/testnum; for(i = 0; i < testnum; i++) { if(results[4*i] != gennum) { ngoal++; //Suc Counter avggen += results[4*i]; res += results[4*i+1]; AveSucFit[radRec]+=results[4*i+3]; } } //printf("%d,%d", avggen, ngoal); if(ngoal == 0) { AveSucFit[radRec] = -1; avggennew = (double)gennum; } else { AveSucFit[radRec] /= ngoal; avggennew = (double)avggen / ngoal; } //printf("AVGEN:%f\t",avggennew); AveOptGen[radRec]=avggennew; //printf("AVRATE:%f",(double)ngoal / testnum * 100); AveOptRate[radRec]=(double)ngoal / testnum * 100; //printf("Function%d is tested under this radius.\n", vfun); printf("%1.30f,%1.30f,%f,%f,%f\n",AveEndFit[radRec],AveSucFit[radRec],AveOptGen[radRec],AveOptRate[radRec],FIndex[radRec]); // Record a radius in a row, literally fprintf(pso,"%1.30f,%1.30f,%f,%f,%f\n",AveEndFit[radRec],AveSucFit[radRec],AveOptGen[radRec],AveOptRate[radRec],FIndex[radRec]); // Record a radius in a row, literally radRec++; printf("\n\nRadius %d is tested\n\n",radRec); } // End of Loop layer 1 (relRadius) printf("\n\n Done! You got it, dude!"); fclose(pso); for(i=0; i<relRange; i++) for(j=0; j<=(gennum/aveDegInterval); j++) { AveOptFit[i][j]=(double)AveOptFit[i][j]/(testnum); } // Get da data in da file! sprintf(storage,"Gen,"); for(i=0; i<relRange; i++) { sprintf(buffer,"ItemRel%f,",0.24+i*0.02); strcat(storage,buffer); } /* pso = fopen("vicpsoFunc-v4.4-AveDeg.txt", "w"); fprintf(pso, "%s\n",storage ); for(j=0; j<=(gennum/aveDegInterval); j++) { int inIte; fprintf(pso,"%d,",j*10); for(inIte=0; inIte<relRange; inIte++) { fprintf(pso, "%f,",AveDeg[inIte][j]); } fprintf(pso, "\n"); } fclose(pso)*/ pso = fopen("../../vicpso-OptFit-dynarad.txt", "w"); fprintf(pso, "%s\n",storage ); for(j=0; j<=(gennum/aveDegInterval); j++) { int inIte; fprintf(pso,"%d,",j*10); for(inIte=0; inIte<relRange; inIte++) { fprintf(pso, "%f,",AveOptFit[inIte][j]); } fprintf(pso, "\n"); } fclose(pso); return 0; } // End of main
int main() { //printf("a"); srand((unsigned)time(NULL)); int i, j, k, l, vfun, vtest, vgen; float w, c1, c2, edge, Vmax, goal, gb, res = 0,avggennew=0; float vel[coornum], pbest[coornum], gbest[coornum], results[2*testnum],lbest[coornum]; pso = fopen("pso1-4.txt", "a+"); fprintf(pso, "num\tw\tc1\tc2\tfuncnum\tavggen\tratio\tfinalresult\n"); w = 0.7298; c1 = 1.49; c2 = 1.49; BAnet_produce(); for(vfun = 1; vfun < 5; vfun++) { if(vfun == 1) { edge = 100; Vmax = edge; goal = 0.01; } if(vfun == 2) { edge = 30; Vmax = edge; goal = 100; } if(vfun == 3) { edge = 5.12; Vmax = edge; goal = 100; } if(vfun == 4) { edge = 600; Vmax = edge; goal = 0.1; } int ngoal = 0, avggen = 0; for(i = 0; i < testnum; i++) { results[2*i] = gennum; results[2*i+1] = 0; } for(vtest = 0; vtest < testnum; vtest++) { int sum = 1; for(i = 0; i < coornum; i++) { pbest[i] = INT_MAX; gbest[i] = INT_MAX; } for(i = 0; i < dim + 1; i++) //粒子初始化; { for(j = 0; j < N; j++) { if(i < dim) { coor[(dim+1)*j+i] = (double)rand() / RAND_MAX * 2 * edge - edge; vel[(dim+1)*j+i] = (double)rand() / RAND_MAX * 2 * Vmax - Vmax; pbest[(dim+1)*j+i] = coor[(dim+1)*j+i]; //printf("%f\t", coor[(dim+1)*j+i]); } else { // i=dim if(vfun == 1) Fun1(j); if(vfun == 2) Fun2(j); if(vfun == 3) Fun3(j); if(vfun == 4) Fun4(j); //printf("\n%d:%f,", j, coor[(dim+1)*j+i]); pbest[(dim+1)*j+i] = coor[(dim+1)*j+i]; if(coor[(dim+1)*j+i] < gbest[(dim+1)*j+i]) { for(l = 0; l < dim + 1; l++) gbest[(dim+1)*j+l] = coor[(dim+1)*j+l]; //printf("gbest:%f\t", gbest[(dim+1)*j+i]); } for(k = 0; k < N; k++) if( gbest[(dim+1)*j+i] < gbest[(dim+1)*k+i]) { //printf("t[%d][%d],",j,k); for(l = 0; l < dim + 1; l++) gbest[(dim+1)*k+l] = gbest[(dim+1)*j+l]; } } } } /*for(i = 0; i < coornum; i++) { printf("\nabc:%f,%f,%f", coor[i], pbest[i], gbest[i]); if(i % (dim + 1) == dim) printf("\n"); }*/ for(vgen = 0; vgen < gennum; vgen++) //gennum代进化; { double PBNINDEX[2][N]; // Pbest & Index for(k=0; k<N; k++) { PBNINDEX[0][k]=pbest[(dim+1)*k+dim]; // pbest PBNINDEX[1][k]=k; // The index of particle } BubbleSort(PBNINDEX,N); // Done sorting the fitness while keeping the index for(j = 0; j < N; j++) { //printf("pbest:%f , "[(dim+1)*j+i]); int flag=1; for(k=0; k<N; k++) { if(t[j][(int)PBNINDEX[1][k]]!=INT_MAX && (int)PBNINDEX[1][k]!=j && flag==1) { for(i=0; i<dim+1; i++) { lbest[(dim+1)*j+i] = pbest[(dim+1)*((int)PBNINDEX[1][k])+i]; } flag=0; } } } for(i = 0; i < dim + 1; i++) { for(j = 0; j < N; j++) { if(i < dim) { float r1 = (double)rand() / RAND_MAX * 1; float r2 = (double)rand() / RAND_MAX * 1; vel[(dim+1)*j+i] = w * vel[(dim+1)*j+i] + c1 * r1 * (pbest[(dim+1)*j+i] - coor[(dim+1)*j+i])\ + c2 * r2 * (lbest[(dim+1)*j+i] - coor[(dim+1)*j+i]); /*if(vel[(dim+1)*j+i] > Vmax) vel[(dim+1)*j+i] = Vmax; if(vel[(dim+1)*j+i] < -Vmax) vel[(dim+1)*j+i] = -Vmax;*/ coor[(dim+1)*j+i] += vel[(dim+1)*j+i]; /*if(coor[(dim+1)*j+i] > edge) { coor[(dim+1)*j+i] = edge; vel[(dim+1)*j+i] = 0; } if(coor[(dim+1)*j+i] < -edge) { coor[(dim+1)*j+i] = -edge; vel[(dim+1)*j+i] = 0; }*/ } else { if(vfun == 1) Fun1(j); if(vfun == 2) Fun2(j); if(vfun == 3) Fun3(j); if(vfun == 4) Fun4(j); if(coor[(dim+1)*j+i] < pbest[(dim+1)*j+i]) for(l = 0; l < dim + 1; l++) pbest[(dim+1)*j+l] = coor[(dim+1)*j+l]; if(coor[(dim+1)*j+i] < gbest[(dim+1)*j+i]) { for(l = 0; l < dim + 1; l++) gbest[(dim+1)*j+l] = coor[(dim+1)*j+l]; } for(k = 0; k < N; k++) if( gbest[(dim+1)*j+i] < gbest[(dim+1)*k+i]) for(l = 0; l < dim + 1; l++) gbest[(dim+1)*k+l] = gbest[(dim+1)*j+l]; } } } gb = pbest[dim]; for(i = 1; i < N; i++) if(pbest[(dim+1)*i+dim] < gb) gb = pbest[(dim+1)*i+dim]; if(gb < goal && sum != 0) { results[2*vtest] = (double)vgen; results[2*vtest+1] = gb; sum = 0; //ngoal++; printf("gb:%f\n",gb); break; } if(vgen == gennum - 1) { results[2*vtest] = gennum - 1; results[2*vtest+1] = gb; } } // End of vgen } // End of vtest for(i = 0; i < testnum; i++) { if(results[2*i] != gennum - 1) { ngoal++; avggen += results[2*i]; res += results[2*i+1]; } } if(ngoal == 0) avggennew = (double)gennum - 1; else avggennew = (double)avggen / ngoal; printf("\n平均代数为:%f\n达优率为:%f%%\n", avggennew, (double)ngoal / testnum * 100); fprintf(pso, "\n平均代数为:%f\n达优率为:%f%%", avggennew, (double)ngoal / testnum * 100); } // end of vfun fclose(pso); return 0; }
int main() { //printf("a"); srand((unsigned)time(NULL)); int i, j, l, varc1, varc2, vfun, vtest, vgen; float x, c1, c2, edge, Vmax, goal, gb, res = 0,avggennew=0; float vel[coornum], pbest[coornum], pm[coornum], results[2*testnum]; float AveEndFit[4]; int sucCounter; for(i = 0; i < 4; i++) AveEndFit[i]=0; //BA(4); pso = fopen("pso1-4.txt", "a+"); fprintf(pso, "num\tw\tc1\tc2\tfuncnum\tavggen\tratio\tfinalresult\n"); /*for(i = 0; i < N; i++) { for(j = 0; j < N; j++) printf("%d,", t[i][j]); printf("\n"); }*/ x = 0.7298; c1 = 2.05; for(varc1 = 0; varc1 < 1; varc1++) { c2 = 2.05; for(varc2 = 0; varc2 < 1 ; varc2++) { for(vfun = 1; vfun < 5; vfun++) { for(i = 0; i < testnum; i++) { results[2*i] = gennum; results[2*i+1] = 0; } if(vfun == 1) { edge = 100; Vmax = edge; goal = 0.01; } if(vfun == 2) { edge = 30; Vmax = edge; goal = 100; } if(vfun == 3) { edge = 5.12; Vmax = edge; goal = 100; } if(vfun == 4) { edge = 600; Vmax = edge; goal = 0.1; } int ngoal = 0, avggen = 0; sucCounter = 0; for(vtest = 0; vtest < testnum; vtest++) { int sum = 1; for(i = 0; i < coornum; i++) { pbest[i] = INT_MAX; } for(i = 0; i < dim + 1; i++) //粒子初始化; { for(j = 0; j < N; j++) { if(i < dim) { coor[(dim+1)*j+i] = (double)rand() / RAND_MAX * 2 * edge - edge; vel[(dim+1)*j+i] = (double)rand() / RAND_MAX * 2 * Vmax - Vmax; pbest[(dim+1)*j+i] = coor[(dim+1)*j+i]; //printf("%f\t", coor[(dim+1)*j+i]); } else { // i=dim if(vfun == 1) Fun1(j); if(vfun == 2) Fun2(j); if(vfun == 3) Fun3(j); if(vfun == 4) Fun4(j); //printf("\n%d:%f,", j, coor[(dim+1)*j+i]); pbest[(dim+1)*j+i] = coor[(dim+1)*j+i]; } } } /*for(i = 0; i < coornum; i++) { printf("\nabc:%f,%f", coor[i], pbest[i]); if(i % (dim + 1) == dim) printf("\n"); }*/ for(vgen = 0; vgen < gennum; vgen++) //gennum代进化; { for(i = 0; i < dim + 1; i++) { for(j = 0; j < N; j++) { if(i < dim) { /* int pm_k; float ran[N],numerator=0; for(pm_k = 0; pm_k < N; pm_k++) { ran[pm_k]=(double)rand() / RAND_MAX * (c1+c2) / N; //printf(" %f ",ran[pm_k]); } for(pm_k = 0; pm_k < N; pm_k++) { numerator += ran[pm_k]*pbest[(dim+1)*pm_k+i]; //printf(" %f ",numerator); } pm[(dim+1)*j+i] = numerator/N; //printf(" %f ",pm[(dim+1)*j+i]); */ float r,acc=0; int k; for (k=0;k<N;k++){ r = (double)rand() / RAND_MAX * (c1+c2); acc+=r*(pbest[(dim+1)*k+i]-coor[(dim+1)*j+i]); } acc/=N; vel[(dim+1)*j+i] = x * ( vel[(dim+1)*j+i] + acc ); /*if(vel[(dim+1)*j+i] > Vmax) vel[(dim+1)*j+i] = Vmax; if(vel[(dim+1)*j+i] < -Vmax) vel[(dim+1)*j+i] = -Vmax;*/ coor[(dim+1)*j+i] += vel[(dim+1)*j+i]; /*if(coor[(dim+1)*j+i] > edge) { coor[(dim+1)*j+i] = edge; vel[(dim+1)*j+i] = 0; } if(coor[(dim+1)*j+i] < -edge) { coor[(dim+1)*j+i] = -edge; vel[(dim+1)*j+i] = 0; }*/ } else { if(vfun == 1) Fun1(j); if(vfun == 2) Fun2(j); if(vfun == 3) Fun3(j); if(vfun == 4) Fun4(j); if(coor[(dim+1)*j+i] < pbest[(dim+1)*j+i]) for(l = 0; l < dim + 1; l++) pbest[(dim+1)*j+l] = coor[(dim+1)*j+l]; } } } gb = pbest[dim]; for(i = 1; i < N; i++) if(pbest[(dim+1)*i+dim] < gb) gb = pbest[(dim+1)*i+dim]; //printf(" %f ",gb); if(gb < goal && sum != 0) { results[2*vtest] = (double)vgen; results[2*vtest+1] = gb; sum = 0; //ngoal++; printf("\ngb:%f,%d\t",gb,vgen); sucCounter+=1; } if(vgen == gennum - 1) { printf("lastgb:%f\n",gb); if(sum == 0) // Succeeded AveEndFit[vfun-1] += gb; else results[2*vtest] = vgen; } } // End of for VGEN! } // End of for VTEST ! if(sucCounter!=0) fprintf(pso,"\n\n\nLast succeeded average gb fitness:%f\n",AveEndFit[vfun-1]/sucCounter); else fprintf(pso,"\n\n\nLast succeeded average gb fitness: U gotta be kidding, man!\n"); for(i = 0; i < testnum; i++) { if(results[2*i] != gennum - 1) { ngoal++; avggen += results[2*i]; res += results[2*i+1]; } } printf("%d,%d", avggen/vtest, ngoal); if(ngoal == 0) avggennew = (double)gennum - 1; else avggennew = (double)avggen / ngoal; fprintf(pso, "\n平均代数为:%f\n达优率为:%f%%", avggennew, (double)sucCounter / testnum * 100); } // End of for VFUN! c2 += 0.1; } c1 += 0.1; } fclose(pso); }
Fun2 chain(Fun1 const &f1, Fun2 const f2) { return Fun2(FunctorChain<Fun1, Fun2>(f1, f2)); }
int main() { srand((unsigned)time(NULL)); /* Generic & Procedural Vars */ int i, j, k, l, vfun, vtest, vgen; float edge, Vmax, goal, gb; float vel[coornum], pbest[coornum], update[coornum]; /* Vars for Radius */ float absRadius; // Absolute perceptive radius float relRadius; // A iteration var for radius /* Vars for Real-time Recording */ int adjmat[N][N]; // Adjacent Matrix float results[2*testnum],res = 0,avggennew=0; // Results[] can be used to record gb int sucCounter, radRec; /* Vars for Storage Section */ float AveOptGen[relRange]; // FuncNumber*RadiusNumber: Every 19[radius] with a func float AveOptRate[relRange]; // FuncNumber*RadiusNumber: Every 19[radius] with a func //So the truth is, we're getting the average(a matrix) of average(a test) //float AveOptFit[relRange][gennum/aveDegInterval+1]; // The average of the gbest fitness of the interval generations float AveEndFit[relRange]; // End fit float AveSucFit[relRange];// SUCCESSFUL EndFit The average of the gbest fitness in the end of each SUCCESSFUL test (last generation) float FIndex[relRange], recordInd[testnum]; /* End of Storage Section */ for(i=0; i<relRange; i++) { AveOptGen[i]=0; AveOptRate[i]=0; AveSucFit[i]=0; AveEndFit[i]=0; FIndex[i]=99; } pso = fopen("../../vicfipso.txt", "a+"); radRec = 0; // Recording radius for(relRadius = relStart; relRadius < relEnd; relRadius += relStep) { for(vfun = 3; vfun < 4; vfun++) { for(i = 0; i < testnum; i++) { results[2*i] = gennum; results[2*i+1] = 0; } if(vfun == 1) { edge = 100; Vmax = edge; goal = 0.01; } if(vfun == 2) { edge = 30; Vmax = edge; goal = 100; } if(vfun == 3) { edge = 5.12; Vmax = edge; goal = 100; } if(vfun == 4) { edge = 600; Vmax = edge; goal = 0.1; } int ngoal = 0, avggen = 0; sucCounter = 0; for(vtest = 0; vtest < testnum; vtest++) { float neoDiag=0; float dist=0; int sum = 1; for(i = 0; i < coornum; i++) { pbest[i] = INT_MAX; } for(i = 0; i < dim + 1; i++) //粒子初始化; { for(j = 0; j < N; j++) { if(i < dim) { coor[(dim+1)*j+i] = (double)rand() / RAND_MAX * 2 * edge - edge; vel[(dim+1)*j+i] = (double)rand() / RAND_MAX * 2 * Vmax - Vmax; pbest[(dim+1)*j+i] = coor[(dim+1)*j+i]; //printf("%f\t", coor[(dim+1)*j+i]); } else { // i=dim if(vfun == 1) Fun1(j); if(vfun == 2) Fun2(j); if(vfun == 3) Fun3(j); if(vfun == 4) Fun4(j); //printf("\n%d:%f,", j, coor[(dim+1)*j+i]); pbest[(dim+1)*j+i] = coor[(dim+1)*j+i]; } } } absRadius = relRadius * sqrt(dim * pow(2 * edge , 2)); for(j=0; j<N; j++) for(k=0; k<N; k++) // Avoid Relapse { if( getDistance(j,k) <= absRadius && j!=k ) // k Can be perceived by j { adjmat[j][k]=1; } else { adjmat[j][k]=0; } } //End of initialization for(vgen = 0; vgen < gennum; vgen++) //gennum代进化; { get_vel_update(update, pbest, c1, c2, adjmat); for(i = 0; i < dim + 1; i++) { for(j = 0; j < N; j++) { if(i < dim) { vel[(dim+1)*j+i] = x * ( vel[(dim+1)*j+i] + update[(dim+1)*j+i] ); /*if(vel[(dim+1)*j+i] > Vmax) vel[(dim+1)*j+i] = Vmax; if(vel[(dim+1)*j+i] < -Vmax) vel[(dim+1)*j+i] = -Vmax;*/ coor[(dim+1)*j+i] += vel[(dim+1)*j+i]; /*if(coor[(dim+1)*j+i] > edge) { coor[(dim+1)*j+i] = edge; vel[(dim+1)*j+i] = 0; } if(coor[(dim+1)*j+i] < -edge) { coor[(dim+1)*j+i] = -edge; vel[(dim+1)*j+i] = 0; }*/ } else { if(vfun == 1) Fun1(j); if(vfun == 2) Fun2(j); if(vfun == 3) Fun3(j); if(vfun == 4) Fun4(j); if(coor[(dim+1)*j+i] < pbest[(dim+1)*j+i]) for(l = 0; l < dim + 1; l++) pbest[(dim+1)*j+l] = coor[(dim+1)*j+l]; } } } if( (vgen+1) % (changeInte) == 0 && vgen!=0) { //printf("%d,%f\n",vgen,neoDiag); absRadius=relRadius*neoDiag; } neoDiag=0;// reset for(j=0; j<N; j++) for(k=0; k<N; k++) // Avoid Relapse { dist=getDistance(j,k); if( getDistance(j,k) <= absRadius && j!=k ) // k Can be perceived by j { adjmat[j][k]=1; } else { adjmat[j][k]=0; } if( (vgen+1) % changeInte == (changeInte-1) ) if(dist>neoDiag) { neoDiag=dist; //printf("%d,%d,dist:%f\t",j,k,dist); } } gb = pbest[dim]; for(i = 1; i < N; i++) if(pbest[(dim+1)*i+dim] < gb) gb = pbest[(dim+1)*i+dim]; if(gb < goal && sum != 0) { results[2*vtest] = (double)vgen; results[2*vtest+1] = gb; sum = 0; //ngoal++; //printf("\ngb:%f,%d\t",gb,vgen); sucCounter+=1; } if(vgen == 1000) recordInd[vtest]=gb; if(vgen == gennum - 1) { printf("lastgb:%1.30f\n",gb); AveEndFit[radRec] += gb; if(sum == 0) // Succeeded AveSucFit[radRec] += gb; else results[2*vtest] = vgen; } } // End of for VGEN! //printf("%1.30f",gb); } // End of for VTEST ! FIndex[radRec]=countFIndex(recordInd); AveEndFit[radRec] = AveEndFit[radRec]/testnum; if(sucCounter!=0) AveSucFit[radRec] = AveSucFit[radRec]/sucCounter; for(i = 0; i < testnum; i++) { if(results[2*i] != gennum - 1) { ngoal++; avggen += results[2*i]; res += results[2*i+1]; } } printf("%d,%d", avggen/vtest, ngoal); if(ngoal == 0) avggennew = (float)gennum - 1; else avggennew = (float)avggen / ngoal; AveOptGen[radRec] = avggennew; AveOptRate[radRec] = (float)sucCounter / testnum * 100; //fprintf(pso, "\n平均达优代数为:%f\n达优率为:%f%%", avggennew, (double)sucCounter / testnum * 100); } // End of for VFUN! if(AveOptRate[radRec]==0){ //Failed in this radius printf("%1.30f,-1,%f,%f,%f\n",AveEndFit[radRec],AveOptGen[radRec],AveOptRate[radRec],FIndex[radRec]); // Record a radius in a row, literally fprintf(pso,"%1.30f,-1,%f,%f,%f\n",AveEndFit[radRec],AveOptGen[radRec],AveOptRate[radRec],FIndex[radRec]); // Record a radius in a row, literally } else { printf("%1.30f,%1.30f,%f,%f,%f\n",AveEndFit[radRec],AveSucFit[radRec],AveOptGen[radRec],AveOptRate[radRec],FIndex[radRec]); // Record a radius in a row, literally fprintf(pso,"%1.30f,%1.30f,%f,%f,%f\n",AveEndFit[radRec],AveSucFit[radRec],AveOptGen[radRec],AveOptRate[radRec],FIndex[radRec]); // Record a radius in a row, literally } radRec++; } // End of relRadius fclose(pso); }
int main() { srand((unsigned)time(NULL)); /* Generic & Procedural Vars */ int i, j, k, l, vfun, vtest, vgen; double edge, Vmax, goal, gb; /* Vars for Real-time Recording */ int sucCounter; /* Vars for Storage Section */ double AveOptGen; // Average number of generations of success optimization double AveOptRate; // Ratio of success tests/ total tests double AveEndFit; // End fit double AveSucFit;// SUCCESSFUL EndFit The average of the gbest fitness in the end of each SUCCESSFUL test (last generation) double FIndex, recordInd[testnum]; /* End of Storage Section */ /* Initialization of some variables */ AveOptGen=0; AveOptRate=0; AveSucFit=0; AveEndFit=0; FIndex=99; // Just a dummy and impossible value pso = fopen("clpso.txt", "a+"); // We cannot use 'w' on the server in the lab in lack of privileges. vfun=1; switch(vfun) { case 1: edge = 100; goal = 0.01; break; case 2: edge = 30; goal = 100; break; case 3: edge = 5.12; goal = 100; break; case 4: edge = 600; goal = 0.1; break; default: edge = 100; goal = 0.01; } Vmax = edge; //Vmax = edge/2; //int ngoal = 0, avggen = 0; sucCounter = 0; for(vtest = 0; vtest < testnum; vtest++) { int sum = 1; for(i = 0; i < coornum; i++) { pbest[i] = INT_MAX; gbest[i] = INT_MAX; } for(i = 0; i < dim + 1; i++) //Initialization { for(j = 0; j < N; j++) { if(i < dim) { coor[(dim+1)*j+i] = (double)rand() / RAND_MAX * 2 * edge - edge; vel[(dim+1)*j+i] = (double)rand() / RAND_MAX * 2 * Vmax - Vmax; pbest[(dim+1)*j+i] = coor[(dim+1)*j+i]; //printf("%f\t", coor[(dim+1)*j+i]); } else { if(vfun == 1) Fun1(j); if(vfun == 2) Fun2(j); if(vfun == 3) Fun3(j); if(vfun == 4) Fun4(j); //printf("\n%d:%f,", j, coor[(dim+1)*j+i]); pbest[(dim+1)*j+i] = coor[(dim+1)*j+i]; if(coor[(dim+1)*j+i] < gbest[(dim+1)*j+i]) { for(l = 0; l < dim + 1; l++) gbest[(dim+1)*j+l] = coor[(dim+1)*j+l]; //printf("gbest:%f\t", gbest[(dim+1)*j+i]); } for(k = 0; k < N; k++) if( gbest[(dim+1)*j+i] < gbest[(dim+1)*k+i]) { //printf("t[%d][%d],",j,k); for(l = 0; l < dim + 1; l++) gbest[(dim+1)*k+l] = gbest[(dim+1)*j+l]; } } } } // refgap_flag: A flag for counting the refreshing gap // ite_a: A iteration counting variable // cl_rec: A 2-dim recording array for comprehensive learning int refgap_flag = 0, ite_a, cl_rec[N]; for(vgen = 0; vgen < gennum; vgen++) //gennum´ú½ø»¯£» { if(refgap_flag == refgap ) refgap_flag = 0; float inert_w; inert_w = (wmax-wmin)*(gennum-vgen-1)/gennum +wmin; for(i = 0; i < dim + 1; i++) { if(refgap_flag == 0 ) { for(ite_a=0; ite_a<N; ite_a++) { float rand_p = (float) rand() / RAND_MAX * 1; if(rand_p<=Prob_c) { // Tournament int learnee1=ceil(rand() / RAND_MAX * N) - 1; // Get a integer number from 0 to N-1 int learnee2=ceil(rand() / RAND_MAX * N) - 1; // Get a integer number from 0 to N-1 if(pbest[(dim+1)*learnee1+dim]< pbest[(dim+1)*learnee2+dim]) cl_rec[ite_a] = learnee1; else cl_rec[ite_a] = learnee2; // End of tournament } else cl_rec[ite_a] = ite_a; } // End of for(ite_a=0; ite_a<N; ite_a++; ) } // End of if(refgap_flag == 0 ) for(j = 0; j < N; j++) { if(i < dim) { double rand_1 = (double)rand() / RAND_MAX * 1; //double r2 = (double)rand() / RAND_MAX * 1; vel[(dim+1)*j+i] = inert_w * vel[(dim+1)*j+i] + para_c * rand_1 * (pbest[(dim+1)*cl_rec[j]+i] - coor[(dim+1)*j+i]); //vel[(dim+1)*j+i] = getMin( getMax(vel[(dim+1)*j+i],(-Vmax) ),Vmax); coor[(dim+1)*j+i] += vel[(dim+1)*j+i]; // Moving } else { if(vfun == 1) Fun1(j); if(vfun == 2) Fun2(j); if(vfun == 3) Fun3(j); if(vfun == 4) Fun4(j); if(coor[(dim+1)*j+i] < pbest[(dim+1)*j+i]) for(l = 0; l < dim + 1; l++) pbest[(dim+1)*j+l] = coor[(dim+1)*j+l]; if(coor[(dim+1)*j+i] < gbest[(dim+1)*j+i]) { for(l = 0; l < dim + 1; l++) gbest[(dim+1)*j+l] = coor[(dim+1)*j+l]; } for(k = 0; k < N; k++) if( gbest[(dim+1)*j+i] < gbest[(dim+1)*k+i]) for(l = 0; l < dim + 1; l++) gbest[(dim+1)*k+l] = gbest[(dim+1)*j+l]; } // End of if/else (i < dim) } // End of for(j = 0; j < N; j++) } // End of (i = 0; i < dim + 1; i++) refgap_flag++; gb = gbest[dim]; for(i = 1; i < N; i++) if(gbest[(dim+1)*i+dim] < gb) gb = gbest[(dim+1)*i+dim]; if(gb < goal && sum != 0) { sum = 0; // Change the flag //ngoal++; sucCounter += 1; AveOptGen += vgen; printf("suc gb:%1.30f\n",gb); } if(vgen == 1000) recordInd[vtest]=gb; if(vgen == gennum - 1) { printf("last gb:%1.30f\n",gb); AveEndFit += gb; if(sum == 0) // Succeeded AveSucFit += gb; } } // End of for VGEN! } // End of vtest FIndex=countFIndex(recordInd); AveEndFit = AveEndFit/testnum; if(sucCounter!=0) // If succeeded { AveSucFit = AveSucFit/sucCounter; AveOptGen = AveOptGen/sucCounter; } else { AveSucFit = -1; // A flag to show the failure AveOptGen = -1; // A flag to show the failure } AveOptRate=(double) sucCounter / testnum * 100; printf("%1.30f,%1.30f,%f,%f,%f\n",AveEndFit,AveSucFit,AveOptGen,AveOptRate,FIndex); fprintf(pso,"%1.30f,%1.30f,%f,%f,%f\n",AveEndFit,AveSucFit,AveOptGen,AveOptRate,FIndex); // AveSucFit is the Average Endfit for only successful runs fclose(pso); return 0; }
int main() { srand((unsigned)time(NULL)); /* Generic & Procedural Vars */ int i, j, k, l, vfun, vtest, vgen; double edge, Vmax, goal, gb; /* Vars for Radius */ float absRadius; // Absolute perceptive radius float relRadius; // A iteration var for radius /* Vars for Real-time Recording */ int adjmat[N][N]; // Adjacent Matrix int sucCounter; int radRec; // A var to count the index of the radius /* Vars for Storage Section */ double AveOptGen[relRange]; // Average number of generations of success optimization double AveOptRate[relRange]; // Ratio of success tests/ total tests double AveEndFit[relRange]; // End fit double AveSucFit[relRange];// SUCCESSFUL EndFit The average of the lbest fitness in the end of each SUCCESSFUL test (last generation) double FIndex[relRange], recordInd[testnum]; /* End of Storage Section */ /* Initialization of some variables */ for(i=0; i<relRange; i++) { AveOptGen[i]=0; AveOptRate[i]=0; AveSucFit[i]=0; AveEndFit[i]=0; FIndex[i]=99; // Just a dummy and impossible value } pso = fopen("vicclpso.txt", "a+"); // We cannot use 'w' on the server in the lab in lack of privileges. vfun=1; switch(vfun) { case 1: edge = 100; goal = 0.01; break; case 2: edge = 30; goal = 100; break; case 3: edge = 5.12; goal = 100; break; case 4: edge = 600; goal = 0.1; break; default: edge = 100; goal = 0.01; } Vmax = edge; //Vmax = edge/2; radRec=0; for(relRadius=relStart; relRadius<relEnd; relRadius+=relStep) // Loop layer 1 (relRadius) { //int ngoal = 0, avggen = 0; sucCounter = 0; for(vtest = 0; vtest < testnum; vtest++) { int sum = 1; for(i = 0; i < coornum; i++) { pbest[i] = INT_MAX; gbest[i] = INT_MAX; } for(i = 0; i < dim + 1; i++) //Initialization { for(j = 0; j < N; j++) { if(i < dim) { coor[(dim+1)*j+i] = (double)rand() / RAND_MAX * 2 * edge - edge; vel[(dim+1)*j+i] = (double)rand() / RAND_MAX * 2 * Vmax - Vmax; pbest[(dim+1)*j+i] = coor[(dim+1)*j+i]; //printf("%f\t", coor[(dim+1)*j+i]); } else { if(vfun == 1) Fun1(j); if(vfun == 2) Fun2(j); if(vfun == 3) Fun3(j); if(vfun == 4) Fun4(j); //printf("\n%d:%f,", j, coor[(dim+1)*j+i]); pbest[(dim+1)*j+i] = coor[(dim+1)*j+i]; } } } // End of for(i = 0; i < dim + 1; i++) absRadius = relRadius * sqrt(dim * pow(2 * edge , 2)); for(j=0; j<N; j++) for(k=0; k<N; k++) // Avoid Relapse { if( getDistance(j,k) <= absRadius && j!=k ) // k Can be perceived by j { adjmat[j][k]=1; } else { adjmat[j][k]=0; } } // Updating each bird's gbest: for(j=0; j<N; j++) { for(k=0; k<N; k++) if( adjmat[j][k] != 0 ) // k Can be perceived by j { // Updating the gbest of j //printf("%f\t",getDistance(j,k)); if( coor[(dim+1)*k+dim] < gbest[(dim+1)*j+dim] ) { for(l=0; l<=dim; l++) gbest[(dim+1)*j+l] = coor[(dim+1)*k+l]; } } //printf("gbest of %d:%f\nfitness of %d:%f\n",j,gbest[(dim+1)*j+dim],j,coor[(dim+1)*j+dim]); } gb = gbest[dim]; // Updating the global gbest for(i = 1; i < N; i++) if(gbest[(dim+1)*i+dim] < gb) gb = gbest[(dim+1)*i+dim]; // End of Updating each bird's gbest in initialization //End of initialization // refgap_flag: A flag for counting the refreshing gap // ite_a: A iteration counting variable // cl_rec: A 2-dim recording array for comprehensive learning int refgap_flag = 0, ite_a, cl_rec[N]; for(vgen = 0; vgen < gennum; vgen++) //gennum´ú½ø»¯£» { if(refgap_flag == refgap ) refgap_flag = 0; float inert_w; inert_w = (wmax-wmin)*(gennum-vgen-1)/gennum +wmin; for(i = 0; i < dim + 1; i++) { if(refgap_flag == 0 ) { for(ite_a=0; ite_a<N; ite_a++) { float rand_p = (float) rand() / RAND_MAX * 1; if(rand_p<=Prob_c) { // Getting the learnee // learnee 1 int lflag=0,learnee1,learnee2,ite_count; ite_count = 0; while( (lflag==0) && (ite_count<N) ) { learnee1=floor(rand() / RAND_MAX * N); // Get a integer number from 0 to N-1 if(adjmat[learnee1][ite_a]!=0) lflag = 1; else ite_count++; } if(ite_count==N) learnee1 = ite_a; // learnee 2 lflag=0; ite_count = 0; while( (lflag==0) && (ite_count<N) ) { learnee2=floor(rand() / RAND_MAX * N); // Get a integer number from 0 to N-1 if(adjmat[learnee2][ite_a]!=0) lflag = 1; else ite_count++; } if(ite_count==N) learnee2 = ite_a; // Tournament //int learnee1=floor(rand() / RAND_MAX * N); // Get a integer number from 0 to N-1 //int learnee2=floor(rand() / RAND_MAX * N); // Get a integer number from 0 to N-1 if(pbest[(dim+1)*learnee1+dim]< pbest[(dim+1)*learnee2+dim]) cl_rec[ite_a] = learnee1; else cl_rec[ite_a] = learnee2; // End of tournament // End of Getting the learnee } else cl_rec[ite_a] = ite_a; //printf("clrec[%d]",cl_rec[ite_a]); } // End of for(ite_a=0; ite_a<N; ite_a++; ) //printf("\n"); } // End of if(refgap_flag == refgap || refgap_flag == 0 ) for(j = 0; j < N; j++) { if(i < dim) { if(adjmat[cl_rec[j]][j]==0) cl_rec[j]=j; //printf("clrec[%d]",cl_rec[j]); double rand_1 = (double)rand() / RAND_MAX * 1; //double r2 = (double)rand() / RAND_MAX * 1; vel[(dim+1)*j+i] = inert_w * vel[(dim+1)*j+i] + para_c * rand_1 * (pbest[(dim+1)*cl_rec[j]+i] - coor[(dim+1)*j+i]); //vel[(dim+1)*j+i] = getMin( getMax(vel[(dim+1)*j+i],(-Vmax) ),Vmax); coor[(dim+1)*j+i] += vel[(dim+1)*j+i]; // Moving } else { if(vfun == 1) Fun1(j); if(vfun == 2) Fun2(j); if(vfun == 3) Fun3(j); if(vfun == 4) Fun4(j); if(coor[(dim+1)*j+i] < pbest[(dim+1)*j+i]) for(l = 0; l < dim + 1; l++) pbest[(dim+1)*j+l] = coor[(dim+1)*j+l]; } // End of if/else (i < dim) } // End of for(j = 0; j < N; j++) } // End of (i = 0; i < dim + 1; i++) refgap_flag++; // Producing the initiating Adjacent Matrix for(j=0; j<N; j++) for(k=0; k<N; k++) // Avoid Relapse { if( getDistance(j,k) <= absRadius && j!=k ) // k Can be perceived by j { adjmat[j][k]=1; } else { adjmat[j][k]=0; } } // Updating each bird's gbest: for( j=0; j<N ; j++ ) { for( k=0; k<N ; k++ ) if( adjmat[j][k] != 0 ) // k Can be perceived by j // Updating the gbest of j if( coor[(dim+1)*k+dim] < gbest[(dim+1)*j+dim] ) for(l=0; l<=dim; l++) gbest[(dim+1)*j+l] = coor[(dim+1)*k+l]; //printf("gbest of %d:%f\nfitness of %d:%f\n",j,gbest[(dim+1)*j+dim],j,coor[(dim+1)*j+dim]); } // End of Updating each bird's gbest // Getting the global optimum gb = gbest[dim]; for(i = 1; i < N; i++) if(gbest[(dim+1)*i+dim] < gb) gb = gbest[(dim+1)*i+dim]; // Evaluating if(gb < goal && sum != 0) { sum = 0; // Change the flag //ngoal++; sucCounter += 1; AveOptGen[radRec] += vgen; printf("suc gb:%1.15f\n",gb); } if(vgen + 1 == 1000) recordInd[vtest]=gb; if(vgen == gennum - 1) { printf("last gb:%1.15f\n",gb); AveEndFit[radRec] += gb; if(sum == 0) // Succeeded AveSucFit[radRec] += gb; } } // End of for VGEN! } // End of vtest FIndex[radRec]=countFIndex(recordInd); AveEndFit[radRec] = AveEndFit[radRec]/testnum; if(sucCounter!=0) // If succeeded { AveSucFit[radRec] = AveSucFit[radRec]/sucCounter; AveOptGen[radRec] = AveOptGen[radRec]/sucCounter; } else { AveSucFit[radRec] = -1; // A flag to show the failure AveOptGen[radRec] = -1; // A flag to show the failure } AveOptRate[radRec]=(double) sucCounter / testnum * 100; printf("%1.30f,%1.30f,%f,%f,%f\n",AveEndFit[radRec],AveSucFit[radRec],AveOptGen[radRec],AveOptRate[radRec],FIndex[radRec]); fprintf(pso,"%1.30f,%1.30f,%f,%f,%f\n",AveEndFit[radRec],AveSucFit[radRec],AveOptGen[radRec],AveOptRate[radRec],FIndex[radRec]); // AveSucFit is the Average Endfit for only successful runs radRec++; printf("\n\nRadius %d is tested\n\n",radRec); // AveSucFit is the Average Endfit for only successful runs } // End of Loop layer 1 (relRadius) printf("\n\n Done! You got it, dude!"); fclose(pso); return 0; }