如何使用补丁来计算 Netlogo 购物模型中的周数?
我必须创建一个 Netlogo 世界,其中 N 个绿色块用于家庭,M 个蓝色块用于超市。在世界上,海龟从家(绿色区域)到超市(蓝色区域),然后再回到家。我在下面发布了我的代码。
I have to create a Netlogo-world with N green patches for households and M blue patches for supermarkets. In the world turtles go from home (green patch) to the supermarket (blue patch) and back home again. I posted my code of this below.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么要让
week
变量成为补丁变量?换句话说,为什么需要补丁来测量时间?我认为您应该考虑其他两个选项:
ticks
全局变量,但您可以创建自己的全局变量,每个代理都会能够阅读它。例如,如果据我了解,您希望每只乌龟每 70 个刻度访问一次他们选择的商店,您可以这样做。
让我们探讨一下我在这里实现的主要内容:
go
的大多数迭代中,每只海龟要么执行shop 或执行
return
。然而,你的go
是:当我去实现倒计时时,这造成了一些混乱,所以我想用一个
phase
海龟自己的变量来清楚地分割海龟的活动:值为 1 意味着海龟必须执行shop
;值为 2 意味着海龟必须执行return
。phase
的值在setup
时和每次乌龟回家时设置为1,每次乌龟到达商店时设置为2。正如您所看到的,我从模型中删除了
stay
因为它使海龟什么也不做。倒计时
变量,在shop
开始时检查。如果countdown
达到0,那么乌龟实际上会向商店移动。countdown
的值在设置
时保留默认值 0,这意味着一开始每只海龟都会朝商店移动。每次访问商店后返回家时,countdown
的值设置为 70。在go
的每次迭代中,所有倒计时大于零的海龟都会带上它下降了一位。请注意,我对代码进行了一些其他细微更改,其中一些是:
setup
过程中删除了set storeVisited 0
。 NetLogo中变量的默认值为0,所以不需要一开始就把它们设置为0。go
结束时,您使用的是reset-ticks
而不是tick
。shop
中,我将if (pcolor = blue)
替换为if (patch-here = target)
。这是因为,在前往目标
的途中,海龟可能会发现自己位于一块不是其目标的蓝色斑块上。Why do you want the
week
variable to be a patch variable? In other words, why do you want patches to measure time?I think you should consider two other options:
ticks
global variable, but you can create your own global variable and every agent will be able to read it.For example, if as I understood you want each turtle to visit their chosen shop every 70 ticks, you can do something like.
Let's explore the main things I implemented here:
go
, each turtle either executesshop
or executesreturn
. However, yourgo
was:This was creating some mess as I went to implement a countdown, so I thought to clearly split turtles' activities with a
phase
turtles-own variable: a value of 1 means that turtles will have to executeshop
; a value of 2 means that turtles will have to executereturn
. The value ofphase
is set as 1 uponsetup
and every time a turtle returns home, and it is set as 2 every time a turtle reaches the shop.As you can see, I removed
stay
from the model because it made turtles do nothing.countdown
variable, that is checked at the beginning ofshop
. Ifcountdown
reached the value of 0, then the turtle actually moves towards the shop. The value ofcountdown
is left at its default of 0 uponsetup
, which means that at the beginning every turtle will start by moving towards the shop. Upon returning home after each visit to the shop, the value ofcountdown
is set to 70. At every iteration ofgo
, all turtles whose countdown is greater than zero will bring it down by one.Note that I implemented some other minor changes to the code, some of them being:
set storeVisited 0
from within thesetup
procedure. The default value of variables is 0 in NetLogo, so there is no need to set them to 0 at the beginning.go
, you were usingreset-ticks
instead oftick
.shop
, I substitutedif (pcolor = blue)
withif (patch-here = target)
. This is because, on the way totarget
, a turtle might find itself on a blue patch which is not its target.