ImageVerifierCode 换一换
格式:DOCX , 页数:18 ,大小:19.14KB ,
资源ID:10634427      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/10634427.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(遗传算法并行源程序.docx)为本站会员(b****7)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

遗传算法并行源程序.docx

1、遗传算法并行源程序/THBGFSHFGBCVXNCH#define WIN32_LEAN_AND_MEAN / 从 Windows 头中排除极少使用的资料#include #include #include #include #include #include #include #include #pragma comment(lib,Wininet.lib) #pragma comment(lib,mpi.lib)#pragma comment(lib,cxx.lib)#define POPSIZE 6 /* 群体大小,其实就是基因总数*/#define MAXGENS 10 /* 进化代数

2、 */#define NVARS 3 /* 问题变量数,即基因的长度*/#define PXOVER 0.8 /* 交叉概率 */#define PMUTATION 0.15 /* 变异概率 */#define TRUE 1#define FALSE 0int generation; /* 记录当前已经进化的代数 */int cur_best; /* 最优个体 */FILE *galog; /* 日志文件*/struct genotype /* 基因类型*/double geneNVARS; /* 染色体字符串*/double upperNVARS; /* 每个基于的上界*/double lo

3、werNVARS; /* 每个基因的下界*/double fitness; /* 个体适应度*/double rfitness; /* 相对适应度*/double cfitness; /* 累积适应度*/;struct genotype populationPOPSIZE+1; /* 种群*/struct genotype newpopulationPOPSIZE+1; /* 新种群*/void initialize(void); /* 种群初始化函数*/double randval(double, double); /* 随机函数*/void keep_the_best(void); /*寻

4、找最优个体*/ void elitist(void); /* 保持最优*/void select(void); /* 选择算子*/void crossover(void); /* 交叉算子*/void Xover(int,int); /* 交叉操作*/void swap(double *, double *); /* 交换*/void evaluate(void); /* 个体适应度评价*/void worker(genotype *,int ) /* 从处理器操作,突变和适应度评价*/void report(void); /* 记录*/*初始化基因值,适应值。从gadata.txt中读入每个

5、变量的上下限,然后随机产生。*/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);for (i = 0; i NVARS; i+)/nvars=3,问题变量数 fscanf(infile, %lf,&lbound); fscanf(infile, %lf,&ubound); for (j = 0; j POPSIZE; j+)/50 pop

6、ulationj.fitness = 0; populationj.rfitness = 0; populationj.cfitness = 0; populationj.loweri = lbound; populationj.upperi= ubound; populationj.genei = randval(populationj.loweri, populationj.upperi); fclose(infile);/*/* 在上下界间产生一个数 */*/double randval(double low, double high)double val;val = (double)(

7、rand()%1000)/1000.0)*(high - low) + low;return(val);/*/* Keep_the_best function: 保持对最优个体的追踪 */*/void keep_the_best()/将最优的值放在最后一个空位上int mem;int i;cur_best = 0; /* 最优个体索引 */for (mem = 0; mem populationPOPSIZE.fitness) cur_best = mem; populationPOPSIZE.fitness = populationmem.fitness; /* once the best

8、member in the population is found, copy the genes */for (i = 0; i NVARS; i+) populationPOPSIZE.genei = populationcur_best.genei;/*/* Elitist function: 如果这一代的最好值比上一代的 */* 差,那么用上一代的最好值替代本代的最差值 */*/void elitist()int i;double best, worst; /* best and worst fitness values */int best_mem, worst_mem; /* in

9、dexes 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; if (populationi+1.fitness = worst) worst = populationi+1.fitness; worst_mem = i + 1; else if (pop

10、ulationi.fitness = best) best = populationi+1.fitness; best_mem = i + 1; /* 如果新一代种群中的最优个体比前一代的最优个体好, */* best取此值,反之 用上一代最优个体取代当前代的最差个体。 */if (best = populationPOPSIZE.fitness) for (i = 0; i NVARS; i+) populationPOPSIZE.genei = populationbest_mem.genei; populationPOPSIZE.fitness = populationbest_mem.

11、fitness;else for (i = 0; i NVARS; i+) populationworst_mem.genei = populationPOPSIZE.genei; populationworst_mem.fitness = populationPOPSIZE.fitness; /*/* Selection function: 适者生存通过适应度大小筛选,适 */* 应度越大,出现的次数越多(会有重复) */*/void select(void)/int mem, i, j, k=0;double sum = 0;double p;/* 计算适应度总和*/for (mem =

12、0; mem POPSIZE; mem+) sum += populationmem.fitness;/* 计算相对适应度,有点像概率密度*/for (mem = 0; mem POPSIZE; mem+) populationmem.rfitness = populationmem.fitness/sum;/* 计算累计适应度,有点像分布函数*/population0.cfitness = population0.rfitness;for (mem = 1; mem POPSIZE; mem+) populationmem.cfitness = populationmem-1.cfitnes

13、s + populationmem.rfitness;/* 使用累计适应度确定幸存者,落在哪个概率区域就选择相应的概率区域上的染色体*/for (i = 0; i POPSIZE; i+) p = rand()%1000/1000.0; if (p population0.cfitness) newpopulationi = population0;/选择 else for (j = 0; j = populationj.cfitness & ppopulationj+1.cfitness) newpopulationi = populationj+1;/选择 /* 确定全部的幸存者后,拷贝回

14、population完成选择*/for (i = 0; i POPSIZE; i+) populationi = newpopulationi;/*/* Crossover selection: 单随机点交叉,前后两个 */* 个体交叉,得到两个新的个体,原来的个体消失 */*/void crossover(void)int i=0, mem, one;int first = 0; /* count of the number of members chosen */double x;for (mem = 0; mem POPSIZE; +mem) x = rand()%1000/1000.0

15、; if (x 1) /等于1时就没有交叉的必要 if(NVARS = 2) point = 1; else point = (rand() % (NVARS - 1) + 1; for (i = 0; i point; i+) swap(&populationone.genei, &populationtwo.genei);/*/* Swap: 互换 */*/void swap(double *x, double *y)double temp;temp = *x;*x = *y;*y = temp;void evaluate(void) /*个体适应度评价函数*/int mem;int i;

16、double xNVARS+1;for (mem = 0; mem POPSIZE; mem+) for (i = 0; i NVARS; i+) xi+1 = populationmem.genei; populationmem.fitness = x1*x1 + x2*x2 + x3*x3;/计算得到适应度void report(void) double best_val; /* 种群中最高适应度值*/ double avg; /* 平均适应度值*/ double stddev; /* 标准方差*/ double sum_square; /* 平方的总数*/ double square_s

17、um; /* 总数的平方*/ double sum; /* 适应度值总和*/ sum = 0.0; sum_square = 0.0; for (int 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); b

18、est_val = populationPOPSIZE.fitness; fprintf(galog, %5d tt %6.3f t %6.3f t %6.3fn, generation, best_val, avg, stddev); printf(%5d tt %6.3f t %6.3f t %6.3fn, generation, best_val, avg, stddev);void worker(genotype * subpopulation,int mysize) /*突变和适应度评价操作*/double lbound, hbound;double x;for (int i = 0

19、; i mysize; i+) for (int j = 0; j NVARS; j+) x = rand()%1000/1000.0; if (x lowerj; hbound = subpopulation-upperj; subpopulationi.genej = randval(lbound, hbound); /end 变异/适应度评价int mem;double xchormNVARS+1;for (mem = 0; mem mysize; mem+) for (int i = 0; i 1) mysize = POPSIZE/(numprocs-1);/每个从处理器上应该分配的

20、个体数else if (numprocs=1) mysize= POPSIZE;genotype * subpopulation = (genotype*)malloc(mysize*sizeof(genotype);if (!subpopulation) printf(不能获得 %d 个大小的空间n,mysize); MPI_Abort(MPI_COMM_WORLD,1);/自定义数据类型MPI_Datatype mystruct;int blocklens6;MPI_Aint indices6;MPI_Datatype old_types6;/各块中数据个数blocklens0 = NVA

21、RS;blocklens1 = NVARS;blocklens2 = NVARS;blocklens3 = 1;blocklens4 = 1;blocklens5 = 1;/数据类型for (i =0;igene,&indices0);MPI_Address(&population-upper,&indices1);MPI_Address(&population-lower,&indices2);MPI_Address(&population-fitness,&indices3);MPI_Address(&population-rfitness,&indices4);MPI_Address(&population-cfitness,&indices5);indices5=indices5-indices0;indices4=indices4-indices0;indices3=indices3-indices0;indices2=indices2-indices0;indices1=indices1-indices0;indices0=0;MPI_Type_struct(6,blocklens,indices,old_types,&mystruct);/生成新的mpi数据类型MPI_Type_commit(&mystruct);/输出表头,初始化,初步测评,初步寻优if(my

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1