0

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.

Raj
  • 1,049
  • 3
  • 16
  • 30

1 Answers1

1

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.

Luke C
  • 10,081
  • 1
  • 14
  • 21
  • So in that case if i do some operation like find the turtle with minimum value in one of the agentsets, using `min-one-of` will it matter that all the agentsets are not similarly ordered or will Netlogo give me the correct result? – Raj Mar 01 '17 at 22:04
  • It should return the correct result so long as you are querying an agentset- something like `ask patch 0 0 [ ask min-one-of tl [distance myself ] [ set size 3 ] ]`. Also note that in your example screenshot only "tl" is an agentset, the rest are lists- you query a list of agents differently than you do an agentset. – Luke C Mar 01 '17 at 22:34
  • Since you are saying that only `tl is an agentset` and everything else is a list. If i want to find the turtle with the `min` value in one of the lists say `ls` I cannot use `lput` right? I may be able to get the index or the `position` of the minimum value in the list but when i reference the position to identify the turtle in the agentset it will throw an error. For example `set tl other turtles set ls [distance myself ^ 2] of tl set index position (min ls) ls`. Till this point it would work but when i use `lput item index tl`. I would get an error asking for a list instead of agentset – Raj Mar 01 '17 at 22:55
  • I may not be understanding you correctly, but I think you could skip a step and just use `sort-on` to build, for each patch, an ordered list of turtles sorted for whatever reporter you want. For example, if you wanted a list of turtles based on distance from the patch, you could use `ask patches [ set ls sort-on [ distance myself ] turtles ]`. This will create a list of turtles in ascending order of distance from the patch. To get the first turtle, have that patch ask the `first` in the list to do a thing- eg `ask patch 0 0 [ ask first ls [ set color red ] ]` – Luke C Mar 01 '17 at 23:14
  • What i meant was if I want to find which turtle is the closest to a patch as you have mentioned in your last comment. If say the first turtle in the list ls is `turtle 7` then I want the output to be `(turtle 7)`. I dont want the value itself but the turtle with the lowest or greatest value in the list. Since the initial reference `tl` is an agentset and remaining are lists it would thrown an error when using `lput` to find the turtle – Raj Mar 01 '17 at 23:27
  • Right. `Lput` is regardless not what you want, it just appends to a list. Instead, just take what I said in the last comment and modify it so that the patch outputs the first item in the list- eg `ask patch 0 0 [ print first ls ]` (After you have made "ls" a sorted list as in my previous comment ) – Luke C Mar 01 '17 at 23:40
  • Got it..Thanks a ton. Appreciate the help and you taking time to patiently answer all my questions. – Raj Mar 01 '17 at 23:43
  • Happy to help! Cheers. – Luke C Mar 01 '17 at 23:45