【问题标题】:Assigning random variables within turtle groups在海龟组内分配随机变量
【发布时间】:2016-08-31 20:52:47
【问题描述】:

我有一个动态数量的海龟组(滑块控制,用变量 group-id 标记的组),它们有一个关联的海龟自己的变量(trait-1),我试图随机分配一个不同的值给 trait每组海龟 (group-id) -1。目前,每只乌龟都有一个随机分配的 trait-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

【问题讨论】:

    标签: netlogo


    【解决方案1】:

    只需将它移到海龟初始化之外的位置即可:

      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
        ] 
      ]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-02
      • 2022-06-23
      • 2018-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多