遺傳算法C語言源代碼(一元函數和二元函數)
《遺傳算法C語言源代碼(一元函數和二元函數)》由會員分享,可在線閱讀,更多相關《遺傳算法C語言源代碼(一元函數和二元函數)(15頁珍藏版)》請在裝配圖網上搜索。
.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當前世代最優(yōu)函數值=%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( 統(tǒng)計結果: ); 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(最優(yōu)值等于:%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當前世代最優(yōu)函數值=%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( 統(tǒng)計結果: ); 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);/-.- 配套講稿:
如PPT文件的首頁顯示word圖標,表示該PPT已包含配套word講稿。雙擊word圖標可打開word文檔。
- 特殊限制:
部分文檔作品中含有的國旗、國徽等圖片,僅作為作品整體效果示例展示,禁止商用。設計者僅對作品中獨創(chuàng)性部分享有著作權。
- 關 鍵 詞:
- 遺傳 算法 語言 源代碼 一元函數 二元 函數
裝配圖網所有資源均是用戶自行上傳分享,僅供網友學習交流,未經上傳用戶書面授權,請勿作他用。
鏈接地址:http://m.italysoccerbets.com/p-12939708.html