【问题标题】:How to ask one or two turtles (if any are present) to do an action?如何让一只或两只乌龟(如果有的话)做一个动作?
【发布时间】:2014-04-21 19:24:59
【问题描述】:

以下代码查找至少有一只雄性和一只雌性存在的所有补丁,然后在每个这样的补丁上,有一个雌性孵化一只随机性别的后代海龟。

turtles-own [ gender] 
to setup 
ask patches [ 
sprout  1 
[set size 0.2 
set color pink 
set gender "female" 

]] 
ask patches [ 
sprout  1 
[set size 0.2 
 set color blue 
 set gender "male" 



]] 

reset-ticks 

end

to-report parents-here?  ;; patch procedure
report any? turtles-here with [gender = "male"]
     and
     any? turtles-here with [gender = "female"]
 end

to go
ask patches with [parents-here?] [
ask one-of turtles-here with [gender = "female"] [
  hatch 1 [
    set gender one-of ["male" "female"]
  ]
 ]
 ]
 tick
 end

我不想问一位雌性孵化,而是询问在场的一位雌性是否要求它孵化“或”是否有两名在场的雌性要求它们孵化(最少一只,最多两只)。 我试着写了

ask n-of 2 turtles-here ............

但我有一个错误说这个补丁只有 1 个来自海龟

我尝试使用(但也有错误) 我也试着写了

ask n-of (1 + random 2 )

作为最小值和最大值,也是错误的。

提前谢谢你

【问题讨论】:

  • 我没明白你的意思,我当然不是想搞笑,但这是我写的标题,标题有什么问题吗???
  • 一切都很好。对不起:)
  • 没问题,我认为我的标题有些问题,因为我不是以英语为母语的人,所以我预计有些问题是错误的

标签: netlogo


【解决方案1】:

这是我能想到的最简单的解决方案:

let females turtles-here with [gender = "female"]
ask n-of (min list 2 count females) females [
  hatch 1 [
    ...
  ]
]

为什么是min list 2 count females?当您想要最大 2 时,您需要使用名为 min 的原语,这有点违反直觉。但 min list 2 ... 的结果始终为 2 或更小。或者,如果您按案例分解:

  • 如果count females 为0,则min list 2 count females 也为0。
  • 如果count females 为1,则min list 2 count females 也为1。
  • 如果count females 为2 或更多,则min list 2 count females 为2。

如果我理解正确的话,这就是你想要的。

【讨论】:

  • 非常感谢您的回复。我试过了(最少 2 个计数),但我有一个错误(Min excepected this input to be a list ,但得到了一个数字)。然后我搜索并找到 (min-n-of) 但也给出了错误。
  • 哎呀,更新了。 min 需要一个列表,所以要取两个数字的最小值,它是 min list a b 而不仅仅是 min a b
猜你喜欢
  • 2022-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多