欢迎来到天天文库
浏览记录
ID:50967338
大小:23.73 KB
页数:16页
时间:2020-03-16
《遗传算法C语言源代码(一元函数和二元函数).doc》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、.专业整理.C语言遗传算法代码以下为遗传算法的源代码,计算一元代函数的代码和二元函数的代码以+++++++++++++++++++++++++++++++++++++为分割线分割开来,请自行选择适合的代码,使用时请略看完代码的注释,在需要更改的地方更改为自己需要的代码。+++++++++++++++++++++++++++++++一元函数代码++++++++++++++++++++++++++++#include#include#include#include#d
2、efinePOPSIZE1000#definemaximization1#defineminimization2#definecmax100#definecmin0#definelength120#definechromlengthlength1//染色体长度//注意,你是求最大值还是求最小值intfunctionmode=minimization;//变量的上下限的修改开始floatmin_x1=-2;//变量的下界floatmax_x1=-1;//变量的上界//变量的上下限的修改结束intpopsize;//种群大小intmax
3、generation;//最大世代数doublepc;//交叉率doublepm;//变异率structindividual{charchrom[chromlength+1];doublevalue;doublefitness;//适应度};intgeneration;//世代数intbest_index;.学习帮手..专业整理.intworst_index;structindividualbestindividual;//最佳个体structindividualworstindividual;//最差个体structindivid
4、ualcurrentbest;structindividualpopulation[POPSIZE];//函数声明voidgenerateinitialpopulation();voidgeneratenextpopulation();voidevaluatepopulation();longdecodechromosome(char*,int,int);voidcalculateobjectvalue();voidcalculatefitnessvalue();voidfindbestandworstindividual();vo
5、idperformevolution();voidselectoperator();voidcrossoveroperator();voidmutationoperator();voidinput();voidoutputtextreport();voidgenerateinitialpopulation()//种群初始化{inti,j;for(i=0;i6、pulation[i].chrom[chromlength]=' ';}}voidgeneratenextpopulation()//生成下一代{selectoperator();crossoveroperator();mutationoperator();}voidevaluatepopulation()//评价个体,求最佳个体{calculateobjectvalue();.学习帮手..专业整理.calculatefitnessvalue();findbestandworstindividual();}longdecodech7、romosome(char*string,intpoint,intlength)//给染色体解码{inti;longdecimal=0;char*pointer;for(i=0,pointer=string+point;i8、size;i++){temp1=decodechromosome(population[i].chrom,0,length1);x1=(max_x1-min_x1)*temp1/(1024*1024-1)+min_x1;//目标函数修
6、pulation[i].chrom[chromlength]=' ';}}voidgeneratenextpopulation()//生成下一代{selectoperator();crossoveroperator();mutationoperator();}voidevaluatepopulation()//评价个体,求最佳个体{calculateobjectvalue();.学习帮手..专业整理.calculatefitnessvalue();findbestandworstindividual();}longdecodech
7、romosome(char*string,intpoint,intlength)//给染色体解码{inti;longdecimal=0;char*pointer;for(i=0,pointer=string+point;i8、size;i++){temp1=decodechromosome(population[i].chrom,0,length1);x1=(max_x1-min_x1)*temp1/(1024*1024-1)+min_x1;//目标函数修
8、size;i++){temp1=decodechromosome(population[i].chrom,0,length1);x1=(max_x1-min_x1)*temp1/(1024*1024-1)+min_x1;//目标函数修
此文档下载收益归作者所有