Search code examples
netlogoagent

Give agents on patch the same value that patches have NETLOGO


I have a model where there are multiple animals in a den

patches-own [den-ID]

where the dens are pink and the number of dens is controlled with a slider global var

ask patches with [pcolor = pink] [set den-ID random n-dens]

Now, I want agents that spawn on that patch to set their own parameter "family" to a value equal to the den-ID that they spawn on - therefore having a group of animals that can be traced back to a den site and all share a home range.

I have tried many solutions but cant get anything to work.

basically something like

set family (family = den-id of patch-here)

thanks


Solution

  • The answer: ask turtles [ set family patch-here ]

    Down below is how we arrived there


    You seem a bit confused in your Netlogo syntax. = is only used for logical expressions in Netlogo, not for setting variables. For that you use set. If you want to use of to access variables of another agent, you need to encase the variable in square brackets: [den-id] of patch-here.

    The solution you are looking for looks something like the following:

    ask turtles [ set family [den-id] of patch-here ]
    

    It can be made even easier. A turtle can directly access the variables of a patch that they are on without needing to name that patch.

    ask turtles [ set family den-id ]
    

    I noticed another thing in your code. Nothing in set den-ID random n-dens prevents multiple dens of having the same number. There are numerous ways of doing it, for example with a while loop where you increment the current-ID each time.

    set current-ID 1
        while [any? patches with [pcolor = pink and den-ID = 0]] [
            ask one-of patches with [pcolor = pink and den-ID = 0] [set den-ID current-ID]
        set current-ID current-ID + 1
        ]
    

    You could even opt to forget about den-id and just let the turtles remember their den itself:

    ask turtles [ set family patch-here ]