Search code examples
netlogoagentset

Order of the agents in agentset don't match


I have a doubt and apologize if the answer is very obvious. I created the following code. Pretty simple & straightforward:

patches-own [ tl ls ls2 ls3 ls4 tsl]
turtles-own [mysize]

to setup
  clear-all
  reset-ticks
  crt 5
  ask turtles [ set heading random 360 jump random 20 set mysize random-float 1]
  asd
  inspect patch 0 0
end

to asd
  let old sum [mysize] of turtles
  ask patches [ set tl other turtles
    set tsl [self] of tl
    set ls [distance myself] of tl 
    set ls2 [distance myself ^ 2] of tl
    set ls3 [(mysize) / old] of tl
    ]
  ;print tl

end

to initial
  set heading random 360 jump random 20 set mysize 1
end

to go
  inspect patch 0 0
  ask turtles [ fd 1 set mysize mysize + random-float 1]
  let qwe random-float 1
  print qwe
  if qwe < 0.2 and count turtles > 2 [ask one-of turtles [die]]
  if qwe > 0.8 [ ask one-of patches [sprout 1 [initial]]]
  asd
  tick
end

As you can see, i have a inspect function in the code and below is the snapshot:

enter image description here My question is why is the ls and ls2 agents out of order. Agentset TSL shows the order of the turtles, so shouldn't the other agentsets created based on that follow the same order.


Solution

  • Unless you sort them somehow, Netlogo will query agents in an agentset in a random order. Functionally, ask tl follows the same logic as ask turtles, and the same goes for retrieving variables from an agentset. For example, if you use the command center to try the code below several times (after running your setup and asd), you will note that the order of agents queried is not the same every time.

    ask patch 0 0 [ print [distance myself] of tl ]

    All this to say that the creation of a patch's "tsl" list is independent of the creation of your other lists. It is not an ordered list, it is a list of randomly called turtles from the agentset "tl". One way to get consistent ordering of an agentset is to use one of the sort primitives.