【问题标题】:Netlogo: How can I obtain the accumulate value in Netlogo?Netlogo:如何获取Netlogo中的累积值?
【发布时间】:2017-10-20 14:53:43
【问题描述】:

我已经确认了the link的信息。 但是我不能应用这个。 以下是示例模型程序的一部分。 我想累积海龟的数量,但是我无法使用示例程序来累积它。 我可能需要你的建议。谢谢。

globals [num-turtles cumulative-sum average-time number-dead ]
turtles-own [count-up]

to setup
  clear-all
  set num-turtles 5
  reset-ticks
end

to go

  if count turtles < num-turtles
  [ ask patch 0 0
    [ sprout 1
      [ set count-up 0 ]
    ]
  ]

  set cumulative-sum cumulative-sum + 1 ;I would like to calculate the integral value here, but this syntax is not a cumulative value. 
  ask (turtles-on patch 0 0)
    [
      set cumulative-sum count turtles-here
    ]

  set average-time ifelse-value (number-dead = 0)
  [ 0 ][(cumulative-sum) / (number-dead)]

  if (count turtles > 0) [
    ask min-one-of turtles [who] [
      if count-up >= 6 [
        set number-dead number-dead + 1
        die
      ]
    ]
  ]

  ask (turtles-on patch 0 0)
  [ set count-up count-up + 1
  ]

  tick
end

【问题讨论】:

  • Goodgest- 您可以尝试将其重新格式化为Minimal, Complete, and Verifiable example? 此外,您能否澄清您的问题?您是否尝试在现有的黄海龟数量中添加任何新的黄海龟?
  • 不幸的是,该代码有问题-它缺少变量(例如number-of-turtles)并且具有cd之类的变量-但cd已经是Netlogo中的原语。真正的 MCVE 是您已将其缩减为隔离问题所需的绝对最少代码。这意味着尽可能多地删减不相关的代码,而且通常更容易从头开始制作一个复制您试图解决的问题的玩具模型。确实,您需要一小段代码,用户只需复制、粘贴和运行删除滑块等即可进行简化。
  • LukeC @,你好,cumulative-sum的意思是每个tick中活龟的数量,所有tick执行的乌龟数量的累加结果。例如,如果有一只乌龟生活在第一个滴答声中,那么累积和就是一个。如果有三只海龟生活在第二个滴答上,那么累积和是 1 + 3 = 4。如果有两只乌龟生活在第三个滴答上,那么累积和是 1 + 3 + 2 = 6。平均时间的含义是累积和除以死数的结果。这是乌龟活着的平均时间。 Number-dead 是可以的,并且是累积值。
  • LukeC @ 非常感谢。我可以用你的样本模型安全地积累在测试模型中。在实际模型中,我使用语法“ask (turtles - on patch 0 0) [set count-up count turtles-here with [color = yellow]]”。 "turtles-own [count-up]" 此设置是必需的。由于我想使用“行为空间”,我将在全局变量中注册“计数”。所以有一些错误,但我将使用“count-turtles”计算积分值。对于错误处理,我可以安排一些公式。谢谢!

标签: netlogo cumulative-sum integrated


【解决方案1】:

这好多了,谢谢- 我现在可以运行代码没问题了。但是,我仍然不认为我理解您想要 cumulative-sum 实际计算的内容。你只是在寻找海龟的总数,包括那些还活着的和已经死去的?如果是这样,我认为这只是移动您的set cumulative-sum cumulative-sum + 1 行的问题。例如:

编辑:

好的,我想我现在从您的评论中明白了。试试这个:

globals [num-turtles cumulative-sum average-time number-dead ]
turtles-own [count-up]

to setup
  clear-all
  set num-turtles 5
  reset-ticks
end

to go

  if count turtles < num-turtles [
    ask patch 0 0 [
      sprout 1 [
        set count-up 0
      ]
    ]
  ] 

  if (count turtles > 0) [
    ask min-one-of turtles [who] [
      if count-up >= 6 [
        set number-dead number-dead + 1
        die
      ]
    ]
  ]

  ask turtles-on patch 0 0 [
    set count-up count-up + 1
  ]

  set cumulative-sum cumulative-sum + count turtles
  set average-time ifelse-value (number-dead = 0) [0]  [(cumulative-sum) / (number-dead)]

  tick
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-27
    相关资源
    最近更新 更多