Netlogo:列表、项目和 -1 的运行时错误

Netlogo: runtime-error with list, item, and -1

我在 netlogo 中有一个相当具体的错误,我已经盯着它看了一段时间了。希望大家多多指教。

错误出现在一个代码中,该代码在名为 'strategy' 的列表中进行回顾。如果列表长于投资时间变量 'REfocus' 和 'PRICE' 将设置为特定值。如果列表不长于投资时间,则不设置变量(因此保持为 0)。

该代码由一个函数 strategy_actions 和一个报告程序 investment_time 组成。投资时间约为 3 年,但由于滴答以月为单位,因此投资时间重新调整为数月。在 strategy_actions 中,投资时间缩减为数年,因为策略列表中的每个条目也是年度的。 (缩放和重新缩放似乎是任意的,但是由于代码的其他部分大量使用了投资时间,所以这样做更有意义)。目标是从 x 时间(等于投资时间)开始采取策略。

代码(错误如下):

to strategy_actions
  set_ROI
  start_supply?
  if current_strategy != 0
  [
    let it (investment_time / 12)
    ifelse it >= length strategy
    [
      set REfocus 0
    ]
    [
      if item (it - 1) strategy  = 1
      [
        if supply? = true [set_PRICE (set_discrete_distribution 0.29 0.19 0.29 0.15 0.07 0 0) (set_discrete_distribution 0.14 0.12 0.25 0.25 0.25 0 0)]
        ifelse any? ids [set REfocus mean [mot_RE] of ids][set REfocus set_discrete_distribution 0.07 0.03 0.07 0.17 0.66 0 0]
      ]
      if item (it - 1) strategy = 2
      [
        if supply? = true [set_PRICE (set_discrete_distribution 0.27 0.21 0.32 0.11 0.09 0 0) (set_discrete_distribution 0.15 0.11 0.22 0.30 0.23 0 0)]
        ifelse any? prods [set REfocus mean [mot_RE] of prods][set REfocus set_discrete_distribution 0.12 0.03 0.10 0.18 0.57 0 0]
      ]
      if item (it - 1) strategy = 3
      [
        if supply? = true [set_PRICE (set_discrete_distribution 0.26 0.22 0.26 0.18 0.09 0 0) (set_discrete_distribution 0.07 0.08 0.19 0.30 0.35 0 0)]
        ifelse any? cons[set REfocus mean [mot_RE] of cons][set REfocus set_discrete_distribution 0.08 0.06 0.15 0.27 0.45 0 0]
      ]
    ]
  set RE_history fput REfocus RE_history
  ]
end

to-report investment_time
  report ((random-normal 3 1) * 12)  ;approximately 3 years investment time
end

不知何故,我在行为空间实验中有时会遇到这个运行时错误:

-1 isn't greater than or equal to zero. error while observer running ITEM called by procedure STRATEGY_ACTIONS called by procedure SET_MEETING_ACTIONS called by procedure GO

有谁知道导致此错误的原因吗? 你会帮我很多忙!

干杯,

玛丽亚

似乎 investment_time 偶尔会以零出现,因此您要求策略列表的 item (0 - 1)。我对 item 进行了一些尝试,并了解到(令我惊讶的是)item (0.0001 - 1) strategy 工作正常,尽管参数为负,但仍产生列表中的第 0 项。但是 item (0 - 1) strategy 确实给出了您引用的错误。显然,大于 -1 的项目编号被解释为零。实际上 item 似乎截断了任何小数参数而不是四舍五入。例如,item 0.9 被解释为 item 0item -0.9

这可能值得放入文档中。

HTH, 查尔斯