Netlogo - учитывая набор агентов из 2+ черепах, найдите наиболее частый цвет - PullRequest
0 голосов
/ 09 сентября 2018

Учитывая набор из двух или более черепах, как мне найти наиболее частый цвет?

Я бы хотел сделать что-то подобное, если это возможно:

set my_color [mostfrequentcolor] of my_agentset

Ответы [ 3 ]

0 голосов
/ 10 сентября 2018

Вот еще два варианта, оба с использованием расширения table:

extensions [ table ]

to-report most-frequent-color-1 [ agentset ]
  report (first first
    sort-by [ [p1 p2] -> count last p1 > count last p2 ]
    table:to-list table:group-agents turtles [ color ])
end

to-report most-frequent-color-2 [ agentset ]
  report ([ color ] of one-of first
    sort-by [ [a b] -> count a > count b ]
    table:values table:group-agents turtles [ color ])
end

Я не совсем уверен, какой из них я предпочитаю ...

0 голосов
/ 13 сентября 2018

Вы ищете modes примитив (https://ccl.northwestern.edu/netlogo/docs/dictionary.html#modes):

one-of modes [color] of my_agentset

Это "моды", множественное число, так как может быть галстук. Один из способов разорвать связь - использовать one-of для случайного выбора.

0 голосов
/ 09 сентября 2018

В этом примере настройки:

to setup
  ca
  crt 10 [
    set color one-of [ blue green red yellow ]
  ]
  print most-frequent-color turtles 
  reset-ticks
end

Вы можете использовать процедуру to-report, чтобы сделать это - подробности в комментариях:

to-report most-frequent-color [ agentset_ ]
  ; get all colors of the agentset of interest
  let all-colors [color] of agentset_

  ; get only the unique values
  let colors-used remove-duplicates all-colors

  ; use map/filter to get frequencies of each color
  let freqs map [ m -> length filter [ i -> i = m ] all-colors ] colors-used

  ; get the position of the most frequent color (ties broken randomly) 
  let max-freq-pos position ( max freqs ) freqs 

  ; use that position as an index for the colors used
  let max-color item max-freq-pos colors-used

  report max-color
end
...