NetLogo 错误:距离预期输入是代理但没有人代替

问题描述

Netlogo 中出现错误代码如下。我想要做的是让每只乌龟检查最近的乌龟并计算自己(乌龟)和最近的乌龟之间的距离。

如果距离小于8,乌龟不满意,否则乌龟满意。不满足的乌龟死了。

有时,周围只有一只乌龟,所以“最近”的乌龟是无名乌龟。它无法计算到不存在的海龟的距离。我明白出了什么问题,我试图用一段代码解决它,如果周围没有人,将海龟的满意度设置为 0(真)。但我仍然收到消息:距离预期输入是代理但没有人代替..

你能看出哪里出了问题吗?

ask turtles [ let nearest min-one-of turtles [ distance myself ] 
    ifelse nearest = nobody 
    [ set satisfaction 1 ]
    [ ifelse distance nearest < 8 [ set satisfaction 0] [ set satisfaction 1 ] ] ]

解决方法

寻找最近的其他海龟是 NetLogo 中一个常见且令人惊讶的棘手问题;我们需要一个原始的“最近的”来做到这一点。同时,我们将在Sect 中介绍它。 our textbook 的 11.2。

你有两个问题:

第一个“min-one-of turtles [distance own]”将始终产生零距离,因为最近的海龟总是执行语句的海龟。你需要“min-one-of(其他海龟)[距离我自己]”

其次,我不清楚您为什么会收到错误,但您可以通过将整个语句包含在 if 语句中来防止它:

问海龟[如果有?其他海龟 [让最近的...

,

请编辑您的问题,而不是提供跟进作为答案。

问题在于您混淆了满意度值。您在问题文本中声明 0 表示满意,但如果 <?php $json = file_get_contents('http://api.ip2proxy.com/?ip=8.8.8.8&key=demo&package=PX10'); $arr = json_decode($json,true); if ($arr['response'] == 'OK') { if ($arr['proxyType'] == 'VPN') { echo "IP address is a VPN.\n"; } else { echo "IP address is not a VPN.\n"; } } else { echo "Error.\n"; } ?> (即存在另一个代理),则分配 1。因此,当 nearest != nobody 为假或最近的实际上无人时,您的代码会跳转到距离计算块。所有这些都可以通过使用 nearest != nobodytrue 来避免,这将使您的代码更易于阅读且不易出错。这不是必需的,但 NetLogo 约定有一个 ?在用于布尔 (true/false) 变量的变量名称的末尾。

因此,重写您的代码,使其可以独立运行并抛弃 0/1,丢弃否定测试(令人困惑)并反转最终分配,它看起来像这样:

false

我还重新格式化了代码,以便您可以查看 turtles-own [satisfied?] to testme clear-all create-turtles 20 [ setxy random-xcor random-ycor ] ask turtles [ if any? other turtles [ let nearest min-one-of (other turtles) [distance myself] ifelse nearest = nobody [ set satisfied? true ] [ ifelse distance nearest < 8 [ set satisfied? false ] [ set satisfied? true ] ] ] ] type "satisfied turtles: " print count turtles with [satisfied?] end 结构的运行位置。现在也清楚了,没有赋值给满意?如果只有一只海龟,则该值将保持为默认值 0。

所以更好的版本应该是这样的:

ifelse

这可以在单个语句中完成(我用积极而非消极的方式表达了它,因为这样读起来更自然,所以另请参阅新的 ask turtles [ ifelse any? other turtles [ let nearest min-one-of (other turtles) [distance myself] ifelse distance nearest < 8 [ set satisfied? false ] [ set satisfied? true ] ] [ set satisfied? true ] ] 语句):

not

您需要对子句进行排序,以便它首先检查 to testme clear-all create-turtles 1 [ setxy random-xcor random-ycor ] ask turtles [ ifelse not any? other turtles or distance min-one-of (other turtles) [distance myself] > 8 [ set satisfied? true ] [ set satisfied? false ] ] type "satisfied turtles: " print count turtles with [satisfied?] end 。这应该是通过您尝试的解决方案中的嵌套 any? 实现的,除非您颠倒了测试。