遗传算法c程序

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

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

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

资源描述

includestdio.h#includegraphics.h#includemath.h#includegraph.c/*全局变量*/structindividual/*个体*/{unsigned*chrom;/*染色体*/doublefitness;/*个体适应度*/doublevarible;/*个体对应的变量值*/intxsite;/*交叉位置*/intparent[2];/*父个体*/int*utility;/*特定数据指针变量*/};structbestever/*最佳个体*/{unsigned*chrom;/*最佳个体染色体*/doublefitness;/*最佳个体适应度*/doublevarible;/*最佳个体对应的变量值*/intgeneration;/*最佳个体生成代*/};structindividual*oldpop;/*当前代种群*/structindividual*newpop;/*新一代种群*/structbesteverbestfit;/*最佳个体*/doublesumfitness;/*种群中个体适应度累计*/doublemax;/*种群中个体最大适应度*/doubleavg;/*种群中个体平均适应度*/doublemin;/*种群中个体最小适应度*/floatpcross;/*交叉概率*/floatpmutation;/*变异概率*/intpopsize;/*种群大小*/intlchrom;/*染色体长度*/intchromsize;/*存储一染色体所需字节数*/intgen;/*当前世代数*/intmaxgen;/*最大世代数*/intrun;/*当前运行次数*/intmaxruns;/*总运行次数*/intprintstrings;/*输出染色体编码的判断,0--不输出,1--输出*/intnmutation;/*当前代变异发生次数*/intncross;/*当前代交叉发生次数*//*随机数发生器使用的静态变量*/staticdoubleoldrand[55];staticintjrand;staticdoublerndx2;staticintrndcalcflag;/*输出文件指针*/FILE*outfp;/*函数定义*/voidadvance_random();intflip(float);rnd(int,int);voidrandomize();doublerandomnormaldeviate();floatrandomperc(),rndreal(float,float);voidwarmup_random(float);voidinitialize(),initdata(),initpop();voidinitreport(),generation(),initmalloc();voidfreeall(),nomemory(char*),report();voidwritepop(),writechrom(unsigned*);voidpreselect();voidstatistics(structindividual*);voidtitle(),repchar(FILE*,char*,int);voidskip(FILE*,int);intselect();voidobjfunc(structindividual*);intcrossover(unsigned*,unsigned*,unsigned*,unsigned*);voidmutation(unsigned*);voidinitialize()/*遗传算法初始化*/{/*键盘输入遗传算法参数*/initdata();/*确定染色体的字节长度*/chromsize=(lchrom/(8*sizeof(unsigned)));if(lchrom%(8*sizeof(unsigned)))chromsize++;/*分配给全局数据结构空间*/initmalloc();/*初始化随机数发生器*/randomize();/*初始化全局计数变量和一些数值*/nmutation=0;ncross=0;bestfit.fitness=0.0;bestfit.generation=0;/*初始化种群,并统计计算结果*/initpop();statistics(oldpop);initreport();}voidinitdata()/*遗传算法参数输入*/{charanswer[2];setcolor(9);disp_hz16(种群大小(20-100):,100,150,20);gscanf(320,150,9,15,4,%d,&popsize);if((popsize%2)!=0){fprintf(outfp,种群大小已设置为偶数\n);popsize++;};setcolor(9);disp_hz16(染色体长度(8-40):,100,180,20);gscanf(320,180,9,15,4,%d,&lchrom);setcolor(9);disp_hz16(是否输出染色体编码(y/n):,100,210,20);printstrings=1;gscanf(320,210,9,15,4,%s,answer);if(strncmp(answer,n,1)==0)printstrings=0;setcolor(9);disp_hz16(最大世代数(100-300):,100,240,20);gscanf(320,240,9,15,4,%d,&maxgen);setcolor(9);disp_hz16(交叉率(0.2-0.9):,100,270,20);gscanf(320,270,9,15,5,%f,&pcross);setcolor(9);disp_hz16(变异率(0.01-0.1):,100,300,20);gscanf(320,300,9,15,5,%f,&pmutation);}voidinitpop()/*随机初始化种群*/{intj,j1,k,stop;unsignedmask=1;for(j=0;jpopsize;j++){for(k=0;kchromsize;k++){oldpop[j].chrom[k]=0;if(k==(chromsize-1))stop=lchrom-(k*(8*sizeof(unsigned)));elsestop=8*sizeof(unsigned);for(j1=1;j1=stop;j1++){oldpop[j].chrom[k]=oldpop[j].chrom[k]1;if(flip(0.5))oldpop[j].chrom[k]=oldpop[j].chrom[k]|mask;}}oldpop[j].parent[0]=0;/*初始父个体信息*/oldpop[j].parent[1]=0;oldpop[j].xsite=0;objfunc(&(oldpop[j]));/*计算初始适应度*/}}voidinitreport()/*初始参数输出*/{voidskip();skip(outfp,1);fprintf(outfp,基本遗传算法参数\n);fprintf(outfp,-------------------------------------------------\n);fprintf(outfp,种群大小(popsize)=%d\n,popsize);fprintf(outfp,染色体长度(lchrom)=%d\n,lchrom);fprintf(outfp,最大进化代数(maxgen)=%d\n,maxgen);fprintf(outfp,交叉概率(pcross)=%f\n,pcross);fprintf(outfp,变异概率(pmutation)=%f\n,pmutation);fprintf(outfp,-------------------------------------------------\n);skip(outfp,1);fflush(outfp);}voidgeneration(){intmate1,mate2,jcross,j=0;/*每代运算前进行预选*/preselect();/*选择,交叉,变异*/do{/*挑选交叉配对*/mate1=select();mate2=select();/*交叉和变异*/jcross=crossover(oldpop[mate1].chrom,oldpop[mate2].chrom,newpop[j].chrom,newpop[j+1].chrom);mutation(newpop[j].chrom);mutation(newpop[j+1].chrom);/*解码,计算适应度*/objfunc(&(newpop[j]));/*记录亲子关系和交叉位置*/newpop[j].parent[0]=mate1+1;newpop[j].xsite=jcross;newpop[j].parent[1]=mate2+1;objfunc(&(newpop[j+1]));newpop[j+1].parent[0]=mate1+1;newpop[j+1].xsite=jcross;newpop[j+1].parent[1]=mate2+1;j=j+2;}while(j(popsize-1));}voidinitmalloc()/*为全局数据变量分配空间*/{unsignednbytes;char*malloc();intj;/*分配给当前代和新一代种群内存空间*/nbytes=popsize*sizeof(structindividual);if((oldpop=(structindividual*)malloc(nbytes))==NULL)nomemory(oldpop);if((newpop=(structindividual*)malloc(nbytes))==NULL)nomemory(newpop);/*分配给染色体内存空间*/nbytes=chromsize*sizeof(unsigned);for(j=0;jpopsize;j++){if((oldpop[j].chrom=(unsigned*)malloc(nbytes))==NULL)nomemory(oldpopchromosomes);if((newpop[j].chrom=(unsigned*)malloc(nbytes))==NULL)nomemory(newpopchromosomes);}if((bestfit.chrom=(unsigned*)malloc(nbytes))==NULL)nomemory(bestfitchromosome);}voidfreeall()/*释放内存空间*/{inti;for(i=0;ipopsize;i++){free(oldpop[i].chrom);free(newpop[i].chrom);}free(oldpop);free(newpop);free(bestfit.chrom);}voidnomemory(string)/*内存不足,退出*/char*string;{fprintf(outfp,malloc:outofmemorymaking%s!!\n,string);exit(-1);}voidreport()/*输出种群统计结果*/{voidrepchar(),skip();voidwritepop(),writestats();repchar(outfp,-,80);skip(outfp,1);if(printstrings==1){repchar(outfp,,((80-17)/2));fprintf(outfp,模拟计算统计报告\n);fprintf(outfp,世代数%3d,gen);repchar(outfp,,(80-28));fprin

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

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

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

×
保存成功