|
改进的遗传算法-函数寻优:
百度网盘链接:
链接:http://pan.baidu.com/s/1jIkQs8u
具体链接在halcom.cn论坛,联系人QQ:3283892722
该论坛是一个学习交流平台,我会逐一的和大家分享学习。
欢迎大家录制视频,你可在论坛进行打赏分享。
视频专用播放器:http://halcom.cn/forum.php?mod=viewthread&tid=258&extra=page%3D1\
具体的代码如下:
- clc,clear,close all
- warning off
- tic
- popmin = [25]/100; % Lb
- popmax =[100]/100; % Ub
- pc = 0.75; % 0-1之间
- pm = 0.25; % 0-1之间
- nvar = 1; % 1个未知数
- itermax = 100; % 迭代次数
- sizepop = 100; % 种群
- % 初始化种群
- for i=1:sizepop
- pop(i,:) = code(popmin,popmax); % 实数编码
- fitness(i) = fun(pop(i,:)); % 目标函数值
- end
- [bestfitness,index] = min(fitness); % 极小值
- zbest = pop(index,:); % 最佳个体
- fitnessgbest = fitness; % 个体最优
- gbest = pop; % 最优种群
- fitnesszbest = bestfitness; % 全局最优
- %% 迭代寻优
- for i=1:itermax
- % 选择算子
- pop = select(sizepop,pop,fitness,popmin,popmax);
- % 交叉算子
- pop = cross(sizepop,pc,pop,popmin,popmax);
- % 变异
- pop = mut(sizepop,pm,pop,popmin,popmax);
-
- for j=1:sizepop
- fitness(j) = fun( pop(j,:) ); % 计算适应度值
- if fitness(j)<fitnesszbest
- fitnesszbest = fitness(j);
- zbest = pop(j,:);
- end
- end
- fitness_iter(i) = fitnesszbest;
- end
- toc
- disp(['最优解:',num2str(zbest*100)])
- disp(['最优解目标值:', num2str(fitnesszbest)] )
- figure,
- plot(fitness_iter,'ro-','linewidth',2)
复制代码 选择算子:
- function pop = select(sizepop,pop,fitness,popmin,popmax)
- [a, b] = sort(fitness,'descend');
- c = floor(sizepop/4);
- % 替换了最差的个体,保留了精英种群
- for i=1:c
- pop(b(i),:) = popmin + rand.*(popmax-popmin);
- end
复制代码 交叉:
- function pop = cross(sizepop,pc,pop,popmin,popmax)
- for j=1:sizepop*2
- if rand>pc
- a = [0,0];
- while min(a)==0
- a = floor( rand(1,2)*sizepop );
- end
- k = rand;
- pop1 = pop(a(1),:) ;
- pop2 = pop(a(2),:) ;
- pop12 = (1-k)*pop1+k*pop2;
- pop21 = (1-k)*pop2+k*pop1;
-
- flag1 = test( pop12 ,popmin,popmax);
- if flag1==1 % 不可行
- pop(a(1),:) = pop12;
- end
- flag2 = test( pop21 ,popmin,popmax);
- if flag2==1 % 不可行
- pop(a(2),:) = pop21;
- end
-
- end
- end
复制代码 变异:
- function pop = mut(sizepop,pm,pop,popmin,popmax)
- for j = 1:sizepop*2
- if rand>pm
- a=0;
- while a==0
- a = floor(rand*sizepop);
- end
- b = pop(a,:);
- if rand>0.5
- b = pop(a,:) + rand.* (popmax-pop(a,:));
- else
- b = pop(a,:) + rand.* (popmin-pop(a,:));
- end
-
- flag2 = test( b,popmin,popmax );
- if flag2==1 % 不可行
- pop(a,:) = b;
- end
-
- end
- end
复制代码 编码:
- function pop = code(popmin,popmax)
- flag =1;
- while flag==1
- pop = popmin + rand.*(popmax-popmin); % 实数编码
- % 检验染色体的可行性
- flag = test(pop,popmin,popmax);
- end
复制代码 染色体检验:
- function flag = test(pop,popmin,popmax)
- % 检验染色体的可行性
- % if pop(1)<popmin(1) || pop(1)>popmax(1) || pop(2)<popmin(2) || pop(2)>popmax(2)
- if pop<popmin || pop>popmax
- flag = 1; % 不可行
- else
- flag = 0; % 可行
- end
复制代码 目标函数:
- function y = fun(x1)
- x1 = x1*100;
- Q = 1000;
- fw = 0.6;
- Rg = 2;
- Qr = 38000;
- nL = 0.8;
- nb = 0.6;
- Rd = 1.5;
- T1 = 25;
- P1 = 0.15;
- L = 10000;
- d = 0.2;
- co = 4.2;
- cw = 2;
- % T2 = x1;
- % P2 = x2;
- % FrL = Rg*0.8/nL/Qr *( co*(1-fw) +cw*fw) *Q*(x1 - T1);
- % Fdf = Rd*(x2-P1)*Q*(1-fw)/3.6/nb;
- v = 6910.13-462.16382*x1+12.45893*x1^2-0.16682*x1^3+0.00111*x1^4-0.000002910778*x1^5;
- Hf = 0.0246*(Q*(1-fw))^1.75*(v^1.75)*L/(d^4.75);
- x2 = Hf/100 + 0.3;
- FrL = Rg*0.8/nL/Qr *( co*(1-fw) +cw*fw) *Q*(x1 - T1);
- Fdf = Rd*(x2-P1)*Q*(1-fw)/3.6/nb;
- y = FrL + Fdf;
复制代码
|
|