BPSO-Discrete optimization(2)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_39286218/article/details/80479198

问题描述:

目标函数:o=sum(D_(x).^2),其中,x={0,1}; D_(x)={10,20,30,40}; 使用BPSO优化

文件目录:

BPSO.m

ObjectiveFunction.m

BPSO.m
clear all  
close all  
clc  
  
% Define the details of the binary optimization problem  
nVar = 20 ;   
ub =ones(1,nVar);  
lb = zeros(1,nVar);  
fobj = @ObjectiveFunction;   
  
% Define the PSO's paramters   
noP = 100;  
maxIter = 1000;  
wMax = 0.9;  
wMin = 0.2;  
c1 = 2;  
c2 = 2;  
vMax = (ub - lb) .* 0.2;   
vMin  = -vMax;  
  
% Initialize the particles   
for k = 1 : noP  
    Swarm.Particles(k).X = round ( (ub-lb) .* rand(1,nVar) + lb );   
    Swarm.Particles(k).V = zeros(1, nVar);   
    Swarm.Particles(k).PBEST.X = zeros(1,nVar);   
    Swarm.Particles(k).PBEST.O = inf;   
    Swarm.GBEST.X = zeros(1,nVar);  
    Swarm.GBEST.O = inf;  
end  
  
% Main loop  
for t = 1 : maxIter   
    % Calcualte the objective value  
    for k = 1 : noP  
        currentX = Swarm.Particles(k).X;  
        Swarm.Particles(k).O = fobj(currentX);            
        if Swarm.Particles(k).O < Swarm.Particles(k).PBEST.O     %update the PBEST  
            Swarm.Particles(k).PBEST.X = currentX;  
            Swarm.Particles(k).PBEST.O = Swarm.Particles(k).O;  
        end  
        if Swarm.Particles(k).O < Swarm.GBEST.O                  %update the GBEST  
            Swarm.GBEST.X = currentX;  
            Swarm.GBEST.O = Swarm.Particles(k).O;  
        end  
    end  
      
    % Update the X and V vectors   
    w = wMax - t .* ((wMax - wMin) / maxIter);  
    for k = 1 : noP  
        Swarm.Particles(k).V = w .* Swarm.Particles(k).V + c1 .* rand(1,nVar) .* (Swarm.Particles(k).PBEST.X - Swarm.Particles(k).X) ...  
                                                         + c2 .* rand(1,nVar) .* (Swarm.GBEST.X - Swarm.Particles(k).X);  
        index1 = find(Swarm.Particles(k).V > vMax);             % Check velocities   
        index2 = find(Swarm.Particles(k).V < vMin);  
        Swarm.Particles(k).V(index1) = vMax(index1);  
        Swarm.Particles(k).V(index2) = vMin(index2);  
       % Sigmoid transfer function   
       s = 1 ./ (1 + exp(Swarm.Particles(k).V));   
       for d = 1 : nVar                               % Update position   
            r = rand();   
            if r < s(d)  
                Swarm.Particles(k).X(d) = 0;  
            else  
                Swarm.Particles(k).X(d) = 1;  
            end  
       end  
    end  
    outmsg = ['Iteration# ', num2str(t) , ' Swarm.GBEST.O = ' , num2str(Swarm.GBEST.O)];  
    disp(outmsg);  
    cgCurve(t) = Swarm.GBEST.O;  
end  
   
semilogy(cgCurve);  
xlabel('Iteration#')  
ylabel('Weight')  
ObjectiveFunction.m
function [ o ] = ObjectiveFunction ( x )

Discrete_set = [10 , 20 , 30 , 40];
D_x = zeros(1,10);           %给 D_x 一个 size=10 的存储空间

idx = 0;                     % D_X 的下标
for k = 1 : 2 :  length (x)  % 1 : 20 
    idx = idx + 1;
    if        x(k) == 0 && x(k+1) == 0
        D_x(idx) = Discrete_set(1);
    elseif    x(k) == 0 && x(k+1) == 1
        D_x(idx) = Discrete_set(2);
    elseif    x(k) == 1 && x(k+1) == 0
        D_x(idx) = Discrete_set(3);
    elseif    x(k) == 1 && x(k+1) == 1
        D_x(idx) = Discrete_set(4);
    end
end

o = sum (D_x .^ 2);  % Sphere test function 

end

结果:


Iteration# 261 Swarm.GBEST.O = 1600
Iteration# 262 Swarm.GBEST.O = 1600
Iteration# 263 Swarm.GBEST.O = 1600
Iteration# 264 Swarm.GBEST.O = 1600
Iteration# 265 Swarm.GBEST.O = 1000
Iteration# 266 Swarm.GBEST.O = 1000
Iteration# 267 Swarm.GBEST.O = 1000
Iteration# 268 Swarm.GBEST.O = 1000

心得:

      1. BPSO.m与BPSO-Discrete optimization(1)中一致;

     2. ObjectiveFunction.m中 size(x)=20; size[D_(x)]=10; x={0,1}; D_(x)={10,20,30,40}; o[D_(x)]; 文件的建立即是将 x 与   D_(x) 联系的建立(for + switch)

猜你喜欢

转载自blog.csdn.net/sinat_39286218/article/details/80479198