2022年遗传算法作业 .pdf
《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; 名师资料总结 - - -精品资料欢迎下载 - -
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 2022年遗传算法作业 2022 遗传 算法 作业
限制150内