GreenSimJ原创GreenSimJ原创遗传算法解决车辆路径_第1页
GreenSimJ原创GreenSimJ原创遗传算法解决车辆路径_第2页
GreenSimJ原创GreenSimJ原创遗传算法解决车辆路径_第3页
GreenSimJ原创GreenSimJ原创遗传算法解决车辆路径_第4页
GreenSimJ原创GreenSimJ原创遗传算法解决车辆路径_第5页
已阅读5页,还剩14页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

GreenSim。J原创】GreenSim.J原创】遗传算法解决车辆路径问题(VRP)(2007—08—2911:22:12)标签:HYPERLINK"http://uni。sina。co/c。php?t=blog&k=??/???&ts=bpost&stype=tag"知识/探究关于遗传算法博客里已经很多了,这次贴个c的代码,下面的源代码是解决车辆路径问题的.谈一点阅历性的东西,就是在进行染色体交叉时,肯定要注意基因结构的问题。依据简略应用情况,尽量要保证好的基因结构遗传到后代中.其实此时的交叉变异等全部的操作,考虑的是基因结构,而不是单个的基因。所以在设计编码方式时就要考虑到设计出良好的基因结构。便于分割和组合的结构是好的设计.部分源程序:(http://blog。sina.com.cn/greensim)/**************************最优化函数***************************/voidGAOptim(float*vData,floatvP,intvGenNum,int*pBestGen){intvFlockSize=FLOCKSIZE;intGENNUM=vGenNum;floatP=vP;intvLen=LEN;intMotherFlock[FLOCKSIZE][LEN];intFatherFlock[FLOCKSIZE][LEN];intChildFlock[2*FLOCKSIZE][LEN];intmBestGen[LEN];intmCounter,mCounter1;int*pTemp1;//产生初始种群for(mCounter1=0;mCounter1<FLOCKSIZE;mCounter1++){pTemp1=MotherFlock[mCounter1];PermutRnd(LEN,1,pTemp1);}for(mCounter1=0;mCounter1〈FLOCKSIZE;mCounter1++){pTemp1=FatherFlock[mCounter1];PermutRnd(LEN,1,pTemp1);}PermutRnd(LEN,1,mBestGen);//初始化最优种群for(mCounter=0;mCounter<GENNUM;mCounter++){//交叉GenCross(MotherFlock,FatherFlock,ChildFlock,vData);//遗传GenHeri(MotherFlock,FatherFlock,ChildFlock,mBestGen,vData);//变异GenAber(MotherFlock,FatherFlock,mBestGen,vP,vData);}for(mCounter=0;mCounter<vLen;mCounter++){*(pBestGen+mCounter)=mBestGen[mCounter];}return;}/**************************交叉子函数***************************/voidGenCross(int*MotherFlock,int*FatherFlock,int*ChildFlock,float*vData){intmCounter,mCounter1,mCounter2,mCounter3;intmIndex=0;intmCrossLen1=0,mCrossLen2=0;intmCrossFlag=0;intmMoFkTemp1[LEN],mMoFkTemp2[LEN],mFaFkTemp1[LEN],mFaFkTemp2[LEN];intmMotherFlock[FLOCKSIZE][LEN],mFatherFlock[FLOCKSIZE][LEN];intmChildFlock[2*FLOCKSIZE][LEN];intmIndex1[LEN],mIndex2[LEN];int*pMo,*pFa,*pChid;pMo=mMotherFlock[0];pFa=mFatherFlock[0];pChid=mChildFlock[0];for(mCounter=0;mCounter<FLOCKSIZE*LEN;mCounter++){*(pMo+mCounter)=*(MotherFlock+mCounter);*(pFa+mCounter)=*(FatherFlock+mCounter);}for(mCounter=0;mCounter<FLOCKSIZE;mCounter++){/*进行双亲双子交叉*///产生交叉点mIndex=floor(UniRnd()*LEN);//依据交叉点,寻找两段合适的遗传序列for(mCounter1=0;mCounter1<LEN;mCounter1++)//数组初始化{mMoFkTemp1[mCounter1]=0;mMoFkTemp2[mCounter1]=0;mFaFkTemp1[mCounter1]=0;mFaFkTemp2[mCounter1]=0;}//存储四段序列for(mCounter1=0,mCounter2=0,mCounter3=0;mCounter1〈LEN;mCounter1++){if(mCounter1〈mIndex){mMoFkTemp1[mCounter2]=mMotherFlock[mCounter][mCounter1];mFaFkTemp1[mCounter2]=mFatherFlock[mCounter][mCounter1];mCounter2++;}else{mMoFkTemp2[mCounter3]=mMotherFlock[mCounter][mCounter1];mFaFkTemp2[mCounter3]=mFatherFlock[mCounter][mCounter1];mCounter3++;}}mCrossLen1=mIndex—1;mCrossLen2=LEN-mCrossLen1;//将两段好的序列放在mMoFkTemp1,mMoFkTemp2中,差的放在mFaFkTemp1,mFaFkTemp2中mCrossFlag=BestChip(mMoFkTemp1,mMoFkTemp2,mFaFkTemp1,mFaFkTemp2,mCrossLen1,mCrossLen2,vData);//依据两段序列构造完整基因,存放在子代中PermutRnd(mCrossLen1,1,mIndex1);//产生补充基因的随机数PermutRnd(mCrossLen2,1,mIndex2);for(mCounter1=0,mCounter2=0;mCounter1<LEN;mCounter1++){if(mCounter1〈=mCrossLen1){mChildFlock[2*mCounter][mCounter1]=mMoFkTemp1[mCounter1];if(mCrossFlag==1)//如果交叉成功,则补充基因,否则直接复制到子代{mChildFlock[2*mCounter+1][mCounter1]=mFaFkTemp1[mIndex1[mCounter1]];}else{mChildFlock[2*mCounter+1][mCounter1]=mFaFkTemp1[mCounter1];}}else{mChildFlock[2*mCounter][mCounter1]=mMoFkTemp2[mCounter2];if(mCrossFlag==1){mChildFlock[2*mCounter+1][mCounter1]=mFaFkTemp2[mIndex2[mCounter2]];}else{mChildFlock[2*mCounter+1][mCounter1]=mFaFkTemp2[mCounter2];}mCounter2++;}}}//最后把产生的子代转入到输入参数ChildFlock中for(mCounter=0;mCounter<2*FLOCKSIZE*LEN;mCounter++){*(ChildFlock+mCounter)=*(pChid+mCounter);}}//子函数,将输入的四段基因中最好的两段存放在mMoFkTemp1和mMoFkTemp2中//返回交叉信息,返回1为交叉,返回-1为没交叉intBestChip(int*mMoFkTemp1,int*mMoFkTemp2,int*mFaFkTemp1,int*mFaFkTemp2,intmCrossLen1,intmCrossLen2,float*mData){intvFlag=0,vCounter1;intvTemp1;intvLen;intFLAG=-1;vLen=mCrossLen1+mCrossLen2;//推断前一部分序列的特性并存储好的序列vFlag=BestJudger(mMoFkTemp1,mFaFkTemp1,mCrossLen1,mData,vLen);if(vFlag==2){for(vCounter1=0;vCounter1〈mCrossLen1;vCounter1++){vTemp1=*(mMoFkTemp1+vCounter1);*(mMoFkTemp1+vCounter1)=*(mFaFkTemp1+vCounter1);*(mFaFkTemp1+vCounter1)=vTemp1;}FLAG=1;}//推断后一部分序列的特性并存储好的序列vFlag=BestJudger(mMoFkTemp2,mFaFkTemp2,mCrossLen2,mData,vLen);if(vFlag==2){for(vCounter1=0;vCounter1<mCrossLen2;vCounter1++){vTemp1=*(mMoFkTemp2+vCounter1);*(mMoFkTemp2+vCounter1)=*(mFaFkTemp2+vCounter1);*(mFaFkTemp2+vCounter1)=vTemp1;}FLAG=-1*FLAG;}return(FLAG);}/************************变异子函数*************************/voidGenAber(int*MotherFlock,int*FatherFlock,int*BestGen,floatP,float*mData){intmCounter,mCounter1,mCounter2;intmIndex[LEN];intmTemp1=0;for(mCounter=0;mCounter<LEN;mCounter++){if(P>UniRnd())//当满意变异概率时进行变异母代{PermutRnd(LEN,1,mIndex);mTemp1=*(MotherFlock+mCounter*LEN+mIndex[1]);*(MotherFlock+mCounter*LEN+mIndex[1])=*(MotherFlock+mCounter*LEN+mIndex[2]);*(MotherFlock+mCounter*LEN+mIndex[2])=mTemp1;mTemp1=*(MotherFlock+mCounter*LEN+mIndex[3]);*(MotherFlock+mCounter*LEN+mIndex[3])=*(MotherFlock+mCounter*LEN+mIndex[4]);*(MotherFlock+mCounter*LEN+mIndex[4])=mTemp1;}if(P>UniRnd())//当满意变异概率时进行变异父代{PermutRnd(LEN,1,mIndex);mTemp1=*(FatherFlock+mCounter*LEN+mIndex[1]);*(FatherFlock+mCounter*LEN+mIndex[1])=*(FatherFlock+mCounter*LEN+mIndex[2]);*(FatherFlock+mCounter*LEN+mIndex[2])=mTemp1;mTemp1=*(FatherFlock+mCounter*LEN+mIndex[3]);*(FatherFlock+mCounter*LEN+mIndex[3])=*(FatherFlock+mCounter*LEN+mIndex[4]);*(FatherFlock+mCounter*LEN+mIndex[4])=mTemp1;}}//保存最好的序列mTemp1=BestJudger(BestGen,MotherFlock,LEN,mData,LEN);if(mTemp1==2){for(mCounter=0;mCounter<LEN;mCounter++){*(BestGen+mCounter)=*(MotherFlock+mCounter);}}else{for(mCounter=0;mCounter<LEN;mCounter++){*(MotherFlock+mCounter)=*(BestGen+mCounter);}}}更多问题请联系我们(HYPERLINK"http://blog.sina.com.cn/greensim"http://blog.sina.com。cn/greensim)(VRP)(2007—08-2911:22:12)关于遗传算法博客里已经很多了,这次贴个c的代码,下面的源代码是解决车辆路径问题的。谈一点阅历性的东西,就是在进行染色体交叉时,肯定要注意基因结构的问题。依据简略应用情况,尽量要保证好的基因结构遗传到后代中。其实此时的交叉变异等全部的操作,考虑的是基因结构,而不是单个的基因。所以在设计编码方式时就要考虑到设计出良好的基因结构.便于分割和组合的结构是好的设计。部分源程序:(http://blog.sina。com.cn/greensim)/**************************最优化函数***************************/voidGAOptim(float*vData,floatvP,intvGenNum,int*pBestGen){intvFlockSize=FLOCKSIZE;intGENNUM=vGenNum;floatP=vP;intvLen=LEN;intMotherFlock[FLOCKSIZE][LEN];intFatherFlock[FLOCKSIZE][LEN];intChildFlock[2*FLOCKSIZE][LEN];intmBestGen[LEN];intmCounter,mCounter1;int*pTemp1;//产生初始种群for(mCounter1=0;mCounter1〈FLOCKSIZE;mCounter1++){pTemp1=MotherFlock[mCounter1];PermutRnd(LEN,1,pTemp1);}for(mCounter1=0;mCounter1<FLOCKSIZE;mCounter1++){pTemp1=FatherFlock[mCounter1];PermutRnd(LEN,1,pTemp1);}PermutRnd(LEN,1,mBestGen);//初始化最优种群for(mCounter=0;mCounter〈GENNUM;mCounter++){//交叉GenCross(MotherFlock,FatherFlock,ChildFlock,vData);//遗传GenHeri(MotherFlock,FatherFlock,ChildFlock,mBestGen,vData);//变异GenAber(MotherFlock,FatherFlock,mBestGen,vP,vData);}for(mCounter=0;mCounter<vLen;mCounter++){*(pBestGen+mCounter)=mBestGen[mCounter];}return;}/**************************交叉子函数***************************/voidGenCross(int*MotherFlock,int*FatherFlock,int*ChildFlock,float*vData){intmCounter,mCounter1,mCounter2,mCounter3;intmIndex=0;intmCrossLen1=0,mCrossLen2=0;intmCrossFlag=0;intmMoFkTemp1[LEN],mMoFkTemp2[LEN],mFaFkTemp1[LEN],mFaFkTemp2[LEN];intmMotherFlock[FLOCKSIZE][LEN],mFatherFlock[FLOCKSIZE][LEN];intmChildFlock[2*FLOCKSIZE][LEN];intmIndex1[LEN],mIndex2[LEN];int*pMo,*pFa,*pChid;pMo=mMotherFlock[0];pFa=mFatherFlock[0];pChid=mChildFlock[0];for(mCounter=0;mCounter<FLOCKSIZE*LEN;mCounter++){*(pMo+mCounter)=*(MotherFlock+mCounter);*(pFa+mCounter)=*(FatherFlock+mCounter);}for(mCounter=0;mCounter<FLOCKSIZE;mCounter++){/*进行双亲双子交叉*///产生交叉点mIndex=floor(UniRnd()*LEN);//依据交叉点,寻找两段合适的遗传序列for(mCounter1=0;mCounter1<LEN;mCounter1++)//数组初始化{mMoFkTemp1[mCounter1]=0;mMoFkTemp2[mCounter1]=0;mFaFkTemp1[mCounter1]=0;mFaFkTemp2[mCounter1]=0;}//存储四段序列for(mCounter1=0,mCounter2=0,mCounter3=0;mCounter1〈LEN;mCounter1++){if(mCounter1<mIndex){mMoFkTemp1[mCounter2]=mMotherFlock[mCounter][mCounter1];mFaFkTemp1[mCounter2]=mFatherFlock[mCounter][mCounter1];mCounter2++;}else{mMoFkTemp2[mCounter3]=mMotherFlock[mCounter][mCounter1];mFaFkTemp2[mCounter3]=mFatherFlock[mCounter][mCounter1];mCounter3++;}}mCrossLen1=mIndex—1;mCrossLen2=LEN—mCrossLen1;//将两段好的序列放在mMoFkTemp1,mMoFkTemp2中,差的放在mFaFkTemp1,mFaFkTemp2中mCrossFlag=BestChip(mMoFkTemp1,mMoFkTemp2,mFaFkTemp1,mFaFkTemp2,mCrossLen1,mCrossLen2,vData);//依据两段序列构造完整基因,存放在子代中PermutRnd(mCrossLen1,1,mIndex1);//产生补充基因的随机数PermutRnd(mCrossLen2,1,mIndex2);for(mCounter1=0,mCounter2=0;mCounter1〈LEN;mCounter1++){if(mCounter1<=mCrossLen1){mChildFlock[2*mCounter][mCounter1]=mMoFkTemp1[mCounter1];if(mCrossFlag==1)//如果交叉成功,则补充基因,否则直接复制到子代{mChildFlock[2*mCounter+1][mCounter1]=mFaFkTemp1[mIndex1[mCounter1]];}else{mChildFlock[2*mCounter+1][mCounter1]=mFaFkTemp1[mCounter1];}}else{mChildFlock[2*mCounter][mCounter1]=mMoFkTemp2[mCounter2];if(mCrossFlag==1){mChildFlock[2*mCounter+1][mCounter1]=mFaFkTemp2[mIndex2[mCounter2]];}else{mChildFlock[2*mCounter+1][mCounter1]=mFaFkTemp2[mCounter2];}mCounter2++;}}}//最后把产生的子代转入到输入参数ChildFlock中for(mCounter=0;mCounter〈2*FLOCKSIZE*LEN;mCounter++){*(ChildFlock+mCounter)=*(pChid+mCounter);}}//子函数,将输入的四段基因中最好的两段存放在mMoFkTemp1和mMoFkTemp2中//返回交叉信息,返回1为交叉,返回-1为没交叉intBestChip(int*mMoFkTemp1,int*mMoFkTemp2,int*mFaFkTemp1,int*mFaFkTemp2,intmCrossLen1,intmCrossLen2,float*mData){intvFlag=0,vCounter1;intvTemp1;intvLen;intFLAG=-1;vLen=mCrossLen1+mCrossLen2;//推断前一部分序列的特性并存储好的序列vFlag=BestJudger(mMoFkTemp1,mFaFkTemp1,mCrossLen1,mData,vLen);if(vFlag==2){for(vCounter1=0;vCounter1<mCrossLen1;vCounter1++){vTemp1=*(mMoFkTemp1+vCounter1);*(mMoFkTemp1+vCounter1)=*(mFaFkTemp1+vCounter1);*(mFaFkTemp1+vCounter1)=vTemp1;}FLAG=1;}//推断后一部分序列的特性并存储好的序列vFlag=BestJudger(mMoFkTemp2,mFaFkTemp2,mCrossLen2,mData,vLen);if(vFlag==2){for(vCounter1=0;vCounter1<mCrossLen2;vCounter1++){vTemp1=*(mMoFkTemp2+vCounter1);*(mMoFkTemp2+vCounter1)=*(mFaFkTemp2+vCounter1);*(mFaFkTemp2+vCounter1)=vTemp1;}FLAG=-1*FLAG;}return(FLAG);}/************************变异子函数*************************/voidGenAber(int*MotherFlock,int*FatherFlock,int*BestGen,floatP,float*mData){intmCounter,mCounter1,mCounter2;intmIndex[LEN];intmTemp1=0;for(

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论