-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathradis-sim.py
More file actions
335 lines (272 loc) · 12.7 KB
/
radis-sim.py
File metadata and controls
335 lines (272 loc) · 12.7 KB
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# -*- coding: utf-8 -*-
import sys
import time
import numpy as np
from numpy.random import default_rng
rng = default_rng()
class PredatorPopulation:
def __init__(self, xpos, ypos, s, i):
self.x = xpos
self.y = ypos
self.species = s
self.id = i
# returns list of the size of the individualsList with 0 when species
# i doesn't coocur with species j and 1 when it does. coocurrence is
# not symmetric as species can have different interaction radius
def getCoOcurrence(self, preyPopulationList):
coOcurrence=np.zeros(len(preyPopulationList))
for ix,preyPopulation in enumerate(preyPopulationList):
if preyPopulation.x == self.x and preyPopulation.y == self.y:
coOcurrence[ix]=1
return coOcurrence
# given a species coorcurrence list and a list of prefered preys, the
# function returns 1 if any of the coocurring species is in the prey list
def getNormalizedPreyWeights(self, param, preyPopulationList):
# first determine the possible interactions and how much the predator
# benefits from them
coOcurrentPreys = self.getCoOcurrence(preyPopulationList)
dietPreferences=self.species.preys
preyWeight=np.zeros(len(preyPopulationList))
dietPreferencesPop = np.zeros(len(preyPopulationList))
for i in range(len(dietPreferencesPop)):
sid=preyPopulationList[i].species.id
dietPreferencesPop[i]=dietPreferences[sid]
for i in range(len(preyWeight)):
preyWeight[i]=coOcurrentPreys[i]*(dietPreferencesPop[i] - (dietPreferencesPop[i]-0.5)*param)
maxPotentialWeight=1-0.5*param
preyWeightNormal=preyWeight/maxPotentialWeight
return preyWeightNormal
def getDietSurvival(self,param,preyPopulationList):
# get the survival probablility
preyWeightNormal = self.getNormalizedPreyWeights(param,preyPopulationList)
dietSurvival = np.max(preyWeightNormal)
return dietSurvival
def survival_probability(self, param, preyPopulationList, dl):
meanEnvironment=self.species.ymean
stdEnvironment=self.species.ystd
# probability of survival given the environment preferences
environmentSuitability = (gaussianFunction((self.y+1)*dl,1,meanEnvironment,stdEnvironment)-gaussianFunction((self.y)*dl,1,meanEnvironment,stdEnvironment))*0.5
dietSurvival = self.getDietSurvival(param,preyPopulationList)
# composed probability where param controls the relative importance of the
# environment versus the diet on survival
#return dietSurvival*(1 - (1-param)*(1-environmentSuitability) )
return dietSurvival*(1-param*(1-environmentSuitability))
class PreyPopulation:
def __init__(self, xpos, ypos, s, i):
self.x = xpos
self.y = ypos
self.species = s
self.id = i
def survival_probability(self,dl):
meanEnvironment=self.species.ymean
stdEnvironment=self.species.ystd
# probability of survival given the environment preferences
environmentSurvival = (gaussianFunction((self.y+1)*dl,1,meanEnvironment,stdEnvironment)-gaussianFunction((self.y)*dl,1,meanEnvironment,stdEnvironment))*0.5
return environmentSurvival
class Predator:
def __init__(self, mean, std, list, i):
self.ymean = mean
self.ystd = std
self.preys = list
self.id = i
class Prey:
def __init__(self, mean, std, i):
self.ymean = mean
self.ystd = std
self.id = i
# normal distribution for environment suitability
def gaussianFunction(x,A,mean,std):
return A*np.exp(-0.5*(x-mean)*(x-mean)/std/std)
def getNeighbours(x,y,xCells,yCells):
north=[x,y+1]
east=[x+1,y]
south=[x,y-1]
west=[x-1,y]
if y+1 > yCells-1:
north=[x,y-1] # reflective border conditions
if x+1 > xCells-1:
east=[0,y] # periodic border conditions
if y-1 < 0:
south=[x,y+1] # reflective
if x-1 < 0:
west=[xCells-1,y] # periodic
return [north, east, south, west]
###############################################################################
# end of classes and functions declaration
###############################################################################
# model Parameters
Time=np.int(sys.argv[1])
xCells=np.int(sys.argv[2])
yCells=np.int(sys.argv[3])
dl=np.float(sys.argv[4])
param=np.float(sys.argv[5])
connectance=np.float(sys.argv[6])
nPredators=np.int(sys.argv[7])
nPreys=np.int(sys.argv[8])
nPredatorPopulations=np.int(sys.argv[9])
nPreyPopulations=np.int(sys.argv[10])
maxstd=np.double(sys.argv[11])
ncells=xCells*yCells
filename_prey= "DATA_RD_PREY_T_"+str(Time)+"_Nx_"+str(xCells)+"_Ny_"+str(yCells)+"_dl_"+str(dl)+"_param_"+str(param)+"_C_"+str(connectance)+"_nPred_"+str(nPredators)+"_nPrey"+str(nPreys)+"_predPop_"+str(nPredatorPopulations)+"_preyPop_"+str(nPreyPopulations)+"_SD_"+str(maxstd)+".csv"
filename_predator= "DATA_RD_PREDATOR_T_"+str(Time)+"_Nx_"+str(xCells)+"_Ny_"+str(yCells)+"_dl_"+str(dl)+"_param_"+str(param)+"_C_"+str(connectance)+"_nPred_"+str(nPredators)+"_nPrey"+str(nPreys)+"_predPop_"+str(nPredatorPopulations)+"_preyPop_"+str(nPreyPopulations)+"_SD_"+str(maxstd)+".csv"
###############################################################################
# create interaction matrix. rows are predators, cols are preys, a 1 means
# interaction and 0 no interaction.
interactionMatrix=np.zeros((nPredators,nPreys))
for i in range(nPredators):
for j in range(nPreys):
if rng.uniform()<connectance:
interactionMatrix[i,j]=1
# predator list initialization
predatorList=[]
for i in range(nPredators):
mean = rng.uniform(0,yCells*dl)
std = rng.uniform(0,yCells*dl)
dietPreferences=interactionMatrix[i,:]
predatorList.append( Predator(mean,std,dietPreferences,i) )
# prey list initialization
preyList=[]
for i in range(nPreys):
mean = rng.uniform(0,yCells*dl)
std = rng.uniform(0,yCells*dl)
preyList.append( Prey(mean,std,i) )
# predator's population initialization
predatorPopulationList=[]
for i in range(nPredatorPopulations):
xpos = rng.integers(0,xCells)
ypos = rng.integers(0,yCells)
s = predatorList[rng.integers(0,nPredators)]
predatorPopulationList.append( PredatorPopulation(xpos,ypos,s,i) )
# prey's population initialization
preyPopulationList=[]
for i in range(nPreyPopulations):
xpos = rng.integers(0,xCells)
ypos = rng.integers(0,yCells)
s = preyList[rng.integers(0,nPreys)]
preyPopulationList.append( PreyPopulation(xpos,ypos,s,i) )
###############################################################################
# begin of simulation
t0= time.time()
for t in range(Time):
for i in range(nPreyPopulations):
if rng.uniform() < (1-preyPopulationList[i].survival_probability(dl)):
xpos = preyPopulationList[i].x
ypos = preyPopulationList[i].y
neighbourList = getNeighbours(xpos,ypos,xCells,yCells)
#making a list with all the species present in the neighbourhood
candidateReplacementSpecies=[]
# think of replacement by choosing first the random neighbour and then the species
# by randomly choosing a population. this takes into account the density of a given species
for preyPop in preyPopulationList:
pos = [preyPop.x,preyPop.y]
for neighbour in neighbourList:
if pos==neighbour or pos==[xpos,ypos]:
candidateReplacementSpecies.append(preyPop.species.id)
break
sid = candidateReplacementSpecies[rng.integers(0,len(candidateReplacementSpecies))]
s = preyList[sid]
preyPopulationList[i]=PreyPopulation(xpos,ypos,s,i)
for i in range(nPredatorPopulations):
if rng.uniform() < (1 - predatorPopulationList[i].survival_probability(param, preyPopulationList, dl)):
xpos = predatorPopulationList[i].x
ypos = predatorPopulationList[i].y
#making a list with all the species present in the neighbourhood
candidateReplacementSpecies=[]
for predatorPop in predatorPopulationList:
pos = [predatorPop.x,predatorPop.y]
for neighbour in neighbourList:
if pos==neighbour or pos==[xpos,ypos]:
candidateReplacementSpecies.append(predatorPop.species.id)
break
sid = candidateReplacementSpecies[rng.integers(0,len(candidateReplacementSpecies))]
s= predatorList[sid]
predatorPopulationList[i]=PredatorPopulation(xpos,ypos,s,i)
##############################################################################
# RESULTS ANALYSIS
##############################################################################
# returns the predators located in each cell
predatorPopByCell=np.zeros((xCells,yCells,nPredatorPopulations))
for predatorPopulation in predatorPopulationList:
x=predatorPopulation.x
y=predatorPopulation.y
ix=predatorPopulation.id
predatorPopByCell[x,y,ix]=1
# returns the preys located in each cell
preyPopByCell=np.zeros((xCells,yCells,nPreyPopulations))
for preyPopulation in preyPopulationList:
x=preyPopulation.x
y=preyPopulation.y
ix=preyPopulation.id
preyPopByCell[x,y,ix]=1
# print("Predator and prey populations per cell")
# print(predatorPopByCell[0,0,:])
# print(preyPopByCell[0,0,:])
# print("\n")
# initialize a list of the interaction matrixes
interactionMatrixList=np.zeros((ncells,nPredators,nPreys))
# store the interaction matrix for each cell
for x in range(xCells):
for y in range(yCells):
interactionMatrix=np.zeros(0)
for ix,predatorPresence in enumerate(predatorPopByCell[x,y,:]):
if predatorPresence == 1:
cellid = x+xCells*y
normalizedPreyWeights = predatorPopulationList[ix].getNormalizedPreyWeights(param,preyPopulationList)
# print("Predator Population "+ str(ix) +" is of species")
# print(predatorPopulationList[ix].species.id)
# print("Normalized prey weights for the predator population "+str(ix))
# print(normalizedPreyWeights)
if np.any(normalizedPreyWeights>0):
normalizedPreyWeights/=np.max(normalizedPreyWeights)
populationInteractionVector=normalizedPreyWeights
populationInteractionVector[populationInteractionVector<1]=0
# print("Interaction Vector for the predator population "+ str(ix))
# print(populationInteractionVector)
for jx,populationInteraction in enumerate(populationInteractionVector):
if populationInteraction == 1:
prey_sid=preyPopulationList[jx].species.id
predator_sid = predatorPopulationList[ix].species.id
interactionMatrixList[cellid,predator_sid,prey_sid]=1
# print("\n")
# print(interactionMatrixList[0,:,:])
# print("\n")
#global interaction matrix
globalInteractionMatrix = np.zeros((nPredators,nPreys))
for ix in range(ncells):
for jx in range(nPredators):
for kx in range(nPreys):
if interactionMatrixList[ix,jx,kx]==1:
globalInteractionMatrix[jx,kx]=1
# number of preys by predator
predatorDiet = np.transpose([np.sum(globalInteractionMatrix,axis=1)])
# print(predatorDiet)
# number of predators by prey
preyDiet = np.transpose([np.sum(globalInteractionMatrix,axis=0)])
# print(preyDiet)
# calculate the fraction of the total area occupied by each species
predatorOccupancyMatrix=np.zeros((ncells,nPredators))
for x in range(xCells):
for y in range(yCells):
for ix,predatorPresence in enumerate(predatorPopByCell[x,y,:]):
if predatorPresence == 1:
cellid = x+xCells*y
sid = predatorPopulationList[ix].species.id
predatorOccupancyMatrix[cellid,sid]=1
preyOccupancyMatrix=np.zeros((ncells,nPreys))
for x in range(xCells):
for y in range(yCells):
for ix,preyPresence in enumerate(preyPopByCell[x,y,:]):
if preyPresence == 1:
cellid = x+xCells*y
sid = preyPopulationList[ix].species.id
preyOccupancyMatrix[cellid,sid]=1
preyOccupancy = np.transpose([np.sum(preyOccupancyMatrix,axis=0)])
predatorOccupancy = np.transpose([np.sum(predatorOccupancyMatrix,axis=0)])
# print(predatorOccupancy)
# print(preyOccupancy)
saveArrayPredator=np.concatenate( (predatorDiet ,predatorOccupancy) , axis=1)
saveArrayPrey=np.concatenate((preyDiet,preyOccupancy),axis=1)
np.savetxt(filename_prey,saveArrayPrey)
np.savetxt(filename_predator,saveArrayPredator)
t1 = time.time()
print("Time elapsed: ", t1 - t0)