-
Notifications
You must be signed in to change notification settings - Fork 0
/
Figure2.R
209 lines (157 loc) · 5.01 KB
/
Figure2.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
source('lek-functions.R')
library(ggplot2)
library(ggdark)
library(scico)
library(gganimate)
library(ggimage)
#Declare array to store CurrLocation, NewLocation, DirMovement of individuals
#Parameters
N <- 20 #number of male blackbuck
L <- 5 #size of the lek
T <- 100 #time duration for simulations.
dt <- 0.1 #time step of integration
Omega_r <- 0.25
rr <- 2 #repulsion radius
s0 <- 2
s <- rep(s0,N) #speed
d0 <- 5
#Initialise blackbuck locations
CurrX <- runif(N,-2*L, 2*L)
CurrY <- runif(N,-2*L, 2*L)
NewX <- runif(N,-2*L, 2*L)
NewY <- runif(N,-2*L, 2*L)
theta <- runif(N, -pi, pi)
DirX <- cos(theta)
DirY <- sin(theta)
#Central breeding ground
BreedX <- 5
BreedY <- 5
distBreedOld <- rep(1000,N)
plot(CurrX, CurrY)
#Initiate attraction to the breeding centre -- origin.
for (t in 1:T)
{
#Attraction to the breeding ground
desdir <- fidelity(CurrX, CurrY, BreedX, BreedY)
#Randomize the directions
ranDirs <- randomizeDir(desdir$X, desdir$Y, Omega_r)
for (i in 1:N)
{
#Check Repulsion
netRepDir <- repulsion(i, rr, CurrX, CurrY)
if (netRepDir$rep > 0) { #if repulsion, just repel
NewX[i] <- CurrX[i] + s[i]*dt*netRepDir$X
NewY[i] <- CurrY[i] + s[i]*dt*netRepDir$Y
}
else { #If no repulision, move as planned.
NewX[i] <- CurrX[i] + s[i]*dt*ranDirs$X[i]
NewY[i] <- CurrY[i] + s[i]*dt*ranDirs$Y[i]
}
}
CurrX <- NewX
CurrY <- NewY
print(t)
plen <- 12
plot(CurrX, CurrY,xlim = c(-plen,plen),ylim=c(-plen,plen))
Sys.sleep(0.1)
}
#Now implement departure.
bbimage = "/home/arathore/PhD/Analysis/4_Leks/LekPersprctivePaper/Simulations/bb.jpg"
gimage = "/home/arathore/PhD/Analysis/4_Leks/LekPersprctivePaper/Simulations/grass.jpg"
glek = "/home/arathore/PhD/Analysis/4_Leks/LekPersprctivePaper/Simulations/lek.png"
#Food is located at (10,0)
FoodX <- -10
FoodY <- -10
Tf <- 1000
foraging <- rep(0,N) #a bolean variation to know if an individual is foraging
total_foragers <- rep(0,Tf)
Tw <- 250 #Minimum waittime to switch to foraging state.
fstatus <- rep(0,N) #foraging status
pf <- 0.001
rs <-rr+0.3 #rs <- 0 means no social copying of foraging status. 1 means social. int.
foraging[ceiling(runif(1,0,1)*N)] <- 1
df=data.frame(CurrX,CurrY,NewX,NewY,ts=rep(t,N),id=1:N,foraging)
for (t in 1:Tf) {
for (i in 1:N){
# if the current state is non-foraging and if time since last foraging state > Tw.
if (foraging[i]==0 && fstatus[i] == 0) {
#individual randomly switches to the foraging state.
if (runif(1,0,1) < pf ) {
foraging[i] <- 1
fstatus[i] <- 1
print(t)
}
else if(rs!=0) { #OR copy foraging status of neighbours
foraging[i] <- copyforage(i, rs, foraging, CurrX, CurrY)
if (foraging[i]==1)
fstatus[i] <- 1
}
}
netRepDir <- repulsion(i, rr, CurrX, CurrY)
#if repulsion, just repel
#repulsion works only in non-foraging state.
if (netRepDir$rep > 0 && foraging[i]==0) {
NewX[i] <- CurrX[i] + s[i]*dt*netRepDir$X
NewY[i] <- CurrY[i] + s[i]*dt*netRepDir$Y
}
else { #If no repulsion, move depending on foraging or not.
#if foraging
if (foraging[i]==1) {
attx <- FoodX
atty <- FoodY
}
else #if not foraging
{
attx <- BreedX
atty <- BreedY
}
#Attraction to the breeding ground
desdir <- fidelity(CurrX[i], CurrY[i], attx, atty)
#Randomize the directions
ranDirs <- randomizeDir(desdir$X, desdir$Y, Omega_r)
NewX[i] <- CurrX[i] + s[i]*dt*ranDirs$X[i]
NewY[i] <- CurrY[i] + s[i]*dt*ranDirs$Y[i]
}
if (foraging[i]==1 && distance(CurrX[i]-FoodX,CurrY[i]-FoodY) < rr) {
foraging[i] = 0
print(t)
print(i)
}
}
CurrX <- NewX
CurrY <- NewY
print(t)
plen <- 12
# par(mfrow=c(1,2))
# plot(CurrX, CurrY,xlim = c(-plen,plen),ylim=c(-plen,plen))
#
# for (i in 1:N) {
# if (fstatus[i]==1 && foraging[i]==1)
# points(CurrX[i],CurrY[i],col='red',pch=0)
# else if (fstatus[i]==1 && foraging[i]==0)
# points(CurrX[i],CurrY[i],col='green',pch=0)
# }
total_foragers[t] <- sum(foraging)
#plot(1:t,total_foragers[1:t],xlim = c(1,T),ylim=c(0,N),pch=1,col='blue')
#plot(1:t,total_foragers[1:t],xlim = c(1,t),ylim=c(0,N),pch=1,col='blue',
# ylab='Number of foragers', xlab='Time')
#Sys.sleep(0.1)
df1=data.frame(CurrX,CurrY,NewX,NewY,ts=rep(t,N),id=1:N,foraging)
df=rbind(df,df1)
}
anim <- ggplot(
df,
aes(x = CurrX, y=CurrY)
) +
geom_image(aes(image = bbimage), size = 0.04) +
geom_point(aes(colour = ifelse(foraging==1,"red","white"),x=CurrX,y=CurrY+1),
size = 6,shape="^") +
scale_color_identity()+
geom_image(aes(image = gimage,x=FoodX,y=FoodY), size = 0.15) +
xlim(-plen,plen)+
ylim(-plen,plen)+
transition_time(ts)+
theme_bw() +
theme(legend.position="none")
animate(anim, fps = 5, duration = 30)
plot(1:t,total_foragers[1:t],ylim=c(0,N),pch=23,col='orange2',bg="cyan",lwd=2,cex=2)