9

I have two agentsets. Are there functions for finding:

  1. An agentset of agents that are present in both (intersection)
  2. An agentset of agents that are present in one and not the other

I'm finding it very difficult to implement this by hand, especially when it's needed inside of a triple ask

Ideal use would be similar to with syntax:

let cross set1 and-in set2
let uniq set1 with [color = red] not-in set2

Simple things like "Is agent A in the agentset X?" are problematic

Seth Tisue
  • 29,985
  • 11
  • 82
  • 149
Mikhail
  • 8,692
  • 8
  • 56
  • 82

1 Answers1

13

For the first one you use the turtle-set primitive. For the second one you need the member? primitive, which also works on agentsets. As such:

to setup
  ca
  create-turtles 10 [set color red]
  create-turtles 10 [set color blue]
  let red-ones turtles with [color = red]
  let blue-ones turtles with [color = blue]

  ;join 2 agent sets
  let joinset (turtle-set red-ones blue-ones)
  show joinset

  let even-ones (turtles with [who mod 2 = 0])
  ;subtract even-ones from red-ones
  let subtractset red-ones with [not member? self even-ones]
  show subtractset
end
Jose M Vidal
  • 8,816
  • 6
  • 43
  • 48
  • 1
    I didn't realize that `[with]` can receive a "reporter". My first question was to find an intersection, not a union, but seeing your answer for the second question I'm guessing it would be as simple as `red-ones with [member? self even-ones]`. Thank you! – Mikhail Dec 08 '11 at 14:10
  • Thanks. I had the same question and didn't realize that one could use `self` in a `with` construct. At first I tried `?`, but got an error message and then had no other ideas of how to proceed. – RussAbbott Dec 29 '14 at 11:28