JenB
JenB

Reputation: 17678

Conditional network neighbours

I want to select the network neighbours filtered by a link attribute rather than a turtle attribute. For example, this might be selecting only the closest friends based on a strength score on the friendship link. I could do this by having different link breeds for close friends, but this would require constantly changing breeds as the condition was or was not required.

Below is an example with a boolean condition.

links-own [flag?]

to testme
  clear-all
  ask patches [set pcolor white]
  create-turtles 20
  [ setxy 0.95 * random-xcor 0.95 * random-ycor
    set color black
  ]
  ask n-of 3 turtles [set color red]
  repeat 40
  [ ask one-of turtles
    [ create-link-with one-of other turtles
      [ set flag? random-float 1 < 0.4
        set color ifelse-value flag? [red] [gray]
      ]
    ]    
  ]
;  colour-neighbours
  colour-neighbours2
end

to colour-neighbours
  ask turtles with [color = red ]
  [ ask my-links with [flag?]
    [ ask other-end [ set color blue ]
    ]
  ]
end

to colour-neighbours2
  ask turtles with [color = red ]
  [ ask turtle-set [ other-end ] of my-links with [flag?]
    [ set color blue ]
  ]
end

I am currently doing the equivalent of colour-neighbours, but it involves stepping through several contexts. The colour-neighbours2 version is conceptually closer because it is referring directly to the network neighbours. However, because of the of, I get a list of neighbours that I then have to convert to an agentset.

This is for teaching and, while both work, they seem very convoluted when compared to the unconditional network neighbourhood with the link-neighbors primitive. That is, if I didn't care about the flag, I could simply say ask link-neighbors [ set color blue ].

Is there a more direct way to identify network neighbours conditional on a link attribute?

Upvotes: 3

Views: 345

Answers (2)

Luke C
Luke C

Reputation: 10301

Just to add a more-convoluted (worse?) option that uses an agentset:

to colour-neighbours4
  ask turtles with [ color = red ] [
    let flag-links my-links with [flag?]
    ask link-neighbors with [ any? my-links with [ member? self flag-links ] ] [ 
      set color blue 
    ]
  ]  
end

Upvotes: 1

Nicolas Payette
Nicolas Payette

Reputation: 14972

You already cover most possibilities. Another way to do it would be:

to colour-neighbours3
  foreach [ other-end ] of my-links with [ flag? ] [ t ->
    ask t [ set color blue ]
  ]
end

I would avoid colour-neighbours2 because, as you stated, it requires the conversion from a list to an agentset. Whether you should use colour-neighbours or colour-neighbours3 is, I think, a matter of personal preference.

Upvotes: 3

Related Questions