2

I have a NetLogo model in which agents retrieve multiple lists of variables from other agents. The order in which these lists are returned is crucial, because variables in each list are associated with each other. However, I believe lists are returned from other agents in a random order each time. For example, take the following simplified test case:

turtles-own [testlist1 testlist2 testlist3 testlist4]

to setup
random-seed 1
clear-all
create-turtles 5
  ask turtles [
    create-links-with other turtles
    set testlist1 []
    set testlist2 []
    set testlist3 []
    set testlist4 []
    set testlist1 lput [who] of self testlist1
    set testlist2 lput [who] of self testlist2] ;identical to testlist1
end

to go
  ask turtles[  
    set testlist3 reduce sentence [testlist1] of link-neighbors
    show testlist3
    set testlist4 reduce sentence [testlist2] of link-neighbors
    show testlist4]
end

For my use case, values in testlist3 and testlist4 should be in the same order, but their orders differ at random. Output:

(turtle 2): [0 3 1 4]
(turtle 2): [3 4 1 0]
(turtle 3): [4 1 0 2]
(turtle 3): [1 0 2 4]
(turtle 0): [4 2 3 1]
(turtle 0): [3 4 2 1]
(turtle 1): [0 4 2 3]
(turtle 1): [4 2 3 0]
(turtle 4): [0 2 1 3]
(turtle 4): [0 3 2 1]

My question: What is the best way to return multiple lists (such as testlist and testlist2 above) from an agent-set in the same order in a given procedure?

Replacing link-neighbors with turtle-set sort link-neighbors doesn't work, because after converting the sorted list back to an agent-set, the agents in the agent-set are called in a random order. If at all possible, I'd prefer not to have to refactor the entire model from lists to matrices using the matrix extension.

1 Answers1

4

You were on the right track with the idea of turning your agentset to a list. The only part missing was using map instead of turning it back into a turtle set and then using of again:

to go
  ask turtles[  
    set testlist3 reduce sentence map [ t -> [ testlist1 ] of t ] sort link-neighbors
    show testlist3
    set testlist4 reduce sentence map [ t -> [ testlist2 ] of t ] sort link-neighbors
    show testlist4
  ]
end

See this answer for a bit more detail about the parallel between list operations and agentset operations.

Nicolas Payette
  • 14,847
  • 1
  • 27
  • 37
  • Thank you, that works brilliantly! Initially, I was surprised not to get the error, `OF expected this input to be an agent or agentset, but got a list instead`, because that would happen with, for example, `set testlist3 reduce sentence [ testlist1 ] of sort link-neighbors`. Please correct me if I'm wrong, but I believe that because map is returning one element of `sort link-neighbors` at a time, `t` in the anonymous procedure is a single agent—so it works. Thanks again! – Joe Wasserman Mar 12 '19 at 15:56
  • 1
    Yes, it is right to say that "map is returning one element of `sort link-neighbors` at a time, `t` in the anonymous procedure is a single agent". Happy it works for you! – Nicolas Payette Mar 13 '19 at 16:34