Search code examples
netlogoagent

Adaptation of segregation model: how to avoid other breed densities at patch-here


The objective of my submodel is to simulate wolves avoiding patches that have a human density greater than their tolerance threshold. The human agents are created only on the urban-patches (grey), but there are grass-patches (brown) and forest-patches (green) in the NetLogo world as well (See interface tab link below for visual). The human agents are stationary whereas the wolf agents have the option to flee and [find-new-spot] if they are [not happy?] with their current location.

Interface Tab

globals [
  grass-patches
  forest-patches
  urban-patches
  percent-unhappy
  ]

breed [wolves wolf]

breed [humans human]

wolves-own [
  happy?
  anthro_tolerance
  ]

humans-own [
  human-population]

patches-own [
  human-density]


;; creating the urban patches
  set urban-patches patches with [pxcor < 10 and pycor < -30 ]
  ask urban-patches [set pcolor grey]
  ask urban-patches [set human-density human-density = pop-density]


to-report pop-density
      report count humans / (count urban-patches)
    end

The code for the wolves to determine their happy? level and report the %-unhappy is:

to update-wolves
  ask wolves [
    set anthro_tolerance 0.049
    ifelse (patch-here human-density >= anthro_tolerance)  ;;Error message
    [set happy? FALSE]
    [set happy? TRUE]
  ]
end

to update-globals
  set percent-unhappy (count wolves with [not happy?]) / (count wolves) * 100
end

How do I code the ifelse happy? to represent the individual wolf asking itself "what is the human-density of the patch I'm on, and is it above my anthro_tolerance?"

Also, when I inspect a patch, the human-density variable is zero for all urban patches (even if there is a human on the patch). How can I correct this?


Solution

  • Okay, I can see a couple of problems here. The first is:

    ask urban-patches [set human-density human-density = pop-density]
    

    I'm not sure why that's not throwing an error. But anyway, you don't use '=' to set variable values in NetLogo. Assuming your intent is that you want to assign the calculated value of pop-density to the patch variable named human-density, that line should be:

    ask urban-patches [set human-density pop-density]
    

    On to your actual error. You have:

    ifelse (patch-here human-density >= anthro_tolerance)
    

    The correct syntax for retrieving the value of a variable belonging to some model entity uses the primitive of, so you could write (not tested):

    ifelse ([human-density] of patch-here >= anthro_tolerance)
    

    But you can also take advantage of that fact that turtles have access to the patch variables of the patch where they are located. Note that this doesn't work in reverse - turtle to patch is unique because a turtle can only be in one place at a time. However, there can be many turtles on the one patch, so a patch wouldn't know which turtle to access the variable of.

    Using that trick gets you to:

    ifelse human-density >= anthro_tolerance
    

    There's another trick that you can use because your ifelse is setting a variable to true of false. This is a bit subtle so some people don't use it as it's a little harder to read, particularly if you are newish to NetLogo. But you can replace:

    ifelse human-density >= anthro_tolerance
    [set happy? FALSE]
    [set happy? TRUE]
    

    with:

    set happy? human-density < anthro_tolerance
    

    Read this from right to left. First it applies the comparison operator '<' and reports true if human-density < anthro_tolerance or false if not human-density < anthro_tolerance. That value (true or false) is then given to the variable named "happy?".