资源预览内容
第1页 / 共7页
第2页 / 共7页
第3页 / 共7页
第4页 / 共7页
第5页 / 共7页
第6页 / 共7页
第7页 / 共7页
亲,该文档总共7页全部预览完了,如果喜欢就下载吧!
资源描述
遗传算法代码#include #include #include #include #include#define cities 10/城市的个数#define MAXX 100/迭代次数#define pc 0.8 /交配概率#define pm 0.05 /变异概率#define num 10/种群的大小int bestsolution;/最优染色体int distancecitiescities;/城市之间的距离struct group/染色体的结构int citycities;/城市的顺序int adapt;/适应度double p;/在种群中的幸存概率groupnum,grouptempnum;/随机产生 10 个城市之间的相互距离void init()int i,j; memset(distance,0,sizeof(distance); srand(unsigned)time(NULL); for(i=0;icities;i+)for(j=i+1;jcities;j+)distanceij=rand()%100; distanceji=distanceij;printf(*城市的距离矩阵如下*n); for(i=0;icities;i+)for(j=0;jcities;j+)printf(%4d,distanceij); printf(n);/随机产生初试群void groupproduce()int i,j,t,k,flag; for(i=0;inum;i+)/初始化for(j=0;jcities;j+) groupi.cityj=-1; srand(unsigned)time(NULL); for(i=0;inum;i+)for(j=0;jcities;)t=rand()%cities; flag=1; for(k=0;kj;k+)if(groupi.cityk=t)flag=0; break;if(flag)groupi.cityj=t; j+;printf(*初始种群如下*n); for(i=0;inum;i+)for(j=0;jcities;j+) printf(%4d,groupi.cityj); printf(n);/评价函数,找出最优染色体void pingjia()int i,j;int n1,n2;int sumdistance,biggestsum=0; double biggestp=0; for(i=0;inum;i+)sumdistance=0; for(j=1;jcities;j+)n1=groupi.cityj-1; n2=groupi.cityj; sumdistance+=distancen1n2;groupi.adapt=sumdistance; /每条染色体的路径总和biggestsum+=sumdistance; /种群的总路径for(i=0;inum;i+)groupi.p=1-(double)groupi.adapt/(double)biggestsum; biggestp+=groupi.p;for(i=0;inum;i+) groupi.p=groupi.p/biggestp;/求最佳路劲bestsolution=0; for(i=0;igroupbestsolution.p) bestsolution=i;/选择void xuanze()int i,j,temp;double gradientnum;/梯度概率double xuanzenum;/选择染色体的随机概率int xuannum;/选择了的染色体/初始化梯度概率for(i=0;inum;i+)gradienti=0.0; xuanzei=0.0;gradient0=group0.p; for(i=1;inum;i+) gradienti=gradienti-1+groupi.p; srand(unsigned)time(NULL);/随机产生染色体的存活概率for(i=0;inum;i+)xuanzei=(rand()%100); xuanzei/=100;/选择能生存的染色体for(i=0;inum;i+)for(j=0;jnum;j+)if(xuanzeigradientj)xuani=j; /第 i 个位置存放第 j 个染色体break;/拷贝种群for(i=0;inum;i+)grouptempi.adapt=groupi.adapt; grouptempi.p=groupi.p; for(j=0;jcities;j+) grouptempi.cityj=groupi.cityj;/数据更新for(i=0;inum;i+)temp=xuani; groupi.adapt=grouptemptemp.adapt; groupi.p=grouptemptemp.p; for(j=0;jcities;j+) groupi.cityj=grouptemptemp.cityj;/变异void bianyi()int i,j; int t;int temp1,temp2,point;double bianyipnum; /染色体的变异概率int bianyiflagnum;/染色体的变异情况for(i=0;inum;i+)/初始化bianyiflagi=0;/随机产生变异概率srand(unsigned)time(NULL); for(i=0;inum;i+)bianyipi=(rand()%100); bianyipi/=100;/确定可以变异的染色体t=0;for(i=0;inum;i+)if(bianyipipm)bianyiflagi=1; t+;/变异操作,即交换染色体的两个节点srand(unsigned)time(NULL); for(i=0;inum;i+)if(bianyiflagi=1)temp1=rand()%10; temp2=rand()%10; point=groupi.citytemp1;groupi.citytemp1=groupi.citytemp2; groupi.citytemp2=point;int main()system(color 3e); char choice;doint i,j,t;init(); groupproduce();pingjia(); t=0;while(t+MAXX)xuanze();bianyi(); pingjia();printf(n*输出最终的种群的适应度*n); for(i=0;inum;i+)for(j=0;jcities;j+)printf(%4d,groupi.cityj);printf(适应度:%4dn,groupi.adapt);printf(*最优解为%d 号染色体*n,bestsolution);printf(n*是否想再一次计算(y or n)*n); fflush(stdin);scanf(%c,&choice);while(choice=y); return 0;“”“”At the end, Xiao Bian gives you a passage. Minand once said, people who learn to learn are very happy people. In every wonderful life, learning is an eternal theme. As a professional clerical and teaching position, I understand the importance of continuous learning, life is diligent, nothing can be gained, only continuous learning can achieve better self. Only by constantly learning and mastering the latest relevant knowledge, can employees from all walks of life keep up with the pace of enterprise development and innovate to meet the needs of the market. This document is also edited by my studio professionals, there may be errors in the document, if there are errors, please correct, thank you!
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号