Calculate index of dissimilarity in NetLogo

Viewed 77

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!

1 Answers

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 

Related