%Generic Algorithm for function f(x1,x2) optimum
clear all;
close all;
Size=30;
CodeL=3;
MinX(1)=zeros(1);
MaxX(1)=20*ones(1);
MinX(2)=zeros(1);
MaxX(2)=1.0*ones(1);
MinX(3)=zeros(1);
MaxX(3)=1.0*ones(1);
E(:,1)=MinX(1)+(MaxX(1)-MinX(1))*rand(Size,1);
E(:,2)=MinX(2)+(MaxX(2)-MinX(2))*rand(Size,1);
E(:,3)=MinX(3)+(MaxX(3)-MinX(3))*rand(Size,1);
G=100;%迭代次数
BsJ=0;
%*************** Start Running ***************
for kg=1:1:G
time(kg)=kg;
%****** Step 1 : Evaluate BestJ ******
for i=1:1:Size
x=E(i,:);
[x,BsJ]=GA_PID_Simulink_fitness(x,BsJ);
BsJi(i)=BsJ;
end
[OderJi,IndexJi]=sort(BsJi);
BestJ(kg)=OderJi(1);
BJ=BestJ(kg);
Ji=BsJi+1e-10; %Avoiding deviding zero
fi=1./Ji;
[Oderfi,Indexfi]=sort(fi); %Arranging fi small to bigger
Bestfi=Oderfi(Size); %Let Bestfi=max(fi)
BestS=E(Indexfi(Size),:); %Let BestS=E(m), m is the Indexfi belong to max(fi)
bfi(kg)=Bestfi;
kg
x
BJ
%****** Step 2 : Select and Reproduct Operation******
fi_sum=sum(fi);
fi_Size=(Oderfi/fi_sum)*Size;
fi_S=floor(fi_Size); % Selecting Bigger fi value
r=Size-sum(fi_S);
Rest=fi_Size-fi_S;
[RestValue,Index]=sort(Rest);
for i=Size:-1:Size-r+1
fi_S(Index(i))=fi_S(Index(i))+1; % Adding rest to equal Size
end
k=1;
for i=Size:-1:1 % Select the Sizeth and Reproduce firstly
for j=1:1:fi_S(i)
TempE(k,:)=E(Indexfi(i),:); % Select and Reproduce
k=k+1; % k is used to reproduce
end
end
%************ Step 3 : Crossover Operation ************
Pc=0.90;
for i=1:2:(Size-1)
temp=rand;
if Pc>temp %Crossover Condition
alfa=rand;
TempE(i,:)=alfa*E(i+1,:)+(1-alfa)*E(i,:);
TempE(i+1,:)=alfa*E(i,:)+(1-alfa)*E(i+1,:);
end
end
TempE(Size,:)=BestS;
E=TempE;
%************ Step 4: Mutation Operation **************
Pm=0.10-[1:1:Size]*(0.01)/Size; %Bigger fi,smaller Pm
Pm_rand=rand(Size,CodeL);
Mean=(MaxX + MinX)/2;
Dif=(MaxX-MinX);
for i=1:1:Size
for j=1:1:CodeL
if Pm(i)>Pm_rand(i,j) %Mutation Condition
TempE(i,j)=Mean(j)+Dif(j)*(rand-0.5);
end
end
end
%Guarantee TempE(Size,:) belong to the best individual
TempE(Size,:)=BestS;
E=TempE;
end
BestS
Bestfi
fi
x
Best_J=BestJ(G)
figure(1);
plot(time,BestJ);
xlabel('Times');ylabel('Best J');
save pfile x;
Simulink仿真_遗传算法PID控制
1星 需积分: 32 2 浏览量
2019-01-03
16:55:29
上传
评论 45
收藏 37KB ZIP 举报
bxy666999
- 粉丝: 18
- 资源: 12
最新资源
- Screenshot_20240427_031602.jpg
- 网页PDF_2024年04月26日 23-46-14_QQ浏览器网页保存_QQ浏览器转格式(6).docx
- 直接插入排序,冒泡排序,直接选择排序.zip
- 在排序2的基础上,再次对快排进行优化,其次增加快排非递归,归并排序,归并排序非递归版.zip
- 实现了7种排序算法.三种复杂度排序.三种nlogn复杂度排序(堆排序,归并排序,快速排序)一种线性复杂度的排序.zip
- 冒泡排序 直接选择排序 直接插入排序 随机快速排序 归并排序 堆排序.zip
- 课设-内部排序算法比较 包括冒泡排序、直接插入排序、简单选择排序、快速排序、希尔排序、归并排序和堆排序.zip
- Python排序算法.zip
- C语言实现直接插入排序、希尔排序、选择排序、冒泡排序、堆排序、快速排序、归并排序、计数排序,并带图详解.zip
- 常用工具集参考用于图像等数据处理
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈