2

I've got a project, to create a very basic PSO (fish swarm) on matlab.

What I've done so far:

1) I've setted the dimensions of the axis,

2) I create a fish swarm of 50 fishes with random x,y coordinates and I plot them (the swarm coordinates are saved in an array),

3) I click somewhere on Figure and I get the click coordinates which represents the shark.

4) I calculate the best x,y position of the fish which is the most distant fish.

Now, I must force the rest fishes to move close to the best position fish.

Here's the code;

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
Jason2000
  • 21
  • 3
  • Code's always welcome, since this is a website for programming questions. See [mcve] – Adriaan Jan 25 '16 at 17:34
  • Thank you!, I've edited my post with the code included.I hope that helps – Jason2000 Jan 25 '16 at 17:39
  • What is the question? – sco1 Jan 25 '16 at 17:55
  • @excaza The swarm must move to the fish which has the best position. That's based on particle swarm optimization formulas. I can't figure out how to do this. Any ideas ? :/ – Jason2000 Jan 25 '16 at 17:59
  • What is the "best position fish"? The position that is the farthest away from the shark? Once you determine where the "best position fish" is, **how** do the fish move? Can they move only north, south, east, west or can they move diagonally? Many of us are not familiar with PSO, so a bit more details on what exactly you want to achieve would go a long way. – rayryeng Jan 25 '16 at 18:13
  • @rayryeng , here's the best explanation about PSO and about what I'm looking for ( http://www.swarmintelligence.org/tutorials.php ) Read the part where it says "3. The Algorithm". – Jason2000 Jan 25 '16 at 18:24
  • @Jason2000 can you incorporate this explanation into your post? Making potential answerers to your question divert to an off-site resource is probably not the best way to get them to answer your question... it requires more effort. – rayryeng Jan 25 '16 at 18:30

1 Answers1

0

I've taken your code, changed the last half, and added some to it:

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

I've done your steps 1-4, and then made a pseudo animation showing the fish moving.

Is this kinda what you are trying to do?

Lui
  • 403
  • 2
  • 15