function survival(action)
%
%
% Simple MALTAB ecosystem. Try to find parameter values
% which will allow both species to survive (with reproduction
% rates higher than zero. Blue dots are preys and red circles
% predadtors. Here is the parameter info:
%
% #Prey - Initial number of preys.
%
% Rep. R - Probability of preys reproducing each turn (between 0-1).
%
% #Pred - Initial number of predators.
%
% Star. R. - Starvation rate of predators (between 0-0.98). Predators die
% when they starvation level is below 0.01. Starvation level
% decreases at a rate of (Star. R.)^k. Where k is the number of
% turns the predator has passed without eating. k resets to 0
% as soon as the predator eats a prey.
%
% Speed - How far a predator can reach its prey (>0).
%
% Rep. R- Probability of predator reproducing each turn (between 0-1). In,
% addition, predator has to have a starvation level of at least
% 0.7 in order to reproduce.
%
%
% When species reproduce, they are subject to small random fluctuations in their
% traits (i.e.: speed and starvation level). But their traits on average, should be
% equal to it's parent's trait.
%
% Time and phase plot of the population are available by cheking the check box.
% This ecosystem is not guaranteed to have a solution where both species can survive
% indefinetly with reproduction rate >0.
% Information regarding the play status will be held in
% the axis user data according to the following table:
play= 1;
stop=-1;
if nargin<1,
action='initialize';
end;
if strcmp(action,'initialize'),
figNumber=figure( ...
'Name','Survival Game', ...
'NumberTitle','off', ...
'DoubleBuffer','on', ...
'Visible','off', ...
'Color','white', ...
'BackingStore','off');
axes( ...
'Units','normalized', ...
'Position',[0.05 0.05 0.75 0.90], ...
'Visible','off', ...
'DrawMode','fast', ...
'NextPlot','add');
text(0,0,'Press the "Start" button to see the Survival demo', ...
'HorizontalAlignment','center');
axis([-1 1 -1 1]);
%===================================
% Information for all buttons
labelColor=[0.8 0.8 0.8];
yInitPos=0.90;
xPos=0.85;
btnLen=0.10;
btnWid=0.10;
% Spacing between the button and the next command's label
spacing=0.05;
%====================================
% The CONSOLE frame
frmBorder=0.02;
yPos=0.05-frmBorder;
frmPos=[xPos-frmBorder yPos btnLen+2*frmBorder 0.9+2*frmBorder];
h=uicontrol( ...
'Style','frame', ...
'Units','normalized', ...
'Position',frmPos, ...
'BackgroundColor',[0.50 0.50 0.50]);
%====================================
% The START button
btnNumber=1;
yPos=0.90-(btnNumber-1)*(btnWid+spacing);
labelStr='Start';
cmdStr='start';
callbackStr='survival(''start'');';
% Generic button information
btnPos=[xPos yPos-spacing btnLen btnWid];
startHndl=uicontrol( ...
'Style','pushbutton', ...
'Units','normalized', ...
'Position',btnPos, ...
'String',labelStr, ...
'Interruptible','on', ...
'Callback',callbackStr);
%====================================
% The STOP button
btnNumber=2;
yPos=0.90-(btnNumber-1)*(btnWid+spacing);
labelStr='Stop';
% Setting userdata to -1 (=stop) will stop the demo.
callbackStr='set(gca,''Userdata'',-1)';
% Generic button information
btnPos=[xPos yPos-spacing+0.04 btnLen btnWid];
stopHndl=uicontrol( ...
'Style','pushbutton', ...
'Units','normalized', ...
'Position',btnPos, ...
'Enable','off', ...
'String',labelStr, ...
'Callback',callbackStr);
%====================================
% The Param1 button
btnNumber=3;
yPos=0.90-(btnNumber-1)*(btnWid+spacing);
labelStr='NPrey';
% Setting userdata to -1 (=stop) will stop the demo.
callbackStr='set(gca,''Userdata'',-1)';
% Generic button information
btnPos=[xPos+0.06 yPos+0.005+0.04 btnLen/2 btnWid/2];
NPreyHndl=uicontrol( ...
'Style','edit', ...
'Units','normalized', ...
'Position',btnPos, ...
'BackgroundColor','w',...
'Enable','on', ...
'String','300', ...
'Callback',callbackStr);
%====================================
% The Param1 button
btnNumber=3;
yPos=0.90-(btnNumber-1)*(btnWid+spacing);
% Generic button information
btnPos=[xPos-0.018 yPos+0.005+0.007+0.04 btnLen/2+0.027 btnWid/4+0.01];
THandl=uicontrol( ...
'Style','text', ...
'Units','normalized', ...
'Position',btnPos, ...
'String','# Prey');
%====================================
%====================================
% The Param2 button
btnNumber=4;
yPos=0.90-(btnNumber-1)*(btnWid+spacing);
labelStr='Prey_R_Rate';
% Setting userdata to -1 (=stop) will stop the demo.
callbackStr='set(gca,''Userdata'',-1)';
% Generic button information
btnPos=[xPos+0.06 yPos+0.09+0.04 btnLen/2 btnWid/2];
Prey_R_RateHndl=uicontrol( ...
'Style','edit', ...
'Units','normalized', ...
'Position',btnPos, ...
'BackgroundColor','w',...
'Enable','on', ...
'String','0.001', ...
'Callback',callbackStr);
%====================================
%====================================
% The Param1 button
% Generic button information
btnPos=[xPos-0.018 yPos+0.09+0.007+0.04 btnLen/2+0.027 btnWid/4+0.01];
THandl=uicontrol( ...
'Style','text', ...
'Units','normalized', ...
'Position',btnPos, ...
'String','Rep. R');
%====================================
%====================================
% The Param3 button
btnNumber=5;
yPos=0.90-(btnNumber-1)*(btnWid+spacing);
labelStr='NPred';
% Setting userdata to -1 (=stop) will stop the demo.
callbackStr='set(gca,''Userdata'',-1)';
% Generic button information
btnPos=[xPos+0.06 yPos+0.175+0.04 btnLen/2 btnWid/2];
NPredHndl=uicontrol( ...
'Style','edit', ...
'Units','normalized', ...
'Position',btnPos, ...
'BackgroundColor','w',...
'Enable','on', ...
'String','1', ...
'Callback',callbackStr);
%====================================
%====================================
% The Param1 button
% Generic button information
btnPos=[xPos-0.018 yPos+0.175+0.007+0.04 btnLen/2+0.027 btnWid/4+0.01];
THandl=uicontrol( ...
'Style','text', ...
'Units','normalized', ...
'Position',btnPos, ...
'String','# Pred.');
%====================================
%====================================
% The Param4 button
btnNumber=6;
yPos=0.90-(btnNumber-1)*(btnWid+spacing);
labelStr='StarvR';
% Setting userdata to -1 (=stop) will stop the demo.
callbackStr='set(gca,''Userdata'',-1)';
% Generic button information
btnPos=[xPos+0.06 yPos+0.257+0.04 btnLen/2 btnWid/2];
StarvRHndl=uicontrol( ...
'Style','edit', ...
'Units','normalized', ...
'Position',btnPos, ...
'BackgroundColor','w',...
'Enable','on', ...
'String','0.95', ...
'Callback',callbackStr);
%====================================
%====================================
% The Param1 button
% Generic button information
btnPos=[xPos-0.018 yPos+0.257+0.007+0.04 btnLen/2+0.027 btnWid/4+0.01];
THandl=uicontrol( ...
'Style','text', ...
'Units','normalized', ...
'Position',btnPos, ...
'String','Starv. R.');
%====================================
%====================================
% The Param5 button
btnNumber=6;
yPos=0.90-(btnNumber-1)*(btnWid+spacing);
labelStr='PSpeed';
% Setting userdata to -1 (=stop) will stop the demo.
callbackStr='set(gca,''Userdata'',-1)';
% Generic button information
btnPos=[xPos+0.06 yPos+0.19+0.04 btnLen/2 btnWid/2];
PSpeedHndl=uicontrol( ...
'Style','edit', ...
'Units','normalized', ...
'Position',btnPos, ...
'BackgroundColor','w',...
'Enable','on', ...
'String','2', ...
'Callback',callbackStr);
%====================================
%====================================
% The Param1 button
% Generic button information
btnPos=[xPos-0.018 yPos+0.19+0.007+0.04 btnLen/2+0.027 btnWid/4+0.01];
THandl=uicontrol( ...
'Style','text', ...
'Units','normalized', ...
'Position',btnPos, ...
'String','Speed');
%====================================
%====================================
% The Param5 button
btnNumber=6;
yPos=0.90-(btnNumber-1)*(btnWid+spacing);
labelStr='PredR';
% Setting userdata to -1 (=stop) will stop the demo.
callbackStr='set(gca,''Userdata'',-1)';
% Generic button information
btnPos=[xPos+0.06 yPos+0.12+0.04 btnLen/2 btnWid/2];
PredRHndl=uicontrol( ...
'Style','edit', ...
'Units','normalized', ...
'Position',btnPos, ...
'BackgroundColor','w',...
'Enable','on', ...
'String','0.004', ...
'Callback',callbackStr);
%====================================
%====================================
% The Param1 button
% Generic button information
btnPos=[xPos-0.018 yPos+0.12+0.007+0.04 btnLen/2+0.027 btnWid/4+0.01];
THandl=uicontrol( ...
'Style','text', ...
'Units','normalized', ...
'Position',btnPos, ...
'String','Rep. R');
%====================================
%====================================
% The Param1 button
% Generic button information
btnPos=[xPos+0.01 yPos+0.07+0.04 btnLen/2+0.027 btnWid/4+0.01];
THandl=uicontrol( ...
'Style','text', ...
'Units','normalized', ...
'Position',btnPos, ...
'String','Plots');
%====================================
%====================================
% The Param1 button
% Generic button information
btnPos=[xPos-0.018 yPos+0.06 btnLen/2-0.005 btnWid/4+0.01];
THandl=uicontrol( ...
'Style','text', ...
'Units','normalized', ...
'Position',btnPos, ...
'String','Time');
%====================================
%====================================
% The Param1 button
% Generic button information
btnPos=[xPos+0.065 yPos+0.06 btnLen/2+0.002 btnWid/4+0.01];
THandl=uicontrol( ...
'Style','text', ...
'Units','normalized', ...
'Position',btnPos, ...
'String','Phase');
%====================================
% The Param5 button
btnNumber=6;
yPos=0.90-(btnNumber-1)*(btnWid+spacing);
labelStr='Param';
% Setting userdata to -1 (=stop) will stop the demo.
callbackStr='set(gca,''Userdata'',-1)';
% Generic button information
btnPos=[xPos+0.09 yPos+0.005 btnLen/4 btnWid/2];
TimeHndl=uicontrol( ...
'Style','checkbox', ...
'Units','normalized', ...
'Position',btnPos, ...
'Enable','on', ...
'String','0.02', ...
'Callback',callbackStr);
%====================================
%====================================
% The Param5 button
btnNumber=6;
yPos=0.90-(btnNumber-1)*(btnWid+spacing);
labelStr='Param';
% Setting userdata to -1 (=stop) will stop the demo.
callbackStr='set(gca,''Userdata'',-1)';
% Generic button information
btnPos=[xPos-0.018 yPos+0.005 btnLen/4 btnWid/2];
PhaseHndl=uicontrol( ...
'Style','checkbox', ...
'Units','normalized', ...
'Position',btnPos, ...
'Enable','on', ...
'String','0.02', ...
'Callback',callbackStr);
%====================================
%====================================
% The INFO button
labelStr='Info';
callbackStr='survival(''info'')';
infoHndl=uicontrol( ...
'Style','push', ...
'Units','normalized', ...
'Position',[xPos 0.035 btnLen 0.10], ...
'String',labelStr, ...
'Callback',callbackStr);
% Uncover the figure
hndlList=[startHndl stopHndl infoHndl NPreyHndl Prey_R_RateHndl...
NPredHndl StarvRHndl PSpeedHndl PredRHndl TimeHndl PhaseHndl ];
set(figNumber,'Visible','on', ...
'UserData',hndlList);
elseif strcmp(action,'start'),
cla;
axHndl=gca;
figNumber=gcf;
hndlList=get(figNumber,'Userdata');
startHndl=hndlList(1);
stopHndl=hndlList(2);
infoHndl=hndlList(3);
Np=str2num(get(hndlList(4),'String'));
P_rate=str2num(get(hndlList(5),'String'));
Npd=str2num(get(hndlList(6),'String'));
StarvR=str2num(get(hndlList(7),'String'));
PSpeed=str2num(get(hndlList(8),'String'));
PredR=str2num(get(hndlList(9),'String'));
pphase=get(hndlList(10),'Value');
ptime=get(hndlList(11),'Value');
set([startHndl infoHndl],'Enable','off');
set(stopHndl,'Enable','on');
% ====== Start of Demo
set(axHndl, ...
'UserData',play, ...
'DrawMode','fast', ...
'Visible','off');
m = 101;
%Start with Np prey species
preys=zeros(Np,2);
preys(:,1)=round(((m-1)/2)+ (rand(Np,1)*-(m-1) +(m-1)/2))+1;
preys(:,2)=round(((m-1)/2)+ (rand(Np,1)*-(m-1) +(m-1)/2))+1;
preys(:,3)=ones(Np,1)* P_rate; %reproduction rate
preys(:,4)=ones(Np,1)*2* PSpeed; %speed rate
p_traits=traits(Np,preys,-1);
%Start with Npd prey species
pred=zeros(Npd,5);
pred(:,1)=round(((m-1)/2)+ (rand(Npd,1)*-(m-1) +(m-1)/2))+1;
pred(:,2)=round(((m-1)/2)+ (rand(Npd,1)*-(m-1) +(m-1)/2))+1;
pred(:,3)=ones(Npd,1)*StarvR; %starvation rate
pred(:,4)=ones(Npd,1)*PSpeed; %speed
pred(:,5)=ones(Npd,1)*PredR; %reproduction rate
pd_traits=traits(Npd,pred,1);
figure(gcf);
plothandle1 = plot(preys(:,1),preys(:,2),'.', ...
'Color','blue', ...
'MarkerSize',12);
axis([0 m+1 0 m+1]);
hold on
figure(gcf);
plothandle2 = plot(pred(:,1),pred(:,2),'o', ...
'Color','red', ...
'MarkerSize',10);
axis([0 m+1 0 m+1]);
if(ptime | pphase)
data=[];t=[];
data(end+1,:)=[Np Npd];
t=[];t(end+1)=0;
end
while get(axHndl,'UserData')==play,
[Np,preys,p_traits]=live_p(Npd,pred,pd_traits,Np,preys,p_traits,m,plothandle2);
pstarve=pd_traits(:,1).^pd_traits(:,2);
[Npd,pred,pd_traits,Np,preys,p_traits]=live_pd(Npd,pred,pd_traits,preys,p_traits,Np,m,plothandle2);
% Update plot.
set(plothandle1,'xdata',preys(:,1),'ydata',preys(:,2))
drawnow
if(~isempty(pred(:,1)))
set(plothandle2,'xdata',pred(:,1),'ydata',pred(:,2),'MarkerSize',6)
drawnow
end
% Bail out if the user closed the figure.
if ~ishandle(startHndl)
return
end
if(ptime | pphase)
if(isempty(Np) & isemtpy(Npd))
data(end+1,:)=[0 0];
return
elseif (isempty(Np))
data(end+1,:)=[0 Npd];
elseif(isempty(Npd))
data(end+1,:)=[Np Npd];
else
data(end+1,:)=[Np Npd];
end
t(end+1)=t(end)+1;
end
end
if(pphase)
figure;
plot(data(:,1),data(:,2))
xlabel('Prey Population')
ylabel('Predator Population')
end
if(ptime)
figure
plot(t,data(:,1)/max(data(:,1)),t,data(:,2)/max(data(:,2)),'r')
xlabel('Time')
ylabel('Normalized Population')
end
% ====== End of Demo
set([startHndl infoHndl],'Enable','on');
set(stopHndl,'Enable','off');
elseif strcmp(action,'info');
helpwin(mfilename);
end; % if strcmp(action, ...
function characters=traits(N,pop,specie)
tr=3;
if(specie==-1)
%Select probability characters of preys
%All characters drawn from rayleigh distribution
%using x=raylrnd(pmean,1);
characters=zeros(N,1);
for i=1:N,
%fertility for individual
characters(i,1)=rand(1).*pop(i,tr);
%speed range for individual
characters(i,3)=raylrnd(pop(i,tr+1),1);
end
else
%Select probability characters of predators
characters=zeros(N,4);
for i=1:N,
%starvation rate for individual
characters(i,1)=pop(i,tr);%[randn(1).*0.001+pop(i,tr)].*pop(i,tr);
if(characters(i,1)>=1)
characters(i,1)=0.999;
end
%starvation level for individual
characters(i,2)=0;
%speed range for individual
characters(i,3)=raylrnd(pop(i,tr+1),1);
%fertility for individual
characters(i,4)=rand(1).*pop(i,tr+2);
end
end
function [Np,preys,p_traits]=live_p(Npd,pred,pd_traits,Np,preys,p_traits,m,plothandle2)
%Priorities
%1 - Reproduction
for i=1:Np
[preys(i,1:2)]=flight2(pred,pd_traits,preys(i,1:2),p_traits(i,:),Np,m,preys);
end
tr=3;
[reproduce,trash]=find((rand(Np,1)<p_traits(:,1)));
Nnew=length(reproduce);
%Prey Reproduction
% for i=1:Nnew,
if(Nnew)
x= preys(reproduce,1) + sign(randn(Nnew,1));
y= preys(reproduce,2) + sign(randn(Nnew,1));
x(x>m)=1;x(x<1)=m;
y(y>m)=1;y(y<1)=m;
del=[];
Inew=sub2ind([m m],x,y);
Iold=sub2ind([m m],preys(:,1),preys(:,2));
for i=1:Nnew,
[ind,trash]=find(Iold==Inew(i));
if(~isempty(ind))
del(end+1)=i;
end
end
if(~isempty(del))
x(del)=[];
y(del)=[];
reproduce(del)=[];
Nnew=length(x);
end
if(~isempty(x))
fert=preys(reproduce,tr)+randn(Nnew,1)*0.00001; %add mutations
fert(fert<0)=0;
mu=preys(Nnew,4);
speed=mu+randn(Nnew,1)*.1; %add mutations
speed(speed<0)=0.001;
preys(end+1:end+Nnew,:)=[x y fert speed];
p_traits(end+1:end+Nnew,:)=traits(Nnew,preys(Np+1:end,:),-1);
Np=Np+Nnew;
end
end
function [Npd,pred,pd_traits,Np,preys,p_traits]=live_pd(Npd,pred,pd_traits,preys,p_traits,Np,m,plothandle2)
persistent deads
%Priorities
if(~isempty(deads))
deads(:,3)=deads(:,3)+1;
[ind,trash]=find(deads(:,3)>50);
if(~isempty(ind))
plot(deads(ind,1),deads(ind,2),'+', ...
'Color','w', ...
'MarkerSize',6);
axis([0 m+1 0 m+1]);
deads(ind,:)=[];
end
end
%1 - eat with probability p(1-starvation level); die if starvation <0.05;
%Die
pd_traits(:,2)=pd_traits(:,2)+1;
p_starve=pd_traits(:,1).^pd_traits(:,2);
[die,trash]=find(p_starve<0.01);
if(~isempty(die))
plothandle2 = plot(pred(die,1),pred(die,2),'+', ...
'Color','red', ...
'MarkerSize',6);
axis([0 m+1 0 m+1]);
deads(end+1:end+length(die),1:3)=[pred(die,1) pred(die,2) ones(length(die),1)];
pred(die,:)=[];
pd_traits(die,:)=[];
Npd=Npd-length(die);
p_starve(die)=[];
end
action=ones(Npd,1);
if(~isempty(p_starve))
%Eat
[eat,trash]=find(rand(Npd,1) > p_starve );
if(~isempty(eat))
for i=1:length(eat)
[Pd,trait,preys,p_traits,Np]=hunt(pred(eat(i),1:2),pd_traits(eat(i),:),preys,p_traits,Np,m);
pred(eat(i),1:2)=Pd;
pd_traits(eat(i),2)=trait;
action(eat(i))=0;
end
end
if(any(action))
chance=(p_starve > 0.7 );
action=(action==chance);
[Npd,pred,pd_traits]=reproduce(pred,pd_traits,Npd,m,action);
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [Pd,trait,preys,p_traits,Np]=hunt(pred,pd_traits,preys,p_traits,Np,m)
if(Np>=1)
d=( (pred(1)-preys(:,1)).^2 + (pred(2)-preys(:,2)).^2 ).^0.5;
[d,pget] = min(d);
d=d(1);
pget=pget(1);
%Get prey if within speed range
if(d<=pd_traits(3)),
%Update position
Pd=preys(pget,1:2);
%caught prey so prey die
preys(pget,:)=[];
p_traits(pget,:)=[];
Np=Np-1;
%Starvation level re-set
trait=0;
else
%Walk towards closest prey at max speed (can't cross through borders yet)
%not using vision either
if(rand(1)>=0.5)
dx=sign(preys(pget,1)-pred(1))*pd_traits(3);
px= pred(1) +dx;
px(px>m)=pred(1);
px(px<1)=pred(1);
dy=sign(preys(pget,2)-pred(2))*(sqrt(pd_traits(3).^2 - dx.^2));
py= pred(2)+dy;
py(py>m)=pred(2);
py(py<1)=pred(2);
Pd=[px py];
trait=pd_traits(:,2);
else
dy=sign(preys(pget,2)-pred(2))*pd_traits(3);
py= pred(2) + dy;
py(py>m)=pred(2);
py(py<1)=pred(2);
dx=sign(preys(pget,1)-pred(1))*(sqrt(pd_traits(3).^2 - dy.^2));
px=pred(1)+dx;
px(px>m)=pred(1);
px(px<1)=pred(1);
Pd=[px py];
trait=pd_traits(:,2);
end
end
else
Pd=pred;
trait=pd_traits(:,2);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [Npd,pred,pd_traits]=reproduce(pred,pd_traits,Npd,m,ind)
tr=3;
[rep,trash]=find((rand(Npd,1)<pd_traits(:,4)).*ind==1);
Nnew=length(rep);
%Predator Reproduction
% for i=1:Nnew,
if(Nnew)
x= pred(rep,1) + sign(randn(Nnew,1));
y= pred(rep,2) + sign(randn(Nnew,1));
x(x>m)=1;x(x<1)=m;
y(y>m)=1;y(y<1)=m;
del=[];
Inew=sub2ind([m m],x,y);
try
Iold=sub2ind([m m],pred(:,1),pred(:,2));
catch
x=1;
end
for i=1:Nnew,
[ind,trash]=find(Iold==Inew(i));
if(~isempty(ind))
del(end+1)=i;
end
end
if(~isempty(del))
x(del)=[];
y(del)=[];
rep(del)=[];
Nnew=length(x);
end
if(~isempty(x))
starv=pred(rep,tr)+randn(Nnew,1)*0.01; %add mutations starvation rate
starv(starv>0.98)=0.98;
speed=pred(rep,tr+1)+randn(Nnew,1)*.1; %add mutations
speed(speed<0)=0.001;
fert=pred(rep,tr+2);%+randn(Nnew,1)*0.0001; %add mutations
fert(fert<0)=0;
pred(end+1:end+Nnew,:)=[x y starv speed fert];
pd_traits(end+1:end+Nnew,:)=traits(Nnew,pred(Npd+1:end,:),1);
Npd=Npd+Nnew;
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [Pd]=flight(pred,pd_traits,this,p_traits,Np,m,preys)
clust=0; %controls density of the pack
pack=0.001; %controls when to stop running from predador and form pack
Pd=this;
if(Np>=1 & ~isempty(pred))
d=( (pred(:,1)-this(1)).^2 + (pred(:,2)-this(2)).^2 ).^0.5;
dp=( (preys(:,1)-this(1)).^2 + (preys(:,2)-this(2)).^2 ).^0.5;
C=Np;
[trash,cls]=sortrows(dp(:));
if(length(cls)<C)
Mx=0;%mean(preys(cls(1:end),1));
My=0;%mean(preys(cls(1:end),2));
else
Mx=0;%mean(preys(cls(1:C),1));
My=0;%mean(preys(cls(1:C),2));
end
[d,pget] = min(d);
d=d(1);
pget=pget(1);
%Walk away closest predator at max speed
if(rand(1)>=0.5)
ds=pred(pget,1)-this(1);
stp=(2-exp(0.05*abs(ds)));
dx=sign(ds)*stp*p_traits(3);
if(stp<pack)
ds=Mx-this(1);
dx=-sign(ds)*p_traits(3);
end
px= this(1) -dx;
px(px>m)=this(1);
px(px<1)=this(1);
dy=sign(pred(pget,2)-this(2))*(sqrt(p_traits(3).^2 - dx.^2));
py= this(2)-dy;
py(py>m)=this(2);
py(py<1)=this(2);
Pd=[px py];
else
ds=pred(pget,2)-this(2);
stp=(2-exp(0.05*abs(ds)));
dy=sign(ds)*stp*p_traits(3);
if(stp<pack)
ds=My-this(2);
dy=-sign(ds)*p_traits(3);
end
py= this(2) - dy;
py(py>m)=this(2);
py(py<1)=this(2);
dx=sign(pred(pget,1)-this(1))*(sqrt(p_traits(3).^2 - dy.^2));
px=this(1)-dx;
px(px>m)=this(1);
px(px<1)=this(1);
Pd=[px py];
end
end
%Cant move if another prey is too close to the desired location
d2=( (preys(:,1)-this(1)).^2 + (preys(:,2)-this(2)).^2 ).^0.5;
[trash,me] = min(d2);
preys(me,:)=[];
if(~isempty(preys))
d2=( (preys(:,1)-this(1)).^2 + (preys(:,2)-this(2)).^2 ).^0.5;
[d2,trash] = min(d2);
if(abs(d2)<=clust)
Pd=this;
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [Pd]=flight2(pred,pd_traits,this,p_traits,Np,m,preys)
clust=0;
pack=0.01;
cM=mean(preys(:,1:2));
Pd=this;
if(Np>=1 & ~isempty(pred))
d=( (pred(:,1)-this(1)).^2 + (pred(:,2)-this(2)).^2 ).^0.5;
[d,pget] = min(d);
d=d(1);
pget=pget(1);
%Walk away closest predator at max speed
ds=pred(pget,1)-this(1);
stp=(2-exp(0.05*abs(ds)));
dx=sign(ds)*stp*p_traits(3)/2;
if(stp<pack)
ds=cM(1)-this(1);
dx=-sign(ds)*p_traits(3)/2;
end
px= this(1) -dx;
px(px>m)=this(1);
px(px<1)=this(1);
ds=pred(pget,2)-this(2);
stp=(2-exp(0.05*abs(ds)));
dy=sign(ds)*stp*p_traits(3)/2;
if(stp<pack)
ds=cM(2)-this(2);
dy=-sign(ds)*p_traits(3)/2;
end
py= this(2)-dy;
py(py>m)=this(2);
py(py<1)=this(2);
Pd=[px py];
end
%Cant move if another prey is too close to the desired location
d2=( (preys(:,1)-this(1)).^2 + (preys(:,2)-this(2)).^2 ).^0.5;
[trash,me] = min(d2);
preys(me,:)=[];
if(~isempty(preys))
d2=( (preys(:,1)-this(1)).^2 + (preys(:,2)-this(2)).^2 ).^0.5;
[d2,trash] = min(d2);
if(abs(d2)<=clust)
Pd=this;
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%