资源描述:
《遗传算法C语言源代码(一元函数和二元函数)》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、C语言遗传算法代码以下为遗传算法的源代码,计算一元代函数的代码和二元函数的代码以+++++++++++++++++++++++++++++++++++++为分割线分割开来,请自行选择适合的代码,使用时请略看完代码的注释,在需要更改的地方更改为自己需要的代码。+++++++++++++++++++++++++++++++一元函数代码++++++++++++++++++++++++++++#include#include#include#include#definePOPSIZE1000#definemax
2、imization1#defineminimization2#definecmax100#definecmin0#definelength120#definechromlengthlength1//染色体长度//注意,你是求最大值还是求最小值intfunctionmode=minimization;//变量的上下限的修改开始floatmin_x1=-2;//变量的下界floatmax_x1=-1;//变量的上界//变量的上下限的修改结束intpopsize;//种群大小intmaxgeneration;//最大世代数doublepc;//交叉率doublepm;//变异率
3、structindividual{charchrom[chromlength+1];doublevalue;doublefitness;//适应度};intgeneration;//世代数intbest_index;intworst_index;structindividualbestindividual;//最佳个体structindividualworstindividual;//最差个体structindividualcurrentbest;structindividualpopulation[POPSIZE];//函数声明voidgenerateinitialpo
4、pulation();voidgeneratenextpopulation();voidevaluatepopulation();longdecodechromosome(char*,int,int);voidcalculateobjectvalue();voidcalculatefitnessvalue();voidfindbestandworstindividual();voidperformevolution();voidselectoperator();voidcrossoveroperator();voidmutationoperator();voidinput
5、();voidoutputtextreport();voidgenerateinitialpopulation()//种群初始化{inti,j;for(i=0;i