2022年遗传算法作业 .pdf

上传人:H****o 文档编号:33663388 上传时间:2022-08-12 格式:PDF 页数:11 大小:81.86KB
返回 下载 相关 举报
2022年遗传算法作业 .pdf_第1页
第1页 / 共11页
2022年遗传算法作业 .pdf_第2页
第2页 / 共11页
点击查看更多>>
资源描述

《2022年遗传算法作业 .pdf》由会员分享,可在线阅读,更多相关《2022年遗传算法作业 .pdf(11页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。

1、现代机械优化设计编程求解作业1 题目:利用遗传算法求 shaffer s F6函数222222001.015.0sin5 .0),(yxyxyxf的最优解100,100yx。要求给出源代码, 求出最优解和最优值,给出进化代数、种群规模、交叉率及变异率。1 求解思路在程序中将种群规模、 进化代数、交叉率及变异率分别取为不同数值,发现计算结果将收敛到不同的极大点。该函数的根据下,x和y的取值可以得到无穷多个极大点。首先需要建立一个文件在里面写上两个变量的最大值及最小值。即:-100 100 -100 100 在程序中取种群规模为50,进化代数为 1500,交叉率为 0.8,变异率为 0.15,该函

2、数的最优解为0 x,0y,对应的最优值为1。实际上,对应于这个最优解,种群规模、进化代数、交叉率及变异率可以取多组不同数值。如:取种群规模为500,进化代数为 1000,交叉率为 0.8,变异率为 0.15时仍可以得到这个最优解。2 程序的源代码#include 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 11 页 - - - - - - - - - 现代机械优化设计编程求解作业2 #include #include #define POPSIZE 50 /* 种群规

3、模*/ #define MAXGENS 2000 /* 最大进化代数*/ #define NVARS 2 /*变量数 */ #define PXOVER 0.8 /* 交叉率 */ #define PMUTATION 0.15 /* 变异率 */ #define TRUE 1 #define FALSE 0 int generation; /* 当前代 */ int cur_best; /* 最佳个体 */ FILE *galog; /* 输出文件*/ struct genotype /* genotype (GT), a member of the population */ double

4、geneNV ARS; /* 基因数组*/ double fitness; /* 适应度 */ double upperNVARS; /* 变量取值上限*/ double lowerNVARS; /*变量取值上限 */ double rfitness; /* 相对适应度*/ double cfitness; /* 累积适应度*/ ; struct genotype populationPOPSIZE+1; /* population */ struct genotype newpopulationPOPSIZE+1; /* new population; /* replaces the */

5、/* old generation */ /* Declaration of procedures used by this genetic algorithm */ 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 *); v

6、oid mutate(void); void report(void); /*/ 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 11 页 - - - - - - - - - 现代机械优化设计编程求解作业3 /* Initialization function: Initializes the values of genes */ /* within the variables bounds. It also initializes (to zero) */ /* all

7、 fitness values for each member of the population. It */ /* reads upper and lower bounds of each variable from the */ /* input file gadata.txt. It randomly generates values */ /* between these bounds for each gene of each genotype in the */ /* population. The format of the input file gadata.txt is *

8、/ /* var1_lower_bound var1_upper bound */ /* var2_lower_bound var2_upper bound . */ /*/ void initialize(void) FILE *infile; int i, j; double lbound, ubound; if (infile = fopen(gadata.txt,r)=NULL) fprintf(galog,nCannot open input file!n); exit(1); /* initialize variables within the bounds */ for (i =

9、 0; i NV ARS; i+) fscanf(infile, %lf,&lbound); fscanf(infile, %lf,&ubound); for (j = 0; j POPSIZE; j+) populationj.fitness = 0; populationj.rfitness = 0; populationj.cfitness = 0; populationj.loweri = lbound; populationj.upperi= ubound; populationj.genei = randval(populationj.loweri, populationj.upp

10、eri); fclose(infile); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 11 页 - - - - - - - - - 现代机械优化设计编程求解作业4 /*/ /* Random value generator: Generates a value within bounds */ /*/ double randval(double low, double high) double val; val = (double)(rand()%1000)/100

11、0.0)*(high - low) + low; return(val); /*/ /* Evaluation function: This takes a user defined function. */ /* Each time this is changed, the code has to be recompiled. */ /* The current function is: 222222001.015 .0sin5.0),(yxyxyxf*/ /*/ void evaluate(void) int mem; int i; double xNVARS+1; for (mem =

12、0; mem POPSIZE; mem+) for (i = 0; i NV ARS; i+) xi+1 = populationmem.genei; populationmem.fitness=-(pow(sin(sqrt(x1*x1+x2*x2),2) -0.5)/pow(1+0.001*(x1*x1+ x2*x2),2)+ 0.5; /*/ /* Keep_the_best function: This function keeps track of the */ /* best member of the population. Note that the last entry in

13、*/ /* the array Population holds a copy of the best individual */ /*/ 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 11 页 - - - - - - - - - 现代机械优化设计编程求解作业5 void keep_the_best() int mem; int i; cur_best = 0; /* stores the index of the best individual */ for (mem

14、 = 0; mem populationPOPSIZE.fitness) cur_best = mem; populationPOPSIZE.fitness = populationmem.fitness; /* once the best member in the population is found, copy the genes */ for (i = 0; i NV ARS; i+) populationPOPSIZE.genei = populationcur_best.genei; /*/ /* Elitist function: The best member of the

15、previous generation */ /* is stored as the last in the array. If the best member of */ /* the current generation is worse then the best member of the */ /* previous generation, the latter one would replace the worst */ /* member of the current population */ /*/ void elitist() int i; double best, wor

16、st; /* best and worst fitness values */ int best_mem, worst_mem; /* indexes of the best and worst member */ best = population0.fitness; worst = population0.fitness; for (i = 0; i populationi+1.fitness) if (populationi.fitness = best) best = populationi.fitness; best_mem = i; 名师资料总结 - - -精品资料欢迎下载 - -

17、 - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 11 页 - - - - - - - - - 现代机械优化设计编程求解作业6 if (populationi+1.fitness = worst) worst = populationi+1.fitness; worst_mem = i + 1; else if (populationi.fitness = best) best = populationi+1.fitness; best_mem = i + 1; /* if best individual from th

18、e new population is better than */ /* the best individual from the previous population, then */ /* copy the best from the new population; else replace the */ /* worst individual from the current population with the */ /* best one from the previous generation */ if (best = populationPOPSIZE.fitness)

19、for (i = 0; i NV ARS; i+) populationPOPSIZE.genei = populationbest_mem.genei; populationPOPSIZE.fitness = populationbest_mem.fitness; else for (i = 0; i NV ARS; i+) populationworst_mem.genei = populationPOPSIZE.genei; populationworst_mem.fitness = populationPOPSIZE.fitness; /*/ /* Selection function

20、: Standard proportional selection for */ /* maximization problems incorporating elitist model - makes */ /* sure that the best member survives */ /*/ 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 6 页,共 11 页 - - - - - - - - - 现代机械优化设计编程求解作业7 void select(void) int mem

21、, i, j, k; double sum = 0; double p; /* find total fitness of the population */ for (mem = 0; mem POPSIZE; mem+) sum += populationmem.fitness; /* calculate relative fitness */ for (mem = 0; mem POPSIZE; mem+) populationmem.rfitness = populationmem.fitness/sum; population0.cfitness = population0.rfit

22、ness; /* calculate cumulative fitness */ for (mem = 1; mem POPSIZE; mem+) populationmem.cfitness = populationmem-1.cfitness + populationmem.rfitness; /* finally select survivors using cumulative fitness. */ for (i = 0; i POPSIZE; i+) p = rand()%1000/1000.0; if (p population0.cfitness) newpopulationi

23、 = population0; else for (j = 0; j = populationj.cfitness & ppopulationj+1.cfitness) newpopulationi = populationj+1; /* once a new population is created, copy it back */ 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 7 页,共 11 页 - - - - - - - - - 现代机械优化设计编程求解作业8 for (

24、i = 0; i POPSIZE; i+) populationi = newpopulationi; /*/ /* Crossover selection: selects two parents that take part in */ /* the crossover. Implements a single point crossover */ /*/ void crossover(void) int i, mem, one; int first = 0; /* count of the number of members chosen */ double x; for (mem =

25、0; mem POPSIZE; +mem) x = rand()%1000/1000.0; if (x 1) if(NVARS = 2) point = 1; else point = (rand() % (NVARS - 1) + 1; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 8 页,共 11 页 - - - - - - - - - 现代机械优化设计编程求解作业9 for (i = 0; i point; i+) swap(&populationone.genei, &po

26、pulationtwo.genei); /*/ /* Swap: A swap procedure that helps in swapping 2 variables */ /*/ void swap(double *x, double *y) double temp; temp = *x; *x = *y; *y = temp; /*/ /* Mutation: Random uniform mutation. A variable selected for */ /* mutation is replaced by a random value between lower and */

27、/* upper bounds of this variable */ /*/ 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) /* find the bounds on the variable to be mutated */ lbound = populationi.lowerj; hbound = populationi.upp

28、erj; populationi.genej = randval(lbound, hbound); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 9 页,共 11 页 - - - - - - - - - 现代机械优化设计编程求解作业10 /*/ /* Report function: Reports progress of the simulation. Data */ /* dumped into the output file are separated by commas *

29、/ /*/ void report(void) int i; double best_val; /* best population fitness */ double avg; /* avg population fitness */ double stddev; /* std. deviation of population fitness */ double sum_square; /* sum of square for std. calc */ double square_sum; /* square of sum for std. calc */ double sum; /* to

30、tal population fitness */ 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 = avg * avg * POPSIZE; stddev = sqrt(sum_square - square_sum)/(POPSIZE - 1); best_val = populat

31、ionPOPSIZE.fitness; fprintf(galog, n%5d, %6.6f, %6.6f, %6.6f nn, generation, best_val, avg, stddev); void main(void) int i; if (galog = fopen(galog.txt,w)=NULL) exit(1); generation = 0; fprintf(galog, n generation best average standard n); fprintf(galog, number value fitness deviation n); 名师资料总结 - -

32、 -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 10 页,共 11 页 - - - - - - - - - 现代机械优化设计编程求解作业11 initialize(); evaluate(); keep_the_best(); while(generationMAXGENS) generation+; select(); crossover(); mutate(); report(); evaluate(); elitist(); fprintf(galog,nn Simulation complete

33、dn); fprintf(galog,n Best member: n); for (i = 0; i NV ARS; i+) fprintf (galog,n var(%d) = %3.3f,i,populationPOPSIZE.genei); fprintf(galog,nn Best fitness = %3.3f,populationPOPSIZE.fitness); fclose(galog); printf(Successn); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 11 页,共 11 页 - - - - - - - - -

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 技术资料 > 技术总结

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知得利文库网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号-8 |  经营许可证:黑B2-20190332号 |   黑公网安备:91230400333293403D

© 2020-2023 www.deliwenku.com 得利文库. All Rights Reserved 黑龙江转换宝科技有限公司 

黑龙江省互联网违法和不良信息举报
举报电话:0468-3380021 邮箱:hgswwxb@163.com