如何检查两个不同海龟品种的变量是否匹配?
我正在尝试根据共享变量将乌龟移向它的后代。在我最初的代码中,我要求具有展示但没有凉亭的雄性(品种)通过孵化来建立凉亭(不同品种)。我使用 create-link-with myself 来让雄性和凉亭共享一个名为 Bird-id 的变量。
to establish-bower
ask males[
if has-bower? = false[
if has-display? = true[
hatch 1 [
set breed bowers
set color yellow
set bower-id who
set shape "house"
create-link-with myself
]
]
]
]
end
在我的代码的第二部分中,我要求拥有凉亭和展示台的雄性返回凉亭。如果它们与自己的凉亭的距离大于 0,则雄性面向该凉亭并向前移动 1。如果距离为 0,则调整其他两个变量和能量,并且雄性会稍微随机移动。
to return-to-bower
ask males[
if has-bower? = true[
if has-display? = true[
(ifelse
distance one-of bowers with [bird-id = (bird-id of myself)] > 0[
face one-of bowers with [bird-id = (bird-id of myself)]
fd 1]
distance one-of bowers with [bird-id = (bird-id of myself)] = 0[
set has-display? false
set nr-display nr-display + 1
rt random 50
lt random 50
fd 1
set energy energy - 3])
]
]
]
end
我在编码识别凉亭属于制造它的男性的部分时遇到了困难。到目前为止,
bowers with [bird-id = (bird-id of myself)]
这显然不起作用,但它传达了我想做的事情。
我该如何写这部分?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你的
bird-id
没有按预期在你的雄性和凉亭之间共享 - 在external-bower
中你有一行设置 Bower-id who
,但其中的who
应该在凉亭的上下文中,而不是男性的上下文中。因此,male 0 可能会要求 Bower 4 将其 Bird id 设置为 4,而不是 0。如果您想使用该选项(这当然是可行的),我认为您需要更改为set Bower-id [who] of myself
- 这反映了您对create-link-with myself
行所做的操作。但是,如果您已经在使用链接,您可能需要使用现有链接的替代方案。
other-end
原语返回代理在提问海龟拥有的链接的另一端(在该链接中有更好的解释)。因此,只要让乌龟弄清楚哪个凉亭是它自己的,就让它查询关联链接。例如:我在上面使用
let
来让bowers
仅由males
临时检查 - 您也可以通过使用males-own
变量类似于my-bower
并将凉亭直接分配给该变量,然后雄性将永久跟踪它。I think your
bird-id
is not being shared as expected between your males and bowers- inestablish-bower
you have the lineset bower-id who
, but thewho
there should be in the context of the bower instead of the context of the male. So, male 0 may ask bower 4 to set its bird id to 4, rather than 0. If you want to use that option (which is certainly doable), I think you'd need to alter toset bower-id [who] of myself
- this mirrors what you did with thecreate-link-with myself
line.However, if you are already using links, you might like an alternative that makes use of the link already in place. The
other-end
the primitive returns the agent on the other end of the link owned by the asking turtle (better explained in that link). So whenever it makes sense to have the turtle figure out which bower is its own, have it query the associate link. For example:I use
let
in the above to have thebowers
only be temporarily checked by themales
- you could also simplify this by having amales-own
variable likemy-bower
and assign the bower directly to that variable and then the males would permanently track it.