1、pc ,则选择vi作为一个父代。将所选的父代两两组队,随机产生一个位置进行交叉,如:8 14 2 13 8 6 3 2 5 7 3 4 3 2 4 2 2 16 12 3 5 6 8 5 6 3 1 8 5 6 3 3 2 1 1交叉后为:8 14 2 13 8 6 3 2 5 1 8 5 6 3 3 2 1 16 12 3 5 6 8 5 6 3 7 3 4 3 2 4 2 2 1变异过程:本文采用均匀多点变异。类似交叉操作中选择父代的过程,在rpm 的标准下选择多个染色体vi作为父代。对每一个选择的父代,随机选择多个位置,使其在每位置按均匀变异(该变异点xk的取值范围为ukmin,ukma
2、x,产生一个0,1中随机数r,该点变异为xk=ukmin+r(ukmax-ukmin)操作。如:变异后:8 14 2 13 10 6 3 2 2 7 3 4 5 2 4 1 2 1反grefenstette编码:交叉和变异都是在grefenstette编码之后进行的,为了循环操作和返回最终结果,必须逆grefenstette编码过程,将编码恢复到自然编码。循环操作:判断是否满足设定的带数xzome,否,则跳入适应度f的计算;是,结束遗传操作,跳出。一个C+的程序:/c+的程序#includestdlib.htemplateclass Graphpublic:Graph(int vertices
3、=10)n=vertices;e=0;Graph()virtual bool Add(int u,int v,const T& w)=0;virtual bool Delete(int u,int v)=0;virtual bool Exist(int u,int v)const=0;int Vertices()constreturn n;int Edges()constreturn e;protected:int n;int e;class MGraph:public GraphMGraph(int Vertices=10,T noEdge=0);MGraph();bool Add(int
4、u,int v,const T& w);bool Delete(int u,int v);bool Exist(int u,int v)const;void Floyd(T*& d,int*& path);void print(int Vertices);private:T NoEdge;T* a;MGraph:MGraph(int Vertices,T noEdge)n=Vertices;NoEdge=noEdge;a=new T* n;for(int i=0;in;i+)ai=new Tn;aii=0;for(int j=0;jj+)if(i!=j)aij=NoEdge;MGraph()i
5、+)deleteai;deletea;bool MGraphExist(int u,int v)constif(u0|vn-1|vn-1|u=v|auv=NoEdge)return false;return true;Add(int u,int v,const T& w)n-1|u=v|auv!=NoEdge)cerrBadInput!endl;return false;auv=w;e+;delete(int u,int v)n-1|u=v|auv=NoEdge)auv=NoEdge;e-;void MGraphFloyd(T*& path)d=new T* n;path=new int* n
6、;di=new Tn;pathi=new intn;j+)dij=aij;if(i!=j&aijNoEdge)pathij=i;else pathij=-1;for(int k=0;kk+)for(i=0;i+)j+)if(dik+dkjdij)dij=dik+dkj;pathij=pathkj;print(int Vertices)Vertices;cout ;if(j=Vertices-1)coutvertices;float b(vertices,noEdge);请输入u,v,w:int u,v;float w;uvw;while(w!=noEdge)/u=u-1;b.Add(u-1,v
7、-1,w);b.Add(v-1,u-1,w);b.print(vertices);int* Path;int*& path=Path;float* D;float*& d=D;b.Floyd(d,path);Pathijif(j=vertices-1)coutint *V;V=new intvertices+1;请输入任意一个初始H-圈:for(int n=0;n0&ji+1&n-1)if(DViVj+DVi+1Vj+1DViVi+1+DVjVj+1)int l;l=Vi+1;Vi+1=Vj;Vj=l;float total=0;最小回路:Vi+1math.halloc.hconio.hflo
8、at.htime.hgraphics.hbios.h#define maxpop100 maxstring100 structppunsigned char chrommaxstring;float x,fitness;unsigned int parent1,parent2,xsite; ;struct pp *oldpop,*newpop,*p1;unsigned int popsize,lchrom,gem,maxgen,co_min,jrand;unsigned int nmutation,ncross,jcross,maxpp,minpp,maxxy;float pcross,pmu
9、tation,sumfitness,avg,max,min,seed,maxold,oldrandmaxstring;unsigned char xmaxstring,ymaxstring;float *dd,ff,maxdd,refpd,fm201;FILE *fp,*fp1;float objfunc(float);void statistics();int select();int flip(float);int crossover();void generation();void initialize();void report();float decode();void crtini
10、t();void inversion();float random1();void randomize1();main()unsigned int gen,k,j,tt;char fname10;float ttt;clrscr();co_min=0;if(oldpop=(struct pp *)farmalloc(maxpop*sizeof(struct pp)=NULL) printf(memory requst fail!n);exit(0);if(dd=(float *)farmalloc(maxstring*maxstring*sizeof(float)=NULL)if(newpop
11、=(struct pp *)farmalloc(maxpop*sizeof(struct pp)=NULL)if(p1=(struct pp *)farmalloc(sizeof(struct pp)=NULL)for(k=0;maxpop;k+) oldpopk.chrom0=0k+) newpopk.chrom0=printf(Enter Result Data Filename:gets(fname);if(fp=fopen(fname,w+)=NULL)printf(cannot open filengen=0;randomize();initialize();fputs(this i
12、s result of the TSP problem:,fp);fprintf(fp,city: %2d psize: %3d Ref.TSP_path: %fn,lchrom,popsize,refpd);Pc: %f Pm: %f Seed:,pcross,pmutation,seed);X site:lchrom;k+) if(k%16)=0) fprintf(fp,%5d,xk); n Y site:,yk);crtinit();statistics(oldpop);report(gen,oldpop);getch();maxold=min;fm0=100.0*oldpopmaxpp
13、.x/ff;do gen=gen+1;generation();if(maxmaxold) maxold=max;fmgen%200=100.0*oldpopmaxpp.x/ff;gotoxy(30,25);ttt=clock()/18.2;tt=ttt/60;Run Clock: %2d: %4.2f,tt/60,tt%60,ttt-tt*60.0);Min=%6.4f Nm:%dn,min,co_min); while(genmax)max=popj.fitness;maxpp=j; if(popj.fitnessmin)min=popj.fitness;minpp=j;avg=sumfi
14、tness/(float)popsize;/*%*/ void generation()unsigned int k,j,j1,j2,i1,i2,mate1,mate2;float f1,f2;j=0; mate1=select(); pp:mate2=select(); if(mate1=mate2)goto pp; crossover(oldpopmate1.chrom,oldpopmate2.chrom,j); newpopj.x=(float)decode(newpopj.chrom); newpopj.fitness=objfunc(newpopj.x); newpopj.parent1=mate1; newpopj.parent2=mate2;
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1