Hi,

I am trying to apply Best Sitting and Sizing of Distrubuted Generators
problem to find the  minimum  LMP by using PSO.
I use discrete and continuous variables in the problem as in the attachment.

But I get some errors during optimization procedure.but when I just create
a function and try to calculate, I don't get any error message, and the
function I wrote seems to be working.

Thanks already for your help.

Feyyaz
%This code can be modified using rounding 
%variables are 
%2 variables discrete (2,3..30) x1 and x3 
%2 variables continuous (0,..50 ) x2 and x4

clear all
close all
clc

% Define the details of the problem
nVar = 4; 
ub = [30 50 30 50 ];
lb = [2 0 2 0];
fobj = @dcrunopf10; 

% Define the PSO's paramters 
noP = 30;
maxIter = 500;
wMax = 0.9;
wMin = 0.2;
c1 = 2;
c2 = 2;
vMax = (ub - lb) .* 0.2; 
vMin  = -vMax;


% The PSO algorithm 

% Initialize the particles 
for k = 1 : noP
    Swarm.Particles(k).X = (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

        Swarm.Particles(k).X(1)=round(Swarm.Particles(k).X(1));
        Swarm.Particles(k).X(3)=round(Swarm.Particles(k).X(3));

        currentX = Swarm.Particles(k).X;
  
        Swarm.Particles(k).O = fobj(currentX);
        
        % Update the PBEST
        if Swarm.Particles(k).O < Swarm.Particles(k).PBEST.O 
            Swarm.Particles(k).PBEST.X = currentX;
            Swarm.Particles(k).PBEST.O = Swarm.Particles(k).O;
        end
        
        % Update the GBEST
        if Swarm.Particles(k).O < Swarm.GBEST.O
            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);
                                                                                
 
        
        % Check velocities 
        index1 = find(Swarm.Particles(k).V > vMax);
        index2 = find(Swarm.Particles(k).V < vMin);
        
        Swarm.Particles(k).V(index1) = vMax(index1);
        Swarm.Particles(k).V(index2) = vMin(index2);
        
        Swarm.Particles(k).X = Swarm.Particles(k).X + Swarm.Particles(k).V;
        
        % Check positions 
        index1 = find(Swarm.Particles(k).X > ub);
        index2 = find(Swarm.Particles(k).X < lb);
        
        Swarm.Particles(k).X(index1) = ub(index1);
        Swarm.Particles(k).X(index2) = lb(index2);
        
    end
    
    outmsg = ['Iteration# ', num2str(t) , ' Swarm.GBEST.O = ' , 
num2str(Swarm.GBEST.O)];
    disp(outmsg);
    
    cgCurve(t) = Swarm.GBEST.O;
end
 
semilogy(cgCurve);

Reply via email to