如何在条件下设置与另一个代理的链接的颜色

问题描述 投票:1回答:1

我想改变连接一个代理(读者的品种)的链接的颜色给其他有标志的读者(TM? = true)。代码是这样的:

breed [ readers reader ]
undirected-link-breed [ rris rri ]
readers-own [ TM? ]

to start-TM [ ?reader ]
  ask ?reader [
    if any? other readers with [ TM? = true ] [
      let other-TM-readers other readers with [ TM? = true ]
      ask my-rris with [other-TM-readers] [ set color red ]
    ]
  ]
end

这会在ask my-rris with [other-TM-readers] [ set color red ]行中返回一个错误,因为with期望为true或false而不是agentset。我怎么能选择连接当前读者的rri链接和有TM? = true的读者?

问候

netlogo
1个回答
2
投票

当你写:

let other-TM-readers other readers with [ TM? = true ]

你告诉NetLogo将TM为true的所有读者的agentset分配给other-TM-readers变量。所以当你写下:

ask my-rris with [other-TM-readers] [ set color red ]

你正在将other-TM-readers agentset传递给with,就像NetLogo在其错误消息中所说的那样。

这很容易解决,但首先,评论:写= true几乎总是多余的。你的TM?变量已经是一个布尔值,所以你可以通过编写with [ TM? ]而不是with [ TM? = true ]直接检查它。

现在,要修复错误,您可以编写:

let other-TM-readers other readers with [ TM? ]
ask other-TM-readers [
  ask rri-with myself [ set color red ]
]

要不就:

ask other readers with [ TM? ] [
  ask rri-with myself [ set color red ]
]

您也可以直接询问链接,并使用other-end原语检查邻居的颜色:

ask my-rris with [ [ TM? ] of other-end ] [ set color red ]

最后一个版本也比以前的版本更安全,因为它不假设调用者和其他读者之间存在链接:它只会询问实际存在的链接。 (前两个版本可以检查rri-with myself != nobody,但那不太优雅。)

© www.soinside.com 2019 - 2024. All rights reserved.