【问题标题】:netlogo error cannot find element x in list of length xnetlogo 错误在长度 x 的列表中找不到元素 x
【发布时间】:2017-02-16 03:44:54
【问题描述】:

以下过程旨在从众多海龟中选择一个“赞助人”并将其颜色更改为白色。选择是基于海龟的财富的概率。

运行此代码,特别是 item i ranking-in-radius 行我收到类似这样的错误消息:“cannot find element 43 in list...(shows list of turtles' wealth)... which is only of length 43.”我知道我收到此错误是因为列表从 0 开始,因此 43 个元素的列表从 0 开始到 42,但我不明白为什么我写的代码不正确。我的i0 开始,如果i 不是< 而不是查询的海龟数量(即应该停止在42),则程序停止。任何人都可以提出解决方案吗?谢谢!

to choose-patron
  let i 0
  let patrons-in-radius count turtles in-radius radius with [wealth >= 80]
  let ranking-in-radius sort-by > [wealth] of turtles in-radius radius with  [wealth >= 80]
  let total-wealth-in-radius sum [wealth] of turtles in-radius radius with [wealth >= 80]
  while [i < (patrons-in-radius)][
    ask turtles in-radius radius with [wealth >= 80] [
      if [wealth] of self = item i ranking-in-radius [   
        if random 100 < ((wealth / total-wealth-in-radius) * 100) [
          set color white
          if any? turtles in-radius radius with [color = white] [stop]
      ]
      set i (i + 1)
    ]
  ]
]
end

【问题讨论】:

    标签: netlogo


    【解决方案1】:

    您的 set i (i + 1) 似乎在错误的区块中。现在,每次运行 if [wealth]... 时,i 都会增加,但您应该只在 while [i &lt; (patrons-in-radius)] 块的末尾增加 i

    尝试使用以下代码替换您的 while 语句:

      while [ i < (patrons-in-radius) ] [
        ask turtles in-radius radius with [ wealth >= 80 ] [
          if [ wealth ] of self = item i ranking-in-radius [   
            if random 100 < ( (wealth / total-wealth-in-radius) * 100) [
              set color white
              if any? turtles in-radius radius with [color = white] [
                stop
              ]
            ]
          ]
        ]
        set i i + 1
      ]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-15
      • 1970-01-01
      • 2021-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-06
      相关资源
      最近更新 更多