2

matlab で非常に基本的な PSO (魚の群れ) を作成するプロジェクトがあります。

私がこれまでに行ったこと:

1) 軸の寸法を設定しました。

2) ランダムな x、y 座標で 50 匹の魚の群れを作成し、それらをプロットします (群れの座標は配列に保存されます)。

3) Figure のどこかをクリックすると、サメ​​を表すクリック座標が得られます。

4) 最も遠い魚である魚の最適な x,y 位置を計算します。

ここで、残りの魚をベスト ポジションの魚に近づける必要があります。

コードは次のとおりです。

Dx=100;
Dy=100;
axis([0 Dx 0 Dy]);
N=50;
K=4;
d=min(Dx,Dy)/K;
click=ginput(1);
fish=zeros(50,4);
i=1;
while i<=N
    x= rand * Dx;
    y= rand * Dy;
    if d>=sqrt((click(1)-x)^2+(click(2)-y)^2)
         fish(i,1)=x;
         fish(i,2)=y;
         hold on;
         plot(x,y,'o'); % fishes
         i=i+1;
    end;
end;

click1=ginput(1);
bestposition=1;
i=1;
while i<=N
    position=sqrt((click1(1)-fish(i,1))^2+(click1(2)-fish(i,2))^2);
    if i==1
        max=position;
    else
        if max<position
            max=position;
            bestposition=i;
        end
    end
    i=i+1;
end

hold on;
plot(click1(1), click1(2), 'r*'); %shark
4

1 に答える 1

0

私はあなたのコードを取得し、後半を変更し、いくつかを追加しました:

Dx=100;
Dy=100;
axis([0 Dx 0 Dy]);
N=50;
K=4;
d=min(Dx,Dy)/K;
click=ginput(1);
fish=zeros(50,4);
i=1;
while i<=N
    x= rand * Dx;
    y= rand * Dy;
    if d>=sqrt((click(1)-x)^2+(click(2)-y)^2)
         fish(i,1)=x;
         fish(i,2)=y;
         hold on;
         plot(x,y,'o'); % fishes
         i=i+1;
    end;
end;

click1=ginput(1);
distances = pdist2(click1, fish(:,1:2));
[bestPosition, bestFish] = max(distances);

fish(:,3) = fish(bestFish,1) - fish(:,1);
fish(:,4) = fish(bestFish,2) - fish(:,2);
% I'm assuming this is what the other columns are for
fish(:,3:4) = normr(fish(:,3:4));
% The best fish is going away from the shark
fish(bestFish,3:4) = normr(fish(bestFish,1:2) - click1);

% Let's say the smarm moves 1 +/- .5 each move, and not exactly towards
for i0 = 1:100
    % Towards the best fish
    pause(0.05);
    fish(:,1:2) = fish(:,1:2) + (fish(:,3:4).*(rand(size(fish(:,3:4))) + 1)/10);

    fish(:,3) = fish(bestFish,1) - fish(:,1);
    fish(:,4) = fish(bestFish,2) - fish(:,2);
    % I'm assuming this is what the other columns are for
    fish(:,3:4) = normr(fish(:,3:4));
    fish(bestFish,3:4) = normr(fish(bestFish,1:2) - click1);

    cla;
    plot(fish(:,1), fish(:,2), 'o')
    plot(fish(bestFish,1), fish(bestFish,2), 'rO');
    plot(click1(1), click1(2), 'kO');
end

手順 1 ~ 4 を実行し、魚の動きを示す疑似アニメーションを作成しました。

これはあなたがやろうとしていることですか?

于 2016-01-25T22:02:51.623 に答える