遗传算法JAVA精品毕业设计完整版.docx
- 文档编号:2100883
- 上传时间:2022-10-26
- 格式:DOCX
- 页数:40
- 大小:254.58KB
遗传算法JAVA精品毕业设计完整版.docx
《遗传算法JAVA精品毕业设计完整版.docx》由会员分享,可在线阅读,更多相关《遗传算法JAVA精品毕业设计完整版.docx(40页珍藏版)》请在冰豆网上搜索。
遗传算法JAVA精品毕业设计完整版
java常用代码---遗传算法
标签:
javadistancecalendarstringrandom算法
2011-11-1718:
23 5815人阅读 评论
(1) 收藏 举报
分类:
数据结构与算法实践(23)
importjava.util.*;
publicclassTsp{
privateStringcityName[]={"北京","上海","天津","重庆","哈尔滨","长春","沈阳","呼和浩特","石家庄","太原","济南","郑州","西安","兰州","银川","西宁","乌鲁木齐","合肥","南京","杭州","长沙","南昌","武汉","成都","贵州","福建","台北","广州","海口","南宁","昆明","拉萨","香港","澳门"};
//privateStringcityEnd[]=newString[34];
privateintcityNum=cityName.length;//城市个数
privateintpopSize=50;//种群数量
privateintmaxgens=20000;//迭代次数
privatedoublepxover=0.8;//交叉概率
privatedoublepmultation=0.05;//变异概率
privatelong[][]distance=newlong[cityNum][cityNum];
privateintrange=2000;//用于判断何时停止的数组区间
privateclassgenotype{
intcity[]=newint[cityNum];//单个基因的城市序列
longfitness;//该基因的适应度
doubleselectP;//选择概率
doubleexceptp;//期望概率
intisSelected;//是否被选择
}
privategenotype[]citys=newgenotype[popSize];
/**
*构造函数,初始化种群
*/
publicTsp(){
for(inti=0;i citys[i]=newgenotype(); int[]num=newint[cityNum]; for(intj=0;j num[j]=j; inttemp=cityNum; for(intj=0;j intr=(int)(Math.random()*temp); citys[i].city[j]=num[r]; num[r]=num[temp-1]; temp--; } citys[i].fitness=0; citys[i].selectP=0; citys[i].exceptp=0; citys[i].isSelected=0; } initDistance(); } /** *计算每个种群每个基因个体的适应度,选择概率,期望概率,和是否被选择。 */ publicvoidCalAll(){ for(inti=0;i citys[i].fitness=0; citys[i].selectP=0; citys[i].exceptp=0; citys[i].isSelected=0; } CalFitness(); CalSelectP(); CalExceptP(); CalIsSelected(); } /** *填充,将多选的填充到未选的个体当中 */ publicvoidpad(){ intbest=0; intbad=0; while(true){ while(citys[best].isSelected<=1&&best best++; while(citys[bad].isSelected! =0&&bad bad++; for(inti=0;i citys[bad].city[i]=citys[best].city[i]; citys[best].isSelected--; citys[bad].isSelected++; bad++; if(best==popSize||bad==popSize) break; } } /** *交叉主体函数 */ publicvoidcrossover(){ intx; inty; intpop=(int)(popSize*pxover/2); while(pop>0){ x=(int)(Math.random()*popSize); y=(int)(Math.random()*popSize); executeCrossover(x,y);//xy两个体执行交叉 pop--; } } /** *执行交叉函数 *@param个体x *@param个体y *对个体x和个体y执行佳点集的交叉,从而产生下一代城市序列 */ privatevoidexecuteCrossover(intx,inty){ intdimension=0; for(inti=0;i if(citys[x].city[i]! =citys[y].city[i]){ dimension++; } intdiffItem=0; double[]diff=newdouble[dimension]; for(inti=0;i if(citys[x].city[i]! =citys[y].city[i]){ diff[diffItem]=citys[x].city[i]; citys[x].city[i]=-1; citys[y].city[i]=-1; diffItem++; } } Arrays.sort(diff); double[]temp=newdouble[dimension]; temp=gp(x,dimension); for(intk=0;k for(intj=0;j if(temp[j]==k){ doubleitem=temp[k]; temp[k]=temp[j]; temp[j]=item; item=diff[k]; diff[k]=diff[j]; diff[j]=item; } inttempDimension=dimension; inttempi=0; while(tempDimension>0){ if(citys[x].city[tempi]==-1){ citys[x].city[tempi]=(int)diff[dimension-tempDimension]; tempDimension--; } tempi++; } Arrays.sort(diff); temp=gp(y,dimension); for(intk=0;k for(intj=0;j if(temp[j]==k){ doubleitem=temp[k]; temp[k]=temp[j]; temp[j]=item; item=diff[k]; diff[k]=diff[j]; diff[j]=item; } tempDimension=dimension; tempi=0; while(tempDimension>0){ if(citys[y].city[tempi]==-1){ citys[y].city[tempi]=(int)diff[dimension-tempDimension]; tempDimension--; } tempi++; } } /** *@paramindividual个体 *@paramdimension维数 *@return佳点集(用于交叉函数的交叉点)在executeCrossover()函数中使用 */ privatedouble[]gp(intindividual,intdimension){ double[]temp=newdouble[dimension]; double[]temp1=newdouble[dimension]; intp=2*dimension+3; while(! isSushu(p)) p++; for(inti=0;i temp[i]=2*Math.cos(2*Math.PI*(i+1)/p)*(individual+1); temp[i]=temp[i]-(int)temp[i]; if(temp[i]<0) temp[i]=1+temp[i]; } for(inti=0;i temp1[i]=temp[i]; Arrays.sort(temp1); //排序 for(inti=0;i for(intj=0;j if(temp[j]==temp1[i]) temp[j]=i; returntemp; } /** *变异 */ publicvoidmutate(){ doublerandom; inttemp; inttemp1; inttemp2; for(inti=0;i random=Math.random(); if(random<=pmultation){ temp1=(int)(Math.random()*(cityNum)); temp2=(int)(Math.random()*(cityNum)); temp=citys[i].city[temp1]; citys[i].city[temp1]=citys[i].city[temp2]; citys[i].city[temp2]=temp; } } } /** *打印当前代数的所有城市序列,以及其相关的参数 */ publicvoidprint(){ /** *初始化各城市之间的距离 */ privatevoidinitDistance(){ for(inti=0;i for(intj=
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 遗传 算法 JAVA 精品 毕业设计 完整版