📄 prog.cpp
字号:
/* find total fitness of the population */for (mem = 0; mem < POPSIZE; mem++) { sum += population[mem].fitness; }/* calculate relative fitness */for (mem = 0; mem < POPSIZE; mem++) { population[mem].rfitness = population[mem].fitness/sum; }population[0].cfitness = population[0].rfitness;/* calculate cumulative fitness */for (mem = 1; mem < POPSIZE; mem++) { population[mem].cfitness = population[mem-1].cfitness + population[mem].rfitness; }/* finally select survivors using cumulative fitness. */for (i = 0; i < POPSIZE; i++) { p = rand()%1000/1000.0; if (p < population[0].cfitness) newpopulation[i] = population[0]; else { for (j = 0; j < POPSIZE;j++) if (p >= population[j].cfitness && p<population[j+1].cfitness) newpopulation[i] = population[j+1]; } }/* once a new population is created, copy it back */for (i = 0; i < POPSIZE; i++) population[i] = newpopulation[i]; }//锦标赛void select_tournament(void){int i, j;int r,s;for(i=0;i<POPSIZE;i++){ r=rand()%POPSIZE; do{s=rand()%POPSIZE; }while(r==s); if(population[r].fitness>population[s].fitness) { newpopulation[i].fitness=population[r].fitness; for(j=0;j<NVARS;j++) newpopulation[i].gene[j]=population[r].gene[j]; }else { newpopulation[i].fitness=population[s].fitness; for(j=0;j<NVARS;j++) newpopulation[i].gene[j]=population[s].gene[j]; }}for (i = 0; i < POPSIZE; i++) population[i] = newpopulation[i]; }//期望值void select_expect(void){ int i=0,k=0,p=0,r=0,j=0; double temp=0.0; double sum=0.0; double savenum[POPSIZE]={0}; int index[POPSIZE]={0}; //计算群体中各个个体在下一代中的期望生存数目 for(i=0;i<POPSIZE;i++) sum=sum+population[i].fitness; for(i=0;i<POPSIZE;i++) savenum[i]=POPSIZE*population[i].fitness/sum; //产生下一代群体 k=-1; for(i=0;i<POPSIZE;i++) { p=(int)savenum[i]; if(p>0) { for(j=1;j<=p;j++) { k++; for(r=0;r<NVARS;r++) newpopulation[j].gene[r]=population[j].gene[r]; } } } for(i=0;i<POPSIZE;i++) { savenum[i]=savenum[i]-(int)savenum[i]; index[i]=i; } for(i=0;i<POPSIZE-1;i++) { for(j=i+1;j<POPSIZE;j++) { if(savenum[i]>savenum[j]) { temp=savenum[i]; savenum[i]=savenum[j]; savenum[j]=temp; p=index[i]; index[i]=index[j]; index[j]=p; } } } j=k+1; for(i=0;i<POPSIZE-j;i++) { k++; for(r=0;r<NVARS;r++) newpopulation[k].gene[r]=population[ index[i] ].gene[r]; }}/***************************************************************//* Crossover selection: selects two parents that take part in *//* the crossover. Implements a single point crossover *//***************************************************************/void crossover(void){int mem, one;int first = 0; /* count of the number of members chosen */double x;for (mem = 0; mem < POPSIZE; ++mem) { x = rand()%1000/1000.0; if (x < PXOVER) { ++first; if (first % 2 == 0) Xover(one, mem); else one = mem; } }}/**************************************************************//* Crossover: performs crossover of the two selected parents. *//**************************************************************/void Xover(int one, int two){int i;int point; /* crossover point *//* select crossover point */if(NVARS > 1) { if(NVARS == 2) point = 1; else point = (rand() % (NVARS - 1)) + 1; for (i = 0; i < point; i++) swap(&population[one].gene[i], &population[two].gene[i]); }}/*************************************************************//* Swap: A swap procedure that helps in swapping 2 variables *//*************************************************************/void swap(double *x, double *y){double temp;temp = *x;*x = *y;*y = temp;}/**************************************************************//* Mutation: Random uniform mutation. A variable selected for *//* mutation is replaced by a random value between lower and *//* upper bounds of this variable *//**************************************************************/void mutate(void){int i, j;double lbound, hbound;double x;for (i = 0; i < POPSIZE; i++) for (j = 0; j < NVARS; j++) { x = rand()%1000/1000.0; if (x < PMUTATION) { /* find the bounds on the variable to be mutated */ lbound = population[i].lower[j]; hbound = population[i].upper[j]; population[i].gene[j] = randval(lbound, hbound); } }}/***************************************************************//* Report function: Reports progress of the simulation. Data *//* dumped into the output file are separated by commas *//***************************************************************/void report(void){int i;double best_val; /* best population fitness */double avg; /* avg population fitness */double stddev; /* std. deviation of population fitness */double sum_square; /* sum of square for std. calc */double square_sum; /* square of sum for std. calc */double sum; /* total population fitness */sum = 0.0;sum_square = 0.0;for (i = 0; i < POPSIZE; i++) { sum += population[i].fitness; sum_square += population[i].fitness * population[i].fitness; }avg = sum/(double)POPSIZE;square_sum = avg * avg * POPSIZE;stddev = sqrt((sum_square - square_sum)/(POPSIZE - 1));//stddev = ((sum_square - square_sum)/(POPSIZE - 1));////////////////// give up 'sqrt'best_val = population[POPSIZE].fitness;fprintf(galog, "\n%5d\t%6.9f", generation,best_val);}/**************************************************************//* Main function: Each generation involves selecting the best *//* members, performing crossover & mutation and then *//* evaluating the resulting population, until the terminating *//* condition is satisfied *//**************************************************************/void main(void){// add 2007-9-29 将 output 置于全局,以适用于 MAXGENS 突破 507 。if ((output = fopen("output.dat","w+"))==NULL) { exit(1); }if ((galog = fopen("galog.txt","w+"))==NULL) { exit(1); }int i=0;generation = 0;fprintf(galog, "\n generation best average standard \n");fprintf(galog, " number\tbest_fitness\n"); generation = 0; srand(time(NULL)); int sel=0; printf("\n------------------------------------------------------------------\n"); printf("\n 遗传算法 选择算子 问题 \n"); printf("\n------------------------------------------------------------------\n\n"); printf("选择方法:1、轮盘赌 2、锦标赛 3、期望值方法 4、选择排序\n"); printf(">>"); scanf("%d",&sel); initialize();//read data and initialize 'population' evaluate(); keep_the_best();//This function keeps track of the best member of the population. while(generation<MAXGENS) { generation++; if(sel==1) select(); else if(sel==2) select_tournament(); else if(sel==3) select_expect(); else select_ranking(); crossover(); mutate(); report(); evaluate(); elitist(); }//whilefprintf(galog,"\n\n Simulation completed\n");fprintf(galog,"\n Best member: \n");for (i = 0; i < NVARS; i++) { fprintf (galog,"\n var(%d) = %3.9f",i,population[POPSIZE].gene[i]); }fprintf(galog,"\n\n Best fitness = %3.9f",population[POPSIZE].fitness);fclose(galog);fclose(output);printf("Success\n");}/***************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -