2

I have seen on here how to create an intersection or union of two agentsets, but I am trying to say if any turtle in agentset a is in agentset b, return true. I was trying

ifelse (member? (one-of my-intersections) destination-intersections) 

but I am pretty sure this is just testing if one element in my-intersections is in destination -intersections instead of testing every element. Is there some way to use a for each? Or is there another functionality I am unaware of?

Again, I have already referenced

NetLogo two agentsets operations

Thank you!!

Community
  • 1
  • 1
Kory
  • 308
  • 1
  • 7

1 Answers1

1

The most straightforward way to write such a test is:

to-report test [ as bs ]
  report any? as with [ member? self bs ]
end

To try it:

create-turtles 5
show test (turtle-set turtle 0 turtle 1 turtle 2) (turtle-set turtle 3 turtle 4)
show test (turtle-set turtle 0 turtle 1 turtle 2) (turtle-set turtle 0 turtle 3 turtle 4)

will show:

observer: false
observer: true

It's not the most efficient way, however, because the with clause builds an intermediate agentset that's not really needed.

A faster test would be:

to-report test [ as bs ]
  let result false
  ask as [
    if member? self bs [ 
      set result true
      stop
    ]
  ]
  report result
end

Edit:

I spoke too fast. As per the NetLogo wiki page on compiler architecture, the combination of any? and with does get optimized to exit early. Bottom line: you should use the first version (i.e., any? as with [ member? self bs ]).

Nicolas Payette
  • 14,847
  • 1
  • 27
  • 37