NetLogo - random listing to identify turtles in the vicinity

I would like to get the sum of specific weights only in patches with turtles present in the respective Moore area. My approach is to work out the sum of products between a list of weights and a list of binary variables. I created a list of weights in the neighborhood for each patch

Let weight_list [ 1 2 3 4 5 6 7 8]; weights of the moore neighborhood for specific patch

      

Then I created a list with a binary variable (1 if the turtle is present in the adjacent patch, 0 otherwise),

ask patches with [  any?  turtles-here ]   [let turtle_present 1 ]
ask patches with [ not any? turtles-here]  [let turtle_present 0 ]
ask patches[ set binary_list  [ turtle_present ] of neighbors ]

      

I tried to multiply this binary_list with their respective weights and get the sum for example. sum (binary_list * weight_list)

My problem is that binary_list is sorting randomly. For example. if I have 2 turtles in the neighborhood, the list may show [01100000] or [01000100]. This randomness prevents me from multiplying with their respective weights. This may be a silly question, but I'm a relatively new NetLogo user. I would appreciate any help, thanks

+3


source to share


1 answer


Definitely not a stupid question. Working with sorted lists of agents usually requires a bit of a workaround, and it's not always particularly intuitive.

If you sort the patches, they will come out sorted in the upper left corner, filling the lines from left to right. So consider this for weight-list

. Since we cannot query the lists to do things the way we can with agents, we will need to iterate over the list using map

, asking each patch to report its neighbors.

There are several different ways to do this, but my suggestion would be to write a reporter according to this:



to-report neighbor-list
  report map [[ifelse-value any? turtles-here [1][0]] of ?]
             sort neighbors
end

      

At this point, you can simply ask the patch (or the turtle, since they have direct access to their "patch variables") for their neighbor list, and it will send you a list of 1s and 0s in sequential order.

+1


source







All Articles