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