PSO拟合源码程序
% PSO : Particle Swarm Optimization (VECTORIZED)
% particle swarm optimization (PSO) is a computational method that
% optimizes a problem by iteratively trying to improve a candidate
% solution with regard to a given measure of quality. PSO optimizes
% a problem by having a population of candidate solutions, here dubbed
% particles, and moving these particles around in the search-space
% according to simple mathematical formulae over the particle's position
% and velocity. Each particle's movement is influenced by its local best
% known position but, is also guided toward the best known positions
% in the search-space, which are updated as better positions are found
% by other particles. This is expected to move the swarm toward the best solutions.
%
% this function is written as a simple example of PSO
% in order to optimiza other functions you have to change the objective
% function.
%
% to increase the efficieny of the PSO this version is a vectorized version
% for MATLAB.
%
% -----------------------------------
% code: REZA AHMADZADEH (based on the original code by Wesam Elshamy)
% (reza.ahmadzadeh@iit.it)
% -----------------------------------
clc;clear all;close all;
%% initialization
%
swarm_size = 64; % number of the swarm particles
maxIter = 50; % maximum number of iterations
inertia = 1.0;
correction_factor = 2.0;
% set the position of the initial swarm
a = 1:8;
[X,Y] = meshgrid(a,a);
C = cat(2,X',Y');
D = reshape(C,[],2);
swarm(1:swarm_size,1,1:2) = D; % set the position of the particles in 2D
swarm(:,2,:) = 0; % set initial velocity for particles
swarm(:,4,1) = 1000; % set the best value so far
plotObjFcn = 1; % set to zero if you do not need a final plot
%% define the objective funcion here (vectorized form)
objfcn = @(x)(x(:,1) - 20).^2 + (x(:,2) - 25).^2;
tic;
%% The main loop of PSO
for iter = 1:maxIter
swarm(:, 1, 1) = swarm(:, 1, 1) + swarm(:, 2, 1)/1.3; %update x position with the velocity
swarm(:, 1, 2) = swarm(:, 1, 2) + swarm(:, 2, 2)/1.3; %update y position with the velocity
x = swarm(:, 1, 1); % get the updated position
y = swarm(:, 1, 2); % updated position
fval = objfcn([x y]); % evaluate the function using the position of the particle
% compare the function values to find the best ones
for ii = 1:swarm_size
if fval(ii,1) < swarm(ii,4,1)
swarm(ii, 3, 1) = swarm(ii, 1, 1); % update best x position,
swarm(ii, 3, 2) = swarm(ii, 1, 2); % update best y postions
swarm(ii, 4, 1) = fval(ii,1); % update the best value so far
end
end
[~, gbest] = min(swarm(:, 4, 1)); % find the best function value in total
% update the velocity of the particles
swarm(:, 2, 1) = inertia*(rand(swarm_size,1).*swarm(:, 2, 1)) + correction_factor*(rand(swarm_size,1).*(swarm(:, 3, 1) ...
- swarm(:, 1, 1))) + correction_factor*(rand(swarm_size,1).*(swarm(gbest, 3, 1) - swarm(:, 1, 1))); %x velocity component
swarm(:, 2, 2) = inertia*(rand(swarm_size,1).*swarm(:, 2, 2)) + correction_factor*(rand(swarm_size,1).*(swarm(:, 3, 2) ...
- swarm(:, 1, 2))) + correction_factor*(rand(swarm_size,1).*(swarm(gbest, 3, 2) - swarm(:, 1, 2))); %y velocity component
% plot the particles
clf;plot(swarm(:, 1, 1), swarm(:, 1, 2), 'bx'); % drawing swarm movements
axis([-2 40 -2 40]);
pause(.1); % un-comment this line to decrease the animation speed
disp(['iteration: ' num2str(iter)]);
end
toc
%% plot the function
if plotObjFcn
ub = 40;
lb = 0;
npoints = 1000;
x = (ub-lb) .* rand(npoints,2) + lb;
for ii = 1:npoints
f = objfcn([x(ii,1) x(ii,2)]);
plot3(x(ii,1),x(ii,2),f,'.r');hold on
end
plot3(swarm(1,3,1),swarm(1,3,2),swarm(1,4,1),'xb','linewidth',5,'Markersize',5);grid
end
相关文章
- Hashmap 面试题 + Hashmap 原理 + Hashmap 源码(史上最全)
- SpringMVC -- 梗概--源码--贰--异常管理
- 2023年2.14情人节最浪漫的表白烟花,送给自己的脑婆(源码)
- 基于matlab程序hough变换检测圆源码
- 7波段参数均衡器源码程序
- 简单的导航仿真源码程序
- 微信小程序 - 实现文本展开与收起功能组件(初始时隐藏文字内容,当点击查看更多时展开)点击隐藏和点击显示 / 折叠与展开 / 大段文本内容过多,实现折叠与展开效果 ,超详细代码注释文章教程源码插件
- 微信小程序 - 最好用的多选功能,列表点击选中并且高亮显示(支持全选 / 反选,回显设置默认选择数据的功能,点击选中并改变样式,点击选中并高亮显示效果)详细教程功能源码组件插件
- Es编译源码
- Windows10超详细esmini的源码安装与测试运行——OpenScenario播放器
- 抓取微信小程序源码
- SwiftUI iOS 提示组件之 经典仿Apple Music和 AppStore反馈Alert Toast弹窗(教程含源码)
- SwiftUI 内功之Swift 数组Array如何按照不同数量划分stride (教程含源码)
- macOS SwiftUI 封装组件之 不确定指示器NSProgressIndicator(教程含源码)
- macOS SwiftUI 开发教程之经典双栏搜索框界面 (教程含源码)
- Kivy教程之用 Python 编写一个简单的基于 Kivy 的 CPU 监控应用程序(教程含源码)
- SwiftUI 动画教程之水波纹动态效果(教程含源码)
- 【毕业设计_课程设计】基于STM32的六臂行走机器小车设计(源码+论文)
- 图书资料管理课程设计(附源码和连接)
- 原生js源码之Array数组some方法
- 同步mysql部分表数据到docker容器中的mysql数据库供其他程序使用,附实际例子源码和镜像