Netlogo - 给定2只以上海龟的Agentset,找到最常见的颜色

问题描述 投票:2回答:3

鉴于两只或更多只乌龟的代理人,我如何找到最常见的颜色?

如果可能的话,我想做类似的事情:

set my_color [mostfrequentcolor] of my_agentset
netlogo agentset
3个回答
1
投票

你正在寻找modes原语(https://ccl.northwestern.edu/netlogo/docs/dictionary.html#modes):

one-of modes [color] of my_agentset

这是“模式”,复数,因为可能会有一个平局。打破平局的一种方法是使用one-of进行随机选择。


1
投票

使用此示例设置:

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

1
投票

这里有两个选项,都使用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

我不太确定我更喜欢哪一个......

© www.soinside.com 2019 - 2024. All rights reserved.