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.

1 comment: