详细介绍如何利用各种语言(C++,C,JAV等)实现遗传算法

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

%TSP问题(又名:旅行商问题,货郎担问题)遗传算法通用matlab程序...........................2一个C++的程序:..........................................................................................................................4C语言程序:...................................................................................................................................8改进后用来求解VRP问题的Delphi程序:..............................................................................21%TSP问题(又名:旅行商问题,货郎担问题)遗传算法通用matlab程序%D是距离矩阵,n为种群个数,建议取为城市个数的1~2倍,%C为停止代数,遗传到第C代时程序停止,C的具体取值视问题的规模和耗费的时间而定%m为适应值归一化淘汰加速指数,最好取为1,2,3,4,不宜太大%alpha为淘汰保护指数,可取为0~1之间任意小数,取1时关闭保护功能,最好取为0.8~1.0%R为最短路径,Rlength为路径长度function[R,Rlength]=geneticTSP(D,n,C,m,alpha)[N,NN]=size(D);farm=zeros(n,N);%用于存储种群fori=1:nfarm(i,:)=randperm(N);%随机生成初始种群endR=farm(1,:);%存储最优种群len=zeros(n,1);%存储路径长度fitness=zeros(n,1);%存储归一化适应值counter=0;whilecounterCfori=1:nlen(i,1)=myLength(D,farm(i,:));%计算路径长度endmaxlen=max(len);minlen=min(len);fitness=fit(len,m,maxlen,minlen);%计算归一化适应值rr=find(len==minlen);R=farm(rr(1,1),:);%更新最短路径FARM=farm;%优胜劣汰,nn记录了复制的个数nn=0;fori=1:niffitness(i,1)=alpha*randnn=nn+1;FARM(nn,:)=farm(i,:);endendFARM=FARM(1:nn,:);[aa,bb]=size(FARM);%交叉和变异whileaanifnn=2nnper=randperm(2);elsennper=randperm(nn);endA=FARM(nnper(1),:);B=FARM(nnper(2),:);[A,B]=intercross(A,B);FARM=[FARM;A;B];[aa,bb]=size(FARM);endifaanFARM=FARM(1:n,:);%保持种群规模为nendfarm=FARM;clearFARMcounter=counter+1endRlength=myLength(D,R);function[a,b]=intercross(a,b)L=length(a);ifL=10%确定交叉宽度W=1;elseif((L/10)-floor(L/10))=rand&&L10W=ceil(L/10);elseW=floor(L/10);endp=unidrnd(L-W+1);%随机选择交叉范围,从p到p+Wfori=1:W%交叉x=find(a==b(1,p+i-1));y=find(b==a(1,p+i-1));[a(1,p+i-1),b(1,p+i-1)]=exchange(a(1,p+i-1),b(1,p+i-1));[a(1,x),b(1,y)]=exchange(a(1,x),b(1,y));endfunction[x,y]=exchange(x,y)temp=x;x=y;y=temp;%计算路径的子程序functionlen=myLength(D,p)[N,NN]=size(D);len=D(p(1,N),p(1,1));fori=1:(N-1)len=len+D(p(1,i),p(1,i+1));end%计算归一化适应值子程序functionfitness=fit(len,m,maxlen,minlen)fitness=len;fori=1:length(len)fitness(i,1)=(1-((len(i,1)-minlen)/(maxlen-minlen+0.000001))).^m;end一个C++的程序://c++的程序#includeiostream.h#includestdlib.htemplateclassTclassGraph{public:Graph(intvertices=10){n=vertices;e=0;}~Graph(){}virtualboolAdd(intu,intv,constT&w)=0;virtualboolDelete(intu,intv)=0;virtualboolExist(intu,intv)const=0;intVertices()const{returnn;}intEdges()const{returne;}protected:intn;inte;};templateclassTclassMGraph:publicGraphT{public:MGraph(intVertices=10,TnoEdge=0);~MGraph();boolAdd(intu,intv,constT&w);boolDelete(intu,intv);boolExist(intu,intv)const;voidFloyd(T**&d,int**&path);voidprint(intVertices);private:TNoEdge;T**a;};templateclassTMGraphT::MGraph(intVertices,TnoEdge){n=Vertices;NoEdge=noEdge;a=newT*[n];for(inti=0;in;i++){a[i]=newT[n];a[i][i]=0;for(intj=0;jn;j++)if(i!=j)a[i][j]=NoEdge;}}templateclassTMGraphT::~MGraph(){for(inti=0;in;i++)delete[]a[i];delete[]a;}templateclassTboolMGraphT::Exist(intu,intv)const{if(u0||v0||un-1||vn-1||u==v||a[u][v]==NoEdge)returnfalse;returntrue;}templateclassTboolMGraphT::Add(intu,intv,constT&w){if(u0||v0||un-1||vn-1||u==v||a[u][v]!=NoEdge){cerrBadInput!endl;returnfalse;}a[u][v]=w;e++;returntrue;}templateclassTboolMGraphT:delete(intu,intv){if(u0||v0||un-1||vn-1||u==v||a[u][v]==NoEdge){cerrBadInput!endl;returnfalse;}a[u][v]=NoEdge;e--;returntrue;}templateclassTvoidMGraphT::Floyd(T**&d,int**&path){d=newT*[n];path=newint*[n];for(inti=0;in;i++){d[i]=newT[n];path[i]=newint[n];for(intj=0;jn;j++){d[i][j]=a[i][j];if(i!=j&&a[i][j]NoEdge)path[i][j]=i;elsepath[i][j]=-1;}}for(intk=0;kn;k++){for(i=0;in;i++)for(intj=0;jn;j++)if(d[i][k]+d[k][j]d[i][j]){d[i][j]=d[i][k]+d[k][j];path[i][j]=path[k][j];}}}templateclassTvoidMGraphT::print(intVertices){for(inti=0;iVertices;i++)for(intj=0;jVertices;j++){couta[i][j]'';if(j==Vertices-1)coutendl;}}#definenoEdge10000#includeiostream.hvoidmain(){cout请输入该图的节点数:endl;intvertices;cinvertices;MGraphfloatb(vertices,noEdge);cout请输入u,v,w:endl;intu,v;floatw;cinuvw;while(w!=noEdge){//u=u-1;b.Add(u-1,v-1,w);b.Add(v-1,u-1,w);cout请输入u,v,w:endl;cinuvw;}b.print(vertices);int**Path;int**&path=Path;float**D;float**&d=D;b.Floyd(d,path);for(inti=0;ivertices;i++){for(intj=0;jvertices;j++){coutPath[i][j]'';if(j==vertices-1)coutendl;}}int*V;V=newint[vertices+1];cout请输入任意一个初始H-圈:endl;for(intn=0;n=vertices;n++){cinV[n];}for(n=0;n55;n++){for(i=0;in-1;i++){for(intj=0;jn-1;j++){if(i+10&&ji+1&&jn-1){if(D[V[i]][V[j]]+D[V[i+1]][V[j+1]]D[V[i]][V[i+1]]+D[V[j]][V[j+1]]){intl;l=V[i+1];V[i+1]=V[j];V[j]=l;}}}}}floattotal=0;cout最小回路:endl;for(i=0;i=vertices;i++){coutV[i]+1'';}coutendl;for(i=0;ivertices;i++)total+=D[V[i]][V[i+1]];cout最短路径长度:endl;couttotal;}C语言程序:#includestdio.h#includestdlib.h#includemath.h#includealloc.h#includeconio.h#includefloat.h#includetime.h#includegraphics.h#includebios.h#definemaxpop100#definemaxstring100structpp{unsignedcharchrom[maxstring];floatx,fitness;unsignedintparen

1 / 54
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功