gpt4 book ai didi

debugging - netlogo 中的 while 循环调试

转载 作者:行者123 更新时间:2023-12-02 23:36:32 24 4
gpt4 key购买 nike

我无法理解网络 Logo 项目中两个连续 while 循环出现的错误。

;;;;;;;;;;;;;;;;;;;;;;;;
;;; Global variables ;;;
;;;;;;;;;;;;;;;;;;;;;;;;

globals [it]

;;;;;;;;;;;;;;;;;;;;;;
;;; Breedin agents ;;;
;;;;;;;;;;;;;;;;;;;;;;

breed [houses house]
breed [firms firm]

;;;;;;;;;;;;;
;;; Setup ;;;
;;;;;;;;;;;;;

to setup
clear-all
reset-ticks
create-firms F
create-houses H

;; sets position of the firms in the space for better visualisation

set it 0
while [it < F ]
[ask firm it [
set color yellow
set heading it * 360 / F
fd 5
]
set it it + 1
]

;; sets position of the households in the space for better visualisation

set it 0
while [it < H ]
[ask house it [
set color yellow
set heading it * 360 / H
fd 15
]
set it it + 1
]

当我运行上面的代码时,我收到一条错误消息

firm 0 is not a HOUSE
error while observer running HOUSE
called by procedure SETUP
called by Button 'Setup'

指向代码中的house it

请注意,当我只运行第一个 while 循环时,一切正常。

我想在 net Logo 中使用 while 循环时有些东西我不明白。

  • 为什么第二个 while 循环似乎认为我正在给公司打电话,尽管我要求给房屋打电话?
  • 是否有更好的方法在 net logo 中实现 while 循环?

非常感谢您的帮助

最佳答案

发生了什么事?

who NetLogo 中的编号对于所有海龟来说都是按照相同的顺序分配的,与它们的品种无关。如果您这样做:

create-firms 1
create-houses 1

然后你将有firm 0house 1 ,您也可以将其称为海龟。例如,在命令中心:

observer> show turtle 0
observer: (firm 0)
observer> show turtle 1
observer: (house 1)

拥有这样的唯一标识符是有意义的,因为海龟的品种是一个非常短暂的事情。可以更改:

observer> ask firm 0 [ set breed houses ]
observer> show turtle 0
observer: (house 0)

乌龟保留了它的who尽管品种发生了变化,但数量仍然如此。

Why does the second while loop seem to consider that I am calling firms although I asked to call houses?

你正在做的事情相当于采取 turtle 0 (这是一家公司)并试图将其类型转换成一所房子。这就是为什么 NetLogo 提示 firm 0不是房子。

更好的方法

Is there a better way to implement while loops in net logo?

是:不要使用while !更严重的是:使用 while通常是不必要的。通常有更好的方法来做事。

此外,一般来说,不要使用 who任何事物的数字。如果您想同时处理许多海龟,请使用海龟套装。如果您想跟踪乌龟,请直接存储对它的引用(例如 set my-firm one-of firms )。如果您发现自己想使用 who数字,退一步重新思考你的问题:几乎肯定还有另一种方法。

你想对你所有的房子做点什么吗?只要问他们就可以了!

ask houses [
set color yellow
fd 15
]

但是,上面的代码片段没有解决一件事:您对均匀分布的海龟的要求。但是您可以通过使用 create-ordered-<breeds> 来实现这一点创造你的海龟。有了它,您的整个setup变成:

clear-all
create-ordered-firms F [
set color yellow
fd 15
]
create-ordered-houses H [
set color yellow
fd 15
]

但是如果您确实需要索引怎么办?

但是如果create-ordered-<breeds>呢?不存在,或者如果您想做类似的事情需要某种索引?你仍然不需要while : foreach 的组合和 n-values 将带您到达那里:

create-houses H
(foreach sort houses n-values H [ ? * 360 / H ] [
ask ?1 [
set color yellow
set heading ?2
fd 15
]
])

(我们使用 sort houses 海龟集转换为列表,但如果您希望它们重新洗牌,则 [self] of houses 也可以工作。)

该代码乍一看可能很奇怪,但它实际上是 NetLogo 中非常常见且有用的模式。它将您想要操作的事物与您想要分配给它的索引“压缩”在一起。与 while 循环相比,它还有一些很好的优点:没有“减一”错误,没有额外的变量,也没有忘记增加计数器或意外改变计数器的风险。

关于debugging - netlogo 中的 while 循环调试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25577290/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com