《遗传算法.doc》由会员分享,可在线阅读,更多相关《遗传算法.doc(11页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、一 需求分析 1本程序演示的是用简单遗传算法随机一个种群,然后根据所给的交叉率,变异率,世代 数计算最大适应度所在的代数 2演示程序以用户和计算机的对话方式执行,即在计算机终端上显示“提示信息”之后, 由用户在键盘上输入演示程序中规定的命令;相应的输入数据和运算结果显示在其后。 3测试数据 输入初始变量后用 y=100*(x1*x1-x2)*(x1*x2-x2)+(1-x1)*(1-x1)其中-2.048 #include #include #include #define POPSIZE 500 #define maximization 1 #define minimization 2 #d
2、efine cmax 100 #define cmin 0 #define length1 10 #define length2 10 #define chromlength length1+length2 /染色体长度int functionmode=maximization;int popsize; /种群大小 int maxgeneration; /最大世代数double pc; /交叉率double pm; /变异率 struct individualchar chromchromlength+1; double value; double fitness; /适应度 ; int ge
3、neration; /世代数 int best_index; int worst_index; struct individual bestindividual; /最佳个体 struct individual worstindividual; /最差个体 struct individual currentbest;struct individual populationPOPSIZE; /函数声明 void generateinitialpopulation(); void generatenextpopulation(); void evaluatepopulation(); long d
4、ecodechromosome(char *,int,int); void calculateobjectvalue(); void calculatefitnessvalue(); void findbestandworstindividual(); void performevolution(); void selectoperator(); void crossoveroperator(); void mutationoperator(); void input(); void outputtextreport();void generateinitialpopulation( ) /种
5、群初始化 int i,j;for (i=0;i0.0)temp=cmin+populationi.value;elsetemp=0.0; else if (functionmode=minimization)if(populationi.valuebestindividual.fitness) bestindividual=populationi; best_index=i; else if (populationi.fitness=currentbest.fitness) currentbest=bestindividual; void performevolution() /演示评价结果
6、if (bestindividual.fitnesscurrentbest.fitness) currentbest=populationbest_index; else populationworst_index=currentbest; void selectoperator() /比例选择算法 int i,index; double p,sum=0.0; double cfitnessPOPSIZE;struct individual newpopulationPOPSIZE; for(i=0;icfitnessindex) index+; newpopulationi=populati
7、onindex; for(i=0;ipopsize; i+) populationi=newpopulationi; void crossoveroperator() /交叉算法 int i,j; int indexPOPSIZE; int point,temp; double p; char ch;for (i=0;ipopsize;i+) indexi=i; for (i=0;ipopsize;i+) point=rand()%(popsize-i); temp=indexi; indexi=indexpoint+i; indexpoint+i=temp; for (i=0;ipopsiz
8、e-1;i+=2) p=rand()%1000/1000.0; if (ppc) point=rand()%(chromlength-1)+1; for (j=point; jchromlength;j+) ch=populationindexi.chromj; populationindexi.chromj=populationindexi+1.chromj; populationindexi+1.chromj=ch; void mutationoperator() /变异操作 int i,j; double p;for (i=0;ipopsize;i+) for(j=0;jchromlen
9、gth;j+) p=rand()%1000/1000.0; if (ppm) populationi.chromj=(populationi.chromj=0)?1:0; void input() /数据输入 printf(“初始化全局变量:n“); printf(“ 种群大小(50-500):“);scanf(“%d“, if(popsize%2) != 0) printf( “ 种群大小已设置为偶数n“); popsize+;printf(“ 最大世代数(100-300):“);scanf(“%d“, printf(“ 交叉率(0.2-0.99):“);scanf(“%f“, printf
10、(“ 变异率(0.001-0.1):“);scanf(“%f“, void outputtextreport()/数据输出 int i; double sum; double average; sum=0.0; for(i=0;ipopsize;i+) sum+=populationi.value; average=sum/popsize;printf(“当前世代=%dn 当前世代平均函数值=%fn 当前世代最高函数值 =%fn“,generation,average,populationbest_index.value); void main() /主函数 int i; printf(“本程
11、序为求函数 y=100*(x1*x1-x2)*(x1*x2-x2)+(1-x1)*(1-x1)的最大值 n 其中- 2.048=x1,x2=2.048n“);generation=0;input();generateinitialpopulation(); evaluatepopulation(); while(generationmaxgeneration) generation+; generatenextpopulation(); evaluatepopulation(); performevolution(); outputtextreport(); printf(“n“); printf(“ 统计结果: “);printf(“n“); printf(“最大函数值等于:%fn“,currentbest.fitness); printf(“其染色体编码为:“); for (i=0;ichromlength;i+) printf(“%c“,currentbest.chromi);printf(“n“); 六 测试结果
限制150内