Search code examples
netlogo

In a Netlogo network, how can turtles "see" properties of other turtles?


I am trying to build a model in which turtles decide to change colour depending on their environment in a network.

The approach would be to "check" the colour of the surrounding turtles and then set an if statement for the turtle in question to switch colour (there will only be 2 colours).

Specifically I would like to know how can a turtle "see" or check other turtles' colour (or other properties).

If possible I would also like to create a slider for "how many links away" can turtles see their neighbouring turtles' (or neighbours of neighbours, etc) colour.

I am new to both Netlogo and Stackoverflow, so please let me know if I should make any modifications to my model and/or question.

Thanks!


Solution

  • Welcome to Stack Overflow! Typically you'll want to stick to a single question per post, both for simplicity and for the benefit of future users with similar questions. As well, in cases where its applicable you should try to include some code to show what you've tried so far, as well as any setup necessary- you want to make a minimal, complete, and verifiable example. In this case, I think you're okay since your questions are clear and well explained, but if you have more complex questions in the future you will be more likely to get useful answers by following those guidelines.

    For your first question, it looks like you want the of primitive- check out the dictionary entry for details. of can be used in a few ways, including allowing agents to check the value of a variable (such as color) of another agent. Check out this example code:

    to setup
      ca
      reset-ticks
      crt 10 [
        setxy random 30 - 15 random 30 - 15
        create-link-with one-of other turtles
      ]
    end
    
    to go
      ask turtles [
        set color [color] of one-of link-neighbors
      ]
    end
    

    Every time the go procedure is called, one of the turtles changes its color to the color of one of its link-neighbors. If you run it long enough, all connected turtles should end up with the same color.

    For your second question, I suggest you check out the Nw extension, which is an extension built to deal more easily with Netlogo networks. Specifically, have a look at nw:turtles-in-radius, which should work with your slider approach. To get it working, include the extension using

    extensions [ nw ]

    at the start of your code. Then, assuming the same setup as above, you can play around with something like

    to network-radius
      ask one-of turtles [
        set color red
        ask other nw:turtles-in-radius 2 [
          set color white
        ]
      ]
    end
    

    When you call the network-radius procedure above, you should see one turtle turn red, and any turtles within 2 links of that turtle turn white. To switch to a slider, just swap the "2" out for your slider variable. Hope that helps!