-
Notifications
You must be signed in to change notification settings - Fork 0
/
lek-functions.R
165 lines (117 loc) · 3.91 KB
/
lek-functions.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
#Functions for the lek model
distance <- function(x,y)
{
sqrt(x^2 + y^2)
}
repulsion <- function (i, rr, CurrX, CurrY)
{
for (j in 1:N)
{
alldist <- distance (CurrX-CurrX[i], CurrY-CurrY[i])
rep <- 0 # A bolean variable to indicate if repulsion happens.
#indices of individuals within repulsion zone
repind <- which(alldist>0 & alldist<rr)
if(length(repind)>0){
#each repulsion direction
repDirsX <- CurrX[i] - CurrX[repind]
repDirsY <- CurrY[i] - CurrY[repind]
magrep <- sqrt(repDirsX^2 + repDirsY^2)
repDirsX <- repDirsX/magrep
repDirsY <- repDirsY/magrep
#print(distance(repDirsX^2+repDirsY^2))
netRepDirX <- sum(repDirsX)
netRepDirY <- sum(repDirsY)
magnetrep <- sqrt(netRepDirX^2 + netRepDirY^2)
netRepDirX <- netRepDirX / magnetrep
netRepDirY <- netRepDirY / magnetrep
rep = 1 #A bolean var to know if there was repulsion
}
else {
netRepDirX <- 0
netRepDirY <- 0
rep = 0 #A bolean var to indicate no repulsion
}
return(list(rep=rep,X=netRepDirX,Y=netRepDirY))
}
}
social <- function (i, rs, CurrX, CurrY, DirX, DirY, OmegaA, OmegaO)
{
for (j in 1:N)
{
alldist <- distance (CurrX-CurrX[i], CurrY-CurrY[i])
soc <- 0 # A bolean variable to indicate if social int happens.
#indices of individuals within social zone
socind <- which(alldist>0 & alldist<rs)
if(length(socind)>0){
#each attraction direction
attDirsX <- CurrX[socind] - CurrX[i]
attDirsY <- CurrY[socind] - CurrY[i]
magatt <- sqrt(attDirsX^2 + attDirsY^2)
attDirsX <- attDirsX/magatt
attDirsY <- attDirsY/magatt
netAttDirX <- sum(attDirsX)
netAttDirY <- sum(attDirsY)
magnetatt <- sqrt(netAttDirX^2 + netAttDirY^2)
netAttDirX <- netAttDirX / magnetatt
netAttDirY <- netAttDirY / magnetatt
#Alignment
alinDirX <- sum(DirX)
alinDirY <- sum(DirY)
magalin <- sqrt(alinDirX^2 + alinDirY^2)
netAlinDirX <- alinDirX / magalin
netAlinDirY <- alinDirY / magalin
#Stochasticity
#theta <- runif(1, -pi, pi)
#noiseDirX <- cos(theta)
#noiseDirY <- sin(theta)
#Social Direction
SocDirX <- OmegaA*netAttDirX + OmegaO*netAlinDirX #+ (1-OmegaA-OmegaO)*noiseDirX
SocDirY <- OmegaA*netAttDirY + OmegaO*netAlinDirY #+ (1-OmegaA-OmegaO)*noiseDirY
magsoc <- sqrt(SocDirX^2 + SocDirY^2)
netSocDirX <- SocDirX / magsoc
netSocDirY <- SocDirY / magsoc
}
else {
netSocDirX <- 0
netSocDirY <- 0
soc = 0 #A bolean var to indicate no repulsion
}
return(list(soc=soc,X=netSocDirX,Y=netSocDirY))
}
}
fidelity <- function(x, y, BreedX, BreedY)
{
DirDesX <- BreedX - CurrX
DirDesY <- BreedY - CurrY
distBreed <- sqrt(DirDesX^2+DirDesY^2)
DirDesX <- DirDesX/distBreed
DirDesY <- DirDesY/distBreed
return(list(X=DirDesX, Y=DirDesY))
}
randomizeDir <- function(dirx, diry, Omega_r)
{
N <- length(dirx)
#random angles and directions
theta <- runif(N, -pi, pi)
DirRandX <- cos(theta)
DirRandY <- sin(theta)
#weight the original direction with the random direction
DirX <- dirx + Omega_r*DirRandX
DirY <- diry + Omega_r*DirRandY
norm <- sqrt(DirX^2 +DirY^2)
DirX <- DirX/norm
DirY <- DirY/norm
return(list(X=DirX, Y=DirY))
}
copyforage <- function(i, rs, foraging, CurrX, CurrY)
{
n<-10
alldist <- distance (CurrX-CurrX[i], CurrY-CurrY[i])
#indices of individuals within social zone
socind <- which(alldist>0 & alldist<rs)
prob_foraging <- sum(foraging[socind])/n
#with a probability, switch to foraging
if (runif(1,0,1) < prob_foraging)
foraging[i] <- 1
return(foraging[i])
}