【问题标题】:Netlogo: change turtle color from set of list with conditionNetlogo:根据条件从一组列表中更改海龟颜色
【发布时间】:2015-12-14 11:53:14
【问题描述】:

我有这组代码。 代理是“消费者”,它具有变量“值类型”。我正在尝试将白龟更改为“值类型”列表中的一种颜色。但我不断收到“预期为常数”的错误消息。

to develop-needs
  if ticks mod 5 = 0 [
    ask consumers [set type-of-value  (list blue red green)] 
    let a count consumers with [color = white]
    if any? consumers with [color = white]
      [ set color one-of type-of-value ]
        ask turtles with [ color = one-of type-of-value ]
      ]]
end

任何帮助将不胜感激。

谢谢

【问题讨论】:

  • 这里的缩进令人困惑——ask consumers 是在 if ticks 中,还是不是? let a 是在 ask consumers 里面,还是不是?如果正确缩进,您的代码会更容易阅读并提供反馈。 (NetLogo 会为您完成 - 选择代码并按 Tab 键。)
  • 编辑缩进。是的,如果滴答作响的话,里面的消费者。

标签: list colors netlogo any


【解决方案1】:

这段代码终于可以工作了。 感谢 JenB,我注意到了我的错误。

to develop-needs
  if ticks mod 5 = 0 [
    ask consumers [set type-of-value  (list blue red green)] 
    let a count consumers with [color = white]
    if any? consumers with [color = white]
      [ ask n-of (random a ) consumers with [color = white]
        [set color one-of type-of-value
         set value? true]
         ]]
end

【讨论】:

    【解决方案2】:

    你还没有告诉它要选择多少。也就是说,如果你想从名为 a 的代理集中选择 5 个消费者,ask n-of a 需要说(例如)ask n-of 5 a

    顺便说一句,您的代码中也存在逻辑错误:

    [ set color one-of type-of-value ]
      ask turtles with [ color = one-of type-of-value ]
    

    如果值的类型是 3 个项目的列表(例如,代码中的 [蓝绿红]),则第一行中的 set color one-of 将选择其中一种颜色,但第二行 one-of ... 将做新的随机选择。我不确定您要实现什么,因此我无法提供代码,但您可能希望选择一种颜色并将其分配给一个变量,然后与该变量进行比较,而不是进行进一步的选择。

    【讨论】:

    • 是的,你是对的。我尝试将随机数的白龟(如果有)更改为列表中的一种颜色并将其分配给变量。我编辑了代码。但仍然返回错误消息
    • 我根据您的提示重新编辑了代码。有用。谢谢。
    猜你喜欢
    • 2013-09-19
    • 1970-01-01
    • 2020-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多