获取所有海龟死亡时的平均年龄 [英] Get the mean age of all turtles on the time of death

查看:129
本文介绍了获取所有海龟死亡时的平均年龄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得所有垂死海龟的平均年龄.我尝试通过以下代码来实现这一点:

I want to get the mean age of all dying turtles. I try to achieve that with the following code snipped:

globals [mean-death-age]
turtles-own [age prob-die?]

to-report mean-age
  if (prob-die? = true) [
    set mean-death-age mean [age] of turtles
  ]
  report mean-death-age
end

每只乌龟都有死亡的概率(概率死亡),应该在每个时间步长(刻度)上重新计算一次.如果有机会死?设置为true平均死亡年龄应进行更新.目前,我遇到的问题是

Every turtle has the probability of dying (prob-die?) which should be calculated new with every time step (tick). If prob-die? is set to true mean-death-age should be updated. At the moment I have the problem that

  • 我不知道如何启动可变均值-死亡年龄.也许,使用if循环会有所帮助

  • I don't know how to initiate the variable mean-death-age. Maybe, working with an if loop would help

if (ticks >= 1) [    
  if (prob-die? = true) [
    set mean-death-age mean [age] of turtles
  ]
]

  • 我没有获得已经计算出的平均死亡年龄的更新,但是变量被覆盖,并且返回了此the的海龟的平均死亡年龄

  • I don't get an update of the already calculated mean-death-age but the variable is overwritten and the mean death age of the turtles of this tick is returned

    我在获取值时遇到问题.当我尝试用例如

    I have problems accesing the value. When I try to call it with e.g.

    print mean-age
    

    即使我试图将其保存为全局变量,我也会得到一条错误消息,意思是平均年龄仅是乌龟.

    I get the error message that mean-age is turtle-only even though I tried to save is as a global variable.

    完整代码可在此处.

    推荐答案

    我认为您正在使它变得比所需的更加复杂.最简单的方法是只保留海龟死亡的年龄列表,然后取该列表的平均值:

    I think you're making this more complicated than it needs to be. The easiest approach would be to just keep a list of ages at which turtles die, and then take the mean of that list:

    globals [death-ages]
    turtles-own [age]
    
    to setup
      clear-all
      create-turtles 100 [
        setxy random-xcor random-ycor
        set age random 100
      ]
      set death-ages [] ; start with an empty list
      reset-ticks
    end
    
    to go
      if not any? turtles [ stop ]
      ask turtles [
        if random 100 < age [
          set death-ages lput age death-ages ; add age to our list of ages of death
          die
        ]
      ]
      print mean death-ages
      tick
    end
    

    唯一的缺点是,随着模型的运行,death-ages的列表将保持增长.如果事实证明这是个问题(尽管可能不会),则您需要跟踪当前平均值和当前观测值,并使用set avg ((n * avg) + age) / (n + 1) set n n + 1之类的值更新平均值.

    The only downside is that your list of death-ages is going to keep growing as your model runs. If that turns out the be a problem (though it probably won't), you will need to keep track of the current average and the current count of observations and update your average with something like set avg ((n * avg) + age) / (n + 1) set n n + 1.

    关于为什么您当前的代码不起作用的原因,有很多解压缩工作来解释这一切(很抱歉,我现在不能这样做).作为一般性评论,我建议尝试在遇到此类困难时退后一步,并考虑解决问题所需的最少信息量.在这种情况下,您需要指平均死亡年龄.最简单的方法是什么?保留死亡年龄列表,并在需要时取列表的平均值.

    As for why your current code doesn't work, there would be a lot of unpacking to do to explain it all (I'm sorry I can't now). As a general comment, I would suggest trying to take a step back when you run into difficulties like this and think about the minimum amount of information that you need to solve the problem. In this case, you need to mean age of death. What the simplest way to get that? Keep a list of ages of death and take the mean of that list when needed­.

    这篇关于获取所有海龟死亡时的平均年龄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆