Рассчитать индекс отличия в NetLogo - PullRequest
2 голосов
/ 25 марта 2020

Я хочу вычислить индекс различий в NetLo go. У меня есть мир, разделенный на разные регионы, и я хочу изучить, как равномерно распределяются виды по всему миру.

Рассмотрим этот пример: мир разделен на 16 различных регионов. Мир населен двумя видами муравьев, красным и синим. Это выглядит так:

enter image description here

Мир на картинке создается с помощью следующего кода:

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

Моя главная проблема это как перебирать части расчетов по каждой окрестности.

Спасибо!

1 Ответ

2 голосов
/ 25 марта 2020

Думаю, мне удалось это решить. Пожалуйста, дайте мне знать, если это выглядит правильно. Вот полный обновленный код.

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 

...