function [pso F] = pso()
pop_size = 10; % pop_size
part_size = 2; % partible_size , ** =n-D
gbest = zeros(1,part_size+1); % gbest the best value so far
max_gen = 80; % max_gen max generation
region=zeros(part_size,2); % define searching regions
region=[-1,1;-1,1]; % define the size of regions
rand('state',sum(100*clock)); % reset the state of random machine
arr_present = ini_pos(pop_size,part_size); % present position, random initialization,rand() from 0~1
v=ini_v(pop_size,part_size); % initialized velocity
pbest = zeros(pop_size,part_size+1); % pbest the best result, last column contain the parameter as well
w_max = 0.9; % w_max maximum of weight
w_min = 0.4; % w_min minumum of weitht
v_max = 2; % ** maximum of velocity
c1 = 2; % learning parameter 1
c2 = 2; % learning parameter 2
best_record = zeros(1,max_gen); % best_record remember the best particle's parameters
% ————————————————————————
% Calulate the paricles affinity and initialization
% ————————————————————————
arr_present(:,end)=ini_fit(arr_present,pop_size,part_size);
pbest = arr_present; %initialized all best value for particles
[best_value best_index] = min(arr_present(:,end)); %initialize the global optima
gbest = arr_present(best_index,:);
x=[-1:0.01:1];
y=[-1:0.01:1];
z=@(x,y) cos(2 * pi .* x) .* cos(2 * pi .* y) .* exp(-0.1 * (x.^2 + y.^2));
for i=1:max_gen
grid on;
ezmesh(z,[-1,1,-1,1]),hold on,grid on,plot3(arr_present(:,1),arr_present(:,2),arr_present(:,3),'*'),hold off;
drawnow
F(i)=getframe;
pause(0.01);
% m(:,i) = getframe; %
w = w_max-(w_max-w_min)*i/max_gen;
reset = 0; % reset = 1
if reset==1
bit = 1;
for k=1:part_size
bit = bit&(range(arr_present(:,k))<0.1);
end
if bit==1 % bit=1, reset position and velocity
arr_present = ini_pos(pop_size,part_size); % present position
v = ini_v(pop_size,part_size); % velocity initialization
for k=1:pop_size % re-calculate affinity
arr_present(k,end) = fitness(arr_present(k,1:part_size));
end
warning('Reset particle because they already converged……');
display(i);
end
end
for j=1:pop_size
v(j,:) = w.*v(j,:)+c1.*rand.*(pbest(j,1:part_size)-arr_present(j,1:part_size))...
+c2.*rand.*(gbest(1:part_size)-arr_present(j,1:part_size)); % renew velocity (a)
% The abs(velocity) must <5————————————————————————————
c = find(abs(v)>6);
v(c) = sign(v(c))*6; %if v>3.14则,v=3.14
arr_present(j,1:part_size) = arr_present(j,1:part_size)+v(j,1:part_size); % renew position (b)
arr_present(j,end) = fitness(arr_present(j,1:part_size));
if (arr_present(j,end)>pbest(j,end))&(Region_in(arr_present(j,:),region)) % renew pbest
pbest(j,:) = arr_present(j,:);
end
end
[best best_index] = max(arr_present(:,end));
if best>gbest(end)&(Region_in(arr_present(best_index,:),region)) % if the result better than before, save to gbest
gbest = arr_present(best_index,:);
end
best_record(i) = gbest(end);
end
pso = gbest;
display(gbest);
figure;
plot(best_record);
% movie2avi(F,'pso_2D1.avi','compression','MSVC');
function fit = fitness(present)
fit=cos(2 * pi .* present(1)) .* cos(2 * pi .* present(1)) .* exp(-0.1 * (present(1).^2 + present(2).^2));
function ini_present=ini_pos(pop_size,part_size)
ini_present = 3*rand(pop_size,part_size+1); %initialized particles positions, randomly separate them in the region %** 6即为自变量范围
function ini_velocity=ini_v(pop_size,part_size)
ini_velocity =3/2*(rand(pop_size,part_size)); %initialized particles velocity, randomly separate them in the region
function flag=Region_in(pos_present,region)
[m n]=size(pos_present);
flag=1;
for j=1:n-1
flag=flag&(pos_present(1:j)>=region(j,1))&(pos_present(1:j)<=region(j,2));
end
function arr_fitness=ini_fit(pos_present,pop_size,part_size)
for k=1:pop_size
arr_fitness(k,1) = fitness(pos_present(k,1:part_size)); %calculate the initial population affinity
end
fpga和matlab
- 粉丝: 17w+
- 资源: 2636
最新资源
- 基于cruise的燃料电池功率跟随仿真,按照丰田氢能源车型搭建,在wltc工况下跟随效果好,最高车速175,最大爬坡30,百公里9s均已实现 1.模型通过cruise simulink联合仿真,策略
- C#源码 上位机 联合Visionpro 通用框架开发源码,已应用于多个项目,整套设备程序,可以根据需求编出来,具体Vpp功能自己编 程序包含功能 1.自动设置界面窗体个数及分布 2.照方式以命令触
- 程序名称:悬架设计计算程序 开发平台:基于matlab平台 计算内容:悬架偏频刚度挠度;螺旋弹簧,多片簧,少片簧,稳定杆,减震器的匹配计算;悬架垂向纵向侧向力学、纵倾、侧倾校核等;独立悬架杠杆比,等效
- 华为OD+真题及解析+智能驾驶
- jQuery信息提示插件
- 基于stm32的通信系统,sim800c与服务器通信,无线通信监测,远程定位,服务器通信系统,gps,sim800c,心率,温度,stm32 由STM32F103ZET6单片机核心板电路、DS18B2
- 充电器检测9-YOLO(v5至v11)、COCO、Create充电器检测9L、Paligemma、TFRecord、VOC数据集合集.rar
- 华为OD+考试真题+实现过程
- 保险箱检测51-YOLO(v5至v11)、COCO、CreateML、Paligemma、TFRecord、VOC数据集合集.rar
- 五相电机邻近四矢量SVPWM模型-MATLAB-Simulink仿真模型包括: (1)原理说明文档(重要):包括扇区判断、矢量作用时间计算、矢量作用顺序及切时间计算、PWM波的生成; (2)输出部分仿
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈