%PSO Approach For Solving Knapsack_Problem
clear;
clc;
%*************************************
seg(1)=0;
success=0;
PopSize=30; % Swarm Size
dim=80; % Particle Size
iter=0;
ErrGoal=1e-14;
w1=0.9;
w2=0.4;
MaxIt=4000;
c1=2.0;
c2=2.0;
% initial swarm
popul=rand(PopSize,dim)*(5.12+5.12);
vel=rand(PopSize,dim);
% mohasebe vazn
for i=1:PopSize
fit(i)=0;
for j=1:dim
fit(i)=fit(i)+popul(i,j).^2-10*cos(2*pi*popul(i,j))+10;
end
fit(i)=fit(i)+1;
end
% Global Best Position
pbestx=popul;
pbestfit=fit;
[bestfar,g]=min(fit);
gbestfit=bestfar;
gbestx=popul(g,:);
%*******************************************
while(success==0)&&(iter<MaxIt)
iter=iter+1;
for i=1:PopSize % mohasebe vazn in Knapsack
t(i)=gbestfit/pbestfit(i);
if t(i)<1
if t(i)<0.0001
g=2;
elseif t(i)>=0.0001&t(i)<0.01
g=1;
elseif t(i)>=0.01&t(i)<0.1
g=0.5;
elseif t(i)>=0.3&t(i)<0.9
g=0.3;
elseif t(i)>=0.9&t(i)<1
g=0;
end
end
w=w1*g+w2; %inertia weiindxht
end
for i=1:PopSize,
A(i,:)=gbestx;
end
R1=rand(PopSize,dim);
R2=rand(PopSize,dim);
% Pso Approach
vel=w*vel+c1*R1.*(pbestx-popul)+c2*R2.*(A-popul);
popul=popul+vel;
for i=1:PopSize
for j=1:dim
if popul(i,j)>5.12
popul(i,j)=5.12;
end
if popul(i,j)<-5.12
popul(i,j)=-5.12;
end
end
end
for i=1:PopSize
fit(i)=0;
for j=1:dim
fit(i)=fit(i)+popul(i,j).^2-10*cos(2*pi*popul(i,j))+10;
end
fit(i)=fit(i)+1;
end
for i=1:PopSize
if fit(i)<pbestfit(i)
pbestfit(i)=fit(i);
pbestx(i,:)=popul(i,:)
end
end
[bestfar,g]=min(fit);
if bestfar<gbestfit;
gbestfit=bestfar;
gbestx=popul(i,:)
end
seg(iter)=gbestfit;
if abs(gbestfit-1)<=ErrGoal
success=1;
end
end % end for
%*******************************************************************
% print result
gbestx
gbestfit
iter
plot(1:iter,seg(1:iter),'-r')
xlabel('MaxIt');
ylabel('Value');
title('PSO Approach For Solving Knapsack Problem');
legend('Knapsack PSO');
PSO--Knapsack_Problem.rar_Knapsack Problem _knapsack matlab_prob
版权申诉
93 浏览量
2022-07-14
22:30:26
上传
评论
收藏 1KB RAR 举报
JonSco
- 粉丝: 66
- 资源: 1万+
最新资源
- MyBatis动态SQL是一种强大的特性,它允许我们在SQL语句中根据条件动态地添加或删除某些部分,从而实现更加灵活和高效的数据
- MyBatis动态SQL是一种强大的特性,它允许我们在SQL语句中根据条件动态地添加或删除某些部分,从而实现更加灵活和高效的数据
- MyBatis动态SQL是一种强大的特性,它允许我们在SQL语句中根据条件动态地添加或删除某些部分,从而实现更加灵活和高效的数据
- Docker在Ubuntu16.04上安装和部署Apache Storm
- test_kong.zip
- springboot权限验证学习-下
- SeetaFace6人脸质量评估C++代码实现Demo
- OCAuxiliaryTools
- 制药公司QC顶岗实习专题报告
- Rust 全面指南:从基础到高级,一网打尽 Rust 的编程知识
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈