Search code examples
netlogoagent-based-modeling

Netlogo: Making a turtle interact with anotherone after evaluating similarity in a given variable


I have several turtles each with three variables opinion1, opinion2 and opinion3. I need them to:

  1. identify which of these three variables has the highest value
  2. find another turtle in their network with a value at least as high as the one found in 1.
  3. update its own value found in 1. with respect to that of the turtle found in 2.

What I have done doesn't really work because it only updates looking at o1 without really having a look at which of the tree (opinion1, opinion2 or opinion3) is the highest and THEN looking for a neighbour.

to update-opinion
  ask turtles [
    let my-nearby-turtles nw:turtles-in-radius 1
    let my-opinion1 opinion1
    set neighbour one-of my-nearby-turtles with [ opinion1 > my-opinion1 ]
    if neighbour != nobody [
      let opinion_n [opinion1] of neighbour
        set opinion1 ((opinion1 + opinion_n) / (2))
    ]
  ]
end

Solution

  • I don't know a simple way to do this with unique variables like opinion1 etc, but maybe having a list of opinions instead of individual variables for each opinion will work. For example, with this setup:

    extensions [ nw ]
    
    turtles-own [ 
      opinions
    ]
    
    to setup
      ca
      resize-world -5 5 -5 5
      set-patch-size 30
      crt 30 [
        set shape "dot"
        set opinions n-values 3 [ precision random-float 10 2]
        set color scale-color blue sum opinions -5 35
        while [ any? other turtles-here ] [
          move-to one-of neighbors4
        ]
      ]
      ask turtles [
        create-links-with turtles-on neighbors4
      ]
      reset-ticks
    end
    

    You get something like this:

    enter image description here

    Where each turtle has an opinions list variable that is three items long. Now, you can have each turtle determine its highest opinion value using max, get that maximum values index position in the list using position, and then query that turtle's neighbors to see if any of them have a higher value in the same index position. If they do, modify your asking turtles opinions list using replace-item to be the average of the two values:

    to go
      ask turtles [
        ; Get adjacent turtles
        let my-nearby-turtles nw:turtles-in-radius 1
    
        ; Identify the highest highest value variable of 
        ; the current turtle, and get its list position
        let my-opinion max opinions
        let my-op-ind position my-opinion opinions
    
        ; Pick one of the turtles whose value in the same indexed
        ; position is higher than my-opinion
        let influence one-of my-nearby-turtles with [ 
          item my-op-ind opinions > my-opinion
        ]
    
        ; If that turtle exists, update my own opinions list as appropriate
        if influence != nobody [
          let new-opinion  precision ( 
            ( [ item my-op-ind opinions ] of influence + my-opinion ) / 2
          ) 2
          set opinions replace-item my-op-ind opinions new-opinion
        ]
        set color scale-color blue sum opinions -5 35
      ]
      tick
    end
    

    Hopefully that is sort of on the right track, not sure if a list will work for what you need. If you must have the variables as standalone values at each tick, I suppose you could convert them to a list then follow the procedure above. If you only need them for output, you could just update your unique variables as needed based on the values in the list (as long as you are consistent with the order).