用浮点数编码解决遗传算法问题_第1页
用浮点数编码解决遗传算法问题_第2页
用浮点数编码解决遗传算法问题_第3页
用浮点数编码解决遗传算法问题_第4页
用浮点数编码解决遗传算法问题_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、遗传算法作业一:用浮点数编码解决以下问题。55f(XrX2) =迟 icos( + 1)x1 + i icos(+ 1)x2 + ii=1i=1其中-10乞xx2乞10求mirf(N,Xz)二:构造过程。第一步:确定决策变量和约束条件。题目中已给出了该问题的决策变量及约束条件。第二步:建立优化模型。既是题目给出所要求的。第三步:确定编码方法。第四步:确定解码方法。第五步:确定个体评价方法。这里可将个体的适应度直接取为对应的目标函数值,并且不再对它做其他变化处理,即有:F(X)=f(x1,x2)第六步:设计遗传算子。选择运算使用比例运算算子;交叉运算是用单点交叉算子;变异运算使用基本位变异算子。

2、第七步:确定遗传算法的运行参数。对于本例,设计基本遗传算法的运行参数如下: int cur_best; /* 最佳个体 */MAXGENS=80POPSIZE=20NVARS=2PXOVER=0.2PMUTATION=0.1三:代码如下。#i nclude "stdafx.h"#in clude <stdio.h>#in clude <stdlib.h>#in clude <math.h>#defi ne MAXGENS 80#defi ne POPSIZE 20#defi ne NVARS 2#defi ne PXOVER 0.2#de

3、fi ne PMUTATION 0.1#defi ne FALSE 0#defi ne TRUE 1int gen erati on;/*最大世代数*/*种群大小*/*变量个数*/*交叉概率*/*变异概率*/*最大世代数*/*种群大小*/*变量个数*/*交叉概率*/*变异概率*/*当前世代*/struct genotypedouble geneNV ARS;double fitness;double upperNV ARS;double lowerNV ARS;double rfitness;double cfitness;struct genotype populationPOPSIZE+1

4、;struct genotype newpopulationPOPSIZE+1;void initialize(void);double randval(double, double);void evaluate(void);void keep_the_best(void);void elitist(void);void select(void);void crossover(void);void Xover(int,int);void swap(double *, double *);void mutate(void);void report(void);void initialize(vo

5、id)int i, j;double lbound, ubound;for (i = 0; i < NV ARS; i+) scanf("%lf",&lbound);scanf("%lf",&ubound);for (j = 0; j < POPSIZE; j+)populationj.fitness = 0;populationj.rfitness = 0;populationj.cfitness = 0;populationj.loweri = lbound;populationj.upperi= ubound;popul

6、ationj.genei = randval(populationj.loweri, populationj.upperi); double randval(double low, double high)double val;val = (double)(rand()%1000)/1000.0)*(high - low) + low;return(val);void evaluate(void)int mem;int i;int X=0;int Y=0;double xNV ARS+1;for (mem = 0; mem < POPSIZE; mem+)for (i = 0; i &l

7、t; NV ARS; i+)xi+1 = populationmem.genei;for(i=1;i<=5;i+)X=X+(i*cos(i+1)*x1+i);Y=Y+(i*cos(i+1)*x2+i); populationmem.fitness = (-1)*X*Y;void keep_the_best()int mem;int i;cur_best = 0;for (mem = 0; mem < POPSIZE; mem+)if (populationmem.fitness > populationPOPSIZE.fitness) cur_best = mem;popul

8、ationPOPSIZE.fitness = populationmem.fitness; for (i = 0; i < NV ARS; i+)populationPOPSIZE.genei = populationcur_best.genei;void elitist()int i;double best, worst;int best_mem, worst_mem;best = population0.fitness;worst = population0.fitness;for (i = 0; i < POPSIZE - 1; +i)if(populationi.fitne

9、ss > populationi+1.fitness)if (populationi.fitness >= best)best = populationi.fitness; best_mem = i;if (populationi+1.fitness <= worst) worst = populationi+1.fitness;worst_mem = i + 1;elseif (populationi.fitness <= worst)worst = populationi.fitness;worst_mem = i;if (populationi+1.fitness

10、 >= best)best = populationi+1.fitness;best_mem = i + 1;if (best >= populationPOPSIZE.fitness)for (i = 0; i < NV ARS; i+)populationPOPSIZE.genei = populationbest_mem.genei; populationPOPSIZE.fitness = populationbest_mem.fitness;elsefor (i = 0; i < NV ARS; i+)populationworst_mem.genei = po

11、pulationPOPSIZE.genei; populationworst_mem.fitness = populationPOPSIZE.fitness;void select(void)int mem, i, j, k;double sum = 0;double p;for (mem = 0; mem < POPSIZE; mem+)sum += populationmem.fitness;for (mem = 0; mem < POPSIZE; mem+)populationmem.rfitness = populationmem.fitness/sum; populati

12、on0.cfitness = population0.rfitness; for (mem = 1; mem < POPSIZE; mem+)populationmem.cfitness = populationmem-1.cfitness + populationmem.rfitness; for (i = 0; i < POPSIZE; i+)p = rand()%1000/1000.0;if (p < population0.cfitness) newpopulationi = population0;else for (j = 0; j < POPSIZE;j+

13、)if (p >= populationj.cfitness && p<populationj+1.cfitness) newpopulationi = populationj+1; for (i = 0; i < POPSIZE; i+)populationi = newpopulationi;void crossover(void)int i, mem, one;int first = 0; double x;for (mem = 0; mem < POPSIZE; +mem)x = rand()%1000/1000.0;if (x < PXO

14、VER) +first;if (first % 2 = 0)Xover(one, mem); elseone = mem;void Xover(int one, int two)int i;int point;if(NV ARS > 1)if(NV ARS = 2)point = 1;elsepoint = (rand() % (NV ARS - 1) + 1;for (i = 0; i < point; i+)swap(&populationone.genei, &populationtwo.genei);void swap(double *x, double *

15、y)double temp;temp = *x;*x = *y;*y = temp;void mutate(void)int i, j;double lbound, hbound;double x;for (i = 0; i < POPSIZE; i+)for (j = 0; j < NV ARS; j+)x = rand()%1000/1000.0;if (x < PMUTATION)lbound = populationi.lowerj;hbound = populationi.upperj; populationi.genej = randval(lbound, hbo

16、und); void report(void)int i;double best_val;double avg;double stddev;double sum_square;double square_sum;double sum;sum = 0.0;sum_square = 0.0;for (i = 0; i < POPSIZE; i+)sum += populationi.fitness;sum_square += populationi.fitness * populationi.fitness; avg = sum/(double)POPSIZE; square_sum = a

17、vg * avg * POPSIZE; stddev = sqrt(sum_square - square_sum)/(POPSIZE - 1); best_val = populationPOPSIZE.fitness;printf("n%5d,%6.3f, %6.3f, %6.3f nn", generation, best_val, avg, stddev); int main() int i; generation = 0; printf("n generation best average standard n"); printf("

18、 number value fitness deviation n"); initialize(); evaluate(); keep_the_best();while(generation<MAXGENS) generation+; select(); crossover(); mutate(); report(); evaluate(); elitist(); printf("nn Simulation completedn"); printf("n Best member: n"); for (i = 0; i < NV AR

19、S; i+)printf ("n var(%d) = %3.3f",i,populationPOPSIZE.genei); printf("nn Best fitness = %3.3f",populationPOPSIZE.fitness); 四:结果。 generation best average standard number value fitness deviation-1010 -10101,35.000, -159.600, 49.378 2,40.000, -162.500, 51.527 3,63.000, -227.800, 107

20、.370 4,338.000, 288.000, 0.000 5,338.000, -1127.000, 722.9456,338.000, -496.200, 188.442 7,338.000, -1949.400, 1010.274 8,338.000, -1228.750, 417.198 9,338.000, -737.400, 321.81410,338.000, -620.500, 201.032 11,338.000, -381.050, 149.048 12,338.000, -492.800, 201.228 13,338.000, -687.950, 264.773 14

21、,338.000, -534.750, 188.388 15,338.000, -683.700, 253.797 16,338.000, -1771.600, 457.733 17,338.000, -1523.000, 489.257 18,338.000, -1231.600, 422.400 19,338.000, -918.600, 412.579 20,338.000, -715.200, 432.551 21,338.000, -822.700, 516.523 22,338.000, -508.350, 253.088 23,338.000, -143.400, 103.914

22、 24,338.000, -336.750, 121.766 25,338.000, -314.800, 238.319 26,338.000, -423.600, 229.694 27,338.000, -381.650, 226.027 28,338.000, -308.400, 167.353 29,338.000, -380.600, 103.331 30,338.000, -1646.000, 1005.859 31,338.000, -4075.900, 1982.759 32,338.000, -1589.300, 956.300 33,338.000, -1566.700, 5

23、89.679 34,338.000, -493.050, 162.640 35,338.000, -251.550, 177.270 36,338.000, -201.550, 40.163 37,338.000, -412.650, 159.800 38,338.000, -144.850, 63.644 39,338.000, -244.450, 45.629 40,338.000, -172.850, 69.363 41,338.000, -37.000, 18.070 42,338.000, -79.000, 31.573 43,338.000, -68.400, 20.556 44,

24、338.000, -79.000, 30.042 45,338.000, 338.000, 0.000 46,338.000, -467.100, 154.784 47,338.000, -365.600, 108.007 48,338.000, -160.150, 70.261 49,338.000, -357.000, 129.672 50,338.000, -364.000, 138.034 51,338.000, -231.200, 62.732 52,338.000, -394.000, 81.458 53,338.000, -373.550, 168.63554,338.000, -206.000, 67.17455,338.000, -101.800, 33.82956,338.000, -186.800, 51.19457,338.000, 99.500, 141.27558,338.000, 142.800, 147.30459,3

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论