clc;
clear;
close all;
warning off;
addpath(genpath(pwd));
fismat = readfis('d.fis');
Npop = 100;
MaxPop = 300;
Ngeneration = 200;
Pc = 0.7;
Pm = 0.9;
CrossPercent = 50;
MutatPercent = 10;
CrossNum = round(CrossPercent/100*Npop);
if mod(CrossNum,2)~=0;
CrossNum = CrossNum - 1;
end
MutatNum = round(MutatPercent/100*Npop);
ElitNum = Npop - CrossNum - MutatNum;
VarMin = 0;
VarMax = 10;
Nvar = 2;
CostFuncName = @Cost_Fcn;
Pop = rand(Npop,Nvar) * (VarMax - VarMin) + VarMin;
Cost = feval(CostFuncName,Pop);
[Cost Indx] = sort(Cost,'descend');
Pop = Pop(Indx,:);
%Initialize prog parameter
BestC = zeros(Ngeneration,1);
MeanC = 0*BestC;
for Iter = 1:Ngeneration
% Elitism
ElitPop = Pop(1:ElitNum,:);
% Cross Over
CrossPop = zeros(CrossNum,Nvar);
k = 1;
ParentIndexes = SelectParents_Fcn(Cost,CrossNum);
rand_cross=rand(floor(CrossNum/2),1);
for ii = 1:CrossNum/2
if rand_cross(ii) > Pc
Par1Indx = ParentIndexes(ii*2-1);
Par2Indx = ParentIndexes(ii*2);
Par1 = Pop(Par1Indx,:);
Par2 = Pop(Par2Indx,:);
Son = CrossOver_Fcn(Par1,Par2);
CrossPop(k:k+1,:) = Son;
k = k+2;
end
end
CrossPop = CrossPop(1:k-1,:);
% Mutation
k = 1;
indx = randperm(Npop);
for ii = 1:Npop
if rand > Pm
C = Pop(indx(ii),:);
MutatPop(k:k+1,:) = Mutation_Fcn(C,VarMax,VarMin,Iter,Ngeneration);
k = k+1;
end
if k > MutatNum,break,end
end
MutatPop = MutatPop(1:k-1,:);
% New Population
Pop = [ElitPop ; CrossPop ; MutatPop;Pop(ElitNum+1:end,:)];
l = size(Pop,1);
if l > MaxPop
Npop = MaxPop;
Pop = Pop(1:Npop,:);
else
Npop = l;
end
Cost = feval(CostFuncName,Pop);
[Cost Indx] = sort(Cost,'descend');
Pop = Pop(Indx,:);
% Algorithm Progress
varcost = mean(var(Pop));
dP = evalfis(varcost,fismat);
Pm = Pm + dP(1);
Pc = Pc + dP(2);
if Pc > .9, Pc = .9;
elseif Pc < .1, Pc = .1;end
if Pm > .97, Pm = .97;
elseif Pm < .03, Pm = .03;end
% Compute number of CrossOver & Mutation
MutatNum = round(MutatPercent/100*Npop);
CrossNum = round(CrossPercent/100*Npop);
BestC(Iter) = Cost(1);
MeanC(Iter) = mean(Cost);
% Show Result
plot(BestC(1:Iter),'r','linewidth',2);
hold on
plot(MeanC(1:Iter),'k','linewidth',2);
hold off
figure(gcf)
end
figure
plot(BestC/max(BestC),'r','linewidth',2);
hold on
plot(MeanC/max(BestC),'k','linewidth',2);
hold off
figure(gcf)
legend('Best Individual','Mean of Individuals')
xlabel('Generations')
ylabel('Normilized Fitness')
ylim([-.2,1.2])
%% Results
disp('----------------------------------------------')
BestSolution = Pop(1,:);
BestCost = Cost(1,:);
disp(['BestSolution = ',num2str(BestSolution)])
disp(['BestCost = ',num2str(BestCost)])
disp('----------------------------------------------')
%% End of Program
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
1.版本:matlab2021a,我录制了仿真操作录像,可以跟着操作出仿真结果 2.领域:自适应遗传算法 3.内容:自适应遗传算法,通过Mamdani模糊推理改变交叉和变异概率以提高遗传算法种群多样性 4.适合人群:本,硕等教研学习使用
资源推荐
资源详情
资源评论
收起资源包目录
基于模糊规则的自适应遗传算法matlab仿真,通过Mamdani模糊推理改变交叉和变异概率以提高遗传算法种群多样性.rar (7个子文件)
基于模糊规则的自适应遗传算法matlab仿真,通过Mamdani模糊推理改变交叉和变异概率以提高遗传算法种群多样性
操作录像0031.avi 2.03MB
d.fis 786B
func
Mutation_Fcn.m 316B
SelectParents_Fcn.m 340B
Cost_Fcn.m 246B
CrossOver_Fcn.m 227B
Runme.m 3KB
共 7 条
- 1
资源评论
- yyc09092023-11-14感谢资源主的分享,这个资源对我来说很有用,内容描述详尽,值得借鉴。
- 一帆风顺7122024-03-26感谢大佬分享的资源,对我启发很大,给了我新的灵感。
- 今天你学废了吗2024-08-08资源和描述一致,质量不错,解决了我的问题,感谢资源主。
- m0_744171542023-09-03资源值得借鉴的内容很多,那就浅学一下吧,值得下载!
fpga和matlab
- 粉丝: 17w+
- 资源: 2630
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功