2014-01-26 44 views
1

我在matlab中使用matlab中的代碼需要一些幫助。我想做粒子羣優化,我想用鼠標單擊來定義一個窗口大小[min1,max1]和[min2,max2]的空間點。然後,一個由n = 10個粒子組成的簇初始化並搜索用戶初始設置的點。matlab中的粒子羣算法

我的代碼是這樣的:

clear all; 

numofdims = 30; 

numofparticles = 50; 

c1 = 2; 

c2 = 2; 

numofiterations = 1000; 

V = zeros(50, 30); 

initialpop = V; 

Vmin = zeros(30, 1); 

Vmax = Vmin; 

Xmax = ones(30, 1) * 100; 

Xmin = -Xmax; 

pbestfits = zeros(50, 1); 

worsts = zeros(50, 1); 

bests = zeros(50, 1); 

meanfits = zeros(50, 1); 

pbests = zeros(50, 30); 

initialpop = Xmin + (Xmax - Xmin) .* rand(numofparticles, numofdims); 

X = initialpop; 

fitnesses = testfunc1(X); 

[minfit, minfitidx] = min(fitnesses); 

gbestfit = minfit; 

gbest = X(minfitidx, :); 

for i = 1:numofdims 

    Vmax(i) = 0.2 * (Xmax(i) - Xmin(i)); 

    Vmin(i) = -Vmax(i); 

end 


for t = 1:1000 

    w = 0.9 - 0.7 * (t/numofiterations); 


    for i = 1:numofparticles 

     if(fitnesses(i) < pbestfits(i)) 

      pbestfits(i) = fitnesses(i); 

      pbests(i, :) = X(i, :); 

     end 

    end 

    for i = 1:numofparticles 

     for j = 1:numofdims 

      V(i, j) = min(max((w * V(i, j) + rand * c1 * (pbests(i, j) - X(i, j))... 

       + rand * c2 * (gbest(j) - X(i, j))), Vmin(j)), Vmax(j)); 

      X(i, j) = min(max((X(i, j) + V(i, j)), Xmin(j)), Xmax(j)); 

     end 

    end 


    fitnesses = testfunc1(X); 

    [minfit, minfitidx] = min(fitnesses); 

    if(minfit < gbestfit) 

     gbestfit = minfit; 

     gbest = X(minfitidx, :); 

    end 


    worsts(t) = max(fitnesses); 

    bests(t) = gbestfit; 

    meanfits(t) = mean(fitnesses); 

end 
+0

我有在這條線在代碼initialpop = Xmin時+(的Xmax - Xmin時)THI線的問題*蘭特(numofparticles,numofdims); – johnpag

+0

問題是什麼? – Dinesh

+0

它說錯誤使用。* 矩陣尺寸必須一致。 PSo錯誤(第35行) initialpop = Xmin +(Xmax - Xmin)。* rand(numofparticles,numofdims); – johnpag

回答

0

您可以使用ginput獲得點擊鼠標的座標:

[x,y] = ginput; 

,然後相應地定義你的窗口。

+0

對不起。 [x,y] = ginput;在哪裏放碼? – johnpag

+0

我認爲應該將它放在需要用鼠標點擊來定義點的位置。也許你在哪裏定義你的初始彈出。 – Dinesh

0

我以前的代碼非常感謝你。

我發現了一個關於PSO的新代碼,我想用鼠標單擊來定義一個窗口大小爲[min1,max1]和[min2,max2]的空間點。

然後,一個由n = 10個粒子組成的簇被初始化並搜索用戶最初設置的點。

的代碼是這樣的:

%% Initialization 
clear 
clc 
n = 50;   % Size of the swarm " no of birds " 
bird_setp = 50; % Maximum number of "birds steps" 
dim = 2;   % Dimension of the problem 

c2 =1.1;   % PSO parameter C1 
c1 = 0.12;  % PSO parameter C2 
w =0.9;   % pso momentum or inertia 
fitness=0*ones(n,bird_setp); 
             %-----------------------------% 
             % initialize the parameter % 
             %-----------------------------% 

R1 = rand(dim, n); 
R2 = rand(dim, n); 
current_fitness =0*ones(n,1); 

           %------------------------------------------------% 
           % Initializing swarm and velocities and position % 
           %------------------------------------------------% 

current_position = 10*(rand(dim, n)-.5); 
velocity = .3*randn(dim, n) ; 
local_best_position = current_position ; 


           %-------------------------------------------% 
           %  Evaluate initial population   %   
           %-------------------------------------------% 

for i = 1:n 
    current_fitness(i) = Live_fn(current_position(:,i));  

end 


local_best_fitness = current_fitness ; 
[global_best_fitness,g] = min(local_best_fitness) ; 

for i=1:n 
    globl_best_position(:,i) = local_best_position(:,g) ; 

end 
               %-------------------% 
               % VELOCITY UPDATE % 
               %-------------------% 

velocity = w *velocity + c1*(R1.*(local_best_position-current_position)) + c2*(R2.*(globl_best_position-current_position)); 

               %------------------% 
               % SWARMUPDATE % 
               %------------------% 


current_position = current_position + velocity ; 

               %------------------------% 
               % evaluate anew swarm % 
               %------------------------% 


%% Main Loop 
iter = 0 ;  % Iterations’counter 
while (iter < bird_setp) 
iter = iter + 1; 

for i = 1:n, 
current_fitness(i) = Live_fn(current_position(:,i)) ;  

end 


for i = 1 : n 
     if current_fitness(i) < local_best_fitness(i) 
      local_best_fitness(i) = current_fitness(i); 
      local_best_position(:,i) = current_position(:,i) ; 
     end 
end 


[current_global_best_fitness,g] = min(local_best_fitness); 


if current_global_best_fitness < global_best_fitness 
    global_best_fitness = current_global_best_fitness; 

    for i=1:n 
     globl_best_position(:,i) = local_best_position(:,g); 
    end 

end 


velocity = w *velocity + c1*(R1.*(local_best_position-current_position)) + c2*(R2.*(globl_best_position-current_position)); 
current_position = current_position + velocity; 




x=current_position(1,:); 
y=current_position(2,:); 

clf  
    plot(x, y , 'h') 
    axis([-5 5 -5 5]); 

pause(.2) 


end % end of while loop its mean the end of all step that the birds move it 


       [Jbest_min,I] = min(current_fitness) % minimum fitness 
       current_position(:,I) % best solution 






%