1、基于遗传算法的函数优化智能多媒体实验报告实验六:基于遗传算法的函数优化姓名:班级:学号:同做者: 一、实验目的利用遗传算法求解函数极值。二、算法概要1遗传算法概要遗传算法是具有“生成+检测”的迭代过程的搜索算法。它的基本处理流程如图6.1所示。由此流程图可见,遗传算法是一种群体型操作,该操作以群体中的所有个体为对象。选择(selection)、交叉(crossover)和变异(mutation)是遗传算法的3个主要操作算子,它们构成了所谓的遗传操作(genetic operation),使遗传算法具有了其它传统方法所没有的特性。遗传算子包含如下6个基本因素:(1) 参数编码:由于遗传算法不能直
2、接处理解空间的解数据,因此必须通过编码将它们表示成遗传空间的基因型串结构数据。(2) 生成初始群体:由于遗传算法的群体型操作需要,所以必须为遗传操作准备一个由若干初始解组成的初始群体。初始群体的每个个体都是通过随机方法产生。(3) 适应度评估检测:遗传算法在搜索进化过程中一般不需要其他外部信息,仅用适应度(fitness)值来评估个体或解的优劣,并作为以后遗传操作的依据。(4) 选择(selection):选择或复制操作是为了从当前群体中选出优良的个体, 使它们有机会作为父代为下一代繁殖子孙。个体适应度越高,其被选择的机会就越多。此处采用与适用度成比例的概率方法进行选择。具体地说,就是首先计算
3、群体中所有个体适应度的总和(),再计算每个个体的适应度所占的比例(),并以此作为相应的选择概率。(5) 交叉操作:交叉操作是遗传算法中最主要的遗传操作。简单的交叉(即一点交叉)可分两步进行:首先对种群中个体进行随机配对;其次,在配对个体中随机设定交叉处,配对个体彼此交换部分信息。(6) 变异:变异操作是按位(bit)进行的,即把某一位的内容进行变异。变异操作同样也是随机进行的。一般而言,变异概率都取得较小。变异操作是十分微妙的遗传操作,它需要和交叉操作配合使用,目的是挖掘群体中个体的多样性,克服有可能限于局部解的弊病。这6个要素构成了遗传算法的核心内容,其流程如图6.1所示。图6.1 遗传算法
4、的基本流程2二进制编码及解码方法二进制编码是遗传算法中最主要的一种编码方法,它使用的编码符号集是由二进制符号0和1所组成的二进制符号集0,1,它所构成的个体基因型是一个二进制编码符号串。二进制编码符号串的长度与问题所要求的求解精度有关。假设某一参数的取值范围是Umin,Umax,我们用长度为L的二进制编码符号串来表示该参数,则它总共能够产生种不同的编码,若使参数编码时的对应关系如下:0000000000000000=0 Umin 0000000000000001=1 Umin+ 11111111111111111=2L1 Umax则二进制编码的编码精度为: (6-1)假设某一个个体的编码是:X
5、 bLbL1bL,则对应的解码公式为: (6-2)例如,对于,若用十位长的二进制编码来表示该参数的话,则下述符号串:X:0010101111就可表示一个个体,它所对应的参数值是。此时的编码精度为。其中:f 个体适应度Umin,Umax 某变量的取值范围 编码精度三、算法步骤及流程图Step1:参数设置及种群初始化;Step2:适应度评价;Step3:轮盘赌选择;Step4:交叉;Step5:变异;Step6:适应度评价;Step7:终止条件判断,若未达到终止条件,则转到Step3;Step8:输出结果。四、实验程序主程序clc;clear;close all;v = 2*rand(50,22)
6、-1;v=hardlim(v);N,L = size(v); ger = 200; pc = 0.5; pm = 0.01; updatef=0;c=0;disp(sprintf(Number of generations: %d,ger);disp(sprintf(Population size: %d,N);disp(sprintf(Crossover probability: %.3f,pc);disp(sprintf(Mutation probability: %.3f,pm);f=-1*(x.2+y.2);% General parameters & Initial operatio
7、nssol1=1; vmfit = ; it = 1; vx = ; C = ;updatef=-10;x = decod(v(:,1:11),11); y = decod(v(:,12:end),11); fit = eval(f);% Generationst0 = clock;while it sp(sindex) sindex=sindex+1; %寻找要选择个体的位置 end newv(i,:)=v(sindex,:); endfor i=1:N v(i,:)=newv(i,:);%用选择出的个体构成的种群替代旧的种群end% Crossverfor i=1:N cindex(i)=
8、i;endfor i=1:N %产生要配对的父代的序号;经过N次顺序调换,将原有顺序打乱,使相邻两个个体作为交叉的父代 point=unidrnd(N-i+1); temp=cindex(i); cindex(i)=cindex(i+point-1); cindex(i+point-1)=temp;endfor i=1:2:N p=rand(1); if(ppc) point=unidrnd(L-1)+1;%1pointL 产生交叉点 for j=point:(L-1) %交叉 ch=v(cindex(i),j); v(cindex(i),j)=v(cindex(i+1),j); %cinde
9、x中相邻的两个为两个父代的序号 v(cindex(i+1),j)=ch; end endend% MutationM=rand(N,L)=sol1 sol1=updatef; v(indb1,:)=updatec;endupdatef=sol1;updatec=v(indb1,:);sol2,indb2 = min(fit);v(indb2,:) = v(indb1,:);x = decod(v(:,1:11),11); y = decod(v(:,12:end),11); fit = eval(f);media = mean(fit);vx = vx sol1; vmfit = vmfit
10、media; if rem(it,1) = 0 | it = 10, if c=1 disp(sprintf(Gen.: %d x: %2.5f y: %2.5f Av: %2.2f f(x,y): %2.40f,it,x(indb1),y(indb1),media,sol1); else disp(sprintf(Gen.: %d Av: %2.2f f(x,y): %2.5f,it,media,sol1); end end; it = it + 1;end;T = etime(clock,t0); %F = flops - f0;X=x;Y=y;x = x(indb1); y = y(in
11、db1); fx = sol1; P = v;disp(sprintf(the total time is %2.4f,T); disp(sprintf(Maximum found f(x,y): %.2f,fx);% xx=vx;yy=vmfit; figure(2); plot(vx,k); title(f(x,y) x Mean); xlabel(Generations); ylabel(f(x,y); hold on; plot(vmfit,k:); legend(best,mean,0);hold off;decod.m子程序function code = decod(w_code,
12、 len)w_size = size(w_code);code = ones(w_size(1), 1);%N*1的矩阵%编写解码表code_arry = ones(1, len);for i = 0:len-1 code_arry(i+1) = 2.i;end%针对每一个样本进行解码for i = 1:w_size(1) w = w_code(i, :); m = w.*code_arry; code(i) = sum(m);end%转换到给定区间rate = 2./(2.len-1);code = -1 +rate.*code;return ;程序流程图:五、实验结果Number of g
13、enerations: 200Population size: 50Crossover probability: 0.500Mutation probability: 0.010Gen.: 1 x: 0.05227 y: 0.17440 Av: -0.55 f(x,y): -0.0331482272125327780000000000000000000000Gen.: 2 x: 0.04934 y: 0.17440 Av: -0.53 f(x,y): -0.0328503900402103420000000000000000000000Gen.: 3 x: -0.09331 y: -0.120
14、66 Av: -0.48 f(x,y): .023*Gen.: 4 x: 0.05227 y: 0.10992 Av: -0.42 f(x,y): -0.0148140582009155010000000000000000000000Gen.: 5 x: 0.05227 y: 0.10992 Av: -0.37 f(x,y): -0.0148140582009155010000000000000000000000Gen.: 6 x: 0.05227 y: 0.10992 Av: -0.34 f(x,y): -0.0148140582009155010000000000000000000000G
15、en.: 7 x: 0.05227 y: 0.07865 Av: -0.31 f(x,y): -0.0089184095590458482000000000000000000000Gen.: 8 x: 0.05227 y: 0.01612 Av: -0.33 f(x,y): -0.0029922135148867180000000000000000000000Gen.: 9 x: 0.05227 y: 0.01612 Av: -0.38 f(x,y): -0.0029922135148867180000000000000000000000Gen.: 10 x: 0.05227 y: 0.016
16、12 Av: -0.32 f(x,y): -0.0029922135148867180000000000000000000000Gen.: 11 x: 0.05227 y: 0.01612 Av: -0.24 f(x,y): -0.0029922135148867180000000000000000000000Gen.: 12 x: 0.04836 y: 0.01612 Av: -0.25 f(x,y): -0.0025989157104096623000000000000000000000Gen.: 13 x: 0.04836 y: 0.01612 Av: -0.24 f(x,y): -0.
17、0025989157104096623000000000000000000000Gen.: 14 x: 0.04836 y: 0.01612 Av: -0.22 f(x,y): -0.0025989157104096623000000000000000000000Gen.: 15 x: 0.04836 y: 0.01612 Av: -0.24 f(x,y): -0.0025989157104096623000000000000000000000Gen.: 16 x: 0.04836 y: 0.00049 Av: -0.25 f(x,y): -0.002339262790949082600000
18、0000000000000000Gen.: 17 x: 0.04836 y: 0.00049 Av: -0.30 f(x,y): -0.0023392627909490826000000000000000000000Gen.: 18 x: 0.04836 y: 0.00049 Av: -0.31 f(x,y): -0.0023392627909490826000000000000000000000Gen.: 19 x: 0.04836 y: 0.00049 Av: -0.34 f(x,y): -0.0023392627909490826000000000000000000000Gen.: 20
19、 x: 0.04836 y: 0.00049 Av: -0.37 f(x,y): -0.0023392627909490826000000000000000000000Gen.: 21 x: 0.03273 y: 0.03175 Av: -0.41 f(x,y): -0.0020796098714885034000000000000000000000Gen.: 22 x: 0.03273 y: 0.03175 Av: -0.33 f(x,y): -0.0020796098714885034000000000000000000000Gen.: 23 x: 0.03273 y: 0.03175 A
20、v: -0.30 f(x,y): -0.0020796098714885034000000000000000000000Gen.: 24 x: 0.03273 y: 0.03175 Av: -0.28 f(x,y): -0.0020796098714885034000000000000000000000Gen.: 25 x: 0.03273 y: 0.03175 Av: -0.26 f(x,y): -0.0020796098714885034000000000000000000000Gen.: 26 x: 0.03273 y: 0.03175 Av: -0.21 f(x,y): -0.0020
21、796098714885034000000000000000000000Gen.: 27 x: 0.03273 y: 0.03175 Av: -0.13 f(x,y): -0.0020796098714885034000000000000000000000Gen.: 28 x: 0.03273 y: 0.03175 Av: -0.13 f(x,y): -0.0020796098714885034000000000000000000000Gen.: 29 x: 0.03273 y: 0.03175 Av: -0.11 f(x,y): -0.0020796098714885034000000000
22、000000000000Gen.: 30 x: 0.03273 y: 0.03175 Av: -0.10 f(x,y): -0.0020796098714885034000000000000000000000Gen.: 31 x: 0.03273 y: 0.03175 Av: -0.10 f(x,y): -0.0020796098714885034000000000000000000000Gen.: 32 x: 0.03273 y: 0.03175 Av: -0.10 f(x,y): -0.0020796098714885034000000000000000000000Gen.: 33 x:
23、0.03273 y: 0.03175 Av: -0.19 f(x,y): -0.0020796098714885034000000000000000000000Gen.: 34 x: 0.02101 y: 0.03175 Av: -0.23 f(x,y): -0.0014495696992679788000000000000000000000Gen.: 35 x: 0.02101 y: 0.03175 Av: -0.21 f(x,y): -0.0014495696992679788000000000000000000000Gen.: 36 x: 0.02101 y: 0.03175 Av: -
24、0.22 f(x,y): -0.0014495696992679788000000000000000000000Gen.: 37 x: 0.02101 y: 0.03175 Av: -0.23 f(x,y): -0.0014495696992679788000000000000000000000Gen.: 38 x: 0.02101 y: 0.03175 Av: -0.17 f(x,y): -0.0014495696992679788000000000000000000000Gen.: 39 x: 0.02003 y: 0.03175 Av: -0.17 f(x,y): -0.00140947
25、62337630387000000000000000000000Gen.: 40 x: 0.02003 y: 0.03175 Av: -0.18 f(x,y): -0.0014094762337630387000000000000000000000Gen.: 41 x: 0.01710 y: 0.00440 Av: -0.15 f(x,y): -0.0003116789639848482400000000000000000000Gen.: 42 x: 0.01710 y: 0.00440 Av: -0.19 f(x,y): -0.00031167896398484824000000000000
26、00000000Gen.: 43 x: 0.01710 y: 0.00440 Av: -0.19 f(x,y): -0.0003116789639848482400000000000000000000Gen.: 44 x: 0.01710 y: 0.00440 Av: -0.21 f(x,y): -0.0003116789639848482400000000000000000000Gen.: 45 x: 0.01710 y: 0.00440 Av: -0.20 f(x,y): -0.0003116789639848482400000000000000000000Gen.: 46 x: 0.01
27、710 y: 0.00440 Av: -0.15 f(x,y): -0.0003116789639848482400000000000000000000Gen.: 47 x: 0.01710 y: 0.00440 Av: -0.15 f(x,y): -0.0003116789639848482400000000000000000000Gen.: 48 x: 0.00537 y: 0.00440 Av: -0.19 f(x,y): -0.0000482076192380845980000000000000000000Gen.: 49 x: 0.00537 y: 0.00440 Av: -0.25
28、 f(x,y): -0.0000482076192380845980000000000000000000Gen.: 50 x: 0.00537 y: 0.00440 Av: -0.20 f(x,y): -0.0000482076192380845980000000000000000000Gen.: 51 x: 0.00537 y: 0.00244 Av: -0.15 f(x,y): -0.0000348431307364366280000000000000000000Gen.: 52 x: 0.00537 y: 0.00049 Av: -0.13 f(x,y): -0.000029115492
29、8071597700000000000000000000Gen.: 53 x: 0.00537 y: 0.00049 Av: -0.13 f(x,y): -0.0000291154928071597700000000000000000000Gen.: 54 x: 0.00147 y: 0.00440 Av: -0.11 f(x,y): -0.0000214786422347901870000000000000000000Gen.: 55 x: 0.00147 y: 0.00440 Av: -0.09 f(x,y): -0.000021478642234790187000000000000000
30、0000Gen.: 56 x: 0.00147 y: 0.00440 Av: -0.07 f(x,y): -0.0000214786422347901870000000000000000000Gen.: 57 x: 0.00147 y: 0.00440 Av: -0.04 f(x,y): -0.0000214786422347901870000000000000000000Gen.: 58 x: 0.00049 y: 0.00440 Av: -0.03 f(x,y): -0.0000195694295916979000000000000000000000Gen.: 59 x: 0.00049 y: 0.00440 Av: -0.03 f(x,y): -0.0000195694295916979000000000000000000000Gen.: 60 x: 0.00049 y: 0.0044
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1