13.6.11

Netlogo Code (summer project)

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

1 comment:

  1. Nice work there
    I was on the edge of giving my work up on this ****
    i try since 2 weeks to find a solution,
    ok.....i'm still in school, but this is an important part for one of my exams, so i probably shouldnt give up :P
    well, this is the first working program to create habitats, landscapes or however you wanna name them

    so what i tried to say is : I'm extremely grateful that there is someone who did this amazing work, thx :D

    ReplyDelete