欢迎来到天天文库
浏览记录
ID:21019878
大小:67.50 KB
页数:11页
时间:2018-10-18
《遗传算法vc程序(英文解释)》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、#include#include#include/*Changeanyoftheseparameterstomatchyourneeds*/#definePOPSIZE50/*populationsize*/#defineMAXGENS1000/*max.numberofgenerations*/#defineNVARS3/*no.ofproblemvariables*/#definePXOVER0.8/*probabilityofcrossover*/#definePMUTATION0.15/*probabilityofmuta
2、tion*/#defineTRUE1#defineFALSE0intgeneration;/*currentgenerationno.*/intcur_best;/*bestindividual*/FILE*galog;/*anoutputfile*/structgenotype/*genotype(GT),amemberofthepopulation*/{doublegene[NVARS];/*astringofvariables*/doublefitness;/*GT'sfitness*/doubleupper[NVARS];/*GT'svariablesupperbound*/
3、doublelower[NVARS];/*GT'svariableslowerbound*/doublerfitness;/*relativefitness*/doublecfitness;/*cumulativefitness*/};structgenotypepopulation[POPSIZE+1];/*population*/structgenotypenewpopulation[POPSIZE+1];/*newpopulation;*//*replacesthe*//*oldgeneration*//*Declarationofproceduresusedbythisgen
4、eticalgorithm*/voidinitialize(void);doublerandval(double,double);voidevaluate(void);voidkeep_the_best(void);voidelitist(void);voidselect(void);voidcrossover(void);voidXover(int,int);voidswap(double*,double*);voidmutate(void);voidreport(void);/****************************************************
5、***********//*Initializationfunction:Initializesthevaluesofgenes*//*withinthevariablesbounds.Italsoinitializes(tozero)*//*allfitnessvaluesforeachmemberofthepopulation.It*//*readsupperandlowerboundsofeachvariablefromthe*//*inputfile`gadata.txt'.Itrandomlygeneratesvalues*//*betweentheseboundsfore
6、achgeneofeachgenotypeinthe*//*population.Theformatoftheinputfile`gadata.txt'is*//*var1_lower_boundvar1_upperbound*//*var2_lower_boundvar2_upperbound...*//***************************************************************/voidinitialize(void){FILE*infile;inti,j;doublelbound,ubound;if((infile=fopen(
7、"gadata.txt","r"))==NULL){fprintf(galog,"Cannotopeninputfile!");exit(1);}/*initializevariableswithinthebounds*/for(i=0;i
此文档下载收益归作者所有