版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、解TSP问题的遗传算法C语言程序#include#include#include#include#include#include#include#include#include#define maxpop 100#define maxstring 100struct ppunsigned char chrommaxstring;float x,fitness;unsigned int parent1,parent2,xsite;struct pp *oldpop,*newpop,*p1;unsigned int popsize,lchrom,gem,maxgen,co_min,jrand;un
2、signed int nmutation,ncross,jcross,maxpp,minpp,maxxy;floatpcross,pmutation,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 ge
3、neration();void initialize();void report();float decode();void crtinit();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(d
4、d=(float*)farmalloc(maxstring*maxstring*sizeof(float)=NULL)printf(memory requst fail!n);exit(0); if(newpop=(struct pp*)farmalloc(maxpop*sizeof(struct pp)=NULL)printf(memory requst fail!n);exit(0); if(p1=(struct pp*)farmalloc(sizeof(struct pp)=NULL)printf(memory requst fail!n);exit(0); for(k=0;kmaxpo
5、p;k+)oldpopk.chrom0=0; for(k=0;kmaxpop;k+) newpopk.chrom0=0;printf(Enter Result Data Filename:); gets(fname);if(fp=fopen(fname,w+)=NULL)printf(cannot open filen);exit(0);gen=0;randomize();initialize();fputs(this is result of the TSP problem:,fp);fprintf(fp,city: %2d psize: %3d Ref.TSP_path:%fn,lchro
6、m,popsize,refpd);fprintf(fp,Pc: %f Pm: %f Seed: %fn,pcross,pmutation,seed);fprintf(fp,X site:n);for(k=0;klchrom;k+)if(k%16)=0) fprintf(fp,n); fprintf(fp,%5d,xk);fprintf(fp,n Y site:n); for(k=0;kmaxold)maxold=max;co_min=0;fmgen%200=100.0*oldpopmaxpp.x/ff; report(gen,oldpop);gotoxy(30,25);ttt=clock()/
7、18.2;tt=ttt/60;printf(Run Clock: %2d: %2d: %4.2f,tt/60,tt%60,ttt-tt*60.0);printf(Min=%6.4fNm:%dn,min,co_min); while(genmaxold)maxold=max;co_min=0;fmgen%200=100.0*oldpopmaxpp.x/ff; report(gen,oldpop);if(gen%100)=0)report(gen,oldpop); gotoxy(30,25);ttt=clock()/18.2;tt=ttt/60;printf(Run Clock: %2d: %2d
8、: %4.2f,tt/60,tt%60,ttt-tt*60.0);printf(Min=%6.4fNm:%dn,min,co_min); while(genmaxgen)&!bioskey(1);getch();for(k=0;klchrom;k+) if(k%16)=0)fprintf(fp,n);fprintf(fp,%5d,oldpopmaxpp.chromk);fprintf(fp,n);fclose(fp);farfree(dd);farfree(p1);farfree(oldpop);farfree(newpop);restorecrtmode();exit(0);/*%*/flo
9、at objfunc(float x1) float y;y=100.0*ff/x1;return y;/*&*/void statistics(pop) struct pp *pop;int j;sumfitness=pop0.fitness; min=pop0.fitness; max=pop0.fitness;maxpp=0;minpp=0;for(j=1;jmax)max=popj.fitness;maxpp=j;if(popj.fitnessmin)for(k=0;kmin)for(k=0;klchrom;k+)oldpopminpp.chromk=newpopj+1.chromk;
10、oldpopminpp.x=newpopj+1.x;oldpopminpp.fitness=newpopj+1.fitness;co_min+;return;j=j+2;while(jpopsize);/*%*/void initdata()unsigned int ch,j;clrscr();printf(n);printf(A SGAn);printf(n);/*pause();*/clrscr();printf(*SGA DATA ENTRY AND INITILIZATION *n);printf(n);printf(input pop size);scanf(%d,&popsize)
11、; printf(input chromlength);scanf(%d,&lchrom); printf(input maxgenerations);scanf(%d,&maxgen); printf(input crossoverprobability);scanf(%f,&pcross); printf(input mutation prob);scanf(%f,&pmutation); randomize1();clrscr();nmutation=0;ncross=0;/*%*/void initreport()int j,k;printf(pop size=%dn,popsize)
12、;printf(chromosome length=%dn,lchrom);printf(maxgen=%dn,maxgen);printf(pmutation=%fn,pmutation); printf(pcross=%fn,pcross);printf(initial generation statisticsn); printf(ini pop maxfitness=%fn,max); printf(ini pop avr fitness=%fn,avg); printf(inipop min fitness=%fn,min); printf(ini pop sum fit=%fn,s
13、umfitness); void initpop()unsigned char j1;unsigned int k5,i1,i2,j,i,k,j2,j3,j4,p5maxstring;float f1,f2;j=0;for(k=0;klchrom;k+)oldpopj.chromk=k;for(k=0;klchrom;k+)p5k=oldpopj.chromk;randomize();for(;jpopsize;j+)j2=random(lchrom);for(k=0;kj2+20;k+)j3=random(lchrom);j4=random(lchrom);j1=p5j3;p5j3=p5j4
14、;p5j4=j1;for(k=0;klchrom;k+)oldpopj.chromk=p5k; for(k=0;klchrom;k+)for(j=0;jlchrom;j+)ddk*lchrom+j=hypot(xk-xj,yk-yj);for(j=0;jpopsize;j+) oldpopj.x=(float)decode(oldpopj.chrom);oldpopj.fitness=objfunc(oldpopj.x);oldpopj.parent1=0;oldpopj.parent2=0;oldpopj.xsite=0;/*&*/void initialize()int k,j,minx,
15、miny,maxx,maxy; initdata();minx=0;miny=0;maxx=0;maxy=0;for(k=0;kmaxx)maxx=xk; if(xkmaxy)maxy=yk; if(yk(maxy-miny)maxxy=maxx-minx;else maxxy=maxy-miny;maxdd=0.0;for(k=0;klchrom;k+)for(j=0;jlchrom;j+)ddk*lchrom+j=hypot(xk-xj,yk-yj);if(maxddddk*lchrom+j)maxdd=ddk*lchrom+j;refpd=ddlchrom-1;for(k=0;klchr
16、om;k+)refpd=refpd+ddk*lchrom+k+2; for(j=0;jlchrom;j+)ddj*lchrom+j=4.0*maxdd; ff=(0.765*maxxy*pow(lchrom,0.5); minpp=0;min=ddlchrom-1;if(ddlchrom*j+lchrom-1min)min=ddlchrom*j+lchrom-1;minpp=j;initpop();statistics(oldpop);initreport();/*&*/void report(int l,struct pp *pop)int k,ix,iy,jx,jy;unsigned in
17、t tt;float ttt;cleardevice();gotoxy(1,1);printf(city:%4d para_size:%4d maxgen:%4d ref_tour:%fn,lchrom,popsize,maxgen,refpd);printf(ncross:%4d Nmutation:%4d Rungen:%4d AVG=%8.4f MIN=%8.4fnn ,ncross,nmutation,l,avg,min);printf(Rinpath:%6.4f Minpath length:%10.4f Ref_co_tour:%fn,popmaxpp.x/maxxy,popmax
18、pp.x,ff); printf(Co_minpath:%6.4fMaxfit:%10.8f,100.0*popmaxpp.x/ff,popmaxpp.fitness); ttt=clock()/18.2;tt=ttt/60;printf(Run clock:%2d:%2d:%4d.2fn,tt/60,tt%60,ttt-tt*60.0);setcolor(1%15+1);for(k=0;klchrom-1;k+)ix=xpopmaxpp.chromk;iy=ypopmaxpp.chromk+110;jx=xpopmaxpp.chromk+1;jy=ypopmaxpp.chromk+1+110
19、;line(ix,iy,jx,jy);putpixel(ix,iy,RED);ix=xpopmaxpp.chrom0;iy=ypopmaxpp.chrom0+110;jx=xpopmaxpp.chromlchrom-1;jy=ypopmaxpp.chromlchrom-1+110; line(ix,iy,jx,jy);putpixel(jx,jy,RED);setcolor(11);outtextxy(ix,iy,*);setcolor(12);for(k=0;k1%200;k+)ix=k+280;iy=366-fmk/3;jx=ix+1;jy=366-fmk+1/3;line(ix,iy,j
20、x,jy);putpixel(ix,iy,RED);printf(GEN:%3d,l);printf(Minpath:%f Maxfit:%f,popmaxpp.x,popmaxpp.fitness);printf(Clock:%2d:%2d:%4.2fn,tt/60,tt%60,ttt-tt*60.0);/*#*/float decode(unsigned char *pp) int j,k,l;float tt;tt=ddpp0*lchrom+pplchrom-1; for(j=0;jlchrom-1;j+)tt=tt+ddppj*lchrom+ppj+1; l=0;for(k=0;klc
21、hrom-1;k+)for(j=k+1;jlchrom;j+)if(ppj=ppk)l+;return tt+4*l*maxdd;/*%*/ void crtinit()int driver,mode;struct palettetype p;driver=DETECT;mode=0;initgraph(&driver,&mode,); cleardevice();/*$*/ int select()double rand1,partsum; float r1;int j;partsum=0.0;j=0;rand1=random1()*sumfitness; dopartsum=partsum
22、+oldpopj.fitness;j=j+1;while(partsumrand1)&(jj5)k=jcross;jcross=j5;j5=k;else jcross=lchrom; if(jcross!=lchrom) s0=1;k=0;for(j=jcross;jj5;j+) ts1k=parent1j; ts2k=parent2j; k+;)j3=k;for(j=0;jlchrom;j+) j2=0;while(parent2j!=ts1j2)&(j2k)j2+;if(j2=k)ts1j3=parent2j;j3+;j3=k;for(j=0;jlchrom;j+)j2=0;while(p
23、arent1j!=ts2j2)&(j2k)j2+;if(j2=k)ts2j3=parent1j;j3+;for(j=0;jlchrom;j+)newpopk5.chromj=ts1j;newpopk5+1.chromj=ts2j; for(j=0;jlchrom;j+)newpopk5.chromj=parent1j; newpopk5+1.chromj=parent2j; mutate=flip(pmutation);if(mutate)s1=1;nmutation=nmutation+1;for(j3=0;j3200;j3+)j1=random(lchrom);j=random(lchro
24、m);jj=newpopk5.chromj;newpopk5.chromj=newpopk5.chromj1;newpopk5.chromj1=jj;mutate=flip(pmutation);if(mutate)s2=1;nmutation=nmutation+1;for(j3=0;j3100;j3+)j1=random(lchrom);j=random(lchrom);jj=newpopk5+1.chromj;newpopk5+1.chromj=newpopk5+1.chromj1;newpopk5+1.chromj1=jj;j2=random(2*lchrom/3);for(j=j2;
25、jj2+lchrom/3-1;j+)for(k=0;kj)i2=k;i1=j;elsei1=k;i2=j;f1=ddlchrom*newpopk5.chromi1+newpopk5.chromi2;f1=f1+ddlchrom*newpopk5.chrom(i1+1)%lchrom+newpopk5.chrom(i2+1)%lchrom;f2=ddlchrom*newpopk5.chromi1+newpopk5.chrom(i1+1)%lchrom;f2=f2+ddlchrom*newpopk5.chromi2+newpopk5.chrom(i2+1)%lchrom;if(f1f2)inver
26、sion(i1,i2,newpopk5.chrom);j2=random(2*lchrom/3);for(j=j2;jj2+lchrom/3-1;j+)for(k=0;kj)i2=k;i1=j;f1=ddlchrom*newpopk5+1.chromi1+newpopk5+1.chromi2;f1=f1+ddlchrom*newpopk5+1.chrom(i1+1)%lchrom+newpopk5+1.chrom(i2+1)%lchrom;f2=ddlchrom*newpopk5+1.chromi1+newpopk5+1.chrom(i1+1)%lchrom;f2=f2+ddlchrom*ne
27、wpopk5+1.chromi2+newpopk5+1.chrom(i2+1)%lchrom;if(f1f2)inversion(i1,i2,newpopk5+1.chrom); return 1;/$/void inversion(unsigned int k,unsigned int j,unsigned char *ss)unsigned int l1,i;unsigned char tt;l1=(j-k)/2;for(i=0;il1;i+)tt=ssk+i+1;ssk+i+1=ssj-i;ssj-i=tt;/*%*/void randomize1()int i;randomize();
28、for(i=0;i=lchrom)jrand=0;randomize1();return oldrandjrand; /*%*/int flip(float probability) float ppp;ppp=random(20001)/20000.0; if(ppp=probability)return 1; return 0;%TSP题(又名:旅行商问题,货郎担问题)遗传算法通用matlab程序D是距 离矩阵,n为种群个数,建议取为城市个数的12倍,C为停止代数,遗传到第C 代时程序停止,C 的具体取值视问题的规模和耗费的时间而定%nJ适应值归一化淘汰加速指数,最好取为1,2,3,4 ,不宜太大alpha为淘汰保护指数, 可取为 01 之间任意小数, 取 1 时关闭保护功能, 最好取为 0.81.0%M最短路径,Rlength为路径长度function R,Rlength=geneticTSP(D,n,C,m,alp
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年度产品包装设计策划合同范本4篇
- 2025年度打桩机租赁项目风险评估与管理合同2篇
- 个性化人身损害补偿协议模板2024版版B版
- 二零二四双方自愿离婚协议书撰写指南3篇
- 二零二五年酒店安保服务与应急管理合作合同2篇
- 个人借款协议模板:2024年私人资金借用协议版B版
- 专业行纪服务与委托责任协议条款版A版
- 二零二五版互联网数据中心托管技术服务合同协议2篇
- 2025年度科技园区场地租赁与科技创新平台建设合同范本4篇
- 2025年度测量仪器销售与全球分销合同4篇
- 保洁服务岗位检查考核评分标准
- 称量与天平培训试题及答案
- 超全的超滤与纳滤概述、基本理论和应用
- 2020年医师定期考核试题与答案(公卫专业)
- 2022年中国育龄女性生殖健康研究报告
- 各种静脉置管固定方法
- 消防报审验收程序及表格
- 教育金规划ppt课件
- 呼吸机波形分析及临床应用
- 常用紧固件选用指南
- 私人借款协议书新编整理版示范文本
评论
0/150
提交评论