在龟群中分配随机变量

时间:2016-08-31 20:52:48

标签: netlogo

我有一组动态的乌龟组(滑块控制,用变量group-id标记的组),它们有一个关联的龟自己的变量(trait-1),我试图随机分配一个不同的值来表示特征每组海龟为-1(group-id)。目前,每只乌龟都有一个随机分配的特征-1值,该值在组内和组之间都有所不同,但我希望每组中的所有个体都是相同的,并且只在不同组之间变化。任何帮助都会很棒!

旁注,我正在使用分组变量而不是品种,因为我无法弄清楚如何通过滑块动态控制品种数量......不确定这是可能的吗?

turtles-own [
  group-id ;grouping id, each group represents a species
  trait-1  ; a trait to be assigned a score from 1 - 10
 ]

to setup
clear-all
set-species
end

to set-species
let n_groups num_species  ;create n groups of turtles based on slider value (num_species)
foreach n-values n_groups [ ? ] [
create-turtles 3 [      ;create 3 indviduals in each group
  set group-id ?
  set color (group-id + 19) ; give each group a different color
  set  trait-1 1 + random 10 ]  ;set trait-1 ranging from 1 to 10
]
ask turtles [ set label group-id ] ; show group-id
ask turtles[  ; move turtles out of the center, not really important, just to help see the turtles. 
fd trait-1 
]
end

1 个答案:

答案 0 :(得分:2)

只需移动到龟初始化之外的地方:

  foreach n-values n_groups [ ? ] [
    let group-trait 1 + random 10  ;set it here
    create-turtles 3 [      ;create 3 indviduals in each group
      set group-id ?
      set trait-1 group-trait
      set color (group-id + 19) ; give each group a different color
      ;set  trait-1 1 + random 10 ]  ;don't set it here
    ] 
  ]