0

Suppose that the more common friends two nodes have, the more closely they are related. I want to find the closest link-neighbor of a node. But it's difficult for me to find the common friends between one random nodes and its link-neighbors.

I wrote an example to see if my thought works:

turtles-own[CF] ;;common friends

to setup
  ca
  crt 10 [set size 1 set color gray]
  ask turtle 1 [create-links-with other turtles]
  ask turtle 2 [create-link-with turtle 3]
  ask turtle 2 [create-link-with turtle 4]

  layout-circle (sort turtles) 5
  reset-ticks
end

to go
  ask one-of turtles [
    let communicator self
    ask link-neighbors [;;setA
      let i 0
      ask link-neighbors [;;setB
        if link-neighbor? communicator[
          set i i + 1
        ]
      ]
      set CF i
    ]
  ]
  tick
end

In the interface, I set a reporter [CF] of turtle 2.If it works, the answer could be 2

But as it keeps going, the reporter shows sometimes 1 and sometimes 2. I don't know why and I hope sth more simple.

yy w
  • 65
  • 7

1 Answers1

2

I haven't traced your code's operation, but your core structural problem is that "common friends" is a property of a PAIR of nodes, not one node .

I tweaked your code to set the common friends count ( CFL ) to the LINK between pairs of nodes. So (link 1 2) ends up with CFL = 2 and it's stable. It's not as efficient as it could be but then you can just run all the turtles, not one of the turtles in your go step.

Here's my solution:

turtles-own[CF] ;;common friends
links-own[CFL]

to setup
  ca
  crt 10 [set size 1 set color gray set shape "circle" set label who ]
  ask turtle 1 [create-links-with other turtles  set color green]
  ask turtle 2 [create-link-with turtle 3 [set color orange set thickness 0.25]  ]
  ask turtle 2 [create-link-with turtle 4 [set color orange set thickness 0.25] ]
  ask turtle 2 [ set color red   ]
   
  ask one-of links with [end1 = turtle 1 and end2 = turtle 3] [ set color green set thickness 0.25 ]
  ask one-of links with [end1 = turtle 1 and end2 = turtle 4] [ set color green set thickness 0.25 ]

  layout-circle (sort turtles) 5
  reset-ticks
end

to go
  let ln-direct "" 
  let ln-once-removed ""
  
  ask turtles [
    let communicator self
    ask link-neighbors [;;setA
      set ln-direct self
     ; print (word "for turtle " myself " one DIRECT link-neighbor  is " ln-direct)
      let i 0
      
      ask link-neighbors [;;setB
        set ln-once-removed self
      ;  print ( word " one neighbor of that neighbor is " ln-once-removed )
        if link-neighbor? communicator[
         ; print (word  ",,,,,,,, BINGO - loops back to " communicator )
          set i i + 1
        ]
      ]
      ask one-of links with [
        ( end1 = communicator and end2 = ln-direct ) or 
        ( end2 = communicator and end1 = ln-direct )
      ] [set CFL i set label CFL   ]
    ]
  ]
  tick
end
Wade Schuette
  • 1,455
  • 1
  • 8
  • 9
  • 1
    Thanks for your response! It's so kind of you to do lots of things like`print` and`set label` for easier understanding. Thank you so much! – yy w Jun 18 '22 at 00:30