breed [randomH] ;; create two breeds of turtle to compare random walks vs the 'real' hedghogs in any landscape
breed [hedgehog]
breed [arable]
breed [ pasture]
breed [ garden]
breed [ wood]
breed [ water]
breed [ urban]
breed [ road]
turtles-own [energy Harable Hpasture Hwood Hgarden Hwater Hurban Hroad] ;;energy relates to the cost of the path taken by the hedgehogs
patches-own [habitat cost visit] ;; habitat assigns a land-cover type to each patch colour
;;includes the cost of the habitat as a patch feature - allowing hedgehogs to search for low cost patches
globals [timestep time hour]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; setup patches and turtles ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to setup
clear-all
set time 0
set hour 0
set timestep 1
setup-patches
setup-turtles
end
to setup-turtles ;; creates 10 of each breed, assigning the paw footprint for an icon. create-randomH Number-of-Random-Hedgehogs-red ;; the random hedgehogs are red, the 'real' are white. create-hedgehog Number-of-Non-Random-Hedgehogs-white
ask turtles [set shape "hedgehog paw"] ;;this is the footprint other renamed to hedgehog - the closest icon resembling a hedgehog! ask turtles [
if breed = randomH [set color red]
if breed = hedgehog [set color white] ;;need to find a colour that shows on map and on the plot ]
ask turtles [setxy random-xcor random-ycor] ;; sets the newly created turtles at random locations on the map end
to setup-patches
colour-turtles
ask turtles [repeat 100 [expand-habitats] ] ;; proportions are supposed to be approx pasture/arable - 30%, garden/urban - 10%, wood - 13%, roads - 5%, water - 2% ask patches [
ifelse show-cellcosts? [
set plabel cost] [
set plabel ""]
ifelse show-habitat? [
set plabel habitat] [
set plabel ""]
]
ask patches [set visit 5]
ask patches [
if pcolor = yellow [set cost 150 set habitat "arable"]
if pcolor = green [set cost 10 set habitat "pasture"]
if pcolor = brown [set cost 1 set habitat "wood"]
if pcolor = lime [set cost 5 set habitat "garden"]
if pcolor = grey [set cost 15 set habitat "urban"]
if pcolor = blue [set cost 300 set habitat "water"]
if pcolor = black + 2 [set cost 150 set habitat "road"]
]
ask patches [
if random 100 <= fragmentation-of-habitats
[swap self one-of patches]
]
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; go statement ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to go ;;need to include all procedures here if they are required to run during the model run
set time time + 1
set hour (time * timestep)
cost-path
if hour mod 24 >= 6 and hour mod 24 < 18 [
move-randomH
ifelse day-and-night-mode? [
repeat 10 [move-hedgehog-day]][
move-hedgehog]
]
if hour mod 24 >= 18 or hour mod 24 < 6 [
move-randomH
ifelse day-and-night-mode? [
repeat 10 [move-hedgehog-night]][ ;; this increments tick counter by one everytime the model completes a single turn of each procedure
move-hedgehog]
]
do-plots
if days = Number-of-days [stop] ;; allows output to run faster as it updates after every tick onlyend
;;;;;;;;;;; do reports on interface for length of time passed in the model ;;;;;;;;;;;;;;;;;
to-report days
report hour / 24
end
to-report hours
report hour - (floor days * 24)
end
;;;;;;;;;;;;;; defines how to create the landscape ;;;;;;;;;;;;;;;;;;;;;;;;;;;
to colour-turtles ;; creates 7 turtles, one for each of the habitats
create-arable 1
create-pasture 1
create-wood 1
create-garden 1
create-water 1
create-urban 1
create-road 1
ask turtles [
set Harable 312
set Hpasture 313
set Hwood 173
set Hgarden 107
set Hwater 19
set Hurban 107
set Hroad 51]
ask turtles [
if breed = arable [set color yellow]
if breed = pasture [set color green]
if breed = wood [set color brown]
if breed = garden [set color lime]
if breed = water [set color blue]
if breed = urban [set color grey]
if breed = road [set color black + 2]
]
ask turtles [
setxy random-xcor random-ycor
hide-turtle
move-to patch-here
set pcolor color] ;; puts turtles in a random place in landscape and sets patch color to turtle colourend
to expand-habitats
ifelse (Harable = 0) and (Hpasture = 0) and (Hwood = 0) and (Hgarden = 0) and (Hwater = 0) and (Hurban = 0) and (Hroad = 0) [stop] [
expand-habitatarable
expand-habitatpasture
expand-habitatwood
expand-habitatgarden
expand-habitatwater
expand-habitaturban
expand-habitatroad
]
end
;;;;;;;;;;;;;functions for 'building' habitats from scratch ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; one run of each function only creates 8 cells. The functions need to be repeated in order to create the appropriate number of cellsto expand-habitatarable
ask arable [
if all? patches [pcolor != black] [stop]
if Harable = 0 [stop]
ifelse all? neighbors [pcolor != black]
[move-to one-of patches with [pcolor = black]
set pcolor color
reduce-colours]
[ifelse all? neighbors4 [pcolor != black] [
move-to one-of neighbors with [pcolor = black]
set pcolor color
reduce-colours
][
move-to one-of neighbors4 with [pcolor = black]
set pcolor color
reduce-colours]
]
]
end
to expand-habitatpasture
ask pasture [
if all? patches [pcolor != black] [stop]
if Hpasture = 0 [stop]
ifelse all? neighbors [pcolor != black]
[move-to one-of patches with [pcolor = black]
set pcolor color
reduce-colours]
[ifelse all? neighbors4 [pcolor != black] [
move-to one-of neighbors with [pcolor = black]
set pcolor color
reduce-colours
][
move-to one-of neighbors4 with [pcolor = black]
set pcolor color
reduce-colours]
]
]
end
to expand-habitatwood
ask wood [
if all? patches [pcolor != black] [stop]
if Hwood = 0 [stop]
ifelse all? neighbors [pcolor != black]
[move-to one-of patches with [pcolor = black]
set pcolor color
reduce-colours]
[ifelse all? neighbors4 [pcolor != black] [
move-to one-of neighbors with [pcolor = black]
set pcolor color
reduce-colours
][
move-to one-of neighbors4 with [pcolor = black]
set pcolor color
reduce-colours]
]
]
end
to expand-habitatgarden
ask garden [
if all? patches [pcolor != black] [stop]
if Hgarden = 0 [stop]
ifelse all? neighbors [pcolor != black]
[move-to one-of patches with [pcolor = black]
set pcolor color
reduce-colours]
[ifelse all? neighbors4 [pcolor != black] [
move-to one-of neighbors with [pcolor = black]
set pcolor color
reduce-colours
][
move-to one-of neighbors4 with [pcolor = black]
set pcolor color
reduce-colours]
]
]
end
to expand-habitatwater
ask water [
if Hwater = 0 [stop]
if all? patches [pcolor != black] [stop]
ifelse all? neighbors [pcolor != black]
[move-to one-of patches with [pcolor = black]
set pcolor color
reduce-colours]
[ifelse all? neighbors4 [pcolor != black] [
move-to one-of neighbors with [pcolor = black]
set pcolor color
reduce-colours
][
move-to one-of neighbors4 with [pcolor = black]
set pcolor color
reduce-colours]
]
]
end
to expand-habitaturban
ask urban [
if all? patches [pcolor != black] [stop]
if Hurban = 0 [stop]
ifelse all? neighbors [pcolor != black]
[move-to one-of patches with [pcolor = black]
set pcolor color
reduce-colours]
[ifelse all? neighbors4 [pcolor != black] [
move-to one-of neighbors with [pcolor = black]
set pcolor color
reduce-colours
][
move-to one-of neighbors4 with [pcolor = black]
set pcolor color
reduce-colours]
]
]
end
to expand-habitatroad
ask road [
if all? patches [pcolor != black] [stop]
if Hroad = 0 [stop]
ifelse all? neighbors [pcolor != black]
[move-to one-of patches with [pcolor = black]
set pcolor color
reduce-colours]
[ifelse all? neighbors4 [pcolor != black] [
move-to one-of neighbors with [pcolor = black]
set pcolor color
reduce-colours
][
move-to one-of neighbors4 with [pcolor = black]
set pcolor color
reduce-colours]
]
]
end
;;;; function to reduce number of habitat cells by one each time a new cell is created ;;;;;
to reduce-colours
let tempcolor 0
set tempcolor [pcolor] of patch-here
if tempcolor = yellow [set Harable Harable - 1]
if tempcolor = green [set Hpasture Hpasture - 1]
if tempcolor = brown [set Hwood Hwood - 1]
if tempcolor = lime [set Hgarden Hgarden - 1]
if tempcolor = blue [set Hwater Hwater - 1]
if tempcolor = grey [set Hurban Hurban - 1]
if tempcolor = black + 2 [set Hroad Hroad - 1]
end
;;;;;;;;;;;;; don't allow a turtle to re-visit a cell it has visited recently ;;;;;;;;;;;;;
to swap [original new]
let oldcolor 0
let newcolor 0
ask original [set oldcolor pcolor]
ask new [set newcolor pcolor]
ask original [set pcolor newcolor]
ask new [set pcolor oldcolor]
end
;;;;;;;;;;;;;;;;;;;;;;;; move turtles ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to move-randomH
ask randomH [pen-down]
ask randomH [right random 360 forward 1]
end
to move-hedgehog
ask hedgehog[pen-down]
ask patches [set visit visit - 1]
ask hedgehog [
face one-of neighbors with-min [cost]
forward 1]
end
to move-hedgehog-day ;; if patch is not wood, move to lowest cost cell until you reachwood ask hedgehog [pen-down] ;; ensure hedgehogs reach desired colour before the function finishes ask patches [set visit visit - 1]
ask hedgehog [
ifelse pcolor = brown
[stop]
[face one-of neighbors with-min [cost]
forward 1]
]
end
to move-hedgehog-night ;;if patch is not preferred eating habitat, move to the lowest cost cell until you reach a preferred eating habitat ask hedgehog [pen-down]
ask patches [set visit visit - 1]
ask hedgehog [
if (pcolor = green) or (pcolor = lime) [stop]
ifelse any? neighbors with [pcolor = lime] [
face one-of neighbors with [pcolor = lime]
forward 1][
ifelse any? neighbors with [pcolor = green] [
face one-of neighbors with [pcolor = green]
forward 1][
face one-of neighbors with-min [cost]
forward 1]
]
]
end
;;;;;;;;;;;;;;;evaluate the cost of each path taken by a turtle ;;;;;;;;;;;;;;;;;;;;;
to cost-path ;;ok this is the cost of the path taken by the hedgehogs so far in the model....
ask turtles [
if pcolor = green [set energy energy + 10]
if pcolor = grey [set energy energy + 15]
if pcolor = brown [set energy energy + 1]
if pcolor = lime [set energy energy + 5]
if pcolor = black + 2 [set energy energy + 150]
if pcolor = blue [set energy energy + 300]
]
ask turtles [
ifelse show-costpaths?
[set label energy]
[set label ""]
]
end
;;;;;;;;;;draw the plots for mean path cost for both turtle breeds ;;;;;;;;;;;;;;;;
to do-plots ;;this sets up and runs the plot box in the interface screen. titles need to match EXACTLY in order for it to work.
set-current-plot "Average Total Path Cost for Random and Non-Random Hedgehogs"
set-current-plot-pen "Random Hedgehogs"
plot mean [energy] of randomH
set-current-plot-pen "Non-Random Hedgehogs"
plot mean [energy] of hedgehog
end
My PhD research on spatially explicit modelling of habitat permeability for mammalian wildlife
Showing posts with label example code. Show all posts
Showing posts with label example code. Show all posts
13.6.11
Netlogo models
Ok, so now I've tested some least-cost models against empirical data of hedgehog dispersal I can use the best-fit least cost model as the basis for some exploratory agent-based simulations.
Luckily there was one clear winner in the least-cost model analysis so I can be fairly confident that this provides a good fit to the way that hedgehoge actually view and move around the landscape. With this in mind I simplified things abit by reducing the number of habitat types from 22, from the original GIS maps, to 7 in the simulations. Whilst this sounds dramatic, it was logical and simply meant lumping all water features together into a single habitat, all urban features together etc. Most of the habitats that we lumped together were given the same cost value anyway, but to clarify, this is how I simplified the landscape:
The proportions of each landscape were for the most part arbitrary, although were estimated from the original GIS maps.
So.... Netlogo. The building of the model was fairly straightforward. Netlogo is brilliant at making it easy to get something up and running fairly quickly. Turtles represent agents and the simplified language and syntax of netlogo makes it really easy to get the turtles to move around in a random direction at every time step.
Of course, by using the least-cost model as a base, turtles were amended so that they searched the cells in their immediate neighbourhood and moved to the cell of the least cost. Sample code is below:
to move-hedgehog
ask hedgehog[pen-down]
ask patches [set visit visit - 1]
ask hedgehog [
face one-of neighbors with-min [cost]
forward 1]
end
The biggest problem I faced with the enitre netlogo model was how to construct the landscape. It is easy to generate different habitats by stating which cells to be of which colour(habitat). What is much harder to get a landscape to develop on its own, randomly each time the simulation is run, and then for the landscape to represent some level of fragmentation. This was particularly tricky and get me and my supervisor busy for a while in coming up with suitable ideas. In the end, we 'seeded' the habitats, so that each habitat started at a random cell and moved outward to neighbouring cells. If the cell it wanted to convert to its habitat is alreayd assigned it jumps to a free random cell on the landscape.
Netlogo code (this was done for each of the 7 habitat types):
to expand-habitatarable
ask arable [
if all? patches [pcolor != black] [stop]
if Harable = 0 [stop]
ifelse all? neighbors [pcolor != black]
[move-to one-of patches with [pcolor = black]
set pcolor color
reduce-colours]
[ifelse all? neighbors4 [pcolor != black] [
move-to one-of neighbors with [pcolor = black]
set pcolor color
reduce-colours
][
move-to one-of neighbors4 with [pcolor = black]
set pcolor color
reduce-colours]
]
]
end
the method to used to call the habitat methods:
to expand-habitats
ifelse (Harable = 0) and (Hpasture = 0) and (Hwood = 0) and (Hgarden = 0) and (Hwater = 0) and (Hurban = 0) and (Hroad = 0) [stop] [
expand-habitatarable
expand-habitatpasture
expand-habitatwood
expand-habitatgarden
expand-habitatwater
expand-habitaturban
expand-habitatroad
]
end
Fragmentation was achieved by swapping cells with other random cells at a given probability. 50% probability of swapping leads to 100% fragmentation. (The definition of fragmentation is actually really difficult to find so this is a good enough demonstration for these purposes):
The extension to the model involved giving the hedgehogs a bit more realistic behaviour, so instead of simply choosing the nearest cell with the lowest cost at every step, they mimicked real hedgehogs by choosing habitat depending on the time of day: i.e. during the daytime, they preferred cover (forest) as they would in reality in order to rest, and during the night time they preferred areas where they could feed (pasture, gardens etc):
to move-hedgehog-day
ask hedgehog [pen-down]
ask patches [set visit visit - 1]
ask hedgehog [
ifelse pcolor = brown
[stop]
[face one-of neighbors with-min [cost]
forward 1]
]
end
to move-hedgehog-night
ask hedgehog [pen-down]
ask patches [set visit visit - 1]
ask hedgehog [
if (pcolor = green) or (pcolor = lime) [stop]
ifelse any? neighbors with [pcolor = lime] [
face one-of neighbors with [pcolor = lime]
forward 1][
ifelse any? neighbors with [pcolor = green] [
face one-of neighbors with [pcolor = green]
forward 1][
face one-of neighbors with-min [cost]
forward 1]
]
]
end
I'm currently trying to find a way to upload the netlogo model as an applet and will post it when I can. The code to the full model can be found in the links on the left.
In a nutshell, by looking at the average cost of a path taken by any hedgehog in any landscape, if you look at the hedgehogs that move simply by least-cost pathways, the average cost of path increases dramatically as the landscape becomes more fragmented. If you look at the hedgehogs with some differentation between night and day, then there average path cost stays more or less the same with any level of fragmentation:
Fig 1. a) Simple hedgehogs, b) hedgehogs with day and night preferences
If there are any questions about Netlogo, or how to specifically code for similar process as I've outlined here then please get in touch.
Luckily there was one clear winner in the least-cost model analysis so I can be fairly confident that this provides a good fit to the way that hedgehoge actually view and move around the landscape. With this in mind I simplified things abit by reducing the number of habitat types from 22, from the original GIS maps, to 7 in the simulations. Whilst this sounds dramatic, it was logical and simply meant lumping all water features together into a single habitat, all urban features together etc. Most of the habitats that we lumped together were given the same cost value anyway, but to clarify, this is how I simplified the landscape:
The proportions of each landscape were for the most part arbitrary, although were estimated from the original GIS maps.
So.... Netlogo. The building of the model was fairly straightforward. Netlogo is brilliant at making it easy to get something up and running fairly quickly. Turtles represent agents and the simplified language and syntax of netlogo makes it really easy to get the turtles to move around in a random direction at every time step.
Of course, by using the least-cost model as a base, turtles were amended so that they searched the cells in their immediate neighbourhood and moved to the cell of the least cost. Sample code is below:
to move-hedgehog
ask hedgehog[pen-down]
ask patches [set visit visit - 1]
ask hedgehog [
face one-of neighbors with-min [cost]
forward 1]
end
The biggest problem I faced with the enitre netlogo model was how to construct the landscape. It is easy to generate different habitats by stating which cells to be of which colour(habitat). What is much harder to get a landscape to develop on its own, randomly each time the simulation is run, and then for the landscape to represent some level of fragmentation. This was particularly tricky and get me and my supervisor busy for a while in coming up with suitable ideas. In the end, we 'seeded' the habitats, so that each habitat started at a random cell and moved outward to neighbouring cells. If the cell it wanted to convert to its habitat is alreayd assigned it jumps to a free random cell on the landscape.
Netlogo code (this was done for each of the 7 habitat types):
to expand-habitatarable
ask arable [
if all? patches [pcolor != black] [stop]
if Harable = 0 [stop]
ifelse all? neighbors [pcolor != black]
[move-to one-of patches with [pcolor = black]
set pcolor color
reduce-colours]
[ifelse all? neighbors4 [pcolor != black] [
move-to one-of neighbors with [pcolor = black]
set pcolor color
reduce-colours
][
move-to one-of neighbors4 with [pcolor = black]
set pcolor color
reduce-colours]
]
]
end
the method to used to call the habitat methods:
to expand-habitats
ifelse (Harable = 0) and (Hpasture = 0) and (Hwood = 0) and (Hgarden = 0) and (Hwater = 0) and (Hurban = 0) and (Hroad = 0) [stop] [
expand-habitatarable
expand-habitatpasture
expand-habitatwood
expand-habitatgarden
expand-habitatwater
expand-habitaturban
expand-habitatroad
]
end
Fragmentation was achieved by swapping cells with other random cells at a given probability. 50% probability of swapping leads to 100% fragmentation. (The definition of fragmentation is actually really difficult to find so this is a good enough demonstration for these purposes):
The extension to the model involved giving the hedgehogs a bit more realistic behaviour, so instead of simply choosing the nearest cell with the lowest cost at every step, they mimicked real hedgehogs by choosing habitat depending on the time of day: i.e. during the daytime, they preferred cover (forest) as they would in reality in order to rest, and during the night time they preferred areas where they could feed (pasture, gardens etc):
to move-hedgehog-day
ask hedgehog [pen-down]
ask patches [set visit visit - 1]
ask hedgehog [
ifelse pcolor = brown
[stop]
[face one-of neighbors with-min [cost]
forward 1]
]
end
to move-hedgehog-night
ask hedgehog [pen-down]
ask patches [set visit visit - 1]
ask hedgehog [
if (pcolor = green) or (pcolor = lime) [stop]
ifelse any? neighbors with [pcolor = lime] [
face one-of neighbors with [pcolor = lime]
forward 1][
ifelse any? neighbors with [pcolor = green] [
face one-of neighbors with [pcolor = green]
forward 1][
face one-of neighbors with-min [cost]
forward 1]
]
]
end
I'm currently trying to find a way to upload the netlogo model as an applet and will post it when I can. The code to the full model can be found in the links on the left.
In a nutshell, by looking at the average cost of a path taken by any hedgehog in any landscape, if you look at the hedgehogs that move simply by least-cost pathways, the average cost of path increases dramatically as the landscape becomes more fragmented. If you look at the hedgehogs with some differentation between night and day, then there average path cost stays more or less the same with any level of fragmentation:
Fig 1. a) Simple hedgehogs, b) hedgehogs with day and night preferences
If there are any questions about Netlogo, or how to specifically code for similar process as I've outlined here then please get in touch.
Subscribe to:
Posts (Atom)


