如何在不初始化的情况下更新变量
How to update variables without being initialised
继我之前的post之后,我想问一下为什么当我试图更新一个变量的值时它被初始化的原因。我正在尝试找到解决此问题的方法。
我想要做的是通过添加一个小的随机量 q = random-float 0.1
来更新变量的原始值,只有当我将 this-item
添加到邻居列表时。
我的预期输出是:
(object 16) with original attribute 0.147 neigh: 0 (object 16)
with updated attribute 0.167 with random-float 0.02
即不初始化原始值:
(object 16) with original attribute 0.147 neigh: 0 (object 16)
with updated attribute 0.02 with random-float 0.02
后者是我目前的输出,这是错误的。
我不知道如何在我的代码中解决这个问题。我仍在努力了解出了什么问题,但我还没有找到解决问题的方法,所以我希望你能帮助我了解更多。
我的代码如下:
if breed = breed1 [
set selected people
hatch-items 1 [
hide-turtle
set chosen selected
set attribute1 random-float 1
set attribute2 attribute1
set this-item self
print (word " attribute1 " attribute1 " attribute2 " attribute2)
ask myself [
set my-list fput this-item my-list
] ;; close ask myself
] ;; close hatch
; ADD NEW ITEM TO NEIGHBOURS' LISTS ;
print (word " Before updating value, attribute1 " attribute1 " attribute2 " attribute2)
ask link-neighbors with [breed = breed1] [
let q random-float 0.1
set attribute1 (attribute1 + q)
print (word " After updating value, attribute1 " attribute1 " attribute2 " attribute2 " q " q)
] ;; close ask link-neighbors
] ;; close if
我添加了一些打印函数来查看输出:
第一个打印returns我attribute1 0.85 and attribute2 0.85
print (word " attribute1 " attribute1 " attribute2 " attribute2)
第二个输出returns我attribute1 0 and attribute2 0
print (word " Before updating value, attribute1 " attribute1 " attribute2 " attribute2)
第三输出returns我attribute1 0 attribute2 0 and q 0.08
print (word " After updating value, attribute1 " attribute1 " attribute2 " attribute2 " q " q)
发生的事情是 attribute1
在 ask link-neighbors
中初始化,所以我将数量 q
添加到 0
,而不是原始值属性,在影线中定义。
能不能帮我更新一下,不初始化值?
非常感谢
没有初始化。你的问题是你有两个不同的范围。
您在孵化块中设置的变量"attribute1"属于孵化代理。 "ask link-neighbors" 块中的同名变量 "attribute1" 属于 link 邻居。
所以你问 link-邻居声明
set attribute1 (attribute1 + q)
要求每个 link 邻居将自己的 "attribute1" 设置为自己的属性 1 加 q。相反,您想要的是要求每个 link 邻居将自己的属性 1 设置为最近孵化的代理变量 "attribute1" 加上 q。
如果我们有点符号,而我们没有,您会想要像
set neigbhor.attribute1 ( hatched_agent.attribute1 + q )
由于您的询问 link-neighbor 代码超出了孵化代理的范围,因此无法使用 "self" 或 "myself" 之类的东西来指向正确的变量那个名字。
解决这个问题的一种方法是定义一个新的全局变量,可以称之为 "passed-ball"
globals [ passed-ball ]
然后在舱口块内你可以
set passed-ball attribute1
然后在你的 ask link-neighbors 代码块中,你可以说
set attribute1 (passed-ball + q)
或者,您可以使用另一种方法在孵化块和询问之间保留您想要的值,而不是使这个传球完全全局化,这违反了使用尽可能少的全局变量的准则link-neighbors 块:"passed-ball" 可能是代理拥有的变量,它是您发布的代码最顶部引用的变量,您正在询问是否 breed = breed1。
我认为 hatch 代码和 ask-link-neighbors 代码都可以看到并隐式引用这个高级代理拥有的名为 "passed ball" 的变量。只是不要再次将其命名为 "attribute1" 否则将会出现三种不同的 "attribute1" 含义,并且您或后来修改代码的人更有可能出错并参考隐含地给错了。
继我之前的post之后,我想问一下为什么当我试图更新一个变量的值时它被初始化的原因。我正在尝试找到解决此问题的方法。
我想要做的是通过添加一个小的随机量 q = random-float 0.1
来更新变量的原始值,只有当我将 this-item
添加到邻居列表时。
我的预期输出是:
(object 16) with original attribute 0.147 neigh: 0 (object 16) with updated attribute 0.167 with random-float 0.02
即不初始化原始值:
(object 16) with original attribute 0.147 neigh: 0 (object 16) with updated attribute 0.02 with random-float 0.02
后者是我目前的输出,这是错误的。
我不知道如何在我的代码中解决这个问题。我仍在努力了解出了什么问题,但我还没有找到解决问题的方法,所以我希望你能帮助我了解更多。 我的代码如下:
if breed = breed1 [
set selected people
hatch-items 1 [
hide-turtle
set chosen selected
set attribute1 random-float 1
set attribute2 attribute1
set this-item self
print (word " attribute1 " attribute1 " attribute2 " attribute2)
ask myself [
set my-list fput this-item my-list
] ;; close ask myself
] ;; close hatch
; ADD NEW ITEM TO NEIGHBOURS' LISTS ;
print (word " Before updating value, attribute1 " attribute1 " attribute2 " attribute2)
ask link-neighbors with [breed = breed1] [
let q random-float 0.1
set attribute1 (attribute1 + q)
print (word " After updating value, attribute1 " attribute1 " attribute2 " attribute2 " q " q)
] ;; close ask link-neighbors
] ;; close if
我添加了一些打印函数来查看输出:
第一个打印returns我
attribute1 0.85 and attribute2 0.85
print (word " attribute1 " attribute1 " attribute2 " attribute2)
第二个输出returns我
attribute1 0 and attribute2 0
print (word " Before updating value, attribute1 " attribute1 " attribute2 " attribute2)
第三输出returns我
attribute1 0 attribute2 0 and q 0.08
print (word " After updating value, attribute1 " attribute1 " attribute2 " attribute2 " q " q)
发生的事情是 attribute1
在 ask link-neighbors
中初始化,所以我将数量 q
添加到 0
,而不是原始值属性,在影线中定义。
能不能帮我更新一下,不初始化值?
非常感谢
没有初始化。你的问题是你有两个不同的范围。
您在孵化块中设置的变量"attribute1"属于孵化代理。 "ask link-neighbors" 块中的同名变量 "attribute1" 属于 link 邻居。
所以你问 link-邻居声明
set attribute1 (attribute1 + q)
要求每个 link 邻居将自己的 "attribute1" 设置为自己的属性 1 加 q。相反,您想要的是要求每个 link 邻居将自己的属性 1 设置为最近孵化的代理变量 "attribute1" 加上 q。
如果我们有点符号,而我们没有,您会想要像
set neigbhor.attribute1 ( hatched_agent.attribute1 + q )
由于您的询问 link-neighbor 代码超出了孵化代理的范围,因此无法使用 "self" 或 "myself" 之类的东西来指向正确的变量那个名字。
解决这个问题的一种方法是定义一个新的全局变量,可以称之为 "passed-ball"
globals [ passed-ball ]
然后在舱口块内你可以
set passed-ball attribute1
然后在你的 ask link-neighbors 代码块中,你可以说
set attribute1 (passed-ball + q)
或者,您可以使用另一种方法在孵化块和询问之间保留您想要的值,而不是使这个传球完全全局化,这违反了使用尽可能少的全局变量的准则link-neighbors 块:"passed-ball" 可能是代理拥有的变量,它是您发布的代码最顶部引用的变量,您正在询问是否 breed = breed1。
我认为 hatch 代码和 ask-link-neighbors 代码都可以看到并隐式引用这个高级代理拥有的名为 "passed ball" 的变量。只是不要再次将其命名为 "attribute1" 否则将会出现三种不同的 "attribute1" 含义,并且您或后来修改代码的人更有可能出错并参考隐含地给错了。