forked from Thiemann96/Geosimulation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
57 lines (46 loc) · 2.19 KB
/
main.py
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
from pcraster import *
from pcraster.framework import *
class MyFirstModel(DynamicModel):
def __init__(self):
DynamicModel.__init__(self)
setclone("main.map")
def initial(self):
# Creation if initial predator & prey map
# Prey:
self.prey = uniform(1)
# Set probability for prey to 10 %
self.prey = self.prey<0.10
# Save as map
self.report(self.prey,"preyMap")
# Predator:
self.predator = uniform(1)
# Set probability for predator to 10 %
self.predator = self.predator<0.10
# Save as map
self.report(self.predator,"predatorMap")
print("Finished building maps for prey&predator distribution")
def dynamic(self):
# Determine cells which were occupied by both, prey and predator in last timestep
both = pcrand(self.prey,self.predator)
# Determine cells which are occupied by prey only in last timestep
alivePrey = pcrand(self.prey, pcrnot(self.predator))
# Determine the number of cells with prey only in Von Neumann neighborhood of each cell in last timestep
preyBirth = window4total(scalar(alivePrey))
# Determine whether a cell had any cells with prey only in Von Neumann neighborhood or was prey only in the last timestep
preyBirth = pcror(preyBirth>0.0, alivePrey)
# All of the cells in preyBirth become prey in the current timestep
self.prey = preyBirth
# Determine the number of cells with both, prey and predator in Von Neumann neighborhood of each cell in last timestep
predatorBirth = window4total(scalar(both))
# Determine whether a cell has any cells with both, prey and predator in Von Neumann neighborhood or is itself both prey and predator in last timestep
predatorBirth = pcror(predatorBirth>0.0, both)
# All of the cells in predatorBirth become predator in the current timestep
self.predator = predatorBirth
# Save prey and predator distribution of current timestep each as a map
self.report(self.predator,'predator')
self.report(self.prey,"prey")
# Run the model 100 times
nrOfTimeSteps=100
myModel = MyFirstModel()
dynamicModel = DynamicFramework(myModel,nrOfTimeSteps)
dynamicModel.run()