资源描述
.C语言遗传算法代码以下为遗传算法的源代码,计算一元代函数的代码和二元函数的代码以+为分割线分割开来,请自行选择适合的代码,使用时请略看完代码的注释,在需要更改的地方更改为自己需要的代码。+一元函数代码+#include #include#include#include#define POPSIZE 1000#define maximization 1#define minimization 2#define cmax 100#define cmin 0#define length1 20#define chromlength length1 /染色体长度 /注意,你是求最大值还是求最小值 int functionmode=minimization; /变量的上下限的修改开始 float min_x1=-2;/变量的下界 float max_x1=-1;/变量的上界 /变量的上下限的修改结束 int popsize; /种群大小int maxgeneration; /最大世代数 double pc; /交叉率 double pm; /变异率struct individual char chromchromlength+1;double value; double fitness; /适应度;int generation; /世代数int best_index;int worst_index;struct individual bestindividual; /最佳个体struct individual worstindividual; /最差个体struct individual currentbest;struct individual populationPOPSIZE;/函数声明 void generateinitialpopulation(); void generatenextpopulation();void evaluatepopulation();long decodechromosome(char *,int,int);void calculateobjectvalue();void calculatefitnessvalue();void findbestandworstindividual();void performevolution();void selectoperator();void crossoveroperator();void mutationoperator();void input();void outputtextreport();void generateinitialpopulation( ) /种群初始化int i,j;for (i=0;ipopsize; i+)for(j=0;jchromlength;j+)populationi.chromj=(rand()%2010)?0:1;populationi.chromchromlength=0;void generatenextpopulation() /生成下一代selectoperator();crossoveroperator();mutationoperator();void evaluatepopulation() /评价个体,求最佳个体calculateobjectvalue();calculatefitnessvalue();findbestandworstindividual();long decodechromosome(char *string ,int point,int length) /给染色体解码int i;long decimal=0;char*pointer;for(i=0,pointer=string+point;ilength;i+,pointer+)if(*pointer-0)decimal +=(long)pow(2,i);return (decimal);void calculateobjectvalue() /计算函数值int i;long temp1,temp2; double x1;for (i=0; ipopsize; i+) temp1=decodechromosome(populationi.chrom,0,length1); x1=(max_x1-min_x1)*temp1/(1024*1024-1)+min_x1; /目标函数修改开始 populationi.value=(pow(x1,5)-3*x1-1)*(pow(x1,5)-3*x1-1); /目标函数修改结束void calculatefitnessvalue()/计算适应度int i;double temp; for(i=0;i0.0) temp=cmin+populationi.value; else temp=0.0; else if (functionmode=minimization) if(populationi.valuecmax) temp=cmax-populationi.value; else temp=0.0;populationi.fitness=temp;void findbestandworstindividual( ) /求最佳个体和最差个体int i;double sum=0.0;bestindividual=population0;worstindividual=population0;for (i=1;ibestindividual.fitness)bestindividual=populationi;best_index=i;else if (populationi.fitness=currentbest.fitness)currentbest=bestindividual;void performevolution() /演示评价结果if (bestindividual.fitnesscurrentbest.fitness)currentbest=populationbest_index;elsepopulationworst_index=currentbest;void selectoperator() /比例选择算法int i,index;double p,sum=0.0;double cfitnessPOPSIZE;struct individual newpopulationPOPSIZE;for(i=0;ipopsize;i+)sum+=populationi.fitness;for(i=0;ipopsize; i+)cfitnessi=populationi.fitness/sum;for(i=1;ipopsize; i+)cfitnessi=cfitnessi-1+cfitnessi;for (i=0;icfitnessindex)index+;newpopulationi=populationindex;for(i=0;ipopsize; i+)populationi=newpopulationi;void crossoveroperator() /交叉算法int i,j;int indexPOPSIZE;int point,temp;double p;char ch;for (i=0;ipopsize;i+)indexi=i;for (i=0;ipopsize;i+)point=rand()%(popsize-i);temp=indexi;indexi=indexpoint+i;indexpoint+i=temp;for (i=0;ipopsize-1;i+=2)p=rand()%1000/1000.0;if (ppc)point=rand()%(chromlength-1)+1;for (j=point; jchromlength;j+)ch=populationindexi.chromj;populationindexi.chromj=populationindexi+1.chromj;populationindexi+1.chromj=ch;void mutationoperator() /变异操作int i,j;double p;for (i=0;ipopsize;i+)for(j=0;jchromlength;j+)p=rand()%1000/1000.0;if (ppm)populationi.chromj=(populationi.chromj=0)?1:0;void input() /数据输入 /printf(初始化全局变量:n);/printf( 种群大小(50-500):); /scanf(%d, &popsize);popsize=500; if(popsize%2) != 0) /printf( 种群大小已设置为偶数n);popsize+; /printf( 最大世代数(100-300):); /scanf(%d, &maxgeneration);maxgeneration=200; /printf( 交叉率(0.2-0.99):); /scanf(%f, &pc);pc=0.95; /printf( 变异率(0.001-0.1):); /scanf(%f, &pm);pm=0.03;void outputtextreport()/数据输出int i;double sum;double average;sum=0.0;for(i=0;ipopsize;i+)sum+=populationi.value;average=sum/popsize;printf(当前世代=%dn当前世代平均函数值=%fn当前世代最优函数值=%fn,generation,average,populationbest_index.value);void main() /主函数 int i;long temp1,temp2; double x1,x2; generation=0; input(); generateinitialpopulation();evaluatepopulation();while(generationmaxgeneration)generation+;generatenextpopulation();evaluatepopulation();performevolution();outputtextreport(); printf(n);printf( 统计结果: ); printf(n);/printf(最大函数值等于:%fn,currentbest.fitness);printf(其染色体编码为:);for (i=0;ichromlength;i+)printf(%c,currentbest.chromi); printf(n); temp1=decodechromosome(currentbest.chrom,0,length1); x1=(max_x1-min_x1)*temp1/(1024*1024-1)+min_x1; printf(x1=%lfn,x1); /这是需要修改的地方 printf(最优值等于:%fn,(pow(x1,5)-3*x1-1)*(pow(x1,5)-3*x1-1);+二元函数代码+#include #include#include#include#define POPSIZE 500#define maximization 1#define minimization 2#define cmax 100#define cmin 0#define length1 20#define length2 20#define chromlength length1+length2 /染色体长度/-求最大还是最小值int functionmode=maximization;/-/-变量上下界float min_x1=0;float max_x1=3;float min_x2=1;float max_x2=5;/- int popsize; /种群大小int maxgeneration; /最大世代数 double pc; /交叉率 double pm; /变异率struct individual char chromchromlength+1;double value; double fitness; /适应度;int generation; /世代数int best_index;int worst_index;struct individual bestindividual; /最佳个体struct individual worstindividual; /最差个体struct individual currentbest;struct individual populationPOPSIZE;/函数声明 void generateinitialpopulation(); void generatenextpopulation();void evaluatepopulation();long decodechromosome(char *,int,int);void calculateobjectvalue();void calculatefitnessvalue();void findbestandworstindividual();void performevolution();void selectoperator();void crossoveroperator();void mutationoperator();void input();void outputtextreport();void generateinitialpopulation( ) /种群初始化int i,j;for (i=0;ipopsize; i+)for(j=0;jchromlength;j+)populationi.chromj=(rand()%4020)?0:1;populationi.chromchromlength=0;void generatenextpopulation() /生成下一代selectoperator();crossoveroperator();mutationoperator();void evaluatepopulation() /评价个体,求最佳个体calculateobjectvalue();calculatefitnessvalue();findbestandworstindividual();long decodechromosome(char *string ,int point,int length) /给染色体解码int i;long decimal=0;char*pointer;for(i=0,pointer=string+point;ilength;i+,pointer+)if(*pointer-0)decimal +=(long)pow(2,i);return (decimal);void calculateobjectvalue() /计算函数值int i;long temp1,temp2; double x1,x2;for (i=0; ipopsize; i+) temp1=decodechromosome(populationi.chrom,0,length1); temp2=decodechromosome(populationi.chrom,length1,length2); x1=(max_x1-min_x1)*temp1/(1024*1024-1)+min_x1; x2=(max_x2-min_x2)*temp2/(1024*1024-1)+min_x2; /-函数populationi.value=x1*x1+sin(x1*x2)-x2*x2;/-void calculatefitnessvalue()/计算适应度int i;double temp; for(i=0;i0.0) temp=cmin+populationi.value; else temp=0.0; else if (functionmode=minimization) if(populationi.valuecmax) temp=cmax-populationi.value; else temp=0.0;populationi.fitness=temp;void findbestandworstindividual( ) /求最佳个体和最差个体int i;double sum=0.0;bestindividual=population0;worstindividual=population0;for (i=1;ibestindividual.fitness)bestindividual=populationi;best_index=i;else if (populationi.fitness=currentbest.fitness)currentbest=bestindividual;void performevolution() /演示评价结果if (bestindividual.fitnesscurrentbest.fitness)currentbest=populationbest_index;elsepopulationworst_index=currentbest;void selectoperator() /比例选择算法int i,index;double p,sum=0.0;double cfitnessPOPSIZE;struct individual newpopulationPOPSIZE;for(i=0;ipopsize;i+)sum+=populationi.fitness;for(i=0;ipopsize; i+)cfitnessi=populationi.fitness/sum;for(i=1;ipopsize; i+)cfitnessi=cfitnessi-1+cfitnessi;for (i=0;icfitnessindex)index+;newpopulationi=populationindex;for(i=0;ipopsize; i+)populationi=newpopulationi;void crossoveroperator() /交叉算法int i,j;int indexPOPSIZE;int point,temp;double p;char ch;for (i=0;ipopsize;i+)indexi=i;for (i=0;ipopsize;i+)point=rand()%(popsize-i);temp=indexi;indexi=indexpoint+i;indexpoint+i=temp;for (i=0;ipopsize-1;i+=2)p=rand()%1000/1000.0;if (ppc)point=rand()%(chromlength-1)+1;for (j=point; jchromlength;j+)ch=populationindexi.chromj;populationindexi.chromj=populationindexi+1.chromj;populationindexi+1.chromj=ch;void mutationoperator() /变异操作int i,j;double p;for (i=0;ipopsize;i+)for(j=0;jchromlength;j+)p=rand()%1000/1000.0;if (ppm)populationi.chromj=(populationi.chromj=0)?1:0;void input() /数据输入 /printf(初始化全局变量:n);/printf( 种群大小(50-500):); /scanf(%d, &popsize);popsize=200; if(popsize%2) != 0) /printf( 种群大小已设置为偶数n);popsize+; /printf( 最大世代数(100-300):); /scanf(%d, &maxgeneration);maxgeneration=200; /printf( 交叉率(0.2-0.99):); /scanf(%f, &pc);pc=0.9; /printf( 变异率(0.001-0.1):); /scanf(%f, &pm);pm=0.003;void outputtextreport()/数据输出int i;double sum;double average;sum=0.0;for(i=0;ipopsize;i+)sum+=populationi.value;average=sum/popsize;printf(当前世代=%dn当前世代平均函数值=%fn当前世代最优函数值=%fn,generation,average,populationbest_index.value);void main() /主函数 int i;long temp1,temp2; double x1,x2; generation=0; input(); generateinitialpopulation();evaluatepopulation();while(generationmaxgeneration)generation+;generatenextpopulation();evaluatepopulation();performevolution();outputtextreport(); printf(n);printf( 统计结果: ); printf(n);/printf(最大函数值等于:%fn,currentbest.fitness);printf(其染色体编码为:);for (i=0;ichromlength;i+)printf(%c,currentbest.chromi); printf(n); temp1=decodechromosome(currentbest.chrom,0,length1); temp2=decodechromosome(currentbest.chrom,length1,length2); x1=(max_x1-min_x1)*temp1/(1024*1024-1)+min_x1; x2=(max_x2-min_x2)*temp2/(1024*1024-1)+min_x2; printf(x=%lf,y=%lfn,x1,x2); /-修改函数 printf(最大值=%fn,x1*x1+sin(x1*x2)-x2*x2);/-.
展开阅读全文