在带有孵化器的不同品种的 NetLogo 中绘制
Plot in NetLogo of different breeds with hatch
我在绘制品种方面遇到了一些麻烦。
具体来说,我有三个品种,sellers, buyers
和 cars
。
品种之间的关系如下所示:
if breed = buyers [
hatch-cars 1
[ set attribute_1 random-float 1
...]
]
if breed = sellers [
hatch-cars 1
[set attribute_2 random-float 1
...]
我想根据品种绘制汽车,以便有两条不同的线,一条用于买家,一条用于卖家。
我试过
ask cars with [breed = buyers ][plotxy attribute_1 ticks]
ask cars with [breed = sellers ] [plotxy attribute_2 ticks]
但它没有绘制任何东西,我也没有收到任何错误消息。
然后,我尝试
ask cars [plotxy attribute_1 ticks]
ask cars [plotxy attribute_2 ticks]
差不多就好了。但是,它还绘制了其他点:它似乎考虑了所有品种,buyers
、sellers
和 cars
。
如果我写
if breed = buyers [ask cars [plotxy attribute_1 ticks]]
if breed = sellers [ask cars [plotxy attribute_2 ticks]]
我收到以下错误消息:You can't use BREED in an observer context, because BREED is turtle/link-only
.
我该如何解决这个问题?
感谢您的帮助。
让我们先看看为什么您的第一次尝试没有绘制任何东西:
ask cars with [breed = buyers ][plotxy attribute_1 ticks]
ask cars with [breed = sellers ] [plotxy attribute_2 ticks]
当您在 NetLogo 中声明一个品种时,它会创建一个与该品种同名的特殊代理集。如果你写:
breed [ buyers buyer ]
breed [ sellers seller ]
breed [ cars car ]
NetLogo 创建了三个特殊代理集:buyers
、sellers
和 cars
。这些代理集中的每一个都只包含来自相应品种的海龟。还有特殊的 turtles
代理集,其中包含模型中的 所有 海龟,无论它们的品种如何。
除此之外,所有海龟都有一个 breed
变量,指的是它们所属的品种代理集。每只海龟可以是 "unbreeded" 并且具有 turtles
作为它们的 breed
变量的值,或者属于一个(但不超过一个)品种并且具有该品种的代理集作为它们的值breed
变量。
关于 breed
变量需要了解的一件事是 您通常不需要使用它。 写作:
ask turtles with [ breed = cars ] [ do-something ]
与写法效果相同:
ask cars [ do-something ]
除了 ask cars
版本比 ask turtles with ...
版本快得多(并且更易读)。
我们现在可以看出 ask cars with [breed = buyers ]
有什么问题吗?根据定义,所有汽车的 breed
变量的值为 cars
,因此 breed = buyers
条件始终为假。这就是该语句什么都不做的原因。 (而且它也没有理由给你一个错误信息。这就像在没有红海龟的情况下要求turtles with [ color = red ]
做某事:什么都没有发生,但是要求没有错。)
现在让我们看看你的第二次尝试:
ask cars [plotxy attribute_1 ticks]
ask cars [plotxy attribute_2 ticks]
你说的是 "it seems that it considers all the breeds",但根据我们对品种运作方式的理解,你应该能够看出它有什么问题,不可能是那样的。据我所知,它应该有效。
我的猜测是您在不知不觉中在某处制造了额外的汽车。或者可能是其他原因。根据您提供的信息我们无法判断。
(一个小的旁注:习惯上在 x
轴上放置刻度,但您将它们绘制在 y
轴上。)
最后,您的最后一次尝试是:
if breed = buyers [ask cars [plotxy attribute_1 ticks]]
if breed = sellers [ask cars [plotxy attribute_2 ticks]]
我们已经看到 breed
是一个 turtle 变量,所以它只能在特定 turtle 的上下文中访问,这就是为什么你会收到关于尝试在观察者上下文(即尝试在 turtle 上下文之外使用它)。
总而言之,我无法为您的问题提供直接的解决方案,但我希望更好地了解品种的工作原理将有助于您找出问题所在。
也许要澄清的最后一件事是说这样的话:
ask one-of sellers [ hatch-cars 1 ]
不会在新孵化的汽车和 sellers
品种之间建立任何关系。这辆车不知道它被卖家孵化了。除非您将此信息保存在某个地方的另一个变量中,否则无法查询 NetLogo 的 "all the cars that have been hatched by sellers"。但是如何做到这一点是另一个问题(如果需要,欢迎您在本网站上提问)。
我在绘制品种方面遇到了一些麻烦。
具体来说,我有三个品种,sellers, buyers
和 cars
。
品种之间的关系如下所示:
if breed = buyers [
hatch-cars 1
[ set attribute_1 random-float 1
...]
]
if breed = sellers [
hatch-cars 1
[set attribute_2 random-float 1
...]
我想根据品种绘制汽车,以便有两条不同的线,一条用于买家,一条用于卖家。 我试过
ask cars with [breed = buyers ][plotxy attribute_1 ticks]
ask cars with [breed = sellers ] [plotxy attribute_2 ticks]
但它没有绘制任何东西,我也没有收到任何错误消息。 然后,我尝试
ask cars [plotxy attribute_1 ticks]
ask cars [plotxy attribute_2 ticks]
差不多就好了。但是,它还绘制了其他点:它似乎考虑了所有品种,buyers
、sellers
和 cars
。
如果我写
if breed = buyers [ask cars [plotxy attribute_1 ticks]]
if breed = sellers [ask cars [plotxy attribute_2 ticks]]
我收到以下错误消息:You can't use BREED in an observer context, because BREED is turtle/link-only
.
我该如何解决这个问题?
感谢您的帮助。
让我们先看看为什么您的第一次尝试没有绘制任何东西:
ask cars with [breed = buyers ][plotxy attribute_1 ticks]
ask cars with [breed = sellers ] [plotxy attribute_2 ticks]
当您在 NetLogo 中声明一个品种时,它会创建一个与该品种同名的特殊代理集。如果你写:
breed [ buyers buyer ]
breed [ sellers seller ]
breed [ cars car ]
NetLogo 创建了三个特殊代理集:buyers
、sellers
和 cars
。这些代理集中的每一个都只包含来自相应品种的海龟。还有特殊的 turtles
代理集,其中包含模型中的 所有 海龟,无论它们的品种如何。
除此之外,所有海龟都有一个 breed
变量,指的是它们所属的品种代理集。每只海龟可以是 "unbreeded" 并且具有 turtles
作为它们的 breed
变量的值,或者属于一个(但不超过一个)品种并且具有该品种的代理集作为它们的值breed
变量。
关于 breed
变量需要了解的一件事是 您通常不需要使用它。 写作:
ask turtles with [ breed = cars ] [ do-something ]
与写法效果相同:
ask cars [ do-something ]
除了 ask cars
版本比 ask turtles with ...
版本快得多(并且更易读)。
我们现在可以看出 ask cars with [breed = buyers ]
有什么问题吗?根据定义,所有汽车的 breed
变量的值为 cars
,因此 breed = buyers
条件始终为假。这就是该语句什么都不做的原因。 (而且它也没有理由给你一个错误信息。这就像在没有红海龟的情况下要求turtles with [ color = red ]
做某事:什么都没有发生,但是要求没有错。)
现在让我们看看你的第二次尝试:
ask cars [plotxy attribute_1 ticks]
ask cars [plotxy attribute_2 ticks]
你说的是 "it seems that it considers all the breeds",但根据我们对品种运作方式的理解,你应该能够看出它有什么问题,不可能是那样的。据我所知,它应该有效。
我的猜测是您在不知不觉中在某处制造了额外的汽车。或者可能是其他原因。根据您提供的信息我们无法判断。
(一个小的旁注:习惯上在 x
轴上放置刻度,但您将它们绘制在 y
轴上。)
最后,您的最后一次尝试是:
if breed = buyers [ask cars [plotxy attribute_1 ticks]]
if breed = sellers [ask cars [plotxy attribute_2 ticks]]
我们已经看到 breed
是一个 turtle 变量,所以它只能在特定 turtle 的上下文中访问,这就是为什么你会收到关于尝试在观察者上下文(即尝试在 turtle 上下文之外使用它)。
总而言之,我无法为您的问题提供直接的解决方案,但我希望更好地了解品种的工作原理将有助于您找出问题所在。
也许要澄清的最后一件事是说这样的话:
ask one-of sellers [ hatch-cars 1 ]
不会在新孵化的汽车和 sellers
品种之间建立任何关系。这辆车不知道它被卖家孵化了。除非您将此信息保存在某个地方的另一个变量中,否则无法查询 NetLogo 的 "all the cars that have been hatched by sellers"。但是如何做到这一点是另一个问题(如果需要,欢迎您在本网站上提问)。