2

I'm want to calculate the index of dissimilarity in NetLogo. I have a world divided into different regions and want to examine how evenly species are distributed around the world.

Consider this example: A world is divided into 16 different regions. The world is populated with two types of ants, red and blue. It looks like this:

enter image description here

The world in the picture is produced with the following code:

globals[indexdissimilarity] ; where I want the index of dissimilarity to be stored.

to setup
  ca

  ;Setting world.
  resize-world 0 19 0 19
  set-patch-size 15

  ;Creating regions.
  let x 5
  let y 5
  let col 45
  while [y <= max-pycor + 1 ][
    while [x <= max-pxcor  + 1][
      ask patches with [pxcor < x and pxcor >= x - 5 and pycor < y and pycor >= y - 5][
        set pcolor col
      ]
      set x x + 5
      set col col + 3
    ]
    set x 5
    set y y + 5
  ]

  ask n-of (count patches * 0.85) patches[sprout 1[
    set shape "bug"
    set color red]]
    
  ask n-of (count turtles * 0.50) turtles [set color blue]
  dissimilarity
end

; Here is where I want to calculate the index of dissimilarity.
to dissimilarity
  let tot_red (count turtles with [color = red]) 
  let tot_blue (count turtles with [color = blue])

  ; set indexdissimilarity 
  
end

My main issue is how to iterate parts of the calculations over each neighborhood.

Thanks!

Community
  • 1
  • 1
ecl
  • 369
  • 1
  • 15
  • Ignoring how to do it in NetLogo, do you know how to do this mathematically? Then make an attempt to implement that in NetLogo and show us the code that makes the attempt. – JenB Mar 25 '20 at 14:36
  • @JenB I'm sorry I didn't spell out exactly where I was stuck. My issue is how to iterate it over each neighborhood. So in (ai/A - bi/B ), how do I identify the number of blue ant in each neighborhood in an efficient way. – ecl Mar 25 '20 at 16:13

1 Answers1

2

I think I managed to solve it. Please, let me know if it looks correct. Here is the full updated code.

globals[indexdissimilarity
  dis
]

patches-own [reg]
to setup
  ca

  ;Setting world.
  resize-world 0 19 0 19
  set-patch-size 15

  ;Creating regions.
  let x 5
  let y 5
  let col 45
  while [y <= max-pycor + 1 ][
    while [x <= max-pxcor  + 1][
      ask patches with [pxcor < x and pxcor >= x - 5 and pycor < y and pycor >= y - 5][
        set pcolor col
      ]
      set x x + 5
      set col col + 3
    ]
    set x 5
    set y y + 5
  ]

  ask patches [set reg [pcolor] of self]

  ask n-of (count patches * 0.85) patches[sprout 1[
    set shape "bug"
    set color red]]

  ask n-of (count turtles * 0.7) turtles [set color blue]
  update
end 


to update
  ;Dissimilarity index. 
  let tot_red (count turtles with [color = red]) 
  let tot_blue (count turtles with [color = blue])

  let neighb1 [reg] of turtles
  let neighb remove-duplicates neighb1
  set dis []

  foreach neighb [i -> set dis lput abs((count turtles with [reg = i and color = red] / tot_red) - (count turtles with [reg = i and color = blue] / tot_blue)) dis]
  set indexdissimilarity sum(dis) / 2
  print(indexdissimilarity)
end 

ecl
  • 369
  • 1
  • 15